Вы находитесь на странице: 1из 146

..

..
..
..

2007

004.65

. :
230100 / .. ,
.. , . , .. , .: , 2007.-,
146

1. .
1 .
1 .
1.1 Delphi.
Delphi Borland Database
Engine (BDE) - Borland. BDE . ,
.
. Delphi ,
BDE. , , .1.
Delphi, , BDE
. BDE DLL ( IDAPI01.DLL, IDAPI32.DLL). , , API (Application Program Interfase - ), IDAPI (Integrated Database Application
Program Interfase).
, .
BDE
. - , ,
. BDE , , BDE
,
. BDE -

, Microsoft Access, FoxPro,


Paradox dBase.
BDE ,
ODBC. ODBC (Open Database Connectivity) DLL,
BDE, Microsoft.
ODBC. DLL. Microsoft ODBC ODBC ,
Borland BDE , ODBC.
BDE SQL- , SQL , , Sybase, Microsoft SQL, Oracle, Interbase. / .
1.2 BDE
Data Access Component Palette VCL,
BDE. VCL
: TTable, TQuery TStoredProc.

TDBDataSet, TBDEDataSet ( , , TDataSet). TDataSet , , .
TBDEDataSet , BDE- . TDBDataSet ,
BDE- (
). BDE-
.
TTable ,
, TQuery ,
, SQL-. TStoredProc SQL-.


(DataControls)


(DataSourse)

DataSet -
(Query, Table)

BDE

BD, TBD

. 1 Delphi

2
2.1
(), . :
: TTable, TQuery, TDataSet, TField
: TDBGrid, TDBEdit
: TDataSource
,
. TTable, TQuery, TDataSet TField. Data Access.
- , , .
TDBGrid, TDBEdit,
TDBImage TDBComboBox. Data Controls.
, ,
. TDataSource.
2.2 TDataSet
TDataSet - .
, :

TDataSet
|
TDBDataSet
|
|-- TTable
|-- TQuery
|-- TStoredProc
.2. , TDataSet
6

TDataSet , . TDBDataSet , , ,
. TTable (.. ), , ..
, TQuery SQL .
TDataSet - , , , . , TDataSet. , TTable, TQuery TDataSet (,
TQBE).
, Dataset , .3

. 3. dataset ( N )

Dataset , ,
, .
, Dataset, , . DataSet TTable .

TTable TQuery,
TTable Table1.
, TDataSet. , , Delphi , .
- , , Delphi , TTable
TQuery, TDataSet.
2.3 DataSet
TTable ,
TTable ( RecordCount, ..).
,
TTable , . DatabaseName TableName.
DatabaseName ,
dBase Paradox, (C:\DELPHI\DEMOS\DATA),
(DBDEMOS).
(Alias) Database Engine Configuration. , Active True,
.

Open TDataSet, , :
Table1.Open;


:
Table1.Close;

Active , :
If not(Table1.Active) then Table1.Open;
8

2.4 DataSet
, TDataSource,
TDataSet. DataSource .
DataSource,
TTable TQuery:
property
property
property
property
property
property
property
property
property
property
property

OnOpen
OnClose
BeforeInsert
AfterInsert
BeforeEdit
AfterEdit
BeforePost
AfterPost
OnCancel
OnDelete
OnNewRecord

. BeforePost TDataSource.OnUpdateData,
. , STATE ,
DataSource1.OnUpdateData, Table1.BeforePost.
, TDataSource ,
.
2.5 .
, ,
.
, (. 1):

First

Last

Next

Prior

TDataSet ,
:
procedure First;
procedure Last;
procedure Next;
procedure Prior;
property BOF: Boolean read FBOF;
property EOF: Boolean read FEOF;
procedure MoveBy(Distance: Integer);

(. 2):
2

Table1.First

Table1.Last

Table1.Next

Table1.Prior

Table1. BOF

Table1. EOF
Table1.MoveBy

N .

BOF EOF, , .
MoveBy N .
Table1.Next -

10

Table1.MoveBy(1). , Table1.Prior , Table1.MoveBy(-1).


,
TTable, TDataSource TDBGrid .
DBGrid1 DataSource1 DataSource1 Table1,
:
- DatabaseName ,
( DBDEMOS);
- TableName CUSTOMER.
, TDBGrid
,
(scrollbar).
,
, .
Next Prior, .4

. 4. Next Prior

Next -
:
procedure TForm1.NextClick(Sender: TObject);
begin
end;

11

, :
procedure TForm1.NextClick(Sender: TObject);
begin
Table1.Next;
end;

Prior, ,
:
procedure TForm1.PriorClick(Sender: TObject);
begin
Table1.Prior;
end;

. ,
.
First Last, .5

. 5.

.
procedure TForm1.FirstClick(Sender: TObject);
begin
Table1.First;
end;
procedure TForm1.LastClick(Sender: TObject);
begin

12

Table1.Last;
end;

, . First , Last , Next Prior


.
TDataSet.BOF - read-only Boolean , , . BOF true
, , :
- ;
- TDataSet.First;
- TDataSet.Prior .
- . , Delphi
; First, Delphi . , , : , Prior , , Prior
- BOF True.
Prior,
:
while not Table.Bof do begin
DoSomething;
Table1.Prior;
end;

, , DoSomething

( ).
, Table1.Prior . BOF True . ,
DataSource1.Enabled False , True .
BOF EOF.
, , ,
Dataset:

13

Table1.First;
while not Table1.EOF do begin
DoSomething;
Table1.Next;
end;

, :
while repeat, Table1.Next:
Table1.First;
repeat
DoSomething;
until Table1.EOF;

, ,
, Ctrl-Alt-Del . , , ,
. repeat, DoSomething , . , while repeat . EOF True , , :
- ;
- TDataSet.Last;
- TDataSet.Next .
, MoveBy, N
. , :
MoveBy(2);

, :
MoveBy(-2);

,
DataSet - , ,
,
.
Prior Next - , MoveBy.
MoveTo (i:integer) ( i>0) (
i<0) i .
14

, dsEdit, Post, ,
, .
Cancel . ,
, dsEdit, ,
- (, dsBrowse).
,
. , Prior, , Next,
. , : EOF (end-of-file) , BOF (beginning of
file) . true,
, Last First.
.
CBdep, , Dep , Tablel. :
CBdep.Clear;
Tablel.First;
while not Tablel.EOF do
begin
CBdep.Items.Add(Tablel.FieldByName{'dep').AsString);
Tablel.Next;
end;

CBdep. . ,
, Tablel.EOF.
Dep,
Next .

15

2.6 -

. -
. . ,
. -
. 6.

. 6 -

6 , .

.
. .
:
- , ;
-
;

16

-
- ;
- , .
2.7 -
- (. 7) - , , SQL - .
: , , , .
: , ,
,
, .

. SQL ,
.
. SQL
, .

:
- ,
;
- .
;
- .
, .
, - , .
SQL
. . 7.
17

7 -

(. 8) -. , ,
.
, .

.8 -

18

: -, , -, ,
.

, .

3 .
3.1
. . , .

.
.
, , [I].
: , .

, . , , (
), . 9
: ,
(), . , . . , .

19

( ),
() . () , .. . .
()
. , . 9, 4
.

. 9.

.10, .
,
( ) , ( ) .

(, ,
) .
. 11 .
, , (). ,
. ,
, . 12.
.

20

. 10.

. 11

( , , )
87695

111

85495

86946

112

113

[87495 | 113
(, , )'

21

1006

..

1009
..

1008
..

1005 ..

. 12.


(. relation ) .
.
,


.

. :
- ;
- , ..
(, ..) ;
- ;
- ;
- .
,
(. 13).

16493
16593
16693

01.01.76 111
15.03.75 112

14.04.76 111
. 13.
22

, , , , .
, , ( ). ,
. , . 13, " ".
,
( );
.
. 14 , : , , .

. 14.

{, , , , , .
);
(, !, 2, , 4, );
(, ).
4 .
,
, . , , , .

23

, .. ,
, .
, , ,
.

.

,
, .

. , .
() ( , ..).
, , ,
.

24

YEAR

JOB

CHAI
R

FIO

..

1948

..

22

..

1953

22

..

1945

22

..

1960

22

..

1959

22

..

1960

..

22

2
3

.15. .

,
, : , , , ,
, , . .15. , , ( !),
(.3)

25

PK

()

, . , , () .
,
, .. . , , , , .
,
, .. null.
, , .
, .
. ,
, ,
.
, .
, ,
.
( ), . ,
.

26

4.1
, .4.
( : 0 ; 1- ; 2,3,4,5
).
,
. , .
.
, , ,
(.5).
, .
5 . . ,
,
. .
,
, :
1. . . , , .
2. . , ,
,
, ,
, .
, , : .
, . .

27

, .. , , (.4).
4

..

..

60

..

28

..

32

..

36

..

32

..

24

..

28

..

24

..

60

..

20

..

28

..

32

..

32

..

36

..

32

..

28

..

60

..

..

28

..

..

32

..

..

36

..

..

32

..

..

24

..

..

28

..

..

24

..

..

60

..

..

20

..

..

28

..

..

32

..

..

32

..

..

36

..

..

32

..

29

..

..

..

..

..

..

..

..

..

..

..

10

..

30

10

24

28

28

32

36

32

60

10

10

20

11

28

12

32

13

32

14

36

15

32

16

24

60

24

, , , , , .
.
.
,
, ( , ).

31

4.2
.
, .
,
, . , , , , 1.
1
(2),
(3) . . , 2, 1 , , , . 3,
2 , , . .
. :
.
, ,
() .
, (..6) ,
( ) ,
(,
).
. , , .

(), .7.

32

.
:
( ..,
.., ..) , ,
(). : , .
, ( ,
).
. 7
()

..

..

..

..

..

..

4.3
(1).
,
.
(1)
,
().
, ,
(.. 1) 4.
(2), 1 (),

33

, .
4, 5. 5 ( , , ,
) (, , ),
. , , , . ,
.
(.7).
(3), 2
.
(.6) ,
,
.

: , ,
.
, , , , . (, ) , . , 3, ,
.
()
, .
3.

34

(4 5) , .
,
.
, .
, (.6)
, .5. , (.6) , .5.
.
(5)
, . , ,
5.
(4) 5,
.
,
4, 5.

4.4
: ,
5. , , 5 (
, ).
.
,
, , .
,

35

. .
.
, , (1,2),
,
(, 2), . , 2 ( 2), .
() , 1,
, ,
2, 1.
, , , ,
1 2, 1, 2
, ,

,
- , .
(.5).
1. .
, ()
.
, ,
, ( , ). :
, , , .
2. ,
.

, -

36

, ..
:
(, , , ).

:
( , , ,
, ).
, , , - ( )
. :
( . , , , , - , ).
( , . , ).
, (
),
,
. ,
.
4.5
:
. :
1. ;
2.
;
3. ;
4. ;

37

5. . .;
(ER- ) .
4.5.1 ER-

:
, .

,
: , , , ( )
, . , , ( ,
),
( ).
, .

: , ,
. ,
, . , () .
,
, ()
(, ): . ,

38

, , - .
,
.. () .
(: ).
: ().
ER- .16.
ER- ,
. ,
, , ,
. , ER-
, .

, , , . ,
, () .

.
(:1), ..
.

39

. 16. ER- .

ER- .17. ER ,
, .

40

.17. ER .

41

4.5.2
. ER .
,
, , , ,
.

(). : ,
, . .
, . 18,
, ,
,
, , .

ID_

ID_

. 18. .

.
, ,
. , 42

ID_, ,
. (,
,
).

ID_,
. ,
.

ID_.
.19.

ID_

ID_ (FK)

ID_ (FK)


ID_

ID_



ID_ (FK)
. 19. .

43

4.5.3
,
.
ID_ ID_
,
, .
() ID_
ID_, .
, , , ,
.
,
.
ID_, , .. , ,
, . _ : , ,


.
,
, .
ID_ ,
ID_, ,
.
,
:
1. - ;

44

2._ - ;
3. - ;
4._ - ;
5._ - .
6. (. 20) ,
, .
,
.
7. .
8. ,
(1).
9. ,
(2).
10. , ,
, (3).
() , . , - .
,

. :

(, , , 1 , 0 255;
,
..)
, ,
-

45

. , ,
, . , ,
: .
. ,
. , _
.
, .20 .
SQL (Structured Query Language),
,
. 7

ID_

30

15

20

30

. 8

ID_

ID_

46

. 9

ID_

30

15

20

20

30

. 10

ID_

ID_


1 10

ID_

. 11

ID_

ID_

0 5

,
.

. 20.

47

2.
.
2
5 , ,

5.1 Delphi.
Delphi Borland Database
Engine (BDE) - Borland. BDE .
,
. . Delphi , BDE. , ,
.21.
Delphi, , BDE
. BDE DLL ( IDAPI01.DLL, IDAPI32.DLL). , , API (Application Program Interfase - ), IDAPI (Integrated Database Application Program Interfase). ,
.
BDE
. - , ,
. BDE , BDE
,
. BDE -

48

, Microsoft Access, FoxPro, Paradox


dBase.
BDE ,
ODBC. ODBC (Open Database Connectivity) DLL,
BDE, Microsoft.
ODBC. DLL. Microsoft ODBC ODBC ,
Borland BDE , ODBC.
BDE SQL- , SQL , , Sybase, Microsoft SQL, Oracle, Interbase. / .


(DataControls)


(DataSourse)

DataSet -
(Query, Table)

BDE

BD, TBD
.21 Delphi

TTable ,
, TQuery ,
, SQL-. 49

TStoredProc SQL-.
bl
, . TQuery
.
5.2
,
Post, Insert, Delete .., . , , ,
, .. .
, .

.
CachedUpdates Table. false . true,
. Apply -Updates.
CancelUpdates, , ApplyUpdates, .
CommitUpdates ,
.
, ,
. 9.43. .
. . , , , . ,

.
. Tablel
CachedUpdates true. modif, . :

50

const modif:boolean = false;

OnClick
:
Tablel.CachedUpdates:= not Tablel.CachedUpdates;
BApplyUpdates.Enabled:= Tablel.CachedUpdates;
BCancelUpdates.Enabled:= Tablel.CachedUpdates;
if Tablel.CachedUpdates then modif:=false;

, ( BApplyUpdates) ( BCancelUpdates) ,
, false modif.
AfterEdit Tablel :
if Tablel.CachedUpdates then modif:=true;

modif .
OnClick
Tablel.ApplyUpdates; Tablel.CommitUpdates; modif :=false;

OnClick
Tablel.CancelUpdates;
modif:=false;

modif false,
.
OnCloseQuery :
if Tablel.CachedUpdates and modif "
then case MessageDlg(' ?',
mtConfirmation,mbYesNoCancel,0) of mrYes: Tablel.ApplyUpdates;
mrCancel: CanClose:=false;
mrNo: Tablel.CancelUpdates;
end;


?. 51

ApplyUpdates.
CancelUpdates. Cancel, (CanClose
false).
,
.

6
6.1 TDataSet
TDataSet - .
, (. 22):
TDataSet
|
TDBDataSet
|
|-- TTable
|-- TQuery
|-- TStoredProc
. 22 , TDataSet

TDataSet , . TDBDataSet , , ,
. TTable (.. ), , ..
, TQuery SQL .
TDataSet - , , , . , TDataSet. , TTable, TQuery TDataSet (, TQBE).
, TDataSet, .
, Dataset , .23

52

. 23 - dataset ( N )

Dataset , , , .
, dataset,
, (join) .
DataSet TTable .
TTable TQuery,
TTable Table1.
, TDataSet. , , Delphi , .
- , , Delphi , TTable TQuery,
TDataSet.
6.2 DataSet

DataSet.

53

TTable ,
TTable ( RecordCount, ..).
, TTable , . DatabaseName TableName.
DatabaseName ,
dBase Paradox (, C:\DELPHI\DEMOS\DATA), (DBDEMOS). (Alias) Database Engine Configuration. , Active True,
.

Open TDataSet, , :
Table1.Open;


:
Table1.Close;

Active , :
If not(Table1.Active) then Table1.Open;

6.3 DataSet
, TDataSource,
TDataSet. DataSource
. DataSource,
TTable TQuery:
property
property
property
property
property

OnOpen
OnClose
BeforeInsert
AfterInsert
BeforeEdit
54

property
property
property
property
property
property

AfterEdit
BeforePost
AfterPost
OnCancel
OnDelete
OnNewRecord

. BeforePost TDataSource.OnUpdateData, . , STATE ,


DataSource1.OnUpdateData Table1.BeforePost. , TDataSource ,
.
6.4 ,

, .
Table.
Table Data Access.
DataSource, . ,
,
. DBGrid Data Control. , . ,
Align alClient (.24)

.24
55

, .23. DBGrid DataSource.


DBGridl DataSource . ,
.
DataSourcel.
DataSource. . DataSourcel DataSet. Tablel ( , ).
Tablel . Table: DatabaseName
TableName. DatabaseName,
BDE . dbP, . ,
..
TableName.
, .
.
. Active. false.
true. ,
DBGridl (. 25) .

56

.25 Table

, , , , . , ( ). ,
Pers dbP Num, (. 9.2.2).
, Dep, Dep, Dep, Dep (. 9.2.3.2 ). ,
, ,
Stop On Delphi Exceptions Language Exceptions
(. 2.5.10.8).
, Active =
true , .
Active = false, OnCreate
true, OnDestroy false.

57

, ,
.
. ,
.
, ReadOnly DBGridl true. Options dgEditing false.
. Options .
Delphi.
Table Exclusive. (,
). true, .
Active = false.

, DBNavigator, Data Control .
Align DBGridl alBottom, DBNavigator (. .).
, . , :
nbFirst

nbPrior

nbNext

nbLast

nblnsert
nbDelete
nbEdit

nbPost

nbCancel

58

nbRefresh ,
VisibleButtons,
. , , false nblnsert. , nbFirst, nbPrior, nbNext nbLast,
.
, DataSour ( DataSource).

7 ,
7.1
TField
TStringField, TSmallintField, TBooleanField .. :

(Table .)


TField , : . ( ).
. . , , , . ,
, , .
TField .
59

:
-
-
-

Fields[i:integer], i . , Delphi,
0. , Tablel.FieldsfO] Tablel.

FieldByName('<HMH>'). , Tablel. FieldByName('Fam') ,
Fam.
,
. Delphi
(Name) . , TablelDep.
, . ,
. , , . TablelDep .

Name . .
,
, . , . ,
, Fields[i] ( i ), FieldByName(s) ( s
).
TField . Read-Only, DisplayLabel, CustomConstraint
. , .
Value. . , Tablel.FieldByName('Fam').Value ,
a Tablel.FieldByName('Year_b').Value .

60

, .
, AsString ,
. ,
EDep.text := Tablel.FieldByName('Dep').AsString;
EYear.text := Tablel.FieldByName('Year_b').AsString;
ESex.text := Tablel.FieldByName('Sex').AsString;

EDep, EYear ESex Dep, Year_b Sex, Dep


, Yearjb , Sex .
Sex DisplayValues, ESex
true false.
DisplayValues, ; ; ,
.
AsString . ,
EDep, EYear ESex, , , :
Tablel.Edit;
Tablel.FieldByName('Dep').AsString:=EDep.text; Tablel.FieldByName('Year_b').AsString:=EYear.text; Tablel.FieldByName('Sex').AsString := ESex.text; Tablel.Post;

Year_b Sex . ,
ESex .
: t f, true false, , .
,
( Edit),
, . ,
, ,

61

. .
AsString Aslnteger, AsFloat, AsBoolean, AsDateTime. Aslnteger 32- , AsFloat ,
As-Boolean , AsDateTime
Delphi TDateTime.
. DataType,
: ftUnknown (), ftAu-tolnc (
), ftString () ..
7.2
, , . (calculated fields). ,
.
Table1, .
New
( ). , .
Field properties ( )
(Name) Age, () Smallint, (Size). .
, Field type Calculated ( ).
,
Age.
DisplayLa-bel .

62

Age, ,
. , , Tablel,
OnCalcFields. , .

:
TableiAge.Value := 1999 TablelYear_b.Value;

TableiAge.Value TablelYear_b.Value
Age Year_b . ,
1999 , . , , . :
procedure TForml.TablelCalcFields(DataSet: TDataSet) ;
var Year, Month, Day: Word;
begin
DecodeDate(Date,Year,Month,Day);
TableiAge.Value := Year - TablelYear_b.Value;
end;

Year, Month Day , . DecodeDate , TDate-Time ( Delphi ), , .


Date, . Year ( Month Day ,
DecodeDate).
, .
, BeforePost .
, MessageDlg ShowMessage, ,
. , :

63

- , , , ,
.
. ,
.
7.3
, , . -, Num, ,
.
,
. Num, Fam .., ,
-. Sex true false,
, , .
. .
Tablel.
IndexName IndexFieldName. , . , , fio, ,
, Fam, Nam Par. depfio ,
. IndexFieldName. , ,
.
.
.
Tablel. .
Add fields...
( ). .
Ctrl,
. , , ,
, . -

64

, Charact, Photo , , Num. ,


,
.
. , - . , ,
, .
- . , ,
: TStringField, TSmallintField, TBooleanField ..
TField .
. , .
Alignment
: , .
DisplayLabel .
, Fam DisplayLabel , Nam ..
DisplayWidth .
EditMask EditFormat
.
( Sex)
Display-Values. ,
, true false. . , true. : ; ;.
ReadOnly, true,
. Visible ,
. , , Visible = false Num.,
Tablel.

65

,
Delphi2,
. - , , Fam,
. TDBEdit
, DisplayLabel. Sex
TDBCheckBox. Cha-ract Photo

TDBMemo TDBI-mage. ,
Active TTable true, .
7.4

. Delphi
. , , . .
,
, Table
. MinValue Max Value, . ,
,
Year_b MinValue = 1970 MaxValue = 1980,
EDatabaseError.
CustomConstraint ConstraintError-Message. CustomConstraint SQL (.
10.1). , Year_b
CustomConst-raint:
X < 1980 and X > 1970

66

( X)
.
CustomConstraint,
Constrain tErrorMessage. ,
, , . ,
. , MinValue MaxValue, .
.
On Validate. . ,
, , EAbort
Abort. , OnValidate OnChange, .

. ,
. Constraints Table. , , . 9.29. Add Constraints . . ,
. CustomConstraint SQL, . ErrorMessage , . ,
CustomConstraint :
((Sex=true) and (Year_b >1955)) or ((Sex=false) and
(Year_b>1965))

ErrorMessage: " >1955 ..


>1965 .." .

67

Table, .

8
8.1 SetKey
, ,
Delphi SetKey GotoKey.
, . Delphi
SEARCH, , .
SEARCH (. 26), TTable,
TDataSource, TDBGrid, TButton, TLabel TEdit ,
.6. Search, , DBGrid1 Customer.

. 26 - SEARCH

SEARCH
, Search. , , CustNo, -

68

. , Search :
procedure TSearchDemo.SearchClick(Sender: TObject);
begin
Table1.SetKey;
Table1.FieldByName(CustNo).AsString := Edit1.Text;
Table1.GotoKey;
end;

Table1 .
Delphi , ,
Fields . ,
Fields , . Table1.GotoKey.
, , IndexName. , Customer City,
IndexName .
, :
Table1.IndexName := CityIndex;
Table1.Active := True;
Table1.SetKey;
Table1.FieldByName(City).AsString := Edit1.Text;
Table1.GotoKey;

: , (- IndexName). , , IndexName - TTable, TDataSet TDBDataSet.


, , . Delphi exception, ,
:
procedure TSearchDemo.SearchClick(Sender: TObject);
begin
Cust.SetKey;
Cust.FieldByName('CustNo').AsString:= CustNoEdit.Text;
if not Cust.GotoKey then

69

raise Exception.CreateFmt('Cannot find CustNo %g', [CustNo]);


end;

, , , Cannot find
CustNo %g.
,
, GotoKey GotoNearest.
8.2
FindNearest() SetKey ().
.GotoNearestf) ,
. ,
(
) 123, :
Tablel.FindNearest([123]);

-, FindNearest()
array of const, , .
SetKey (). .GotoNearest() :
with Tablel do begin
SetKey;
Fields[O].AsInteger := 123;
GotoNearest; end;

KeyExclusive False,
.
KeyExclusive True, ,
, .
,
FindKey() FindNearest(),

70

SetKey(), GotoX() , , .
8.3 (Refresh)
, ,
. ,
, . , TTable, , ,
, .
, ,
.
Refresh Open, , , .
, , Delphi . , , Delphi
.
, ,
. , Refresh, Close
Open.
, , TTable
. , a , , ,
Refresh. , , Refresh . , , , .
8.4 (Bookmarks)

, .
Delphi , .

71

function GetBookmark: TBookmark;


( )
procedure GotoBookmark(Bookmark: TBookmark);
( )
procedure FreeBookmark(Bookmark: TBookmark);
( )

, GetBookmark
TBookmark. TBookmark ,
Delphi
TBookmark. TBookmark
GotoBookmark, , .
, GetBookmark
TBookmark, FreeBookmark
, Tbookmark
( GetBookMark).

, . ,
( TDBGrid), .
StartBookmark , StopBookmark
, .
var SaveRecPos: TBookmark;
procedure TFormI.FormShow(Sender: TObject);
begin
try Tablel.Open;
BookmarkControl.Brush.Color := clBtnFace;
except ShowMessage(' ');
end;
end;
procedure TFormI.FormClose(Sender: TObjeCt; var Action:
TCloseAction) ;
begin
try
finally Tablel.Open;
end;
end;
procedure TForml.StartBookmarkClick(Sender: TObject);

72

begin
if Not Tablel.BookmarkValid(SaveRecPos)
then SaveRecPos := Tablel.GetBookmark;
BookmarkControl.Brush.Color := cILime
end;
procedure TForml.StopBookmarkClick(Sender: TObject);
begin
with Tablel do
begin
if Not BookinarkValid( SaveRecPos) then Exit;
GotoBookmark(SaveRecPos);
FreeBookmark(SaveRecPos);
end;
BookmarkControl.Brush.Color := clBtnFace;
end;

Bookmarkvalid , , . BookmarkControl TShape , .


TDBGrid. SelectedRows TBookmarkList,
, .

9
, . Delphi
.
9.1 c OnFilterRecord
Delphi :
OnFilterRecord (TDataSet),
, Filtered
True.
. , Accept
True. DataSet , .
Filtered: Boolean ,
( False) (True).

73

,
country.db, CONTINENT South America,
:
procedure TForm1.Table1FilterRecord(DataSet: TDataSet; var
Accept: Boolean);
begin
Accept:=(DataSet['Continent']='South America');
end;
:
procedure TForm1.Table1FilterRecord(DataSet: TDataSet; var
Accept: Boolean);
begin
if DataSet.FieldByName('Continent').AsString='South
America' then
Accept:=True else Accept:=False;
end;

9.2 Filter
Filter
,
(>, >=, <, <=, =, <>) (AND, OR,
NOT).
, , Filter :
Continent=South America

Filtered Table1 True.



, . ,
, .
Filtered False,
True, :
procedure TForm1.Button1Click(Sender: TObject);
begin
Tablel.Filtered:= False;
Tablel.Filter := Continent=+Edit1.Text;

74

Tablel.Filtered := True;
end;


, (*). , S:
Country=S*

9.3
ApplyRange , . , Employee,
EmpNo 2 500. , 100
200, ApplyRange,
. .
,
:
- SetRangeStart;
- SetRangeEnd;
-ApplyRange;
-CancelRange;
, TTable
:
-EditRangeStart;
-EditRangeEnd;
-SetRange;
:
1. SetRangeStart Fields .
2. SetRangeEnd Fields .
3. , , ApplyRange, .
4.
CancelRange.

75

:
Table1.SetRangeStart;
Table1.Fields[0].Value := 100;
Table1.SetRangeEnd;
Table1.Fields[0].Value := 200;
Table1.ApplyRange;


SetRange:
Table1.SetRange([100], [200]);
Table1.ApplyRange;

, , KeyExclusive. True, , .
9.4
SQL

. , , , ,
OnFilterRecord. , , ,
OnFilterRecord
. , OnFilterRecord ,
,
.

76

2. 2 SQL-
10 SQL: , ,

10.1 SQL- Delphi
10.1.1 TQuery
TQuery , TTable TDataset.
TDataset ( DatabaseName, SessionName .). TTable, , TQuery
. , TQuery
TTable, .
10.1.2 SQL
SQL TQuery TStrings,
, .
( ). ,
SQL , TQuery .
SQL .
SQL TQuery :
- (Alias) DatabaseName.
, DBDEMOS.
- SQL, SQL- SELECT * FROM
Country.
- Open.

77

SQL . SQL- Memo1:


procedure TMainForm.Button1Click(Sender: TObject);
begin
try
if Query1.Active then Query1.Close;
Query1.SQL.Clear;
Query1.SQL.Assign(Memo1.Lines);
Query1.Open;
except
one : EDBEngineError do ShowMessage('!');
end;
end;

10.1.3 Params
, SQL.
, . ,
SQL-. Delphi, SQL- . . (:), . SQL :
SELECT * FROM Employee
WHERE Salary = :Salary

, , Params Query1.
'...'. . Salary. Integer.
, ,
"Value".
SQL- , (bind). , , . Params

78

ParamByName. Params TParams. , .


,
Query1.Params[0].asInteger := StrToInt(Edit1.Text);

asInteger Integer ( ). ,
Integer. , VARCHAR(10), Delphi . ,
:
Query1.Params[0].asString := Edit1.Text;

, ParamByName.
TParam . :
Query1.ParamByName(Salary).asInteger :=
StrToInt(Edit1.Text);

,
. Prepare, Delphi SQL . .
. . ,
, .
TQuery TTable - TDataSource.
. -, TDataSource OnDataChange. ,
OnDataChange :
procedure TForm1.DataSource1DataChange(Sender: TObject;
Field: TField);
begin
if Query1.Active then Query1.Close;
if not(Query1.Prepared) then Query1.Prepare;
Query1.ParamByName('Salary').asInteger := Table1Salary.asInteger;

79

try
Query1.Open;
except
one : EDatabaseError do
MessageDlg(e.Message, mtError, [mbOK], 0);
end;
end;

OnDataChange , Query . TQuery


DataSource. TDataSource DataSource, TQuery
SQL- TDataSource.
, .
, DataSource
.
Query1 SQL :
SELECT * FROM Employee
WHERE Salary = :Salary

DataSource , Table1 (DataSource2 ). Active True.


10.1.4 Format
Format (%s, %d, %n
.) ( C). ,
Format('SELECT * FROM %s', ['Employee'])


SELECT * FROM Employee

. ,
. ,
tblName := 'Employee';
fldName := 'EmpNo';
fldValue := 2;
80

Format('SELECT * FROM %s WHERE %s=%d', [tblName, fldName,


fldValue])


SELECT * FROM Employee WHERE EmpNo=2


.
10.2 SQL Monitor
SQL Monitor (. 27) , BDE,
, . SQL Monitor Database Delphi. SQL
Monitor . SQL- ,
, -
SQL-. , ,
. .27
, .

. 27 SQL Monitor
81

10.3 SQL Explorer


SQL Explorer (.28)
. Database
Explore .
Explorer Databases
BDE .
, SQL- ,
( , ,
, BDE),
( , , .) , , .

. 28 SQL Explorer

TQuery SQL
. Delphi
TQuery
(, Paradox dBASE), SQL.
DatabaseName TQuery ,
SQL SQL, -

82

. , Queryl
DBDEMOS, BIOLIFE, Length (cm) 100:
select * from BIOLIFE where BIOLIFE."Length (cm)" > 100

, , Active True Open (). ,



(, insert into),
ExecuteSQL(), ().
TQuery
RequestLive. ,
. , True.
RequestLive ,
.
, BDE , . , ,
having, to_date
(ADT Abstract Data Type), , . (
BDE.) , ,
CanModif .

TQuery, SQL-.

83

11 ,

11.1 SQL.
11.1.1 SQL
Structured Query Language (SQL)
, .
, , , . SQL ,
.
, SQL :
- (Data Manipulation Language, DML)
- (Data Definition Language, DDL)
- (Data Control Language, DCL).
11.1.2
SQL-. Database Desktop
SQL-
, ,
. ,
, , SQL. Query
Delphi, SQL-,
SQL- . , SQL,
! SQL SQL-,
.

84

11.2 SQL
TQuery, . DatabaseName
. SQL- SQL.
, , , ExecSQL TQuery.
, (.. , SELECT), Open
TQuery. , BDE
SELECT , ( . , TQuery).
SQL- SQL- InterBase (
online- SQL, InterBase):
CREATE TABLE table
(<col_def> [, <col_def> | <tconstraint> ...]);

table - ,
<col_def> - ,
<tconstraint> - / (
[] , | ).
(
- . 9), , :
<col_def> = col {datatype | COMPUTED BY (<expr>) | domain}
[DEFAULT {literal | NULL | USER}]
[NOT NULL] [<col_constraint>]
[COLLATE collation]

col - ;
datatype - SQL- ( InterBase
SMALLINT, INTEGER, FLOAT, DOUBLE PRECISION,
DECIMAL, NUMERIC, DATE, CHAR, VARCHAR, NCHAR, BLOB), -

85

CHARACTER SET - , .
WIN1251;
COMPUTED BY (<expr>) - , <expr> - SQL-, ;
domain - ( ), ;
DEFAULT - , ;
NOT NULL - , ,
;
COLLATE - , ( BLOB ).
WIN1251 2 - WIN1251
PXW_CYRL. , , PXW_CYRL.
/
CONSTRAINT , , , , CHECK (
, ,
):

<tconstraint> = [CONSTRAINT constraint <tconstraint_def>]


<tconstraint>

<tconstraint_def> = {{PRIMARY KEY | UNIQUE} (col[,col...])


| FOREIGN KEY (col [, col ...]) REFERENCES other_table
| CHECK (<search_condition>)}
<search_condition> =
{<val> <operator> {<val> | (<select_one>)}
| <val> [NOT] BETWEEN <val> AND <val>
| <val> [NOT] LIKE <val> [ESCAPE <val>]
| <val> [NOT] IN (<val> [, <val> ...] |
<val> = {
col [<array_dim>] | <constant> | <expr> | <function>
| NULL | USER | RDB$DB_KEY } [COLLATE collation]

86

<constant> = num | "string" | charsetname "string"


<function> = {
COUNT (* | [ALL] <val> | DISTINCT <val>)
| SUM ([ALL] <val> | DISTINCT <val>)
| AVG ([ALL] <val> | DISTINCT <val>)
| MAX ([ALL] <val> | DISTINCT <val>)
| MIN ([ALL] <val> | DISTINCT <val>)
| CAST (<val> AS <datatype>)
| UPPER (<val>)
| GEN_ID (generator, <val>)
}
<operator> = {= | < | > | <= | >= | !< | !> | <> | !=}

<select_one> = SELECT , .
, .
SQL .
SQL.
A: PRIMARY KEY

CREATE TABLE REGION (


REGION
REGION_NAME NOT NULL PRIMARY KEY,
POPULATION
INTEGER NOT NULL);

, REGION_NAME,
, :
CREATE DOMAIN REGION_NAME
AS VARCHAR(40) CHARACTER SET WIN1251 COLLATE PXW_CYRL;

B: UNIQUE ,

CREATE TABLE GOODS (
MODEL
SMALLINT NOT NULL UNIQUE,
NAME
CHAR(10) NOT NULL,
ITEMID
INTEGER NOT NULL, CONSTRAINT MOD_UNIQUE
87

UNIQUE (NAME, ITEMID));

C: , CHECK,
CREATE TABLE JOB
(
JOB_CODE JOBCODE NOT NULL,
JOB_GRADE JOBGRADE NOT NULL,
JOB_REGION
REGION_NAME NOT NULL,
JOB_TITLE
VARCHAR(25) CHARACTER SET WIN1251 COLLATE
PXW_CYRL NOT NULL,
MIN_SALARY
SALARY NOT NULL,
MAX_SALARY
SALARY NOT NULL,
JOB_REQ
BLOB(400,1) CHARACTER SET WIN1251,
LANGUAGE_REQ
VARCHAR(15) [5],
PRIMARY KEY (JOB_CODE, JOB_GRADE, JOB_REGION),
FOREIGN KEY (JOB_REGION) REFERENCES REGION (REGION),
CHECK (MIN_SALARY < MAX_SALARY));

,
(). JOBCODE, JOBGRADE,
REGION_NAME SALARY. LANGUAGE_REQ, 5 VARCHAR(15). ,
JOB_REQ, BLOB 1 ( )
400. ,
JOB_CODE, JOB_GRADE JOB_REGION. ,
(JOB_REGION), REGION REGION. ,
, CHECK,
.
D:
CREATE TABLE SALARY_HISTORY (
EMP_NO
EMPNO NOT NULL,
CHANGE_DATE
DATE DEFAULT "NOW" NOT NULL,
UPDATER_ID
VARCHAR(20) NOT NULL,
OLD_SALARY
SALARY NOT NULL,
PERC_CHANGE
DOUBLE PRECISION DEFAULT 0 NOT NULL
CHECK (PERC_CHANGE BETWEEN -50 AND 50),
NEW_SALARY COMPUTED BY
(OLD_SALARY + OLD_SALARY * PERC_CHANGE / 100),
PRIMARY KEY (EMP_NO, CHANGE_DATE, UPDATER_ID),
FOREIGN KEY (EMP_NO) REFERENCES EMPLOYEE (EMP_NO));

88

, ( ) NEW_SALARY, (OLD_SALARY
PERC_CHANGE).
11.3
, , SQL. , , Database Desktop,
.

12
SQL
12.1
12.1.1 Select

SELECT,
. :
SELECT _
FROM _
[WHERE _]
[ORDER BY _]

SELECT SELECT FROM;


, WHERE ORDER BY, .
SELECT ,
. (*)
, :
SELECT *

89

SELECT Capital

:
SELECT Name, Capital, Continent


, ,
, :
SELECT Customers.Name, Country. Name

12.1.2 From
, , FROM, ,
Country, :
SELECT * FROM Country


Country.

CompanyName ContactName,
SELECT:
SELECT Name, Capital FROM Country

:
SELECT Country.Name, Customers.FirstName
FROM Country, Customers

12.1.3 Where
, SELECT,
WHERE, :
WHERE BOOL1 [{AND | OR} BOOL2 []]

90

, , Population 800000:
SELECT * FROM Country
WHERE Population = 800000

WHERE ,
:
SELECT * FROM Country
WHERE Continent = South America AND Area > 100000

:
SELECT Name, Capital FROM Country
WHERE Continent <> South America OR Population <=
10000000

: =, <, >, >=, <=, <>, :


BETWEEN , :
SELECT Name, Continent
FROM Country
WHERE Name BETWEEN C AND D

Name Continent Country , .



LIKE, :
SELECT Name, Continent
FROM Country
WHERE Name LIKE C%

% () , _ () , .. .
, , si,
:
91

SELECT *
FROM Country
WHERE Capital LIKE %si%

IN, ,
:
SELECT Name, Continent
FROM Country
WHERE Capital IN (Brasilia, Santiago, Quito)
:
SELECT Name, Continent
FROM Country
WHERE Capital = Brasilia
OR Capital = Santiago
OR Capital = Quito

12.1.4 Order By
ORDER BY () .
ASC (
) DESC ( ).
. ORDER BY :
ORDER BY 1 [{ASC | DESC}]
[, 2 [{ASC | DESC}] [,]

,
SQL-:
SELECT LastName, FirstName, Salary
FROM Employee
ORDER BY LastName, FirstName

, DESC:
SELECT LastName, FirstName, Salary
FROM Employee
ORDER BY LastName DESC, FirstName DESC

92

12.2 SQL-

:
SELECT first_name, last_name, dept_no,
job_code, salary
FROM employee
ORDER BY last_name

, :
FIRST_NAME
============
Janet
Oliver H.
Ann
Dana
Kelly
Jennifer M.
Kevin
Roger
Roberto

LAST_NAME
=============
Baldwin
Bender
Bennet
Bishop
Brown
Burbank
Cook
De Souza
Ferrari

DEPT_NO
=======
110
000
120
621
600
622
670
623
125

JOB_CODE
SALARY
======== ===========
Sales
61637.81
CEO
212850.00
Admin
22935.00
Eng
62550.00
Admin
27000.00
Eng
53167.50
Dir
111262.50
Eng
69482.62
SRep
99000000.00

SELECT first_name, last_name, dept_no,


job_code, salary
FROM employee
ORDER BY last_name DESC

, ,
:
FIRST_NAME
============
Katherine
Bruce
Michael
Takashi
Randy
K. J.
Claudia
Walter
Willie
Roger

LAST_NAME
=============
Young
Young
Yanowski
Yamamoto
Williams
Weston
Sutherland
Steadman
Stansbury
Reeves

DEPT_NO
=======
623
621
100
115
672
130
140
900
120
120

93

JOB_CODE
SALARY
======== ===========
Mngr
67241.25
Eng
97500.00
SRep
44000.00
SRep
7480000.00
Mngr
56295.00
SRep
86292.94
SRep
100914.00
CFO
116100.00
Eng
39224.06
Sales
33620.62

, ,
():
SELECT first_name, last_name, dept_no,
job_code
FROM employee
ORDER BY salary

, :
FIRST_NAME
===============
Ann
Kelly
Sue Anne
Mark
Roger
Bill

LAST_NAME
===============
Bennet
Brown
O'Brien
Guckenheimer
Reeves
Parker

DEPT_NO
=======
120
600
670
622
120
623

JOB_CODE
========
Admin
Admin
Admin
Eng
Sales
Eng

BETWEEN , true.
NOT BETWEEN.
SELECT first_name, last_name, salary
FROM employee
WHERE salary BETWEEN 20000 AND 30000

, 20000
30000:
FIRST_NAME
===============
Ann
Kelly

LAST_NAME
SALARY
========== ===============
Bennet
22935.00
Brown
27000.00


:
SELECT first_name, last_name, salary
FROM employee
WHERE salary >= 20000
AND salary <= 30000

, 20000
30000:
94

FIRST_NAME
===============
Ann
Kelly

LAST_NAME
SALARY
========== ===============
Bennet
22935.00
Brown
27000.00

BETWEEN :
SELECT first_name, last_name, salary
FROM employee
WHERE last_name BETWEEN "Nelson" AND "Osborne"

,
Nelson Osborne
FIRST_NAME
===============
Robert
Carol
Sue Anne
Pierre

LAST_NAME
SALARY
=============== ================
Nelson
105900.00
Nordstrom
42742.50
O'Brien
31275.00
Osborne
110000.00

, , . -
.
SELECT first_name, last_name, salary
FROM employee
WHERE last_name BETWEEN "Nel" AND "Osb"

,
Nel Osb
FIRST_NAME
===============
Robert
Carol
Sue Anne

LAST_NAME
SALARY
=============== ================
Nelson
105900.00
Nordstrom
42742.50
O'Brien
31275.00

Nel Osb . ,
, , Nel (.. ),
Osb (..
- O, Os, Osb), . ,

95

BETWEEN , , .

96

13
Delphi , QReport, Delphi ,
Rave Reports v5.0. ,
QReport - DCLQRT70.BPL C:\Program Files\Delphi7\Bin .
13.1 QReport
QReport
, .
, ,
QuickRep.
, , , ,
.
QRBand, .
QRBand , , .
QRSubDetail .
. True HasChild.
QRExpr ,
. .
QRSysData : ,
, ..
QRLabel, QRMemo, QRRichText, QRShape, QRImage,
,
97

- Label, Memo, RichText, Shape, QRImage. QuickReport


QRBand, . , : Frame Size.
Frame ,
: Color - , Style - , Width - , DrawBotton,
DrawLeft, DrawRight, DrawTop -
, , .
Size , . ,
Units QuickRep.
AlignToBand -
. true,
, Alignment: taLeftJustify - ,
taCenter - , taRightJustify - .

13.2 QuickRep
, . .
QuickRep:
Bands: TQuickRepBands boolean, True :
HasColumnHeader ;
HasDetail ;
HasPageFooter ;
HasPageHeader ;
HasSummary ;
HasTitle .
Dataset: TDataSet (TTable, TQuery),
.
QuickRep:

98

procedure Prepare; - , ;
procedure Preview; - ;
procedure Print; - ;
:
procedure TForm1.Button1Click(Sender: TObject);
begin
try
QuickRep1.Prepare;
QuickRep1.QRPrinter.Save(MyReport.qrp);
finally
QuickRep1.QRPrinter.Free;
end;
end;


, ,
Preview QuickRep. Report Settings.

13.3 QRBand
QRBand ,
QRLabel, QRDBText, QRImage ..
:
BandType TQRBandType .
Frame: TQRFrame - , , , .

13.4 QRExpr
, ,
- , ., QRExpr.

99

QRExpr Expression Wizard, Object Inspector


[] Expression (. 29)

. 29 - Expression Wizard

Database field ,
, Function
, ..
Variable ( , .), .
Validate .

Area
MAX(Table1.Area)

Population
SUM(Table1.Population)

: : N
': '+PAGENUMBER

100

13.5 TQRSysData
TQRSysData
.
property Data: TQRSysDataType;
:
qrsDate ;
qrsDateTime ;
qrsDetailNo ;
qrsPageNumber ;
qrsPageCount ;
qrsTime
property Text: TCaption , .
13.5 QReport
. ,
,
. , , .
1. .
2. QuickRep Bands
True HasDetail - , , HasPageFooter - .
HasTitle True, , . ,
,
. ,
.

101

true HasDetail HasPageFooter,


QuickRep
"Detail " "Page Footer ".
1.
QRImage. , Picture
- .
2. QRLabel,
Caption.
Alignment taCenter AlignToBand true. .
QRLTitle. .
3. QRRichText,
. -
Lines.
" "
. QRRichText,
.
. .
4. QRSubDetail.
AligneToBottom true.
.
5. Sub Detail QRLabel,
Caption .
6. QRSysData. Data
qrsData. .
, , . , ,
QRSysData QRExpr . ,
. ,
Copy.
.
7. Page Footer QRLabel,
Caption (, Delphi). Alignment taLeftJustify AlignToBand true. -

102

.
Frame DrawTop true. , .
8. Page Footer QRSysData.
Data qrsPageNumber.
. Alignment taRightJustify AlignToBand true.
.
.
QuickRep Preview.
, .
, .
, ,
. QRRichText. . , , .
1. FRep ( Name).
.
2. , Visible false.
, .
3. , URep.
.
4. File/New .
5. Project/Options, Forms Main Form
Form1. FRep .
6. , URep1.
URep.
7. MainMenu,
FontDialog, RichEdit, Edit Button. , .
Lines RichEdit ,

103

, , QRRichText. Edit .
.
8. URep implementation
uses URep1;


.
9. URep1
implementation
uses URep;

10. QRRichText URep


ParentRichEdit.
RichEdit Form1. ,
.
11. OnClick Form1

FRep.QRLTitle.Caption:=Edit1.Text;

QRLTitle , Edit1.
1.
if FontDialog1.Execute then
with RichEdit1.SelAttributes do begin
Color:=FontDialog1.Font.Color;
Name:=FontDialog1.Font.Name;
Size:=FontDialog1.Font.Size;
Style:=FontDialog1.Font.Style;
end;
RichEdit1.SetFocus;

2.
FRep.QuickRep1.Preview;

104

. .
. .
, ,
, ( ,
), , ,
, , -
.

105

3.
-
1. -

14.
,
, -.
- , , .
. .

-, , , .
. - .
- (.30) - - -. -
, -. - ,
-. , - , , - .

106

. 30. -.

, - - .
, .
14.1 -
, -
, , . , , , - .
,
. ,
, . , , . , .
- ( ) , .
, , - .
, , . :
10 .
. 6 , - 7 . , .
, ! ! 107

! ? ,
, , -
, .
. .
,
, , . .
.
, , , . , , . ,
- .
. , , . , , , .. .
, -
. -?
- ,
,
. ,
.
, , , ,
- , (-) , (). , .

108

14.1.1
- ,
. ,
, . , - :
1. ;
2. ;
3.
;
4.
;
5. ;
6. .
.

. , . ,
(, , -) , . , ,
, .
, ,
.
. , , . ,
, . , ,
.

109

- . . ,
, ( , ). , -

. .
. . - , -
, -
, (.
). , , -, , , , , .

. ,
,
. , ,
. , , -,
,
- , , , .
,
, , . ,
, .
. ,

110

,
. , , . .
, , . ,
, .
.
, ,
.
. , , , , .
, ,
.

. ,
, ..
, , , .

, . ,
- , , .
,
.
.
, , , -

111

, () ..
14.1.2 -
, -
. , - ( ,
). , , - . , , - .
-,
, . , , .
, = , .
14.2 -

: TIBDatabase, TIBtransaction.
14.2.1 TIBDatabase
, .

12.

112

12

TIBDataBase

DataBaseName

String

Params

TStrings

DefaultTransaction TIBTransaction
Connected

Boolean

Params , 13.
13
.


User
name

User_name = SYSDBA

Password
lc_ctype

Password = masterkey

- lc_ctype=WIN1251

TIBDatabase
:
procedure TForm1.DBConnect(server_name, database_name, user_name, password: string);
begin
try
IBDatabase1.DatabaseName:=server_name+':'+database_name;
//
with IBDatabase1.Params do
begin
113

Clear; //
Add('user_name='+user_name); //
Add('password='+password); //
end;
IBDatabase1.Connected := true; //
except
on e:exception do //
begin
ShowMessage('
:'+#13+e.Message);
Exit;
end;
end;
end;

14.2.2 TIBTransaction
. ,

. ,
.

, , . 31.

114

.31. .

:
Snapshot , ;
Read Committed , , . ;
Read-Only Table Stability
,
,
;
Read-Write Table Stability
,
, .

Read Committed.
DefaultDatabase: TIBDataBase.

115

14.2.3 TIBTable
TIBTable
. 14.
14
TIBTable

DataBase

TIBDatabase

TableName

Transaction

TIBTransaction

UbdateObject

TIBUpdateSQL

SQL

Active

Boolean

14.2.4 TIBQuery
TIBQuery
.
15.
15
TIBQuery

DataBase

TIBDatabase

SQL

TStrings

SQL

Transaction

TIBTransaction

Active

Boolean

116

14.2.5 TIBUpdateSQL
SQL
. 16.
16
TIBUpdateSQL

DeleteSQL

TStrings

SQL

InsertSQL

TStrings

SQL

UpdateSQL

TStrings

SQL

RefreshSQL

TStrings

SQL

SQL , , .
, :
INSERT INTO Student //
(family, name, second_name) //
values (:family, :name, :second_name);

// , ..
:
UPDATE Student
SET family =: family,
name = :name,
second_name = :second_name
where number = :number;

//
:
DELETE FROM Student
Where number = :number;

117

15.
15.1
,
10. 10.5 DataBases Create DataBase.
(. 32) :
Alias ;
Files
Filename(s) Size (Pages) 4096;
Page Size Size (Pages) 4096.
Default Character Set .
.

118

. 32.

15.2
. , .
.
Tools Interactive SQL. , .33.
.
Query Execute, .

119

.33. Interactive SQL

15.3
. ,
. , .
15.3.1
CREATE DOMAIN, :
CREATE DOMAIN domain [AS] <datatype>
[DEFAULT { literal \ NULL \ USER}}) \
[NOT NULL] [CHECK ( <dom_condition>)][COLLATE collation];

domain - ;
datatype - InterBase ;
DEFAULT ;
CHECK - ;

120


CREATE DOMAIN MONTH AS SMALLINT CHECK( VALUE BETWEEN 1 AND 12);

.
15.3.2
ALTER DOMAIN.
ALTER DOMAIN , ( ) NOT NULL. ()
, . ALTER DOMAIN
, SYSDBA,
.
ALTER DOMAIN :
ALTER DOMAIN name {
[SET DEFAULT { literal / NULL / USER}]
| [DROP DEFAULT]
| [ADD [CONSTRAINT] CHECK { <dom_condition>)]
| [DROP CONSTRAINT]
};

:
ALTER DOMAIN DM_INTEGER ADD CHECK (VALUE>0);

.
15.3.3
DROP DOMAIN. . - , ( ).
DROP DOMAIN :
DROP DOMAIN name;
:
DROP DOMAIN DM_INTEGER;
121

15.4
,
. .
: ( ),
, ( ), , .
15.4.1
CREATE TABLE
CREATE TABLE :
CREATE TABLE table [EXTERNAL FILE <filespec>]
(LIST_<col_def> [, LIST_<tconstraint>J);
table - .

,
EXTERNAL [FILE] "<filespec>" , ( InterBase) ,
<col_def> - ( ) ,
<tconstraint> - .

<col_def> :
<col_def>:= col {datatype | COMPUTED [BY] (< expr>) / domain}
[DEFAULT {literal \ HULL \ USER}]
[NOT NOLL] [<col_constraint>]
[COLLATE collation]

():
col - ; ;
( datatype \ COMPUTED [BY] (< expr>) / domain)
, datatype - InterBase .

122

domain ( ,
, );
COMPUTED [BY] (< expr>) - .
.
InterBase ,
( ).

.
, .
CREATE TABLE "UNIVER"
// UNIVER
("ID"
INTEGER NOT NULL,
// ,
NULL
"CAPTION"
VARCHAR(50),
// , 50
PRIMARY KEY ("ID")
// ID );

:
CREATE TABLE "STUDENT"
("ID"
INTEGER NOT NULL,
// ,
NULL
"NAME" VARCHAR(30),
// , 30
"SECOND_NAME"
VARCHAR(30),
// , 30
"FAMILY"
VARCHAR(30),
// , 30
"BIRTHDAY"
DATE,
//
ID_UNIVER
INTEGER,
//
PRIMARY KEY ("ID")
// ID );

,
.

123

15.4.2
,
. ,
ALTER TABLE. , , .
.
:
ALTER TABLE "STUDENT"
// "STUDENT"
ADD course INTEGER NOT NULL;
// COURSE,

student course,
, .
:
ALTER TABLE "STUDENT"
// "STUDENT"
DROP COURSE;
// COURSE

, ALTER TABLE.
Alter table "STUDENT"
ALTER course TO course_

15.4.3
Drop Table. (, , , ). :
DROP TABLE STUDENT;

124

15.5

. , .

( ),
, .

, .
15.5.1
CREATE INDEX.
CREATE INDEX:
CREATE INDEX [ASC/ENDING / DESC/ENDING]
INDEX index ON t a b l e (LIST_col);
ASC/ENDING/ DESC/ENDING/ -
.
ASC ASCENDING - .
DESC DESCENDING - .
index .
table .
col , ;
.
UNIQUE .
, . ,
. , ,
.
:
CREATE UNIQUE ASCENDING
INDEX SCLAD_PRODUCT ON SCLAD (PRODUCT_NAME);
.
125

15.5.2
InterBase
.
:
ALTER INDEX name {ACTIVE / INACTIVE} ;
.
ALTER INDEX SCLAD_PRODUCT INACTIVE;
15.5.3
DROP INDEX. :
DROP INDEX name;
:
DROP INDEX SCLAD_PRODUCT;
15.6
- ,
, ,
( ), ( )
( ) .
. , , . ,
.
15.6.1
CREATE TRIGGER
CREATE TRIGGER name FOR {table / view}
[ACTIVE / INACTIVE]
{BEFORE / AFTER} {DELETE / INSERT / UPDATE
[POSITION number

126

AS < trigger_body>
< trigger_body> :=
[< variable_declaration_list>;
< block>
< variable_declaration_list> := DECLARE VARIABLE variable datatype;
[< variable_declaration_list>
< bl> : =
BEGIN
<L_statement>
END
<L_statement> ;=
< compound_statement>
<L_statement>
.
.
CREATE TRIGGER I_TAUTHOR_1 FOR TAUTHOR
ACTIVE BEFORE INSERT POSITION 0
as
begin
if (new.AUTHOR is NULL) then
new.AUTHOR=GEN_ID(sysnumber,1);
if (new.COMMENT is NULL) then
new.COMMENT="
end;
< compound_statement> = {< block> / statement;}

127

17
CREATE TRIGGER

Name

table | view

ACTIVE | INACTIVE

. . ACTIVE - , INACTIVE - .

BEFORE | AFTER

. , :
BEFORE - , .
AFTER - ,

DELETE | INSERT | UP- , DATE


: DELETE - , INSERT , UPDATE .
POSITION number

POSITION number , ( ),
number 0 ( )
32767 . (number).
.
,
.

DECLARE ARIABLE va- ,


riable datatype;
. DECLARE VARIABLE ";".
, datatype .
<compound_statement>

SQL .
";", BEGIN-END. END ";" .

128

16.

- , SQL
InterBase. .
,
. SQL
, NEW.co/wmn,
OLD.column, . , ,

. , - , , .

. , ,
SELECT .
:
- . ,
, ,
, .
- . , ,
, . .
- . ,
, . .
: .

129

- , (, ) , -
(, ) .
- , (, ) , -
(, ) .
, ,
.
, , SELECT.
,
, , , ,
. , , ,
.
16.1
CREATE PROCEDURE
ISQL. SQL .
. :
- , , .
- , , .
, ,
.
RETURNS.
:
- .
, BEGIN-END.
. .

130

Script- , ";". Script-


";" , - . CREATE PROCEDURE
SET TERM, ,
SET TERM (";")
CREATE PROCEDURE:
CREATE PROCEDURE name [(LIST_<params>)]
[RETURNS (LIST_<params>)]
AS
<procedure__body> ;
LIST_<params> : : = param datatype
<procedure_body> :: = [ <variable_declaration_list>J
<block>
<variable_declaration_list> ;:=
DECLARE VARIABLE var datatype;
[<variable_declaration_list>]
< block> ::=
BEGIN
<L_statement>
END
<L_statement> ;;= < compound_statement>
[<L_statement>]
< compound_statement> = {< block> / statement;}

131

18.
CREATE PROCEDURE

name

.
,

param <datatype>

, , param -
,

,
<datatype> - InterBase
.

RETURNS param <datatype>

()
,
, param - ,
, <datatype> - InterBase .

SUSPEND .

AS

,
.

DECLARE VARIABLE var <datatype>

, .

DECLARE VARIABLE ";".
, var - , . <datatype> - InterBase
.

statement


.
";" ( BEGINEND)

, , .
CREATE PROCEDURE PBUTHOR(CODE INTEGER)

132

RETURNS (AUTHORS VARCHAR(250))


AS
declare variable auname varchar(60);
declare variable UNIKEY integer;
declare variable ws integer;
begin
ws=-l;
for select a.UNIKEY, b.auname
from tbook a, tauthor b, tbook_author
where (a.unikey=:Code and a.unikey=c.bookkey and
.author=b.author)
into :UNIKEY, :auname
do
begin
if(ws=-l) then authors=auname;
else authors=authors||', '||auname;
WS=UNIKEY;
end
if(ws!=-l) then suspend;
end;
16.2
ALTER
PROCEDURE. ALTER PROCEDURE ISQL. SQL .
ALTER PROCEDURE
. , , . .
ALTER PROCEDURE
, SYSDBA .

133

. , ,
, , .
, AFTER PROCEDURE. , .
ALTER PROCEDURE CREATE PROCEDURE:
ALTER PROCEDURE name [(LIST_<params>)]
[RETURNS (LIST_<params>)]
AS
<procedure_body>;
name - .
CREATE PROCEDURE (. .
).
ALTER PROCEDURE PBUTHOR(CODE INTEGER)
RETURNS (AUTHORS VARCHAR(250))
AS
declare variable auname varchar(60);
declare variable UNIKEY integer;
declare variable ws integer;
begin
ws=-l;
for select a.UNIKEY, b.auname
from tbook a, tauthor b, tbook_author
where (a.unikey=:Code and a.unikey=c.bookkey and
.author=b.author)
into :UNIKEY, :auname
do
begin
if(ws=-l) then authors=auname;

134

else authors=authors||' - '||auname;


/*
*/
ws=UNIKEY;
end
if(ws!=-l) then suspend;
end;
16.3
DROP
PROCEDURE, ISQL. SQL .
DROP PROCEDURE
, ,
SYSDBA . DROP PROCEDURE
. , , .
, .
, . .
DROP PROCEDURE SQL, SQL.
:
DROP PROCEDURE name;
name - .
DROP PROCEDURE PBUTHOR;
16.4 Alter Procedure Drop Procedure

, ,

135

. .
,
, ,
,
.
, , . ,
.

. .
16.4
:
EXECUTE PROCEDURE name [LIST_: paramj
/RETURNING_VALUES [LIST_: par am ];
name - ,
param - .
: .
ISQL .
EXECUTE PROCEDURE name [ ( ] [LIST_param] [ ) ];
name - ,
param - .

:
SELECT < col_list> from name (LIST param)
[WHERE < search_condition>]
[ORDER BY < order_list>];
name - ,

136

param - ,
< col_list> - (
),
<search_condition>, < order_list> - .
SELECT ,
(. . 3.1).
,
.

137

3. 2.

17.
. (User defined functions - UDF) - , . UDF , .
UDF BLOB isql . UDF .
.
(DLL), ,
. , .
, InterBase. , , SQL .
,
,
, .
17.1
UDF ,
, .
DELCARE EXTERNAL FUNCTION.
.
() , , .
:
138

DECLARE EXTERNAL FUNCTION name


[\LIST_datatypes]
[RETURNS (datatype [BY VALUE] / CSTRING ( int)] [FREE_IT]
ENTRY_POINT 'entryname'
MODULE_NAME 'moduIename';
datatypes ; : = {datatype / CSTRING ( i n t )}
datatype - InterBase
BLOB.
CSTRING ( int) - , ,
FREE_IT - ,
, .
, UDF ,
InterBase, , ,
.
:
DECLARE EXTERNAL FUNCTION IIF
DOUBLE PRECISION, INTEGER, INTEGER
RETURNS INTEGER BY VALUE
ENTRY_POINT "if_i"
MODULE_NAME "func.dll";
IIF,
DOUBLE PRECISION INTEGER, INTEGER. if_i,
dll c func.dll.

139

19

name

UDF
SQL;
( )
ENTRYPOINT

datatype

.
UDF
.
, .
.

RETURNS

BY VALUE

,
. ,

CSTRING (int)

, UDF ,

FREE_IT

, UDF.
, UDF
.

'entryname'

UDF,

'modulename'

,
, UDF.
.

140


1) .. , .. . - :
- , 1989. - 154.
2) .. , . - .:
, 1981. -151.
3) .., .., .. ,
. .: . .. .-. ., 1982.-384.
4) .. . .: . . -., 1988. 127.
5) . . .: ,
1985. 328.
6) . . :
. .: , 2002. 464.
7) . . . - .: ,
1985 - 368.
8) .. . . .: , 1980. - 199.
9) . , . : . .:
, 1999
10) .. : - .:
, 1988
11) .. , .. ,
: .- : - , 2005.
12) . , . , . . .
.: . .-.: . , 2003.
13) .. : , , .- .:
, 2002.
14) . , . . SQL.: . . - . , 1998.
15) .

1) . . , . . , . . SQL: 3- . . . . .-.: . ,
2001.
141

2) . / . .
. .. . .: , 1977 . 400.
3).. . .:,1977.-183.
4).., ... . - .: , 1984. - 279.
5) . , / . . .: . , 1978. 347.
6) . / . . .: , 1979. 326.
7).. . .: , 1974. 144.

142


1. ............................... 3
1 . ...................................................... 3
1 . ............................. 3
1.1 Delphi............................................ 3
1.2 BDE ......................................................... 4
2 ........................................ 6
2.1 ............................................................................................................ 6
2.2 TDataSet ............................................................................................ 6
2.3 DataSet ..................................................................... 8
2.4 DataSet .............................................................. 9
2.5 . ..................................................................................... 9
2.6 - ..................................................................... 16
2.7 - .................................................................. 17
3 . ................................................................................... 19
3.1 .............................................................................. 19
4 . .................................... 23
4.1 ....................................................................... 27
4.2 .................................... 32
4.3 ................................................................................... 33
4.4 ......................................................................... 35
4.5 .............................................. 37
4.5.1 ER- ................................................................... 38
4.5.2 ........................................................... 42
4.5.3 ............................................................................ 44
2. . 48
2 ........................ 48
5 , , .............. 48
5.1 Delphi.......................................... 48
5.2 .......................................................................... 50

143

6 ...................... 52
6.1 TDataSet .......................................................................................... 52
6.2 DataSet ................................................................... 53
6.3 DataSet............................................................. 54
6.4 ,
............................................................................................................ 55
7 , ...................... 59
7.1 .......................................................................................... 59
7.2 .................................................................................... 62
7.3 .......................................................................................... 64
7.4 ............................................................ 66
8 .............................................. 68
8.1 SetKey .......................................... 68
8.2 ............................................................. 70
8.3 (Refresh) ................................................................................ 71
8.4 (Bookmarks) ............................................................................... 71
9 ............................................................ 73
9.1 c OnFilterRecord .................................... 73
9.2 Filter ........................................ 74
9.3 ......................................................................... 75
9.4 ............................................................. 76
2. 2 SQL- .... 77
10 SQL: , ,
......................................................................................................... 77
10.1 SQL- Delphi ............................................................................. 77
10.1.1 TQuery ............................................................................... 77
10.1.2 SQL ....................................................................................... 77
10.1.3 Params ................................................................................... 78
10.1.4 Format .................................................................................... 80
10.2 SQL Monitor ........................................ 81
10.3 SQL Explorer ............................................................................................ 82
11 , ............. 84
11.1 SQL. ............................................................................... 84
11.1.1 SQL................................................................................. 84
11.1.2 ..................................................................................................... 84
11.2 SQL.......................................................... 85
11.3 .............................................................................................. 89

144

12 SQL ........ 89
12.1 ......................................... 89
12.1.1 Select..................................................................................... 89
12.1.2 From........................................................................... 90
12.1.3 Where ............... 90
12.1.4 Order By ........................................................ 92
12.2 SQL-
...................................................................................................... 93
3 .......... 97
13 ........................ 97
13.1 QReport ................................................................ 97
13.2 QuickRep................................................................................ 98
13.3 QRBand .................................................................................. 99
13.4 QRExpr .................................................................................. 99
13.5 TQRSysData........................................................................ 101
13.5 QReport ...................................... 101
3.
- ......................................................................................... 106
1. -
................................................................................................ 106
14. -
..................................................................................................... 106
14.1 - ................ 107
14.1.1 ............................................... 109
14.1.2 - ..................................... 112
14.2 - .................. 112
14.2.1 TIBDatabase ..................................................................... 112
14.2.2 TIBTransaction ................................................................. 114
14.2.3 TIBTable .......................................................................... 116
14.2.4 TIBQuery ......................................................................... 116
14.2.5 TIBUpdateSQL ................................................................ 117
15. ............................. 118
15.1 .......................................................................... 118
15.2 ............................................................. 119

145

15.3 .................................................................................................. 120


15.3.1 .............................................................................. 120
15.3.2 ............................................................................ 121
15.3.3 ................................................................................ 121
15.4 ................................................................................................. 122
15.4.1 .............................................................................. 122
15.4.2 ...................................................................... 124
15.4.3 .............................................................................. 124
15.5 ................................................................................................. 125
15.5.1 ............................................................................. 125
15.5.2 .......................................................................... 126
15.5.3 ............................................................................. 126
15.6 ................................................................................................ 126
15.6.1 .............................................................................. 126
16.
............................................................................................... 129
16.1 .............................................................. 130
16.2 ........................................................... 133
16.3 ............................................................. 135
16.4 Alter Procedure Drop Procedure ................... 135
16.4 .................................................. 136
3. 2.
...................................................................................................................... 138
17. .................... 138
17.1 ............................................................. 138
........................................................ 141

146