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

SQL

- ,

. , - SQL
,
.

SQL-

,
,

SQL. ,
3-


SQL


SQL.

: /SQL

, , , 1970
IBM.
.
. 2004
.

.

SQL

. .

SQL . : , , SQL- . , SQL, , ,


.
. . , 1970 , ,
SQL. ,
, .
,
SQL, :

?
null- ?

?

SQL-?

www.symbol.ru
-
(812) 324-5353, (495) 945-8100

9 785932 861738

. .

ISBN 978-5-93286-173-8

SQL

and Relational
Theory
How to Write Accurate SQL Code

C. J. Date

SQL

SQL

. .

-
2010

. .

SQL
SQL
.

.
.
.

.

.

.

.
. .
SQL . SQL. . .
.: -, 2010. 480., .
ISBN 978-5-93286-173-8
SQL . : ,
, SQL- .
, SQL, , , .
. . , 1970 , , SQL.
, , .
,
. , -
SQL ,
.
ISBN 978-5-93286-173-8
ISBN 978-0-596-52306-0 ()
-, 2010
Authorized translation of the English edition 2009 OReilly Media Inc. This trans
lation is published and sold by permission of OReilly Media Inc., the owner of all rights
to publish and sell the same.
,
.
, , .

-.199034,-,16,7,
.(812) 324-5353, www.symbol.ru. N 000054 25.12.98.

005-93, 2; 953000 .
17.02.2010. 70100 1/16. .
30 .. 1200 .

199034, -, 9 , 12.

,

,
,
.

, ,
, ,
,
, .

.
(14521519)

, - ,
, .
(18181885)

...
.
. .

,
,
.


................................................................................ 13
1. ......................................................................... 21
....................................... 22
.......................................... 23
, ........................................................... 25
........................................................ 26
.................................................................. 35
................................................................... 39
............................................... 42
-........................................... 44
.............................................................. 46
........................................................ 47
.............................................................................. 49

2. ................................................................ 51
..................................................................... 51
.............................................................. 52
..................................................... 58
?........................................................................... 62
................................................... 66
SQL................................................................ 68
SQL.............................................. 70
SQL......................................................... 72
SQL........................................................ 74
........................................................ 76
.............................................................................. 77

3. , .......................... 81
?...................................................................... 81
........................................................... 84
SQL............................................................................. 86
?................................................................. 88
.................................................................. 90
n-...................................................................91

................................................................. 92
TABLE_DUM TABLE_DEE........................................................ 93
SQL........................................................................... 94
SQL.........................................................96
........................................................ 98
..............................................................................99

4. , null-.............................. 101
?............................................................. 101
: ..................................................... 106
SQL................................................. 107
null-?......................................................... 109
null- SQL.............................................. 113
............................................... 115
...................................................... 116
............................................................................ 117

5. -, ...... 121
................................................................ 122
................................. 122
....................................................... 125
............................................................ 126
.................................................... 127
............................................................. 130
- ......................................... 134
................................................................... 137
............................................................................ 139

6. SQL I:
........................................................... 142
....................................................... 142
................................................................... 145
........................................................................... 149
................................................................................ 149
............................................................................. 151
, ......................................... 155
?............................... 157
..................................... 157
?...................................... 159
SQL................................... 160
...................................................... 161
.................................................. 165
............................................................................ 167

7. SQL II:
......................................................... 171
................................................. 171
............................................................................ 172
-................................................................... 174
.................................................................................. 177
.............................................................. 179
-...................................................... 183
.............................................................................. 185
.................................................................... 190
........................................... 191
.................................................................. 193
ORDER BY?.......................................................... 194
............................................................................ 195

8. SQL ..................................... 200


.................................................................... 201
........................................................ 204
SQL............................................................ 205
......................................................... 206
SQL................................................ 210
............................................................................. 211

............................................... 213

?............................................. 216
......................................................... 219
.................................................................................... 221
............................................................................ 223

9. SQL ...................................................... 228


-............................... 229
...................................................... 233
................................................................... 234
................................................... 236
............................................................... 240
?.................................................... 244
.................................................................... 245
............................................................................ 247

10. SQL ............................................. 250


........................................... 251
................................................ 254

10

....................................................................... 256
........................................................... 260
.............................................................. 267
.......................................................... 274
...................................................... 277
............................................................................ 278

11.
SQL-........................................ 281
............................................ 282
1. ............................................ 284
2. ............................... 285
3. ............................ 286
4. .................................... 288
5. ........................................ 290
6. .............................. 293
7. ...................................... 294
8. COUNT.............................................. 296
9. .............................................. 297
10. UNIQUE..................................................... 298
11. ALL ANY........................................ 299
12. GROUP BY HAVING.............................................. 303
............................................................................ 304

12. , SQL............................ 306


SELECT *................................................................................ 307
........................................................................ 307
................................................................. 307
................................................................ 308
............................................................................. 311
..................... 314
................................................................... 315
- SQL.............................. 315
............................................................................ 318

A. .......................................... 320
.................................................... 322
.............................................. 325
........................................................ 331
............................................... 331
?............................................................... 333

B. .................... 338
................................................... 339

11


/.............................................. 342
..................... 348
.................................................. 357
...................................................................... 361
.................... 364
...................................................... 366
............................................................................ 368

. ....................................... 372
1................................................................................... 372
2................................................................................... 379
3................................................................................... 387
4................................................................................... 392
5................................................................................... 398
6................................................................................... 404
7................................................................................... 413
8................................................................................... 424
9................................................................................... 433
10.................................................................................. 440
11.................................................................................. 448
12.................................................................................. 450
B......................................................................... 450

D. .............................. 460
............................................................... 469


SQL . SQL :
, , SQL-
, ,
. ,
SQL- ( , , ,
), .
, SQL
. ? SQL ?
, , SQL , .
, SQL ; - null- , . ,
, .
, ( ), ,
SQL , , . , SQL . ,
SQL ,
, .
,
SQL , , , . , SQL
. ,
, ( ); .
1969 ,
, , -
.
; , , SQL.

14


, ,
- SQL. , ,
SQL, ( ) - , SQL. ( , , , ).
, , SQL
.
, , , , , ,
, --.
( , ,
; .)
, SQL. , , SQL
, . ( . . 12.)

Database in Depth
Database in Depth: Re
lational Theory for Practitioners (OReilly, 2005) .
(
):
,
,
( ), ,
SQL. , , , ,
, , ,
. , , , , , , ,
, ,

15

. .
, , 1...
?
?
?
?
-?
?
?
-?
?
, ?
SQL ?
?
XML ?
.
, -

.

, ,
. , SQL , , , , .
, , (
SQL ). , , ( -
), , , . , , SQL
( -
).
1

, , .

16


. ,
.
;
( , ,
), . , ,
; , , , .
-, , , , , .
, , , : ,
, . , ( , , )
.
, , .
. ,
(Stphane Faroult), : ,
, .
, , , , , , ( ).
,
.
,
: , . : , ? ,
, ,
, .
-, , SQL . ( , , .) , SQL, , . , SQL. SQL

17

, , , , ,
. ,
,
. , , , , , : SQL . , .
, , , , SQL . , (, ), .
, ,
, : , . ,
, ,
.
,
, , ( , ). .
-, , SQL , - ( ). ,
,
1 ( ).
-, , ( , ). , . ,
, ,
. , , C.
, , . .
http://www.clik.to/chris_date http://www.thethirdmanifesto.com.
1

SQL, SQL. . .

18

.
. , , , -
, x.

.

,
.


. . ,
. ,
, . - .
.
, .
, , . , ,
ISBN, : SQL and Relational Theory, C. J. Date, Copyright
2009 C. J. Date, 978-0-596-52306-0.
,
, ,
permissions@oreilly.com.


, , - .
- ,
:
OReilly Media, Inc.
1005 Gravenstein Highway North
Sebastopol, CA 95472

19

800-998-9938 ( )
707-829-0515 ( )
707-829-0104 ()
. , :
info@oreilly.com

:
bookquestions@oreilly.com
-, (
).
:
http://www.oreilly.com/catalog/9780596523060

OReilly:
http://www.oreilly.com

Safari

Safari Enabled, , e Safari OReilly.
Safari .
, , , .
http://safari.oreilly.com.

, , SQL, , , , 2007 ,
. (Toon
Koppelaars) ,
(Lex de Haan) ( ), , . , SQL. , - , -

20

, , . . , , . (Herb Edelstein), (Sheeri


Ktitzer), (Andy Oram), (Peter Robson) (Baron Schwartz) (Hugh Darwen) (Jim Melton)
. , . , OReilly (Isabel Kunkle), (Andy Oram) (Adam Witwer)
, .
. .
, ,
2008

1
.

SQL . , , , ,
SQL. , . SQL .
, , , . ,
,
. , , , , . , (, , ); ,
, .
, . , , ,
? ?1
1

. , ( !)
SQL: Oracle SQL
Server

-



(-, . . .). . .

22

1.



.
,
(
), .
, , , , ; , . .
, . ,
, .
.
SQL - , Oracle.
, SQL - , ,
. , . , , - SQL,
SQL, , , ,
, , - . : SQL
. , SQL (
):
,

- -

23

( ). .
: SQL, . , , , , , , , , - , , , .


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

24

1.

1, , .
, . , (table expression) (query expression), () , , () ,
. ( , , ;
, , SELECT SELECT.)
, ,
SQL , . ,
JOIN, , , ,
( ),
, (. 12). ,
, ,
. ( 12.)
, , ,
Core SQL ( SQL),
.
(OLAP), SQL, ,
, .
, , , .
, /* */.
, SQL , . . :
,
( , SQL). , , , (. 1

International Organization for Standardization (ISO): Database Language SQL,


Document ISO/IEC 9075:2003 (2003).

25

9), , . ,
: , , , .

,
, ( ) . ,
; . ? :
, , ,
; , ;
-.1

. ( SQL )
. , , Oracle; ,
Oracle. Oracle,
, , DB2 SQL Server (
). , ,
:
, .
, , , . ,
. , , . SQL. , SQL ( , ),
, -
(, , ). , . , :
, , .
, , .
. . .
. .

26

1.

; , (, - , - ), .

(14521519), , , 500 :
, , ,
, , , .
.

( .)


; , .
! , . .
: ,
.
. ,
. , , , ,
.
, , . . ,
IBM ( ,
, ; ,
, ). 1968 , , , ,
. - IBM
1969 , D.


, , . , -

27

; , .
, . ,
, , ,
( .1.1
). ( ). ,
, . , .1.1,
DNO ( ),
, DNO DEPT DNO
EMP . (, , ,
. .)
DEPT

EMP

DNO

DNAME

D1
D2
D3

Marketing
Development
Research

BUDGET
10M
12M
5M

ENO

ENAME DNO

E1
E2
E3
E4

Lopez
Cheng
Finzi
Saito

D1
D1
D2
D2

SALARY
40K
42K
30K
35K

DEPT.DNO EMP.DNO

. 1.1.

, , .1.1, , n- . N-
n ; , . n
. 1- , 2- , 3- ..
.
!
1.
; ,
, ,
, ,
. , .1.1 1

, :
- (.
- ). . . 1.1 .

28

1.

, , , {DNO}
DEPT, {ENO} EMP. , ; , , , , , .


( ) (comma
list), . . xyz (, ). xyz
xyz, (
). , A, B C , :
A,B,C
C,A,B
A,C
B
. ,
, , , () (
) () , , (
).

, , - .

, . , , , - . , , , , ,
, ,
{ENO}, {ENAME} EMP.
, , {ENO} .
: , . . , ; , -

29

( , ). , ,
. , ,
- , .1.1, , , ,
. ,
, . ( , ,
;
.)
,
, - ( )
. , .1.1 {DNO} EMP, {DNO} DEPT ( ). , , EMP , , , DNO D2, DEPT , DNO D2,
EMP , ,
.


(, , )
, TRUE. ,
, , SALARY
() . ,
,

. ,
( ).
, .

, , null-.

, .

30

1.

. ,
, ,

; ,
,
EMP , , D2 DNO,
DEPT DNO. , , ,
. , : B A, A
.
, , , . , null-; , . ( , , .) , , ( , ) . , null- 3 4.
, null , , ( , null-
, , ). , ,
E2. , - SALARY , EMP, , , ,
SALARY null:
ENO

ENAME DNO

E2

Cheng

SALARY

D1

, SA
LARY. !
, SALARY , , . ,
: , , . , ,
, null.
EMP, ( ),
, , , -

31

, , ,
() .
, null- .
.


, ,
:
, , ( MINUS), .
, (, r1 MINUS r2,
r1 r2 ) - .

,
-. :
,
(update) INSERT, DELETE UPDATE ( ). UPDATE,
.
, ,
.
;
, (MINUS)
,
. , :
. ; ,
,
, , r1 MINUS r2 r3, r4 ..
, : , . , ( , , )1; 6 7 -

divide. 7 , .

32

1.

.
.1.2 .
: ,
, ; , , .
1
,
, . ,
EMP , DNO D2.

a
b
c

x
y

a
a
b
b
c
c

x
y
x
y
x
y

()
a1
a2
a3

b1
b1
b2

b1
b2
b3

c1
c2
c3

a1
a2
a3

b1
b1
b2

c1
c1
c2

. 1.2.
1

constraint ( ) restriction () . ,
, .. .

33

, ()
,
. , EMP ENO SALARY ( ENAME DNO).

, , , .
( ), , ; ,
,
6.

, , . ( ,
, 6.)

, , , .

, , ,
.

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

34

1.

,
, ( ) ( ). ,
.
, 10 11.


, , , :
. ( , , ,
, .) .1.3 .
S SNO
S1
S2
S3
S4
S5

P PNO
P1
P2
P3
P4
P5
P6

SNAME STATUS

CITY

Smith
Jones
Blake
Clark
Adams

London
Paris
Paris
London
Athens

20
10
30
20
30

PNAME COLOR
Nut
Bolt
Screw
Screw
Cam
Cog

Red
Green
Blue
Red
Blue
Red

WEIGHT
12.0
17.0
17.0
14.0
12.0
19.0

SP SNO

CITY
London
Paris
Oslo
London
Paris
London

S1
S1
S1
S1
S1
S1
S2
S2
S3
S4
S4
S4

PNO

QTY

P1
P2
P3
P4
P5
P6
P1
P2
P2
P2
P4
P5

300
200
400
200
100
100
300
400
200
200
300
400

. 1.3.

S (, ).
(SNO) ( , {SNO} ), (SNAME), ( .1.3
SNAME ) (STATUS), ,
, (CITY).

35

P (, ). (PNO) ({PNO}
), (PNAME), (COLOR),
(WEIGHT) , , (CITY).

SP ( , ). (SNO), (PNO)
(QTY). ,
({SNO,PNO} ,
, {SNO} {PNO} , S P, ). ,
.1.3 , S5,
.


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

36

1.

. ,
,
,
, .
: ,
, , .. -, .
, ,
, , /
, .
: ( ) , ,
.
;

, . ,
, (, , . 8).
:
( ) , .
, .
, ; , . , , , .
.
( ),
, . , ,
. ! ,
, ; . , X
, Y, .
, . ,
;
, ,
. ,
, :
. S JOIN SP, -

37

, ;
,
( ):
do for all tuples in S ;
fetch S tuple into TNO , TN , TS , TC ;
do for all tuples in SP with SNO = TNO ;
fetch SP tuple into TNO , TP , TQ ;
emit tuple TNO , TN , TS , TC , TP , TQ ;
end ;
end ;

. : .
.1
,
SQL. SQL, ( ); . SQL , . ,
.

. , , ; , . , , ( ) SQL . , : , ,
, .

. , , , , , . , 1

, ( ?).
, , , ; , ,
,
/ .

38

1.

, , , ,

, , . ,
, ,
; , , , , ,
. ,
, ,
.
, ( ) ,
,
, ; ,
. (, .) , , SQL. : SQL, . ( ,
, , SQL.)
, ,

. , , SQL-
, . , , ,
, . .
, , , .
:
: ( ) ( )
.
, ( , , )
. , ,
.
, , , ,
.

39

, , .
,
, , ,
.
, ,
,

. ,
, . A.
, , .


.

. , .
(
-/-), , . ,
.1.3 , . ,
, , , ,
, .
, ,
-/-, , .1.3, , - ,
. ,
STATUS , , INTEGER, .1.3 . , - !
, ( ), . ,
S, P SP .1.3 4, 5
3, 5, 6 12. : . , S 4 ( S).
-.
, , .

40

1.

SQL, , , , :
SQL -
. , , SQL. , ,
, . , .1.3 CITY , , :
CITY

CITY

London
Paris
Athens

London
Paris
Paris
London
Athens

( SQL-
SELECT DISTINCT CITY FROM S. DISTINCT,
, . , ,
, .)
, . , , ( , {a,b,c} {c,a,b} , , ,
). ,
,
, . ,
.1.3 , , S3, S1, S5, S4 S2,
. : , , , ORDER
BY, , , , . ORDER BY , .
, , .
, , . , .1.3
, , STATUS,
SNAME, CITY, SNO,
( , SQL
). , , SQL:

41


SNO

CITY

CITY

SNO

S1
S2
S3
S4
S5

London
Paris
Paris
London
Athens

London
Paris
Paris
London
Athens

S1
S2
S3
S4
S5

( SQL
SELECT SNO, CITY FROM S SELECT CITY, SNO FROM S. ,
,
, , . ., , SQL- JOIN 6.)
, (, , , 1).1 ,
. .
.

, :
, , .1.1 1.3. , ,
.1.1 1.3, , , , ,
SQL.
, ,
,
, , , .
, ,
.
, . . ,
Cei nest pas une pipe , ,
, , .
, ,

1

, , , ,
.., . . 2.9 2, B.

42

1.

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

;
, ,
, .
. , :
SQL

( ) ( )
.


, ,
, , .1.3, (, ). , . , , . SQL CREATE
TABLE ( SQL , ,
). , , :
CREATE TABLE S ... ;

,
, . (
) , t .
SQL:
CREATE VIEW SST_PARIS AS
SELECT SNO , STATUS

43

FROM S
WHERE CITY = Paris ;

, , 1, .
,
, ,
, , . , ; , . , 9.
.
:
, .
, .
,
! , ,
. ,
, ,
, - , ( , ). ,
. ,
, ; S SP
. -: ,
( !) , .
. ,
, , 1

, , . , ; ,
. .
9.

44

1.

. , , SQL-
, , ; 1, ( ) , . , SQL ( SQL ), ,
.
, , , ,
, , , , , .
, ( ), , ( , ),
. (, , ..); () ,
, .
: . ,
,
SQL .

-
, , ,
; , , , . , , , , .
, ,
: .
. ,
:
DECLARE N INTEGER ... ;

, , SQL- , , , .
, .

45

N , , .
. , SQL :
CREATE TABLE T ... ;

T ; , ( , SQL, null-
-) -, ( ).
.1.3, . -,
, - . , , , , -. , S, P
SP , -. , ,
- S
-, .1.3, (
, ) :
DELETE S WHERE CITY = Athens ;

:
S

SNO

SNAME STATUS

CITY

S1
S2
S3
S4

Smith
Jones
Blake
Clark

London
Paris
Paris
London

20
10
30
20

S . , ( ) ( ) , - . DELETE
:
S := S WHERE NOT ( CITY = Athens ) ;

, : ()
()
. , .

SQL,
SQL .
( DELETE) TutorialD,
- . Tutorial D

46

1.

(Hugh Darwen),
Databases, Types, and the Relational Model: The
Third Manifesto (. D) ,
- . SQL, SQL.

INSERT UPDATE
. ,
, ; , .
, -. , ,
, , , , 1. , , , , :
- -.
-
( , ).

, ,
-.


-
, . (, ,
, , .) .
: , ,
3. . 1

, SQL,
, , , .

47

, ,
, . , , (. )
.
, ( ) ; ,
.
: .
. , , ,
, ,
. (
; ,
.)
, ,
3. :
, , , XML-, , , , , (
). , , .
.
, ,
. . , , (
):
,
[, ] [, ]
[ , ,]
[, ].


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

48

1.

(, ,
) SQL.
, , , : , n- , , , , , , , , . ,
, ,
, , , .
, , , . -, ,
. (,
-) .
,
- . .
, SQL
. , : SQL null- -; SQL ; SQL
( , ). . .
( , , ):
, , ,
, . : , ,
( , , ). ,
, , ..

, ,
SQL- ,
! , -

49

(, , ),
, , . . Caveat lector1.

1.1. ( .) ,
, ,
-.
1.2. ..?
1.3. ?
1.4. ?
1.5. , , , , , ,
-. , .
1.6.
.
1.7. , ()
; () .
1.8. , ,
.1.1 1.3, ,
.
.
1.9. ( , .) -
? ?
? (
, . ,
.)
1.10. . .
1.11.
:
, , , ,
1

(.). . .

50

1.

.
,
, , , .
. ,
.
1.12. : [] -. , .

.
?
1.13. CREATE TABLE SQL .
1.14. SQL- INSERT
:
INSERT INTO SP ( SNO , PNO , QTY ) VALUES ( S5 , P6 , 250 ) ;

.
: ,
,
, .
1.15 ( ). SQL-
UPDATE :
UPDATE S SET STATUS = 25 WHERE CITY = Paris ;

.
( ,
.
.
. 7.)
1.16. , SQL . ? , ?
: INSERT / DELETE / UPDATE?
, ? ?
1.17. , ,
-, ? (
,

. .)

2
.


. , , ,
, (
). , , ,
SQL. , SQL , , , ; , SQL (, , - ).
.


( ) .
, ,
( , , -).
, STATUS - S () INTEGER.
, -
S, STATUS INTEGER, ,
, ,
STATUS INTEGER,
STATUS .
.
, ( ,

52

2.

) ,
. , , . , :
(
, , , ) ,
.
, - ,
- .
, , ,
, . BOOLEAN
( ). BOOLEAN
, , , TRUE FALSE. , , ,
INTEGER ( ), FIXED ( ) CHAR ( ). : SQL
, SQL.
, , , .
. , , . ,
, ,
. , :

,
.

,
.


, , -

53

(SNO) - S SP , , SNO. (:
.) , , (PNO) P SP (
), PNO. ,
; , , , .
, , (?), ,
. , ( WHERE SQL-), , :
SP.SNO = S.SNO

/*

*/

, , (?), :
SP.PNO = S.SNO

/* */

? , .
, , 1
(,
..), . , , SQL-, , :
SELECT
FROM
WHERE
(

S.SNO , S.SNAME , S.STATUS , S.CITY


S
NOT EXISTS
SELECT *
FROM SP
WHERE SP.PNO = S.SNO )
/* */

( , , . . 2.23 .)
, . ,
, , WHERE
SP.SNO = S.SNO, ,
WHERE SP.PNO = S.SNO.
( ),
1

= . ,
? ( , , , Oracle,
, .
, ,
?)

54

2.

, ,
, , , ,
, .
SQL-,
; , , , . , , . ( ?)
: , SP.PNO = S.SNO, . ,
,
, , , . ,
, , , - .
, , , , ,
.
, , -. - , : , ? ,
. , (- ). , , ,
; , .
,
(domain
check override DCO). , , .
SQL ,
IGNORE DOMAIN CHECKS, SQL-, , :
SELECT
FROM
WHERE
IGNORE

...
...
CUSTNO = SNO
DOMAIN CHECKS


( , ).
, .
:

55


SELECT ...
FROM P , SP
WHERE P.WEIGHT = SP.QTY

|
|
|

SELECT ...
FROM P , SP
WHERE P.WEIGHT - SP.QTY = 0

( ), , , , .
? , !
The Relational Model for Database Management Version 2
(Addison-Wesley, 1990), 47 ,
[] , ; ( ), .
. , P.WEIGHT = SP.QTY P.WEIGHT SP.QTY = 0
. ; , , , . ,
- ,
. ( , ,
. ,
P.WEIGHT = SP.QTY , P.WEIGHT
SP.QTY = 0 .)
. ( WHERE SQL):
S.SNO = X4

P.PNO = X4

S.SNO = P.PNO

, ,
( TRUE),
. , ,
, : ,
a, b c , a = c , b = c , a = b, ,
! ?
, S.SNO P.PNO SNO PNO,
, ;
, SNO PNO . , CHAR, ,
1, ,
;
( , )1. , ,
1

(representation) , .

56

2.

, , , ,
CHAR (. ?
). , ,
( ,
SNO ).
, ( ?).
,
, , 1. , SNO ( ,
, SNO)
SNO, CHAR.
:
SNO(S1)

SNO (, , S1). :
PNO(P1)

PNO (, , P1). , SNO PNO CHAR SNO PNO, .


S.SNO = X4. , ( , SNO CHAR).
, (,
,
). , ,
SNO, CHAR SNO. CHAR ,
:
S.SNO = SNO(X4)

, .
P.PNO = X4 :

,
SQL ( ) - , ,
SQL , , SQL . , ,
SQL- SELECT.

57

P.PNO = PNO(X4)

S.SNO = P.PNO ( ,
) ,
, :
.


(coercion). , X4 SNO, PNO. SQL SQL .

. , SNO PNO, THE_, SNO


PNO ( ), 1. , THE_ SNO PNO THE_SC THE_PC .
S.SNO P.PNO , ,
,
. :
THE_SC ( S.SNO ) = THE_PC ( P.PNO )

: , .
, , , THE_,
: () ()
. , , . ,
; ,
( , , ).
, , , , . -

- ,
SQL , ( ) THE_ SQL , ,
THE_ SQL . ,
THE_; . POINT ?.

58

2.

, , ()
()
,
( ). , , , ; , , , , . , , :
P.WEIGHT * SP.QTY
P.WEIGHT + SP.QTY

, -, ( , ,
). , , , ( ?).
, (=).
. , , ,
. ,
(. ?), = , !
, T v,
, v -
, T.
,
= : v1 v2 , v1 = v2 TRUE, v1 v2 , FALSE
. (, v1 v2 ,
v1 = v2
, v1 v2
, v1 v2.)


, , , .

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

59

. : ?
6 (The Relational Model for Data
base Management Version 2) , ( ). ,
/ . , ? ?
LIKE, SUBSTR (),
|| () , ,
. ? ?
, ,
:
, ( , , , ,
, , ).
, .
, /
/ // .
.
.
.2.1. R1 ; , - - , SNO/PNO.
,
, , R1
.
, R1 R2, , - ( PNO R2, , , ). , R2
1; , , , , 1 (
, ?).

60

2.
R1

R2

R3

SNO

PNO

SNO

PNO

SNO

PNO_SET

S2
S2
S3
S4
S4
S4

P1
P2
P2
P2
P4
P5

S2
S3
S4

P1, P2
P2
P2, P4, P5

S2
S3
S4

{P1, P2}
{P2}
{P2, P4, P5}

. 2.1. R1, R2 R3

, , R2 1.
R2 R3. , R3 1!
( , , ,
, . , , . R3 .) :
-, , PNO_SET , PNO_
SET, , ,
: -, ,
.
- ( , ), , {P2,P4,P5}, , , .
, ,
.
, 1,
, , .
, ;
, . , ,

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

61

R3. .2.1
PNO_SET . , (. .2.2, PNO_REL). ?
, , 1.
, , , ..
, ( ,
)
, !
PNO_REL .2.2 , (relation valued attribute RVA). RVA- 7, , SQL
. (, , RVA-, . , SQL , - , ,
, . , ,
, ,
SQL- , , SQL.
.
, RVA- ,
, ,
. ; (, , ),
, , , ,
- , , , XML , . ,
, , .
, ,
- ,
1

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

62

2.

, , .
- , .
, -:
( , , ),
, , . , - , , .
R4

SNO

PNO_REL

S2

PNO
P1
P2

S3

PNO
P2

S4

PNO
P2
P4
P5

. 2.2. R4 ( R3)

?
, .
? , 1 , : , , , XML-,
.. :
, , ,
. : , ( ).
, , .
, , , , ? 1

, ,
. :
.

63

, , , , ,
, .
, ,
, , .
, , , ,
. , , , V T, ,
v, V, T.

, , , (
, ).
,
( a / b ) + ( x - y )

+, .
, , , , , , , . Op
T , Op
T.1 , ;
; XML- XPath-; . (:=)
(=). , , , , , , . -

. : , T, T, T. , ,
SNO CHAR, ,
; ,
(||) SNO. ( ,
.)

64

2.

:
, .
, , ,
, . , , INTEGER:
:=, .
=, , < ..
.
+, * .
|| (), SUBSTR ()
; ,
.
SNO, , ,
(:=, =,
, , < ), , ,
+, * ,
. ( ,
?)
,
:
1. ( ).
2. , . 8.
3. .
, , , .
4. - , ,
.
5. , THE_, (. ).
6. , , ( . ).
, 4, 5 6, , ,
, , .

65

,
POINT . Tutorial D SQL, SQL , , REFLECT, P (x, y) ( x, y):
1 OPERATOR REFLECT ( P POINT ) RETURNS POINT ;
2 RETURN POINT ( - THE_X ( P ) , - THE_Y ( P ) ) ;
3 END OPERATOR ;

:
1 , REFLECT, P POINT
POINT ( , POINT).
2 , .
RETURN. ,
POINT; , X Y
. THE_; X Y
-, P, , 1.
3 .
,
. , , . , INTEGER
, , , :=, =, + .. ( , , ).
. , , , ,
1

,
: (. 2.5 ). , POINT, , ( ,
).

66

2.

. , , ( , ,
); , POINT(X,Y),
POINT(1.0,2.5) POINT,
POINT . , , . (,
, , .)


. , , , . ,
, , , T
, T. :
INTEGER ; , ,
.. INTEGER ,
.
;
; , , .. .
, .
, ,
, , -.
- . ( );
, , , , , ,
.
. TutorialD
- S (); ,

:
1 VAR S BASE
2 RELATION { SNO CHAR , SNAME CHAR , STATUS INTEGER , CITY CHAR }
3 KEY { SNO } ;

67

:
VAR 1 ,
S, BASE ,
-.
2 . RELATION
, ; , (,
1 , -/
-). , , .
.
3 , {SNO}
-.
, , :
RELATION { SNO CHAR , SNAME CHAR , STATUS INTEGER , CITY CHAR }

. ,
( RELATION).
, ; () ( )
() , .
, ARRAY, . TUPLE RELATION. TUPLE:
VAR S_TUPLE
TUPLE { STATUS INTEGER , SNO CHAR , CITY CHAR , SNAME CHAR } ;

S_TUPLE
, - S ( , ,
). ,
, ,
( , S1), - S, , , S_TUPLE. Tutorial D :
S_TUPLE := TUPLE FROM ( S WHERE SNO = S1 ) ;

: .
, S_TUPLE, , -
, -

68

2.

, , .
: -,
.
A
.
, (
) t
r, t. :
t , r ( ).
:
, ( ), , . ,
. ( 1.)
, , ,
(
). . THE_ ; ,
( , THE_ ).

. ; ,
, .

SQL
SQL.
, 1:
BOOLEAN
CHARACTER(n)
CHARACTER VARYING(n)
FLOAT(p)

NUMERIC(p,q)
DECIMAL(p,q)
INTEGER
SMALLINT

DATE
TIME
TIMESTAMP
INTERVAL

,
, .

SQL

69

, , : CHAR CHARACTER, VARCHAR


CHARACTER VARYING, INT INTEGER; , , SQL, Tutorial D, , . :
,
- . : ,
, , THE_.
.
:
SET <scalar variable ref> = <scalar exp> ;

(, FETCH). :
,
, ref exp reference
() expression () .
1. (, , , ).
BOOLEAN , SQL, , . , WHERE, ON HAVING,
BOOLEAN .
BOOLEAN, .
.
, (CLOB),
(BLOB), (NCHAR
..) ( , XML).
.

, . , CHAR VARCHAR = TRUE,


, , (.
SQL). , ,
CHAR VARCHAR, = TRUE,
; , ,
null ( ).

70

2.

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

SQL
SQL ( , ). :
BOOLEAN
BOOLEAN BOOLEAN.

( CHAR VARCHAR).

( FLOAT, NUMERIC, DECIMAL, INTEGER
SMALLINT).
, , ,
.
, .
,
, INTEGER FLOAT (
INTEGER FLOAT ). : ,
. SQL
: , ,
, ! , ,
SQL- T1 T2, .2.3. , X T1 INTEGER, T2 NUMERIC(5,1),
Y T1 NUMERIC(5,1),
T2 INTEGER. SQL-:
SELECT X , Y FROM T1
UNION
SELECT X , Y FROM T2

71

SQL

.2.3, X Y (. ) NUMERIC(5,1),

INTEGER NUMERIC(5,1). , , T1, T2, , - .1
T1 X
0
0

Y
1.0
2.0

T2 X
0.0
0.0
1.0

0
1
2

0.0
0.0
0.0
1.0

1.0
2.0
0.0
2.0

. 2.3.

: -
. ( ,
, SQL -.) SQL ; , ,
. , CAST . ( UNION):
SELECT CAST ( X AS NUMERIC(5,1) ) AS X , Y FROM T1
UNION
SELECT X , CAST ( Y AS NUMERIC(5,1) ) AS Y FROM T2

, , , SQL, . (
,
, .) :
tx , , t, 1

,
, , , . ,
, , .
SQL ,
, , , ,
-, (UNION),
, - ( ), ,
, , SQL
, .

72

2.

tx, r, t
r. : SQL . 12,
, , .
tx
, , t,
tx, ,
v, t , v. :
, , SQL (. 7).
rx rx sq ALL ANY ( ,
>ALL <ANY, sq ) , ,
, , . :
, ( , SQL . 3). , , ;
, . ALL ANY,
11.
, SQL , . .

SQL
SQL,
, , ,
. -,
,
: ()
() () (collation,
collating sequence). , ,
, . C S, a b -

SQL

73

S. C : a < b, a = b a > b TRUE, FALSE


( C).
. .
, PAD SPACE ( ) NO PAD ( ). , AB AB ( ) . , , , PAD
SPACE. : PAD SPACE, NO PAD. (, ,
PAD SPACE / NO PAD , .)
, a = b TRUE , a b
. , CASE_
INSENSITIVE, . , , .
, , SQL v1 = v2
TRUE , v1 v2 (, , ). , . , , , (, MATCH, LIKE, UNIQUE, UNION JOIN),
, . , CASE_
INSENSITIVE PAD SPACE. ,
PNO P SP
- P PNO P2, SP p2 ,
SP P, p .
, , , UNION, INTERSECT, EXCEPT, JOIN, GROUP BY DISTINCT,
, ,
. , SQL . SQL, , SQL -

74

2.

, ; ,
. , Z T
CASE_INSENSITIVE, SELECT MAX(Z)
FROM T ZZZ, zzz,
T .
SQL, , ( 12). , (. 8),
( , ). , , SELECT
, ! : .

SQL
:
VAR S_TUPLE
TUPLE { STATUS INTEGER , SNO CHAR , CITY CHAR , SNAME CHAR } ;

, TUPLE{...} TUPLE. SQL ROW


( ). SQL
:
DECLARE S_ROW /*
ROW ( SNO
SNAME
STATUS
CITY

SQL */
VARCHAR(5) ,
VARCHAR(25) ,
INTEGER ,
VARCHAR(20) ) ;

, SQL
1; 24 (= 4 * 3 * 2 * 1)
, (!).
SQL . , ,
TutorialD:
1

, SQL ,
ROW ( ), , .

75

SQL
SET S_ROW = ( S WHERE SNO = S1 ) ;

, ,
, , ,
(. SQL).

SQL- UPDATE (.
3).
: , SQL
( ) TABLE! RELATION,
. CREATE TABLE ,
. , , :
VAR S BASE
RELATION { SNO CHAR , SNAME CHAR , STATUS INTEGER , CITY CHAR }
KEY { SNO } ;

SQL:
CREATE TABLE S
( SNO
VARCHAR(5)
SNAME VARCHAR(25)
STATUS INTEGER
CITY VARCHAR(20)
UNIQUE ( SNO ) ) ;

NOT
NOT
NOT
NOT

NULL
NULL
NULL
NULL

,
,
,
,

,
,
TABLE. ( , , UNIQUE(SNO) , ,
SNO SNAME.) - S ( SQL),
,
ROW (SNO VARCHAR(5), SNAME VARCHAR(25), STATUS INTEGER,
CITY VARCHAR(20)).
- , SQL , . , , TT , T, TT, - T
! ,

76

2.

, SQL
, 1.
, , ,
. , , , , SQL; , , ( REF).
, ,
SQL; , - .
:
, , .

, SQL .
. , , ; , , . , , ! , ,
, , ,
, (
1, , ,
).


,
: , , , . , , , . ,
, ,
( , 1

, , . ( , ), , . : ()
V, V
V; () v ( ), v , v ( ).

77

). , , , .
, : .
,
, ,
. , , ,
,
.
,
,
. , . , r
T, r T
( !). . ,
; - ,
. ( , .)
, , .

2.1. ? ?
2.2. ?
?
2.3. THE_?
2.4. : ?
2.5.
( 1, ), :

78

2.


, .
2.6. ?
?
2.7. ?
2.8. ?
2.9. .
, ?
2.10. X . X?
, X - ?
2.11. REFLECT ( ?) , Tutorial D ,
.
2.12. Tutorial D ,
x y f(x) g(y), f g .
2.13. . , -
-.
2.14. Tutorial D SQL - P SP . SQL,
Tutorial D, , . , - P ( ) ?
2.15. , 1 (.1.1) :
DNO
: DNO
DNAME : NAME

79


BUDGET
ENO
ENAME
SALARY

:
:
:
:

MONEY
ENO
NAME
MONEY

, LOCATION
CITY ( ).
? ,
, ;
, .
a. LOCATION = London
b. ENAME = DNAME
c. SALARY * 5
d. BUDGET + 50000
e. ENO > E2
f. ENAME || DNAME
g. LOCATION || burg
2.16. , . , , . .
2.17. ,
. , ,
.
? SQL?
2.18. = . , SQL ,
= (
, , , ), , , .
?
2.19. ,
, v1 = v2 TRUE , Op v1 Op v2
Op. , SQL . ? ?
2.20. ?

80

2.

2.21. ,
,
v V V = v TRUE (.
5). SQL ( ). ? ?
2.22. ,
, -?
2.23. SQL- SELECT, , ,
, , .
?
2.24. , RVA-
.
2.25. ?
2.25. , =
.
SQL?

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

?
?
SNO CHAR SNAME CHAR
S1
Smith

STATUS

INTEGER
20

CITY CHAR
London

, , ( , ,
). 1 , ,
. , , ( ?):

82

3. ,
STATUS

INTEGER
20

SNAME CHAR
Smith

CITY CHAR
London

SNO CHAR
S1

, , ,
, ,
. , , :
: T1, T2, ..., Tn
(n 0), . Ti Ai; n
-/- . vi Ti;
n / . , n , t, ( ) A1, A2, ..., An. n t; 1 ,
2 , 3 , , , n n-.
, n , t.
, ,
S1, :
: 4. , 4.
: CHAR, CHAR, INTEGER, CHAR.
: SNO, SNAME, STATUS, CITY.
: S1, Smith, 20, London.
, , ; , ,
, .

, 2, SNO SNO ( ), CHAR. ,


SNO S1 S1; SNO(S1). SNO SNO,
CHAR! , , . (, 2 ,
SNO(S1) SNO.)

83

: :
SNO CHAR SNAME CHAR

STATUS

INTEGER

CITY

CHAR

, -
.
:
STATUS

INTEGER

SNAME CHAR

CITY

CHAR

SNO CHAR

: ,
? (: 4 * 3 * 2 * 1 = 24)
, ; , ,
( 2), , , .
Tutorial D TUPLE{H}, {H} . , :
TUPLE { SNO CHAR , SNAME CHAR , STATUS INTEGER , CITY CHAR }

( ).
: . , , - (, ,
).
(- Tutorial D):
TUPLE { SNO S1 , SNAME Smith , STATUS 20 , CITY London }

( ). , Tutorial D
-/, ;
,
.
( , ,
):
TUPLE { SNO SX , SNAME James , STATUS STX , CITY SCX }

, SX, STX SCX CHAR, INTEGER


CHAR .
,
Tutorial D TUPLE, -/, , . , TUPLE Tutorial D :
,
, , . -

84

3. ,

RELATION (.
).


, .
null-. , , , ( )
, , 1, null
, SQL , ,
null-. : null- ,
, null1.
, null-,
; ,
null. .
, . (:
A A B, ,
A B , , . , ,
.
, .) S1, ,
{SNO,CITY} , ( 2):
SNO CHAR CITY
S1

CHAR

London

, TUPLE {SNO
CHAR, CITY CHAR}.
:
SNO CHAR
S1

, null-
. . .

85

1 TUPLE {SNO CHAR}. ,


S1, -
. Tutorial D SNO FROM t ( t , SNO). SQL : t.SNO. : 2 , t r, t , . , v t,
, ; , .
, , . , ,
, , , , . TUPLE{} (
); 0-,
, 0.
. ,
, , , ,
, .
TABLE_DUM TABLE_DEE.
, ,
. (, 2 , = , .)
, ,
( ,
, ).
,
, , .
:
: tx ty
, A1,
A2, ..., An ,
i (i = 1, 2, ..., n) vx
Ai tx vy Ai ty.
( , - ), ,
.
, , 0- . 0-, - 0-.

86

3. ,

, , < > . , (, ), .

SQL
SQL , ; , , , , TUPLE Tutorial
D. ( , , 2.) , , ,
: . , ROW(1,2) ROW(2,1) , SQL . : ROW SQL .
SQL ( )
, . , ,
( , SQL ):
( S1 , Smith , 20 , London )

, ( ) Smith; ,
SNAME, , .
, SQL
, 0-
.
2, SQL 1. , ( ) SQL- UPDATE. , UPDATE
UPDATE S
SET
STATUS = 20 , CITY = London
WHERE CITY = Paris ;
,
, , .
SQL .

87

SQL

(
):
UPDATE S
SET ( STATUS , CITY ) = ( 20 , London )
WHERE CITY = Paris ;

, SQL, ( , ) , , . SELECT, WHERE :


SELECT SNO
FROM S
WHERE ( STATUS , CITY ) = ( 20 , London )

SELECT :
SELECT SNO
FROM S
WHERE STATUS = 20 AND CITY = London


SELECT SNO
FROM S
WHERE ( STATUS , CITY ) <> ( 20 , London )

:
SELECT
FROM
WHERE
OR

SNO
S
STATUS <> 20
CITY <> London

OR !
, , SQL
< > , :
SELECT SNO
FROM S
WHERE ( STATUS , CITY ) > ( 20 , London )

:
SELECT
FROM
WHERE
OR (

SNO
S
STATUS > 20
STATUS = 20 AND CITY > London )

1, :

88

3. ,
SELECT SNO
FROM S
WHERE ( STATUS ) = ( 20 )


. ,
, :
SELECT SNO
FROM S
WHERE STATUS = 20

WHERE (STATUS 20 ). , ,
SQL , , ; STATUS = 20 -
( ).
:
( ),
<, <=, > >=;
, , , . (
, SQL, ,
.)

?

. :
SNO CHAR SNAME CHAR
S1
S2
S3
S4
S5

Smith
Jones
Blake
Clark
Adams

STATUS

INTEGER
20
10
30
20
30

CITY

CHAR

London
Paris
Paris
London
Athens

:
: {H} , t1,
t2, ..., tm (m 0) {H}.
r, {H}
{t1, t2, ..., tm}, - (
) A1, A2, ...,
An, A1, A2, ..., An , {H}. r {H}; r (, , ),

89

. r {t1,
t2, ..., tm}. m r.
.
, , .
, n-
(relationship) n ( ), , (relation). ,
, (relating)
- , . ,
,
, .
, , , ,
. Tutorial D
RELATION{H}, {H} , :
RELATION { SNO CHAR , SNAME CHAR , STATUS INTEGER , CITY CHAR }

( .) ,
-
, :
RELATION
{ TUPLE {
TUPLE {
TUPLE {
TUPLE {
TUPLE {

SNO
SNO
SNO
SNO
SNO

S1
S2
S3
S4
S5

,
,
,
,
,

SNAME
SNAME
SNAME
SNAME
SNAME

Smith
Jones
Blake
Clark
Adams

,
,
,
,
,

STATUS
STATUS
STATUS
STATUS
STATUS

20
10
30
20
30

,
,
,
,
,

CITY
CITY
CITY
CITY
CITY

London
Paris
Paris
London
Athens

}
}
}
}
}

,
,
,
,
}

. (
, ):
RELATION { tx1 , tx2 , tx3 }

, tx1, tx2 tx3 -


. ,
Tutorial D RELATION,
-
.

,
, 1,
,
,
. , , :

90

3. ,

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


:
( ,
). , ,
( ). , ,
. -
SP , , ( , ; , ):
SNO

PNO

QTY

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

PNO

QTY

S1

P1

300

(- ,
1).
, -
. Tutorial D TUPLE FROM rx ( rx , 1, RELATION
{TUPLE {SNO S1, PNO P1, QTY 300}}, ). , SQL

n-

91

: () tx , ( ,
), () , t,
tx, r ()
r. :
SET S_ROW = ( S WHERE SNO = S1 ) ;

, t r. Tutorial D :
t r

TRUE, t
r, FALSE . , t r
t r. , , , SQL IN , SQL IN
, , (
). ( , ):
SELECT SNO , SNAME , STATUS , CITY
FROM S
WHERE SNO IN ( SELECT SNO
FROM SP )

n-
,
, . ( ,
.) , ,
. , 1 , ,

,
. , , , .
, , , , ,
.
. r n , r n- (
). ,
4-

92

3. ,

, . , n-, .1
( ): .


, ,
=; ,
rx ry T, , , , . ; , , rx ry ( , , rx, ry) ry ( , rx, ry, ry , rx). Tutorial D:
S { CITY } = P { CITY }

CITY, CITY, TRUE,


, FALSE . , ( )
: , ,
, .
:
S { SNO } SP { SNO }

. :
, ?
(), () ( ). = rx
; , , rx.
:
IS_EMPTY ( rx )

, TRUE, ,
rx, , FALSE , , ( , ), ,
, .

TABLE_DUM TABLE_DEE

93

. ( 8). :
IS_NOT_EMPTY ( rx )

NOT (IS_EMPTY(rx)).

TABLE_DUM TABLE_DEE
,
, , , , (
0-), , , .
, ,
, ?
RELATION{}, .
, r 0.
? : . -, r (
) ,
. -, r ,
0-.
0-! , , 0- , r . , : ,
.
( , , ).
? , .
, ?
( , ,
), - ? ,
, ,
: TABLE_DUM TABLE_DEE,
DUM DEE (DUM , DEE
).1 : FALSE () DUM
, . , , -, ,
: Tweedledum Tweedledee
,
( . -- -). -, , , ,
,
! ,
- .

94

3. ,

TRUE () DEE.
. :
5 6.
, , ,
: DEE yes E, DUM no
.
DUM DEE, .
,
: ( TABLE_
DEE) , . , ; ,
( ,
). ,
TABLE_DEE. SQL
SQL 0-, , ( ), TABLE_DUM TABLE_DEE.

SQL


SQL, (,
CREATE TABLE). 5.

2 , SQL
; SQL (
, ),
. , SQL ,
RELATION, , , ARRAY, MULTISET 3 ROW. , , - . :
VALUES ( 1, 2 ), ( 2, 1 ), ( 1, 1 ), ( 1, 2 )

(
, SQL ) ! .
. , SQL-
; , ( ) ,
.

:

95

SQL
VALUES (
(
(
(
(

S1
S2
S3
S4
S5

,
,
,
,
,

Smith
Jones
Blake
Clark
Adams

,
,
,
,
,

20
10
30
20
30

,
,
,
,
,

London
Paris
Paris
London
Athens

)
)
)
)
)

,
,
,
,

, ,
( , , - S, .1.3), :
1. , VALUES,
, . (
, i- - , , i- ).
2. , .

,
. , SQL, , , (, ). , , (?) .

?
,
5 7. , SQL
, . , ,
SQL:
NOT EXISTS ( SELECT
EXCEPT
SELECT
AND
NOT EXISTS ( SELECT
EXCEPT
SELECT

CITY FROM S
CITY FROM P )
CITY FROM P
CITY FROM S )

S{SNO} SP{SNO}:
EXISTS ( SELECT SNO
EXCEPT
SELECT SNO
AND
NOT EXISTS ( SELECT
EXCEPT
SELECT

FROM S
FROM SP )
SNO FROM SP
SNO FROM S )

96

3. ,

SQL
()
( ) ()
(
). SQL , . , ,
, CREATE TABLE CREATE
VIEW , 1.
: AS,
, ()
() . :
SELECT DISTINCT SNAME , Supplier AS TAG
FROM S
SELECT DISTINCT SNAME , 2 * STATUS AS DOUBLE_STATUS
FROM S
SELECT MAX ( WEIGHT ) AS MBW
FROM P
WHERE COLOR = Blue
CREATE VIEW SDS AS
SELECT DISTINCT SNAME , 2 * STATUS AS DOUBLE_STATUS
FROM S ;
SELECT
FROM
WHERE
AND

DISTINCT S.CITY AS SCITY , P.CITY AS PCITY


S , SP , P
S.SNO = SP.SNO
SP.PNO = P.PNO

SELECT TEMP.*
FROM ( S JOIN P ON S.CITY > P.CITY ) AS TEMP
( SNO , SNAME , STATUS , SCITY ,
PNO , PNAME , COLOR , WEIGHT , PCITY )

, ,

. , 1

, SQL . , ; , ,
,
, . , ,
(. 12). , ,
.

SQL

97

(,
WHERE HAVING) :
SELECT MAX ( WEIGHT )
FROM P
WHERE COLOR = Blue

,
, VALUES.
: , . , 6 , UNION , (, , );
. , , ! ,
SQL ,
SQL.
:
, . ( ,
, SNO, , , SNO
SNUM .) , , .
, , ,
. , ,
EMP,
, . -, ENO MNO. , :
( SELECT ENO , MNO FROM EMP ) AS TEMP1
NATURAL JOIN
( SELECT ENO AS MNO , ... FROM EMP ) AS TEMP2
/* ... EMP, ENO MNO */

SQL ,
, ( , - ). :
, , .

98

3. ,

, .
, (,
, , ).
, , SQL
. (, SQL
.) , , SQL , (
);
.
: SQL ,
. , SQL , :
SELECT *
JOIN, UNION, INTERSECT, EXCEPT ,
CORRESPONDING (.
6)
, ,
(range variable) (. 12)
, , CREATE VIEW (. 9)
INSERT,
ALL ANY,
1 (. 11)
VALUES



. ,
, , ,
, . ,
, , , ( , TABLE_DUM TABLE_DEE).
, SQL ,
.
( ,
) SQL.
.

99

3.1. , ,
, , , ,
.
3.2. , ,
: () , ()
.
3.3. Tutorial D
() - , () - . SQL, .
3.4. Tutorial D
. SQL,
.
3.5. ( , 1.8 1, ,
.) .
, .
3.6. (
; - ?). () ,
; () , (RVA).
3.7. () RVA-
, () RVA-.
, ,
RVA-. RVA-
, ,
, RVA-.
3.8. TABLE_
DUM and TABLE_DEE. SQL?
3.9. TABLE_DEE TRUE, TABLE_DUM FALSE.
,
BOOLEAN? , DEE DUM , . ,
- 0?
3.10. ( )
SQL:
VALUES ( 1, 2 ), ( 2, 1 ), ( 1, 1 ), ( 1, 2 )
VALUES ( ( 1, 2 ), ( 2, 1 ), ( 1, 1 ), ( 1, 2 ) )

100

3. ,

3.11. SQL?
SELECT SNO
FROM S
WHERE ( NOT ( ( STATUS , SNO ) <= ( 20 , S4 ) ) ) IS NOT FALSE

3.12. , .
3.13. ,
SQL .
3.14. SQL IS_NOT_
EMPTY(rx) Tutorial D.
3.15.
SQL.
3.16. , ,
AS. . ,
X AS..., ...AS X,
AS , 1.
, AS,
: X AS ... ...AS X , .

,
. ,
SQL , .
, AS, AS
AS, AS .

4
.

, null-
():
-,
(),
.
null-,
,
null-.
, , , . . : , SQL,
, ,
SQL, ( , ). , ,
.

?
, ( ) .
,
.1 ,
, :
1

, ( ,
) ,
, ,
, 1 .
.

102

4. , null-

1. , ,
,
(
).
2. , ,
. exp1 (, , ) exp2 , ,
exp2 ( ,
). : , .
. ,
, , , , SQL - , . () ,
.4.1. ,
( ).
: ,
,
, , ,
P PNO
P1
P1
P1
P2

PNAME
Screw
Screw
Screw
Screw

SP SNO
S1
S1
S1

PNO
P1
P1
P2

.4.1.

, : P (P1,Screw), ,
, , ? - ,
? - :
, . , - , , . ,
, ,
Double Trouble, Double Trouble (. D).

103

, , 1.
, - , . ( ,
, , ,
, .
, - .) , , - ,
, P (P1,Screw), , , - -. , ()
?
: , (screw),
S1, .
SQL :
1. SELECT P.PNO
FROM P
WHERE P.PNAME = Screw
OR
P.PNO IN
( SELECT SP.PNO
FROM SP
WHERE SP.SNO = S1 )

: P1 * 3, P2 * 1.
2. SELECT SP.PNO
FROM SP
WHERE SP.SNO
OR
SP.PNO
( SELECT
FROM
WHERE

= S1
IN
P.PNO
P
P.PNAME = Screw )

: P1 * 2, P2 * 1.
3. SELECT P.PNO
FROM P, SP
WHERE ( SP.SNO = S1 AND
SP.PNO = P.PNO )
OR
P.PNAME = Screw

- , : !, : ,
.

104

4. , null-

: P1 * 9, P2 * 4.
4. SELECT SP.PNO
FROM P, SP
WHERE ( SP.SNO = S1 AND
SP.PNO = P.PNO )
OR
P.PNAME = Screw

: P1 * 8, P2 * 4.
5. SELECT P.PNO
FROM
WHERE
UNION
SELECT
FROM
WHERE

P
P.PNAME = Screw
ALL
SP.PNO
SP
SP.SNO = S1

: P1 * 5, P2 * 2.
6. SELECT DISTINCT P.PNO
FROM
WHERE
UNION
SELECT
FROM
WHERE

P
P.PNAME = Screw
ALL
SP.PNO
SP
SP.SNO = S1

: P1 * 3, P2 * 2.
7. SELECT P.PNO
FROM
WHERE
UNION
SELECT
FROM
WHERE

P
P.PNAME = Screw
ALL
DISTINCT SP.PNO
SP
SP.SNO = S1

: P1 * 4, P2 * 2.
8. SELECT P.PNO
FROM P
WHERE P.PNAME = Screw
OR
P.PNO IN
( SELECT SP.PNO
FROM SP
WHERE SP.SNO = S1 )

: P1 * 3, P2 * 1.
9. SELECT DISTINCT SP.PNO
FROM SP
WHERE SP.SNO = S1
OR
SP.PNO IN
( SELECT P.PNO

105

FROM P
WHERE P.PNAME = Screw )

: P1 * 1, P2 * 1.
10. SELECT
FROM
GROUP
HAVING
OR

P.PNO
P
BY P.PNO, P.PNAME
P.PNAME = Screw
P.PNO IN
( SELECT SP.PNO
FROM SP
WHERE SP.SNO = S1 )

: P1 * 1, P2 * 1.
11. SELECT P.PNO
FROM P, SP
GROUP BY P.PNO, P.PNAME, SP.SNO, SP.PNO
HAVING ( SP.SNO = S1 AND
SP.PNO = P.PNO )
OR
P.PNAME = Screw

: P1 * 2, P2 * 2.
12. SELECT P.PNO
FROM
WHERE
UNION
SELECT
FROM
WHERE

P
P.PNAME = Screw
SP.PNO
SP
SP.SNO = S1

: P1 * 1, P2 * 1.
( , ? , , . .)
, ,

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

106

4. , null-

1 3 , . , -
. .
,
,
.
, .
.
, ,
, , ,
.
, , , , , .
:
.
, , .

,
.
, , DISTINCT,
(,
,
!). -
.

:
, . , , , , , .
-, ; , SQL
. ,
, -

107

SQL

. , ,
, (,
):
SELECT
FROM
WHERE
(

SNO
S
SNO IN
SELECT SNO
FROM SP )

|
|
|
|
|

SELECT SNO
FROM S NATURAL JOIN SP


, . (:
?)
.4.1 , ; ,
,
.
, ,
( (Jonathan Gennick), ): ,
(. 3),
, n- , , .
. , T . ,
! , , , ,
T ? (, SQL
.) , ? ,
, .

SQL
, SQL ,
, . ,
(. 5),
. , , SQL-
, . , :
SELECT ALL
UNION ALL
VALUES (.. )
VALUES . 3. ALL , ( DISTINCT) :

108

4. , null-

SELECT, SELECT.
, , (UNION, INTERSECT EXCEPT
).
, SUM,
-.

DISTINCT UNION, INTERSECT EXCEPT;


ALL.

; ALL,
, , ,
(. 7). , , , SQL . , : DISTINCT, ,
ALL. - .
. ? ,
:
, , DISTINCT ,
, SQL.
DISTINCT. : , SQL,
, SELECT DISTINCT . ,
() , , -, , SQL, () , , ,

DISTINCT.1 SELECT DISTINCT SNO FROM
S ..., DISTINCT .
EXISTS (SELECT DISTINCT ...) IN (SELECT DISTINCT ...),
DISTINCT . SELECT
DISTINCT SNO FROM SP ... GROUP BY SNO, DISTINCT
. SELECT DISTINCT ... UNION

, SELECT DISTINCT
, SELECT ALL, DISTINCT
. , , , ,
DISTINCT , , ( ,
).

null-?

109

SELECT DISTINCT ... . , , , , DISTINCT , ?


SQL , , ?
. ,
( , ) , ,
. , ,
, . , .

, ( ) : -
, , , SQL , . -, ,
, , -,
. -, - ,
DISTINCT (, ,
). ALL!

null-?
? , ,
. , null- .
, .
, , :
1. , , , null,
UNKNOWN, TRUE FALSE.
, null , A , , A > B,
B. ,
(3VL): null ,
SQL, ,
. (,
, 2VL.)
2. ,
,
, NOT, AND OR (T = TRUE, F = FALSE, U = UNKNOWN):

110

4. , null-
p

NOT p

T
U
F

F
U
T

p q
T
T
T
U
U
U
F
F
F

T
U
F
T
U
F
T
U
F

p q

p AND q

T
T
T
U
U
U
F
F
F

T
U
F
U
U
F
F
F
F

T
U
F
T
U
F
T
U
F

p OR q
T
T
T
T
U
U
T
U
F

, , NOT UNKNOWN,
UNKNOWN; AND UNKNOWN,
UNKNOWN, UNKNOWN,
TRUE; OR UNKNOWN,
UNKNOWN, UNKNOWN, FALSE.
. , , , ,
, .
() , .4.2, P1 CITY null.
, CITY, -; ( P1 , ).
S SNO
S1

CITY
London

P PNO

CITY

P1

.4.2.

(, )
. 4.2: (SNO,PNO), , ( ).
SQL:
SELECT
FROM
WHERE
OR

S.SNO, P.PNO
S, P
S.CITY <> P.CITY
P.CITY <> Paris

WHERE:
( S.CITY <> P.CITY ) OR ( P.CITY <> Paris )

null-?

111

( .) ,
, UNKNOWN OR UNKNOWN, UNKNOWN. SQL- , WHERE TRUE, FALSE
UNKNOWN; , .
P1 - 1; , , P1 CITY null,
, -, , , ,
xyz. xyz , . ,
( S.CITY <> P.CITY ) OR ( P.CITY <> Paris )

( , )
( London <> Paris ) OR ( Paris <> Paris )

TRUE, TRUE.
, xyz , (-
)
( London <> xyz ) OR ( xyz <> Paris )

, , TRUE, TRUE. , TRUE,


(S1,P1) ,
null. , (
) , ,
!

P, . 4.2 ( , , ,
):
SELECT PNO
FROM P
WHERE CITY = CITY

, , ,
, P ( , , P1, ,
.4.2). SQL .
. null-, . , , ,
1

, -. ,
. . 5.

112

4. , null-

; . , , null-. ,
.

,
SQL UNKNOWN, TRUE FALSE BOOLEAN! ( SQL; ,
.) , BOOLEAN ; null.
:

UNKNOWN X BOOLEAN
X null.
X = UNKNOWN TRUE, null (
SQL , , , , X UNKNOWN).
X = UNKNOWN null ( UNKNOWN)
X,
X = NULL ( ).
, , null
.

, null-
,
. , , , null . , :
, null, ( ).
, null, (
).
, null, (
, null).
, null-
, (. A).
, , null-,
( ,

null- SQL

113

, ); ,
.

null- SQL
null-;
SQL , , . ,
NOT NULL, (.
5); null- . , , SQL- ,
null. , null-
.
SQL, SUM, null,
(
COUNT COUNT(*), ).
,
null.
,
, null. : C null- null-
(, ),
SQL , ,
.
.
(union join) , null-.
CASE ELSE, ,
ELSE ELSE NULL.
x = y, NULLIF(x,y) null.
ON DELETE SET NULL ON
UPDATE SET NULL null- ( ).
:
NOT NULL, ,
.
NULL
( , NOT NULL).
UNKNOWN .

114

4. , null-

ELSE CASE, ,
.
NULLIF.
OUTER, FULL, LEFT RIGHT (, , , ).
.
PARTIAL FULL MATCH ( null-).
MATCH IS DISTINCT FROM.
( null- x IS DISTINCT FROM y x <> y.)
IS TRUE, IS NOT TRUE, IS FALSE IS
NOT FALSE. , bx ,
null- :
bx
bx
bx
bx

IS
IS
IS
IS

TRUE
NOT TRUE
FALSE
NOT FALSE

bx
NOT bx
NOT bx
bx

, IS TRUE null .
, COALESCE , null. ( ,
null .)
.
, COALESCE null , null, ( ,
). .
x, y, ..., z . COALESCE (x,y,...,z)
null, null, , null. , , x, y, ..., z
null. :
SELECT S.SNO , ( SELECT COALESCE ( SUM ( ALL QTY ) , 0 )
FROM SP
WHERE SP.SNO = S.SNO ) AS TOTQ
FROM S

, SUM
null , , , , COALESCE null . -

115

, ,
:
SNO

TOTQ

S1
S2
S3
S4
S5

1300
700
200
900
0


, null-, , ,
. , - : -,
, , ,
, (. 6). , ,
null- , , :
SELECT
FROM
UNION
SELECT
FROM
WHERE

SNO , PNO
SP
SNO , nil AS PNO
S
SNO NOT IN ( SELECT SNO FROM SP )

(
):
SNO

PNO

S1
S1
S1
S1
S1
S1
S2
S2
S3
S4
S4
S4
S5

P1
P2
P3
P4
P5
P6
P1
P2
P2
P2
P4
P5
nil

SQL-
COALESCE:
SELECT SNO , COALESCE ( PNO , nil ) AS PNO
FROM ( S NATURAL LEFT OUTER JOIN SP ) AS TEMP

116

4. , null-


null . , ,
, , , . , , , .
.
, null-
, . ! ,
, , null- ,
, , ,
.

, . : , null- ? ,
, - , , null-, (
),
. , , null-;
, .
-, , - ( , ) ,

- null-. ,
.
-, , , , null-, . , . , , null- . , ,
; , null, , . , , , .
, null-,
- . , ,

117

. SQL ( null). , ,
,
null-, , D.

4.1. , . , . ?
4.2. r , bx by
. ( ), (r WHERE bx)
UNION (r WHERE by) r WHERE bx OR by ( ). , r
, , ?
4.3. a, b .
( ) , a INTERSECT
(b UNION c) (a INTERSECT b) UNION (a INTERSECT c). , a, b c , ?
4.4. SQL,
FROM ( SELECT - FROM - WHERE), :
<clause from> ( from)
, <table reference> (
) [ <from clause>]. , CP, r, , r ,

, CP !
, : CP . , , T1 T2:
T1

C1
0
0

T2

C2
1
2

, CP T1 T2
( ,
):

118

4. , null-

CP1

C1

C2

0
0
0
0

1
1
2
2

CP2

C1

C2

0
0
0
0

1
2
2
2

?
4.5. :
DECLARE X CURSOR FOR SELECT SNO , QTY FROM SP ;

, () X , ()
, X, , ()
SP . ,
DELETE ... WHERE CURRENT OF X.
,
SP . ?
4.6. , (10100) :
.
4.7. , null- ?
4.8. null . ?
( , : ,
?)
4.9.
(NOT) (AND OR) , (. ). , MAYBE
:
p

MAYBE p

T
U
F

F
T
F

SQL?
4.10. , , (2VL)?
(3VL)? ?
4.11. ,
. ; , , , -

119

, +.
2VL ? 3VL?
4.12. bx . bx OR NOT bx
, 2VL
TRUE ( , ). 3VL? ,
3VL ?
4.13. , , bx
. bx AND NOT bx , 2VL FALSE ( ,
). 3VL? , 3VL ?
4.14. 2VL r JOIN r r, INTERSECT
TIMES JOIN (. 6).
3VL?
4.15. SQL: ROW (1,NULL).
null null?
4.16. bx SQL-. NOT (bx)
(bx) IS NOT TRUE SQL-.
?
4.17. x SQL-. x IS NOT NULL
NOT (x IS NULL) SQL-. ?
4.18. DEPT EMP SQL-, DNO , ENO EMP. DEPT.DNO = EMP.DNO AND EMP.DNO = D1
( , , WHERE).

DEPT.DNO = EMP.DNO AND EMP.DNO = D1 AND DEPT.DNO = D1,
, a = b b = c, a = c (. 6.13
6). ? , ? ?
4.19. ,
null-.
, , , SQL, (. 10):
S WHERE NOT EXISTS SP ( SP.SNO = S.SNO AND SP.PNO = P2 )

? ?
S WHERE NOT ( S.SNO IN ( SP.SNO WHERE SP.PNO = P2 ) )

120

4. , null-

4.20. k1 k2 .
SQL?
a. k1 k2 , WHERE.
b. k1 k2 .
c. k1 k2 .
4.21. , UNION ALL
.
INTERSECT ALL EXCEPT ALL?
4.22.
DISTINCT ALL ?
4.23. TABLE_DEE TRUE (), TABLE_
DUM FALSE (), UNKNOWN ( )?

-,

, ( ) -
.
; ,
, -,
. : , , SQL, ,
SQL
,
. , TABLE CREATE
TABLE, , , , , S , ,
S ( S). .
:
-, - , , -
, , INSERT, DELETE
UPDATE (, , , INSERT, DELETE UPDATE
).
, R -, r ,
R, R r (
).
, , , , , , 3 ,

122

5. -,

-
.
-
( SQL);
, ,
- -,
. 9
, -,
.


( TutorialD, SQL,
):
VAR S BASE RELATION
{ SNO
CHAR ,
SNAME CHAR ,
STATUS INTEGER ,
CITY CHAR }
KEY { SNO } ;

|
|
|
|
|
|

CREATE TABLE S
( SNO
VARCHAR(5)
SNAME VARCHAR(25)
STATUS INTEGER
CITY VARCHAR(20)
UNIQUE ( SNO ) ) ;

NOT
NOT
NOT
NOT

NULL
NULL
NULL
NULL

,
,
,
,

VAR P BASE RELATION


{ PNO
CHAR ,
PNAME CHAR
COLOR CHAR ,
WEIGHT FIXED ,
CITY CHAR }
KEY { PNO } ;

|
|
|
|
|
|
|

CREATE TABLE P
( PNO
VARCHAR(6)
PNAME VARCHAR(25)
COLOR CHAR(10)
WEIGHT NUMERIC(5,1)
CITY VARCHAR(20)
UNIQUE ( PNO ) ) ;

NOT
NOT
NOT
NOT
NOT

NULL
NULL
NULL
NULL
NULL

,
,
,
,
,

VAR SP BASE RELATION


{ SNO
CHAR ,
PNO
CHAR ,
QTY
INTEGER }
KEY { SNO , PNO }
FOREIGN KEY { SNO }
REFERENCES S
FOREIGN KEY { PNO }
REFERENCES P ;

|
|
|
|
|
|
|
|
|

CREATE TABLE SP
( SNO
VARCHAR(5) NOT
PNO
VARCHAR(6) NOT
QTY
INTEGER
NOT
UNIQUE ( SNO , PNO ) ,
FOREIGN KEY ( SNO )
REFERENCES S ( SNO )
FOREIGN KEY ( PNO )
REFERENCES P ( PNO )

NULL ,
NULL ,
NULL ,

,
) ;


, , .
(
,

123

- , ). , INSERT
-, DELETE , UPDATE . , , , , , :
, .
, 1 .
, , - S
(. 8), S1 S4 . UPDATE ,
,
. ,
, :
UPDATE S
WHERE SNO = S1
OR SNO = S4 :
{ CITY := New York } ;

|
|
|
|

UPDATE
SET
WHERE
OR

S
CITY = New York
SNO = S1
SNO = S4 ;

,
.
, SQL ( c UPDATE DELETE WHERE CURRENT OF cursor),
, , (, ), . SQL- , ,
. , , ,
, , . : SQL , , , , , . ( , , SQL .)
. , , ,
( ). 1. V , V , , ( ) .

124

5. -,

t1 t2 -
R, t1 R t2.
!
r1, R, r2. r2? :
s1 s2 , t1 t2 ; r2 (r1 MINUS s1) UNION s2. , t1 t2 - R
: t1, t2, ( , ) .
A t r, - R.
, , ( ), , , 1,
, , , .


, ,
, , ( . 8). , ( SQL). ,
SQL , , , ;
(. ). ,
, ,
, .


, ,
:
, ON DELETE CASCADE (.
), ,
, .
, , SQL
, ( ) , , ,
-. : ,
. , 1,
:

125


UPDATE S WHERE SNO = S5 :
|
{ CITY := New York } ; |
|

UPDATE S
SET
CITY = New York
WHERE SNO = S5 ;


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


-, , -,
. :
S := S WHERE NOT ( CITY = Athens ) ;

1 ,
DELETE TutorialD:
DELETE S WHERE CITY = Athens ;

, DELETE
DELETE R WHERE bx ;

( R -, bx )
:
R := R WHERE NOT ( bx ) ;

INSERT TutorialD
INSERT R rx ;

( R -, rx ) :
R := R D_UNION rx ;

D_UNION . , -
(. 6).

126

5. -,

, UPDATE Tutorial D ,
, INSERT DELETE, 7.

SQL
INSERT, DELETE UPDATE SQL
TutorialD,
, INSERT. SQL INSERT (, , VALUES . 3). , INSERT SQL ,
,
. INSERT SQL
. : . , INSERT
INSERT INTO SP ( PNO , SNO , QTY ) VALUES ( P6 , S5 , 700 ) ;

:
INSERT INTO SP VALUES ( S5 , P6 , 700 ) ;


SP, . :
INSERT INTO SP ( SNO , PNO , QTY ) VALUES ( S3 , P1 , 500 ) ,
( S2 , P5 , 400 ) ;

, SQL
. ,

R := rx ;

:
DELETE FROM T ;
INSERT INTO T ( ... ) tx ;

( T tx SQL- R rx ). , ,
, (
) , , T
, .



, , , -

127

. , , v V v = V TRUE. : , , . , , ,
, ()
. , -
, , .
8
.


1,
. .
: K
- R. K
( ) R, :
1. : R K.
2. : K .
K n , n K.
, . S {SNO,CITY} SK , , S, ( ,
- S, SK).
, CITY, {SNO} .
SK ,
. , {SNO} .
, ?
: , ,
. , ,
(!), {SNO,CITY} . , ,
, .

128

5. -,

,
, .
: SQL , , .
-, , .
( TutorialD) -
. .
VAR TAX_BRACKET BASE RELATION
{ LOW MONEY , HIGH MONEY , PERCENTAGE INTEGER }
KEY { LOW }
KEY { HIGH }
KEY { PERCENTAGE } ;
VAR ROSTER BASE RELATION
{ DAY DAY_OF_WEEK , TIME TIME_OF_DAY , GATE GATE , PILOT NAME }
KEY { DAY , TIME , GATE }
KEY { DAY , TIME , PILOT } ;
VAR MARRIAGE BASE RELATION
{ SPOUSE_A NAME , SPOUSE_B NAME , DATE_OF_MARRIAGE DATE }
/* , */
/* ... */
KEY { SPOUSE_A , DATE_OF_MARRIAGE }
KEY { DATE_OF_MARRIAGE , SPOUSE_B }
KEY { SPOUSE_B , SPOUSE_A } ;

, , , . ,
,
-/-. KEY
TutorialD , . , , , .
TutorialD, , .

. -, ,
-, .1 ? ,
1

, , .
, , , , ,
, .

129

, , , , , ,
. ( ,
, ,
. . 8.)
-, - , 1, (
-
). - , , ,
, . -, ,
, , , , , .
-, R -, R . , R , , -;
R , , . , ,
, . , . : SQL-,
SQL -, . :
PRIMARY
KEY / UNIQUE,
.
-, , ( SQL),
. , - S,
{SNO},
, S1
TUPLE { SNO S1 }

( 3, , .) , ,
S1 , , S1, .
, , , ,
. , , , ,
, ,

130

5. -,

- S, 1 .
-, SK -
R, ,
. SK R ( , , ). , {SNO}
{SNO,CITY} - S, . , -
R R.

. , 8 B, , ,
. . SK (, , ) -
R, A R. R
SK A

. SK A , R SK , A .
SK, SK , , ,
! A . , ,
, ( , )
-. ,
8 B.


(. 1),
-,
( ) -. , {SNO} {PNO} SP,
{SNO} S {PNO} P . (
, , , , -
SP , SNO S1, S , SNO
S1, SP ,
,
.) ( ):

131

: R1 R2 -,
, K R1. FK
R2 , , ,
R1, K K, K FK . R2 R1
, t2 R2 FK
K ( ) t1 R1 . FK
( , K), , R2 R1
- -, , .
,
, - , -,
. , , , ,
( SQL ).
, {SNO}
{PNO} SP, , S P
. :
VAR EMP BASE RELATION
{ ENO CHAR ,
MNO CHAR ,
... }
KEY { ENO }
FOREIGN KEY { MNO }
REFERENCES EMP { ENO }
RENAME ( ENO AS MNO ) ;

| CREATE TABLE EMP


|
( ENO VARCHAR(6) NOT NULL ,
|
MNO VARCHAR(6) NOT NULL ,
|
..... ,
|
UNIQUE ( ENO ) ,
|
FOREIGN KEY ( MNO )
|
REFERENCES EMP ( ENO ) ) ;
|

,
FOREIGN KEY. , TutorialD1. MNO ,
, ENO
(, EMP E3 MNO
1

, Tutorial D . (.
Inclusion Dependencies and Foreign Keys, D), , .

132

5. -,

E2,
EMP, E2). - ( R2)
-, (
R1) . :
, , ;
,
. ( ?
: ,
; ,
, , , , .) TutorialD
EMP, EMP{ENO} RENAME (ENO AS MNO). : RENAME , ,
.
SQL. , , SQL K
FK ,
. ( , .) , FOREIGN KEY, B1, B2, ..., Bn (
FK) A1, A2, ..., An ( K).1 Bi Ai (1 i n) , .
SQL
FOREIGN KEY ( MNO ) REFERENCES EMP ( ENO )

.
: , , ( 3). , ,
100:
T ,
T (, , EMP).
T2 , K T1.
1

A1, A2, ..., An -


UNIQUE PRIMARY KEY ,
, FOREIGN KEY. , FOREIGN KEY
, PRIMARY
KEY, UNIQUE , , , .

133


. ,
,
, K, (
). . 5.15 .


, , , SQL ,
, CASCADE.
ON DELETE ON UPDATE. , CREATE TABLE :
FOREIGN KEY ( SNO ) REFERENCES S ( SNO ) ON DELETE CASCADE


.
, , .
!
, . ,
, , , (, ,
). .
. 2 ,
, , , ,
.
. ,
( ), ( ),
. , ON
DELETE CASCADE,
, DELETE
(, , ), ON DELETE.1 , , , -
, , 1

, SQL ON DELETE CASCADE (referential triggered action),


CASCADE (referential action).

134

5. -,

, . : . : ,
SQL , .
,
, , . ( , , - ,
, ; .)
. ,
, ,
. , , , , 8. (
,
.)

-
, -
. ,
-. ,
-
, , - .
, , ,
, .
- S, .
, , -,
- . : - ,
- (, , ). :
SNO , SNAME,
STATUS CITY.
,
- S, .
, , . ,

135

, , : TRUE FALSE. , SNO, SNAME, STATUS CITY


( -),
(CHAR, CHAR, INTEGER CHAR).
. ,
S1, Smith, 20 London.
:
S1 , Smith, 20 London.
,
,
, . :
1. (The Monkey Wrench Gang) .
2. .
, . ,
, ! ,
, .
, - , -.
( -
S.)
- R P.
t, R ,
p, ( ) P
t .
( !) , p .
, - S, ,
:
S1 , Smith, 20 London.
S2 , Jones,
10 Paris.
S3 , Blake,
30 Paris.

136

5. -,

. :
-
- , , , .
,
TUPLE { SNO S6 , SNAME Lopez , STATUS 30 , CITY Madrid }

,
- S, , :
S6 , Lopez,
30 Madrid.
. -
R , ( -
R TRUE) ; ,
. , (
. A).
. P -
, - R,
r R . r r,
, P . , , - , .
:
, , SQL
, ( SQL IN, EXISTS ..). ,
, SQL , .

, . statement.
, 1. ,
.
, , 1

statement , , ,
, . . .

137

. , , , . Caveat lector.


2 .
. .
, , , ,
S1 ,
Smith, 20 London. , , ( S1, Smith, 20 London) , , . , :
,
; () .
, (, ),
. (, : ,
.) ,
, , :
, , , . ( , ,
).
, , , , , , , , . (,
, . , , S1, , S1
, Smith, 20
, .
, , ,
, . . ?
6.)

138

5. -,

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

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

3. . , , ! , ,
(, , ),
, , , . (,
, , .) , , , ;
, -
SQL-.
.
.
, . ,
(, ),
. , , , , ( ) , ( ) . , 1969 , , (
) , , . ,
-,
.

139

, ; , , . , ( , ),
, !
, ,
. , :
?
?
?
?
, ?
?
?
( ) , , , .
, , ,
, , , .
, - ,
.
, , .
, , ,
. A.

5.1. -
, ,
. .
5.2. ,
UPDATE
. , .
5.3. SQL
?

140

5. -,

5.4. SS ,
S. SQL- INSERT:
INSERT INTO SS ( SNO , SNAME , STATUS , CITY )
( SELECT SNO , SNAME , STATUS , CITY
FROM S
WHERE SNO = S6 ) ;
INSERT INTO SS ( SNO , SNAME , STATUS , CITY ) VALUES
( SELECT SNO , SNAME , STATUS , CITY
FROM S
WHERE SNO = S6 ) ;

? ,
?
5.5. (, , 2.21 2, .) . , SQL ? ,
?
5.6. SQL, - TAX_BRACKET, ROSTER
MARRIAGE .
5.7. ,
?
5.8. ,
. ?
5.9. , . .
5.10. - R n. R?
5.11. ?
, , , , ?
5.12. - EMP
-. -
-. , null-?
(: , , .) , null ?
5.13. SQL TutorialD?

141

5.14. , - R1 R2 ,
? , ?
5.15.
- ( PP),
(), ( ) ,
(, P1 4 P2).
PP TutorialD SQL. ,
?
5.16. SQL-
. ? ,
, ? ,
, ?
5.17. .
.
5.18. - P
SP .
5.19. ?
5.20. DB - ,
R - DB. R? : ,
.
, .
5.21. . ?
5.22. , ; ,
. ? ?
5.23. ,
; , . ?
5.24. - 0? ( ? .)
5.25. -
. ? ,
-?

6
.

SQL I:

,
; ( ,
1) , ( ). , SQL.


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

143

, , -
.
3. , ,
-
. , R1 R2 -, R1
UNION R2, , TutorialD ( , -
). R1 R2 - , , - . , , ,
- -, -
1,
-.
. , N INTEGER,
t 3. N + 2 , t 3 + 2, .
4. , ,
, INSERT, DELETE UPDATE ( ), , ,
, , , .
TutorialD,
,
SQL. , UNION JOIN, ,
Tutorial D , ,
(
). , TutorialD
:
P JOIN S

, , ,
CITY , P S.
SQL (
, , ):
SELECT P.PNO , P.PNAME , P.COLOR , P.WEIGHT , P.CITY /* S.CITY */ ,
S.SNO , S.SNAME , S.STATUS
1

SQL ! , T1 T2 SQL-,
, , T1 UNION T2,
SELECT * FROM T1 UNION SELECT * FROM T2.

144

6. SQL I:
FROM P , S
WHERE P.CITY = S.CITY

-, SQL .
. , TutorialD ( , , ,

CITY, P.CITY S.CITY):
SELECT P.PNO , P.PNAME , P.COLOR , P.WEIGHT , P.CITY /* S.CITY */ ,
S.SNO , S.SNAME , S.STATUS
FROM P JOIN S
ON
P.CITY = S.CITY
SELECT P.PNO , P.PNAME , P.COLOR , P.WEIGHT , CITY ,
S.SNO , S.SNAME , S.STATUS
FROM P JOIN S
USING ( CITY )
SELECT P.PNO , P.PNAME , P.COLOR , P.WEIGHT , CITY ,
S.SNO , S.SNAME , S.STATUS
FROM P NATURAL JOIN S

, , ,
SQL, ,
, SQL , TutorialD.
SQL , ,
, Tutorial D . :
SQL 12.
Tutorial D , . SQL
( RENAME,
Tutorial D ,
).
, Tutorial D , SQL;
, , , . : SQL 12.
, SQL , , ( ) . TutorialD .
, SQL
( ,

145

EXISTS). TutorialD
.
, SQL , ,
. (- . D, , , , P2, SQL
50 .)
SQL
: SELECT - FROM - WHERE. TutorialD . :
.
TutorialD, SQL.


,
. , ,
, . , , , (.
ORDER BY ). SQL
, , -, , , null-, ,
. !
,
, ( )
, , . : , , ,
.

, () ,
, , . ,
.

, , ,
, ;
, . , , (

146

6. SQL I:

; , , TutorialD, SQL, SQL- ):


( P JOIN S )
WHERE PNAME > SNAME

|
|
|
|

SELECT
FROM
WHERE
AND

P.* , S.SNO , S.SNAME , S.STATUS


P , S
P.CITY = S.CITY
P.PNAME > S.SNAME

, ,
, PNAME > SNAME (P.PNAME > S.SNAME SQL-),
. ,
. (
,
.)
.
PNAME > SNAME Tutorial D. PJOINS,
, PNAME SNAME
,
- P S. ,
? ? ,
,
? , - , ,
(, , ) (, , ) . .
, P JOIN S
:
RELATION { PNO CHAR , PNAME CHAR , COLOR CHAR , WEIGHT FIXED ,
CITY CHAR , SNO CHAR , SNAME CHAR , STATUS INTEGER }

, , - , ,
. ,
, CITY
, . , , ,
P JOIN S :
RELATION { SNO CHAR , PNO CHAR , SNAME CHAR , PNAME CHAR ,
CITY CHAR , STATUS INTEGER , WEIGHT FIXED , COLOR CHAR }

147

, - , ,
.
, ,
; ,
.
, RENAME, ,
- ;
, , ,
-
( ). .
: r , A r.
r RENAME (A AS B) , ()
r, , A B, () r ( ,
A B, ).
:
S RENAME ( CITY AS SCITY )

|
|
|

SELECT SNO , SNAME , STATUS ,


S.CITY AS SCITY
FROM S

:
SNO

SNAME STATUS

SCITY

S1
S2
S3
S4
S5

Smith
Jones
Blake
Clark
Adams

London
Paris
Paris
London
Athens

20
10
30
20
30

, ,
, .

: -
S ! RENAME
SQL- ALTER TABLE , RENAME
( , , , P JOIN S N + 2),
, , . , , ,
, .
.

148

6. SQL I:

SQL ? :
. -, 3 , SQL
( ). -,
SQL (, ,
SELECT DISTINCT 2 * WEIGHT FROM P). -,
(, , SELECT DISTINCT P.CITY, S.CITY FROM P, S).
: , 3, , SQL . , AS
, ()
() .
, . SQL (, , ) .
.
:
( P JOIN S )
WHERE PNAME > SNAME

|
|
|
|

SELECT
FROM
WHERE
AND

P.* , S.SNO , S.SNAME , S.STATUS


P , S
P.CITY = S.CITY
P.PNAME > S.SNAME

, PNAME > SNAME TutorialD SQL P.PNAME > S.SNAME,


,
FROM (. SQL ), - P S ! , , P.PNAME WHERE SELECT (,
, ) -
FROM. SQL , , , TutorialD, ,
, , . ,
SQL. , ,
- , .
. , ,
. , , , . -

149

, ,
.

: r , bx , , ,
r, -
. bx , r bx, r WHERE bx, , () r
() r,
bx TRUE.
:
P WHERE WEIGHT < 17.5

|
|
|

SELECT *
FROM P
WHERE WEIGHT < 17.5

r . r WHERE TRUE (,
, r WHERE bx, bx , 1 = 1, TRUE) r. .

TutorialD r WHERE bx, , , bx ,


. SQL. .

, (select), - SELECT SQL. SQL SELECT,


SELECT SELECT , UNGROUP, EXTEND, RENAME ( , ). :
UNGROUP EXTEND .

: r A, B, ..., C
r. r r{A,B, ... ,C}
, ()

150

6. SQL I:

{A,B,...,C} (b) x,
, r t,
A A x, B B x,
..., C C x.
:
P { COLOR , CITY }

|
|

SELECT DISTINCT COLOR , CITY


FROM P

, , , ,
, SQL DISTINCT .1
COLOR CITY ( SQL ).
r . r{H}, {H}
( , ) r, r.
.
, TutorialD , . , TutorialD :
P { COLOR , CITY }

P { ALL BUT PNO , PNAME , WEIGHT }

( 100
99 ).2 TutorialD,
.

. , TutorialD
S JOIN P { PNO }

S JOIN ( P { PNO } )


( S JOIN P ) { PNO }

: .
1

, , ( SQL),
, .

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

151

, . r1 r2 , , , ,
-
. , , . , ,
.
. r1 r2 . (
) r1 JOIN r2 , () - r1 r2 ()
t, , t -
r1 r2.

,
SQL- , :
P JOIN S

|
|
|
|

SELECT PNO , PNAME , COLOR , WEIGHT ,


P.CITY , SNO , SNAME , STATUS
FROM P , S
WHERE P.CITY = S.CITY

, , SQL
-, , TutorialD ( SELECT
*):
SELECT *
FROM P NATURAL JOIN S

, CITY, PNO, PNAME, COLOR, WEIGHT, SNO, SNAME


STATUS ( SQL, TutorialD, ).

. , , ( r1 INTERSECT r2
r1 JOIN r2 TutorialD). , ,
r1 r2 ,
. , INTERSECT .

152

6. SQL I:

, (
r1 TIMES r2 r1 JOIN r2 TutorialD).
, , r1 r2
. ? () , () ( , 0-) , , () r1 r2, , .
.
. ( ) r1 r2, r1 TIMES
r2, r1 r2 ,
, () - r1
r2 () t, , t - r1 r2.
:
( P RENAME ( CITY AS PCITY ) )
TIMES /* JOIN */
( S RENAME ( CITY AS SCITY ) )

|
|
|
|
|

SELECT PNO , PNAME , COLOR ,


WEIGHT , P.CITY AS PCITY,
SNO , SNAME , STATUS ,
S.CITY AS SCITY
FROM P , S

, CITY
. , , PNO, PNAME, COLOR, WEIGHT,
PCITY, SNO, SNAME, STATUS SCITY ( SQL ).
, , n-
( TutorialD ),
JOIN { r1 , r2 , ... , rn }

r1, r2, ..., rn.1 , :


JOIN { P , S }

, ,

1

, TutorialD n- INTERSECT TIMES.

153

! , JOIN{r}, ,
r; , , (?). , ,
, JOIN{}, ,
TABLE_DEE! (, TABLE_DEE ,
.)
TABLE_DEE? .
0 +, x x+ 0
0 + x x. ,
0. ( , , , : 0, . , n = 0?)
1 *, x x * 1 1 * x x. , 1.
TABLE_DEE JOIN, r
TABLE_DEE r (. ). ,
TABLE_DEE.
, .
, , r JOIN TABLE_DEE TABLE_DEE JOIN r
r. , , ,

(?).

JOIN SQL
SQL JOIN
( ). , , , ( t1 t2
, bx , C1, C2, ..., Cn ,
t1 t2):
1. t1 NATURAL JOIN t2
2. t1 JOIN t2 ON bx
3. t1 JOIN t2 USING ( C1 , C2 , ... , Cn )
4. t1 CROSS JOIN t2
,
, .

154

6. SQL I:

1. 1 .
2. 2 :
( SELECT * FROM t1 , t2 WHERE bx )

3. 3 2, bx
t1.C1 = t2.C1 AND t1.C2 = t2.C2 AND ... AND t1.Cn = t2.Cn

, C1, C2, ..., Cn


, ,
: C1, C2, ..., Cn (
), t1 ,
t1, t2 , t2. ( ,
?)
4. , 4 :
( SELECT * FROM t1 , t2 )

:
1. 1 (NATURAL JOIN)
( ,
). , NATURAL JOIN , , .
2. 2 (JOIN ON), ( ,
t1 t2 ).
2, , ,
1, , :
SELECT TEMP.*
FROM ( S JOIN P ON S.CITY > P.CITY ) AS TEMP
( SNO , SNAME , STATUS , SCITY ,
PNO , PNAME , COLOR , WEIGHT , PCITY )

3. 3 , .
4. 4 , .
t1 t2 , SQL . tr
. tr , tr AS, 1


-, .

155

, ,
( . 12). :
( SELECT SNO , CITY FROM S ) AS TEMP1
NATURAL JOIN
( SELECT PNO , CITY FROM P ) AS TEMP2

. , JOIN
SQL () ( ),
() , .

,
, (UNION, INTERSECT
MINUS TutorialD; UNION, INTERSECT EXCEPT SQL)
. .

: r1 r2
; r1 UNION r2
, , r1, r2 .
( ,
STATUS INTEGER):
P { STATUS , CITY } UNION
S { CITY , STATUS }

|
|
|
|
|

SELECT
FROM
UNION
SELECT
FROM

STATUS , CITY
P
CORRESPONDING
CITY , STATUS
S

, , . , SQL DISTINCT; UNION , SELECT (DISTINCT ALL), DISTINCT, ALL ( 4


, SELECT
). , ,
STATUS CITY ( SQL ). , CORRESPONDING SQL ,
- .
:
,
.

156

6. SQL I:

, , CORRESPONDING.1
(, ),
, :
SELECT STATUS , CITY FROM P
UNION
SELECT STATUS , CITY FROM S /* */
/* */

BY ( ) COR
RESPONDING,
(, BY (STATUS,CITY)
);
ALL. :
ALL UNION ,
-. , , , ,
, .
, . .
SQL 4.
Tutorial D (D_UNION) , ,
. :
S { CITY } D_UNION P { CITY }


, . SQL D_UNION.
, Tutorial D n-
UNION D_UNION. .

: r1 r2
; r1 INTERSECT r2
,
, r1 r2.
:
P { STATUS , CITY } INTERSECT
S { CITY , STATUS }

|
|
|

SELECT
STATUS , CITY
FROM
P
INTERSECT CORRESPONDING

CORRESPONDING,
.

157

?
|
|

SELECT
FROM

CITY , STATUS
S

, , . :
, . TutorialD, SQL ,
. ,
TutorialD n- , .

: r1 r2
; r1 MINUS r2
, , r1, r2.
:
P { STATUS , CITY } MINUS
S { CITY , STATUS }

|
|
|
|
|

SELECT
FROM
EXCEPT
SELECT
FROM

STATUS , CITY
P
CORRESPONDING
CITY , STATUS
S

, , .

?
, . , .
, , ( ), . : ,
. , , (. 7.3 7). ,
!
,
.


TutorialD (
,
):
( ( ( S RENAME ( SNO AS SA ) ) { SA , CITY } JOIN

158

6. SQL I:
( S RENAME ( SNO AS SB ) ) { SB , CITY } )
WHERE SA < SB ) { SA , SB }

, SA SB (, , ).
SA < SB :
(a,a);
,
(a,b) (b,a).
, , TutorialD WITH ,
:
WITH ( S RENAME ( SNO
( S RENAME ( SNO
R1 JOIN R2 AS R3
R3 WHERE SA < SB
R4 { SA, SB }

AS SA ) ) { SA , CITY } AS R1 ,
AS SB ) ) { SB , CITY } AS R2 ,
,
AS R4 :

, WITH TutorialD
WITH, expression
AS name, . expression AS name expression, () name. , ,
. , WITH , ( ). .
SQL WITH :
: WITH name
AS expression, , name AS expression.
- .
Tutorial D WITH
, SQL .
, SQL name name AS expression
(
(range variable), . 12),
,
.
SQL:

?
WITH T1 AS ( SELECT
FROM
T2 AS ( SELECT
FROM
T3 AS ( SELECT
FROM
T4 AS ( SELECT
FROM
WHERE
SELECT SA , SB
FROM T4

159

SNO AS SA , CITY
S ) ,
SNO AS SB , CITY
S ) ,
*
T1 NATURAL JOIN T2 ) ,
*
T3
SA < SB )

?
, :
- -
, -. , - S :
SNO , SNAME, STATUS CITY.
, . , ,
, CITY:
S { SNO , SNAME , STATUS }

,
TUPLE { SNO sno , SNAME sn , STATUS st }

, - S
TUPLE { SNO sno , SNAME sn , STATUS st , CITY sc }

sc CITY. ,
(. 5, , ):
CITY, SNO , SNAME, STATUS CITY.
, ,
() S{SNO,SNAME,STATUS}. ,
,
; CITY , , - ,
( -

160

6. SQL I:

. 10).1 : , ,
, , ,
, :
SNO , SNAME, STATUS ( ,
).
. : rx
, , rx , -,
, . ( SQL) , .

SQL
,
, ( =, , < ..). -
, . , ,
( ):
( ( S RENAME ( CITY AS SCITY ) )
TIMES
( P RENAME ( CITY AS PCITY ) ) )
WHERE SCITY PCITY

|
|
|
|
|
|

SELECT SNO , SNAME, STATUS,


S.CITY AS SCITY, PNO,
PNAME, COLOR, WEIGHT,
P.CITY AS PCITY
FROM S , P
WHERE S.CITY <> P.CITY

SQL-. , SQL .
1. FROM S P.
: ,
CITY . SQL
,
, CITY .
.
1

, CITY
, .
! : CITY.
Logic and Databases: The Roots of Relational Theory
(Trafford, 2007), . 387391.

161

2. WHERE , , . : =, ( SQL <>), : , , , . ,


-, =. :
?
3. , SELECT , SELECT. ( SELECT ,
, , SELECT , .)
, FROM
, WHERE ,
SELECT , , , SELECT - FROM - WHERE
. , ,
SELECT - FROM WHERE SQL. , . , , ; , , , . ,
, ,
, - . , ,
. , , ,
,
.


, . , , ,
. : 4
,
;
, (, , -

162

6. SQL I:

).
.
TutorialD ( :
, P2, , SQL ):
( ( S JOIN SP ) WHERE PNO = P2 ) { ALL BUT PNO }

, 100 1000000 ,
500 P2.
, ,
:
1. S SP. 100 ; 100 1 000 000
; , 1000000 , 1000000
. ( ,
. ,
.)
2. , 1. 1000000 ,
500 , , ,
. ( 1 ,
, 1000000 .)
3. , 2.
, ,
.
,
, , , .
1. SP P2.
1000000 , 500 , .
2. S , 1.
100 ( ,
P2, P2 ). 500 (
).
3. , 2. .

163

102000000 , 1000100. , 100 . , , , !



( S JOIN SP ) WHERE PNO = P2

( ,
) :
S JOIN ( SP WHERE PNO = P2 )

, , ,
. ,

, , , . , ,
, ; , , , , ,
( ). , . :
,
(, , , , ), , ,
, .
, .
. -, , ,
, . f g, f(g(a,b)) = g(f(a),f(b)) a b. , SQRT ,
SQRT ( a * b ) = SQRT ( a ) * SQRT ( b )

a b ( SQRT f * g).

. , SQRT , a + b a b.

164

6. SQL I:

, . , , (AND) ,
.
, .
. ,
, , , ,
.
,
. -,
, . -, , , , . , , .
: .
g , g(a,b) =
g(b,a) a b. ,
, . ,
, .1 , , r1 r2, , , , . , ( ).
g , g(a,g(b,c))=
g(g(a,b),c) a, b, c. , . , , . , , 1

, SQL , ( )
, ; , , , . ,
, SQL ,
.

165

r1, r2 r3,
. , .
, ,
( . .). ,
, .


, , , ,
. , , , , . , TutorialD R1 JOIN
R2, , R1 R2 -,
R1 R2 . :
? , ,
- R2
, R1?
. , , , , .
,
( , , ). , R1 JOIN R2 -
R1 R2 A, A , ,
.1 , ( ) RENAME.
. , , . SQL-
, , ( SQL). SQL , , ,
1

SQL , SQL
; TutorialD ,
, , .

166

6. SQL I:


. , . . :1
,
.
,
, , 1960- . ,
(, , . 9)
, ,
, . , , .
, , .
(
..), . , , .
SQL.
(public table), . :
CREATE PUBLIC TABLE X
/* ! */
( SNO VARCHAR(5) NOT NULL ,
SNAME VARCHAR(25) NOT NULL ,
CITY VARCHAR(20) NOT NULL ,
UNIQUE ( SNO ) ) ;
CREATE PUBLIC TABLE Y
/* ! */
( SNO VARCHAR(5) NOT NULL ,
PNO VARCHAR(6) NOT NULL ,
UNIQUE ( SNO , PNO ) ) ,
FOREIGN KEY ( SNO ) REFERENCES X ( SNO ) ) ;

Atsushi Ohori, Peter Buneman, Val Breazu-Tannen Database


Programming in Machiavelli A Polymorphic Language with Static Type Inference, Proc. ACM SIGMOD International Conference on Management of
Data, Portland, Ore. ( 1989).

167

, , , , X Y, . ,
S SP (
, X Y , ), :
X ::= SELECT SNO , SNAME , CITY FROM S ; /* !*/
Y ::= SELECT SNO , PNO FROM SP ;

/* !*/

( ::= ). ,
SNO. SP , , SNAME, , ! , . , .
, SQL . ,
, SQL- S NATURAL JOIN SP, ,
, (
SELECT * FROM S, ).
, ,
, 3.
:
, , .
, , 3.
, .
, .

6.1. ( ?)
SQL- ( , ):
a. SELECT * FROM S , SP
b. SELECT SNO , CITY FROM S
c. SELECT SNO , PNO , 2 * QTY FROM SP
d. SELECT S.SNO FROM S , SP
e. SELECT S.SNO , S.CITY FROM S NATURAL JOIN P

168

6. SQL I:

f. SELECT CITY FROM S UNION SELECT CITY FROM P


g. SELECT S.* FROM S NATURAL JOIN SP
6.2. , .
, , . ?
6.3. ,
JOIN{S,SP,P} TutorialD?
?
SQL?
6.4. ,
?
6.5. ,
Tutorial D?
() SQL () ( )
.
a. ( S JOIN ( SP WHERE PNO = P2 ) ) { CITY }
b. ( P { PNO } MINUS ( SP WHERE SNO = S2 ) { PNO } ) JOIN P
c. S { CITY } MINUS P { CITY }
d. ( S { SNO, CITY } JOIN P { PNO, CITY } ) { ALL BUT CITY }
e. JOIN { ( S RENAME ( CITY AS SC ) ) { SC } ,
( P RENAME ( CITY AS PC ) ) { PC } }
6.6. , , .
. SQL?
6.7. ?
6.8. SQL FROM t1, t2, ..., tn ( ti ) .
n = 1? ?
, , t1 t2, t1, t2 -?
6.9. TutorialD / SQL
:
a. .
b. ,
P1.

169

c. ,
15 25 .
d. , , .
e. , , .
f. , , .
g. , S1.
h. , ,
.
i. (SNO,PNO), , SNO PNO.
j. , , S2.
6.10. ( ,
):
a.
.
b. .
c.
.
6.11. , r UNION r r r. ( SQL?) . ?
6.12. r . r{}
TutorialD ( )?
? TutorialD r{ALL BUT},
?
6.13.
x > y AND y > 3

( ) (, , ) :
x > y AND y > 3 AND x > 3

, > . ,

170

6. SQL I:

, x y , (x > 3) ,
, x > y.
, ;
, .
- SQL, ?
6.14. TutorialD:
WITH ( P WHERE COLOR = Purple ) AS PP ,
( SP RENAME ( SNO AS X ) ) AS T :
S WHERE ( T WHERE X = SNO ) { PNO } PP { PNO }

? , . ? .
6.15. SQL D_
UNION. SQL?
6.16. ?
(
)?
6.17. n- JOIN UNION ( D_UNION)?
SQL? n- MINUS?
6.18. , TABLE_DEE TRUE,
TABLE_DUM FALSE. / .

SQL II:

,
, . 1 , , . 6
(, ..),
,
. ,
SQL.


.
, , , , (,
, ).
: r1 r2 (
), r1 MATCHING r2, (r1
JOIN r2){A,B,...,C}, A, B, ..., C r1.
, r1 MATCHING r2 r1 r2, r1. ( , ):
S MATCHING SP

|
|
|

SELECT S.* FROM S


WHERE SNO IN
( SELECT SNO FROM SP )

172

7. SQL II:

, S. , r1
MATCHING r2 r2 MATCHING r1 . , IN SQL MATCH; , NOT IN NOT MATCH
(. ) , SQL
NOT MATCH.
. -
, , , .
: r1 r2 (
), r1 NOT MATCHING r2, r1 MINUS
(r1 MATCHING r2).
( , ):
S NOT MATCHING SP

|
|
|

SELECT S.* FROM S


WHERE SNO NOT IN
( SELECT SNO FROM SP )

- , S. :
r1 r2 , r1 NOT MATCHING r2 r1 MINUS r2; ,
(MINUS) . , , ,
, . . 7.19 .

, ,
. SQL ; ,
: SELECT A + B AS C ... .
, +, . , , - , -
EXTEND. , , ( P) ,
. 454 , :
EXTEND P
ADD ( WEIGHT * 454 AS GMWT )

|
|
|

SELECT P.* ,
WEIGHT * 454 AS GMWT
FROM P

173

:
PNO

PNAME COLOR

P1
P2
P3
P4
P5
P6

Nut
Bolt
Screw
Screw
Cam
Cog

Red
Green
Blue
Red
Blue
Red

WEIGHT
12.0
17.0
17.0
14.0
12.0
19.0

CITY

GMWT

London
Paris
Oslo
London
Paris
London

5448.0
7718.0
7718.0
6356.0
5448.0
8626.0

: - P !
EXTEND SQL- ALTER TABLE;
EXTEND , , , .
,
,
7000 :
( ( EXTEND P ADD
( WEIGHT * 454 AS GMWT ) )
WHERE GMWT > 7000.0 )
{ PNO , GMWT }

|
|
|
|

SELECT PNO ,
WEIGHT * 454 AS GMWT
FROM P
WHERE WEIGHT * 454 > 7000.0

, SQL- WEIGHT * 454 , , , , ,


( ).
TutorialD .
, , ,
SELECT - FROM - WHERE SQL .
TutorialD
;
SQL WHERE SELECT. SELECT - FROM - WHERE WHERE
FROM, SELECT.
-: ( ) ,
.
SELECT - FROM - WHERE SQL ,
, ,
, / , / , / . , .
, , , SQL-
:

174

7. SQL II:
SELECT PNO , WEIGHT * 454 AS GMWT
FROM P
WHERE GMWT > 7000.0

( ). , GMWT
; P ,
WHERE .
SQL , TutorialD (
):
SELECT TEMP.PNO , TEMP.GMWT
FROM ( SELECT P.PNO , ( P.WEIGHT * 454 ) AS GMWT
FROM P ) AS TEMP
WHERE TEMP.GMWT > 7000.0

SQL- FROM. , (
TEMP) , ,
SQL-, , .
:
: r . EXTEND
r ADD (exp AS X) , ()
r, X, () t, t r, X, exp
r. r
X exp X. ,
r, r 1. X exp.

-
- , , (
- ). , S4 ( ) :
PNO

QTY

P2
P4
P5

200
300
400

175

, - TutorialD:
( SP WHERE SNO = S4 ) { ALL BUT SNO }

- .
: r1 r2 (
);
t1 r1, t2 r2, t1 , t1. r3 r2, t2,
r4 r3 , . r4
- ( r2), t1.
-:
S WHERE ( !!SP ) { PNO } = P { PNO }

, WHERE .
:
-, r1 r2
(
-
S, ).
, ,
WHERE t1 r1 ( ) .
, , Sx. !!SP
- r4 r2; ,
(PNO,QTY) SP ,
Sx. !!SP -.
(!!SP){PNO}, -
{PNO}, , ,
, Sx.
, ( S WHERE ...) S,
P {PNO}. , , ( ).

176

7. SQL II:

- ( t1), , - ,
, ,
, t1 .
WHERE, .
-.

SQL - . TutorialD ( , ,
, (?), ):
SELECT
FROM
WHERE
(

*
S
NOT EXISTS
SELECT PNO
FROM SP
WHERE SP.SNO = S.SNO
EXCEPT
SELECT PNO
FROM P )
AND
NOT EXISTS
( SELECT PNO
FROM P
EXCEPT
SELECT PNO
FROM P
WHERE SP.SNO = S.SNO )

-. , !!
MATCHING. ,

S WHERE ( !!SP ) { PNO } = P { PNO }

:
S WHERE ( SP MATCHING RELATION { TUPLE { SNO SNO } } ) { PNO } = P { PNO }

: S, , Sx. TUPLE{SNO SNO}


,
SNO Sx ( SNO , Sx -
S). ,
RELATION { TUPLE { SNO SNO } }

177


, . ,
SP MATCHING RELATION { TUPLE { SNO SNO } }

SP, ,
, SNO , Sx . .
, ( ) ( ):
S
SP
PJ
J

{
{
{
{

SNO }
/*
SNO, PNO } /*
PNO, JNO } /*
JNO }
/*

*/
*/
*/
*/

- J (JNO
), - PJ , .
(sno,jno), sno SNO, - S, jno JNO, - J, sno , jno. ! - :
( S JOIN J ) WHERE !!PJ !!SP


( , , SQL):
DELETE SP WHERE IS_NOT_EMPTY
( !!(S WHERE
CITY = London) ) ;

|
|
|
|

DELETE FROM SP
WHERE SNO IN
( SELECT SNO FROM S
WHERE CITY = London ) ;

- !!(S WHERE ...)


, .

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

178

7. SQL II:

, , , -, . -, , , , , , . , .
: r1 r2 , {Y} r2
r1, {X} r1. r1 r2, r1 DIVIDEBY
r2,1 :
r1 { X } NOT MATCHING ( ( r1 { X } JOIN r2 ) NOT MATCHING r1 )

,
SP { SNO , PNO } DIVIDEBY P { PNO }

( ) :
SNO
S1

,
, ( , ). , ,
, , , ,
:
( SP { SNO , PNO } DIVIDEBY P { PNO } ) JOIN S

, , -:
S WHERE ( !!SP ) { PNO } = P { PNO }

() , () (
, ) () . , , .
, .
, r1 r2 ,
, r1 TIMES r2,
r2, r1.2 , .
1

TutorialD ,
r1 DIVIDEBY r2 .

, r2 . , ?

179

,
SP { SNO , PNO } DIVIDEBY P { PNO }

, .
,
. , , . ,
.1 , , , .


- , ,
.2 , .
, (
) - .
COUNT(*), ,
. :
X := COUNT ( S ) ;

|
|

Y := COUNT ( S { STATUS } ) ; |
|
|

SELECT COUNT ( * ) AS X
FROM S
SELECT COUNT ( DISTINCT STATUS )
AS Y
FROM S

TutorialD .
X 1

, ,
, (, , , ). ,
! S5,
, , -
SP, DIVIDEBY. - , 11,
.

,
.

180

7. SQL II:

5 (
- S), Y
3 ( -
S {STATUS}, STATUS -).
TutorialD :
<agg op name> ( <relation exp> [, <exp> ] )

<agg op name> COUNT, SUM, AVG,


MAX, MIN, AND, OR XOR ( ,
). <exp> ,
, <attribute
ref>. <exp> , <agg op name> COUNT; , <relation exp> 1, , <exp> . :
1. SUM ( SP , QTY )
QTY SP ( 3100).
2. SUM ( SP { QTY } )
SUM(SP{QTY},QTY),
QTY - SP
( 1000).
3. AVG ( SP , 3 * QTY )
, , ( 775). ,
agg ( rx , x )

( x , , <attribute
ref>) :
agg ( EXTEND rx ADD ( x AS y ) , y )

SQL.
:
X := COUNT ( S ) ;

|
|

Y := COUNT ( S { STATUS } ) ; |
|
|

SELECT COUNT ( * ) AS X
FROM S
SELECT COUNT ( DISTINCT STATUS )
AS Y
FROM S

181

, , SQL
! , ,
SQL.1 .
. , 5 3. SQL- ,
, , . ,
, :
X

, SELECT
; , . SQL . , , , SQL SELECT GROUP BY. , SQL- GROUP BY , (
, ):
SELECT COUNT ( * ) AS X
FROM S
GROUP BY ( )
SELECT COUNT ( DISTINCT STATUS ) AS Y
FROM S
GROUP BY ( )

, , , SQL () GROUP BY () GROUP BY . GROUP BY ,


.

SQL , . , , , , . - ,

, , COUNT
. ,
SQL ;
- .

182

7. SQL II:

SQL , , , , . ( , ) !
, , SELECT
, , ,
:
SET X = ( SELECT COUNT ( * ) FROM S ) ;
SET Y = ( SELECT COUNT ( DISTINCT STATUS ) FROM S ) ;

, (. 2 12).

, SQL (
, , SQL, ):
SELECT - FROM T - WHERE - GROUP BY - HAVING
, G,
G ,
WHERE, GROUP BY HAVING T.
WHERE HAVING,
SQL, WHERE TRUE HAVING TRUE . GROUP
BY G.
, T nT .
nT . , G nG , nG nT, SELECT G nG .
, nT ( T ); nG,
, ( G , , SELECT ).
, ,
SELECT COUNT ( * ) AS X
FROM S
GROUP BY ( )

, , SELECT COUNT(*) AS X FROM S,


, , , ,
S .
X

X
0

183

, . ? :
. : , , <group by
clause> , T . , SQL
( )
, ,
, ; , , T. , COUNT , ,
0.
, , ; , , - .
( ) , ,
.
, , ,
.

-
,
, , -. : - , , ,
(
).
1. ,
, , 1000.
S WHERE SUM ( !!SP , QTY ) < 1000

SUM(!!SP,QTY) , . - :
S WHERE SUM ( SP MATCHING RELATION { TUPLE { SNO SNO } } , QTY ) < 1000

SQL , : SQL-
TutorialD (?):
SELECT S.*
FROM S , SP
WHERE S.SNO = SP.SNO

184

7. SQL II:
GROUP BY S.SNO , S.SNAME , S.STATUS , S.CITY
HAVING SUM ( SP.QTY ) < 1000

, ( ) SQL S.* SELECT, GROUP BY,


.

2. ,
.
S WHERE COUNT ( !!SP ) < 3

3. , (
).
S WHERE MAX ( !!SP , QTY ) < 2 * MIN ( !!SP , QTY )

4. , .
SP WHERE COUNT ( !!(SP RENAME ( SNO AS SN , PNO AS PN )) ) > 2

, , , , - . , -, ,
QTY. SN PN .
, RENAME:
SP RENAME ( SNO AS SN , PNO AS PN )

:
( SP RENAME ( SNO AS SN ) ) RENAME ( PNO AS PN )

,
EXTEND ( ).
5. , , , 1000,
.
UPDATE S WHERE SUM ( !!SP , QTY ) < 1000 :
{ STATUS := 0.5 * STATUS } ;

185

: r1 r2 , r2 r1,
A, B, ..., C r2. SUMMARIZE r1
PER (r2) ADD (summary AS X) ,
() r2, X, () t , t r2,
x X. x

r1, A, B, ..., C, t. r2 X,
X. , r2,
r2 . X exp.
( SX1 SUM
MARIZE Example 1):
SUMMARIZE SP PER ( S { SNO } ) ADD ( COUNT ( PNO ) AS PCT )

:
SNO
S1
S2
S3
S4
S5

PCT
6
2
1
3
0

,
PER
, .

, COUNT(PNO)
, ( , TutorialD), SUMMARIZE COUNT. ,
COUNT ; , ,
. COUNT(PNO)

186

7. SQL II:

SUMMARIZE . , SUMMARIZE
- - . .

r2 , r1, ,
, PER
BY, ( SX2):
SUMMARIZE SP BY { SNO } ADD ( COUNT ( PNO ) AS PCT )

:
SNO
S1
S2
S3
S4

PCT
6
2
1
3

,
S5. , BY {SNO}
, , PER (SP{SNO})
SP, SP , SP{SNO} S5.
SX2 SQL :
SELECT SNO , COUNT ( ALL PNO ) AS PCT
FROM SP
GROUP BY SNO

,
SQL SELECT GROUP BY ( . !).
.
,
. , FROM,
,
GROUP BY. ,
: ( ) , SELECT. : SQL (set
function) , () , ,
() .

187

SELECT,
SELECT DISTINCT, () ,
(
) ()
(- ).
COUNT ALL
, . (, ,
ALL DISTINCT,
SP).
COUNT(*)
(, , SUM), . ( COUNT(PNO)
COUNT(*), .)
SX1. SQL :
SELECT S.SNO , ( SELECT COUNT ( ALL PNO )
FROM SP
WHERE SP.SNO = S.SNO ) AS PCT
FROM S

, S5, , ,
,
S. , , SX2 , S5 , , GROUP BY ( , ).

, , SQL, . , SELECT SQL-


(SELECT ... S.SNO) AS PCT AS (
).
FROM, AS (range variable) (. 10). SELECT
. ,
:
SELECT S.SNO , ( SELECT COUNT ( ALL PNO ) AS PCT
FROM SP
WHERE SP.SNO = S.SNO )
FROM S

t, , PCT. t

188

7. SQL II:

, , , , PCT,
.

. , SX2
GROUP BY
:
SELECT DISTINCT SPX.SNO , ( SELECT COUNT ( ALL SPY.PNO )
FROM SP AS SPY
WHERE SPY.SNO = SPX.SNO ) AS PCT
FROM SP AS SPX

, GROUP BY SQL ,
, .
. , SX1 , ,
:
SUMMARIZE SP PER ( S { SNO } ) ADD ( SUM ( QTY ) AS TOTQ )

:
SNO

TOTQ

S1
S2
S3
S4
S5

1300
700
200
900
0

SQL-
SELECT S.SNO , ( SELECT SUM ( ALL QTY )
FROM SP
WHERE SP.SNO = S.SNO ) AS TOTQ
FROM S

, TOTQ S5 null,
0. , SQL
, COUNT(*) COUNT, , () null. , COALESCE:
SELECT S.SNO , ( SELECT COALESCE ( SUM ( ALL QTY ) , 0 )
FROM SP
WHERE SP.SNO = S.SNO ) AS TOTQ
FROM S

189

, SX1
, 250:
( SUMMARIZE SP PER ( S { SNO } ) ADD ( SUM ( QTY ) AS TOTQ ) )
WHERE TOTQ > 250

:
SNO

TOTQ

S1
S2
S4

1300
700
900

SQL :
SELECT
FROM
GROUP
HAVING

SNO , SUM ( ALL QTY ) AS TOTQ


SP
BY SNO
SUM ( ALL QTY ) > 250 /* TOTQ > 250 !!! */

:
SELECT DISTINCT SPX.SNO , ( SELECT SUM ( ALL SPY.QTY )
FROM SP AS SPY
WHERE SPY.SNO = SPX.SNO ) AS TOTQ
FROM SP AS SPX
WHERE ( SELECT SUM ( ALL SPY.QTY )
FROM SP AS SPY
WHERE SPY.SNO = SPX.SNO ) > 250

, HAVING, GROUP BY,


, , . ,
GROUP BY HAVING ; , , (, , , , , 250).
: GROUP BY HAVING, ,
, ( , ). ,
COALESCE , .
, GROUP BY
HAVING. SQL-:
SELECT SNO , CITY , SUM ( ALL QTY ) AS TOTQ
FROM S NATURAL JOIN SP
GROUP BY SNO

, CITY SELECT, . ,

190

7. SQL II:

S (.
8 B), SNO
CITY ( ). , SQL ,
. ,
CITY , , CITY , CITY SELECT, ( HAVING,
).
, ( )
:
SELECT SNO , CITY , SUM ( QTY ) AS TOTQ
FROM S NATURAL JOIN SP
GROUP BY SNO , CITY


SUMMARIZE Tutorial D . - , , , (,
), : , EXTEND.1
SX1 ( ). SUMMARIZE :
SUMMARIZE SP PER ( S { SNO } ) ADD ( COUNT ( PNO ) AS PCT )

EXTEND:
EXTEND S { SNO } ADD ( COUNT ( !!SP ) AS PCT )

( {SNO,PNO} -
SP, - {PNO}
.) ,
EXTEND , -
; , ,
WHERE.
. , -.
1

, , ,
SUMMARIZE
, , ,
, . , ,
SUMMARIZE .

191

6.
, .
EXTEND S ADD ( SUM ( !!SP , QTY ) AS TOTQ )

7.
,
.
EXTEND S ADD ( SUM ( !!SP , QTY ) AS TOTQ ,
MAX ( !!SP , QTY ) AS MAXQ ,
MIN ( !!SP , QTY ) AS MINQ )

EXTEND .
8.
, , , , .
EXTEND S ADD ( SUM ( !!SP , QTY ) AS TOTQ ,
SUM ( SP , QTY ) AS GTOTQ )

:
SNO

TOTQ

GTOTQ

S1
S2
S3
S4
S5

1300
700
200
900
0

3100
3100
3100
3100
3100

9. c c , ,
c.
WITH ( S JOIN SP JOIN P ) AS TEMP :
EXTEND TEMP { CITY } ADD ( SUM ( !!TEMP , QTY ) AS TOTQ ,
AVG ( !!TEMP , QTY ) AS AVGQ )

, ,
WITH , , , .


, 2 , (RVA-). .7.1 R1 R4 2.1 2.2; R4 RVA-,
R1 , , .

192

7. SQL II:
R1

SNO

PNO

S2
S2
S3
S4
S4
S4

P1
P2
P2
P2
P4
P5

R4

SNO
S2

PNO_REL
PNO
P1
P2

S3

PNO
P2

S4

PNO
P2
P4
P5

.7.1. R1 R4 . 2.1 2.2 2

, - , RVA- .
GROUP UNGROUP.
, , , .7.1,
R1 GROUP ( { PNO } AS PNO_REL )

R4,
R4 UNGROUP ( PNO_REL )

R1. SQL .
, ,
EXTEND R1 { SNO } ADD ( !!R1 AS PNO_REL )

, GROUP . , GROUP EXTEND


-. GROUP; , ,
UNGROUP ( Tutorial D), GROUP, , (
). , ,
, , GROUP EXTEND
-.
: R4 Sx PNO_REL , UNGROUP
Sx.
Introduction to Database

193

Systems (. D) Databases, Types, and the


Relational Model: The Third Manifesto ( . D), .
: , , RVA-. , A T
, ,
A, , , T. T ,
,
, ( ), . . 7.11 7.13
.


;
, , (
). ( , , , ?):
UPDATE P WHERE CITY = Paris : | WITH T1 AS
{ CITY := Nice ,
| ( SELECT P.*
WEIGHT := 2 * WEIGHT } |
FROM P
|
WHERE CITY = Paris ) ,
|
T2 AS
| ( SELECT P.* , Nice AS NC ,
|
2 * WEIGHT AS NW
|
FROM T1 )
|
SELECT PNO , PNAME , COLOR ,
|
NW AS WEIGHT ,
|
NC AS CITY
|
FROM T2

, UPDATE, TutorialD ,
( ), , ,
- P. ( ,
UPDATE ! ,
?) ,
t2 t1 -
P, (Paris). t2 t1 , (weight) , (city) Nice (), Paris. ,
(,
SQL):

194

7. SQL II:
WITH ( P WHERE CITY = Paris ) AS R1 ,
( EXTEND R1 ADD ( Nice AS NC , 2 * WEIGHT AS NW ) ) AS R2 ,
R2 { ALL BUT CITY , WEIGHT } AS R3 :
R3 RENAME ( NC AS CITY , NW AS WEIGHT )

, 5. ,
UPDATE UPDATE,
, , , INSERT
DELETE. .
UPDATE:
UPDATE P WHERE CITY = Paris :
{ CITY := Nice , WEIGHT := 2 * WEIGHT } ;

(
UPDATE):
P := ( P WHERE CITY Paris )
UNION
( UPDATE P WHERE CITY = Paris :
{ CITY := Nice , WEIGHT := 2 * WEIGHT } ) ;

ORDER BY?
ORDER BY. ,
1, ,
( , , ). , , , ORDER BY ;
, (
).1 , ,
, , :
S MATCHING SP
ORDER ( ASC SNO )

|
|
|
|

SELECT
FROM
WHERE
ORDER

DISTINCT S.*
S , SP
S.SNO = SP.SNO
BY SNO ASC

, ,
! : , ORDER BY
,
(. 7.14).

195

, , , - ORDER BY ( ORDER Tutorial D)


. -,
, , 3, < > .

, . ORDER BY .
ORDER
BY CITY . ,
,
( ):
S5 , S1 , S4 , S2 , S3
S5 , S4 , S1 , S2 , S3
S5 , S1 , S4 , S3 , S2
S5 , S4 , S1 , S3 , S2
, , SQL-
. ,
, 2, SQL , v1 = v2 TRUE, v1 v2 . ,
Paris Paris (
); , ,
SQL . 2 , SQL- . :
SELECT DISTINCT CITY FROM S

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

7.1. Tuto
rialD ?
() SQL- () -

196

7. SQL II:

( )
.
a. S MATCHING ( SP WHERE PNO = P2 )
b. P NOT MATCHING ( SP WHERE SNO = S2 )
c. P WHERE ( !!SP ) { SNO } = S { SNO }
d. P WHERE SUM ( !!SP , QTY ) < 500
e. P WHERE TUPLE { CITY CITY } S { CITY }
f. EXTEND S ADD ( Supplier AS TAG )
g. EXTEND S { SNO } ADD ( 3 * STATUS AS TRIPLE_STATUS )
h. EXTEND ( P JOIN SP ) ADD ( WEIGHT * QTY AS SHIPWT )
i. EXTEND P ADD ( WEIGHT * 454 AS GMWT , WEIGHT * 16 AS OZWT )
j. EXTEND P ADD ( COUNT ( !!SP ) AS SCT )
k. EXTEND S
ADD ( COUNT ( ( SP RENAME ( SNO AS X ) ) WHERE X = SNO )
AS NP )
l. SUMMARIZE S BY { CITY } ADD ( AVG ( STATUS ) AS AVG_STATUS )
m. SUMMARIZE ( S WHERE CITY = London )
PER ( TABLE_DEE ) ADD ( COUNT ( SNO ) AS N )
n. UPDATE SP WHERE SNO = S1 : { SNO := S7 , QTY = 0.5 * QTY }
7.2. ( ) r1 MATCHING r2 r2 MATCHING r1 ?
7.3. ,
.
7.4. , EXTEND
SUMMARIZE, :
SUMMARIZE SP PER ( S { SNO } ) ADD ( COUNT ( PNO ) AS NP )

7.5. Tutorial D ? SQL-.


a. SUMMARIZE r PER ( r { } ) ADD ( SUM ( 1 ) AS CT )
b. SUMMARIZE r PER ( TABLE_DEE ) ADD ( SUM ( 1 ) AS CT )
c. SUMMARIZE r BY { } ADD ( SUM ( 1 ) AS CT )
d. EXTEND TABLE_DEE ADD ( COUNT ( r ) AS CT )
7.6. Tutorial D, , COUNT, SUM, -

197

0; MAX MIN
; AND OR TRUE FALSE, AVG ( XOR TutorialD). SQL? ?
7.7. R4 . 7.1
-. R4 ,
2, -?
7.8. r , TutorialD:
SP GROUP ( { } AS X )

r SP?
?
r UNGROUP ( X )

7.9. TutorialD / SQL


:
a. , S1.
b. , ,
, .
c. ,
.
d. SNAME CITY CHAR, ( ) Y,
,
, ;
N.
7.10. TutorialD, R4, . 7.1. ?
( R4 WHERE TUPLE { PNO P2 } PNO_REL ) { SNO }
( ( R4 WHERE SNO = S2 ) UNGROUP ( PNO_REL ) ) { PNO }

7.11. Tutorial D
?
EXTEND S
ADD ( ( ( SP RENAME ( SNO AS X ) ) WHERE X = SNO ) { PNO }
AS PNO_REL )

198

7. SQL II:

7.12. ,
, -
SSP. ?
INSERT SSP RELATION
{ TUPLE { SNO S6 , SNAME Lopez , STATUS 30 , CITY Madrid ,
PNO_REL RELATION { TUPLE { PNO P5 } } } } ;
UPDATE SSP WHERE SNO = S2 :
{ INSERT PNO_REL RELATION { TUPLE { PNO P5 } } } ;

7.13. , SSP - , :
a. ,
.
b. ,
.
7.14. , , ,
:
2. TutorialD SQL.
?
7.15. SUMMARIZE:
( )
?
7.16. :
S
SP
SJ
J

{
{
{
{

SNO }
/*
*/
SNO, PNO } /* */
SNO, JNO } /* */
JNO }
/*
*/

Tutorial D SQL : , , , , . Tutorial D EXTEND SUMMARIZE.


7.17. Tutori
alD?
S WHERE ( !!(!!SP) ) { PNO } = P { PNO }

7.18. TutorialD? ,
?

199


EXTEND TABLE_DEE ADD ( COUNT (

SP ) AS NSP )

EXTEND TABLE_DEE ADD ( COUNT ( !!SP ) AS NSP )

7.19. ,
, , .
, r1 JOIN r2
r1 MATCHING r2 .
7.20. r1 r2 ,
t1 r1. !!r2 t1? , r1 r2 , ?
7.21. SQL-, , ?
SELECT COUNT ( * ) FROM S
SELECT SUM ( 1 ) FROM S

8
.

SQL

, . 1:
( , ) ,
TRUE. , , , ,
,
.
: . , , , ,
( ,
). ,
SQL.
, ,
, -.
( , ), , , - ( ) , .
, ,
, .
. , .
; , , , , , .
-

201

. , .


2 ,
, , . ,
, .
, , , . , INTEGER,
- , QTY. TutorialD:
1 TYPE QTY
2
POSSREP QPR
3
{ Q INTEGER
4
CONSTRAINT Q 0 AND Q 5000 } ;

:
1 , QTY.
2 , , QPR. 2 , .
TutorialD , TYPE
POSSREP, , - .1 , , , (
,
QPR). ,
, ; , , .
3 , QPR
Q INTEGER. , QTY ( ).
, 4 ,
0 5000 . , 24
, ,
1

,
.

202

8. SQL

; QTY.
(,
, . , ,
, , . ,
.)
:
TYPE POINT
POSSREP CARTESIAN { X FIXED , Y FIXED
CONSTRAINT SQRT ( X ** 2 + Y ** 2 ) 100.0 } ;

POINT ,
CARTESIAN X
Y ( , ).
FIXED, CONSTRAINT,
, , 100
(SQRT ). : 2,
, POINT, POSSREP CONSTRAINT; ,
POINT, CARTESIAN. . .

THE_
, , QTY POINT.
3 , , , THE_.
; ,
THE_
. .
1. QPR ( 250 )
QTY. QPR,
; , , ( QTY). :
,
, QTY , -

203

, .
Tutorial D ,
TYPE, . QTY:
TYPE QTY POSSREP { Q INTEGER CONSTRAINT Q 0 AND Q 5000 } ;

,
QTY,
QTY(250) 2 (
). QTY, .
2. QTY ( A + B )
QTY (
INTEGER). , , ; , ,
2.
3. THE_Q ( QZ )
THE_ QTY.
THE_Q, Q QTY;
( ) QTY
Q .
POINT, ,
, :
TYPE POINT POSSREP { X FIXED , Y FIXED CONSTRAINT ... } ;

:
1. POINT( 5.7, -3.9 )
POINT ( POINT).
2. THE_X ( P )
FIXED X
, P ( POINT).
, (
) TutorialD , TYPE
POSSREP. TutorialD
. POINT;
: . -

204

8. SQL

, ,
SQL .


. , QTY
, , CONSTRAINT:
TYPE QTY POSSREP { Q INTEGER } ;

,
TYPE QTY POSSREP { Q INTEGER CONSTRAINT TRUE } ;

, , QTY, QTY , . , , CONSTRAINT . ( ,


CONSTRAINT.)
,
. . , QTY
0 5000 .
QTY(250) QTY, . QTY(6000), , . , ,
, T, ; ,
T, T
. ,
-, , .
: , ,
.1 , , QTY - SP ( )
QTY, -
SP QTY , QTY. : QTY .

, .

205

SQL

SQL
, , SQL. , , SQL , . , SQL QTY ,
, , , . ,
SQL :
CREATE TYPE QTY AS INTEGER FINAL ;

( FINAL , QTY ,
.)
. , ,
, ,
(. SQL) . , SP,
, ( QTY
CONSTRAINT ):
CREATE TABLE SP
( SNO
VARCHAR(5) NOT NULL ,
PNO
VARCHAR(6) NOT NULL ,
QTY
QTY
NOT NULL ,
UNIQUE ( SNO , PNO ) ,
FOREIGN KEY ( SNO ) REFERENCES
FOREIGN KEY ( PNO ) REFERENCES
CONSTRAINT SPQC CHECK ( QTY >=
QTY <=

S ( SNO ) ,
P ( PNO ) ,
QTY(0) AND
QTY(5000) ) ) ;

QTY(0) QTY(5000) CONSTRAINT


QTY. , , SQL ( THE_);
2 , THE_ SQL ,
. , , , TutorialD.

POINT SQL ( FINAL NOT FINAL
, , ):
CREATE TYPE POINT AS
( X NUMERIC(5,1) , Y NUMERIC(5,1) ) NOT FINAL ;

206

8. SQL

, SQL .
, .1
: , , QTY
. , ,
, .
. 8.8 .


, . TutorialD
CONSTRAINT (
), SQL CREATE ASSERTION (, -, ). (
, ),
. .
( , TutorialD, SQL):
1:
CONSTRAINT CX1 IS_EMPTY
( S WHERE STATUS < 1
OR
STATUS > 100 ) ;

|
|
|
|
|

CREATE ASSERTION
( NOT EXISTS
( SELECT * FROM
WHERE STATUS
OR
STATUS

CX1 CHECK
S
< 1
> 100 ) ) ;

CX1 : 1 100 . -. ,


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

, ,
, SQL, , SQL
, (,
SQUARE , , ,
, ).

207

, ( 5) .
2:
CONSTRAINT CX2 IS_EMPTY
( S WHERE CITY = London
AND STATUS 20 ) ;

|
|
|
|
|

CREATE ASSERTION
( NOT EXISTS
( SELECT * FROM
WHERE CITY =
AND
STATUS

CX2 CHECK
S
'London'
<> 20 ) ) ;

CX2 : 20. , ,
-, , CX1,
(, , ).
3:
CONSTRAINT CX3
COUNT ( S ) =
COUNT ( S { SNO } ) ;

|
|
|

CREATE ASSERTION CX3 CHECK


( UNIQUE ( SELECT ALL SNO
FROM S ) ) ;

CX3 : -
S ; , {SNO}
(, ) - (, ).
CX1 CX2,
-, ( , ). , CX3 , ,
KEY
. :
, .
, CX3 SQL .
SQL UNIQUE , TRUE , - . , UNIQUE TRUE
, S . 10.
, , SQL-
CX3, TutorialD:
CREATE ASSERTION CX3 CHECK
( ( SELECT COUNT ( ALL SNO ) FROM S ) =
( SELECT COUNT ( DISTINCT SNO ) FROM S ) ) ;

208

8. SQL

4:
CONSTRAINT CX4
COUNT ( S { SNO } ) =
COUNT ( S { SNO , CITY } ) ;

|
|
|
|
|
|
|

CREATE ASSERTION CX4 CHECK


( NOT EXISTS ( SELECT *
FROM S AS SX
WHERE EXISTS ( SELECT *
FROM S AS SY
WHERE SX.SNO = SY.SNO
AND SX.CITY <> SY.CITY ) ) ) ;

CX4 : -
S , .
, {SNO} {CITY} , :
{ SNO } { CITY }

.
: A B
- R. , R () A B,
, R, ,
A , ,
B .
A B B A,
A B, A B. ,
, (
, ).
5 , - S
, {SNO} . , , {SNO} ,
. .
, , , , , (,
). ,
:
{ CITY } { STATUS }

, CX4
( ).
, ,
, . ,
,
. , , , ? !

209


.
B, , ( SQL)
,
- ( ). ,
(, , ),
. ,
, SQL.

5:
CONSTRAINT CX5 IS_EMPTY
( ( S JOIN SP )
WHERE STATUS < 20
AND PNO = P6 ) ;

|
|
|
|
|
|

CREATE ASSERTION CX5 CHECK


( NOT EXISTS
( SELECT *
FROM S NATURAL JOIN SP
WHERE STATUS < 20
AND
PNO = P6 ) ) ;

CX5 : 20
P6. , -: S SP.

-. : ,
-, - (,
, -). ,
-, -. (
, CX3 CX4 -,
CX5 -. CX1
CX2 -.) ,
,
.
6:
CONSTRAINT CX6
SP { SNO } S { SNO } ;

| CREATE ASSERTION CX6 CHECK


| ( NOT EXISTS
|
( SELECT SNO
|
FROM SP
|
EXCEPT
|
SELECT SNO
|
FROM S ) ) ;

CX6 : ,
- SP, S. , Tutorial D -

210

8. SQL

. SQL
, .
, {SNO} ( , )
- S, , CX6 , ,
SP S. FOREIGN
KEY CX6.

SQL
,
CONSTRAINT TutorialD, CREATE ASSERTION SQL. CX1-CX6
.1 , TutorialD, SQL
, .
, , SQL- CX5 :
CREATE ASSERTION CX5 CHECK
( NOT EXISTS ( SELECT
FROM
WHERE
AND

*
S NATURAL JOIN SP
STATUS < 20
PNO = P6 ) ) ;

, CONSTRAINT
CREATE TABLE SP:
CREATE TABLE SP
( ... ,
CONSTRAINT CX5 CHECK
( PNO <> P6 OR ( SELECT STATUS FROM S
WHERE SNO = SP.SNO ) >= 20 ) ) ;

, ,
S
P, (. 8.17 ).
, ,
(, , ), , CREATE ASSERTION. , ,
CX1 CX2 S:
1

(. 2), SQL ; . . 12.

211

CREATE TABLE S
( ... ,
CONSTRAINT CX1 CHECK ( STATUS >= 1 AND STATUS <= 100 ) ) ;
CREATE TABLE S
( ... ,
CONSTRAINT CX2 CHECK ( STATUS = 20 OR CITY <> London ) ) ;

,
, CREATE ASSERTION ,
, .

, NOT NULL , SQL .


, , .
,
NOT NULL.

.
, , CREATE TABLE T, , T , T
! ( : T -5 1 = 0.) . 8.15 8.16 .
(!) , CREATE
ASSERTION ,
, . ( ,
.) (,
), .1
, , .

,
( ), 1

(Lex de
Haan) (Toon Koppelaars) Applied Mathematics for
Database Professionals (. D).

212

8. SQL

. : ? ,
- ( -),
-
( ).
, , ,
SQL , .
, .
. ( , ), . , , ,
.
(, ) (Jim Gray)
(Andreas Reuter) Transaction Processing: Concepts
and Techniques (. D). ACID- . ACID
atomicity consistency isolation durability
( ).
, .
,
, . ( ,
; .)
, ,
,
, .
, ,
.
, (, ). ;
, , , , ,
. , ,
. .

213



,
. : 5 , , .
, . ,
, !
, , ,
: , , .
, ,
. , , ;
, - . ,
, , , , . , TX1 -, TX2, TX1 TX2
- (
, ). , () TX1

() TX2,
() , TX1, TX2.
, , , , .
, , ,
( ) .
, , , ,
.
, , ,
. (, , .)
(
- , . ) ,

214

8. SQL

- - .
TutorialD (LS =
, NLS = ):
VAR LS VIRTUAL ( S WHERE CITY = London ) ;
VAR NLS VIRTUAL ( S WHERE CITY London ) ;

. , -, {SNO}
- S ( , , - S). , LS NLS - , S. :
CONSTRAINT CX7 IS_EMPTY
( LS { SNO } JOIN
NLS { SNO } ) ;

|
|
|
|
|

CREATE ASSERTION CX7 CHECK


( NOT EXISTS
( SELECT *
FROM LS , NLS
WHERE LS.SNO = NLS.SNO ) ) ;

, , -
, . , ,
- , , - (
).
, ,
( ,
6). ,
, (SP JOIN S){PNO}.
-
SP - S,
. , SP S, , , SP
, . , ! SP{PNO}. ,
;
,
, ,
, . SP S
- ,
, ,

215

, - . , , , .
, ,
.1 , , :
, :
,
,
.
,
, , :
, .
,
. , ( ),
, ,
.
, ,
, (,
), .
, , , ,
. , - ,
( ). : , , ( , ) , , ,
.
. (

TRUE) (,
1

; ,
, (
).

216

8. SQL

, , ).
, , . ,
.


?
, ,
-
. , , :
CONSTRAINT CX8
COUNT ( ( S WHERE SNO = S1 ) { CITY }
UNION
( P WHERE PNO = P1 ) { CITY } ) < 2 ;

, S1 P1 . : - S
P S1 P1 , CITY ( , COUNT 2);
- S S1
- P1 P1, ( COUNT 1 0). SQL UPDATE :
UPDATE S SET CITY = Paris WHERE SNO = S1 ;
UPDATE P SET CITY = Paris WHERE PNO = P1 ;

SQL, TutorialD
,
TutorialD . ? : 1 UPDATE :
1

, , , ( - , ) SQL () DEFERRABLE () NOT DEFERRABLE ( ) () INITIALLY


DEFERRED ( ) INITIALLY IMMEDIATE ( ).
, SET CONSTRAINTS <constraint
name commalist> <option>, <option> DEFERRED IMMEDIATE,

217

START TRANSACTION ;
UPDATE S SET CITY = Paris WHERE SNO = S1 ;
UPDATE P SET CITY = Paris WHERE PNO = P1 ;
COMMIT ;

, UPDATE
. ,
UPDATE S1 P1 ? ( , , , S1 P1 ), .



, . (
TutorialD):
UPDATE S WHERE SNO = S1 : { CITY := Paris } ,
UPDATE P WHERE PNO = P1 : { CITY := Paris } ;

: -, , , UPDATE . -, UPDATE , , , UPDATE :


S := ... , P := ... ;


- S, - P,
.
:

( )
( ,
,
, .) ,
,
( . COMMIT ; -
, COMMIT ,
.

218

8. SQL

, ).
, , , . ,
UPDATE,
UPDATE . , .

, ,
. ,
. ,
, .

SQL? SQL
- , . -, ,
CASCADE, , DELETE
UPDATE . -,
,
, . -,
FETCH INTO SELECT INTO -
. -, SQL
SET ( ,
, . 2 3). (
). SQL
, , .1
. ,
, .
.
, ,
.
: SQL- , . ( , ,
1

, ,
.

219

), , , .


5 , -
( , ). , - S :
SNO , SNAME, STATUS CITY.
- S, ,
INSERT, DELETE UPDATE -. .
-, , -; . , S1
,
, , S1 ,1
.
-, , -,
, !
( ),
S6 30, , . ,
. ,
, , , ( ,
, ).
, , , ,
, . , -:
, .

- , , , (, , ).

220

8. SQL

, .
, ,
, . , ,
- . , ,
( ), ( ). ,
, , , .
. R -, C1, C2, ..,
Cm (m 0)
-, R.
, Ci (
).
( C1 ) AND ( C2 ) AND ... AND ( Cm ) AND TRUE

- R. ,
AND TRUE; ,
1 , -
, TRUE.
RC - R.
, R ,
RC FALSE. ,
( ):
, -
FALSE.
DB , R1, R2, ..., Rn (n 0)
- DB. - RC1, RC2, ..., RCn .

( RC1 ) AND ( RC2 ) AND ... AND ( RCn ) AND TRUE

DB. :
, FALSE.

; , -
, , .

221

, , , , .
. , , . . , . , ,
, p, p, NOT p. q .
:
p , p OR q .
p OR q NOT p , q
.
q ! ,
, (
, 1 = 0) . : ,
10.

, , - , - .
-, , , TRUE, ( ,
). , CX1 :
CONSTRAINT CX1 IS_EMPTY ( S WHERE STATUS < 1 OR STATUS > 100 ) ;

- S , (designator);
, - . , ( s), :
CONSTRAINT CX1 IS_EMPTY ( s WHERE STATUS < 1 OR STATUS > 100 ) ;

, (
, CONSTRAINT CX1
) , , ,
(. 5).
-, , - S
, CX1

222

8. SQL

CONSTRAINT; . , , , , ; , , .
-, , , , , . 1
.
, , ,
. ,
,
, . ,
,
SP S P, , . 1 EMP DEPT,
.
, , . ( ,
-) (
, ,
). ,
/,
. (
):
CONSTRAINT CX9 IS_EMPTY
( ( ( S { SNO , STATUS } RENAME ( STATUS AS OLD ) )
JOIN
( S { SNO , STATUS } RENAME ( STATUS AS NEW ) ) )
WHERE OLD > NEW ) ;

: , -
(, S) -
. , CX9
: S ,
, . (
SNO,
, , .)

223


TutorialD, SQL ( ).
, , , , , ,
. , , .
, ; , ,
, , ,
.1
. , . , , , ( , ).
, ? ,
, , .

8.1. . ?
, ?
8.2. . ,
,
- -?
8.3. (asser
tion), , , , , -
, , -
, , , -, , ?
() , () ?
8.4.
?
1

, ; , , . ,
, , , .

224

8. SQL

8.5. , QTY
TutorialD, , ?
a. THE_Q ( QTY ( 345 ) )
b. QTY ( THE_Q ( QTY ) )
8.6. : () ;
() THE_. : , .
8.7. , :
London, Paris, Rome, Athens, Oslo, Stockholm, Madrid Amsterdam.
Tutorial D CITY,
.
8.8. , ,
SQL CITY
S P. .
.
8.9. TutorialD. , , , S, 1 9999. , , .
8.10. TutorialD , .
8.11. , ?

?
8.12. , ?
8.13.
CX1-CX9, ?
8.14. TutorialD - SQL?
8.15. SQL ( )
CREATE ASSERTION? :

225

.
, .
8.16. , ,
SQL , , , .
, ( )? TutorialD?
8.17. CX5
SP. ,

S P, . .
8.18. CX1 ( ) ,
; CX5
. ? ( )?
8.19. TutorialD
, KEY{SNO}
- S?
8.20. CX8 SQL.
8.21. TutorialD / SQL
, :
a. 50 .
b. , , P2.
c. .
d.
.
e. .
f. 50 .
g. 10.
h.
.
i.
,
.
j. ,
.

226

8. SQL

k. ,
.
l.
, .
m. ,
, ,
.
n. ( ,
) 20000 .
, . : -, .
8.22.
:
( RC1 ) AND ( RC2 ) AND ... AND ( RCn ) AND TRUE

AND TRUE?
8.23. , S1 London ,
( ), S1 . ,
. ? :
.
8.24. , . TutorialD
, ( ).
8.25. ,
, , :
a. , .
b.
.
c. , ,

. ( ? ? ?)
8.26. ?

227

8.27. SQL-. - ?
8.28. , SQL ? ?
8.29. ,
() () . /
?
8.30. - SQL
? , ? , ?

9
.

SQL
, . .
-,
-, ,
, -
, -
-.
-,
( , ,
) -. :
, -
,
-, . . .
,
-, ; ,
-.
,
(, , ).
,
SQL. SQL 1: ! , ,
-. SQL , , . , -

229

, . , . , , , , - ; ,
SQL. , , CREATE TABLE CREATE
VIEW , ,
() ,
, () ,
. !
: , , SQL 3
6.

-

, , -, .
: V -, t
t.
(, ) V -
.
( ) 8,
SQL:
VAR LS VIRTUAL
| CREATE VIEW LS
( S WHERE CITY = London ) ; |
AS ( SELECT *
|
FROM S
|
WHERE CITY = London )
|
WITH CHECK OPTION ;
VAR NLS VIRTUAL
( S WHERE CITY London ) ;

|
|
|
|
|

CREATE VIEW NLS


AS ( SELECT *
FROM S
WHERE CITY <> London )
WITH CHECK OPTION ;

,
: , S .
.

230

9. SQL

, .
;
SQL- CREATE VIEW , , :
CREATE VIEW SDS ( SNAME , DOUBLE_STATUS )
AS ( SELECT DISTINCT SNAME , 2 * STATUS
FROM S ) ;

: SQL 3. , SDS
( ) :
CREATE VIEW SDS
AS ( SELECT DISTINCT SNAME , 2 * STATUS AS DOUBLE_STATUS
FROM S ) ;

, , ,
SNAME.
CREATE VIEW SQL WITH
CHECK OPTION, . : . . .


-,
- , .
.
.
. S -, LS NLS . ,
LS NLS -, S
( TutorialD):
VAR LS BASE RELATION
{ SNO CHAR , SNAME CHAR , STATUS INTEGER , CITY CHAR }
KEY { SNO } ;
VAR NLS BASE RELATION
{ SNO CHAR , SNAME CHAR , STATUS INTEGER , CITY CHAR }
KEY { SNO } ;
VAR S VIRTUAL ( LS D_UNION NLS ) ;

231

(, D_UNION
, , .) : , , , , LS CITY
London, NLS, ,
CITY , .
.
, , , - ,
( , ). , , . ( ,
,

.) :
: (
-) , ,
, -

.
.
, ,
-, . ( , -, , .)
. .
,
( , , ; TutorialD , SQL ).
, , ,
. . .
1, ,

-, ,
. ,

232

9. SQL

, null-. ( , , , .)
SQL- SQL
. , ,
, .
( . . A.) , , . ,
(, , SQL,
SQL- ) -
, , , ! ,
(. 2) .
- , (
). ,
- .
,
, , ,
. .
.

-
, , , ,
, -. ? -
-! ,
. ,
CREATE VIEW SQL:
CREATE VIEW S_CONST ( SNO , SNAME , STATUS , CITY ) AS
VALUES ( S1 , Smith , 20 , London )
( S2 , Jones , 10 , Paris )
( S3 , Blake , 30 , Paris )
( S4 , Clark , 20 , London )
( S5 , Adams , 30 , Athens )

,
,
,
,
;

, , . ,
, ; ,

233

-. .
, . ,
; , ( ),
.
, ; , ,
. , , .
, , -.
TutorialD -, , , -, , :
CONST PERIODIC_TABLE INIT ( RELATION
{ TUPLE { ELEMENT Hydrogen , SYMBOL H , ATOMICNO 1 } ,
TUPLE { ELEMENT Helium , SYMBOL He , ATOMICNO 2 } ,
....................................................
TUPLE { ELEMENT Uranium , SYMBOL U , ATOMICNO 92 } } ) ;

, -
- . , Tuto
rial D -: TABLE_DUM
TABLE_DEE, , , . , , Tutorial D, SQL
-. , ( ),
, , , ,
- -, .


, (
-) ,

- ( ). , V, : -, V, , , . , -

234

9. SQL

, 6 , , , , , . LS ( ), -. - S, ,
AND S :
SNO , SNAME, STATUS CITY
AND
CITY London.
:
SNO , SNAME, STATUS .
, ,
, CITY . , London. (
LS CITY,
, .)
NLS :
SNO , SNAME, STATUS CITY, .


,
, -
,
-, . :
, , -, , , , :
CREATE VIEW LS_STATUS
AS ( SELECT SNO , STATUS
FROM LS ) ;


(, ).

235

- . ,
, SQL- LS:
SELECT LS.SNO
FROM LS
WHERE LS.STATUS > 10

FROM , . :
SELECT LS.SNO
FROM ( SELECT S.*
FROM S
WHERE S.CITY = London ) AS LS
WHERE LS.STATUS > 10

.
, ,
:
SELECT
FROM
WHERE
AND

S.SNO
S
S.CITY = London
S.STATUS > 10

, .
,
. , , - (,
), , .1 , FROM
SQL-, , SQL. , LS, LS.
. , ,
SQL; , 1992 . ,

. , , , , ( ) , , , -

,
, ( )
.

236

9. SQL

, . . :
CREATE VIEW V
AS ( SELECT CITY , SUM ( STATUS ) AS ST
FROM S
GROUP BY CITY ) ;

:
SELECT CITY
FROM V
WHERE ST > 25

SQL 1992 , :
SELECT
FROM
WHERE
GROUP

CITY
S
SUM ( STATUS ) > 25
BY CITY

, , , , ,
, !
, , , , , . -
, ; , ,
, , . , , , .


, ,
-, ,
,
- (. 8). , , ,
V, -,
V, , , . LS. - S, - S,
() -

237

AND () -
S . , - S
, , {SNO} . - LS
AND
, LS
(
, ,
-).
, , , . , , , . , ,
, . , ( ,
). , .
, , . , () KEY FOREIGN
KEY () -
FOREIGN KEY. , ():
VAR LS VIRTUAL ( S WHERE CITY = London )
KEY { SNO } ;

Tutorial D , SQL .
: SQL ,
:
CREATE VIEW LS
AS ( SELECT *
FROM S
WHERE CITY = London )
/* UNIQUE ( SNO ) */
WITH CHECK OPTION ;

, SQL ,
CREATE ASSERTION
(, , ). , CREATE ASSERTION ,

238

9. SQL

, , .
.

,
, , ,
. ,
, KEY{SNO}, ,
LS, ,
- S. ,
LS, . ( -
, .)
, , , - ,
. , SQL. ( ):
CREATE TABLE FDH
( FLIGHT ... , DESTINATION ... , HOUR ... ,
UNIQUE ( FLIGHT ) ) ;
CREATE TABLE DFGP
( DAY ... , FLIGHT ... , GATE ... , PILOT ... ,
UNIQUE ( DAY , FLIGHT ) ) ;

: FLIGHT DESTINATION HOUR ( FDH) DAY FLIGHT PILOT GATE


( DFGP). ( ):
IF ( f1,n1,h ), ( f2,n2,h ) IN FDH AND
( d,f1,g,p1 ), ( d,f2,g,p2 ) IN DFGP
THEN f1 = f2 AND p1 = p2
IF ( f1,n1,h ), ( f2,n2,h ) IN FDH AND
( d,f1,g1,p ), ( d,f2,g2,p ) IN DFGP
THEN f1 = f2 AND g1 = g2

: , ()
FDH h () DFGP,
f1 f2 FDH, d g, FDH DFGP . , ,
, .
: () FDH h
() DFGP, f1 f2

239

FDH, d p,
FDH DFGP .
, , ,
.

:
CREATE ASSERTION BTCX1 CHECK
( NOT ( EXISTS ( SELECT * FROM FDH AS FX WHERE
EXISTS ( SELECT * FROM FDH AS FY WHERE
EXISTS ( SELECT * FROM DFGP AS DX WHERE
EXISTS ( SELECT * FROM DFGP AS DY WHERE
FY.HOUR = FX.HOUR AND
DX.FLIGHT = FX.FLIGHT AND
DY.FLIGHT = FY.FLIGHT AND
DY.DAY = DX.DAY AND
DY.GATE = DX.GATE AND
( FX.FLIGHT <> FY.FLIGHT OR
DX.PILOT <> DY.PILOT ) ) ) ) ) ) ) ;
CREATE ASSERTION BTCX2 CHECK
( NOT ( EXISTS ( SELECT * FROM FDH AS FX WHERE
EXISTS ( SELECT * FROM FDH AS FY WHERE
EXISTS ( SELECT * FROM DFGP AS DX WHERE
EXISTS ( SELECT * FROM DFGP AS DY WHERE
FY.HOUR = FX.HOUR AND
DX.FLIGHT = FX.FLIGHT AND
DY.FLIGHT = FY.FLIGHT AND
DY.DAY = DX.DAY AND
DY.PILOT = DX.PILOT AND
( FX.FLIGHT <> FY.FLIGHT AND
DX.GATE <> DY.GATE ) ) ) ) ) ) ) ;

, , :
CREATE VIEW V AS
( FDH NATURAL JOIN DFGP ,
UNIQUE ( DAY , HOUR , GATE ) ,
/* */
UNIQUE ( DAY , HOUR , PILOT ) ) ; /* !!!! */

,
:
CREATE VIEW V AS FDH NATURAL JOIN DFGP ;
CREATE ASSERTION VCX1
CHECK ( UNIQUE ( SELECT DAY , HOUR , GATE FROM V ) ) ;
CREATE ASSERTION VCX2
CHECK ( UNIQUE ( SELECT DAY , HOUR , PILOT FROM V ) ) ;

240

9. SQL

V V UNIQUE
, V:
CREATE ASSERTION VCX1
CHECK ( UNIQUE ( SELECT DAY , HOUR , GATE
FROM FDH NATURAL JOIN DFGP ) ) ;
CREATE ASSERTION VCX2
CHECK ( UNIQUE ( SELECT DAY , HOUR , PILOT
FROM FDH NATURAL JOIN DFGP ) ) ;


, ( ). : , ? , , , , , - S P
{CITY}; , ,
, ? :
; 5, . .
, S JOIN P (, ),
, . ,
- SP ,
SNO - S. ,
- - , . , ,
, ,
-
(
). :
-, ,
.
.
, V . V, V, VC.
, , , ,
-, V, VC. , SQL-

241

.
( , ). SQL- ,
(
). LS.
S,
DELETE:
DELETE
FROM LS
WHERE LS.STATUS > 15 ;

:
DELETE
FROM S
WHERE S.CITY = London
AND
S.STATUS > 15 ;

, , . : SQL SQL.

CHECK
SQL- INSERT
LS:
INSERT INTO LS ( SNO , SNAME , STATUS , CITY )
VALUES ( S6 , Lopez , 30 , Madrid ) ;

:
INSERT INTO S ( SNO , SNAME , STATUS , CITY )
VALUES ( S6 , Lopez , 30 , Madrid ) ;

( .) , LS, . ?
SQL S, , ( LS), . ,
LS
( , INSERT ). .
, ,
. TutorialD . SQL,
CHECK: ( ) WITH CASCADED CHECK OPTION, -

242

9. SQL

, . : , ,
WITH CASCADED CHECK OPTION.
, SQL
, , (
) , , SQL. :
CASCADED LOCAL, .1
, CASCADED, LOCAL,
CASCADED .

SQL
, SQL .
, ! (
) :
< > (query expression) QE1
, < > (query
expression) < > (query specification) QE2, (simply contained) QE1:
a) QE1 QE2 <
> (non join query expression) UNION DISTINCT,
EXCEPT ALL EXCEPT DISTINCT.
b) QE1 < >
(non join query expression) NJQE UNION ALL, :
i)

NJQE < > (query expression) LO < > (query term)


RO ,
LO (leaf generally underlying table)
RO.

ii)

NJQE (under
lying columns) ,
LO RO, .

c) QE1 QE2 < > (non join query term) INTERSECT.


d) QE2 .
1

WITH LOCAL CHECK OPTION ,


. ,
; , , -
.

243


, , SQL.
, , ,
.

, : ,
, < >, .
,
SQL . , ,
, , :
1. , /
.
2. , --
-- ( - ).
3. , UNION ALL
INTERSECT .
4. 13 .
- , SQL
, , , - SQL null- . ,
SQL , : , , .
, , . , UPDATE DELETE, INSERT, , . , , , , (, DELETE, INSERT), DELETE
INSERT . ,
, .
1 , . , SQL-, , , :
, , () SELECT ( UNION, INTERSECT EXCEPT), () (. 12),

244

9. SQL

. : , () ().
SELECT SELECT
ALL ( ).
, ,
(, AS),
.
FROM SELECT
(. 12), (, T) , ( , ).
SELECT WHERE,
, FROM T.
SELECT GROUP BY HAVING.
: , SQL-
.

?
, ,
, . .
, V, ,
X, .
V , X. ,
.
, , , V , ( ,
) X. V , -,
. , .



. (. 1),
, -

245

. ,
, . .
, - ( , )
- S - LS
NLS:
VAR LS BASE RELATION
/* */
{ SNO CHAR , SNAME CHAR , STATUS INTEGER , CITY CHAR }
KEY { SNO } ;
VAR NLS BASE RELATION
/* */
{ SNO CHAR , SNAME CHAR , STATUS INTEGER , CITY CHAR }
KEY { SNO } ;

, -
S LS NLS ( LS, NLS
S). , -, S:
VAR S VIRTUAL ( LS D_UNION NLS ) KEY { SNO } ;

, S, S. ,
,
, ,
,
.
, - S () LS NLS .
, - - SP (), , -. . 9.11 .



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

246

9. SQL

, ( , ). ( ):
VAR LSS SNAPSHOT ( S WHERE CITY = London )
KEY { SNO }
REFRESH EVERY DAY ;


:
( LSS) -
-, (
, . ).
( EVERY DAY) , .
, LSS ,
24 .
,
.
( )
.
; ,
(, ), ,
.
. , (
, ) . ! 1; , ,
-. , . ,
( ),
. ,
, .
1

, , , .

247

: ; , , .

9.1. , , . SQL Tu
torialD.
9.2. LSSP
( SQL):
CREATE VIEW LSSP
AS ( SELECT S.SNO , S.SNAME , S.STATUS , SP.PNO , SP.QTY
FROM S NATURAL JOIN SP
WHERE S.CITY = London ) ;

:
SELECT
FROM
WHERE
(

DISTINCT STATUS , QTY


LSSP
PNO IN
SELECT PNO
FROM P
WHERE CITY <> London )

?
9.3. ( ) LSSP
? ?
9.4. TutorialD:
VAR HEAVYWEIGHT VIRTUAL
( ( P RENAME ( WEIGHT AS WT , COLOR AS COL ) )
WHERE WT > 14.0 ) { PNO , WT , COL } ;

,
:
a. HEAVYWEIGHT WHERE COL = Green
b. ( EXTEND HEAVYWEIGHT ADD ( WT + 5.3 AS WTP ) ) { PNO , WTP }
c. INSERT HEAVYWEIGHT RELATION { TUPLE { PNO P99 , WT 12.0 , COL
Purple } } ;
d. DELETE HEAVYWEIGHT WHERE WT < 9.0 ;
e. UPDATE HEAVYWEIGHT WHERE WT = 18.0 : { COL := White } ;

248

9. SQL

9.5. HEAVYWEIGHT
9.4 :
VAR HEAVYWEIGHT VIRTUAL
( ( ( EXTEND P ADD ( WEIGHT * 454 AS WT ) )
RENAME ( COLOR AS COL ) ) WHERE WT > 6356.0 )
{ PNO , WT , COL } ;

( WT , ).
9.4.
9.6. 9.4 9.5 SQL.
9.7. SQL- WITH CHECK OPTION?
9.8.
9.4 9.5? ?
-?
9.9. , , .
9.10.
,
( ), , , -
, , .
9.11.
, ( ) - S (LS NLS). ,
. ?
9.12. -
SQL.
a. - , ? , , .
?
b. ? .
,
?
c. , ( !)?

249

9.13. . SQL? - ?
9.14. ? ?
9.15. ,
- . :
a. S { SNO , SNAME , STATUS , CITY }
SP { SNO , PNO , QTY }
b. SSP { SNO , SNAME , STATUS , CITY , PNO , QTY }
XSS { SNO , SNAME , STATUS , CITY }
(a) . (b) -
SSP , , , - XSS , . ( , ?)
, (b) (a) . , ,
. -
? , .
9.16. ,
. 6,

? ?

10
.

SQL
1 , . , , ,

, . ,
.
? , (
), .
(
), , , SQL.
, , , . ,
TutorialD (
), Query-By-Example (. D) . SQL? , , : SQL
, , ; , - IN.1 ,
1

, , -
. , ,
. ,
, . ; , -

251

, . , SQL
, ,
. , ,
.., SQL,
, 6.


(. 5), , TRUE FALSE. (1, 4 5 , 2 3 ):
1. 2 + 3 = 5
2. 2 + 3 > 7
3.
4.
5.

,
.
NOT, AND, OR, IF ... THEN ... ( IMPLIES,
) IF AND ONLY IF ( IFF, BIIMPLIES, IS EQUIVALENT TO, , ).
,
3, 4 5 :
6. ( ) OR ( )
7. ( ) AND ( )
8. ( ) AND NOT (
)
9. IF ( ) THEN (
)
10. IF ( ) THEN ( )

, . , , . ,

( ), , , .

252

10. SQL

, , .

,
. NOT , . ,
, . ,

( TRUE
FALSE T F ):
p

NOT p

T
F

F
T

p q
T
T
F
F

p AND q

p OR q

T
F
F
F

T
T
T
F

T
F
T
F

IF p THEN q

p IFF q
T
F
F
T

T
F
T
T

, ( IF ... THEN ... IF):


NOT
T
F

AND T F
F
T

T
F

T F
F F

OR

T F

IF

T F

IFF

T F

T
F

T T
T F

T
F

T F
T T

T
F

T F
F T

, . , .
9. IF ( ) THEN (
)
IF p THEN q (, , p IMPLIES
q), p , q . , TRUE, TRUE,
. ,
,
! ?
, , , . , , .

253

, , 16 ,
16 ( ).
, 16 ,
AND OR. ;
, ( )
, . , , AND , .
p AND q q AND p ,
. :

.
, ! , AND ; ,
(
). .
16 IMPLIES , () . , , , ,
, . ,
, . ,
, ,
, . , , .
( ):
IF ( ) THEN ( )

,
TRUE, ( ); ,
, . , ,
TRUE, IMPLIES.
(, ), . ,
, -

254

10. SQL

(
):
IF ( COLOR = Red ) THEN ( CITY = London )

, , . , (
) TRUE,
FALSE.
, p IMPLIES q (,
, IF p THEN q)
(NOT p) OR q FALSE , p TRUE q FALSE.
, NOT, AND, OR, IMPLIES IF AND ONLY IF
; .
NOT AND, OR . (:
.) , , ,
. ?


AND OR ; p
AND q q AND p . , , ,
p q . , SQRT ( ) ,
. SQL-:
SELECT ...
FROM ...
WHERE X >= 0 AND SQRT ( X ) ...

, , SQRT X.


:
11. x
12. x
13. x m
14. x y
15. x y z

255

x, y, z m , -
(placeholders). , ( ,
) , . , x x, , , , x.
, , , .

. ,
x x
, .
, ( , , ). (x ), , . , (. 5) , . ,
; , . ,
, . , x , .
, , ( , ,
, , ). . , 5.23 5 ,
( , , TRUE FALSE). , , .
x m
: x m. x , m 2
; x , m 2 . . N- , n ; , x
y z 3 , x m 2. 0 . :
n- n. n = 1, , n = 2 .
, ,

256

10. SQL

(NOT, AND, OR . .); ,


, ,
, . ; , , . :
16. ( x ) OR ( x y )
,
, , : x y.

, .
. p(x)
( ).
:

EXISTS x ( p ( x ) )

:
a, x,
, p(a) TRUE ( , a p). , p x
,
EXISTS x ( x )

, TRUE (,
, a ).

FORALL x ( p ( x ) )

: a, x, , p(a) TRUE ( , a p). , p


x ,
FORALL x ( x )

FALSE ( ,
).
, EXISTS
, FORALL . ,

257

( ).
.
EXISTS x FORALL x ( quantify,
-). EXISTS , FORALL .
EXISTS E (), FORALL A ().
EXISTS
FORALL.

,
. , . p q x. p q ,
x. , x, , EXISTS x (p) FORALL x (q), . ,
. ( , , , . , .)

x
, y. x,
:
EXISTS x ( x , y )

, ; , y. , :
EXISTS x ( x , )

(
, , ,
TRUE). : , :
EXISTS x ( EXISTS y ( x , y ) )

;
FALSE , ,
, TRUE ( !).

258

10. SQL

:
n-
, . , m (m n) k- , k =
n m.
. , , , ,
:
EXISTS x ( EXISTS y ( x is taller than y ) )
EXISTS y ( EXISTS x ( x is taller than y ) )

, : x y , x , y. , , ( ) , . ,
(. ).
,
, . , ; . ( ,
.)
.
, !
,
, . .
EXISTS x ( EXISTS y ( x , y ) )

: - , - ; TRUE,
.
EXISTS x ( FORALL y ( x , y ) )

: - ( -!); FALSE.
FORALL x ( EXISTS y ( x , y ) )

: -; FALSE.
EXISTS y ( FORALL x ( x , y ) )

: - ( -); FALSE. : , , ! , -

259

, x , y y , x , .
FORALL y ( EXISTS x ( x , y ) )

: -; FALSE.
FORALL x ( FORALL y ( x , y ) )

: -; FALSE.
( , ): FALSE, ,
; ,
.


, ,
,
.
:
x , y

x y . x , :
EXISTS x ( x , y )

y ( ), x .

EXISTS x EXISTS y ( x , y )

x, y, ( ).
,
, .
;
,
. , , ( , ):
EXISTS x ( x > 3 )

, , . ( , x
. .)
, x y. ,
EXISTS y ( y > 3 )

260

10. SQL

.
:
EXISTS x ( x > 3 ) AND x < 0

x, .
y ()
.
. ,
, :
EXISTS y ( y > 3 ) AND x < 0
EXISTS y ( y > 3 ) AND y < 0

,
,
, , . , y , . ,
( ) 1, . !
,
(, ) ,
, , , .



. , P2.
:
( S WHERE CITY = Paris ) { SNO , STATUS }
MATCHING ( SP WHERE PNO = P2 )

:
RANGEVAR SX RANGES OVER S ;
RANGEVAR SPX RANGES OVER SP ;
{ SX.SNO , SX.STATUS }
WHERE SX.CITY = Paris AND
EXISTS SPX ( SPX.SNO = SX.SNO AND SPX.PNO = P2 )
1

,
.

261

:
(range variable) S SP .
,
SX , - S . SPX
, - SP .
.
:
WHERE

(. . , ) ,
, ,
TRUE,
. ( ,
, , , ). ,
2, (SNO,STATUS)
- S , ()
() - SP , , P2.
( , ); , SQL.
SQL
:
SELECT
FROM
WHERE
AND
(

SX.SNO , SX.STATUS
S AS SX
SX.CITY = Paris
EXISTS
SELECT *
FROM SP AS SPX
WHERE SPX.SNO = SX.SNO
AND
SPX.PNO = P2 )

, SQL ,
( 12
SQL). , SQL EXISTS; , .1
1

, , EXISTS SQL IS_NOT_EMPTY TutorialD (. 3). . .

262

10. SQL

, sq , EXISTS sq
( ), FALSE, , sq, ,
TRUE . ( tx , sq, , , SELECT *
FROM ... WHERE ..., WHERE , ,
, sq .
S,
SX. . 12.)

. 4 , SQL , null-,
3VL ( ,
). 3VL
: TRUE, FALSE UNKNOWN (-
. 4). SQL EXISTS TRUE FALSE UNKNOWN. , EXISTS(tx) TRUE, UNKNOWN, tx , , null- (
), UNKNOWN. , (a) EXISTS
SQL 3VL (b)
SQL ( ).
.

, . , , , PX:
RANGEVAR PX RANGES OVER P ;
{ SX.SNAME } WHERE FORALL PX ( EXISTS SPX ( SPX.SNO = SX.SNO AND
SPX.PNO = PX.PNO ) )

, , :
,
, , , . : , , , SQL
FORALL.
SQL- , ,
.
, :
{ SX.SNAME } WHERE EXISTS SPX ( FORALL PX ( SPX.SNO = SX.SNO AND
SPX.PNO = PX.PNO ) )

263

: ? , ?
( , ):
{ SX.SNAME } WHERE FORALL PX ( IF PX.COLOR = Red THEN
EXISTS SPX ( SPX.SNO = SX.SNO AND
SPX.PNO = PX.PNO ) )

. ,
, ; ,
.
, -:
{ SX.SNAME } WHERE FORALL PX
( EXISTS SPX ( IF PX.COLOR = Red THEN
SPX.SNO = SX.SNO AND
SPX.PNO = PX.PNO ) )

WHERE . , , . :
: , (PNF), () , () EXISTS x (p)
FORALL x (p), p . , PNF- :
Q1 x1 ( Q2 x2 ( ... ( Qn xn ( q ) ) ... ) )

n 0, Qi (i = 1, 2, ..., n) EXISTS, FORALL,


q ( ) .
,
,
.


,
. ,

;
(, , , -

264

10. SQL

-). : ,
. ,
range variable tuple variable.
, tuple variable ( ) .1
.
, , ,
(,
AS, ), . ( , . .
5 .) : ,
,
, .
R.A, A ,
R ( SX.SNO). , SX,
, , , .

, , , R. WHERE , R,
R.A, - , R .
WHERE ; ,
WHERE TRUE.

, range variable ( : ), , . .
. . . .: , 2001.
. .

265


, ;
.
RANGEVAR, ,
, SX, SY, SZ .. S; PX, PY, PZ .. P; SPX, SPY, SPZ, ..
SP. ,
.
SQL.
1. , .
{ SX.SNO AS SA , SY.SNO AS SB } WHERE SX.CITY = SY.CITY
AND SX.SNO < SY.SNO

AS .
2. ,
.
{ SX.SNAME } WHERE EXISTS SPX ( EXISTS PX ( SX.SNO = SPX.SNO AND
SPX.PNO = PX.PNO AND
PX.COLOR = Red ) )

3. ,
, S2.
{ SX.SNAME } WHERE EXISTS SPX ( EXISTS SPY ( SX.SNO = SPX.SNO AND
SPX.PNO = SPY.PNO AND
SPY.SNO = S2 ) )

4. , P2.
{ SX.SNAME } WHERE NOT EXISTS SPX ( SPX.SNO = SX.SNO AND
SPX.PNO = P2 )

, NOT, .
5. ,
.
{ SPX , PX.WEIGHT * SPX.QTY AS SHIPWT } WHERE PX.PNO = SPX.PNO

. EXTEND.

266

10. SQL

6. .
{ PX.PNO , SUM ( SPX WHERE SPX.PNO = PX.PNO , QTY ) AS TOTQ }

( , WHERE). , ,
,
(?):
{ PX.PNO , SUM ( SPX.QTY WHERE SPX.PNO = PX.PNO ) AS TOTQ }

: , .
7. ,
.
{ PX.CITY } WHERE
COUNT ( PY WHERE PY.CITY = PX.CITY AND PY.COLOR = Red ) > 5


. , 8, .
, , . , ,
, .
1. 1
100 .
CONSTRAINT CX1 FORALL SX ( SX.STATUS > 0 AND SX.STATUS < 101 ) ;

SQL
, , . ,
(. 8) S:
CONSTRAINT CX1 CHECK ( STATUS > 0 AND STATUS < 101 )

2. 20.
CONSTRAINT CX2 FORALL SX ( IF SX.CITY = London
THEN SX.STATUS = 20 ) ;

3. - S .

267

CONSTRAINT CX3 FORALL SX ( FORALL SY ( IF SX.SNO = SY.SNO THEN


SX.SNAME = SY.SNAME AND
SX.STATUS = SY.STATUS AND
SX.CITY = SY.CITY ) ) ;

4. - S
, .
CONSTRAINT CX4 FORALL SX ( FORALL SY ( IF SX.SNO = SY.SNO
THEN SX.CITY = SY.CITY ) ) ;

5. 20 P6.
CONSTRAINT CX5 FORALL SX ( IF SX.STATUS < 20 THEN
NOT EXISTS SPX ( SPX.SNO = SX.SNO AND
SPX.PNO = P6 ) ) ;

6. , -
SP, - S.
CONSTRAINT CX6 FORALL SPX ( EXISTS SX ( SX.SNO = SPX.SNO ) ) ;

-
.
7.
- LS NLS.
CONSTRAINT CX7 FORALL LX ( FORALL NX ( LX.SNO NX.SNO ) ) ;

LX NX LS NLS .
8. S1 P1 .
CONSTRAINT CX8 NOT EXISTS SX ( EXISTS PX ( SX.SNO = S1 AND
PX.PNO = P1 AND
SX.CITY PX.CITY ) ) ;

9. . ( 8
.)
CONSTRAINT CX9 EXISTS SX ( TRUE ) ;

EXISTS SX (TRUE) FALSE , SX .


, .


EXISTS FORALL, , EXISTS,

268

10. SQL

FORALL . :
EXISTS x ( x , )

(- , ). - :
NOT ( FORALL x ( NOT ( x , Steve ) ) )

(, ). ,
EXISTS x ( p ( x ) )


NOT ( FORALL x ( NOT ( p ( x ) ) )

( p , x).
FORALL x ( p ( x ) )


NOT ( EXISTS x ( NOT ( p ( x ) ) )

( - p , x).
,
EXISTS FORALL. . , EXISTS,
FORALL. , , SQL EXISTS,
FORALL; SQL
. , , :
{ SX } WHERE FORALL PX ( EXISTS SPX
( SPX.SNO = SX.SNO AND SPX.PNO = PX.PNO ) )

SQL :
SELECT
FROM
WHERE
(

SX.*
S AS SX
NOT EXISTS
SELECT *
FROM P AS PX
WHERE NOT EXISTS
( SELECT *
FROM SP AS SPX
WHERE SX.SNO = SPX.SNO
AND
SPX.PNO = PX.PNO ) )

( SX , PX ,
SPX, SX -

269

PX). ( ), , , .


,
EXISTS x ( p ( x ) )

NOT ( FORALL x ( NOT ( p ( x ) ) ) )

. , x
. ,
; ,
(
-, ). :
EXISTS x (p(x)) FALSE,
x, , x,
. ,
, p(x). , ,
IBM ( ).
, NOT EXISTS x (p(x)) TRUE, - , p(x). , , IBM ( IBM
) ( ).
NOT EXISTS x (p(x)) FORALL x (NOT
(p(x)), , , TRUE
p(x).
p(x) , NOT
(p(x)). , , ; FORALL x (...) TRUE,
x ,
, . , IBM , , , .
,
( , ). ,

270

10. SQL
{ SX } WHERE FORALL PX ( EXISTS SPX ( SPX.SNO = SX.SNO AND
SPX.PNO = PX.PNO ) )

( , )
, .

EXISTS FORALL
, , , EXISTS FORALL OR AND . EXISTS. p(x) x, x
X = {x1, x2, ..., xn}.
EXISTS x ( p ( x ) )

, , , ( )
p ( x1 ) OR p ( x2 ) OR ... OR p ( xn ) OR FALSE

, FALSE,
X ( ). , p(x)
x , X {, , ,
}. EXISTS x (p(x)) EXISTS x ( x
)
( ) OR ( ) OR
( )
OR ( ) OR FALSE

TRUE, , .
FORALL x ( p ( x ) )

, , , ( )
p ( x1 ) AND p ( x2 ) AND ... AND p ( xn ) AND TRUE

( ) TRUE,
X . , p(x) X ,
EXISTS. FORALL x (p(x))
FORALL x ( x )
( ) AND ( ) AND
( )
AND ( ) AND TRUE

FALSE, , .
, , , EXISTS FORALL OR AND, , , . -

271

, ? : EXISTS FORALL
OR AND , ,
, , (
, ). ,
, .1
, ,
, EXISTS FORALL ,
! AND OR,
. , ,
( OR AND).


EXISTS FORALL, , , . , ,
,
x,

x ,

x ,
( ).
x, .
UNIQUE. :
UNIQUE x ( x , )

: , , ; , FALSE.
UNIQUE x ( x y )

: , , EXISTS x (p), p x. x ,
OR
,
( x,
p).
AND FORALL x (p)
, ( x, p).

272

10. SQL

:
y (y ).
, () , .
FORALL y ( UNIQUE x ( x y ) )

:
. ( , y ,
. : , ,
, TRUE?)
: ?
FORALL x ( UNIQUE y ( x y ) )

: ,
- SP,
- S. :
CONSTRAINT CX6 FORALL SPX ( EXISTS SX ( SX.SNO = SPX.SNO ) ) ;

, , :
CONSTRAINT CX6 FORALL SPX ( UNIQUE SX ( SX.SNO = SPX.SNO ) ) ;

, , SP (EXISTS),
(UNIQUE) - S.
, , , {SNO} S. ,
.
SQL UNIQUE ( - ),
, EXISTS. , sq ;
UNIQUE sq , FALSE,
, sq, -, TRUE
. ,
UNIQUE x ( p ( x ) )

a, x, p(a) TRUE, SQL-


( !)
UNIQUE ( SELECT * FROM T WHERE p ( x ) )

a, x, T , p(a)
TRUE. , ,
UNIQUE, , (. 12),
UNIQUE TRUE, .

273

SQL-
UNIQUE ( ,
):
SELECT DISTINCT SNAME
FROM S
WHERE NOT UNIQUE ( SELECT QTY
FROM SP
WHERE SP.SNO = S.SNO )

, ; ,
, SQL SELECT,
DISTINCT.1 , , 4 , DISTINCT . ,
, ,
-
. , UNIQUE :
SELECT DISTINCT SNAME
FROM S
WHERE ( SELECT COUNT ( DISTINCT QTY )
FROM SP
WHERE SP.SNO = S.SNO ) <
( SELECT COUNT ( * )
FROM SP
WHERE SP.SNO = S.SNO )

SQL UNIQUE
. , CREATE TABLE S
:
UNIQUE ( SNO )

, , ( CREATE ASSERTION):
CHECK ( UNIQUE ( SELECT SNO FROM S ) )

, SELECT CHECK
DISTINCT! (?)
, UNIQUE SQL MATCH. ( ,
):
SELECT SNO , SNAME , STATUS , CITY
1

,
( ). ,
. 12.

274

10. SQL
FROM S
WHERE SNO MATCH UNIQUE ( SELECT SNO FROM SP )

. , :
SELECT SNO , SNAME , STATUS , CITY
FROM S
WHERE EXISTS ( SELECT * /* ... */
FROM SP
WHERE SP.SNO = S.SNO )
AND
UNIQUE ( SELECT *
/* ...
*/
FROM SP
WHERE SP.SNO = S.SNO )

, , :
SELECT SNO , SNAME , STATUS , CITY
FROM S
WHERE ( SELECT COUNT ( * )
FROM SP
WHERE SP.SNO = S.SNO ) = 1


, , , . , IS_EMPTY, 3 8. , :
EXISTS x ( p ) NOT ( IS_EMPTY ( X WHERE p ) )

FORALL x ( p ) IS_EMPTY ( X WHERE NOT ( p ) )

( , x X.)
, SQL EXISTS ( FORALL
, ) . , EXISTS SQL
, .
: BOOLEAN,
. ,
EXISTS : , , . , EXISTS (tx), tx , tx, t, t
EXISTS, TRUE, t , FALSE -

275

. (, , ;
, .)
, SQL FORALL.
,
, FORALL(tx), tx
, . , , FORALL (SELECT * FROM S WHERE
CITY = Paris). ? ,
, , ,
, .
, COUNT, :
EXISTS x ( p ) COUNT ( X WHERE p ) > 0
FORALL x ( p ) COUNT ( X WHERE p ) = COUNT ( X )
UNIQUE x ( p ) COUNT ( X WHERE p ) = 1

, , COUNT, ,
, .

,
,
. , , , SQL ,
:
SELECT
FROM
WHERE
(

*
S
EXISTS
SELECT *
FROM SP
WHERE SP.SNO = S.SNO )

, :
SELECT *
FROM S
WHERE ( SELECT COUNT ( * )
FROM SP
WHERE SP.SNO = S.SNO ) > 0

,
, , , -

276

10. SQL

; , ,
. , . , ,
! COUNT
, EXISTS.



. ( , , 6 7 ,
.) , (
) . ,
(, , ), : , , .
, , , . , ,
, , . A
.


.
, 8 , ( ). .
. , () -
S ; () , ,
,
- P ( ). :
SX WHERE EXISTS PX ( TRUE )

, SQL:
SELECT
FROM
WHERE
(

*
S
EXISTS
SELECT *
FROM P )

277

,
. , ( ) ,
, , EXISTS PX (TRUE)
TRUE, WHERE WHERE TRUE,
.
! ; ,
( ), . ,
: , p , , p,
.
,
SQL ( ):
SELECT DISTINCT
CASE WHEN EXISTS ( SELECT * FROM P ) THEN x ELSE y END
FROM S

x, y , ,
x, y , EXISTS
TRUE. , x y
, x SQL-, , y 0. , null, 0.


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

278

10. SQL

, ,
.
, , , , , . , , . , ( ). ; ,
, .
!
Logic and Databases: The Roots of Relational Theory (Trafford,
2007) :
,
, , -. -
( ). , , , , ,
. , - , .

, -,
, , .

10.1. , 16
. . ?
10.2. ( , -). () ,
NOT AND, OR; () , - .
10.3. x .
x , ? ,
? ?
10.4. x . x , ?
, ? ?

279

10.5. CX1 8:
CONSTRAINT CX1 IS_EMPTY ( S WHERE STATUS < 1 OR STATUS > 100 ) ;

IS_EMPTY (...), , .
8 , - S . ? ,
?
10.6. ( .) :
{ SX.SNAME } WHERE EXISTS SPX ( FORALL PX ( SPX.SNO = SX.SNO AND
SPX.PNO = PX.PNO ) )

10.7. ( .) SQL .
10.8. , AND OR .
10.9. p(x) q , x
. ? (,
, . , A B B
A , A B.)
a. EXISTS x ( q ) q
b. FORALL x ( q ) q
c. EXISTS x ( p(x) AND q ) EXISTS x ( p(x) ) AND q
d. FORALL x ( p(x) AND q ) FORALL x ( p(x) ) AND q
e. FORALL x ( p(x) ) EXISTS x ( p(x) )
f. EXISTS x ( TRUE ) TRUE
g. FORALL x ( FALSE ) FALSE
h. UNIQUE x ( p(x) ) EXISTS x ( p(x) )
i. UNIQUE x ( p(x) ) FORALL x ( p(x) )
j. FORALL x ( p(x) ) AND EXISTS x ( p(x) ) UNIQUE x ( p(x) )
k. FORALL x ( p(x) ) AND UNIQUE x ( p(x) ) EXISTS x ( p(x) )
10.10. p(x,y)
x y. ?
a. EXISTS x EXISTS y ( p(x,y) ) EXISTS y EXISTS x ( p(x,y) )
b. FORALL x FORALL y ( p(x,y) ) FORALL y FORALL x ( p(x,y) )
c. FORALL x ( p(x,y) ) NOT EXISTS x ( NOT p(x,y) )

280

10. SQL

d. EXISTS x ( p(x,y) ) NOT FORALL x ( NOT p(x,y) )


e. EXISTS x FORALL y ( p(x,y) ) FORALL y EXISTS x ( p(x,y) )
f. EXISTS y FORALL x ( p(x,y) ) FORALL x EXISTS y ( p(x,y) )
10.11. p(x) q(y)
x y .
?
a. EXISTS x ( p(x) ) AND EXISTS y ( q(y) )
EXISTS x EXISTS y ( p(x) AND q(y) )
b. EXISTS x ( IF p(x) THEN q(x) )
IF FORALL x ( p(x) ) THEN EXISTS x ( q(x) )
10.12. , ,
89 .
10.13. : ,
- , - .
?
, ?
10.14. , SQL (
,
SQL).
10.15. 8:
,
, .
,
, - .
, ? , ?
10.16.
?

11.
SQL-

11


SQL-

6 , ; , , ,
.
:
a. ,
b. ,

. ( , , , , 10 .)
, ;
, ,
, . , ( ) , . , ,
,
, SQL-. : , , ( )
,
SQL. SQL
, -

282

11. SQL-

. :
SQL.


, ,
:
, : exp1
exp2 (, ).
, exp1 exp2
: exp1 exp2 , .
, exp1 exp2 ,
, exp1, , exp2, .
, , . , X1
, x1 ,
x2 x1, X2 , x2 x1 X1. X1 X2 , , X1 X2.
. SQL-
SELECT
FROM
WHERE (
OR
(

SNO
S
STATUS > 10 AND CITY = London )
STATUS > 10 AND CITY = Athens )

WHERE, , :
STATUS > 10 AND ( CITY = London OR CITY = Athens )


SELECT
FROM
WHERE
AND
(

SNO
S
STATUS > 10
CITY = London OR CITY = Athens )

,
.

IF p THEN q ( NOT p ) OR q

10, . ( ) , ,
. : p q -

283

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

NOT ( NOT p ) p

( ).

NOT ( p AND q ) ( NOT p ) OR ( NOT q )
NOT ( p OR q ) ( NOT p ) AND ( NOT q )

, , . ,
: , p q , ,
p, q ( ). ,
. , , ,
:
p q
T
T
F
F

T
F
T
F

p AND q

NOT (p AND q)

(NOT p) OR (NOT q)

T
F
F
F

F
T
T
T

F
T
T
T

, NOT (p AND q) (NOT p) OR (NOT q) .


.

p AND ( q OR r ) ( p AND q ) OR ( p AND r )
p OR ( q AND r ) ( p OR q ) AND ( p OR r )

.
, , SQL- . ,
,
, , 6.
, , ();
(AND
OR) .

284

11. SQL-


FORALL x ( p ( x ) ) NOT EXISTS x ( NOT p ( x ) )

. , ,
EXISTS FORALL (, , EXISTS FORALL , , OR AND ).
: ,
( q NOT q ):
NOT ( p AND NOT q ) ( NOT p ) OR q

( ):
( NOT p ) OR q NOT ( p AND NOT q )

, :
IF p THEN q NOT ( p AND NOT q )


.

SQL-.
.

1.
, , . -, (- ) :
IF COLOR = Red THEN CITY = London

, . , SQL
, :
( NOT ( COLOR = Red ) ) OR CITY = London

( .) , SQL, :
CONSTRAINT BTCX1 CHECK ( NOT ( COLOR = Red ) OR CITY = London )

2.

285

, , ( ,
NOT (a = b) a b ( SQL a <> b)
):
CONSTRAINT BTCX1 CHECK ( COLOR <> Red OR CITY = London )

2.
, 1 , , , .
SQL , , . , , ,
, P. , PX,
PX , PX :
FORALL PX ( IF PX.COLOR = Red THEN PX.CITY = London )

PX , , . , , ,
PX, PY, PZ .. , P; SX, SY, SZ .. S . , ( , SQL), . SQL AS, , SQL-.

SQL FORALL ,
:
NOT EXISTS PX ( NOT ( IF PX.COLOR = Red
THEN PX.CITY = London ) )

( . ,
.) :
NOT EXISTS PX ( NOT ( NOT ( PX.COLOR = Red )
OR PX.CITY = London ) )

SQL, , ,
.
:
NOT EXISTS PX ( NOT ( NOT ( ( PX.COLOR = Red )
AND NOT ( PX.CITY = London ) ) ) )

286

11. SQL-

:
NOT EXISTS PX ( PX.COLOR = Red AND NOT ( PX.CITY = London ) )


NOT EXISTS PX ( PX.COLOR = Red AND PX.CITY London )

, ,
, . SQL . :
- ( ), NOT NOT.
-, EXISTS PX (bx) EXISTS (SELECT *
FROM P AS PX WHERE (sbx)), sbx SQL- bx.
-, , sbx, ,
.
,
CREATE ASSERTION.
:
CREATE ASSERTION ... CHECK
( NOT EXISTS ( SELECT
FROM
WHERE
AND

*
P AS PX
PX.COLOR = Red
PX.CITY <> London ) ) ;

3.
, , .

( ):
{ PX.PNAME } WHERE FORALL PY ( IF PY.CITY = Paris
THEN PY.WEIGHT PX.WEIGHT )

: PNAME PX ,
PY, PY , PY PX . ,
,
.
:
{ PX.PNAME } WHERE NOT EXISTS PY ( NOT ( IF PY.CITY = Paris
THEN PY.WEIGHT PX.WEIGHT ) )

3.

287

:
{ PX.PNAME } WHERE
NOT EXISTS PY ( NOT ( NOT ( PY.CITY = Paris )
OR ( PY.WEIGHT PX.WEIGHT ) ) )

:
{ PX.PNAME } WHERE
NOT EXISTS PY ( NOT ( NOT ( ( PY.CITY = Paris )
AND NOT ( PY.WEIGHT PX.WEIGHT ) ) ) )

,
, NOT (a b) a = b:
{ PX.PNAME } WHERE NOT EXISTS PY ( PY.CITY = Paris AND
PY.WEIGHT = PX.WEIGHT )

SQL:
SELECT
FROM
WHERE
(

DISTINCT PX.PNAME
P AS PX
NOT EXISTS
SELECT *
FROM P AS PY
WHERE PY.CITY = Paris
AND
PY.WEIGHT = PX.WEIGHT )

, DISTINCT SELECT !
:1
PNAME
Screw
Cog

, . ,
,
null. ,
- , , ( , , , ). SQL , EXISTS, PX, P, NOT EXISTS TRUE
PX,
P, .
1

. : ,
SQL-
, DISTINCT . , , , .

288

11. SQL-

, , , null, . ,
, , ! . - , D.

SQL, . ,
, ,
: , .
,
( , null)
SQL-
SELECT DISTINCT PX.PNAME
FROM P AS PX
WHERE PX.WEIGHT NOT IN ( SELECT PY.WEIGHT
FROM P AS PY
WHERE PY.CITY = Paris )

(
null-), :
, , .
: null-!
.

4.
, P1 P2. -
:
{ SX.SNAME } WHERE
EXISTS SPX ( SPX.SNO = SX.SNO AND SPX.PNO = P1 ) AND
EXISTS SPX ( SPX.SNO = SX.SNO AND SPX.PNO = P2 )

SQL- :
SELECT DISTINCT SX.SNAME
FROM S AS SX
WHERE EXISTS ( SELECT *
FROM SP AS SPX
WHERE SPX.SNO = SX.SNO
AND
SPX.PNO = P1 )
AND
EXISTS ( SELECT *
FROM SP AS SPX

4.

289

WHERE SPX.SNO = SX.SNO


AND
SPX.PNO = P2 )

:
SNAME
Smith
Jones

, SQL- . ( , 3 . . 12.)

, ( )
,
. , .
( EXISTS) ,
.
:
SELECT DISTINCT SX.SNAME
FROM S AS SX
WHERE SX.SNO IN ( SELECT
FROM
WHERE
AND
SX.SNO IN ( SELECT
FROM
WHERE

SPX.SNO
SP AS SPX
SPX.PNO = P1 )
SPX.SNO
SP AS SPX
SPX.PNO = P2 )

, SQL-
SELECT sic /* SELECT item commalist SELECT */
FROM T1
WHERE [ NOT ] EXISTS ( SELECT *
FROM T2
WHERE T2.C = T1.C
AND
bx )


SELECT sic
FROM T1
WHERE T1.C [ NOT ] IN ( SELECT T2.C
FROM T2
WHERE bx )

,
. (, ,
, , , ,
.)

290

11. SQL-

, . 3, null- (, null- 4?), , , null-


. ,
.

5.
, . : , .
. .
:
{ SX } WHERE FORALL PX ( IF PX.COLOR = Purple THEN
EXISTS SPX ( SPX.SNO = SX.SNO AND SPX.PNO = PX.PNO ) )

( SX , PX, PX
, SPX, SNO
SX, PNO PX).
:
{ SX } WHERE FORALL PX ( NOT ( PX.COLOR = Purple ) OR
EXISTS SPX ( SPX.SNO = SX.SNO AND SPX.PNO = PX.PNO ) )

:
{ SX } WHERE
FORALL PX ( NOT ( ( PX.COLOR = Purple ) AND
NOT EXISTS SPX ( SPX.SNO = SX.SNO AND SPX.PNO = PX.PNO ) ) )

:
{ SX } WHERE
NOT EXISTS PX ( NOT ( NOT ( ( PX.COLOR = Purple ) AND
NOT EXISTS SPX ( SPX.SNO = SX.SNO AND SPX.PNO = PX.PNO ) ) ) )

:
{ SX } WHERE
NOT EXISTS PX ( ( PX.COLOR = Purple ) AND
NOT EXISTS SPX ( SPX.SNO = SX.SNO AND SPX.PNO = PX.PNO ) )

SQL:
SELECT
FROM
WHERE
(

*
S AS SX
NOT EXISTS
SELECT *
FROM P AS PX

5.

291

WHERE PX.COLOR = Purple


AND
NOT EXISTS
( SELECT *
FROM SP AS SPX
WHERE SPX.SNO = SX.SNO
AND
SPX.PNO = PX.PNO ) )

:1
SNO

SNAME STATUS

CITY

S1
S2
S3
S4
S5

Smith
Jones
Blak e
Clark
Adams

London
Paris
Paris
London
Athens

20
10
30
20
30

, , . , , ,
. exp1
PX.COLOR = Purple

exp2 :
EXISTS SPX ( SPX.SNO = SX.SNO AND SPX.PNO = PX.PNO )

(, SQL).
:
{ SX } WHERE FORALL PX ( IF exp1 THEN exp2 )

, ,
,
. , :
{ SX } WHERE NOT EXISTS PX ( NOT ( IF exp1 THEN exp2 ) )

:
{ SX } WHERE NOT EXISTS PX ( NOT ( NOT ( exp1 ) OR exp2 ) )

:
{ SX } WHERE NOT EXISTS PX ( NOT ( NOT ( exp1 AND NOT ( exp2 ) ) )
1

(. 7), , ,
, S5,
. .
10.

292

11. SQL-

:
{ SX } WHERE NOT EXISTS PX ( exp1 AND NOT ( exp2 ) )

, exp1 exp2
SQL:
SELECT
FROM
WHERE
(

*
S AS SX
NOT EXISTS
SELECT *
FROM P AS PX
WHERE PX.COLOR = Purple
AND
NOT EXISTS
( SELECT *
FROM SP AS SPX
WHERE SPX.SNO = SX.SNO
AND
SPX.PNO = PX.PNO ) )

, , SQL-
, , , , , , .

TutorialD :
S WHERE ( !!SP ) { PNO } ( P WHERE COLOR = Purple) { PNO }

, . PP
P WHERE COLOR = Purple. , , ,
, . ,
:
SP { SNO , PNO } DIVIDEBY PP { PNO }

DIVIDEBY , . , 7.

PP, , , PP {PNO}, ( ), S1, S2, S3, S4.


,
(. )
S5, .

S5, SP,

6.

293

S, S5 SP .
,
,
, . , ( 7), , .

6.
,
. , , .
:
{ SX } WHERE FORALL PX
( IF EXISTS SPX ( SPX.SNO = SX.SNO AND SPX.PNO = PX.PNO )
THEN PX.CITY = SX.CITY )

( SX , PX, PX SX, PX.CITY = SX.CITY).


,
( ):
{ SX } WHERE FORALL PX ( IF exp1 THEN exp2 )
{ SX } WHERE NOT EXISTS PX ( NOT ( IF exp1 THEN exp2 ) )
{ SX } WHERE NOT EXISTS PX ( NOT ( NOT ( exp1 ) OR exp2 ) )
{ SX } WHERE NOT EXISTS PX ( NOT ( NOT ( exp1 AND NOT ( exp2 ) ) ))
{ SX } WHERE NOT EXISTS PX ( exp1 AND NOT ( exp2 ) )

exp1 exp2 SQL:


SELECT
FROM
WHERE
(

*
S AS SX
NOT EXISTS
SELECT *
FROM P AS PX
WHERE EXISTS
( SELECT *
FROM SP AS SPX
WHERE SPX.SNO = SX.SNO
AND
SPX.PNO = PX.PNO )
AND
PX.CITY <> SX.CITY )

294

11. SQL-

:
SNO

SNAME STATUS

CITY

S3
S5

Blake
Adams

Paris
Athens

30
30

, , ,
S3 P2, S5
, , ,
,
, .
Tutori
alD:
S WHERE RELATION { TUPLE { CITY CITY } } = ( ( !!SP ) JOIN P ) { CITY }

7.
. , , : , . -, (?) . , , ,
! , :
SX , PX
PY, SX , PX.CITY= PY.CITY.
, , , . (, , ,
.) :
SX ,
PX PY, SX ,
PX.CITY = PY.CITY.
,
, .
, . ,
-, , .
:
{ SX } WHERE FORALL PX ( FORALL PY
( IF EXISTS SPX ( SPX.SNO = SX.SNO AND SPX.PNO = PX.PNO )

7.

295

AND EXISTS SPY ( SPY.SNO = SX.SNO AND SPY.PNO = PY.PNO )


THEN PX.CITY = PY.CITY ) )

( , ):
{ SX } WHERE FORALL PX ( FORALL PY
( IF exp1 AND exp2 THEN exp3 ) )
{ SX } WHERE NOT EXISTS PX ( NOT FORALL PY
( IF exp1 AND exp2 THEN exp3 ) )
{ SX } WHERE NOT EXISTS PX ( NOT ( NOT EXISTS PY ( NOT
( IF exp1 AND exp2 THEN exp3 ) ) ) )
{ SX } WHERE NOT EXISTS PX ( EXISTS PY ( NOT
( IF exp1 AND exp2 THEN exp3 ) ) )
{ SX } WHERE NOT EXISTS PX ( EXISTS PY ( NOT
( NOT ( exp1 AND exp2 ) OR exp3 ) ) )
{ SX } WHERE NOT EXISTS PX ( EXISTS PY ( NOT
( NOT ( exp1 ) OR NOT ( exp2 ) OR exp3 ) ) )
{ SX } WHERE NOT EXISTS PX ( EXISTS PY (
( exp1 AND exp2 AND NOT ( exp3 ) ) ) )

SQL-:
SELECT
FROM
WHERE
(

*
S AS SX
NOT EXISTS
SELECT *
FROM P AS PX
WHERE EXISTS
( SELECT *
FROM P AS PY
WHERE EXISTS
( SELECT *
FROM SP AS SPX
WHERE SPX.SNO = SX.SNO
AND
SPX.PNO = PX.PNO )
AND
EXISTS
( SELECT *
FROM SP AS SPY
WHERE SPY.SNO = SX.SNO
AND
SPY.PNO = PY.PNO )
AND
PX.CITY <> PY.CITY ) )

, SPX
SPY, SP, ;
(,

296

11. SQL-

SPX) ! :
SNO SNAME STATUS

CITY

S3
S5

Paris
Athens

Blake
Adams

30
30

, : . IF NOT q THEN NOT p. , NOT (NOT q) OR NOT p, q OR NOT p,


NOT p OR q, IF p THEN q. , :
IF p THEN q IF NOT q THEN NOT p

, : p
q, q p.
, , , ,
.
, ( SX ,
PX PY, SX , PX.CITY=
PY.CITY) :
SX ,
PX PY, PX.CITY PY.CITY, SX
. (, , ?)
(
) SQL. .

8. COUNT
. : , .
:
SX ,
, SX, .
, , , .
:
{ SX } WHERE COUNT ( PX.CITY WHERE EXISTS SPX
( SPX.SNO = SX.SNO AND SPX.PNO = PX.PNO ) ) 1

9.

297

, .
, , . SQL :
SELECT *
FROM S AS SX
WHERE ( SELECT COUNT ( DISTINCT PX.CITY )
FROM P AS PX
WHERE EXISTS ( SELECT *
FROM SP AS SPX
WHERE SPX.SNO = SX.SNO
AND
SPX.PNO = PX.PNO ) ) <= 1

, 7.
, COUNT, , , EXISTS .
. -, DISTINCT COUNT SQL-? -, GROUP BY HAVING. ,
, . ( GROUP BY HAVING . 12.)

9.
: SQL, . :
,
( 7 8), . SQL:
SELECT
FROM
WHERE
(

DISTINCT SX.* , PX.CITY


S AS SX , P AS PX
EXISTS
SELECT *
FROM SP AS SPX
WHERE SPX.SNO = SX.SNO
AND
NOT EXISTS
( SELECT *
FROM SP AS SPY
WHERE SPY.SNO = SPX.SNO
AND
EXISTS
( SELECT *
FROM P AS PY
WHERE PY.PNO = SPY.PNO
AND
PY.CITY <> PX.CITY ) ) )

298

11. SQL-

:
SNO

SNAME STATUS

CITY

S3

Blake

Paris

30

: DISTINCT ? ?

10. UNIQUE
10:
CONSTRAINT CX6 FORALL SPX ( UNIQUE SX ( SX.SNO = SPX.SNO ) ) ;

- , .
, EXISTS SX (bx) SQL- EXISTS (SELECT * FROM S AS SX WHERE
(sbx)), sbx SQL- bx. UNIQUE SX (bx) SQL-
UNIQUE (SELECT * FROM S AS SX WHERE (sbx))! (
.) :
UNIQUE ( SELECT * FROM S AS SX WHERE ( sbx ) )
AND
EXISTS ( SELECT * FROM S AS SX WHERE ( sbx ) )

, CX6 :
NOT EXISTS
( SELECT *
FROM SP AS SPX
WHERE NOT UNIQUE
( SELECT *
FROM S AS SX
WHERE SX.SNO = SPX.SNO ) )
OR
NOT EXISTS
( SELECT *
FROM S AS SX
WHERE SX.SNO = SPX.SNO ) )

, ,
UNIQUE SX (bx), , :
COUNT ( SX WHERE ( bx ) ) = 1

:
NOT EXISTS
( SELECT *
FROM SP AS SPX

11. ALL ANY

299

WHERE ( SELECT COUNT ( * )


FROM S AS SX
WHERE SX.SNO = SPX.SNO ) ) <> 1

SQL,
UNIQUE, COUNT. .
NOT EXISTS
( SELECT *
FROM SP AS SPX
WHERE NOT EXISTS
( SELECT *
FROM S AS SX
WHERE SX.SNO = SPX.SNO
AND
NOT EXISTS
( SELECT *
FROM S AS SY
WHERE SY.SNO = SX.SNO
AND ( SY.SNAME <> SX.SNAME OR
SY.STATUS <> SX.STATUS OR
SY.CITY <> SX.CITY ) ) ) )

,
, - ( , S);
. -!

11. ALL ANY


, , SQL ALL ANY ( ,
- EXISTS UNIQUE
SQL). ALL ANY rx sq, rx , sq ,
, SQL
(=, <>, <, <=, >, >=),
ALL, ANY SOME. (SOME ANY, 12.) .
ALL TRUE,
ALL TRUE ,
sq. ( ,
TRUE.)
ANY TRUE,
ANY TRUE
, sq. ( ,
FALSE.)

300

11. SQL-

( ,
).
SELECT DISTINCT PX.PNAME
FROM P AS PX
WHERE PX.WEIGHT >ALL ( SELECT PY.WEIGHT
FROM P AS PY
WHERE PY.COLOR = Blue )

:
PNAME
Bolt
Screw
Cog

, rx
ALL ANY rx sq ,
,
, , , . (, ,
rx , 1,
= <>, . . 3.)
: ALL
ANY ,
. ,
, >ANY >ALL.
, ,
SQL-:
SELECT DISTINCT SNAME
FROM S
WHERE CITY <>ANY ( SELECT CITY FROM P )

: , , .
1 : , , :

? , ,
null?

11. ALL ANY

301

SELECT DISTINCT SNAME


FROM S
WHERE EXISTS ( SELECT *
FROM P
WHERE P.CITY <> S.CITY )

:
SNAME
Smith
Jones
Blake
Clark
Adams

, ALL ANY
, EXISTS,
. , MAX
MIN, ( ) , . MAX MIN
, ALL ANY. . (
.)
ANY

ALL

=
<>

IN

<
<=

< MAX
<=MAX

< MIN
<=MIN

>

> MIN

> MAX

>=

>=MIN

>=MAX

NOT IN

, , =ANY <>ALL IN NOT IN


.1 =ALL <>ANY , , , , EXISTS, .
: , ,
( MAX MIN) , MAX MIN . ,
SQL MAX MIN 1


ALL ANY;
=ANY IN, <>ALL NOT IN.
( , IN NOT IN ,
.)

302

11. SQL-

null.1 , :
SELECT DISTINCT PX.PNAME
FROM P AS PX
WHERE PX.WEIGHT >ALL ( SELECT PY.WEIGHT
FROM P AS PY
WHERE PY.COLOR = Blue )

:
SELECT DISTINCT PX.PNAME
FROM P AS PX
WHERE PX.WEIGHT > ( SELECT MAX ( PY.WEIGHT )
FROM P AS PY
WHERE PY.COLOR = Blue )

, .
P, .2
,
, COALESCE, ,
:
SELECT DISTINCT PX.PNAME
FROM P AS PX
WHERE PX.WEIGHT > ( SELECT COALESCE ( MAX ( PY.WEIGHT ) , 0.0 )
FROM P AS PY
WHERE PY.COLOR = Blue )


, - ,
. :
{ PX.PNAME } WHERE EXISTS PY ( PY.CITY = Paris AND
PX.WEIGHT < PY.WEIGHT )

SQL:
SELECT DISTINCT PX.PNAME
FROM P AS PX
WHERE EXISTS ( SELECT *
FROM P AS PY
WHERE PY.CITY = Paris
AND
PX.WEIGHT < PY.WEIGHT )

: MAX
, MIN
( ).

, . ,
.

12. GROUP BY HAVING

303


ANY:
SELECT DISTINCT PX.PNAME
FROM P AS PX
WHERE PX.WEIGHT <ANY ( SELECT PY.WEIGHT
FROM P AS PY
WHERE PY.CITY = Paris )

:
PNAM E
Nut
Screw
Cam

( ), ,
ALL ANY,
, EXISTS.
:
, <ANY ? (
, ? ? , , <ALL,
?)
?

, null-? ?

12. GROUP BY HAVING


, GROUP BY
HAVING. : , , , , ,
, .
( ):
{ PX.PNO , PX.CITY ,
SUM ( SPX.QTY WHERE SPX.PNO = PX.PNO , QTY ) AS TPQ }
WHERE COUNT ( SPY WHERE SPY.PNO = PX.PNO ) 2

SQL:
SELECT PX.PNO , PX.CITY ,
( SELECT COALESCE ( SUM ( SPX.QTY ) , 0 )
FROM SP AS SPX
WHERE SPX.PNO = PX.PNO ) AS TPQ

304

11. SQL-
FROM P AS PX
WHERE ( SELECT COUNT ( * )
FROM SP AS SPY
WHERE SPY.PNO = PX.PNO ) <= 2

:
PNO

CITY

TPQ

P1
P3
P4
P5
P6

London
Oslo
London
Paris
London

600
400
500
500
100

, , ,
, ( , , )
GROUP BY HAVING:
SELECT
FROM
WHERE
GROUP
HAVING

PX.PNO , PX.CITY , COALESCE ( SUM ( SPX.QTY ) , 0 ) AS TPQ


P AS PX , SP AS SPX
PX.PNO = SPX.PNO
BY PX.PNO
COUNT ( * ) <= 2

:
PX.CITY
SELECT?
, GROUP BY / HAVING ?
, (, .)
,
null-? -?

SQL, () GROUP BY HAVING, () GROUP BY HAVING, :
, N
N > 3.
, N
N < 4.
?

11.1. , , .

305

11.2. SQL-
. , SQL- ( )?
, .
11.3.
SQL-,
.
11.4. ,
( ), ,
SQL-, ,
. , ?

12
.

, SQL
; SQL, . -
SQL.
, : .
SQL . .
: ,
,
. , ,
,
. .
:
,

. ,
;
, , ( ). ORDER BY,

.

307

SELECT *

SELECT *
SELECT * SELECT SQL , , , EXISTS.
- *, . :
. , SELECT * ,
. . :
(.
3), SELECT * , .


SQL TABLE T, T
(.
WITH 6). :
( SELECT * FROM T )

( , , , ):
WITH T1 AS ( SELECT
FROM
WHERE
T2 AS ( SELECT

PNO , PNAME , COLOR , WEIGHT , CITY


P
CITY = London ) ,
PNO , PNAME , COLOR , WEIGHT , CITY ,
2 * WEIGHT AS NEW_WEIGHT , Oslo AS NEW_CITY
FROM T1 ) ,
T3 AS ( SELECT PNO , PNAME , COLOR ,
NEW_WEIGHT AS WEIGHT , NEW_CITY AS CITY
FROM T2 ) ,
T4 AS ( TABLE P EXCEPT CORRESPONDING TABLE T1 )
TABLE T4 UNION CORRESPONDING TABLE T3


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

308

12. , SQL

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


( ) CREATE VIEW
( )
(. )
ORDER BY
JOIN ... USING
( ) INSERT
SET UPDATE


10 , ( , ,
),
(, ,
). SQL AS FROM
JOIN, :
SELECT SX.SNO
FROM S AS SX
WHERE SX.STATUS > 15

SX , S; , S. , SELECT . - , , S1. 15? , S1 .

309

S,
, S2, ; 15, . , SX .

SQL , SX ,
(correlation name), , , , , , ; , , SQL . (, , ,
.) .

SQL , SELECT ; , ,
. , SELECT
SELECT SNO
FROM S
WHERE STATUS > 15

, , :
SELECT S.SNO
FROM S AS S
WHERE S.STATUS > 15

S S
AS AS S S,
S, .1
SQL FROM (
FROM) 2,
1

( ), ,
; , , ,
.

.
;
, , ,
. SQL
, ,
, (, ,
) , SQL.

310

12. , SQL

JOIN ( ,
6). tr .
, tr (.
), tr AS
, , . :
( SELECT SNO , CITY FROM S ) AS TEMP1
NATURAL JOIN
( SELECT PNO , CITY FROM P ) AS TEMP2

( 7):
SELECT PNO , GMWT
FROM ( SELECT PNO , WEIGHT * 454 AS GMWT
FROM P ) AS TEMP
WHERE GMWT > 7000.0

, :
SELECT TEMP.PNO , TEMP.GMWT
FROM ( SELECT P.PNO , P.WEIGHT * 454 AS GMWT
FROM P ) AS TEMP
WHERE TEMP.GMWT > 7000.0

SQL ( , ), :
SELECT *
FROM ( S JOIN P ON S.CITY > P.CITY ) AS TEMP
( A , B , C , D , E , F , G , H , I )

A, B, ..., I SNO, SNAME, STATUS,


S.CITY, PNO, PNAME, COLOR, WEIGHT P.CITY ( JOIN ... ON . 6). , , , .

:
,
, .
,
SQL (, ,
).
: SQL ( ) , , -

311

.
, .

SQL , tx,
; , tx, t, , , t. tx JOIN, (SELECT *
FROM A NATURAL JOIN B) , (A NATURAL
JOIN B) .
(
):
, .
, , . rsq ; rsq ,
. t, r;
rsq , r ( ,
t r).
, ,
. ssq ;
ssq ,
. t, r, v; ssq
, v ( , t
r, r v).
,
( ):
SELECT SNO
FROM S
WHERE CITY IN ( SELECT CITY
FROM P
WHERE COLOR = Red )
UPDATE S
SET ( STATUS , CITY ) = ( SELECT DISTINCT STATUS , CITY
FROM S
WHERE SNO = S1 )
WHERE CITY = Paris ;
SELECT SNO
FROM S

312

12. , SQL
WHERE CITY = ( SELECT CITY
FROM P
WHERE PNO = P1 )

, (
, ), ,
.
IN ,
S ( : , P1).
SELECT DISTINCT S.SNAME
FROM S
WHERE P1 IN ( SELECT PNO
FROM SP
WHERE SP.SNO = S.SNO )

11 , ( , ),
, . ( , , , N , N S.)
, .
, :
SELECT DISTINCT S.SNAME
FROM S
WHERE SNO IN ( SELECT SNO
FROM SP
WHERE PNO = P1 )

, , , () FROM () , ,
FROM. , , .
SQL:
SELECT S.SNO , TEMP.PCT
FROM S , LATERAL ( SELECT COUNT ( ALL PNO ) AS PCT
FROM SP
WHERE SP.SNO = S.SNO ) AS TEMP

LATERAL ,
, -, FROM (

313

SNO S).
:
SNO
S1
S2
S3
S4
S5

PCT
6
2
1
3
0

, , . FROM ,
. , LATERAL, , , ,
LATERAL , ,
, ; , , . , ( , ) ,
.
, . , , (?) :
SELECT S.SNO , ( SELECT COUNT ( ALL PNO )
FROM SP
WHERE SP.SNO = S.SNO ) AS PCT
FROM S

FROM SELECT; - (S.SNO,


), LATERAL .
, AS PCT,
, .
: ; , !
. , : (query) (retrieval request); , , , ,
, , SELECT ( ) SQL, .
, ,
.

314

12. , SQL

(. 2), SQL , ,
. ,
SQL.
. tx .
tx , :
tx ( ALL), , .
tx SELECT, SELECT ( C) , :
SELECT DIS
TINCT.
C MAX MIN.
tx GROUP BY C
.
tx SELECT,
HAVING, HAVING
,
MAX MIN .
. :
ALL, , , SELECT.
NATURAL JOIN JOIN USING
, , , .
ALL , .
, , , , NO PAD , , .

315


, .
, SQL . , ,
. (, ):
VALUES .
SQL null,
( COUNT(*) COUNT,
).

, null.

, , null.

, GROUP BY , ,
, , , .
( ).
.
SELECT .
FROM .
UNION, INTERSECT EX
CEPT .
.

- SQL

- SQL. , ,
SQL; ,
, ,
, (,
).1 :
1

,
. ,
- , .

316

12. , SQL

exp expression ()
ref reference ()
spec specification ()
,
(<identifier>) :
<table name> ( )
<column name> ( )
<range variable name> ( )
(
,
):
<scalar exp>
<boolean exp>

, <top level table


exp>, . , , , - 1, , <table exp>, ,
100 , ! ( , <top level table exp> ,
<table exp>.) , - , , ,
.
<top level table exp>
::= [ <with spec> ] <nonjoin exp>
<with spec>
::= WITH <name intro commalist>
<name intro>
::= <table name> AS <table subquery>
<table exp>
::= [ <with spec> ] <nonjoin or join exp>
<nonjoin or join exp>
::= <nonjoin exp> | <join exp>
1

( ) 2003 ; 1999
.

- SQL
<nonjoin exp>
::= <nonjoin term>
| <nonjoin exp> UNION
[ DISTINCT ] [ CORRESPONDING ] <table term>
| <nonjoin exp> EXCEPT
[ DISTINCT ] [ CORRESPONDING ] <table term>
<table term>
::= <nonjoin term>
| <join exp>
<nonjoin term>
::= <table term> INTERSECT
[ DISTINCT ] [ CORRESPONDING ] <table primary>
| <nonjoin primary>
<table primary>
::= <nonjoin primary>
| <join exp>
<nonjoin
::=
|
|
|

primary>
( <nonjoin exp> )
TABLE <table name>
<table selector>
<select exp>

<table selector>
::= VALUES <row exp commalist>
<row exp>
::= <scalar exp>
| <row selector>
| <row subquery>
<row selector>
::= ( <scalar exp commalist> )
<row subquery>
::= <subquery>
<subquery>
::= ( <top level table exp> )
<select exp>
::= SELECT [ DISTINCT ] [ * | <select item commalist> ]
FROM <table ref commalist>
[ WHERE <boolean exp> ]
[ GROUP BY <column name commalist> ]
[ HAVING <boolean exp> ]

317

318

12. , SQL
<select item>
::= <scalar exp> [ AS <column name> ]
| <range variable name>.*
<table ref>
::= <table name> [ AS <range variable name> ]
| [ LATERAL ] <table subquery> AS <range variable name>
| <join exp>
<table subquery>
::= <subquery>
<join exp>
::= <table ref> CROSS JOIN <table ref>
| <table ref> NATURAL JOIN <table ref>
| <table ref> JOIN <table ref>
USING ( <column name commalist> )
| ( <join exp> )

12.1. SQL- <top level table exp>, , ? (A B


.)
A NATURAL JOIN B
A INTERSECT B
TABLE A NATURAL JOIN TABLE B
TABLE A INTERSECT TABLE B
SELECT * FROM A NATURAL JOIN SELECT * FROM B
SELECT * FROM A INTERSECT SELECT * FROM B
( SELECT * FROM A ) NATURAL JOIN ( SELECT * FROM B )
( SELECT * FROM A ) INTERSECT ( SELECT * FROM B )
( TABLE A ) NATURAL JOIN ( TABLE B )
( TABLE A ) INTERSECT ( TABLE B )
( TABLE A ) AS AA NATURAL JOIN ( TABLE B ) AS BB
( TABLE A ) AS AA INTERSECT ( TABLE B ) AS BB
( ( TABLE A ) AS AA ) NATURAL JOIN ( ( TABLE B ) AS BB )
( ( TABLE A ) AS AA ) INTERSECT ( ( TABLE B ) AS BB )

, ,
, .
?
12.2. - SQL-
. () UNIQUE, ()

319

, () , () ?
12.3.
SQL ( ).
, ,
. SQL.
, . (
- .)


, , ,
. ! .
5 , , , , ,
, , , :
50K .
50K
(,
50K), , , NAME, 50K MONEY ().
, ,
, . , 50K x y , x NAME,
y MONEY.
, x y
.

. :
A B
A B; , (a,b) , a A,
b B.

321


, :
NAME
MONEY
Joe
Am y
Sue
...
Ron

50K
60K
45K
...
60K

( ,
, ,
. .)
, ,
(
NAME) ( MONEY), .
,
( ) -. 1969
, :
n-, (, 50K, D4 1993 ). , n-, ,
n- ( ),
.
, , ,
n > 2 ; (
) .
:
NAME
MONEY
PERSON

SALARY

Joe
Am y
Sue
...
Ron

50K
60K
45K
...
60K

, , , .

322

A.


(), .. (, 60K).
( ), ( ),
, . ( ), ( ).
.


, , , ( , 5),
, .
, . ? ,
, , . , , 2000 (384322 ).
, -
, , CODA
SYL, ? ,
. , , .1
! , , . , , ,
. - , ,
; , ,
. ,
. , , ,
, , . (, - , , .)
1

. , , ,
, ,
.

323

, , . -, ,
, ,
, , . , , , ;
( , ,
). , (, ) ,
.
- ? , (Joe Celko) Data and
Databases: Concepts in Practice (Morgan Kaufmann, 1999) :
[sic],
.

.
, . , :
,
(, , ..). ?
, ? , .
; , ,
, , ,
, . (,
, .
, ,
.)

(- ):
,
19701980-
.
, . , (Mike
Stonebraker) ( Readings in Database
Systems, , Morgan Kaufmann, 1994),
:

324

A.
1: , CACM
1970
2: 1981 ,
3: 12
4:

, .
. 1970 CACM CACM 13, No.6
(June 1970) (A Relational Model of
Data for Large Shared Data Banks).
Relational Database: A Practical Foundation
for Productivity, CACM 25, No. 2 (February 1982). 12
Computerworld: Is Your DBMS Really Relational? Does Your DBMS
Run By The Rules? (14 21 1985). , , The Relational Model For Database Management
Version 2 (Addison-Wesley, 1990). .
, , ,

Databases, Types, and the Relational Model: The Third Manifesto1
, ( !). ,
,
.
; ,
, , i,

. ;
.

, , , . , . ? . , . ,
, , (
, , ) . , ,
, , , , . ,
, ,
1

. D .

325

, . 1 , , , , .
? :
, .

-.

(RVA-
).
,

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

, .

.
, (, ).


, . , ; , ,
1. ( : , , ). -, , , ,
, , , :
. (, ,
, ,
.)

326

A.

,
; ,
.
,
, : , null-, , , - , ,
- . , null-: ,
1969 , null-
1979. ,
, null. , .
,
, . , ,
, , (
), -, , , -,
( , , , ) TABLE_DUM TABLE_
DEE. ( , ,
, , , .
, , ,
, . , ,
.)
, , .
: :
, ,
, BOOLEAN;
;
-, ;
- -;
- .

327


; ,
( , ). , . BOOLEAN ,
(INTEGER,
CHAR ).1
BOOLEAN (NOT, AND, OR ), , , . , ,
,
=, , ,
. , . , v1 v2
, v1 = v2 TRUE, v1 v2 ,
FALSE .



: , - RVA-.
;
() , , ()
() .
-, , - -,
-. (.
).
1

, ()
()
. . 2.

328

A.

RT . RT -
: () RT ()
RT .
, =, RT. (); r1 r2
, r1 r2 , r1 r2.

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

,
.
,
. ,
. , , -, , null, , . ? 5
, ( ) . , , ,
.
. ,
, n , n . , , . n ,

329

, , , , , ( ).
,
! ,
n > 1 n = 1 ( n 1).
, , , , (. ) : , , -, (
, ) null-.
, , , . SQL ; , SQL 1, .
, , , .
, , ,

. (, , -
.)
,
-. , -
, , .
( ), db-
. , -
; , ,
, . , , db-
, ( , -, - ).
1

, , SQL ,
,
, , . SQL ;
, SQL ,
+.
. The Theory of Bags: An Investigative Tutorial
( D).

330

A.


=,
:= ,
. , , . INSERT,
DELETE UPDATE , , , . , () ()
.


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

, -, (,
, ,
- -)
( ; , , IS_EMPTY)
, ,
( ).
, . , , , (
), ,
. -

331

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


, , . ,
Recent Investigations into Relational Data Base Systems (, IFIP 1974 ),
:
1. .
2. ,
,
(
).
3.
.
4. , , (, , , ,
).
5. , .
6.
( , ) ,
.
, , , . , .


11 , , ,
. ,

332

A.

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

t -
- R, , p, t. , t - R - ,
, , p, t, .

, -.

v V V = v TRUE.


, FALSE.

a b (, , ). a b, , .1 : ,
. -: ,
. , :
(. ), .

, , .

333

?
, ,
. , , - , : , , .

-
:
!
, , SQL
. , SQL .
, , - . , , , ,
.
. ,
, , . ( , ,
, .)
, ( , ).1 , , :
- , - ,
, , ( -), . , ,
1

, , , SQL. , ,
(.
B).

334

A.

. ; , , . :
-, ( New York
Times 24 1997 ).

( , ,
). .
rx . , r,
rx, rc,
, , rx. rc ?

, ? ,
?
, ?


, . ,
5 , KEY FOREIGN
KEY , , , Tutorial D. :
, ,
. -
.
. 6 7 ,
, ,
, , .1 ,
1

,
, , : remove ( , )
nand, nor.

335

, - , - .
( , ).

;
, . , , . Temporal Data and the Relational Model (. D)
, (Nikos Lorentzos) , ,
. , , .A.1,
,
( d04 4 d06 6 ..; [d04:d06]
4 6 ). DURING .
SNO

PNO

DURING

S1
S1
S1
S2
S2
S2
S2

P1
P1
P3
P1
P1
P2
P2

[d04:d06]
[d09:d10]
[d05:d10]
[d02:d04]
[d08:d10]
[d03:d03]
[d09:d10]

.A.1.

, , , , , , . , , U_
, U_, U_, U_ .. U_ ,
. .
. -, U_ , () U_ U_ , () U_ () U_ INSERT,
DELETE UPDATE;

336

A.

. -, ,
,
.


, .
, ,
. , ,
- . (

, , .)
, ,
. :
- (, - )?
, , ,
, : , , , SQL?
, .
, SQL , , ,

SQL, . - , ? .
SQL-
, .
, SQL , . , - .

SQL?
SQL , .
. SQL
- TutorialD.
, , , SQL . , TutorialD -
, ( ), SQL -

337

. , COBOL ( ), , , . SQL COBOL , ,


.
, ,
SQL
, SQL. . , SQL - ,
SQL- . ,
, , , SQL, null-,
.


, 1, , . , ,
, .
, ( ,
, ).
( ), , .
. ,
- R
-; , R , , R. , ;

- . , ( 8 , -), - .
, , - (E/R entity/
relationship) . ,

339

- , ,
. , ,
, ,
, . :
-, , , .
, . (. 9),
, - (
, ,
, -
).
, -
( ), ( ).
, , , .
. , , ,
. . , , . ,
;
, , , , ,
, , . . , ,
, (
, /, ).



; ,
. ( ,
, , .) -

340

B.

, , .
: , ,
, . ,
,
. . , ; , -, STATUS
- S, COLOR - P,
QTY - SP .
? , -, , STATUS S SP ( , , , ). .B.1 -
( STP ).
STP SNO
S1
S1
S1
S1
S1
S1
S2
S2
S3
S4
S4
S4

STATUS
20
20
20
20
20
20
10
10
30
20
20
20

PNO

QTY

P1
P2
P3
P4
P5
P6
P1
P2
P2
P2
P4
P5

300
200
400
200
100
100
300
400
200
200
300
400

.B.1. - STP -

, , : , S1 , S1 20,
S2 S2 10 . , , , , , , . ,
, . , . , ,
. .

341

, , , , - ,
.
, , . ; , , , , , , .
, . ,
: . , .
, :
C (, ,
..).
, - R (n+1)-
, n- .
- n-,
(n+1)- .
, .
( ).
.
, , , . , ,
,
, (
) . , , , , .
, . , , , , , ;
, , , .

342

B.


/
, (2), (3) /
() .1 :
: A B
- R. , -
R ()
A B, ,
R, , A, B.
A B A, A B, A B.
, , , , (. . B.2, S2 10 30,
).
{ CITY } { STATUS }

RS
- S. , , , ,
, , ( )
.
RS SNO
S1
S2
S3
S4
S5

SNAME STATUS
Smith
Jones
Blake
Clark
Adams

20
30
30
20
30

CITY
London
Paris
Paris
London
Athens

.B.2. - RS

, , -
R , 8; , ( -)
- R. , TutorialD:
1

343

CONSTRAINT RSC COUNT ( RS { CITY } ) =


COUNT ( RS { CITY , STATUS } ) ;

, : - R A B, A B
A A B B. ,
, , , - , -.
. . (. 5), , , ( );1 ,
SK - R R , , . , , . , {SNO,CITY} ,
- S. , ,
- R R. ,
SK R A R,
R SK A, R SK, , ,
, , , A .
, , . , . ,
, STATUS and CITY:
{ CITY , STATUS } { CITY }
{ CITY , STATUS } { STATUS }
{ CITY }
{ CITY }

, , CITY STATUS, , CITY . , ,


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

,
.

344

B.

: , - R , A B,
R, A R.
, -, ,
: ( , , ) , (
). , : , , ,
, , , ,
, , .
, . ,
, -
RS .B.2 {SNO}
{STATUS}; (
) - SNC CS ( SNC SNO, SNAME CITY, CS CITY STATUS),
- , , .
, , ? :
-
( ,
-). , , . .
, , :
- R ,
, (
). :
- STP (. .B.1) {SNO}
{STATUS}, , , , {SNO} STP, , - (,
, ).
-, SP SS, SP SNO, PNO QTY ( ), SS SNO STATUS.
: - SP SS,
STP, .B.1; ,
SP SS , , ,
.
- RS (. .B.2)
{CITY} {STATUS}
- SNC ( SNO, SNAME CITY) CS

345

( CITY STATUS). :
- S CS, RS,
.B.2; , SN CS ,
, , .


- , -. , , , .
- RS (.B.2) {CITY}
{STATUS}. ,
- SNC CS, - SNS CS,
.B.3. (CS , SNS SNO, SNAME STATUS SNO, SNAME CITY.) , , , () SNS CS , ()
, , S2 .
SNS SNO
S1
S2
S3
S4
S5

SNAME STATUS
Smith
Jones
Blake
Clark
Adams

20
30
30
20
30

CS

CITY
London
Paris
Athens

STATUS
20
30
30

.B.3. - SNS CS

,
? -, , ,
, ; -

-. , ( ,
). : .
, ,
-. ,
- CS - RS, , , - CS , ,
- RS (,
).
. , , : ,

346

B.

-. , . B.3, ,
- RS
SNS CS, . , .B.2, ,
RS SNC CS,
.
, , . ,
, :
R -, R1, R2, ..., Rn R. , R
?

, , (Ian Heath)
1971 , :
A, B, C - R
, - A, B C
. AB -
A B AC. R
A B, R AB AC.

- RS
(.B.2). - {CITY}
{STATUS}. , A {CITY}, B
{STATUS}, C {SNO,SNAME}, RS
{CITY,STATUS}
{CITY,SNO,SNAME}, .

, , . , , , .B.2, , , , .B.3, . , , , , . ( ,
, 1977 (Ron Fagin), . . B.18 .)

, , ,
, ,
.
, ,
,
. .

347

: ,
- RS
CONSTRAINT RSC COUNT ( RS { CITY } ) =
COUNT ( RS { CITY , STATUS } ) ;

- :
CONSTRAINT RSC RS = JOIN { RS { SNO , SNAME , CITY } ,
RS { CITY , STATUS } } ;

( - RS {SNO,SNAME,CITY} {CITY,STATUS};
, n- JOIN.)

?
,
, . , ,
- STP (.B.1). , ; , ,

SP SS, , . ? , ?
: . ,
. , . (- , , !) ,
. ,
:
, , ( ). ; , ,
, , , ,
, , .

1, 2, 3
, , ;
. , - -

348

B.

, , 1, , RVA-.1
- RVA , , . , RVA- (
, ); RVA-
. , , - RVA-
, , IMS,
, . :
,
. , , ,
.
, .
.
, .
.
, , .
7.10, 7.12 7.13 7 , . , , RVA -
. . B.14 .



(5) , , . , , :

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

349

: A, B, ..., C
- R. R ()
* { A , B , ... , C }

( A, B, ..., C),
, R, A, B, ..., C.
:
, R
A, B, ..., C , * {A, B, ..., C}.
, ,
( ) R ,
( , ).

- RS (. . B.2). -
{CITY} {STATUS}
SNC ( SNO, SNAME CITY) CS ( CITY
STATUS). , - RS *{SNC,CS} SNC
CS , -.
, ,
,
,
. , , . , - R
*{A,B,...,C},
, A, B, ..., C R.1 , S.
SNO SNAME, SNO STATUS SNO CITY;
,
* { SN , SS , SC }

, ,
, .
, , .
The Relational Database
Dictionary, Extended Edition (. D).

350

B.

SN {SNO,SNAME}, SS {SNO,STATUS}, SC {SNO,CITY}. , ,


S, . (
.
, , .)
, . , , , . ,
*{A,B,...,C} ,
A, B, ..., C R. , ,
- S:
* { S , SN , SS , SC }

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

, , 5.
: , - R 5, , R, R.
, ,
-, 5, , . - - ,
5 ( ) , , .

5
, , 5 -, , 5
( - ). : , , () ()
, . -

351

( ), , ,
. .
, , , ,
5, , ( ) , -
5. ( ), , , , 5. -, ,
5. :
R -, ,
R ( ,
). R 5.

. , ( ) -,
, ( ,
), , 5, , , - 5.
,
3, , ,
-, 3 , 5. -,
, 3,
3.
:
R -, ,
R ( , R). R 5.

, , , .
5, -,
. , (
) ,
. , - R 5, ,
.
,
; , -

352

B.

- R. ,1 , .
-. , - R
5, , , R , ,
. ,
R 5 , R . , , ,
, , . , , .B.4,
- SPJ, 5 . , , S2
P3, , , P3
J4 (JNO ).
, J1 S2. ( SNO PNO
JNO QTY, {SNO,PNO,JNO}.)
,
-, 2
{ SNO , PNO , JNO } { QTY }

, . , QTY SNO, PNO JNO, - , ;


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

, , , R .
, ,
. The
Relational Database Dictionary, Extended Edition (. D).

, SPJQ SPJ,
- *{SPJQ},
r. ( (. 6), r r.)


SPJ

SNO

PNO

JNO

QTY

S1
S1
S2
S2
S2
S2
S2
S2
S2
S2

P1
P3
P3
P3
P3
P3
P3
P3
P3
P5

J1
J4
J1
J2
J3
J4
J5
J6
J7
J1

200
700
400
200
200
500
600
400
800
100

353

.B.4. - SPJ, 5,

-, -, 5, , , R , ,
, R 5. , R , , R . (
).
-, 5 , - (/), ,
, . , ,
, . , () , , () , , ()
6.
Temporal Data and the Relational Model, , (. D).
6,
( ) -.
: , - R 6,
.
, -, 6, , .1
, - 6 1

, , .

354

B.

, 5, n , n 1 (,
-, 6, 5).
- SP
6, - SPJ (. .B.4). S () P () 5, 6.
: - 6 , , .
, ,
- , 5 6. , - RS
(.B.2) {CITY} {STATUS},
{SNO,STATUS}, , 6, , - . . .

5
. B.5, - SPJ . , *{SP,PJ,SJ}, SP, PJ SJ
{SNO,PNO}, {PNO,JNO} {SNO,JNO}. ?
SPJ

SNO

PNO

JNO

S1
S1
S2
S1

P1
P2
P1
P1

J2
J1
J1
J1

.B.5. - SPJ

, , -
SP, PJ SJ , , . ( SP, PJ SJ , - SPJ; ,
.)
,
IF (s,p) SP AND (p,j) PJ AND (s,j) SJ THEN (s,p,j) SPJ

(s,p), (p,j) (s,j) SP, PJ


SJ, (s,p,j), , SP, PJ SJ,

355

SPJ (
). , , . B.5, (S1,P1), (P1,J1) (S1,J1) SP, PJ
SJ, (S1,P1,J1) SPJ. : ,

, , .
, (s,p) SP , (s,p,z) SPJ z. ,
(p,j) PJ , (x,p,j) SPJ x, (s,j) SJ
, (s,y,j) SPJ
y. :
IF x, y, z (s,p,z)
(x,p,j)
(s,y,j)
THEN
(s,p,j)

SPJ AND
SPJ AND
SPJ
SPJ

.B.5, , , (S1,P1,J2), (S2,P1,J1) (S1,P2,J1) SPJ,


(S1,P1,J1).
, .
?
. , - SPJ , , :
1. - .
2. -
.
3. - .
, - , :
4. .
1, 2 3, , 4
. , 1, 2 3 ,
, -
( z), - ( x) -
( y) , , x , y , z

356

B.

. ,
(connection trap). , ,
4 1, 2 3.
( s p, p j, j s, s p j ,
). , , , -,
, 5.1 ,
, , , .
(4). 5 ,
5.
, () .
; 2 3, , ,
. :
, (
);
- 4, , , .
, , (.
B.19 ); , , 4. , , , . , , 5
n ( n > 2) , -
n , , , , n ,
.

s p j
s, s p j , , ,
-, ,
4 ( 5). . B.16 .

357


,
, , .
, :
,
,



.
. . .
.
, . ,
, , ,
; , ,
. , , , . .
- ( ZIP
):
ADDR { STREET , CITY , STATE , ZIP }

, - :
{ STREET , CITY , STATE } { ZIP }
{ ZIP }
{ CITY , STATE }

, {STREET,CITY,STATE} ,
- . , H ( A {ZIP},
B {CITY,STATE}, C {STREET}):
ZCS { ZIP , CITY , STATE }
KEY { ZIP }
ZS { ZIP , STREET }
KEY { ZIP , STREET }

{STREET,CITY,STATE} {ZIP}, -
, , , . ( -

358

B.

ZCS ZS, .) , - ZCS ZS . , ,


, .B.6;
<10111,Broadway> ZS .
, ZCS,
ZS.
: . , , , - .
ZCS

ZIP
10003
10111

CITY
New York
New York

STATE
NY
NY

ZS

ZIP
10003

STREET
Broadway

.B.6. ZCS ZS

9 , - , , , ZCS ZS
, {ZIP,CITY,STATE}
; , .

. ( ). ,
- . , - STP (.B.1) , S1 20,
25. ,
. , : , ,
, , , (. ). : ( , ).
. , . :

359

5000, ,
6000. A B, ,
A, B ( , B ,
). 5 ;
, ,
( ),
. .
, , ,
:
-, ,
.
-, -
5, , .
-, , . , , - ,
, ? ,
.
,
- . ,
. , , , , , . , ,
. (, ,
, . SQL ; ,

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

360

B.

). . -
RS (. .B.2) {CITY} {STATUS}.
- SNC (
SNO, SNAME CITY) CS ( CITY STATUS). . .B.2
, 30,
20 30,
26,667.
SQL:
1. SELECT AVG ( STATUS ) AS RESULT
FROM
RS
(): 26. , . ,
DISTINCT AVG? :
2. SELECT AVG ( DISTINCT STATUS ) AS RESULT
FROM RS
(): 25. , , . :
3. SELECT CITY, AVG ( STATUS ) AS RESULT
FROM
RS
GROUP BY CITY
(): (Athens,30), (London,20), (Paris,30).
, . , ?
4. SELECT CITY, AVG ( AVG ( STATUS ) ) AS RESULT
FROM RS
GROUP BY CITY
: . SQL .1
:
1

, SQL;
Tutorial D, ,
. . SQL- SELECT
SUM(SP.QTY) AS SQ FROM SP WHERE SP.QTY > 100 (
). SUM
SP.QTY FROM SP WHERE SP.QTY > 100,
. SQL. ,
AVG(SUM(QTY)) ,
SQL ,
AVG, SUM.

361

5. SELECT AVG ( TEMP.STATUS ) AS RESULT


FROM ( SELECT DISTINCT RS.CITY, RS.STATUS
FROM RS ) AS TEMP
(- ): 26,667. ,

, .B.3:
SELECT AVG ( STATUS ) AS RESULT
FROM CS

, (, ).
,
. , .

.B.7 , : - SA , SB ,
, 30. ,
,
S3 -, , , .
( .)
SA

SNO
S2
S3

SNAME STATUS
10
Jones
Blake
30

CITY
Paris
Paris

SB

SNO

SNAME STATUS

CITY

S1
S3
S4
S5

Smith
Blake
Clark
Adams

London
Paris
London
Athens

20
30
20
30

???
???

/* */

/* 30*/

.B.7. - SA SB

, , S3 -. ,
SB, SA. ,
SA, , S3 . SB , S3 . ,
, ( 8
, ).

362

B.

, .B.7, . ,
-, , , ,
- . , , , :
CONSTRAINT SA_AND_SB_OVERLAP
( SA WHERE STATUS = 30 ) =
( SB WHERE STATUS = 30 AND CITY = Paris ) ;

, :
( ):
-
, t
, - , .
, , - , , .
, - , ,
, ,
,
- . .B.8,
, , .
-, , SX SY {SNO,SNAME}1,
. :
SX SNO
S1
S2
S3
S4
S5

SNAME STATUS
Smith
Jones
Blake
Clark
Adams

20
10
30
20
30

SY SNO
S1
S2
S3
S4
S5

SNAME CITY
Smith
Jones
Blake
Clark
Adams

London
Paris
Paris
London
Athens

.B.8. - SX SY
1

, ,
, ,
, . .

363

( ): A B -. A B
A1, A2, ..., Am B1, B2, ..., Bn , Ai A1,
A2,..., Am Bj B1, B2, ...,
Bn -, , t
Ai, t Bj.
,
,
- R,
R. . , , .
(
): A B -.
A B
A1, A2, ..., Am B1, B2, ..., Bn ,
Ai (i = 1, ..., m) Bj (j = 1, ..., n)
6. i j , : () Ai Ak () Ak
Bj .
, , (Ak WHERE ax) = (Bj WHERE bx), ax bx ,
FALSE .
.
, , , , ( ,
) .
, ( ). ,
,
, -
.

. -
S SX SY (.B.8). , :
5, ,
- S

364

B.

.1 ,
.
, -
- (restric
tion). ,
,
, . ( ,
,
, -.) .



.
,
. ,
, ( ,

).
: .
,
,
, . , ,
, . . ( , . .)
: 1 ,
,
, , -
. , SQL 1

, ,
-. , . , : . ,
- S
{SNO,STATUS}, {SNO,SNAME} {SNO,STATUS,CITY} , , S, , . ,
.

365

; , . , ,
, ,
(.B.9). ( ,
, .)


. . . . . . . . .

. . . . . . . . . . . . .

.B.9. ( )

, , .
: ( , ), . ,
. ,
,
,
. , . ,
. ,
, , , . The TransRelational Model.
, ,
An Introduction to Database Systems (. D).
,
.
-, ( ); 5, 6, . .
-, 6 - ( null ). null-, , ,
. - ,

366

B.

. : Wovon man nicht


reden kann, darber muss man schweigen ( , ). , ,
S1 S2, S1 , S2 . 6 ,
.B.10.
SN SNO
S1
S2

SNAME
Smith
Jones

SS SNO
S1

STATUS
20

SC SNO
S1
S2

CITY
London
Paris

.B.10. S2

,
,
. ( The Closed World Assumption, D, .)
, , S2 null,
, S2.
: , ,
. - , .


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

, ,
, . , , .

367

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

.
-,
-. 5,
. ( , 6.) ,
(, , ) ; , , . , , , . , , , , .
, , , , , ,
( , ). .
(
) . -,
, .
-, . -,
, .
: , . , . , ( , ).

368

B.

. , ,
, . , , .

B.1. .
B.2. , ,
- SP.
B.3. . ?
B.4. ,
- ,
-. , r1 r2 r
, r r1 r2, r1 r2
r. . ( , , .
, , ,
.)
B.5. . ,
.
B.6. ? ? ?
B.7. , . , .
?
B.8. () , () ?
?
B.9. R - n. , R
( )?
B.10. , A B A B
, ,
?

369

B.11. : d p l, t c ( d , , p , 1 8
). , , .
-
. 5? 6?
?
B.12. ,
,
- .
?
B.13.
. ,
( SPNO) -
(, ), {SPNO} -. (, , {SNO,PNO} ,
.) , ,
() ()
(
, , ).
, ,
, . : ? ,
?
B.14. ( .) , , , . , , , . , , , ; , , ; ,
; , ,
. ,
, .
B.15. .
. , . -

370

B.

, . () . ,
?
B.16. A, B C
- R , -
A, B C . AB -
A B AC. ,
R ()
A B
A C

( A B A B, A B, B A), R
*{AB,AC}. , -
R, A B A C,
: (a,b1,c1) (a,b2,c2),
(a,b1,c2) (a,b2,c1).
B.17. , R A B,
A B.
B.18. ( ) R , B.16. , R AB AC , A B A C.
B.19. , K R, K A A - R. : , . , R 4, , R , - . A B , AB R A B;
, A .
B.20. -,
, 4.
B.21. .
,
.
; .

; . ,

. , .

371

B.22. Tutorial D CON


STRAINT, , -
SPJ .B.5.
B.23. ( B.21.)
. , .
;
.
;
. , . ,
r a, p a, r p, r p a.
B.24. ?
a. -,
, .
b. -,
, 5.
c. - .
B.25. XML. XML . ,
XML? .
B.26. -
B.11, B.14, B.21 B.23. ?


1
1.1. , :

;

, - (
) ;

- ;
( ) , t .
.
1.2. . . (19232003) , . 2003 ,
, ACM SIGMOD
http://www.acm.org/sigmod . Date on Database:
Writings 2000-2006 (Apress, 2006).

373

1.3. ,
. ,
, , , ( , ).
. :
SQL,
. . 2 ( , 2.1).
1.4. , , ( , ), ( -,
,
). , B A, A
. . 5 8.
1.5. R -, r
, R .
, R , r. , R , r. , , , R , ,
.
, , SQL
T, , , T , T ( SQL-
-). ,
T , . .
1.6. . .
1.7. . . , , ,
, (, ), . , -

374

.
, ,
. , , , , ( ,
). : ,
, , ,
, .. , , .. , . (, , , , , , . , , - , ,
.) : , ,
.
1.8. , ; , ; , -.
, ,
( ),
, , , ( ) 5,
. 3.5 3.
1.9. .
1.10. , (
, ). - . , . (
, A.)
1.11. .

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

375

,
-
, , , , ,
()
. , ,
( , ) , .
-.
, , , (
, ).
, ,
(/ -).
SQL, , ,
, , ,
, .

. ( , ) !
1.12. :

, .
(, -).
.
, ? ?
-? ?
,
: , , ,
. (, , ,
,
.) , , , , . , ( , STATUS CITY?).

376

,
, ,
. ,
.
? - ? , (
), . -, -,
, - ,
, , .
, ?
, , . !
, ,
.
1.13. CREATE
TABLE. . 2. : CREATE TABLE, TutorialD, 5, .
CREATE TABLE S
( SNO
VARCHAR(5)
SNAME VARCHAR(25)
STATUS INTEGER
CITY VARCHAR(20)
UNIQUE ( SNO ) ) ;

NOT
NOT
NOT
NOT

NULL
NULL
NULL
NULL

,
,
,
,

CREATE TABLE P
( PNO
VARCHAR(6)
PNAME VARCHAR(25)
COLOR CHAR(10)
WEIGHT NUMERIC(5,1)
CITY VARCHAR(20)
UNIQUE ( PNO ) ) ;

NOT
NOT
NOT
NOT
NOT

NULL
NULL
NULL
NULL
NULL

,
,
,
,
,

CREATE TABLE SP
( SNO
VARCHAR(5) NOT NULL ,
PNO
VARCHAR(6) NOT NULL ,
QTY
INTEGER
NOT NULL ,
UNIQUE ( SNO , PNO ) ,
FOREIGN KEY ( SNO ) REFERENCES S ( SNO ) ,
FOREIGN KEY ( PNO ) REFERENCES P ( PNO ) ) ;

, SQL ,

( , Tutorial D, . 2). , SQL

377

null-; ( ), . , NOT NULL


, , .
1.14. TutorialD ( SQL , SQL ):
SP := SP UNION
RELATION { TUPLE { SNO S5 , PNO P6 , QTY 250 } } ;

UNION (. 3),
, . :
UNION D_UNION. .
5.
1.15. ,
7:
S := WITH ( S WHERE CITY = Paris ) AS R1 ,
( EXTEND R1 ADD ( 25 AS NEW_STATUS ) ) AS R2 ,
R2 { ALL BUT STATUS } AS R3 ,
R3 RENAME ( NEW_STATUS AS STATUS ) AS R4 :
( S MINUS R1 ) UNION R4 ;

1.16. :
R := rx ;

R -, rx , , R.
SQL :
DELETE FROM T ;
INSERT INTO T tx ;

T SQL-, -
R, tx SQL-,
rx. DELETE; , ( ) DELETE
INSERT ,
DELETE INSERT
( ).
, : INSERT / DELETE / UPDATE?
,
, DELETE/
INSERT . , , , ,
, .

378

. ,
SQL ,
. , , , SQL- ( ):
SELECT LS.*
FROM ( SELECT SNO, SNAME, STATUS
FROM S
WHERE CITY = London ) AS LS

, , LS. . ,
LS - . LS ,
,
.
. (
, .)
. SQL CREATE TABLE CREATE TABLE AS,
, , ,
.
, , CREATE
TABLE AS .
1.17.
, An Introduction
to Database Systems (. D).
-. , .
,
: SNO CITY, ,
, , S1
. ( ),
. ( 4),
,
, !
. 4 Double Trouble, Double Trouble, D.
. , , ,
97- , -

379

. ,
.
, ,
: n- , ,
. ( A), ,
n , n . n > 1, , , , ,
, . , ! , n > 1 n = 1.
, ()
,
. , (, ) .
. , , ( ), ( ),
, . . , , - . , , , , . :
( SQL) .
. A Sweet Disorder, D.
, ,
, ( )
. .

2
2.1.
, : , , , XML-, .. ..

380

. : SQL . , CREATE TYPE CREATE DOMAIN.


CREATE TYPE SQL- TYPE TutorialD, 8
( ,
). CREATE
DOMAIN CREATE
TYPE ( 1992 , CREATE TYPE
1999); , CREATE TYPE, CREATE DOMAIN.
2.2.
; , .
T , S T; T S, S T. . 8. : TutorialD
(
, , ), SQL ( ) . ,
, SQL ,
SQL . .
; , . , , ,
( ).
TutorialD:
4
XYZ
FALSE
5.0
POINT(5.0,2.5)

/* INTEGER
/* CHAR
/* BOOLEAN
/* FIXED
/* POINT

*/
*/
*/
*/
*/

,
, - . ; ,
, (, , , , INTEGER, 95, ). ,
; , , .

381

2.3. THE_ -
. . 8. : , THE_ Tutorial D SQL . ,
THE_ , SQL
, THE_
SQL . .
2.4. , ; (,
- ).
2.5. ,
.
, . ( , - , .) :

, . ,
(2+3)1,
. , , 2+3 (
+; ,
- ).
. (:
, 5
.) ; , ,
, .

-, - ( )
-. ( , ),
, , .1 :
.
, , ARRAY RELATION;
,
. ,
.
1


Inclusion Dependencies and
Foreign Keys (. D).

382

; , . ( ).
,
; , . , , .. ,
. ,
, . , 3 TABLE_DUM TABLE_DEE,
( , )!
, ; ,
.
, ; , . ( ), X T, ,
X, , T, , T. , , ENO ( ) CHAR ( ), ,
; , (||) ENO.
( ) ,
(
). , ,
,
.
(
, .)
( ) , (
). , , , . (
, .) ,
( ), .
2.6. . , ( -

383

, ;
,
).
2.7.
.
2.8. , , ( ,
).
: TUPLE RELATION. : , ,
. , REF SQL .
2.9. , (1), ; ,
.
(, , ). , , , ()
- ( )
() -
,
. , 1 ,
.
.

, 1 ,
. , . ( ), .
1 What First Normal Form
Really Means (. D).

2.10. X , T,
X . , X (
) , T.
2.11.
OPERATOR CUBE ( I INTEGER ) RETURNS INTEGER ;
RETURN I * I * I ;
END OPERATOR ;

384

2.12.
OPERATOR FGP ( P POINT ) RETURNS POINT ;
RETURN POINT ( F ( THE_X ( P ) ) , G ( THE_Y ( P ) ) ) ;
END OPERATOR ;

2.13.
- S:
RELATION { SNO CHAR , SNAME CHAR , STATUS INTEGER , CITY CHAR }

- S . ,
.1.3 1, .
2.14. SQL
1.13 1. TutorialD:
VAR P BASE RELATION
{ PNO CHAR , PNAME CHAR , COLOR CHAR , WEIGHT FIXED , CITY CHAR }
KEY { PNO } ;
VAR SP BASE RELATION
{ SNO CHAR , PNO CHAR , QTY INTEGER }
KEY { SNO }
FOREIGN KEY { SNO } REFERENCES S
FOREIGN KEY { PNO } REFERENCES P ;

SQL Tu
torialD:
1.13 1, SQL 1
. - , . ( SQL
, 3.)
SQL . . 5.
SQL .
, - (, P) , :
- P
.
- P ,
(, , P JOIN SP), ,
1

, .

385
-
. ( , -
TutorialD; , , SQL,
, 100 .) .
6.

2.15. a. ; LOCATION = CITY(London). b.


; BOOLEAN. c. ; MONEY ( , MONEY
MONEY). d. ; BUDGET+MONEY(50000).
e.; ENO > ENO(E2). f. ; NAME(THE_C(EN
AME)||THE_C(DNAME)) ( , NAME C CHAR).
g. ; CITY(THE_C(LOCATION)||burg) ( ,
CITY C CHAR). :
, T . . 8.
2.16.
, ( ). . , ; (, , INTEGER).
( TYPE Tutorial D . 8)
, . , ,
. ,
,
. , SQL
, ,
.
2.17. , , ;
, ! , ,
,
( ) . . Databases, Types, and the Relational Model: The Third Manifesto
(. D).
2.18. T SQL, =
, C T. C

386

, DISTINCT, GROUP BY ORDER BY C , , , . , ? , .


, T SQL, =
, C T. C , DISTINCT GROUP BY ( ..
..) , .
2.19. .
X AB ( ),
Y AB, PAD SPACE. X = Y TRUE, CHAR_LENGTH(X)
CHAR_LENGTH(Y) 3 2 .
,
, DISTINCT, GROUP BY ORDER BY ( ,
).
2.20. () , () , () ,
() , , - , ()
. ( .) . . Inclusion
Dependencies and Foreign Keys.
2.21. null-; X null ( X , null , ), X =
NULL TRUE. , null-. .
2.22. ! ( , ,
INTEGER?)
. , , .
2.23. ; . ; -

387

, ,
. , SQL
X + Y

,
SET Z = X + Y ;

.
2.24. RVA- ,
, , ,
. T, T, ! , , ( . .)
T. : ( -, ). , , , ,
ENO ( ), ENAME ( ) JOBHIST,
JOB (), FROM TO ( FROM TO ).
2.25. SQL , ,
, .
( 12) .
2.26. . 3.

3
3.1. . .
3.2. ,
! , () tx
ty ,
A1, A2, ..., An i (i = 1, 2, ..., n) vx Ai tx vy Ai ty; ()
rx ry , .
3.3. TutorialD:
TUPLE { PNO P1 , PNAME Nut ,
COLOR Red , WEIGHT 12.0 , CITY London }
TUPLE { SNO S1 , PNO P1 , QTY 300 }

SQL ( ):
ROW ( P1 , Nut , Red , 12.0 , London )

388

.
ROW ( S1 , P1 , 300 )

( ,
SQL) SQL-.
ROW .
3.4. ,
:
RELATION { TUPLE { SNO S1 , PNO P1 , QTY 300 } ,
TUPLE { SNO S1 , PNO P2 , QTY 200 } }

SQL ( ):
VALUES ROW ( S1 , P1 , 300 ) ,
ROW ( S1 , P2 , 200 )

ROW. , .
, SQL-
VALUES ( ROW ( S1 , P1 , 300 ) ,
ROW ( S1 , P2 , 200 ) )

( )
! . 3.10.
3.5.
An Introduction to Database Systems (. D).
,
(
).

,
.
, (,
, ) , S1 S1.
,
. ,
. , SQL:
SELECT DISTINCT S.CITY, S.STATUS * 2, P.CITY
FROM S, P

389

,
.
-, -.
, , (.
TABLE_DUM TABLE_DEE ).
( , SQL) null-,
.
, ,
n-.
3.6. :
, . , , t {H},
t
{H} .
() , , () , :
TUPLE { NAME Superman ,
ADDR TUPLE { STREET
CITY
STATE
ZIP

1600 Pennsylvania Ave. ,


Washington ,
DC ,
20500 } }

TUPLE { SNO S2 ,
PNO_REL RELATION { TUPLE { PNO P1 } ,
TUPLE { PNO P2 } } }

3.7. RVA-
R4 .2.2 2;
RVA- R1 .2.1 2.
RVA-:
CNO
C1

C2

TEACHER

TEXT

TNO

XNO

T2
T4
T5

X1
X2

TNO

XNO

T4

X2
X4
X5

390

: CNO TNO TEACHER


XNO TEXT. RVA-, :
CNO

TNO

XNO

C1
C1
C1
C1
C1
C1
C2
C2
C2

T2
T2
T4
T4
T5
T5
T4
T4
T4

X1
X2
X1
X2
X1
X2
X2
X4
X5

RVA- ,
RVA-, , .2.2 2,
, , PNO_REL S2
:
SNO

PNO_REL

S2

PNO

S3

PNO
P2

S4

PNO
P2
P4
P5

, RVA- , RVA-, . ( :
! ,
B.14 .)
, , , ,
. , r1 r2 ,
r1 r2 , ,
. , R4
.2.2 2 R1 .2.1 2, :

391

3
R4 = R1 GROUP ( { PNO } AS PNO_REL )
R1 = R4 UNGROUP ( PNO_REL )

, ,
.1 GROUP UNGROUP . 7.
3.8. TABLE_DEE TABLE_DUM ( DEE
DUM) ; DEE
(0-), DUM . SQL , SQL
. ( , SQL-
.)
3.9. (:
10.)
0, . . . TRUE FALSE. TRUE FALSE DEE DUM,
!
, ( ),

, , , .
- 0? , , , - ,
.
-. , .
3.10. SQL-
( ). SQL-
, ,
, , . ,
( ) .
3.11. :

:
r1 r2 ,
q1, r1, q2 r2, ( ).

392

.
SELECT
FROM
WHERE
OR (
OR
OR

SNO
S
STATUS
STATUS
STATUS
SNO IS

> 20
= 20 AND SNO > S4 )
IS NULL
NULL

3.12. . .
3.13. . .
3.14. EXISTS (tx), tx SQL- rx (, tx SQL).
3.15. . .
3.16. AS SELECT (
); CREATE VIEW FROM (

AS); WITH , .
, ()
; () AS
<-> AS ; () AS
<->. .

4
4.1. , , , (. A). a b , . a b,
, ! , , , (
, ). Double Trouble,
Double Trouble (. D).
4.2. , ,
, WHERE UNION
. The Theory
of Bags: An Investigative Tutorial (. D);
, SQL, , , . , UNION
ALL UNION DISTINCT! ,
r SQL- C, -

393

, v.
:
SELECT
FROM
WHERE
OR

C
r
TRUE
TRUE

: v * 2.
SELECT
FROM
WHERE
UNION
SELECT
FROM
WHERE

C
r
TRUE
DISTINCT
C
r
TRUE

: v * 1.
SELECT
FROM
WHERE
UNION
SELECT
FROM
WHERE

C
r
TRUE
ALL
C
r
TRUE

: v * 4.

ALL ( )
DISTINCT, . ?

4.3. ,
, .
, , . .
4.4. , , () - (), , . ( .) , , ,
SQL ( ,

394

- ) ,
, .
4.5. , .
4.6. !
4.7. : ,
null- ? , , ,
. , 50000 . , . ,
, ,
. , , null, ! 50000 . null (, ).

-
! : -

.
, . null? ! , -
. -, , , , null (,
null ). , null .
4.8. ( SQL!). Null , . UNKNOWN ,
, TRUE FALSE.
, SQL ( ,
,
).
4.9. , ; MAYBE p SQL p IS
UNKNOWN.
4.10. 2VL 4 16 , 4 16 . (
, NOT, AND OR, ,
):

395

NOT
T
F

T
T

T
F

T
F

T
F

F
T

T F

IF

T F

NAND

T F

T
F

T T
T T

T
F

T F
T T

T
F

F T
T T

OR

T F

T
F

T
F

T
F

T
F

F
F
T F

T
F

F F
T T

T F

XOR

T F

T T
T F

T
F

T F
T F

T
F

F T
T F

T
F

F F
T F

T F

IFF

T F

T F

NOR

T F

T T
F T

T
F

T F
F T

F T
F T

T
F

F F
F T

T F

AND

T F

T T
F F

T
F

T F
F F

T
F

T F

T F
T
F

F T
F F

T F
T
F

F F
F F

3VL 27 (3 )
19683 (3 32) . ( n-
(nVL) n n n n2
). ,
, 3VL ,
2VL ( , , ,
, , null ).
4.11. 2VL ,
NOT AND, OR ( . 10.2). , 3VL SQL
!
. . Is SQLs Three-Valued Logic Truth Functionally Com
plete? (. D).
4.12. 3VL , , bx UNKNOWN, UNKNOWN. 3VL , : bx OR NOT bx OR MAYBE bx. : , SQL ;
,
, , , . , -

396

(
, ). : , 2VL 3VL, .
4.13. 3VL, , bx UNKNOWN, UNKNOWN. 3VL (
!) , : bx AND NOT bx AND NOT MAYBE
bx. : ,
, .
4.14. 3VL () r JOIN r r () INTER
SECT JOIN. ,
() null- ,
() ( ? , null). TIMES - JOIN, 2VL.
4.15. . x SQL. , x : x1 x2 (, !). x
IS NULL x1 IS NULL AND x2 IS NULL,
x IS NOT NULL x1 IS NOT NULL
AND x2 IS NOT NULL. , null, null ?
, , ,
, null-. ( , ), , ,
, . , null ,
, , , ( ,
) , . , , SQL ( , , ),
, .1
4.16. . 3VL:
NOT
T
U
F

IS NOT TRUE
F
U
T

T
U
F

F
T
T

, .

397

4.17. . ,
x SQL-. ( 4.15), x : x1 x2. x IS
NOT NULL x1 IS NOT NULL AND x2
IS NOT NULL, NOT (x IS NULL) x1
IS NOT NULL OR x2 IS NOT NULL. ?
4.18. , , , , , EMP.
DNO null ( ?). : , (
).
4.19. , , P2 ( , , ).
, ,
P2. (, , , , P2 SP, null).
4.20. . (a) SQL;
(b) UNIQUE SQL;
(c) SQL. , k1 k2 null, (a) UNKNOWN, (b)
FALSE, (c) TRUE. : , :
SQL k1 = k2
TRUE, k1 k2 null ; FALSE, k1
k2 null , UNKNOWN .
SQL- UNIQUE k1 = k2
TRUE , k1 k2 null ,
FALSE .
SQL k1 k2 , ()
null () null.

= SQL ( ) (. 2). : ,
? .

4.21. INTERSECT
ALL EXCEPT ALL ,
.

398

4.22. ! ( ,
, .)
4.23. .

5
5.1. , , . - , . ,
, , ,
, , . ? , ; ; null-; ; ;
( .. ..).
- : ,
.
5.2. ,
STATUS .
( ) ,
. :
s - S.
ls s, CITY .
ls , ls , STATUS ,
UPDATE.
s , (s MINUS ls)
UNION ls.
s S.
5.3. , SQL (, ).
1 , , . , , .

399

5.4. . t1, ; t2,


,
( VALUES). S
S6, t1 t2 . S
, t1 , t2 , null.
5.5. , v V V = v TRUE. SQL , v
null, , , ,
=, XML, ( ) . : , .
5.6. ,
, :
CREATE TABLE TAX_BRACKET
( LOW
MONEY NOT NULL ,
HIGH
MONEY NOT NULL ,
PERCENTAGE INTEGER NOT NULL ,
UNIQUE ( LOW ) ,
UNIQUE ( HIGH ) ,
UNIQUE ( PERCENTAGE ) ) ;
CREATE TABLE ROSTER
( DAY DAY_OF_WEEK NOT NULL ,
HOUR TIME_OF_DAY NOT NULL ,
GATE GATE
NOT NULL ,
PILOT NAME
NOT NULL ,
UNIQUE ( DAY, HOUR, GATE ) ,
UNIQUE ( DAY, HOUR, PILOT ) ) ;
CREATE TABLE MARRIAGE
( SPOUSE_A
NAME NOT NULL ,
SPOUSE_B
NAME NOT NULL ,
DATE_OF_MARRIAGE DATE NOT NULL ,
UNIQUE ( SPOUSE_A, DATE_OF_MARRIAGE ) ,
UNIQUE ( DATE_OF_MARRIAGE, SPOUSE_B ) ,
UNIQUE ( SPOUSE_B, SPOUSE_A ) ) ;

5.7. ,
, . (
, , , , k r , r, r. , ,

400

, , ,
, ,
-, .)
5.8. . , -
A , K X, K X A,
K . - A K X. ,
- B ,
A. B K X; B, , (, , , -).
5.9. ( ),
,
. , ,
- P {PNO}, PNO,
P1 TUPLE
{PNO P1}, P1.
5.10. m ,
n/2. R
, () m , () n
m-1 . ,
R n!/(m!*(n-m)!). : n! n ,
n n * (n-1) * ...
* 2 * 1. - TAX_BRACKET MARRIAGE
(. 5.6) - ,
- 0. ( n = 0,
0!/(0!*0!), 0! 1.)
5.11. ,
. , . , .
. . X
- R; X R, K R, K X. ,
- SP: {SNO,PNO}, {SNO}, {PNO} {}. ,

401

{}
- R.
5.12. :
EMP

ENO

MNO

E4
E3
E2
E1

E2
E2
E1
E1

, ( E1) , null . , ,
-,
, .
EMP ENO
E4
E3
E2
E1

...
...
...
...
...

EM

ENO

MNO

E4
E3
E2

E2
E2
E1

: -
EM - EMP? ( ,
.)
5.13. , ,

, . . .
5.14. , . ,
- , :
VAR SNT BASE RELATION
{ SNO SNO, SNAME NAME, STATUS INTEGER }
KEY { SNO }
FOREIGN KEY { SNO } REFERENCES SC ;
VAR SC BASE RELATION
{ SNO SNO, CITY CHAR }
KEY { SNO }
FOREIGN KEY { SNO } REFERENCES SNT ;

, ,
,
- (, , -

402

-). . 8.
5.15. TutorialD ( , TutorialD CASCADE NO CASCADE):
VAR P BASE RELATION { PNO ... , ... } KEY { PNO } ;
VAR PP BASE RELATION { MAJOR_PNO ... , MINOR_PNO ... , QTY ... }
KEY { MAJOR_PNO , MINOR_PNO }
FOREIGN KEY { MAJOR_PNO } REFERENCES P
RENAME ( PNO AS MAJOR_PNO ) ON DELETE CASCADE
FOREIGN KEY { MINOR_PNO } REFERENCES P
RENAME ( PNO AS MINOR_PNO ) ON DELETE NO CASCADE ;

, MAJOR_PNO ( ), ,
MINOR_PNO ( ).
SQL:
CREATE TABLE P ( PNO ... , ... , UNIQUE ( PNO ) ) ;
CREATE TABLE PP ( MAJOR_PNO ... , MINOR_P# ... , QTY ... ,
UNIQUE ( MAJOR_PNO , MINOR_PNO ) ,
FOREIGN KEY ( MAJOR_PNO ) REFERENCES P ( PNO )
ON DELETE CASCADE ,
FOREIGN KEY ( MINOR_PNO ) REFERENCES P
ON DELETE RESTRICT ) ;

5.16. ,
. , : NO ACTION ( ), CASCADE, RESTRICT,
SET DEFAULT SET NULL. : NO ACTION RESTRICT?
5.17. ,
; . , 10 .
5.18. - P: PNO , PNAME, COLOR
WEIGHT CITY. -
SP: SNO PNO QTY.
5.19. - R
. -

403

R , R .
5.20. .
5.21. (),
, .
(, ) ,
, .
,
null- , .
The Closed World Assumption (. D).
5.22. , - R
, , , R (
; R , ). , , , .
.
5.23. . 5.17.
5.24. , , , ,
- . ,
-, , (, , ). ,
- C :
VAR C BASE RELATION { CITY CHAR } KEY { CITY } ;

! :
CITY ; CITY ,
; CITY
1. - 0
VAR Z BASE RELATION { } KEY { } ;

, , ( - , ,
),
1

CITY - .
- CITY , , .

404

, . TRUE, Z TABLE_DEE,
FALSE, Z TABLE_DUM.
, , - Z ; ,
- 0 (. 5.22).
5.25. , . ,
-.
, , {CITY}, S CITY,
- . , , , ,
,
-.

6
, , . , (
) S JOIN (P{PNO}) (S
JOIN P){PNO}. : S P{PNO} ,
;
( ),
30. ,
( ,
); , P3.
, . :
r1 r2, , r1 r2
A. , . r2: r3 = r2 RENAME (A AS B).
(r1 TIMES r3) WHERE A = B.
, , A, B , .
, B, r1 JOIN r2.
6.1. a.
( ). b.
. c. ( ).
d. -. e. -

405

SQL: S NATURAL JOIN P S.CITY.1 f.


. g. -,
; , SNO, .
6.2. ! ,
, , .
( 7):
a. PZ RELATION {PNO PNO},
.
SP { SNO , PNO } DIVIDEBY PZ { PNO }

SP{SNO} SP SNO.
b. z TABLE_DEE, TABLE_DUM.
r DIVIDEBY z

r JOIN z.
c. r s .
r DIVIDEBY s

TABLE_DEE, r s
r, TABLE_DUM .
d. , r DIVIDEBY r TABLE_DUM, r , TABLE_
DEE .
6.3. SNO, PNO
CITY. :
SNO

SNAME STATUS

CITY

PNO

QTY

PNAME COLOR

S1
S1
S1
S2
S3
S4

Smith
Smith
Smith
Jones
Blake
Clark

London
London
London
Paris
Paris
London

P1
P4
P6
P2
P2
P4

300
200
100
400
200
200

Nut
Screw
Cog
Bolt
Bolt
Screw

20
20
20
10
30
20

Red
Red
Red
Green
Green
Red

WEIGHT
12.0
14.0
19.0
17.0
17.0
14.0

: SNO , SNAME, STATUS CITY;


PNO , PNAME,
1

S.SNO ( SNO); ,
. ( , , ,
, .)

406

COLOR WEIGHT
CITY; SNO PNO QTY.
, SNO , PNO CITY.
SQL :
S NATURAL JOIN SP NATURAL JOIN P

( SELECT * FROM).
6.4.
(x,0) (0,y) (x,y) X Y ; , (x) (y) (x,y) . n ( (. 3),
n-).
6.5.
a. SQL-:
SELECT DISTINCT CITY
FROM S NATURAL JOIN SP
WHERE PNO = P2

SQL- ,
SQL.

: CITY ,
, P2.
CITY
London
Paris

b. SQL-:
SELECT
FROM
WHERE
(

*
P
PNO NOT IN
SELECT PNO
FROM SP
WHERE SNO = S2 )

: PNO , PNAME, COLOR WEIGHT,


CITY S2.

407

6
PNO

PNAME COLOR

P3
P4
P5
P6

Screw
Screw
Cam
Cog

Blue
Red
Blue
Red

WEIGHT
17.0
14.0
12.0
19.0

CITY
Oslo
London
Paris
London

c. SQL-:
SELECT
FROM
EXCEPT
SELECT
FROM

CITY
S
CORRESPONDING
CITY
P

: CITY ,
, .
CITY
Athens

d. SQL-:
SELECT SNO , PNO
FROM S NATURAL JOIN P

TutorialD S P. , ?

: SNO PNO .
SNO

PNO

S1
S1
S1
S2
S2
S3
S3
S4
S4
S4

P1
P4
P6
P2
P5
P2
P5
P1
P4
P6

e. SQL-:
SELECT S.CITY AS SC , P.CITY AS PC
FROM S , P

: SC, PC.

408

.
SC

PC

London
London
London
Paris
Paris
Paris
Athens
Athens
Athens

London
Paris
Oslo
London
Paris
Oslo
London
Paris
Oslo

6.6.
, .
,
. , . t
. , ,
, :
t r UNION (s UNION u)

t r OR t (s UNION u)
t r OR (t s OR t u)
(t r OR t s) OR t u
t (r UNION s) OR t u
t (r UNION s) UNION u

, OR.
.
SQL, , , null
- ( , ?). :
SELECT A, B FROM T1 UNION CORRESPONDING SELECT B, A
FROM T2 SELECT B, A FROM T2 UNION CORRESPONDING
SELECT A, B FROM T1 ,
.
, SQL, , ( ).
T1 JOIN T2 T2 JOIN T1 ( ),
. , SQL, , ( ).
.
6.7. RENAME.
6.8. t t. , t1
t2 , t1 t2 -, ! . 4.4.

409

6.9. , TutorialD , SQL :1


a. SP

| SELECT * FROM SP
| or
| TABLE SP /* . 12 */

b. ( SP WHERE PNO = P1 )
{ SNO }

c. S WHERE STATUS 15

AND STATUS 25

d. ( ( S JOIN SP )

| SELECT SNO
| FROM SP
| WHERE PNO = P1
| SELECT *
| FROM S
| WHERE S.STATUS BETWEEN
|
15 AND 25

| SELECT
WHERE CITY = London ) | FROM
{ PNO } | WHERE
| AND

DISTINCT PNO
SP, S
SP.SNO = S.SNO
S.CITY = London

e. P { PNO } MINUS

| SELECT PNO
( ( S JOIN SP )
| FROM P
WHERE CITY = London ) | EXCEPT CORRESPONDING
{ PNO } | SELECT PNO
| FROM SP , S
| WHERE SP.SNO = S.SNO
| AND
S.CITY = London

f. WITH SP { SNO , PNO } AS Z :

| SELECT DISTINCT
( ( Z RENAME ( PNO AS X ) )
|
JOIN
| FROM SP AS XX
( Z RENAME ( PNO AS Y ) ) ) | WHERE XX.SNO =
{ X , Y }
|

g. ( S WHERE STATUS <

| SELECT
STATUS FROM ( TUPLE FROM ( S | FROM
WHERE SNO = S1 ) ) ) { SNO } | WHERE
|
(
|
|

SNO
S
STATUS
SELECT
FROM
WHERE

XX.PNO AS X ,
YY.PNO AS Y
, SP AS YY
YY.SNO

<
STATUS
S
SNO = S1 )

(. 3), TutorialD STATUS FROM


(TUPLE FROM r) STATUS r ( r 1). SQL-
:

, . , Tu
torialD , 7.

410

, , .
h. WITH ( S WHERE CITY =
London ) AS RX ,
( SP RENAME ( PNO AS Y ) )
AS RY :
( P WHERE RX { SNO }

( RY WHERE Y = PNO )
{ SNO } ) { PNO }

|
|
|
|
|
|
|
|
|
|
|

SELECT
FROM
WHERE
SELECT
WHERE
AND
SELECT
FROM
WHERE
AND

DISTINCT SPX.PNO
SP AS SPX
NOT EXISTS (
S.SNO FROM S
S.CITY = London
NOT EXISTS (
SPY.*
SP AS SPY
SPY.SNO = S.SNO
SPY.PNO =
SPX.PNO ) )

Tu
torialD. SQL- EXISTS (. 10).
i. ( S { SNO } JOIN P { PNO } ) | SELECT SNO , PNO
MINUS SP { SNO , PNO }

|
|
|
|

j. WITH ( SP WHERE SNO = S2 )


AS RA ,
( SP RENAME ( SNO AS X ) )
AS RB :
S WHERE ( RB WHERE X = SNO )
{ PNO } RA { PNO }

FROM
EXCEPT
SELECT
FROM
|
|
|
|
|
|
|
|
|
|
|
|

S, P
CORRESPONDING
SNO , PNO
SP

SELECT
WHERE
SELECT
WHERE
SELECT
WHERE
AND
OR
SELECT
FROM
WHERE
AND

S.SNO FROM S
NOT EXISTS (
P.* FROM P
NOT EXISTS (
SP.* FROM SP
SP.PNO = P.PNO
SP.SNO = S2 )
EXISTS (
SP.*
SP
SP.PNO = P.PNO
SP.SNO = S.SNO ) )

6.10. , . .
6.11.
SQL, SELECT R.* FROM R UNION CORRES
PONDING SELECT R.* FROM R
SELECT R.* FROM R. , R ,
. ( , R null? !)
, . , SQL, null.

411

6.12. r{} r
; TABLE_DUM, r
TABLE_DEE . ? r. , SP{} ( ) : SNO, PNO QTY
, SNO PNO
QTY. , , , ; SP ( SP{} TABLE_DUM), FALSE,
( SP{} TABLE_DEE) TRUE.
r{ALL BUT} r
( , r); r.
6.13. , DB2 Ingres . , .
6.14. ,
. , ,
( ). . ( , , ) .
11.
6.15. SQL :
SELECT CITY
FROM ( SELECT CITY FROM S
UNION CORRESPONDING
SELECT CITY FROM P ) AS TEMP
WHERE NOT EXISTS
( SELECT
CITY FROM S
INTERSECT CORRESPONDING
SELECT
CITY FROM P )

SQL- . , , , . :
CORRESPONDING (?), CORRESPONDING;
, .
6.16. r1 r2 , , , (, , - -

412

). .
n- : r1, r2, ...,
rn (n > 0) ,
i, j (1 i n, 1 j n) ri rj .
6.17. n- JOIN, UNION
D_UNION.
SQL n- ,
, n < 2. :
t1 NATURAL JOIN t2
NATURAL JOIN t3
..........
NATURAL JOIN tn
SELECT * FROM t1 UNION CORRESPONDING SELECT * FROM t2
UNION CORRESPONDING SELECT * FROM t3
...............................
UNION CORRESPONDING SELECT * FROM tn

n- MINUS ,
MINUS , .
6.18. . 6.12.
. -
S SNO S{SNO}. r; r . r r{}. ,
; , r
r .
, r, 5 ,
() ,
, TABLE_DEE.
, - . - S
:
SNO , SNAME,
STATUS CITY.
r = S{SNO} :
SNAME STATUS CITY ,
SNO , SNAME, STATUS CITY.
r{} :

413

SNO,
SNAME STATUS
CITY , SNO ,
SNAME, STATUS
CITY.
, : TRUE
FALSE. r{} TABLE_DEE,
() TRUE. ,
. S{SNO} r, r{} TABLE_
DUM, () FALSE.

7
7.1.
a. SQL-:
SELECT
FROM
WHERE
(

*
S
SNO IN
SELECT SNO
FROM SP
WHERE PNO = P2 )

SQL- ,
SQL.

: SNO ,
SNAME, STATUS, CITY P2.
SNO

SNAME STATUS

CITY

S1
S2
S3
S4

Smith
Jones
Blake
Clark

London
Paris
Paris
London

20
10
30
20

b. SQL-:
SELECT
FROM
WHERE
(

*
S
SNO NOT IN
SELECT SNO
FROM SP
WHERE PNO = P2 )

414

: SNO ,
SNAME, STATUS, CITY
P2.
SNO

SNAME STATUS

CITY

S5

Adams

Athens

30

c. SQL-:
SELECT
FROM
WHERE
(

*
P AS PX
NOT EXISTS
SELECT *
FROM S AS SX
WHERE NOT EXISTS
( SELECT *
FROM SP AS SPX
WHERE SPX.SNO = SX.SNO
AND
SPX.PNO = PX.PNO ) )

: PNO , PNAME, COLOR WEIGHT,


CITY .
PNO

PNAME COLOR

WEIGHT CITY

d. SQL-:
SELECT *
FROM P
WHERE ( SELECT COALESCE ( SUM ( QTY ) , 0 )
FROM SP
WHERE SP.PNO = P.PNO ) < 500

: PNO , PNAME, COLOR WEIGHT,


CITY ,
, 500.
PNO

PNAME COLOR

P3
P6

Screw
Co g

WEIGHT

Blue
Red

e. SQL-:
SELECT
FROM
WHERE
(

*
P
CITY IN
SELECT CITY
FROM S )

17.0
19.0

CITY
Oslo
London

415

: PNO , PNAME, COLOR WEIGHT,


CITY , .
PNO

PNAME COLOR

P1
P2
P4
P5
P6

Nut
Bolt
Screw
Cam
Cog

Red
Green
Red
Blue
Red

WEIGHT
12.0
17.0
14.0
12.0
19.0

CITY
London
Paris
London
Paris
London

f. SQL-:
SELECT S.* , Supplier AS TAG
FROM S

: SNO ,
SNAME, STATUS, CITY, P2 TAG Supplier.
SNO

SNAME STATUS CITY

S1
S2
S3
S4
S5

Smith
Jones
Blake
Clark
Adams

20
10
30
20
30

London
Paris
Paris
London
Athens

TAG
Supplier
Supplier
Supplier
Supplier
Supplier

g. SQL-:
SELECT SNO , 3 * STATUS AS TRIPLE_STATUS
FROM S

: SNO ,
SNAME, STATUS, CITY, P2 TRIPLE_STATUS, STATUS.
SNO

SNAME STATUS CITY

S1
S2
S3
S4
S5

Smith
Jones
Blak e
Clark
Adams

20
10
30
20
30

London
Paris
Paris
London
Athens

TRIPLE_STATUS
60
30
90
60
90

h. SQL-:
SELECT PNO , PNAME, COLOR , WEIGHT , CITY , SNO , QTY
WEIGHT * QTY AS SHIPWT
FROM P NATURAL JOIN SP

: PNO , PNAME, COLOR WEIGHT,


CITY, SNO -

416

QTY, ( PNO
SNO) SHIPWT WEIGHT QTY.
SNO

PNO

QTY

PNAME COLOR

S1
S1
S1
S1
S1
S1
S2
S2
S3
S4
S4
S4

P1
P2
P3
P4
P5
P6
P1
P2
P2
P2
P4
P5

300
200
400
200
100
100
300
400
200
200
300
400

Nut
Bolt
Screw
Screw
Cam
Cog
Nut
Bolt
Bolt
Bolt
Screw
Cam

WEIGHT
12.0
17.0
17.0
14.0
12.0
19.0
12.0
17.0
17.0
17.0
14.0
12.0

Red
Green
Blue
Red
Blue
Red
Red
Green
Green
Green
Red
Blue

CITY

SHIPWT

London
Paris
Oslo
London
Paris
London
London
Paris
Paris
Paris
London
Paris

3600. 0
3400. 0
6800. 0
2800. 0
1200. 0
1900. 0
3600. 0
6800. 0
3400. 0
3400. 0
4200. 0
4800. 0

i. SQL-:
SELECT P.* , WEIGHT * 454 AS GMWT , WEIGHT * 16 AS OZWT
FROM P

: PNO , PNAME, COLOR, WEIGHT,


GMWT (= 454, WEIGHT) OZWT
(=16, WEIGHT).
PNO

PNAME COLOR

P1
P2
P3
P4
P5
P6

Nut
Bolt
Screw
Screw
Cam
Cog

Red
Green
Blue
Red
Blue
Red

WEIGHT
12.0
17.0
17.0
14.0
12.0
19.0

CITY

GMWT

OZWT

London
Paris
Oslo
London
Paris
London

5448.0
7718.0
7718.0
6356.0
5448.0
8626.0

192.0
204.0
204.0
168.0
192.0
228.0

j. SQL-:
SELECT P.* , ( SELECT COUNT ( SNO )
FROM SP
WHERE SP.PNO = P.PNO ) AS SCT
FROM P

: PNO , PNAME, COLOR, WEIGHT,


CITY SCT .
PNO

PNAME COLOR

P1
P2
P3
P4
P5
P6

Nut
Bolt
Screw
Screw
Cam
Cog

Red
Green
Blue
Red
Blue
Red

WEIGHT
12.0
17.0
17.0
14.0
12.0
19.0

CITY
London
Paris
Oslo
London
Paris
London

SCT
2
4
1
2
2
1

417

k. SQL-:
SELECT S.* , ( SELECT COUNT ( PNO )
FROM SP
WHERE SP.SNO = S.SNO ) AS NP
FROM S

: SNO ,
SNAME, STATUS, CITY NP .
SNO

SNAME STATUS CITY

S1
S2
S3
S4
S5

Smith
Jones
Blake
Clark
Adams

20
10
30
20
30

London
Paris
Paris
London
Athens

NP
6
2
1
3
0

l. SQL-:
SELECT CITY , AVG ( STATUS ) AS AVG_STATUS
FROM S
GROUP BY CITY

: CITY
AVG_STATUS.
CITY

AVG_STATUS

London
Paris
Athens

20
20
30

m. SQL-:
SELECT COUNT ( SNO ) AS N
FROM S
WHERE CITY = London

: N .
N
2

.
n. SQL-:
( SELECT *
FROM SP
EXCEPT CORRESPONDING
SELECT *
FROM SP
WHERE SNO = S1 )
UNION CORRESPONDING

418

.
SELECT S7 AS SNO , PNO , QTY * 0.5 AS QTY
FROM SP
WHERE SNO = S1

: () SNO PNO
QTY ( SNO S1), () SNO S7
S1 PNO ,
QTY. : , SP
- S7?
SNO

PNO

QTY

S7
S7
S7
S7
S7
S7
S2
S2
S3
S4
S4
S4

P1
P2
P3
P4
P5
P6
P1
P2
P2
P2
P4
P5

150
100
200
100
50
50
300
400
200
200
300
400

7.2. r1 MATCHING r2 r2 MATCHING r1 , r1 r2


, JOIN{r1,r2}, ,
, INTERSECT{r1,r2}.
7.3. , ,
S RENAME ( CITY AS SCITY )

( EXTEND S ADD ( CITY AS SCITY ) ) { ALL BUT CITY }

.
7.4. EXTEND S { SNO } ADD ( COUNT ( !! SP ) AS NP )
7.5. , , ,
. , SUM(1), n , n.
a.
r : CT

r n (n > 0): CT
n

b.
r : CT

r n (n > 0): CT

419

c.
r : CT

r n (n > 0): CT
n

d.
r :

CT

r n (n > 0):

CT
n

,
1. r , ,
(a) (c), (b) (d). SQL:
a. SELECT COUNT ( * ) AS CT
FROM
EXCEPT
SELECT
FROM

r
CORRESPONDING
0 AS CT
r

b. SELECT COUNT ( * ) AS CT
FROM

INTERSECT CORRESPONDING SELECT 0 AS CT FROM r.

c. , (a).
d. , (b).
7.6. SQL null , COUNT,
0. .
7.7. : SNO PNO , PNO PNO_REL.
, (?).
7.8. r ,
SP, , RVA- X. , X, 0, TABLE_DEE,
TABLE_DUM, sp SP 0- sp, . ,
r SP,

420

TABLE_DEE X, GROUP :
EXTEND SP ADD ( TABLE_DEE AS X )

r UNGROUP (X) SP.


7.9.
a. N := COUNT ( SP

| SET N = ( SELECT COUNT ( * )


WHERE SNO = S1 ) ; |
FROM S
|
WHERE SNO = S1 ) ;
b. ( S WHERE CITY =
| SELECT *
MIN ( S , CITY ) ) { SNO } | FROM S
| WHERE CITY =
|
( SELECT MIN ( CITY )
|
FROM S )
c. S { CITY }
| SELECT DISTINCT CITY
WHERE COUNT ( !!S ) > 1 | FROM S AS SX
| WHERE ( SELECT COUNT ( * )
|
FROM S AS SY
|
WHERE SY.CITY = SX.CITY )
|
> 1
d. RESULT :=
| SET RESULT = CASE WHEN
IF AND ( S , SNAME < CITY ) |
( SELECT COALESCE ( EVERY
THEN Y ELSE N END IF ; |
( SNAME < CITY ) , TRUE ) )
|
FROM S ) THEN Y
|
ELSE N
|
END ;

EVERY SQL- AND TutorialD, null, TRUE, .

7.10. , P2, , S2, . , , ;


R4 , -. : , RVA- () , ().
7.11. , , .
PNO_REL RVA-. , ,
, S5,
, ( , S SP) null-.
, ,

421

; , , RVA- !
SNO

SNAME STATUS CITY

S1

Smith

20

London

PNO_REL
PNO
P1
P2
...
P6

S2

Jones

10

Paris

PNO
P1
P2

...

.....

..

......

.....

S5

Adams

30

Athens

PNO

,
r,
( r UNGROUP ( PNO_REL ) ) { ALL BUT PNO }

. ,
, , S5.
7.12. : ,
SNO S6, SNAME Lopez, STATUS 30, CITY Madrid,
PNO_REL , , PNO
P5. , , ( ) TutorialD,
(, ):
<relation
::=
|
|
|

assign>
<relvar ref> := <relation exp>
<relation insert>
<relation delete>
<relation update>

<relation insert>
::= INSERT <relvar ref> <relation exp>
<relation delete>
::= DELETE <relvar ref> [ WHERE <boolean exp> ]
<relation update>
::= UPDATE <relvar ref> [ WHERE <boolean exp> ] :

422

.
{ <attribute assign commalist> }

<attribute assign> , , , ,
<relation assign>, . , S2
, PNO_REL S5.
, :
a. , S6 P5.
b. , S2 P5.

( RVA-)
-
SP (
QTY):
a. INSERT SP RELATION { TUPLE { SNO S6 , PNO P5 } } ;
b. INSERT SP RELATION { TUPLE { SNO S2 , PNO P5 } } ;
SSP . 7.10,
(,
) -, RVA-.
7.13. (a) :
WITH ( SSP RENAME ( SNO AS XNO ) ) { XNO, PNO_REL } AS X ,
( SSP RENAME ( SNO AS YNO ) ) { YNO, PNO_REL } AS Y :
( X JOIN Y ) { XNO, YNO }

, RVA- ( ).
(b) . (a)
, SSP ,
(b) ,
. :1
1

RVA- IMS ( XML?). IMS


, , , ,
. ,
.

423

7
WITH ( SSP UNGROUP ( PNO_REL ) ) GROUP ( { SNO } AS
AS
( PPS RENAME ( PNO AS XNO ) ) { XNO, SNO_REL }
( PPS RENAME ( PNO AS YNO ) ) { YNO, SNO_REL }
( X JOIN Y ) { XNO, YNO }

SNO_REL )
PPS ,
AS X ,
AS Y :

7.14.
WITH ( P RENAME ( WEIGHT AS WT ) ) AS R1 ,
( EXTEND P ADD ( COUNT ( R1 WHERE WT > WEIGHT )
AS N_HEAVIER ) AS R2 :
( R2 WHERE N_HEAVIER < 2 ) { ALL BUT N_HEAVIER }
SELECT *
FROM P AS PX
WHERE ( SELECT COUNT ( * )
FROM P AS PY
WHERE PX.WEIGHT > PY.WEIGHT ) < 2

P2, P3 P6 (
3, 2). , ; , , .

.
Databases, Types, and the Relational Model: The Third Manifesto (. D) , :
( ( RANK P BY ( DESC WEIGHT AS W ) ) WHERE W 2 ) { ALL BUT W }

SQL .

7.15. SUMMARIZE. :
SUMMARIZE SP PER ( S { SNO } ) ADD ( SUMD ( QTY ) AS SDQ )

SUMD D
distinct ,
. (,
), ( ) ,
EXTEND -:
EXTEND S { SNO } ADD ( SUM ( !!SP , QTY ) AS SDQ )

7.16.
EXTEND S ADD ( COUNT ( !!SP ) AS NP , COUNT ( !!SJ ) AS NJ )

424

.
( SUMMARIZE SP PER ( S { SNO } ) ADD ( COUNT ( PNO ) AS NP ) )
JOIN
( SUMMARIZE SJ PER ( S { SNO } ) ADD ( COUNT ( JNO ) AS NJ ) )
SELECT SNO , ( SELECT
FROM
WHERE
( SELECT
FROM
WHERE
FROM S

COUNT ( PNO )
SP
SP.SNO = S.SNO ) AS NP ,
COUNT ( JNO )
SJ
SJ.SNO = S.SNO ) AS NJ

7.17. , !! , , !!(!!SP) !!SP, :


S WHERE (!!SP) { PNO } = P { PNO }

( , ).
7.18. .
7.19. S JOIN SP ; S MATCH
ING SP ( ). r1 JOIN r2 r1 MATCHING r2
, r1 r2 ( r1
INTERSECTION r2).
7.20. r1 r2 t1 r1, !!r2 ( t1) 0: TABLE_DEE,
t1 r2, TABLE_DUM . r1
r2 , !!r2 TABLE_DEE
r1.
7.21. , S ,
, , ,
null.

8
8.1. , . T T; , .
,
. , ,
,

425
-. , . :
. , .

8.2. ,
FALSE ,
FALSE
-. -
FALSE , -, -
-. ,
, , -
.
8.3. SQL , CREATE ASSERTION. , . SQL ,
(
). SQL , . - ,
-
. , B A, A . -
- R
, R. SQL , ,
, . - ,
-. ,
. DB
AND -
DB TRUE.
- R AND , R, TRUE.
( ) .
-, , ,
. -

426

() , () ?
.
8.4. . .
8.5. a. 345. b. QTY.
8.6. . .
8.7.
TYPE CITY POSSREP { C CHAR CONSTRAINT
OR
OR
OR
OR
OR
OR
OR

C
C
C
C
C
C
C
C

=
=
=
=
=
=
=
=

London
Paris
Rome
Athens
Oslo
Stockholm
Madrid
Amsterdam } ;

CITY - S P
CITY, CHAR.
8.8. ,
,
, . ,

,
. . ,
:
CREATE TABLE C ( CITY CHAR , UNIQUE ( CITY ) ) ;

, :
INSERT INTO C VALUES London
Paris
Rome
Athens
Oslo
Stockholm
Madrid
Amsterdam

,
,
,
,
,
,
,
;

:
CREATE TABLE S ( ... ,
FOREIGN KEY ( CITY ) REFERENCES C ( CITY ) ) ;
CREATE TABLE P ( ... ,
FOREIGN KEY ( CITY ) REFERENCES C ( CITY ) ) ;

, .

427

( ) S P. CREATE ASSERTION. .
, , , .
8.9.
TYPE SNO POSSREP
{ C CHAR CONSTRAINT
CHAR_LENGTH ( C ) 2 AND CHAR_LENGTH ( C ) 5
AND SUBSTR ( C, 1, 1 ) = S
AND CAST_AS_INTEGER ( SUBSTR ( C, 2 ) ) 0
AND CAST_AS_INTEGER ( SUBSTR ( C, 2 ) ) 9999 } ;

, CHAR_LENGTH, SUBSTR CAST_


AS_INTEGER .
8.10.
TYPE LINESEG POSSREP { BEGIN POINT, END POINT } ;

, POINT , .
8.11. POINT, . , PARALLELOGRAM,
(
?). : ,
. :
TYPE POINT
POSSREP CARTESIAN { X FIXED , Y FIXED
CONSTRAINT SQRT ( X ** 2 + Y ** 2 ) 100.0 }
POSSREP POLAR { R FIXED , THETA FIXED
CONSTRAINT R 100.0 } ;

, ,
, .
8.12. , , ,
.
8.13. INSERT,
DELETE UPDATE, :
CX1: INSERT S, UPDATE STATUS S
CX2: INSERT S, UPDATE CITY STATUS S

428

CX3: INSERT S, UPDATE SNO S


CX4: INSERT S, UPDATE SNO CITY S
CX5: UPDATE STATUS S, INSERT SP, UPDATE
SNO PNO SP ( ,
CX6 SP S)
CX6: DELETE S, UPDATE SNO S, INSERT SP, UPDATE
SNO SP
CX7: INSERT LS NLS, UPDATE SNO LS NLS
CX8: INSERT S P, UPDATE SNO CITY S,
UPDATE PNO CITY P
CX9: UPDATE SNO STATUS S
8.14. , ,
.
8.15. ( ,
.) c
T; c CREATE
ASSERTION FORALL r (c)
, SQL, NOT EXISTS r
(NOTc), r T. , , .
8.16. FORALL ,
; . 10.
TutorialD , .
8.17.
CREATE TABLE S
( ... ,
CONSTRAINT CX5 CHECK
( STATUS >= 20 OR SNO NOT IN ( SELECT SNO
FROM SP
WHERE PNO = P6 ) ) ;
CREATE TABLE P
( ... ,
CONSTRAINT CX5 CHECK
( NOT EXISTS ( SELECT
FROM
WHERE
AND

*
S NATURAL JOIN SP
STATUS < 20
PNO = P6 ) ) ;

,
, -

429

. ,
.
( , CREATE ASSERTION.)
8.18. CX1
(restriction); CX5 . , , S, CX1 , , CX5 .
8.19. , , ; CX3. ,
CX3, KEY , . ( , - , . ,
.)
8.20.
CREATE ASSERTION CX8 CHECK
( ( SELECT COUNT ( * )
FROM ( SELECT CITY
FROM S
WHERE SNO = S1
UNION CORRESPONDING
SELECT CITY
FROM P
WHERE PNO = P1 ) AS POINTLESS ) < 2 ) ;

8.21. - TutorialD SQL , , . , , .


a. CONSTRAINT CXA IS_EMPTY

( P WHERE COLOR = Red AND WEIGHT 50.0 ) ;

CREATE ASSERTION CXA CHECK ( NOT EXISTS (


SELECT *
FROM P
WHERE COLOR = Red
AND
WEIGHT >= 50.0 ) ) ;

b. CONSTRAINT CXB IS_EMPTY (


( S WHERE CITY = London )
WHERE TUPLE { PNO P2 } (!!SP) { PNO } ) ;

430

.
CREATE ASSERTION CXB CHECK (
NOT EXISTS ( SELECT * FROM S
WHERE CITY = 'London'
AND
NOT EXISTS
( SELECT * FROM SP
WHERE SP.SNO = S.SNO
AND
SP.PNO = 'P2' ) ) ) ;

c. CONSTRAINT CXC COUNT ( S ) = COUNT ( S { CITY } ) ;


CREATE ASSERTION CXC CHECK ( UNIQUE ( SELECT CITY FROM S ) ) ;

d. CONSTRAINT CXD COUNT ( S WHERE CITY = 'Athens' ) < 2 ;


CREATE ASSERTION CXD CHECK
( UNIQUE ( SELECT CITY FROM S WHERE CITY = 'Athens' ) ) ;

e. CONSTRAINT CXE COUNT ( S WHERE CITY = 'London' ) > 0 ;


CREATE ASSERTION CXE CHECK
( EXISTS ( SELECT * FROM S WHERE CITY = 'London' ) ) ;

f. CONSTRAINT CXF COUNT ( P WHERE COLOR = 'Red'


AND WEIGHT < 50.0 ) > 0 ;
CREATE ASSERTION CXF CHECK
( EXISTS ( SELECT * FROM P
WHERE COLOR = 'Red'
AND
WEIGHT < 50.0 ) ) ;

g. CONSTRAINT CXG CASE


WHEN IS_EMPTY ( S ) THEN TRUE
ELSE AVG ( S , STATUS ) > 10
END CASE ;
CREATE ASSERTION CXG CHECK
( CASE
WHEN NOT EXISTS ( SELECT * FROM S ) THEN TRUE
ELSE ( SELECT AVG ( STATUS ) FROM S ) > 10
END ) ;

h. CONSTRAINT CXH
CASE
WHEN IS_EMPTY ( SP ) THEN TRUE
ELSE IS_EMPTY ( SP WHERE QTY > 2 * AVG ( SP , QTY ) )
END CASE ;
CREATE ASSERTION CXH CHECK
( CASE
WHEN NOT EXISTS ( SELECT * FROM SP ) THEN TRUE
ELSE NOT EXISTS ( SELECT * FROM SP
WHERE QTY > 2 *

431

8
( SELECT AVG ( QTY )
FROM SP ) )
END ) ;

i. CONSTRAINT CXI CASE


WHEN COUNT ( S ) < 2 THEN TRUE
ELSE IS_EMPTY ( JOIN
{ ( S WHERE STATUS = MAX ( S { STATUS } ) ) { CITY } ,
( S WHERE STATUS = MIN ( S { STATUS } ) ) { CITY } } )
END CASE ;
CREATE ASSERTION CXI CHECK ( CASE
WHEN ( SELECT COUNT ( * ) FROM S ) < 2 THEN TRUE
ELSE NOT EXISTS
( SELECT * FROM S AS X , S AS Y
WHERE X.STATUS = ( SELECT MAX ( STATUS ) FROM S )
AND
Y.STATUS = ( SELECT MIN ( STATUS ) FROM S )
AND
X.CITY = Y.CITY )
END ) ;

j. CONSTRAINT CXJ P { CITY } S { CITY } ;


CREATE ASSERTION CXJ CHECK ( NOT EXISTS
( SELECT * FROM P
WHERE NOT EXISTS
( SELECT * FROM S
WHERE S.CITY = P.CITY ) ) ) ;

k. CONSTRAINT CXK IS_EMPTY (


( ( EXTEND P ADD ( (!!SP) JOIN S ) { CITY } AS SC )
WHERE TUPLE { CITY CITY } SC ) ) ;
CREATE ASSERTION CXK CHECK ( NOT EXISTS
( SELECT * FROM P
WHERE NOT EXISTS
( SELECT * FROM S
WHERE S.CITY = P.CITY
AND
EXISTS
( SELECT * FROM SP
WHERE S.SNO = SP.SNO
AND
P.PNO = SP.PNO ) ) ) ) ;

l. CONSTRAINT CXL
COUNT ( ( ( S WHERE CITY = 'London' ) JOIN SP ) { PNO } ) >
COUNT ( ( ( S WHERE CITY = 'Paris' ) JOIN SP ) { PNO } ) ;
CREATE ASSERTION CXL CHECK (
( SELECT COUNT ( DISTINCT PNO ) FROM S , SP
WHERE S.SNO = SP.SNO
AND
S.CITY = 'London' ) >
( SELECT COUNT ( DISTINCT PNO ) FROM S , SP

432

.
WHERE S.SNO = SP.SNO
AND
S.CITY = 'Paris' ) ) ;

m. CONSTRAINT CXM
SUM ( ( ( S WHERE CITY = 'London' ) JOIN SP ) , QTY ) >
SUM ( ( ( S WHERE CITY = 'Paris' ) JOIN SP ) , QTY ) ;
CREATE ASSERTION CXM CHECK (
( SELECT COALESCE ( SUM ( QTY ) , 1 ) FROM S , SP
WHERE S.SNO = SP.SNO
AND
S.CITY = 'London' ) >
( SELECT COALESCE ( SUM ( QTY ) , 0 ) FROM S , SP
WHERE S.SNO = SP.SNO
AND
S.CITY = 'Paris' ) ) ;

n. CONSTRAINT CXN IS_EMPTY


( ( SP JOIN P ) WHERE QTY * WEIGHT > 20000.0 ) ;
CREATE ASSERTION CXN CHECK
( NOT EXISTS ( SELECT * FROM SP NATURAL JOIN P
WHERE QTY * WEIGHT > 20000.0 ) ) ;

8.22. ,
(
-).
8.23. ,
- SC SNO CITY SNO CITY. , S1 .
, - SC
n-10 S1, n ( , )!
8.24.
( ):
DELETE S WHERE SNO = x , DELETE SP WHERE SNO = x ;

8.25. (
SQL, Tutorial D ; , ).
,
- , :
a. CONSTRAINT CXA

IS_EMPTY ( ( ( S WHERE CITY = Athens ) { SNO } ) JOIN S )


WHERE CITY Athens
AND CITY London
AND CITY Paris )

433

9
AND IS_EMPTY ( ( ( S WHERE CITY = London ) { SNO } ) JOIN S )
WHERE CITY London
AND CITY Paris ) ;

b. CONSTRAINT CXB IS_EMPTY


( P WHERE SUM ( !!SP , QTY ) > SUM ( !!SP , QTY ) ) ;

c. CONSTRAINT CXC IS_EMPTY

( S WHERE SUM ( !!SP , QTY ) < 0.5 * SUM ( !!SP , QTY ) ) ;

, , , .
8.26. . .
8.27. .
8.28. SQL , .
; , Databases, Types, and the Relational Model: The Third
Manifesto, (. D).
, , SQL , ! ,
. -
( , , 1000).
8.29. , , Tutori
al D -
.
8.30. . 12. .

9
9.1.
VAR NON_COLOCATED VIRTUAL
( S { SNO } JOIN P { PNO } ) MINUS ( S JOIN P ) { SNO , PNO }
KEY { SNO , PNO } ;
CREATE VIEW NON_COLOCATED AS
SELECT SNO , PNO
FROM S , P
WHERE S.CITY <> P.CITY
/* UNIQUE ( SNO , PNO ) */ ;

434

9.2. FRON (
), :
SELECT DISTINCT LSSP.STATUS , LSSP.QTY
FROM ( SELECT S.SNO , S.SNAME , S.STATUS , SP.PNO , SP.QTY
FROM S NATURAL JOIN SP
WHERE S.CITY = London ) AS LSSP
WHERE LSSP.PNO IN
( SELECT P.PNO
FROM P
WHERE P.CITY <> London )

:
SELECT
FROM
WHERE
AND
(

DISTINCT STATUS , QTY


S NATURAL JOIN SP
CITY = London
PNO IN
SELECT PNO
FROM P
WHERE CITY <> London )

9.3. {SNO,PNO}. : SNO , SNAME,


STATUS, PNO
QTY.
9.4.
a. ( ( P WHERE WEIGHT > 14.0 ) { PNO , WEIGHT , COLOR } )
WHERE COLOR = Green

b. ( EXTEND ( ( P WHERE WEIGHT > 14.0 ) { PNO , WEIGHT , COLOR } )


ADD ( WEIGHT + 5.3 AS WTP ) ) { PNO , WTP }

c. INSERT ( ( P WHERE WEIGHT > 14.0 ) { PNO , WEIGHT , COLOR } )


RELATION { TUPLE { PNO P99, WEIGHT 12.0, COLOR Purple } } ;

, INSERT , , -.
, ,
.
(d) (e) .
d. DELETE ( ( P WHERE WEIGHT > 14.0 ) { PNO , WEIGHT , COLOR } )
WHERE WEIGHT < 9.0 ;

e. UPDATE ( ( P WHERE WEIGHT > 14.0 ) { PNO , WEIGHT , COLOR } )


WHERE WEIGHT = 18.0 : { COLOR := White } ;

435

: ?
9.5.
a. ( ( ( EXTEND P ADD ( WEIGHT * 454 AS WT ) ) WHERE WT > 6356.0 )
{ PNO , WT , COLOR } ) WHERE COLOR = Green

b. ( EXTEND
( ( ( EXTEND P ADD ( WEIGHT * 454 AS WT ) ) WHERE WT > 6356.0 )
{ PNO , WT , COLOR } ) ADD ( WT + 5.3 AS WTP ) ) { PNO , WTP }

c. INSERT
( ( ( EXTEND P ADD ( WEIGHT * 454 AS WT ) ) WHERE WT > 6356.0 )
{ PNO , WT , COLOR } )
RELATION { TUPLE { PNO P99 , WT 12.0 , COLOR Purple } } ;

d. DELETE
( ( ( EXTEND P ADD ( WEIGHT * 454 AS WT ) ) WHERE WT > 6356.0 )
{ PNO , WT , COLOR } ) WHERE WT < 9.0 ;

e. UPDATE
( ( ( EXTEND P ADD ( WEIGHT * 454 AS WT ) ) WHERE WT > 6356.0 )
{ PNO , WT , COLOR } ) WHERE WT = 18.0 : { COL := White } ;

9.6. SQL-
9.4:
CREATE VIEW HEAVYWEIGHT AS
SELECT PNO , WEIGHT AS WT , COLOR AS COL
FROM P
WHERE WEIGHT > 14.0 ;

ae SQL-
TutorialD, , :
a. SELECT *
FROM HEAVYWEIGHT
WHERE COL = Green
SELECT *
FROM ( SELECT PNO , WEIGHT AS WT , COLOR AS COL
FROM P
WHERE WEIGHT > 14.0 ) AS POINTLESS
WHERE COL = Green

( ).
b. SELECT PNO , WT + 5.3 AS WTP
FROM

HEAVYWEIGHT

SELECT PNO , WT + 5.3 AS WTP

436

.
FROM ( SELECT PNO , WEIGHT AS WT , COLOR AS COL
FROM P
WHERE WEIGHT > 14.0 ) AS POINTLESS

c. INSERT INTO HEAVYWEIGHT ( PNO , WT , COL )


VALUES ( P99 , 12.0 , Purple ) ;
INSERT INTO ( SELECT
FROM
WHERE
VALUES

PNO , WEIGHT AS WT , COLOR AS COL

WEIGHT > 14.0 ) ( PNO , WT , COL )


( P99 , 12.0 , Purple ) ;

, INSERT SQL (
); , SQL
. INSERT
, , .
d. DELETE FROM ( SELECT PNO , WEIGHT AS WT , COLOR AS COL
FROM
WHERE WEIGHT > 14.0 ) WHERE WT < 9.0 ;


SQL,
:
DELETE FROM P WHERE WEIGHT > 14.0 AND WEIGHT < 9.0 ;

(, , . , ?)
e. UPDATE ( SELECT PNO , WEIGHT AS WT , COLOR AS COL
FROM P
WHERE WEIGHT > 14.0 )
SET
COL = White
WHERE WT = 18.0 ;

:
UPDATE P
SET
COLOR = White
WHERE WEIGHT = 18.0 AND WEIGHT > 14.0 ;

SQL- 9.5 .
9.7. , HEAVYWEIGHT ( ), .
9.8. {PNO}. 9.4: PNO
,

437

, COL WT ( WT 14). 9.5: PNO , , COL


WT ( WT 6356).
-: 9.4 , - P,
, 14. 9.5 .
9.9. :
-, , ,
-. , ,
, , -.
- , ( ), ( ).
(
SQL-, ). , -,
( )
.
, , , ,
, , .
,
, .
,
,
.
: , ,
?
9.10. : - {SNO,SNAME},
{SNO,STATUS} {SNO,CITY} , ,
(?).

438

-,
.
9.11. . -,
,
:
/* - */
VAR LS BASE RELATION
{ SNO CHAR , SNAME CHAR , STATUS INTEGER , CITY CHAR }
KEY { SNO } ;
VAR NLS BASE RELATION
{ SNO CHAR , SNAME CHAR , STATUS INTEGER , CITY CHAR }
KEY { SNO } ;
/* - */
INSERT LS ( S WHERE CITY = London ) ;
INSERT NLS ( S WHERE CITY London ) ;
/* - */
DROP VAR S ;
/* */
VAR S VIRTUAL ( LS D_UNION NLS ) ;

- -
SP, -S.
, ,
S;1
( ),
- :
VAR SS BASE RELATION { SNO CHAR } KEY { SNO } ;

(,
- S):
INSERT SS S { SNO } ;

,
,
-, . . 9.9.

439


- LS NLS:
FOREIGN KEY { SNO } REFERENCES SS

, {SNO} - SP, SS
S.
9.12. a. ,
,
. b. , (a). c.
.
9.13. . .
SQL . (
CREATE TABLE AS . 1.16 1, , REFRESH.)
9.14. . , , . ,
, , .
.1
: ,
.
9.15. (b) (a):
VAR SSP VIRTUAL ( S JOIN SP )
KEY { SNO , PNO } ;
VAR XSS VIRTUAL ( S NOT MATCHING SP )
KEY { SNO } ;

(a) (b):
VAR S VIRTUAL ( XSS D_UNION SSP { ALL BUT PNO , QTY } )
KEY { SNO } ;
VAR SP VIRTUAL ( SSP { SNO , PNO , QTY } )
KEY { SNO , PNO } ;
1

, , , ,
.

440

:
CONSTRAINT DESIGN_A IS_EMPTY ( SP { SNO } MINUS S { SNO } ) ;
CONSTRAINT DESIGN_B IS_EMPTY ( SSP { SNO } JOIN XSS { SNO } ) ;


. (a) , -
. (b)
- SSP ( ), , . , (b), -
,
, -
. , (b), , ,
D, (.
B).
, , , {SNO}
- S, DESIGN_A
.
9.16. (
6 , .)
, .
,
.
,
, .

10
10.1. . 4.10 4.
10.2. , , OR
AND ,
p AND q NOT ( NOT ( p ) OR NOT ( q ) )

( .) ,
OR AND.

441

10

, p. c(p) . :
c(p)
c(p)
c(p)
c(p)

p OR NOT ( p )
p AND NOT ( p )
p
NOT ( p )

/*
/*
/*
/*

TRUE
FALSE

NOT

*/
*/
*/
*/

p q.
c(p,q) . :
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c

(p,q)
(p,q)
(p,q)
(p,q)
(p,q)
(p,q)
(p,q)
(p,q)
(p,q)
(p,q)
(p,q)
(p,q)
(p,q)
(p,q)
(p,q)
(p,q)

p OR NOT ( p ) OR q OR NOT ( q )
p AND NOT ( p ) AND q AND NOT ( q )
p
NOT ( p )
q
NOT ( q )
p OR q
p AND q
p OR NOT ( q )
p AND NOT ( q )
NOT ( p ) OR q
NOT ( p ) AND q
NOT ( p ) OR NOT ( q )
NOT ( p ) AND NOT ( q )
( NOT ( p ) OR q ) AND ( NOT ( q ) OR p )
( NOT ( p ) AND q ) OR ( NOT ( q ) AND p )

,
, .
(b) . : NOR NAND, ,
, , ( ),
, | ( ).
:
NOR

T F

NAND

T F

T
F

F F
F T

T
F

F T
T T

, pq (p NOR q) NOT
(p OR q), p|q (p NAND q) NOT (p AND q).
NOR (NAND ). ,
- ( , -

442

). ,
NOT, OR AND:
p p
( p q ) ( p q )
( p p ) ( q q )

NOT ( p )
p OR q
p AND q

, p AND q:
p

p p

q q

T
T
F
F

T
F
T
F

F
F
T
T

F
T
F
T

(p p )

(q q )
T
F
F
F

, (pp)(qq) p AND q, ,
AND:
p

p AND q

T
T
F
F

T
F
T
F

T
F
F
F

, NOT, OR AND, .
10.3. , , .
10.4. , .
10.5. , . ;
(
, ).
10.6. , , . , ()
, - P , () .
10.7. SQL-
:
1. ,
.

443

10
SELECT
FROM
WHERE
AND

SX.SNO AS SA , SY.SNO AS SB
S AS SX , S AS SY
SX.CITY = SY.CITY
SX.SNO < SY.SNO

2. ,
.
SELECT
FROM
WHERE
(

DISTINCT SX.SNAME
S AS SX
EXISTS
SELECT *
FROM SP AS SPX
WHERE EXISTS
( SELECT *
FROM P AS PX
WHERE SX.SNO = SPX.SNO
AND
SPX.PNO = PX.PNO
AND
PX.COLOR = Red ) )

3. ,
, S2.
SELECT
FROM
WHERE
(

DISTINCT SX.SNAME
S AS SX
EXISTS
SELECT *
FROM SP AS SPX
WHERE EXISTS
( SELECT *
FROM SP AS SPY
WHERE SX.SNO = SPX.SNO
AND
SPX.PNO = SPY.PNO
AND
SPY.SNO = S2 ) )

4. , P2.
SELECT DISTINCT SX.SNAME
FROM S AS SX
WHERE NOT EXISTS
( SELECT *
FROM SP AS SPX
WHERE SPX.SNO = SX.SNO
AND
SPX.PNO = P2 )

5. , .
SELECT SPX.* , PX.WEIGHT * SPX.QTY AS SHIPWT
FROM P AS PX , SP AS SPX
WHERE PX.PNO = SPX.PNO

444

6. .
SELECT PX.PNO ,
( SELECT COALESCE ( SUM ( ALL SPX.QTY ) , 0 )
FROM SP AS SPX
WHERE SPX.PNO = PX.PNO ) AS TOTQ
FROM P AS PX

7. ,
.
SELECT DISTINCT PX.CITY
FROM P AS PX
WHERE ( SELECT COUNT ( * )
FROM P AS PY
WHERE PY.CITY = PX.CITY
AND
PY.COLOR = Red ) > 5

10.8. , AND ; OR .
p

p AND q

T
T
T
T
F
F
F
F

T
T
F
F
T
T
F
F

T
F
T
F
T
F
T
F

T
T
F
F
F
F
F
F

(p AND q) AND r
T
F
F
F
F
F
F
F

(q AND r)
T
F
F
F
T
F
F
F

p AND (q AND r)
T
F
F
F
F
F
F
F

10.9. a. (, x
q TRUE, EXISTS x (q) FALSE). b. (, x q FALSE, FORALL x (q) TRUE). c. . d. .
e. (, x , FORALL x (p(x)) TRUE, EXISTS x (p(x)) FALSE,
TRUE FALSE FALSE). f. (, x
, EXISTS x (TRUE) FALSE).
g. (, x , FORALL x (FALSE) TRUE). h. . i. (,
, , ,
, ). j. (,
, 24 , ,
, 24 , ,
24 ). k. . , (!) , ,
, , 6.

445

10

10.10. a. . b. . c. . d. . e. (, , y x, , ,
x, y). f. .
10.11. a. . b. .
10.12. , - . 6:
6.12.
TABLE_DEE TABLE_DUM:
{ } WHERE TRUE
{ } WHERE FALSE


- S :
{ } WHERE EXISTS ( SX )

,
r{ALL BUT ...} TutorialD,
, .
6.15. ,
D_UNION Tutorial D, , .
10.12 (). 7:
7.1.
d. { PX } WHERE SUM ( SPX WHERE SPX.PNO = PX.PNO , QTY ) < 500
e. { PX } WHERE EXISTS ( SX WHERE SX.CITY = PX.CITY )
j. { PX , COUNT ( SPX WHERE SPX.PNO = PX.PNO ) AS SCT }
7.8. SP GROUP ({}
AS X) TutorialD :
{ SPX , { } AS X }

7.10. (R4 WHERE


TUPLE {PNO P2} PNO_REL){SNO} TutorialD :
RANGEVAR RX RANGES OVER R4 ,
RANGEVAR RY RANGES OVER RX.PNO_REL ;
RX.SNO WHERE EXISTS ( RY WHERE RY.PNO = P2 )

, RY RX ( , ,
).

446

((R4 WHERE SNO = S2) UNGROUP (PNO_


REL)){PNO}:
RY.PNO WHERE RX.SNO = S2

7.11. { SX , { SPX.PNO WHERE SPX.SNO = SX.SNO } AS PNO_REL }


7.12. INSERT, DELETE UPDATE ( ),
, (
, - RVA-,
, ).
, . .
10.12 (). 8: .
10.12 (). 9: .
10.13. , , , , , , - (
). , , , :
RANGEVAR CX RANGES OVER { SX.CITY } , { PX.CITY } ;
{ CX } WHERE TRUE

, CX
SX PX, , ,
.
10.14. SQL 1 , () SQL-
, , ,
() SQL-
SQL-.
, , SQL RENAME AS SELECT.2 -

, ?

: SQL- T RENAME (A AS B) ( !) SQL- SELECT A AS


B, X, Y, ..., Z FROM T ( X, Y, ..., Z T A,
, SQL- , ).

447

10

, , ,
, , , . ,
, SQL , 6.
SQL-, :

SQL

R WHERE p

SELECT * FROM R WHERE p

R { A , B , ... , C }

SELECT DISTINCT A , B , ... , C FROM R

R1 TIMES R2

SELECT * FROM R1 , R2

R1 UNION R2

SELECT * FROM R1
UNION CORRESPONDING
SELECT * FROM R2

R1 MINUS R2

SELECT * FROM R1
EXCEPT CORRESPONDING
SELECT * FROM R2

, () R1 R2 SQL- , ()
SQL- , .1 , SQL
.

SQL (
SELECT). , TABLE_DEE TABLE_
DUM, , , SQL - 100 .

10.15. TP DC , ,
. :
IF TP THEN DC

:
IF NOT ( DC ) THEN NOT ( TP )
1

, SQL
.

448


NOT OR, ,
. ( , . . 11.)
10.16. , , . A Remark on Prenex Normal Form (. D) .

11
11.1. SQL
SX , PX PY,
PX.CITY PY.CITY, SX . ( ?)
SELECT
FROM
WHERE
(

SX.*
S AS SX
NOT EXISTS
SELECT *
FROM P AS PX
WHERE EXISTS
( SELECT *
FROM P AS PY
WHERE PX.CITY <> PY.CITY
AND
EXISTS
( SELECT *
FROM SP AS SPX
WHERE SPX.SNO = SX.SNO
AND
SPX.PNO = PX.PNO )
AND
EXISTS
( SELECT *
FROM SP AS SPX
WHERE SPX.SNO = SX.SNO
AND
SPX.PNO = PY.PNO ) ) )

SQL- () GROUP BY HAVING, (b) GROUP


BY HAVING, :
, N N > 3.
, N N < 4.
GROUP BY HAVING:
SELECT SNO
FROM SP

449

11
GROUP BY SNO
HAVING COUNT ( * ) > 3
SELECT
FROM
GROUP
HAVING
UNION
SELECT
FROM
WHERE
(

SNO
SP
BY SNO
COUNT ( * ) < 4
CORRESPONDING
SNO
S
SNO NOT IN
SELECT SNO
FROM SP )

GROUP BY HAVING:
SELECT SNO
FROM S
WHERE ( SELECT COUNT ( * )
FROM SP
WHERE SP.SNO = S.SNO ) > 3
SELECT SNO
FROM S
WHERE ( SELECT COUNT ( * )
FROM SP
WHERE SP.SNO = S.SNO ) < 4

, . , GROUP BY
HAVING. , , ,
GROUP-BY/HAVING, , .
GROUP BY HAVING, , .
11.2. .
11.3. .
11.4. , -
, , , ,

,
:
r WHERE bx1 AND bx2 ( r WHERE bx1 ) JOIN ( r WHERE bx2 )
r WHERE bx1 OR bx2

( r WHERE bx1 ) UNION ( r WHERE bx2 )

r WHERE NOT ( bx )

r MINUS ( r WHERE bx )

( 6)
.

450

12
12.1.
A NATURAL JOIN B :
A INTERSECT B :
TABLE A NATURAL JOIN TABLE B :
TABLE A INTERSECT TABLE B :
SELECT * FROM A NATURAL JOIN SELECT * FROM B :
SELECT * FROM A INTERSECT SELECT * FROM B :
( SELECT * FROM A ) NATURAL JOIN ( SELECT * FROM B ) :
( SELECT * FROM A ) INTERSECT ( SELECT * FROM B ) :
( TABLE A ) NATURAL JOIN ( TABLE B ) :
( TABLE A ) INTERSECT ( TABLE B ) :
( TABLE A ) AS AA NATURAL JOIN ( TABLE B ) AS BB :
( TABLE A ) AS AA INTERSECT ( TABLE B ) AS BB :
( ( TABLE A ) AS AA ) NATURAL JOIN ( (TABLE B) AS BB ) :
( ( TABLE A ) AS AA ) INTERSECT ( ( TABLE B ) AS BB ) :

, ( ). , SQL-
INTERSECT JOIN.
12.2. .
12.3. .

B
B.1. .
B.2. ( ,
) - SP :
{
{
{
{
{
{
{
{

SNO
SNO
SNO
SNO
SNO
SNO
SNO
SNO

,
,
,
,
,
,
,
,

PNO
PNO
PNO
PNO
PNO
PNO
PNO
PNO

,
,
,
,
,
,
,
,

{ SNO , PNO }
{ SNO , PNO }

QTY
QTY
QTY
QTY
QTY
QTY
QTY
QTY

}
}
}
}
}
}
}
}

{
{
{
{
{
{
{
{

SNO
SNO
PNO
SNO
SNO
PNO
QTY
}

,
,
,
,
}
}
}

PNO
PNO
QTY
QTY

, QTY }
}
}
}

{ SNO , PNO , QTY }


{ SNO , PNO }

451

B
{
{
{
{
{
{

SNO
SNO
SNO
SNO
SNO
SNO

,
,
,
,
,
,

PNO
PNO
PNO
PNO
PNO
PNO

}
}
}
}
}
}

{
{
{
{
{
{

PNO
SNO
SNO
PNO
QTY
}

{
{
{
{

PNO
PNO
PNO
PNO

,
,
,
,

QTY
QTY
QTY
QTY

}
}
}
}

{
{
{
{

PNO , QTY }
PNO }
QTY }
}

{
{
{
{

SNO
SNO
SNO
SNO

,
,
,
,

QTY
QTY
QTY
QTY

}
}
}
}

{
{
{
{

SNO , QTY }
SNO }
QTY }
}

, QTY }
, QTY }
}
}
}

{ SNO }
{ SNO }

{ SNO }
{ }

{ PNO }
{ PNO }

{ PNO }
{ }

{ QTY }
{ QTY }

{ QTY }
{ }

{ }

{ }

B.3. ( A, B , {A,B} ). : , ,
, (,
). ,
.
B.4. ( ).
B.5. : R{A,B,C} A B, R R1 {A,B} R2
{A,C}. ,
.
, R .
(a,b,c) R. (a,b) R1 (a,c) R2, (a,b,c) R1 JOIN R2.
, R ( ,
). (a,b,c) R1 JOIN R2.

452

, (a,b) R1 (a,c) R2. , (a,c) R2, (a,b,c) R b. (a,b) R1.


, (a,b) R1 (a,b) R1; b = b, A B. (a,b,c) R.
, R{A,B,C} {A,B} {A,C}, R
A B. . ,
; . ( , B.20.
- SPJ .B.5 .)
B.6. . .
B.7. . 5.
B.8. . . . , - S {CITY,STATUS} {STATUS}. , , S *{AB,AC},
A {CITY,STATUS}, B {STATUS}, C {SNO,SNAME}.
B.9. ( , ) A B, A B R. n 2n
, A B 2n , , 22n. , - 5 1024.
B.10. -
R. t, R, , , ( , 0-). B ,
A B A
R; ( ). , A , A B ,
R B (
A, , ). B
R , R ,
R .
B.11. , -
R D, P, S, L, T C (
). R :
{
{
{
{

L
D
D
D

}
{
, P , C } {
, P , T } {
, P , S } {

D
L
L
L

,
,
,
,

P
T
C
C

, C , T }
}
}
}

453

-,
, :
SCHEDULE { L
KEY
KEY
KEY

,
{
{
{

D
L
D
D

, P , C , T }
}
, P , C }
, P , T }

STUDYING { S , L }
KEY { S , L }

STUDYING 6; SCHEDULE 5, 6.
{D,P,T} {L,C}; ,
SCHEDULE 6- {L,D}, {L,P},
{L,C} {L,T} ( , , ), {D,P,C} {L,T} {D,P,T} {L,C}. , , .
B.12. , , ; ,
5
4. 5 -, , . , ()
, , .
B.13. , . - ( ),
, , , ,
. (, , ,
. , ,
.) ,
, ( , ,
). ,
,
( . A); , , ,
( !) - ,
.
:
, .
?
, ;
, , -

454

, , .
, . , , , .
Composite
Keys (. D). : ,
, (
) .
. -
, ,
5, , , ,
-,
5. . , -
,
.
B.14.
X Y , , X, , ,
Y, (
). X Y
.
,
- IXAYWA (if X attends, Y will
attend X , Y ), (. , INV invitee ). , X Y - RVA-.
IXAY WA

INV

INV

INV

INV

Fay

Hal

Am y

Bob
Cal

INV

INV
Guy

INV

INV

Don
Eve

Hal

INV

INV
Joe

...

...

Bob
Amy

. , . , X , Y , , X Y -

455

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

, ( ) ,
, , .
, , , . -, B ,
X Y , X Y
X X Y Y.
, X Y, X , Y .
:
X,
.
Y,
.
, , X Y
Y , Y X ( , X X , , , , ). , .
, - IXAYWA , ,
{X} .
An In
troduction to Database Systems (. D).
B.15. :
EMP { ENO , ENAME , SALARY }
KEY { ENO }
PGMR { ENO , LANG }
KEY { ENO }
FOREIGN KEY { ENO } REFERENCES EMP

EMP (
EMP ). - -

456

PGMR ( PGMR
). , EMP PGMR
( , , ) ( ).
,
- ,
{ENO,LANG}.
B.16. R {A,B} {A,C} R1 R2 , (a,b1,c1) R (a,b2,c2) R. (a,b1) R1
(a,b2) R1, (a,c1) R2 (a,c2) R2; , (a,b1,c2) J
(a,b2,c1) J, J = R1 JOIN R2. R *{AB,AC},
J = R; , (a,b1,c2) R (a,b2,c1) R.
, ,
B C; , R A B
, A C. , :
A B | C

B.17. C R, A, B.
, R A B, *{AB,AC}. (. B.16),
R *{AB,AC}, A B
A C. , A B A B.
B.18. (. B.16).
B.19. B.17 , K A,
K A. K , K A, .
B.20. C , -
R{A,B} , (a,b) R
, a, b C. R R{A} R{B}, K{A,B} , , :
{ } A | B

, , ,
-, . , R 4. , (. B.24).
B.21. - :

457

REP
{ REPNO , ... } KEY { REPNO }
AREA
{ AREANO , ... } KEY { AREANO }
PRODUCT { PRODNO , ... } KEY { PRODNO }

, ()
() -:
RA { REPNO , AREANO } KEY { REPNO , AREANO }
RP { REPNO , PRODNO } KEY { REPNO , PRODNO }

.
-
AP { AREANO , PRODNO } KEY { AREANO , PRODNO }

:
CONSTRAINT C1 AP = AREA { AREANO } JOIN PRODUCT { PRODNO } ;

, , AP 4. , AP , -. ,
:
AP { AREANO } = AREA { AREANO }
AP { PRODNO } = PRODUCT { PRODNO }

, -
- AP .

. , {AREANO,
PRODNO} REPNO, -
APR { AREANO , PRODNO , REPNO } KEY { AREANO , PRODNO }

( )
{ AREANO , PRODNO } { REPNO }

( {AREANO,PRODNO} .) , , - RA,
RP AP ,
APR; , . C1
C2:
CONSTRAINT C2 APR { AREANO , PRODNO } =
AREA { AREANO } JOIN PRODUCT { PRODNO } ;


( ).

458

, ,
, ,
C3 -
APR:
{ REPNO } { AREANO } | { PRODNO }

( , - APR 4.) .
, -
REP, AREA, PRODUCT APR C2 C3:
CONSTRAINT C2 APR { AREANO , PRODNO } =
AREA { AREANO } JOIN PRODUCT { PRODNO } ;
CONSTRAINT C3 APR = APR { REPNO , AREANO } JOIN
APR { REPNO , PRODNO } ;

, ( , , ,
),
. ,
, (
- APR , 4).

,
RVA-. , ?

B.22.
CONSTRAINT SPJ_JD SPJ = JOIN { SPJ { SNO , PNO } ,
SPJ { PNO , JNO } ,
SPJ { JNO , SNO } ;

B.23. . :
REP
{ REPNO , ... } KEY { REPNO }
AREA
{ AREANO , ... } KEY { AREANO }
PRODUCT { PRODNO , ... } KEY { PRODNO }
RA { REPNO , AREANO } KEY { REPNO , AREANO }
AP { AREANO , PRODNO } KEY { AREANO , PRODNO }
PR { PRODNO , REPNO } KEY { PRODNO , REPNO }

459

, ,
RA, AP PR :
CONSTRAINT
(
(
(

NO_TRAP
RA JOIN AP JOIN PR ) { REPNO , AREANO } = RA AND
RA JOIN AP JOIN PR ) { AREANO , PRODNO } = AP AND
RA JOIN AP JOIN PR ) { PRODNO , REPNO } = PR ;

B.21, , RVA-

B.24. a. . b. . c. , .
: - USA {COUNTRY,
STATE} (STATE () COUNTRY ()),
COUNTRY U.S. -
{} {COUNTRY}, , , -
(
).
B.25. , . ?
B.26.
-, , B:
, -
, , . , , , , ,
. :
-, , , [ ].


, . ,
. , . :
SQL, , , . , , , ,
- .
1. Surajit Chaudhuri and Gerhard Weikum Rethinking Database System Architecture: Towards a Self-tuning RISC-style Database System
Proc. 26th Int. Conf. on Very Large Data Bases, Cairo, Egypt (September 2000).
, ( ):
SQL , , SQL-
, ,
. :
SQL . ,
, SQL.
( ) . , - , , , . SQL ( SQL-92),
( )

461

, null-, . ., .
SQL ,

.

SQL, SQL. , , .
,
, , ,
. ,
SQL

.

2. E. F. Codd Derivability, Redundancy, and Consistency of Relations


Stored in Large Data Banks IBM Research Report RJ599 (August
19th, 1969); A Relational Model of Data for Large Shared Data Banks,
CACM 13, No. 6 (June 1970).
1969 1970 ,
( ,
1969 , , 1970 ). 1970 Milestones of Research, CACM 26, No. 1 (January
1982) . , , ,
1969 . , ,
, , .
3. E. F. Codd Relational Completeness of Data Base Sublanguages
Randall J. Rustin (ed.), Data Base Systems, Courant Com
puter Science Symposia Series 6. Englewood Cliffs, N.J.: Prentice Hall
(1972).
. , .
4. E. F. Codd and C. J. Date Much Ado about Nothing
C.J.Date, Relational Database Writings 19911994. Reading, Mass.:
Addison-Wesley (1995).
, , null-
,
(, , null- ).

462

. :
, (). : ( ),
.
5. Hugh Darwen The Role of Functional Dependence in Query Decomposition C. J. Date and Hugh Darwen, Relational Database
Writings 19891991. Reading, Mass.: Addison-Wesley (1992).
, (), r, , ,
-, . , , r, ,
4.
6. Hugh Darwen What a Database Really Is: Predicates and Propositions C. J. Date, Relational Database Writings 19941997.
Reading, Mass.: Addison-Wesley (1998).
-
.
7. Hugh Darwen How to Handle Missing Information Without Using
Nulls ( ), http://www.thethirdmanifesto.com
(May 9th, 2003; revised May 16th, 2005).
, ( , null).
8. C. J. Date Fifty Ways to Quote Your Query, http://www.dbpd.com
(July 1998).
6.
9. C. J. Date Composite Keys C. J. Date and Hugh Darwen,
Relational Database Writings 1989-1991. Reading, Mass.: AddisonWesley (1992).
B.
10. C. J. Date An Introduction to Database Systems (8th edition). Boston,
Mass.: Addison-Wesley (2004).

. SQL SQL:1999
, SQL:2003; , -
( REF, ..), ,

463

. : [32], [41] [42].


11. C. J. Date The Relational Database Dictionary, Extended Edition.
Berkeley, Calif.: Apress (2008).

.
12. C. J. Date Double Trouble, Double Trouble, Date on Data
base: Writings 20002006. Berkeley, Calif.: Apress (2006).
, . 4 , .
13. C. J. Date What First Normal Form Really Means, Date
on Database: Writings 20002006. Berkeley, Calif.: Apress (2006).
. , .
(. 2), ( )
.
14. C. J. Date A Sweet Disorder, Date on Database: Writings
20002006. Berkeley, Calif.: Apress (2006).
, SQL .
,
, .
15. C. J. Date On the Notion of Logical Difference, On the Logical Difference Between Model and Implementation On the Logical Differences Between Types, Values, and Variables, Date on
Database: Writings 20002006. Berkeley, Calif.: Apress (2006).
( ,
,
, ).
16. C. J. Date Two Remarks on SQLs UNION Date on Data
base: Writings 20002006. Berkeley, Calif.: Apress (2006).
, SQL- UNION , ,
INTERSECT EXCEPT - () () .
17. C. J. Date A Cure for Madness Date on Database: Writings
20002006. Berkeley, Calif.: Apress (2006).

464

, SQL SELECT FROM (SELECT * FROM t WHERE p)


WHERE q SELECT FROM t WHERE p AND q , ,
SQL
.
18. C. J. Date Why Three- and Four-Valued Logic Dont Work
Date on Database: Writings 20002006. Berkeley, Calif.: Apress (2006).
, null SQL . SQL , , , , .
19. C. J. Date The Logic of View Updating Logic and Data
bases: The Roots of Relational Theory. Victoria, BC: Trafford Publishing (2007). . http://www.trafford.com/07-0690.
,
[28] , ,
.
20. C. J. Date The Closed World Assumption Logic and Data
bases: The Roots of Relational Theory. Victoria, BC: Trafford Publishing (2007). . http://www.trafford.com/07-0690.
,
, . ; , , ( , , , , ).
, -
( ) ,
null-.
21. C. J. Date The Theory of Bags: An Investigative Tutorial
Logic and Databases: The Roots of Relational Theory. Victoria, BC:
Trafford Publishing (2007). . http://www.trafford.com/07-0690.
, , , ,
.
22. C. J. Date Inclusion Dependencies and Foreign Keys ( ).
Rethinking
Foreign Keys ( );

465

, ,
. . ( 2 , , ,
.)
23. C. J. Date Image Relations ( ).
-
(. 7).
24. C. J. Date Is SQLs Three-Valued Logic Truth Functionally Complete?
( ).
null- SQL.
25. C. J. Date A Remark on Prenex Normal Form ( ).
.
26. C. J. Date Go Faster! The TransRelational Approach to DBMS Imple
mentation ( ).
The TransRelational Model , A B. : A [10].
27. C. J. Date and Hugh Darwen A Guide to the SQL Standard (4th edition). Reading, Mass.: Addison-Wesley (1997).
SQL 1997
. (
- (. [28])),
SQL .
, ,

SQL ( , ),
.
28. C. J. Date and Hugh Darwen Databases, Types, and the Relational
Model: The Third Manifesto (3rd edition). Boston, Mass.: AddisonWesley (2006).
. , ,
, (
), ,
, TutorialD.

466

29. C. J. Date and Hugh Darwen Multiple Assignment Date


on Database: Writings 20002006. Berkeley, Calif.: Apress (2006).
. 8.
30. C. J. Date, Hugh Darwen, and Nikos A. Lorentzos Temporal Data and
the Relational Model. San Francisco, Calif.: Morgan Kaufmann (2003).
, ,
A.
31. C. J. Date and David McGoveran Why Relational DBMS Logic Must
Not Be Many-Valued, C. J. Date, Logic and Databases: The
Roots of Relational Theory. Victoria, BC: Trafford Publishing (2007).
See http://www.trafford.com/07-0690.
, (
, SQL).
32. Ramez Elmasri and Shamkant Navathe Fundamentals of Database
Systems (4th edition). Boston, Mass.: Addison-Wesley (2004).
33. Stphane Faroult with Peter Robson The Art of SQL. Sebastopol, Calif.: OReilly Media Inc. (2006).
,
SQL .
, , :
1.
2.
3.
4. SQL
5.
6. SQL
7.
8.
9.
10.
11.
12.

, , . , -

467

, ,

, ( ). ,
, , MIN , , , /, ( ,
). 1 (hint)
( ), : , , ,
. , , , , .. ,
, ,
, .
34. Patrick Hall, Peter Hitchcock, and Stephen Todd An Algebra of Re
lations for Machine Computation, Conf. Record of the 2nd ACM Sym
posium on Principles of Programming Languages, Palo Alto, Calif.
(January 1975).
, , , . Tutorial D, ,
, .
35. Jim Gray and Andreas Reuter Transaction Processing: Concepts and
Techniques. San Mateo, Calif.: Morgan Kaufmann (1993).
.
36. Lex de Haan and Toon Koppelaars Applied Mathematics for Database
Professionals. Berkeley, Calif.: Apress (2007).

( ), , 11,
SQL-. ,
(.
8). .
37. Wilfrid Hodges Logic. London, England: Penguin Books (1977).
.
38. International Organization for Standardization (ISO) Database Lang
uage SQL, Document ISO/IEC 9075:2003 (2003).
1

optimizer hint (, ,
), (). . .

468

SQL. , , ( , , ) ANSI.
39. Jim Melton and Alan R. Simon SQL:1999 Understanding Relational
Components; Jim Melton Advanced SQL:1999 Understanding Ob
ject-Relational and Other Advanced Features. San Francisco, Calif.:
Morgan Kaufmann (2002 and 2003, respectively).
,
SQL:1999 (
SQL:2003).
SQL.
40. Fabian Pascal Practical Issues in Database Management: A Reference
for the Thinking Practitioner. Boston, Mass.: Addison-Wesley (2000).

, . : , , ,
,
, .
41. Raghu Ramakrishnan and Johannes Gehrke Database Management
Systems (3rd edition). New York, N.Y.: McGraw-Hill (2003).
42. Avi Silberschatz, Henry F. Korth, and S. Sudarshan Database System
Concepts (5th edition). New York, N.Y.: McGraw-Hill (2005).
43. Robert R. Stoll Sets, Logic, and Axiomatic Theories. San Francisco,
Calif.: W. H. Freeman and Company (1961).

. ,
. .
Hodges [37].
44. Dave Voorhis Rel. http://db@builder.sourceforge.net/rel.html.
Tutori
alD.
45. Mosh M. Zloof Query-By-Example, Proc. NCC 44, Anaheim, Calif.
(May 1975). Montvale, N.J.: AFIPS Press (1977).
Query-By-Example (QBE) ,
, , .
QBE,
.

0-, 85
2VL, , 109
3VL, , 109
>, (), 130, 208
>>, (), 370
, , 251
, , 92
, , 92, 145
, , 91
-, 160
, , 441
, , 92
, , 92
|, , 441
, , 251

EVERY, 420

NO PAD, 73
NOT NULL, 113, 211
null-, 30, 84, 109
, 394
UNKNOWN, 394
SQL, 113
n- , 27

ACID-, 212
ALL BUT, 150
ALL ANY, , 299
AND ( ), 180, 420

B
BOOLEAN, 52, 327
, 259

C
CASCADE, 133
SQL, 402
CHECK, , 241
COALESCE, 114
CORRESPONDING, 156
CREATE DOMAIN, 380
CREATE TABLE AS, 378, 439
CREATE TYPE, 380

D
DELETE , 125
DISTINCT, 107, 155

G
GROUP BY, 297, 303, 448
, 188

H
HAVING, 297, 303, 448
, 189

I
INSERT (SQL), 126
INSERT , 125
IS_EMPTY, 92

O
ORDER BY, 40, 194
OR ( ), 180

P
PAD SPACE, 73

Q
Query-By-Example, 468

R
REF, , 76
RVA-, 61, 191
, 348, 419

470

, 82
, 23
, 204

SELECT *, 307
SUMD, 423

T
TABLE_DEE, 93, 153, 233
TRUE, 391
TABLE_DUM, 93
FALSE, 391
THE_, , 57, 202, 381
SQL, 57, 205
TransRelational Model, 333, 365, 465
TUPLE FROM, 90

U
UNIQUE (SQL), 207, 272, 298, 397
UNIQUE, , 271
UNKNOWN, 109, 112
null, 394
UPDATE
, 193
, 194
U_, , 335

V
VALUES, 95

, 53
-, 43, 121
-, 200, 278, 338
/ , 343

, 42, 183, 366


, 92, 145
, 115
, 29
, 130
, 146
SQL, 148
, 53, 386
, , 229
, 135, 251
, 252
, 252

WITH, 158

, 67
RELATION, 67
TUPLE, 67
, , 192, 420

XML, 47, 61, 62, 63, 69, 371


XOR ( ), 180

, 179
, 138
, 129
, 358
, 255, 381
, 381
, 39
, 164, 408

, 59
, 218
, 125, 218
, 218
, 212
, 27, 39, 82
FROM, 85
, 165

, 29
, 109
, 48
,
345
, 177, 405
, 293
, 178
, 283
, 359
, 125, 156
n-, 156
, 163
, 283
, 212
, 27
SQL, 70
, 201
, 136, 141,
332, 403
, 141, 403

471

, 349
, 349
, 350
, 306

, 82
, 39, 88
, 283
, 31, 142, 145, 168, 235
, 450
, 313
, 468
, 46
, 46
, 117
, 220, 332

, 257
, 271
COUNT, 275
, 267
, 194, 198
, 27, 28
, 127, 368
, 127
-,
129
, 127
, 164, 254, 408
, 233
-, 232
, 86
, 94

ROW, 74
, 75
, 86
, 448
, 296
, 144, 309
, 27, 81
, 23
, 82
, 123

, 39, 85, 101


null, 397
, 156, 392
, 392
, 149
SQL, 107

, 151

, 169, 410
, 24, 232
, 340, 352, 357, 367
, 212
, , 96,
132, 148
, 252
SQL (), 284
, 282
, 134
, 390
, 250

, 39, 89
, 144, 261, 307
, 256
SQL (), 285
SQL, 262, 268, 275
AND, 270
, 257
3VL, 262
SQL, 261, 274
OR, 270

, 312
, 66, 380
, 233
, 356

(), 236
, 356, 370,
456
, 370
, 370
, 330
, 35, 38
, 35
, 61
, 329

SQL, 337

, 166, 244, 438
, 37, 373
, 294

472

-,
354
, 212
, 221, 276
, 41, 209, 341
-
, 353

, 88
, 89
-, 183, 190
, 175
null,
116, 365, 461, 462

, 255
, 41, 59, 383
, 28, 129

, 418
, 147
, 47
, 261, 263, 446
SQL, 261, 308
-, 45, 46, 121
, 131
, 102, 281
, 33
n-, 152
, 151
, 156
, 91
, 59, 80, 387
, 54
, 72
, 288, 312
, 312
, 311
, 311
, 311
, 400
, 134
(), 235
, 123, 398
, 74
, 172
, 171
, 135
( SQL), 98
, 252

, 73, 195, 314
, 27, 127
, 138
, 284
, 282
, 263,
280
, 134, 255
, 283


, 122
, , 185
, 221, 442
, 33
n-, 156
, 155
-, 61
(constraint)
, 204
, 210
, 206
SQL, 210
, 221
, 219
, 206
-, 209
, 222, 432
( ), 220
(-), 220
, 236
, 124, 211
-
, 209
-, 209
, 131
, 211
, 201
SQL, 205
, 204
(restriction), 32
, 149
, 201
SQL, 205
, 204
, 23, 65
, 306
, 102, 161
(
), 361
, 166, 249
, 27, 46
, 23, 49, 99, 375, 388
, 137

473


, 255
, 233
, 256
, 159
, 256
, 42
, 234
, 244
, 240
SQL, 242
, 229
, 233
, 57
SQL, 70, 91
, 213, 230,
332
, 328, 332
,
329, 332
,
329, 332
, 362
, 80, 126, 332,
399
, 332,
392
, 45, 63
SQL, 69
,
46
, 67
, 217
, 31, 125
, 74, 86
, 338
, 364
, 33
, 149
, 33, 152
n-, 152
, 152
-, 42
, 114
3VL, 396
, 261
, 365
, 310
, 85
SQL, 315
, 269
, 90
, 403
, 85

, 79, 385
, 348
, 350

, 58, 63, 69
, 85, 129
, , 73
, , 191, 420,
421
, 33
, 157
, 141, 402
, 76
, 256, 325
, , 172
, 174
, 35
, 31, 142
, 320
, 325
, 331
, 145, 250, 260
, 92

-
, 140
, 259
, 109, 118, 251, 394
3VL, 395
nVL, 395
, 67
, 56, 202, 380
SQL, 56, 205
, 83
, 89
, 323
, 214
, 66
SQL, 68
, 252
, 246, 439
, 84
, 84
, 130
, 33
n-, 152
SQL, 153
, 151
, 154
, 151, 170, 411
, 358

474
, , 28
, 87
-,
131
, 75
, 76
-, 143, 384
, 154
, 29, 30, 373
, 222
, 76
, 162

, 127
, 82
, 39
, 323
, 97, 167
, 441
, 57
SQL, 70
, 92
, 130, 207, 343
, 369, 453
-, , 338,
371
, 72

, 44, 229

2VL, 394
3VL, 109, 118
, 24
SQL, 315
, 160
, 119
3VL, 395
, 39, 89, 90
, 335, 353
, 138
, 27, 62
, 83
, 86
, 75

, 282

, 149
, 150
, 211
, 105, 161


, 282
, 109
, 124, 134

, 76
, 36

, 40
, 40
, 149

, 370
, 456
, 130, 190,
342, 462
, 130
, 130
, 343
, 118
, 108, 187

X
, 346, 349, 368, 451

, 29, 30, 231

, 356
, , 193

, 353
, 441

, 251
, 282
, 161, 404

, 307

Оценить