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

Understanding

SQL
MARTIN GRUBER
SQL

""
Understanding SQL.
Martin Gruber.
Copyright All rights reserved

SQL .
.
..
...
..

Copyrigl1t 1990 SYBEX Inc., 2021 Challenger Drive,


Alameda, 94501.

, 2014
,
.

FFF Software
FirstSQL .
:

1.

? 3
5
...... . 7
2. SQL . 9
SQL? 10
12
.......... . 15
3. SQL 17
. . . . . . . . . . . . . . 18
- WHERE 24
. . . . . . . . . . . . . . . . . . . . . . . . 26
,~ 4.
29
30
. 32
....... . 37

5. "" 39
IN . . . . . 40
BETWEEN 41
LIE 44
IS NULL 47
....... . 49
6. 51
? 52
................ . 61
7. . 63
64
67
.............. . 71

vii

8. . 75
76
81
9. , 83
. 84
90
10. . 93
? 94
.......... . 105

11. 107
108
................. . 115
12. EXISTS . 117
EXISTS? . . . 118
EXISTS 119
. . . . . . . . . . . . . . . . . . . . . . . . . 124
13. ANY, ALL SOME 127
ANY SOME 128
ALL . . . . . . . 135
ANY, ALL 11 EXISTS
. 139
. . . . . . . . . . . . . . . . . . . 143
14. 1 UNION 145
146
UNION ORDER 151
................. . 157
15. , :11 11 159
DML . . 160
. . . . . . . . . 160
162
163
........... . 165
16. 1111 167
INSERT 168
11 DELETE 170

viii

UPDATE 173
...... . 174
.1 17. 177
CREATE ABLE 178
......... . 179
, 181
182
........ . 183
18. 11 185
186
.......... . 195
19. 197
. 198
FOREIGN ( ) 199
204
............ . 209
20. 211
? 212
CREATE VIEW 212
221
21. 223
. . . . . . . . . . . . . 224
, 228
.................. . 232
22. 235
. . . . 236
237
241
245
.......... . 247
.'J 23. SQL 249
. . 250
? 252
? . . . . . . . . . 253
SQL 255
............................... . 259

ix

24. SQL 261


. . . . . . . . . . . 262
266
. . 268
275
............. . 276
25. SQL
( SQL) . . . . . . . . . . . . . . . . . . . . . . 279
SQL? . . . . . . . . . . . . 280
SQL 282
SQLCODE . . . . . . . . 288
. . 291
293
.......... . 296

. 301
. SQL . . . 319
NSI ..... . 320
. 322
. SQL 325
. . . 326
FORAT . . . . . . . . . . . . . . . . 328
.................... . 330
INTERSECT () MINUS () 332
OUTER JOINS ( ) . 333
. . . . . . . . . . 334
. . 337
SQL . . . . . . . . . . . 338
SQL 345
. , 355
F. SQL .. 357
SQL 358

SQL ( "SEQUEL") -
(Structured Query Language). ,
, ,
.

,
,
.
, ,
, ,
.
, ,
.
,
, SQL
.
, ,
, SQL.
SQL
(American National Standarts Institute) ISO
(Intemational Standards Organization) .
, ,
SQL , ANSI,
. , ,
" " "
" . NSI
. ,
,
.

: ?


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

xi
. , ,
, .
SQL ,
,
. .
, SQL.
, SQL
. ,
.
,

SQL.

.

.
.
SQL,
(queries). - ,
;
SQL. 8 14 .

. SQL: , ,
- 15
23. 24 ,
. 25 SQL ,
.

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

1
(priary keys). ,
.

xii
2 SQL.
, , SQL,
SQL .

3
. SQL
.

4 , SQL
, (=,<,>, ..)
(AND, OR, NOT).

5 , ,
, SQL.
, NULL-.

6 ,
, , , .
, .

7 , :
, , .

8 ,
. ,
.

9
, .

1
.

ll , 10,
.

12 SQL. EXISTS - ,
, .

13 -ANY, ALL, SOME, ,


EXISTS, .

14 ,
,
.

xiii
15 , ,
, , .

16 . ,
.

17 .

18 . ,
.

19 , ,
.

20 , "", ,
, .

21 ,
.
.

22 :
, ,
, .1 ..

23 .
, ,
, SQL.

24 , SQL
.

25 ,
SQL- .
, , ,
FETCH.

( ), ,
( ),
( ), , (
D), SQL ( ), SQL
( F).

xiv
,
SQL , ,
, .
,
"", , .
, .
, ,
.
,
, (FirstSQL,
, I ). ,
, ,

(, )
.

xv
1
rnWllJM'(Q


1.

SQL, ,
. SQL,
,

. ,
,
, .
. .

?
- ,
. . ,
.
, , , , .
, .
( )
, : ,
, - .
:

Name Telephone Address


() () ()
Gcry Farish (415 )365-8775 127 Primrose Ave., SF
Ce\ia Brock (707) 874-3553 246 #4 3rd St., Sonoma
Yves Grillet (762)976-3665 778 Modernas, Barcelona

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


, ,
. ,
.
.
: Patient (), Doctor (), Insurer (),
Balance ().

2
?

Patient Doctor Insurer Balance


() () () ()

Farish Drume ../.S. $272.99


Grillet Halben None $44.76
Brock Halben Health, Inc. $9077.47


, ,
. , Dr. Halben
.
, ( )
, .
Grillet
Brock,
. , ,
,
.
( , , ),
SQL Halben .


ri ,
, .
. . ,
, ,
.
.
, -
(Balance), - .

, .
: " ".
- ,
. ,
.

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

3
1.

Mary Smith.
. .
( ),
, . 1
(p1imary key of t/1e t).
- .
:
, . ,
.
, ,
. ,
(referential integrity) 19.


, ( 1 (fields)
. , ,
, "Address" " ".
, , ,
, , . ,
.
, : 11111 -
(order date). ,
(customer name), odate -
,
. ,
.

1.1, 1.2, 1.3 ,


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

1.1. Salespeople ()

SNUM SNAME I

1001 ! London .12


1002 Sees San Jose .13
1004 Motika London .11
1007 Rifkin Barcelona .15
1003 Axelrod New York .10

1.2. Customers ()

CNUM CNAME I RAING SNUM


2001 Hoffman London 100 1001
2002 Giovani Rome 200 1003
2003 Liu San Jose 200 1002
2004 Grass Berlin 300 1002
2006 Clemens London 100 1001
2008 Cisneros San Jose 300 1007
2007 Pereira Rome 100 1004

1.3. Orders ()

iUM ODATE CNUM SNUM


3001 18.69 10/03/1990 2008 1007
3003 767.19 10/03/1990 2001 1001
3002 1900.10 10/03/1990 2007 1004
3005 5160.45 10/0311990 2003 1002
3006 1098.16 10/03/1990 2008 1007
3009 1713.23 10/04/1990 2002 1003
3007 75.75 10/0411990 2004 1002
3008 4723.00 10/05/1990 2006 1001
3010 1309.95 10/0611990 2004 1002
3011 9891.88 10/06/1990 2006 1001

, snum Customers ,
(salespeople) (customer). snum -

5
1.

Salespeople,
(salespeople). , , ,
, .. snum Customers
Salespeople. ,
(referential itegrity).
19.

, SQL , ,
.
.
l . l :

snum , ("
")

sname

city

comm ()

1.2 :

cnum ,

cname

city

rating ,
. ,

snum , (
Salesperson)
, , 1.3:

onum ,

amt

odate

cnum , ( Customers)
snum , (
Salespeople)

, , .
,
, ,
, , ,
. , ""
"" "" "".
SQL .
.
,
.
,
.

SQL. ,
,
.

7
SQL
1. Custoers ?

2. 4 Custoers?

3. ?

4. ?

( . .)

8
2
SQL
2. SQL

SQL, ,
, ,
, SQL.
, . SQL,
, , ,
, .

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

l. .

2. , , .

3. , .

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

SQL . SQL
, ,
,
.

NSI-?
SQL ANSI (American National Standards Institute -
). SQL ANSI,
- I.
SQL; , Oracle I
SQL-.
, SQL-,
ANSI , . -

10
SQL?

post factum .
SQL : , ANSI,
,
SQL- ,
ANSI,
. , (
- ), ANSI SQL

, . ANSI
.
, , ,

ANSI. ANSI - ;
, , ,
, .

SQL
SQL: .
SQL , -.
SQL
, .
SQL , ,
( ).
SQL SQL, ,
- (
, Cobol Pascal).
. ,
SQL
SQL. SQL SQL
"" , ,
SQL.
SQL,
, ,
. SQL . ,
SQL, .
, ,
.

11
2. SQL

SQL
, SQL
. SQL
, ,

SQL. ANSI,
, SQL- ,
SQL-.
(Data Definition Language, DDL; ANSI
(Schema Definition Language)) ,
(, , ) .
(Data Manipulation Language, DML) - ,
, .
(Data Control Language, DCL) ,
, . ANSI, DCL
DDL. .
, SQL,
.

, , .
.
.
,
,
.
SQL " " (data type ),
, .
. Customers, , cname city
, rating, snum, cnum - .
"Highest" "None" rating,
. ,
. ,
, ,

.
,
SQL .
ANSI SQL ,
. , DATE
() TIME () de-facto (
).
MONEY () BINARY (). (BINARY -

12

, .
, -
.)
ANSI .
ANSI . ANSI
, ,
SQL .
ANSI, INTEGER DECIMAL (
INT DEC }, ,
. INTEGER
DECIMAL , ,
.

CHAR (CHARACTER),
. CHAR ,
, . SQL
, VARCHAR, -
, SQL. CHAR
VARCHAR , , , ''.
, CHAR ,
, VARCHAR
.
, ,
. , 1 , '1 '. '1'
,

1. 1+1=2, '1' + '1' '2'. CHARACTER


,

. ,
, .
(, ),
: ASCII (
) EBCDIC ( ).
, , .
4.
DATE , ANSI.
SQL, DATE,
, .
SQL-,
, .

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

13
2. SQL

, (
).
, , ,
ID, . ,
, ID,
; , ,
( )
ID.
SQL . SQL-
ID,
. ( ) ,
( ) .
, . 22
, ,
.
USER .
ID , .

, SQL.
, .
.
. SQL ,
. , , .
, SQL .
(comma11ds) (stateme11ts) - ,
SQL.
, . (, c/auses).
, ,
. : "FROM Salespeople" "WHERE
city = 'London"'. 1 .
"Salespeople" , FROM -
FROM. "city = 'London'"
WHERE. 1 - ,
. .1, (
) .
, ,
.
, .

SQL. ([ ]) ,
, ( ... ) ,

14

. , (<>), -
, , .

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

.
,
SQL.
.

15
SQL
1. SQL?
2. ANSI DA ?
3. SQL ?

4. ?

( .)

16
SQL


3. SQL

,
,
. , - ,
, .
SQL .

SQL (Structured Query


Language). SQL.
SQL,
. SQL ,
.

? ,
.
,
, ,1
.

11?
DML.
, , ,
DML,
, .
SQL .
, ,
. SELECT.

SELECT
SELECT
. , Salespeop\e,
:

SELECT snum, sname, city, comm


FROM Salespeople;
. 3 .1.
. ,
, .

18

".
-- - - -- -SOl Exccut1on l o q - - - - - - - - - . - -
SllECT snum snamo. c1ty. comm
I IROM
!
Salespeople

.".. 1001 Peel


,.,,",, London
,,, .1.q,,1
0. 12
1002 Serres San Jose 0. 13
1004 Motika London 0. 11
1007 Rifkin Barcelona 0. 15
101!3 Axelrod New York "'. 10

. 3.1. SELECT

,
. :

SELECT , ,
.
, .

snum, sname ". ,


. ,
,

. , ,
,
,
: .

FROM FROM, SELECT, ,


Salespeople .
, - ,
.
Salespeople.
" "(;)
SQL ,
.

19
3. SQL

" " ("\") ,


.

,
- . ,
,

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

SELECT snum, sname, city, comm FROM Salespeople;


SQL ,
, SQL-porpa " " (
Return Enter) .

,
. "*" (""),
.

SELECT
FROM Salespeople;
, .

SELECT

, , SELECT
SELECT, .
, .
,
(*) (). FROM,
- , .
(;) , ,
.

20


SELECT
.
. ,
, SELECT. ,

SELECT sname, comm


FROM Salespeople;
, . 3.2.
, ,
, . ,
.

. 3.2.

, ,
. (*)
, ,
. Orders :
" " (odate), - " " (snum), - "
" (onum) "" (amt):
SELECT odate, snum, onum, amt
FROM Orders:

21
3. SQL

, , . 3.3.
,
SQL.


DISINCT - , -

. 3.3.

SELECT. ,
, Orders.
,

(snum). :

SELECT snum
FROM Orders;
, . 3.4.
, ,
:

SELECT DISTINCT snum


FROM Orders;
. 3.5.
, ,
DISTINCT
.
. , D!SINCT,
. , . -

22

11. 3.4. SELECT

. 3.5. SELECT

Clemens Customers
SELECT DISINCT cname, ,
. Clemens,
.

23
3. SQL

DISINCT. DISINCT
SELECT. SELECT ,
, . ,
, - , . DISINCT,
, , (
, . 6),
.

DISINCT ALL. DISTINCT ALL.


:
. ,
DISINCT, ALL, ALL;
.

-
WHERE

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

SELECT sname, city


FROM Salespeople
WHERE city = 'London';
WHERE
,
. , /
city (), , 'London',
. Serres ..
m . 3.6.
city , WHERE,
, SELECT.
, , WHERE,
, .

WHERE. rating Custoers ,
.

24
1t - :: WHERE

. 3.6. SELECT WHERE


,

.
. (Customers)
(rating) 100 :

SELECT *
FROM Customers
WHERE rating = 100;
, rating
. 3.7.
WHERE ,
. .. , ~1
SELECT, WHERE.

25
3. SQL

. 3. 7. SELECT

,
, . ,
,
.

, , , , ,
.
,

, .
SQL .
. 4
, ,

, .

26
SQL
l. SELECT, (ordernumber),
(amount) (date) Order.
2. , Customers,
l 001.
3. , salesperson :
city, sname, snum, comm.
4. SELECT, rating name
(customer), San Jose.

5. , snum
(salespeople ), (orders)
Orders, .

( : .)

27



J1
4.

3 ,
"" "",
. ,
SQL, ,
.
,
.
.

- ,
.
, 2 + 3 = 5 city = 'London'.
. , (Salespeople),
(commissios) .
" ". SQL
:

> ,

< ,

>= .1

<=

<>

.

(ASCII EBCDIC). SQL
, . ,
, , 'l', ,
.

,
; , '' < '' , '' '' ,
. ASCII,
EBCDIC, ,
. ASCII , ,
'Z' <'', , , 'l' < 'Z'. EBCDIC
. , , ASCII.
, ,
.

30

, , .11.
: 1 + 2
, 3.
,
, + *. ,
SQL-, ,
. SQL-
5.
, (Customes)
(rating) 200. 200 - ,
rating, :

SELECT
FROM Customers
WHERE rating > 200;
. 4.1.
, (ating)
200, :

rating >= 200

. 4.1. ", " (>}

31
4.

SQL . -
, , , ,
. "/"
"/".
, SQL, - AND, OR, N. ,
(, , " "),
. "/"
. SQL (
- ) .
:

AND ( AND )
, .

OR ( OR )
, .

NOT ( NOT )

,
. ,
(customers) San Jose, (rating) 200:
SELECT
FROM Customers
WHERE city = 'San Jose'
AND rating > 200;
. 4.2.
, .
j1 OR ,
(custoers}, San Jose, (rating},
200.

SELECT
FROM Customers
WHERE city = san Jose
OR rating > 200;
. 4.3.

32

. 4.1. SELECT AND

. 4.3. SELECT OR

NOT ( )
. NOT:
SELECT *
FROM Customers
WHERE city = san Jose
OR NOT rating > 200;

33
4. 1101111

. 4.4. SELECT NOT

. 4.4.
, Grass, . Grass San Jose
200, .
, (
). , NOT ,
,

, .

rating NOT > 200


, ,
r. . , SQL ?

SELECT *
FROM Customers
WHERE NOT city = san Jose
OR rating > 200;
NOT city = 'San Jose' : ,
, rating > 200?
. SQL NOT
, .
:

SELECT *
FROM Customers
WHERE NOT (city san Jose'

34
011

OR rating > 200);


SQL : ,
,
, (
). , SQL
, city = 'San Jose'
rating > 200. , ,
, . ,
, ;: , NOT
. . 4.5.
. (
. 4.6):

. 4.5. SELECT NOT

SELECT
FROM Orders
WHERE NOT((odate = 10/03/1990 AND snum > 1002)
OR amt > 2000.00);
,
. :
() (), ()
, ,
,
.

35
4.

.
: odate = 10/03/1990 and snum >
1002, AND, ,
, .
( 1 ,
, Bl) amt > 2000.00 ( 2) OR
(3), ,
Bl 2 . 3
, NOT,
(4), . , 4 - -
, 3 . , Bl 2. Bl
, order date
10/03/1990,
snum 1002. 2 ,
amount 2000.00. ,
2000.00, 2 , 3 , 84 - .
, .
3 1990 snum, 1002 (, ,
onum 3001 , 3, 1990 snum 1007), ! ,
, 3 , .
. (. .
4.6).

. 4.6.

36

. ,
,
, ,
AND OR ,
. NOT
.
,
.
. ,
, .
5 SQL.

37
SQL

1. , , $1,000.
2. , (names) (cities)
London (commission), .10.
3. Customers,
, rating <= 100, ,
Rome.
4. ?

SELECT *
FAOM Orders
WHEAE (amt < 1000

NOT (odate = 10/03/1990


AND cnum > 2003));
5. ?

SELECT *
FAOM Orders
WHEAE NOT((odate = 10/03/1990 snum > 1006)
AND amt > = 1500);
6. ?

SELECT snum,sname,city,comm
FAOM Salespeople
WHERE (comm > +.12 OR
comm < .14);

( . 11 .)

38

",,
5. 1111 011 ""

, 4,
SQL IN, BETWEEN, LIKE IS NULL.
, ,
. IS NULL
NULL-, .

OnepamoplN
IN ,
. ,
'Barcelona', 'London', ,
, (
. 5.1 ):
SELECT
FROM Salespeople
WHERE city = ln

OR ci ty London ;
:

SELECT
FROM Salespeople
WHERE city IN ('Barcelona, 'London');

11
Sl-ll ~----------SUL lxeCL1tJon Log- ~-~~ ___ l-:
11f S,11 """] '-' 1

'IW11lH[ c1ty - uarcelona'

.""".
1 011 1t 'l odon' .

1001 Peel
jjFI
London .1"1,,1
0. 12
101114 Motika London 0. 11
1007 Rifkin Barcelona 0. 15

. 5.1. (Salespeople) Barselona London

40
BETWEEN

1- - - ---------oOL fxcc11t1or1 L 0 ~ ___ 1,


, ::,1L1 f * 1
1' 1 f/~ ::, J lf,spr0 op]
WHll!l c1ty IN ( lJarcelod', Ln') 1

1111"9 51 .1 11 ;,
1001 Peel 0. 12
1004 Motika 0.11
1007 Rifkin 0. 15
~~~-rowse : ti~+

. 5.2. SELECT IN
. 5.2.
, IN ,
. ,
IN, (
), .
, ,
. ,
1001, 1007, 1004.
. 5.3:
SELECT
FROM Customers
WHERE snum IN (1001, 1007, 1004);

BETWEEN
BETWEEN IN. ,
IN, BETWEEN
, ,
. BETWEEN,
, AND .
IN, BETWEEN :
. (
SQL : ("is BETWEEN") -

41
5. ""

rrsu-EC ;---~~-~~-SQL l:.xecutron Log~-~~--~ __ i


'FROM Customers 1

WfHRfsrmIN(HHJ1
,,,,.."_
HHJI 1004) 1

Mll!l,,M ;;;;,1
2001
2006
2008
2007
Hoffman
Clemens
Cisneros
Pereira
London
London -
San Jose
Rome
100
100
300
100
MiifM
1001
1001
1007
1004

. 5.3. SELECT IN

rs;-L~CT * ~--------SOL Exccutron 109~-~~- ~ ~ ~ ~ 1 ]

llFROM Salespeople 11

WH[RE comm BElWElN 10 AND 12. ,

MilJIM 11 .1 11 1.11
1001 0. 12
1004 0. 11
1003 0.10

. 5.4. SELECT BETWEEN


11, 1 ("BETWEEN") ~~.
LIKE.)
Sa\espeople (salespeople),
.10 .12 ( . 5.4):

SELECT
FROM Salespeople

42
BETWEEN

WHERE comm BEWEEN .10 AND .12;


BETWEEN , .. (
.1 .12) . SQL
BETWEEN.
, ,
:

SELECT *
FROM Salespeople
WHERE (comm BEWEEN . 10, AND . 12)
AND NOT comm IN ( .10, .12);
. 5.5.
, ,
.
, IN BETWEEN , ,
, ( IN) (
BETWEEN).
, BETWEEN
, (ASCII) , ..
.
:

SELECT
FROM Customers
WHERE cname BEWEEN AND 'G';
. 5.6.

, ----
1
~- -~SOl lxecLJt1on log~--~- - ,
1, Sf ll f '
1 liO"l 0,,1! ~ " 1 ,,
Wlll <1 (n 11' TWI N 1 NI, 1,) 1
NlJ NO 1 r ornni 1 r, ( 10 1, )

i*i!il"* 11/Jl/J4 otika


."---Browse : t.i.<-" """

. 5.5. BETWEEN

43
5. t( ""

. 5.6. BETWEEN

Grass Giovanni , BETWEEN - ,


. 'G' 'Giovanni',
BETWEEN 'G' .
( ), Giovanni
. Grass.
BETWEEN .
, 'G',
('') 'z' ( 'z',
) .

LIE

LIKE CHAR VARCHAR,


. ,
: .
- , , .
, LIE:

"" (_) . ,
'_t' 't' 'it', 'rat'.

44
11 LIKE

"" (%)
, . , '%p%t' 'put',
'posit', 'opt', 'spite'.

, 'G' (
, , . 5.7):

. 5. 7. SELECT LIE %
SELECT *
FROM Customers
WHERE cname LIKE GX.;
LIE
, . , ,
(sa\espeop\e): \ \.
, ,
( .
5.8):
SELECT *
FROM Salespeople
WHERE sname LIKE - _. ;
,
, , Prettel .
(%) SQL, sname
\ ( ,
). same

45
5. ""

----~~-~- -SQL lxeCIJclO Log-~--- ~


llSEL[CT
FROM Salespeople 1
1 \IHERE sname LIK[ ' 1%'.

1i81ff11M g;

. 5.8. SELECT LIE _ ( )

!, . , '1'
. (%) .
sname VARCHAR.
, LIKE
s-.
,
,
. , sname
:

SELECT
FROM Salespeople
WHERE sname LIKE '/ _' ESCAPE' /.;
, ,
, . ESCAPE
'/' s-, LI-,
, '/', .. ,

. s-
, .
,
.
s- .
, s-,
. s- : "

46
IS NULL

, ", ,
s-.
'_}' sname:
SELECT
FROM Salespeople
WHERE sname LIKE '/_//' ESCAPE. /';
.
(%), (/_),
s- (//) (%).

NULL-
-
, . SQL
NULL-. , NULL-
. NULL ,
, -
().
"" "",
. NULL , .
NULL . , NULL, NULL-
, SQL.
, , .
, NULL snum,
, .

IS NULL
NULL ,
NULL- . NULL-
, NULL-, .
"" , "" - ,
"", -
: NOT (NOT (false)=true), NOT
. ,
"city = NULL" "city IN (NULL)"
city.
false unknown - ,
, , , NULL.
SQL IS,
NULL NULL-.

47
5. 1111 ""

NULL Customers
city :

SELECT
FROM Customers
WHERE city IS NULL;
,
NULL-.
NULL- ,
.

NOT z 01~
, ,
NOT.
, NOT . ,
NULL-, , ,
, NOT ,
:

SELECT
FROM Customers
WHERE city IS NOT NULL;
NULL- ( ),
Customers,
:

SELECT
FROM Customers
WHERE NOT city IS NULL;
.

NOT IN:
SELECT
FROM Salespeople
WHERE city NOT IN ('London', san Jose');
:

SELECT
FROM Salespeople
WHERE NOT ci ty IN ( 'London' , 'San Jose');
. 5.9.

48

SQl lcxecut1on Log-- -- - -- -~

* 11
Salespeople
WHERE c1t NOl IN ('London', 'San Josc'):

WilifM i1#i-
HIHIJ7 Ri fi<in
,,,
Barcelona
.1,,r, 8 1

". 15
1011!3 Axelrod 0. 111!

. 5.9. NOT IN

NOT BETWEEN NOT LIKE.

,
SQL, (BETWEEN)
, (IN),
, (LIE).
, SQL (
) NULL-. NULL-
, IS NULL ( IS NOT
NULL).
SQL,
, . 6.

49
SQL
1. , , 3
4 1990 .
2. , , !
Motika. (: snum .)
3. , ,
'' 'G'.

4. , ,
''.

5. , , amt (amount)
NULL.

( . 11 .)

50
6
8J []] ~ D D u



6. 11


,
. ,
.
, , ,
,

.

, .
.
. :

COUNT ,
NULL-;

SUM
;

VG ;

MIN .

?

SELECT : . SUM
AVG . COUNT, MIN -
. MIN
SII-: MIN (),
- ()
( 4).
(SUM) Orders,
, . 6.1 :

SELECT SUM(amt)
FROM Orders;

52
?

. 6.1.

,
.
,
GROUP .
(
. 6.2):
SELECT AVG(amt)

FROM Orders;

. 6.2.

53
6.

COUNT
COUNT ,
.
, DISTINCT
. , ,
, Orders (
. 6.3):
SELECT COUNT (DISTINCT snum)
FROM Orders;

DISINCT. DISINCT
, ,
SELECT, 3.
DISINCT COUNT
ANSI, .
COUNT DISTINCT ; ,
3, DISTINCT .
DISINCT
,
COUNT. MIN
; SUM AVG,
,
.

COUNT , .
COUNT -

. 6.3.

54
?

. 6.4. ,

, ,
. 6.4:
SELECT COUNT ()
FROM Customers;
COUNT NULL-,
, DISTINCT .
, COUNT ,
NULL-. DISTINCT
COUNT(*),
. ,
NULL-,
( ,
). , NULL-
, COUNT
.

.
( ) ALL,
, DISTINCT, :
. ANSI COUNT,
. ALL *
COUNT :

ALL ;

ALL NULL-.

55
6.

*
, NULL-
COUNT, , COUNT,
NULL- .
rating, NULL-, Customers
( ):

SELECT COUNT (ALL rating)


FROM Customers;

,

. ,
, . (
DISINCT.) , Orders
(n)
. , amount (amt)
!n. :

SELECT (binc + amt)


FROM Orders;


. , ,
.
,
. .1
. SQL
.

GROUP
GROUP

.
SELEC. , ,
, .
, (amt) Orders
snum GROUP , , :

SELECT snum, MAX(amt)


FROM Orders
GROUP snum;
. 6.5.

56
1114 11?

. 6.5. (amounts) (salesperson)

GROUP ,
.
, snum,
. , ,
GROUP ,
.
.
GROUP .
, , ,
.
Orders (date) ,1 salesperson
. :

SELECT snum, odate, MAX(amt)


FROM Orders
GROUP snum, odate;
. 6.6.
, .. , ,
.

57
6. n 11

WiiifM +.1:1,
1001 10/03 1990 767.19
1'!101 10/05/1990 4723.01!1
1001 10/06/1990 9891.88
1002 11!1/03/1990 5160.45
1002 10/04/1990 75.75
1002 10/06/1990 1309.95
HlliJ3 10/'14/1991iJ 1713.23
1004 10/03/1990 1900.10
11!107 11!1/03/1990 1098. 16

. 6.6. (orders) (salesperson)

VING
, ,
, $3000.00.
WHERE ( ,
), ,
. ,
:

SELECT snum, odate, MAX(amt)


FROM Orders
WHERE MAX(amt) > 3000.00
GROUP snum, odate;
ANSI.
, $3000.00,
HAVING. ,
, , WHERE
. :

SELECT snum, odate, MAX(amt)


FROM Orders
GROUP snum, odate
HAVING MAX(amt) > 3000.00;
. 6.7.

58
?

. 6. 7.

HAVING , SELECT
, GROUP ,
. :

SELECT snum, MAX(amt)


FROM Orders
GROUP 8 snum
HAVING odate = 10/03/1988;
HAVING odate, ~--
( ) .
HAVING , GROUP .
(
. 6.8):
SELECT snum, MAX(amt)
FROM Orders
WHERE odate = 10/03/1990
GROUP 8 snum;
odate ,
, , , .

:
" 3 ". 7 ,
.

HAVING ,
.

59
6. t

. 6.8. 3 1990 .
, GROUP . ,
Serres Rifkin:
SELECT snum, MAX(amt)
FROM Orders
GROUP snum
HAVING snum IN (1002, 1007)
. 6.9.


SQL, ANSI,
. ,
. .1 :

SELECT odate, ( SUM (amt) )


FROM Orders
GROUP odate;
, , . ( ,
, ,

, .) ,
SUM dt-, -
, .
GROUP ,
odate.

60

. 6.9. VING GROUP

. ,
,

, .
,
, . ,

.

,
GROUP .
, .
, .

.
HAVING.
, . 7 ,
.

61
SQL
l. , 3 1990 .

2. , (
NULL) Customers.
3. ,
.

4. , ,
'G'.
5. , (rating)
.

6. , ,
. (, ,
.)

( . .)

62


7.

-
. , ,
,

, ,

.
,
, .

, SQL, ,
. ,
, .
SQL ,
, .
.

. ,

. SQL
.
SELECT . ,
, ,

. 7.1.

64

SELECT snum, sname, city, comm 100


FROM Salespeople;
. 7.1.
. ,
. - ,
( ,
SELECT , ),
.
, , ,
. ,
, .

. '',
, , 1. ,
, SELECT. ,
, , . SL
1+2, ''+ '', '' ''
, , -
. ,
.

, ,
, ""(%),
, :

SELECT snum, sname, city, , comm 100


FROM Salespeople;

-~SOL Execut1on Log-- - - --- --- ;


SELfCT snum, sname. c1ty, '%'. comm * 1"1'1

.
1
FROM Salespeople,

,.."". IF!-

1001
1002
1004
1007
1 l/Jl/J3
Peel
Serres
Mot.ika
Rifkin
)(elrod
London
San Jose
london
Barcelona
New Vork
'
%
%
%
%
12.00000
13.00000
11. 00000
1 '5. 00001/J
10.00000

. 7.1.

65
7.

. 7.2.
,
, . ,
,
. , ,
.
(. . 7.3), :

SELECT "F, odate, , there ,

COUNT (DISTINCT onum), 'orders ..

FROM Orders

GROUP odate;

10/05/1990 ,
. (
UNION, 14.)

, .
,
.

, SQL,
,
. SQL
, . SQL
. , ,

. 7.3. ,

66

SQL,
. , SQL.

,
- .
SQL ORDER ,
.
.
, GROUP ,
(ASC) (DESC)
.
.

(Orders), , (
cnum) :
SELECT *
FROM Orders
ORDER cnum DESC;
. 7.4.

. 7.4. cnum

67
7.

J1
cnum
, , amt (
. 7.5):
SELECT
FROM Orders
ORDER cnum DESC, amt DESC;
ORDER
. , ,
. ANSI .
, :

SELECT cname, city


FAOM Customers
ORDER cnum;
cnum ,
ORDER .
, ,

, ,

. , ORDER
, .

.1.111" 1 .1.n
3006 1098.16 10/03/1990
3001 18.69 10/03/1990
3002 1900.10 10/03/1990
3011 9891.88 10/06/1990
3008 4723.00 10/05/1990
3010 1309.95 10/06/1990
3007 75.75 10/04/1990
3005 5160.45 10/03/1990
3009 1713.23 10/04/1990
3003 767.19 10/03/1990
; . .. ' ". .
. 7.5.

68


ORDER GROUP . ORDER
.
ORDER . ,
;
:

SELECT snum, odate, MAX(amt)


FROM Orders
GROUP snum, odate
ORDER snum;
. 7.6.
,
.

. 7.6.


,
, . , ,
, . ..
, SELECT, ORDER
1, . ,
, Salespeople,
commission (comm) (
. 7.7):

69
7. 11

. 7. 7.

SELECT sname, comm


FROM Salespeople
ORDER 2 DESC;
ORDER ,
;
ORDER . ,
, SELECT,
ORDER , . ,
(orders) (salespeople)
, . 7.8:
SELECT snum, COUNT (DISTINCT onum)
FROM Orders
GROUP snum
ORDER 2 DESC;
,
, .
ANSI SQL, ,
:

SELECT snum, COUNT (DISTINCT onum)


FROM Orders

70

. 7.8.

GROUP snum
OROER COUNT (OISTINCT onum) OESC;
.

ORDER NULL-
, ,
NULL-,
. ANSI,
,

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

71
7.

, , ,
.
. ,
, ORDER
.


, .
,
, ,
ORDER
SELEC.
8 . ,
,
.

72
---------

SQL
l. , 12% .
u Orders, (order number),
(salesperson number) (amount of
salesperson's commission).
.

2. Customers,
. :

For the city (city), the highest rating is: (rating).


( (city) : (rating).)
3. , (customers)
(rating). rating
, (customer's name)
(customer's number).
4. ,
.

( . : .)

73

J1

8.


.
() .
,
, .
, ,
.

SQL

.
(join)
. 1,
(relationships),
.
, , .1,
.
~ FROM;
.
, ,
.
, , WHERE.


,
, - . :
Salespeople.snum
Customers.city
Orders.odate
,
, SQL
.
, .
.,
-it. ,

76

Salespeople.city Customers.city, SQL


, .

(joi)
, (Salespeople)
(Customers) ,
.
Salespeople Customes
, city.
, ( . 8.1 ):
SELECT Customers.cname, Salespeople.sname, Salespeople.city
FROM Salespeople, Customers
WHERE Salespeople.city = Customers.city;
city Salespeople Customers,
city .
, ,
.
, , , r
, .

,

. SQL ,

r SQL Execution Log~~~~~~~~~~~~


fSElfCT Customers cname. Salespeople.sname,
!1Salespeople.c1ty
'FROM Salespeople. Customcrs
WHERE Salespeople c1ty Customers c1ty:
1

srme

Peel
Peel
Serres

. 8.1.

77
8. 11

! Salespeople,
Customers, .
"", .. city Customers
London , !, SELECT
.
Salespeople (
, ).

r , .

. 1 snum.
u , 1.
.
,
, :

SELECT Customers.cname, Salespeople.sname


FROM Customers, Salespeople
WHERE Salespeople.snum = Customers.snum;
. 8.2.

1@I
Hof f man
Giovanni
Liu
Grass
Clemens

. 8.2.

78

, ,
, - snum -
. ,
. snum,
, ,

. , , .1 ,
, - .


, , ,
.
, WHERE
, . "City=='London'"
"Sa\espeop\e.snum==Orders.snum" -
. , -,
, .
. (
. 8.3 ):
SELECT sname, cname
FROM Salespeople, Customers
WHERE sname < cname
AND rating < 200;
.
, -

. 8.3. ,

79
8.

, 200.
,
.

.
, , ,
. (
. 8.4):
SELECT onum, cname, Orders.cnum, Orders.snum
FROM Salespeople, Customers, Orders
WHERE Customers.city <> Salespeople.city
AND Orders.cnum Customers.cnum
AND Orders.snum Salespeople.snum;
, , ,
,

( snum),
( cnum
snum Orders).

. 8.4.

80


,
.
,
.
.

81
SQL
, ,
, .

2. ,
.

3. , ,
, 12%.
, .

4. ,
, 100.

( . .)

82
9
~~~

,



9. , 11

8 , .
.
. "
",
.

:
( )
.
, .

- ,
.


: , SQL ,
. ,
, ,
.


,
, .

. ,
. ,
t , t. .
FROM.
, , .
, (
. 9 .l ):

SELECT first.cname, second.cname, first. rating


FROM Customers first, Customers second
WHERE first.rating = second. rating;

84
1111

Giovann1 Giovanni
Giovanni Liu 200
Liu Giovanni 200
Liu Liu 200
Grass Grass 300
Grass Cisneros 300
Clemens Hoffman 100
Clemens Clemens 100
Clemens Pereira 100
Cisneros Grass 300
Cisncros Cisneros 300
Pereira Hoffman 100
Pereira Clemens 100
Pereira Pereira 100
Browse : 1'.1.<--+ PgUp -~

. 9.1. " "

(, . 9.1 , ,
,

).
SQL ,
, "first" () "secod" ().
Customers,
. first second
FROM .
SELECT, ,
FROM. . SQL
- , ,
FROM .
.
.

Customers , SQL .1
JOIN, :
.


,
- . ,
, . ,
first second,

85
9. ,

second - first.
Hoffman Clemens, Clemens Hoffman.
Cisneros Grass, Lie Giovanni .. ,
, , Lie Lie.
- ,
, ,
. ,
, :

SELECT first.cname, second.cname, first.rating


FROM Customers first, Customers second
WHERE first.rating second.rating
AND first.cname < second.cname;
. 9.2.
Hoffman Periera ,
.
(.. Periera
first Hoffman second),
. , Hoffman ,
, -

. 9.2.

86
11 11

. ,
<= <.


SQL
. Orders, , cnum
snum .
(customer) (salesperson),
Orders
.
:

SELECT first.onum, first.cnum, first.snum, second.onum, second.cnum,


second.snum
FROM Orders first, Orders second
WHERE first.cnum = second.cnum
AND first.snum <> second.snum;
, .
Orders first,
Orders second.
, .
, cnum 2008, snum
1007; , cnum
. , snum
( 1007) , "",
,
SELECT. snum
cnum . ,
.

~
- ,
,
.
SELEC. ,
, ,
, , ,
SELECT .
( 11).

87
9. ,



, SELECT .
, (salespeople) (customers).
, ,
.
, ,

(
. 9.3):
SELECT a.cnum, b.cnum, c.cnum
FROM Customers , Customers , Customres

WHERE a.rating = 100


AND b.rating = 200
AND . rating = 300;
(customers)
,
100, - 200, -
300. .
, GROUP
ORDER , .
, FROM
SELECT, .
, . ,
(customers), ,

. 9.3.

88

(salesperson) Serres (snum 1002) (


. 9.4):
SELECT b.cnum, b.cnam
FROM Customers , Customers

WHERE a.snum = 1002


AND b.city = a.city;
, ,
snum 1002. ,
Serres. ""
(city), (city) ;
, city
, city .
,
. (customers)
Serres ,
. , (customers) Serres,
Liu
Grass. (customers),
(San Jose Berlin ), , , Liu Grass.
(joins),
. Customers
,
. (customer)
Salespeople , (salesperson) (
. 9.5):

. 9.4. , , Serres

89
9. ,

. 9.5.

SELECT sname, Salespeople.snum, first.cname, second.cname


FROM Customers first, Customers second, Salespeople
WHERE first.snum = second.snum
AND Salespeople.snum = first.snum
AND first.cnum < second.cnum;

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

,
,
. SQL 1
.

90
SQL
1. , (Salespeople ),
.
Salespeople ,
.

2. ,
, ,
!.

3. ,
(customers ), , Hoffman. ,
cnum rating ,
rating.

( . 11 .)

91
10
31
10. ;:

,
. ,
, ,

. ,
, DISTINCT,
;
HAVING
.

?
SQL .
<1, .
, , snu Motica.
Orders.
( . 10.1 ):
SELECT
FROM Orders
WHERE snum
(SELECT snum
FROM Salespeople
WHERE sname = 'Motika');

. 10.1.

94
?

() , SQL
( ) WHERE.
, :
Salespeople , sname Motika,
snum.
snum = 1004.
SQL
,

WHERE snum = 1004


, ,
. 10.1.
,
, .
( , snum).
(salesperson
number) Motika, :
WHERE snum = 1004
,
.
Motika.
.

,
. WHERE sname = 'Motika' WHERE city = 'London',
.

, .
, (
, 4), ,
.
, , ,
.
, ( NULL-xo),
, ,
"" (unknown). ""
"": -

95
10. t~

( unknoun-peaa . 5).

SELECT
FROM Orders
WHERE snum =
(SELECT snum
FROM Salespeople
WHERE city = 'Barcelona');
.
Barcelona (salesperson) Mr.Rifkin,
snum, , , .
.1 .
Salespeople , (city) Barcelona
, , ,

, .

DISTINCT
DISTINCT
. ,
(orders), ,
Hoffman (cnm 2001 ). (
=
. 10.2):

SELECT
FROM Orders
WHERE snum =
(SELECT DISTINCT snum
FROM Orders
WHERE cnum = 2001);
, snum ,
Hoffman, l 00 l; , Orders
snum.
, Orders
cnum snum. ,
, snum
(, ) cnum.
, . DISTINCT
.

96
?

. 10.2. DISTINCT

- Customers,

Orders. cnu - Customer,
,
. , Orders,
Customers, , . (SQL
, .
22.)
,
, ,

. ,
.


, , < >
<> <>, <> <> < >
<> <> <>.
:

SELECT
FROM Orders
WHERE (SELECT DISTINCT snum
FROM Orders

97
10.

WHERE cnum 2001)


snum;
ANSI, ,
. ANSI
, , ,
.


,
, , .
,
GROUP ,
. , ,
4 1990 . (
. 10.3):
SELECT *
FROM Orders
WHERE amt >
(SELECT AVG (amt)
FROM Orders

. 10.3. , ,
, 4 1990 .

98
?

WHERE odate = 10/04/1990);


4 1990 . 1788.98
(1713.23 + 75.75),
2, 894.49. , amt
(amount) , 894.49,
.

, GROUP ,
.
. , ,
GROUP HAVING
.
WHERE. ,
, (comm)
(salespeople}, ,

SELECT AVG (comm)


FROM Salespeople
GROUP city
HAVING city = 'London';
!
. , :

SELECT AVG (comm)


FROM Salespeople
WHERE city = 'London';

,
IN
,
, IN ( BETWEEN, LIE,
IS NULL ). IN ,
'l

. IN , SQL
. , IN ,
, (Orders)
(salespeople) London ( . 10.4):
SELECT "
FROM Orders
WHERE snum IN
(SELECT snum

99
10.

. 10.4. IN

FROM Salespeople
WHERE city = 'London);
, (
) , , joi:

SELECT onum, amt, odate, cnum, Orders.snum


FROM Orders, Salespeople
WHERE Orders.snum = Salespeople.snum
AND Salespeople.city = 'London;
, SQL
,
.
Salespeople snum , city = 'Lodon',
Orders;
. 1001 1004.
Orders, snum
(1001 1004).
-
, .
, ,

.
joi ,
, , . -

100
?

,
.

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

SELECT onum, amt, odate


FROM Orders
WHERE snum =
(SELECT DISTINCT snum
FROM Orders
WHERE cnum = 2001);
DISINCT, IN .
:

SELECT onum, amt, odate


FROM Orders
WHERE snum IN
(SELECT snum
FROM Orders
WHERE cnum = 2001);
~,. (orders) m
(salesperson), IN .
, , m
. , ,
, .
.

, . IN ,
, ,
. , (salespeople),
(customers) (London):
SELECT comm
FROM Salespeople
WHERE snum IN

101
10.

(SELECT snum
FROM Customers
WHERE city = "London");
, . 10.5,
PEEL (snum = 1001),
. , .
() , , - -

. 1.5. IN ,

. ,
IN.
. .
city ,
, city Salespeople ,1 Customers. SQL
(), ()
FROM ().
, . ,
"city" WHERE city Customers
(Custoers.city). Custoers FROM
, .
, ;
, .
, .

102
?

. ,
, , .
, SL-
. , SELECT *
.
EXISTS, 12.
n. SELECT
, , .
, IN. ,
= (
. 10.6:
SELECT *
FROM Customers
WHERE cnum
(SELECT snum + 1000
FROM Salespeople
WHERE sname = serres');
, cnu 1
snu Serres. , snu
( , UNIQUE INDEX,
17, UNIQUE, 18);
. -

. 10.6.

103
1. J ll

, , , ,
, snum cnum , ,

. .

HAVING
HAVING.
,
, GROUP HAVING. (
. 10.7):
SELECT rating, COUNT (DISTINCT cnum)
FROM Customers
GROUP rating
HAVING rating >
(SELECT AVG (rating)
FROM Customers
WHERE city = 'San Jose');
,

. 10. 7. (customers) (raiting),


San Jose
San Jose. , -

104

300,
, .

,
.
,
, IN WHERE
HAVING .
.
, ,
. 12 13 , ,
IN, , ,
.

105
SQL
1. ,
Cisneros. ,
.

2. , ,
.

3. , ,
amount Orders.

( . .)

106
11
811811

11. 1111

,
,- .
WHERE HAVING.
(join),
( ).

SQL , ( )
, FROM
, (correlated subqel}').
,
. , - ,
SQL. , , ,
,
.

, , ,
3 1990 ( . l l. l ):
SELECT
FROM Customers outer
WHERE 10/03/1990 IN
(SELECT odate
FROM Orders inner
WHERE outer.cnum = inner.cnum);

.
"inner" "outer" ,
9. (inner - , outer -
);
. cnum ,
.
, ,
- (current candidate 1).
:

l. , .
-.

108

~---- - -- SOL Lxecut l Log ~ - 1


1

. ..
Sf 1 f CJ 1
1 ~1-iOM lLJStomcr:::. f ir-,t Urdcr<J sccurid
!~w1 1~! 1 ir st ~ccond criom 1

1:ANf) ~fcnr1 0(1ctte- lrJ/OJ/ 19Y0


1 1

+111111. +1111
2f/Jf/J1 Hof fman London '' " 1110
111
1f/Jf/J1
2f/J03 Liu San Jose 2f/Jf/J 10f/J2
2f/J08 Cisneros San Jose 3f/Jf/J 1007
2L!Jl'18 Cisneros San Jose Jf/Jf/J 1f/Jf/J7
2f/Jf/J7 Pereira Rome H'f/J 1f/Jf/J4

-~-~--~~
1 " 1

. 11.1.

2. ,
FROM .

3. . , , ,
( "outer"),
. -
(outer refe1e11ce).

4. ,
3. , -
.

5. - ,
.

SQL :

1. Hoffman Customers.
2. - "outer".
3. : Orders
, cnum outer.cnum;
2001 ( cnum Hoffman).
odate Orders,
true (),
odate.

4. odate, cnum 2001,


, ,

109
11.

3 1990 . ,
( ), Hoffman
.

5. Giovanni,
-, ,
Customers.
SQL .
(join),
, (
. 11.2):
SELECT
FROM Customers first, Orders second
WHERE first.cnum = second.cnum
AND second.odate = 10/03/1990;
Cisneros , ,
. , SELECT
DISINCT SELECT. ,
. IN,
, ,
. , DISINCT
.

, ,
. (
. 11.3):

~~~ -~ -- ~-SOL ExeCLJt1on Log~---- -- -


1Sf LI ! Ji
.fROM Lustomers f 1rst. Orders second 1

..".".
~ 1 Wlif1--i'( 1 i r st t:rm sccond cnum 1
'AND Sf"<on Odte - HJ/03/1990. 1

,,,
Miifi~'lt
2003
2008
MiFf-
Hoffman
Liu
Cisneros
London
San Jose
San Jose
"''''
100
200
300
1001
1002
1007
2008 Cisneros San Jose 300 1007
211107 Pereira Rome 100 100~
11 1

[[_~-~~~~-~~~~--]
. 11.2.

110
11

. 11.3.

SELECT snum, sname


FROM Salespeople main
WHERE 1 <
(SELECT COUNT (*)
FROM Customers
WHERE snum = main. snum);
FROM .
SQL ,
, FROM
. ( -
snum), SQL . ,
, ,
. ,

- .


,
. ,

111
11.

. .
Orders , snum cnum
Customers,
, . ,

(, cnum, Customers,
).

SELECT *
FROM Orders main
WHERE NOT snum =
(SELECT snum
FROM Customers
WHERE cnum = main. cnum);
,
( 19).
, ,


, ,
.
. , ,
( . 11.4):
SELECT *
FROM Orders outer
WHERE amt >
(SELECT AVG (amt)
FROM Orders inner
WHERE inner.cnum = outer.cnum);
, ,
, ,
, . - (
. 11.5):
SELECT *
FROM Orders outer
WHERE amt >=
(SELECT AVG (amt)
FROM Orders inner

112
tu lloallpoc

WHERE inner.cnum = outer.cnum);


,
, ( ,
).

. 11.4.

.1.;
3003 767.19 10/03/1990
3002 1900. 10 10/03/1990
3005 5160.45 10/03/1990
3006 1098. 16 10/03/1990
3009 1713.23 10/04/1990
3010 1309.95 10/06/1990
3011 9891.88 10/06/1990

. 11.5. ,

113
11.

VING
HAVING .
,
HAVING.
6, HAVING
SELECT GROUP .
, , HAVING
, .
,
, .
, amounts (amt)
Orders, ,
, 2000.00:
SELECT odate, SUM (amt)
FROM Orders

GROUP odate
HAVING SUM (amt) >
(SELECT 2000.00 + (amt)
FROM Orders

WHERE a.odate = b.odate);


n ()
, ,
. , ,
WHERE. GROUP
HAVING.


,
(
) . , ,
,


DISINCT Uoin),
. ,
. , ,
, ,
, ,

114

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

SQL -
. , ,
HAVING.
.

- , ,
IN, , IN
. , EXISTS, 12.

115
SQL
1. SELECT,
,
.

2. , ( ) ,
, .
(join), - .
?

(: -
, , ,
.)

( . 1t .)

116

EXISTS
12. EXJSTS

,
, , EXISTS.
, ,
.
EXISTS .
, NULL- .
,
.

EXISTS?
EXISTS - , "" "",
, . ,
AND,
OR NOT. ,
, ,
. ,
unknown. , Customers ,
( ) San Jose (
. 12. l ):
SELECT cnum, cname, city
FROM Customers
WHERE EXISTS

Giovanni
Liu
Grass
Clemens London
C1sneros San Jose
Pereira Rome

. 12.1. EXISTS

118
EXJSTS 11

(SELECT *
FROM Customers
WHERE city = san Jose');
San Jose.
EXISTS , ,
EXISTS ,
"". ( )
, ,

. EXISTS
,

EXISTS
EXISTS , ,
, ,
. ,
EXISTS, ,
.

EXISTS

EXISTS,
, ,
m . EXISTS
, ,
. , EXIST
, .
, ,
( . 12.2):
SELECT DISTINCT snum
FROM Customers outer
WHERE EXISTS
(SELECT *
FROM Customers inner
WHERE inner.snum = outer.snum
AND inner.cnum <> outer.cnum);

119
12. EXISTS

. 11.2. EXISTS

- (
), ,
snum ( ), cnum (
). , ,
, ,
(.. , ,
- ). , EXISTS
, (snum)
. DISINCT,
, .

EXISTS
.
Customers Salespeople (
. 12.3):
SELECT DISTINCT first.snum, sname, first.city
FROM Salespeople first, Customers second
WHERE EXISTS
(SELECT
FROM Customers third
WHERE second.snum third.snum

120
EXJSTS

AND second.cnum <> third.cnum)


AND first.snum = second.snum;
, , .
Salespeople Customers.
(AND first.snum = second.snum)
, EXISTS.
,
snum. AND, ,
,
. , ,
.

.

. 12.3. EXISTS JOIN ()

NOT EXISTS
, EXISTS
. EXISTS -
-
NOT. ,
, - NOT EXISTS (. 12.2)
( . 12.4):

SELECT DISTINCT snum


FROM Customers outer

121
12. ll 01repamopa EXISTS

WHERE NOT EXISTS


(SELECT
FROM Customers inner
WHERE inner.snum = outer.snum
AND inner.cnum <> outer.cnum);

. 12.4. EXISTS NOT

EXISTS

EXISTS .
. , EXISTS
"", ;
, EXISTS - "".
EXISTS .1 ,
.
EXISTS
. , ,
, ,
, . ,
, .

- ,
, .
, EXISTS (SELECT COUNT (DISINCT sname) FROM sa\espeop\e)
EXISTS (SELECT sname FROM Sa\espeop\e),
.

122
11 EXJSTS


.
, .
, , ,
.
SQL.
:

SELECT
FROM Salespeople first
WHERE EXISTS
(SELECT
FROM Customers second
WHERE first.snum = second.snum
AND 1 <
(SELECT COUNT()
FROM Orders
WHERE Orders.cnum = second.cnum));
. 12.5.

Sd~ cumm
London 1. 12
San Jose ". 13
Barcelona ". 15
Dn Up ~ 14~ Home

. 12.5. EXISTS

123
12. EXISTS

.
Salesperson - ( )
. -
Customers ( ).
(customer) (.. first.snum <>
second.snum), .
, ,
, ,
. (orders)
( ). 1,
"", .
EXISTS , , ,
, .
, .
- ,

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

, EXISTS ,
SQL.

.
- ,
: ANY, ALL, SOME. 13
, ,
.

124
SQL
1. EXISTS , ,
, 300.
2. , ?

3. EXISTS,
, , .

4. , Customers ,
, (
, , ) Orders
(: ,
).

( . )

125

ANY,

ALLuSOME
13. ANY, ALL SOME

,
. ( , ANY SOME
.)
SQL, .
,
; .

ANY, ALL SOME , EXISTS,


; ,
EXISTS ,
. IN, ..
, . ,
IN, .

ANY SOME
ANY SOME. ,
.
. ,
,
.
,
( . 13.1):

. 13.1. ANY

128
~ Ollepamop ANY S1

SELECT *
FROM Salespeople
WHERE city = ANY
(SELECT city
FROM Customers);
ANY sity Customers,
, , - (ANY)
city . ,
,
. ANY EXISTS,
.

IN EXISTS ANY
,
IN:
SELECT *
FROM Salespeople
WHERE city IN
(SELECT city
FROM Customers);
, . 13.2.

--_ ----~~~so1 fxecLJt1on Log-- - -~


,f SI 1 1 ( l 1

'f HO'IJ ::-..1 l -__, 1 (' 1


'Wlll !<! < 11 IN ,
, ( Sl 1 l l ! t '
1 f ~ l11~>tumr1 ,) ,

.""". 11111111 Peel


11111112 Serres
.".. london
San Jose
1'""'
. 12
111.13
11111114 Motika London 111. 11
1

11

1 :

t __ --~ ~~~~ _-~ __ _ -~~-~-- __ _


1 i
1

. 13.2. IN ANY

129
13. ANY, ALL SOME

ANY
, , , IN. ,
, ,
( . 13.3):
SELECT *
FROM Salespeople
WHERE sname < ANY
(SELECT cname
FROM Custome rs);
, , Serres Rifkin,
,
. EXISTS,
. 13.4:
SELECT *
FROM Salespeople outer
WHERE EXISTS
(SELECT *
FROM Customers inner
WHERE outer.sname < inner.cname);
, ANY ( ALL),
EXISTS, . , EXISTS
ANY ALL. NULL-
( ).

____ _ _ _.__ ~~ Excct1t 11 L--- ~~- - --- 1

Sl L f ( 1 1

~r11111L>
ROM S,1 I 1 , 1 t-' 1
1lw11LHt < ANY 1
' ( l l ( 1 r 11,;me 1
11 f<Oll 1Jst omcrs)

*""'1'*1
001 Peel
,,,,",
London " .. r"
12
1004 Mot1ka London 0. 11
100} Axelrod New Vork l1J. 10

. 13.3. ANY

130
ANY SOME

- - -----==----= - - ~01 fxeClt ior1 1 1 ------=-- - 1

~1 l Ll I 1
1 ll~OM ~d.1(1--'upJ~ 011{Pt
,Wllf !!! f J :, 1::, 1
('! 11 ( 1
]t l~1Jf\l ( ll t r J r 11r ' 1
W11! fJI u11t f r 11 1'1< < 1 rlf'' c.:r1 illlf-)

*"''* 11111111 Peel


1llH/14 Motika
london
london
i'"'*
. 12
111. 11
1111111} Axelrod New York ". 1 fll
.~ 1

,' 1

l - -~ --__ 1

. 13.4. EXISTS ANY

ANY ALL, (
IS NULL).
EXISTS
, ANY ALL ,
EXISTS,
. , ANY ALL ,
, EXISTS.
ANY ALL
. , EXISTS
,
. SQL
,
(
, ).
EXISTS, ANY
ALL, ANY ALL ,
.
,
.

ANY
ANY .
, ,
Rome, ,
( . 13.5):
SELECT

131
13. 01t ANY, ALL SOME

. 13.5. , ANY SQL

FROM Customers
WHERE rating > ANY
( SELECT rating
FROM Customers
WHERE city = 'Rome');
" , () (
city Rome)", :
, (eve1y) ,
city Rome. SQL ANY . ANY
, () ( ),
() .
ANY ,
300 Giovanni, Rome
200. , ANY Periera Rome
100. 200 (
100) , Rome (Giovanni),
200 (, Rome,
). , ,
.

: , ,
, 6 1990 :
SELECT *
FROM Orders

132
l ANY SOME

WHERE amt > ANY


(SELECT amt
FROM Orders
WHERE odate = 10/06/1990);
. 13.6.
, (9891.88),
6 , ,
amounts 6 1990 - 1309.95.

. 13.6. , amt ANY 6


>= >,
, .
ANY SQL-, , .
,
San Jose ( . 13.7):
SELECT *
FROM Orders
WHERE amt < ANY
(SELECT amt
FROM Orders a,Customers

WHERE a.cnum = b.cnum


AND b.city = 'San Jose');
San Jose,
, , . -

133
13. ANY, ALL SOME

**ililM
311101
31111113 767.19 10/03/199111
31111112 19111111.1111 1111/1113/199111
311106 1098.16 10/1113/1990
31111119 1713.23 1111/04/199111
31111117 75.75 10/1114/199111
3008 4723.1110 10/05/1990
31111111 131119.95 10/06/199111
- - -l:Jr Ov/SC ri~~ Dn u -- 1

. 13. 7. ANY JOIN ()

. 13.8. ANY

, < ANY ", ", > ANY


", ". ,
(
. 13.8):

134
ALL

SELECT
FROM Orders
WHERE amt <
(SELECT MAX(amt)
FROM Orders , Customers
WHERE a.cnum = b.cnum
AND b.city = 'San Jose');

~ ALL
ALL "", : (eve1y) ,
, ,
. ,
,
Rome, 1)10
, . 13.9:
SELECT
FROM Customers

. 13.9. ALL

135
13. ANY, ALL SOME

WHERE rating > ALL


(SELECT rating
FROM Customers
WHERE city = 'Rome');
,
,
. Giovanni, 200.
, ,
200.
EXISTS , ANY,
( . 13.10):
SELECT *
FROM Customers outer
WHERE NOT EXISTS
(SELECT *
FROM Customers inner
WHERE outer. rating <= inner. rating
AND inner.city = 'Rome');

IF- ~--~~-~~ -SQL lxec1Jt l l_og


S[LECT
FROM CLJst.omcrs ot1tcr-
WHtRf NOT [XISIS
(SFI FCT
f Ct1st omes inncr
WHLRI_ OL1tcr rat19 = inner.rat1ng
AND inc!" 1 t.y - 'f<ome');

1-
1

~----------
~ Grass Berlin 300 ~

1:~
2008 Cisneros San Jose 300 1007
----- -~

1'
.1

l[_~~~rowse D U - --~ 4--

. 13.10. EXISTS ALL

136
ALL


ALL, , , ,
" " (equal to all),
, , .
:

SELECT
FROM Customers
WHERE rating = ALL
(SELECT rating
FROM Customers
WHERE city = san Jose');
,
. , , -
San Jose .
:

SELECT
FROM Customers
WHERE rating =
(SELECT DISTINCT rating
FROM Customers
WHERE city = san Jose");
,
;
ALL .
,
.
ALL ,
<>. , " "
, .
, ,
rr . SQL <> ALL "
" . ,
, .
, , , (
. 13 .11 ):
SELECT
FROM Customers
WHERE rating <> ALL
(SELECT rating

137
13. 011 ANY, ALL SOME

. 13.11. ALL <>

FROM Customers
WHERE city = san Jose');
, city
San Jose. : 200 ( Liu) 300
( Cisneros). ,
rating , .. l 00.
NOT IN:

SELECT
FROM Custome rs
WHERE rating NOT IN
(SELECT rating
FROM Customers
WHERE city = san Jose');
ANY:
SELECT
FROM Customers
WHERE NOT rating ANY
(SELECT rating
FROM Customers
WHERE city = san Jose');
.

138
ANY, ALL EXISTS

ANY ALL
SQL " ( ), (ANY)
" " ( )
- ". , "
(not equal ALL)" " ,
".

ANY, ALL EXISTS


, EXISTS
, NULL-. ANY ALL
,
, .
, .


ALL ANY ,
.
, ALL "", ANY - "".
, :
SELECT
FROM Customers
WHERE rating > ANY
(SELECT rating
FROM Customers
WHERE city = 'Boston');
, :

SELECT
FROM Customers
WHERE rating > ALL
(SELECT rating

139
13. tr ANY, ALL SOME

FROM Customers
WHERE city = 'Boston');
Customers. Bosto
, .

ANY ALL EXISTS NULL-


NULL-
. SQL , - NULL-,
unknown (. 5). Unknown-pea,
fls-, ,
, ,

ALL ANY EXISTS.


:

SELECT
FROM Customers
WHERE rating > ANY
(SELECT rating
FROM Customers
WHERE city = 'Rome');
:

SELECT
FROM Customers outer
WHERE EXISTS
(SELECT
FROM Customers inner
WHERE outer. rating > inner.rating
AND inner.city = 'Rome');
. ,
Customers NULL- rating:

CNUM CNAME I RAING SNUM


2003 Liu San Jose
NULL 1002
ANY,
rating Mr. Liu ,
NULL- unknown, Liu
. , NOT EXISTS
, NULL- ,
unknown. , -

140
ANY, ALL EXISTS

EXISTS "". ,
, NOT EXISTS . , Mr. Liu
. ,
r , EXISTS - "" "",
unknown.
ANY. NULL-
. , ,
.

COUNT EXISTS
, ANY () EXISTS,
ALL
. ,
EXISTS NOT EXISTS
COUNT(*)
SELECT. ,
EXISTS; , NOT
EXISTS. (
. 13.12):

SELECT *
FROM Customers outer
WHERE NOT EXISTS
(SELECT *
FROM Customers inner
WHERE outer.rating <= inner.rating
AND inner.city = "Rome");
:

SELECT *
FROM Customers outer
WHERE 1 >
(SELECT COUNT(*)
FROM Customers inner
WHERE outer.rating <= inner.rating
AND inner.city = "Rome');
. 13.13.

141
13. 011 ANY, ALL SOME

. 13.12. EXISTS

. 13.13. COUNT EXISTS

142

. - ,
,
. NULL-.

.

SQL,
. .


UNION.

143
SQL
l. , ,
ANY ( SQL) Serres.
2. ?

3. , ANY ALL,
, .

4. , , amount
( ) .

5. .

( . .)

144


UNION
14. 11 11 UNION


" ".
- . UNION
SQL. (nions) , (
) .
, .


UNION. UNION
SQL- .
(sa\espeop\e) (customers)
, :

SELECT snum, sname


FROM Salespeople
WHERE city = 'London'

UNION

SELECT cnum, cname


FROM Customers
WHERE city = 'London;
( . 14,l).
, , ,
. ,
.
.
.
SQL , .

?
, (
UNION), , ,
1 110 (nion compati/e). ,
: , , -

146
11

. 14.1.

.., - , ,
- .. ,
. " "
. ANSI :
. (
ANSI.)
( , ,
).
SQL .
, ANSI , DATE BINARY,
.
. ,

UNION. ,
( ANSI) , .

.

, NULL-
,
. NULL-
NOT NULL, 18.
UNION ,
SELECT (
).

147
14. 11 U1VION

UNION
UNION .
, SQL
DISINCT .
,
. 14.2.:
SELECT snum, city
FROM Customers;

. 14.2.

(1001 London),
SQL ( ). ,
UNION
Salespeople, . . 14.3
:

SELECT snum, city


FROM Customers

UIOON

SELECT snum, city


FROM Salespeople:
( SQL),
UNION ALL UNION:

148
Jt 1~

1001 London
1002 Berlin
1002 San Jose
1003 New York
1003 Rome
1004 london
101/.14 Rome
101/.17 Barcelona
1007 San Jose

. 14.3.

SELECT snum, city


FROM Customers

UNION ALL

SELECT snum, city


FROM Salespeople;

UNION
SELECT,
UNION. ANSI,
.
, .
, , , ,
.

, ,
.
, ,
(
).

149
14. . UNION

SELECT a.snum, sname, onum, 'Highest , odate


FROM Salespeople , Orders
WHERE a.snum b.snum
AND b.amt =
(SELECT (amt)
FROM Orders
WHERE c.odate b.date)

UNION

SELECT a.snum, sname, onum, 'Lowest , odate


FROM Salespeople , Orders
WHERE a.snum b.snum
AND b.amt =
(SELECT MIN (amt)
FROM Orders
WHERE c.odate = b.odate);
. 14.4.
'Lowest on',
'Highest on'. ! ,
5 1990 .,

Peel 3f/Jf/J8 Highest 10,f/J5,199&


Peel 301/JB Lowest 10/85/199"
Peel 3f/J11 Highest 1f/J/f/J6/199f/J
Serres 3f/Jf/J5 Highest 11/J/83/1991/J
Serres 3f/J87 Lowest 1f/J/04/199f/J
Serres 3!1J1 f/J Lowest 1f/J/f/J6/199f/J
Axelrod 3f/Jf/J9 Highest 1f/J/f/J4/199f/J
Rifki 3f/Jf/J1 Lowest 1f/J/f/J3/199f/J
---- i3rO'vJSC 't.J.+--;

. 14.4. (highest) (lowest)

150
ll UNJON ORDER

. ,
, ,

UNION ORDER

.
, - . ,
. ()

. , ORDER
, () .
,
. , , , !
:

SELECT a.snum, sname, onum, 'Highest ', odate


FROM Salespeople , Orders
WHERE a.snum = b.snum
AND b.amt =
(SELECT (amt)
FROM Orders

R1fk1 Lowest
Serres Highest
Serres Lowest
Peel Highest
Peel Lowest
Axelrod Highest on
Serres Lowest on
Peel Highest

. 14.5. ORDER

151
14. 11 11 UNION

WHERE c.odate b.odate)

UNION

SELECT a.snum, sname, onum, 'Lowest on', odate


FROM Salespeople , Orders
WHERE a.snum = b.snum
AND b.amt =
(SELECT MIN (amt)
FROM Orders
WHERE c.odate = b.odate)

ORDER ;

. 14.5.
(ASC) ORDER
, .
:
(ASC DESC),
. 3 ORDER
SELECT. ,
, ,
,
.


,
, .
,

. 11. 11//u. (outer join). ,


.
, ,
. ,
, ,
NULL- snum. ,
same .

, ,
.

, .

152
UNION ORDER

Serres Cisneros
Serres liu
Rifkin NO
Peel Clemens
Peel Hoffman
Motika Clemens
Motika Hof fman
Axelrod NO

. 14.6.

(salespeople) (customers),
, .
, ,
(city), , .
, . 14.6,
:

SELECT Salespeople.snum, sname, cname, comm


FROM Salespeople, Customers
WHERE Salespeople.city = Customers.city

UNION

SELECT snum, sname, . NO , comm


FROM Salespeople
WHERE NOT city = ANY
(SELECT city
FROM Customers)
ORDER 2 DESC;
'NO ' ,
n (
SQL). ,
.

153
14. UNION


.
SELECT
.
, ,
. ,
.
('MATCHED' - 'NO '):
SELECT a.snum, sname, a.city, 'MATCHED'
FROM Salespeople , Customers
WHERE a.city = b.city

UNION

SELECT snum, sname, city, 'NO

FROM Salespeople
WHERE NOT city = ANY
(SELECT city
FROM Customers)
ORDERD 2 DESC;
. 14.7.
,
(unmatched) .
, ,

Serres San Jose


Rifkin Barcelona
Peel London
Motika London
Axelrod New York

. 14. 7.

154
UNION ORDER

. , (
. 14.8):
(SELECT snum, city, 'SALESPERSON - MATCHED'
FROM Salespeople
WHERE city = ANY
(SELECT city
FROM Customers)

UNION

SELECT snum, city, 'SALESPERSON - NO '

FROM Salespeople
WHERE NOT city = ANY
(SELECT city
FROM Customers))

UNION

(SELECT cnum, city, 'CUSTOMER - MATCHED'


FROM Customers
WHERE city = ANY
(SELECT city
FROM Salespeople))

UNION

SELECT cnum, city, 'CUSTOMER - NO '

FROM Customers
WHERE NOT city = ANY
(SELECT city
FROM Salespeople))
ORDER 2 DESC;
( , ANY,
.)
, ,
, ( ).
:

155
.JJ 14. UNION

2553 San Jose CUSTOMER D


21'/Jl/18 San Jose CUSTOMER D
21!11!12 Rome CUSTOMER NO
21!11!17 Rome CUSTOMER NO
, 1!11!13 New York SLESPERSON - D
, l!lf/13 New York SLESPERSON - NO
21!11!11 London CUSTOMER D
2f/lf/16 london CUSTOMER D
21!11!14 Berlin CUSTOMER NO
1l!lf/17 Barcelona SLESPERSON - D
, l!lf/17 Barcelona SlESPERSON - NO

. 14.8.

, .
, ,

query UNION query UNION query Z;


(query UNION query ) UNION query Z;


query UNION (query UNION query Z);


, UNION UNION ALL
.

(query UNION ALL query ) UNION query Z;


,

query UNION ALL (query UNION query Z);


, .

156

, UNION,
.
, .. ,
,
. -
, ,
.

, . -
. ,
, .

157
SQL
1. ,
names, cities, ratings (customers). , (rating)
200 , "High Rating", - "Low
Rating".
2. ,
name number ,
. .

3. .
snums San Jose.
cnums San Jose;
onums 3 1990.
,
( ) .
(: , ,
, .)

( . .)

158
15
m [UJ [1] [1j
,

_. _.

15. , 1 11

,
.
, , .

, ,1
. ,
.
.

DML

(Data Manipulation Language - DML: INSERT (),
UPDATE () DELETE (). SQL
(update commands).

SQL INSERT.
INSERT :
INSERT INTO < >
VALUES (<>,<> ... );
, Salespeople,
:

INSERT INTO Salespeople


VALUES (1001, "Peel", "London',.12);
,
, .
( Salespeople) (
INSERT) CREATE TABLE (. 17),
,
, .
ANSI, . , 3
, 1 + 2 - . , ,
, ,
VALUES INSERT, 1, -
2 ..

160

NULL-
NULL-,
. , city Ms.Peel .
, NULL city,
:
INSERT INTO Salespeople
VALUES (1001, 'Peel', NULL,.12};
NULL , ,
.

INSERT
, ,
. ,
Customers
,

: city,cnae,cnum. ,
. :

INSERT INTO Customers (city, cname, cnum}


VALUES ('London, 'Hoffman, 2001};
rating snu . ,
.
NULL, .
NULL-
" ",
INSERT (
NULL- " " 18).


INSERT ,
, .
VALUES , :

INSERT INTO Londonstaff


SELECT
FROM Salespeople
WHERE city = 'London';
, (..
Salespeople, city = 'London'),

161
15. ,

Londonstaff. ,
Londonstaff :
CREA ABLE.

,
Salespeople : .. , ..
(
).

, ,
,
, Salespeople .
Londonstaff , ,
Salespeople. Salespeople
, ,
Londonstaff ( ,
). , INSERT
, (
SELECT) (
INSERT).
, Daytotals,
. ,
Orders,
. , Orders
, , .
INSERT
:

INSERT INTO Daytotals (date, total)


SELECT odate, SUM (amt)
FROM Orders
GROUP odate;
, Orders Daytotals .
date total -
, INTO .

DELETE.
, ;
, , ,

162
.111 1111 ll

.
Salespeople, :
DELEE FROM Salespeople;

DROP TABLE ( 17).
.
, , , . ,
Axelrod , :

DELEE FROM Salespeople


WHERE snum = 1003;
sum same,
- .
.
, , :

DELETE FROM Salespeople


WHERE city = 'London';

UPDATE
. UPDATE,
, , SET ,
(),
(). , 200,
:

UPDATE Customers
SET rating = 200;

011.
, , .
UPDATE, DELETE, .
rating, ,
Peel (snum = 1001), :

UPDATE Customers
S rating 200
WHERE snum 1001;

163
15. , 11 1111 1111

UPDATE ::
r
UPDATE. SET
, .
, .
. , Motika
(salesperson), ,
:

UPDATE Salespeople
SET sname = 'Gibson, city Boston , comm . 10
WHERE snum = 1004;
Motika
Gibson, Motika snum.

,
SET. .. :

... S Salespeople.sname = 'Gibson'


UPDATE, :

... S sname = Gibson ...

J1 UPDATE
SET UPDATE ,
VALUES
INSERT, .
. , .
:

UPDATE Salespeople
S comm = comm2;

SET,
,
( ).
UPDATE. ,

:

UPDATE Salespeople
SET comm = comm2
WHERE city = 'London';

164

UPDATE NULL-
SET . NULL
- (, IS
NULL). ,
(city = 'London') NULL-,
:

UPDATE Customers
SET rating NULL
WHERE city London ;

( NULL-).


. INSERT
, DELETE - , UPDATE -
. UPDATE DELETE
, .
INSERT,
, . ,
INSERT
INSERT.
. , , " ",
, . NULL-.
, UPDATE
, INSERT - .

, . 16

DML.

165
. SQL
1. ,
Salespeople: city - San Jose, name - Blanco, comm - NULL,
cnum - 1100.
2. , Clemens
Orders.
3. , Rome
100.
4. Serres .
Motika.

( . .)

166




16. ll ll

, .
. ,
SELECT, , ,
.
SELECT, ,
.
INSERT,
.

, ,
, FROM
, .
. ,
, ,
SQL,
. ,
, .. .

INSERT
INSERT . ,
.
, INSERT ,
- HAVING.
, SJpeop\e,
Salespeople. ,
, , (Customers), (city)
San Jose:
INSERT INTO SJpeople
SELECT *
FROM Salespeople
WHERE city = san Jose;
,
SJpeople , San Jose,
:

INSERT INTO SJpeople


SELECT *
FROM Salespeople
WHERE snum = ANY
(SELECT snum
FROM Customers

168
1t 1t JNSERT

WHERE city = san Jose');


,
INSERT. San Jose
snum.
Salespeople, snum.
SJpeople Rifkin Serres,
San Jose Liu Cisneros.

( )


. Serres San Jose , ,
.
, San Jose. -
SJpeople,
, ( )
. - .
,
( EXISTS, IN, <>ALL ..) ,
SJpeople FROM
, , (
), . INSERT,
,
, , ,
INSERT . ""
, INSERT .

,
INSERT, ,
(), FROM SELECT. ,
r1 INSERT,
,
, .
, Samecity,
(salespeople), (cities).
, :

INSERT INTO Samecity

169
16.

SELECT
FROM Salespeople outer
WHERE city IN
(SELECT city
FROM Customers inner
WHERE inner.snum = outer.snum);
Salespeople, Samecity
INSERT. : ,
, .
Bonus, snum
, (odate) (amount, amt).
, Orders, :
INSERT INTO Bonus
SELECT snum, odate, amt
FROM Orders
WHERE amt =
(SELECT (amt)
FROM rde rs
WHERE a.odate = b.odate);
, , ,
, Bonus,
. , .
Orders, .1
. amt
, , Bonus.

DELETE
DELETE .
,
. , ,
,
London:
DELEE

FROM Customers
WHERE snum = ANY
(SELECT snum
FROM Salespeople

170
1111 1111 DELETE

WHERE city = 'London');


Customers
Hoffman, Clemens ( \) Periera ( Motika).
,
! Motika.
! , r
, .1 - :~ , -

, . ,
, ..
. city
() ,
.
, ,
.
SQL, .
FROM () ,
, -

, .. ,
. , . "1
INSERT ,
, ,
.

DELETE FROM Salespeople


WHERE EXISTS
(SELECT "
FROM Customers
WHERE rating = 100
AND Salespeople.snum = Customers.snum);
AND Salespeople.
,
Salespeople, r .
, 100,
Salespeople. . :
DELEE FROM Salespeople
WHERE 100 IN
(SELECT rating
FROM Customers
WHERE Salespeople.snum = Customers.snum);
,
100.

171
16. 11 11 1111

, ..
, ( \'!
DELETE). ,
, , :

DELEE FROM Salespeople


WHERE snum IN
(SELECT snum
FROM Orders
WHERE amt =
(SELECT MIN (amt)
FROM Orders
WHERE a.odate = b.odate));
DELETE .

. ,
"". ,
. snum , ,
DELETE,
snum 1 Salespeople. (
snum - Salespeople, ,
. , ,
, .)
snum: 1007 - 3 1990 .;
1002 - 4 1990 .; 1001 - 5
1990 . ( , \,
5 1990 ., ).
!, ,
:

DELETE FROM Salespeople


WHERE snum IN
(SELECT snum
FROM Orders
WHERE amt =
(SELECT MIN (amt)
FROM Orders
WHERE a.odate b.odate)
AND 1 <
(SELECT COUNT (onum)

172
VPDATE

FROM rde rs
WHERE a.odate = b.odate));
, ,
1 ,
, , , snum

UPDATE
UPDATE, DELETE, .
, DELETE:
, , ,
. , ,
, ,
:

UPDATE Salespeople
SET comm = comm + .01
WHERE 2 <=
(SELECT COUNT (cnum)
FROM Customers
WHERE Customers.snum = Salespeople.snum);
! Serres, ,
.

,
DELETE. ,
:

UPDATE Salespeople
SET comm = comm - .01
WHERE snum IN
(SELECT snum
FROM Orders
WHERE amt =
(SELECT MIN (amt)
FROM Orders
WHERE a.odate = b.odate));

173
16. 11

DML
,
, . ,
.
, ,
Customers, :

1.
SELECT AVG (rating)
FROM Customers;
200.

2.

DELETE
FROM Customers
WHERE rating < 200;

, .
,
:
.
, INSERT , .
VALUES (
), (
,
). , ,
- :
FROM, ( ).
.
,
FROM ( ;
).
DELETE UPDATE
. ,
.
, , -

174

,
. ,
FROM.
.

175

SQL
1. , Multicust,
Salespeople.
, ,
.

2. , ,
.

3. , 20% ,
$3,000.

( . .)

176
17

17.

- ,
,
, .
, .
, , .
,
,
SQL
. SQL,
(Data Definition Language - DDL),
SQL.
SQL - ,
, ,
. (
),
( ), . ,
,
,

CREATE TABLE
CREATE TABLE,
- , . DML
( ) INSERT (. 15). CREATE L
.
. ,
, . CREATE TABLE:
CREATE TABLE < >

(< > < > [(<>)],

< > < > [(<>)], ... );


VI .
, , ,

NSI-. .
SQL,
( -
, , ). U
.
.,,. . ,
. ,
,
,

178

.
. ,
.

, , - CHAR.
- ,

, .
( NULL-)
. 1, ..
.
, ,
, , .
, .
city,
Salespeople Customers. , ,1 ,
, , ,

- . , Employees, Smith,
m : Smith.Employees. ,
Smith . SQL ,
.
Salespeople:

CREATE TABLE Salespeople


(snum integer,
sname char( 10),
city char( 10),
comm decimal);
, ,
.
, .

(index) - ( )
.
,
, -
.
, .
18 ,
SQL,
.

179
17.

- SQL, .
ANSI ,
.

- ,
. , Customers
2999.
, , ,
, cnum 2999. cnum
, 2999
, .
,

( INSERT DELETE); ,
. ,
.

.
,

, .
,

..

, ,
.
.
:

CREATE INDEX < > ON < > (< >

[,< >] .. );
,
. , ,
, -
. ,
.SQL , ,
. , Customers
snum,
snn Customers.
CREATE INDEX Clientgoup ON Customers(snum);
.

.
. ,
UNIQUE INDEX.

180
, :

cnum, ,
:

CREATE UNIQUE INDEX Custid ON Customers(cnum);


: , cnn
. -
. ,
, ,
.
, cnum
Customers.
.

z
.
. SQL
, . (
) :

DROP INDEX < >;

().

ALTER TABLE ( ), ANSI,


. ,
.
, -
. ,
:

ALTER TABLE < > ADD < >

< > <>;

,
NULL-.
. ;
.
. ,
. -

181
17.

, , ,

ALTER TABLE,
. (
). , -
.
,
. - ALTER TABLE
,
- .
ALTER TABLE ,
, , ,
, . ,
, . ,
, ,
; , SQL,
,
. ,
, .
,
ALTER TABLE
.

ALTER TABLE
,
, INSERT SELECT
* . ,
,
.

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

DROP TABLE < >;

,
, ,
DROP. , -

182

-

ANSI,
. (
ALTER TABLE). ANSI
.

. ,
.
SQL-,
,
ALTER TABLE. DROP TABLE ,
. , ,
.

, .
SQL
.
.
SQL.
.

183
~----------------------------------------

SQL
1. CREA ABLE, Customers.
2. ,
Orders, .

3. onum ,
Orders ? (,
.)

4. ,
, .

5. ,
.
. , .

( . : .)

184
18
[i] ~ [j] Llj) 11 111 [1 1j


31



18.

17 . ,
. (constraints)
, ,
.
, : ,
, (
CREATE L ALTER TABLE).
.

, , " ", -
, ,
INSERT . "
", - NULL-.
, " ", , ,
, .

( ),
, , SQL ,
. -
. ,
(l constraints)
, 111.1 (t const1ai11ts) ,
.



.
, ,
. CREATE TABLE ,
:

CREATE TABLE < >

(< > < > < >,

< > < > < > ...


< > (< >

[,< > ... ] ) ... ) ;


( ,
.) ,

186

, - , .
, .
.

NULL-

NULL- ,
CREATE TABLE, NOT NULL.
.
NULL - , , .
. NULL-,
. ,
. , ,
Customers m .
NOT NULL
( ) , NULL-
. SQL NULL-
.

, Sa\espeople,
NULL- snum sname:
CREATE TABLE Salespeople
(snum integer NOT NULL,
sname char(10) NOT NULL,
city char(10),
comm decimal);
, , NOT NULL,
INSERT .
NULL- ,
( ).
ALTER TABLE
, ,
NOT NULL, . NOT NULL,
.

187
18. :

,
z

17
, .
SQL UNIQUE. -
,
(), .

.
UNIQUE : ,
. ,
.

. ,
, , . ,
.
UNIQUE ,
, .
, NOT NULL,
, NULL-
.
Salespeople:
CREATE TABLE Salespeople
(snum integer NOT NULL UNIQUE,
sname char(10) NOT NULL UNIQUE,
city char( 10),
comm decimal);
sname , ,
Smitl1 , : Mary Smith . Smith.
- snum,
, ;

. , ,
, (candidate
keys) 11.11 (U (unique keys).
.
, UNIQUE.

, , ,
. . , -

188

'', ' ', '' (


).
,
. ,
(customer number) (salespeople number) Customers
. , Customers
:

CREATE TABLE Customers


(cnum integer NOT NULL,
cname char(10) NOT NULL,
city char(10).
rating integer,
snum integer NOT NULL,
UNIQUE (cnum, snum));
UNIQUE
NOT NULL. UNIQUE
cnuin, . cnum
, cnum
snum. ,
snum, :

. ,
UNIQUE ,
.
, ,
, .
, .
, ,
, ..
snum odate . ,
, Salestotal:
CREATE TABLE Salestotal
(snum, integer NOT NULL,
odate, date NOT NULL,
totamt, decimal,
UNIQUE (snum, odate));
:

INSERT INTO Salestotal


SELECT snum, odate, SUM (amt)
FROM Orders
GROUP snum, odate;

189
18.


(PRIMARY )
.
, .
"" SQL? UNIQUE
, .
SQL , .
SQL
PRIMARY .
.

PRIMARY
. UNIQUE,
, (
) . ,

.
(UNIQUE).
NULL-. , ,
UNIQUE, ,
PRIMARY , NOT NULL.
Salespeop\e:
CREATE TABLE Salespeople
(snum intege NOT NULL PRIMARY ,

sname char(10) NOT NULL UNIQUE,


city char(10),
comm decimal);
, UNIQUE .
PRIMARY -
UNIQUE ,
, (,
snae ).

, .
PRIMARY ,
. , (name),
) (\ast ) (
(first
). ,
( )
PRIMAR :
CREATE TABLE Namefield
(firstname h(10) NOT NULL,

190

lastname char(10) NOT NULL,


city char(10),
PRIMARY (firstname, lastname));
, ,
, . ,
Mary Smith . Smith, . ,
, PRIMARY ,
UNIQUE .


,
, , :
, . SQL
, ,
;
.
, ,
.
, "", .
Salespeople. ( comm)
decimal, .1,
, amount,
. , ,
. 14 .14 ,
, 14.0, ..
.
, , comm
1.
CREATE TABLE Salespeople
(snum integer NOT NULL UNIQUE,
sname char(10) NOT NULL UNIQUE,
city char(10),
comm decimal (comm < 1 ) );
.

. ,
London, Barcelona, San Jose New York. ,
,
( city) Salespeople.

191
18.

.
:
CREATE TABLE Salespeople
(snum integer NOT NULL UNIOUE,
sname char(10) NOT NULL UNIOUE,
city char(10)
(city IN ("London, "New York", san Jose, ln")),

comm decimal (comm < 1) );


, ,
.
- , ,
ANSI.
ALTER TABLE,
, . ,
,
. ,
,
CREATE
. .
Orders:
CREATE TABLE Orders
(onum integer NOT NULL UNIQUE,
amt decimal,
odate date NOT NULL,
cnum integer NOT NULL,
snum integer NOT NULL);
DATE , ANSI.
, , ANSI
DATE? , odate ,
("/"), ("-") .
, ,
ASCII, odate (R)
. :
, odate.
, DATE
. , odate R.
odate :
CREATE TABLE Orders
(onum integer NOT NULL UNIQUE,
amt decimal,
odate char(10) NOT NULL (odate LIKE _/ _ _/_ - - _'),

192

cnum integer NOT NULL,


snum integer NOT NULL);
, , ,
.

, .
,
. , .15
(Barcelona). ,
() :

CREATE TABLE Salespeople


(snum integer NOT NULL UNIOUE,
sname char(10) NOT NULL UNIQUE,
city char(10),
comm decimal,
(comm < . 15 OR city = ln') );
, .
,
. SQL
. ,
,
. SQL
, ,
. SQL
.


, .
, :

( (comm=. 15 AND city="London")


OR (comm=. 14 AND city aselona)

OR (comm=. 11 AND city san Jose') .. )


. ,
, "1 (view)
WIH OPION,
. ,
. . WITH
OPION -
(. 21).

193
18. 11 1111

" "

, SQL ,
;
.
NULL. ,
NOT NULL, ,
.

(DEFAULT) ,
, CREATE TABLE, DEFAULT
, , ,
, . ,
(salespeople) - (New York).
New York , ,
Salespeople,
:

CREATE TABLE Salespeople


(snum integer NOT NULL UNIQUE,
sname char(10) NOT NULL UNIQUE,
city char(10) DEFAULT = 'New York',
comm decimal (comm < 1) );
New York ,
; ,
New York.
, , , , Orders,
. .
( ) () ,
, .

NULL-,
. NULL- ( )
, IS NULL,
.
- .
, , , ,
. NULL- , SQL
.
, .
, ,
. ,
, ,
NULL:

194

SELECT *
FROM Customers
WHERE rating <= 100;
, (DEFAULT)
.
rating,
: ,

?
,
NOT NULL.
NULL-,
.
UNIQUE PRIMARY
. ,
. , , ,
.
UNIQUE PRIMARY ( )
.

.
NOT NULL NULL-,
UNIQUE - ,
PRIMARY - , , UNIQUE,
; - ,
. , DEFAULT
,
INSERT. , NULL- ,
DEFAULT NOT NULL.
FOREIGN REFERENCES, 19,
, ,

, , ,
.

195
SQL
1. Orders , onum,
cnum snum, NULL-
date.
2. Sa\espeople ,
10%, NULL- , n snum ,
'' '' (
r, ).

3. Orders, ,
onum cnum, cnum
snum. , NULL-
.

( . .)

196
19
k11~.Jm~
J


19. ;


. , snum Customers snum
Salespeople Orders. .
.
,
, ,
DML.
, ,
, , ,
, . , ,
.


, , (refers to)
(refe1ences) .
. , Customers snum,
, Salespeople.
Orders .
snum cnum Orders.
( ),
-1 (f01eig11 key); , ,
1 (parent key). , snum Customers
, snum, Salespeople -
. cnum snum Orders
, ::~
Customers Salespeople.
; ,


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

198
FOREIGN ( )

, , ,
.


, ,
, .. ( )
( ). ( )
() .
, u .
snum Customers 1001 Hoffman
Clemens. , Salesperson
snum = 1001. ,
Hoffman Clemens? , Salespeople ,
, Hoffman Clemens , !
:
.

,
, , :
.
, . Hoffinan Clemens
\,
.
,
.
, Customers,
Salespeople, . 19.1.
.

FOREIGN (
)

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

199
19.

CUSOMERS TABLE SALESPEOPLE TABLE

cnum cname snum snum sname comm

2001 Hoffman 1001 1001 ] .12


2002 Giovanni 1003 1002 Serres .13
2003 Liu 1002 1004 Motika .11
2004 Grass 1002 1007 Rifkin .15
2006 Clemens 1001 1003 Axelrod .10
2008 Cisneros 1007
2007 Periera 1004

. 19.1. Customers

(
).


FOREIGN CREATE TABLE (
ALTER TABLE}, ,
. ,
. , ,
. ,
,
.

FOREIGN
FOREIGN :

FOREIGN < > REFERENCES < >


[< >]


, ,
. REFERENCES
, . , -

200
11 FOREIGN ( )

(
).
, (
). :

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

Customers, snum
, Salespeople:
CREATE TABLE Customers
(cnum integer NOT NULL PRIMARY ,
cname char(10),
city char(10),
snum integer,
FOREIGN (snum) REFERENCES Salespeople (snum) );
, ALTER TABLE
CREATE TABLE FOREIGN , ,
,
, .
. ALTER TABLE ,
, , ,
.

FOREIGN
FOREIGN , ,
REFERENCES, FOREIGN ,
REFERENCES,
. :

CREATE TABLE Customers


(cnum integer NOT NULL PRIMARY ,
cname char(10),
city char(10),
snum integer REFERENCES Salespeople (snum) );

201
19.

Customers.snum ,
Salespeople.snum.
:

FOREIGN (snum} REFERENCES Salespeople (snum}


FOREIGN ,
, PRIMARY
. , ,

, , ,
. , PRIMARY
snum Salespeople,
Customers ( , )
:

CREATE TABLE Customers


(cnum integer NOT NULL PRIMARY ,

cname char (10),


city char (10),
snum integer REFERENCES Salespeople};

. .


,
,
, .
, ,
. ,
NULL-. ,

, . SQL ,
NULL- . ,
, ,
, PRIMARY , UNIQUE,
NOT NULL.

202
FOREJGN ( )

PRIMARY UNIQUE

,
. ,
, , ,
. ,
.
, snum Customers,
, snum,
, Sa\espeople, ,
..
, ,
. snn
Customers
Salespeople, :
, , ,
, ..
,
. ,
, ,
.
, ,

,
,
.



NULL-. .
NOT NULL ,
. , ,
.
NULL-,
.

203
19. :

, , , ,
FOREIGN :
CREATE TABLE Salespeople
(snum integer NOT NULL PRIMARY ,

sname char {10) NOT NULL,


city char {10),
comm decimal);

CREATE TABLE Customers


(cnum integer NOT NULL PRIMARY ,
cname char {10) NOT NULL,
city char (10),
rating integer,
snum integer,
FOREIGN (snum) REFERENCES Salespeople,
UNIOUE (cnum,snum) );

CREATE TABLE Orders


(onum integer NOT NULL PRIMARY ,

amt decimal,
odate date NOT NULL,
cnum integer NOT NULL,
snum integer NOT NULL,
FOREIGN (cnum,snum) REFERENCES
CUSTOMERS (cnum,snum) );


, .
cnn snum Orders .
, ,
, Customers. ,

204
11 11 .

UNIQUE Customers,
, . cnum
PRIMARY , , ,
, cnum
.


,
.
- ,
, .
.
, snum cnum Orders
Salespeople Customers
.

snum ,
Orders
. cnum
Customers, Orders Customers
snum ( ,
). -
- ,
, .
, ,
( ), ,
.
(denormalization)
,
.
,
, .

,
DML? , ,
: ,
INSERT UPDATE, .
NULL- , NULL-
, NOT NULL-.
( DELETE) ,
.
, ,
ANSI, , : ,
, . ,
Customers,

205
19. ;

Orders. ,
, . ,
, ,
Customers, .
, Orders, Customers
Salespeople ,
.
,
.

,
ANSI, .
,
, :

( ANSI), ,
(restricted).


; ,
(cascades).

,
NULL (, NULL-
); ,
nll.

,
. INSERT , , .
,
. ,
.
UPDATE DELETE. ,
(update effects) (delete f!f!ects),
, UPDATE DELETE
. , ,
RESTRICTED, CASCADES, NULLS.
ANSI
- -
, . ,
.
, .
, ,
.
, snum
Salespeople , , . (
.

206
111111 11

: .)

, ,
, . -1
UPDATE CASCADES, DELETE - RESTRICTED.
CREATE TABLE Customers
(cnum integer NOT NULL PRIMARY ,

cname char (10) NOT NULL,


city char (10),
rating integer,
snum integer REFERENCES Salespeople,
UPDATE of Salespeople CASCADES,
DELEE of Salespeople RESTRICTED);
\ Salespeople,
, snum Hoffman Clemens
, . ,
snum \ 1009,
Hoffman Clemens .
NULL-.
, . ,
, .

. Orders, :

CREATE TABLE Orders


(onum integer NOT NULL PRIMARY ,

amt decimal,
odate date NOT NULL,
cnum integer NOT NULL REFERENCES Customers,
snum integer REFERENCES Salespeople,
UPDATE OF Customers CASCADES,
DELEE OF Customers CASCADES,
UPDATE OF Salespeople CASCADES,
DELETE OF Salespeople NULLS);
, DELETE NULLS
Salespeople, NOT NULL snum.

207
19.

, ,

, FOREIGN
.
. , Employees ,
"manager". ,
, .
, empno ( employee number - )
, manager (, ) - ,
:

CREATE TABLE Employees


(empno integer NOT NULL PRIMARY ,
name char (10) NOT NULL UNIQUE,
manager integer REFERENCES Employees);
( ,
.) , , :

EMPNO NAME MANAGER


l 003 Terrence 2007
2007 Atali NULL
1688 McKenna 1003
2002 Collier 2007
, Atali,
. Atali, ,
manager NULL.
. , ,
NULL-.
,
( ), ..
, , , ,
. , .1 Salespeople
, Customers,
; CREATE TABLE:
CREATE TABLE Salespeople
(snum integer NOT NULL PRIMARY ,
sname char(10) NOT NULL,
city char(10),
comm decimal,
cnum integer REFERENCES Customers);
CREATE TABLE Customers

208

(cnum integer NOT NULL PRIMARY ,

cname char(10) NOT NULL,


city char(10),
rating integer,
snum integer REFERENCES Salespeople);
(ci1culaity) (coss
1eferencing). SQL ,
. , ,
, . SQL
,
, . ,
, .
, .
, :
, .
, ,
, ;
,
.

,
. ,

. ,
, , 5-
; .
PRIMARY UNIQUE ,
.
, , . , ,
NULL-
,
UPDATE DELETE. CREATE TABLE
. CREATE.
20 - ,
, .
, ,
,
.

209
SQL
1. Cityorders. onum, amt, snum,
Orders, cnum city, Customers,
.
onum Cityorders.
Cityorders
Customers Orders. ,
.

2. . Orders :
prev, onum
. ,
, ( Orders).
cnum ,
, .

( . 11 .)

210
20
1]


20.

(view) - , .
,
. ,
.

, "", , -
.
, .
, ,
.

?
, , .111
.11 (base ta/es). , .
, "" (views). -
, .
DML , ,
. ,
, () .
, ,
.
.

CREATE VIEW
CREATE VIEW,
CREATE VIEW ( ),
AS, .
:

CREATE VIEW Londonstaff


AS SELECT *
FROM Salespeople
WHERE city = 'London;
,
Londonstaff.
: , , ,
.
(. . 20.1):

212
CREATE VJEW

. 20.1. Londonstaff

SELECT
FROM Londonstaff;
,
Londonstaff, .
,

, { ).

, .
, ,
. ,
(city = 'London'), n-
.

.
, ,
. ,
Sa\espeople, ,
( . 20.2):
CREATE VIEW Salesown
AS SELECT snum,sname,city
FROM Salespeople;

213
20. 11

. 20.2. Salesown

, , Salespeople,
comm, , ,
.


DML,
,
:

UPDATE Salespeople
SET city = 'Palo Alto'
WHERE snum = 1004;

Salespeople. ,
UPDATE:
UPDATE Salesown
SET comm = .20
WHERE snum = 1004;

214
CREATE VIEW

, m. ,
. 21.



, .
.
:

, ,
.

, ,
. , ,
. .
,
.

,

- .
.
Londonstaff:
CREATE VIEW Londonstaff
AS SELECT
FROM Salespeople
WHERE ci ty = . London. ;
:

SELECT
FROM Londonstaff
WHERE comm > . 12;
, Salespeople:
SELECT
FROM Salespeople
WHERE city = "London

215
20. 11

AND comm > . 12;


, .
,
. , (CREATE) :

VIEW Ratingcount (rating, number)


AS SELECT rating, COUNT ()
FROM Customers
GROUP rating;

.
, -
:

SELECT
FROM Ratingcount
WHERE number = 3;
, :

SELECT rating, COUNT ()


FROM Customers
WHERE COUNT () 3
GROUP rating;
. ,
COUNT, .
:

SELECT rating, COUNT ()


FROM Customers
GROUP rating;
HAVING COUNT () = 3;
SQL .
Ratingcount ? . SQL,
.
, , -
. ,
, SQL
.

216
11 CREATE VIEW


(grouped vie11's) - ,
Ratingcount , GROUP
.

. ,
, , ,
, ,
. , ,
:

CREATE VIEW Totalforday


AS SELECT odate, COUNT (DISTINCT cnum), COUNT (DISTINCT snum),
COUNT (onum), AVG(amt), SUM(amt)
FROM Orders
GROUP odate;

:

SELECT *
FROM Totalforday;
SQL , , .1,
,
. ,
.


,

. , ,
:

VIEW Nameorders
AS SELECT onum, amt, a.snum, sname, cname
FROM Orders , Customers , Salespeople

WHERE a.cnum = b.cnum


AND a.snum = c.snum;
,
. , ,
( . 20.3):

217
20.

. 20.3. Rifkin, Nameorders

SELECT *
FROM Nameorders

WHERE sname = 'Rifkin';


, ,
; Axelrod
:

SELECT a.sname, cname, amt * comm

FROM Nameorders , Salespeople

WHERE a.sname = xelrod'


AND b.snum = a.snum;
. 20.4.
WHERE a.sname = 'Axelrod' AND b.sname ""
'Axelrod', .
, Axelrod. ,
snum Salespeople, , ,
. Axelrod,
name .
snum, .

218
CREATE VIEW

. 20.4.


,
. , ,
.
:

CREATE VIEW Elitesalesforce


AS SELECT b.odate, a.snum, a.sname,
FROM Salespeople , Orders

WHERE a.snum = b.snum


AND b.amt =
(SELECT (amt)
FROM Orders
WHERE c.odate = b.odate);
,
1 ,
, :

CREATE VIEW Bonus


AS SELECT DISTINCT snum, sname

219
20.

FROM Elitesalesforce

WHERE 10 <=
(SELECT-COUNT ()
FROM Elitesalesforce

WHERE a.snum = b.snum);


, , ,
:

SELECT
FROM Bonus;
.1 RPG COBOL,
. SQL
,

. , , -
, , ,
.

?
( ,
), . ,
,
( 21).
." ,
:
; UNION UNION ALL . ORDER
. ,
, .

DROP VIEW < >

,
, ,
. ,
, .
, .

220

. ,
, .
, , .
, ,
.
, , - ,
.
21 .

221
. SQL
l. ,
.

2. , .

3. ,
. ,
.

4. ,
.

( . .)

222


21. 11

DML - INSERT, UPDATE


DELETE - . ,

, ,
. .
, ,
, .
WIH OPION,
, , .

.


DML.
, .
,
. (
), , , ,
, .
, . 21.1:
CREATE VIEW Citymatch (custcity, salescity)
AS SELECT DISTINCT a.city, b.city
FROM Customes , Salespeople

WHERE a.snum = b.snum;


,
, custcity,
salescity.
, - Lodon London - ,
, , ,
. Hoffan \,
. , Clemens,
, Peel.
DISTINCT,
.
?
DISINCT,
, ,
,

224

( 11 t ( 1 t "11 (. l t

Berlin San Jose


London London
Rome London
Rome New Vork
San Jose Barcelona
San Jose San Jose

. 21.1. Citymatch

( , , ORDER
, .
, ;

ORDER .
, ). ,
, .
, London London ?
Customers Hoffman Clemens,
? SQL ! Salespeople?
,

. Citymatch
, - .

1~
, ,
(updata!e);
(read-only).
" " ("updating view"),
, DML
(INSERT, UPDATE DELETE), .

225
21. 11

, ?
. ,
,
, , ,
- -
. .
, , , r
SQL. ,
SQL:

(
ANSI , ).

,
.

DIS'FINCT .

GROUP VING .

( ANSI,
).

,
.

, (, comm *
100) .

INSERT
, NOT NULL,
.


,, ,,

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

226
11

. (
DISINCT).
"
" . .
.1 , .
, :
.
,
- . ( 22 ,
, , .)
, " ",
.
,
, , .
, ,
(, ),
. " "
, . , ,
, , ,


" ":

CREATE VIEW Dateorders (odate, ocount)


AS SELECT odate, COUNT ()
FROM Orders
GROUP odate;
" ",
GROUP .

CREATE VIEW Londoncust


AS SELECT *
FROM Customers
WHERE city = 'London';
.
CREATE VIEW SJsales (name, number, percentage)
AS SELECT sname, snum, comm * 100

227
21.

FROM Salespeople
WHERE city = san Jose;
,
'comm *
100'. .

sname snum.
CREATE VIEW Salesonthird
AS SELECT *
FROM Salespeople
WHERE snum IN
(SELECT snum
FROM Orders
WHERE odate = 10/03/1990);
" " ANSI,
. .
CREATE VIEW Someorders
AS SELECT snum, onum, cnum
FROM Orders
WHERE odate IN (10/03/1990, 10/05/1990);
.

, , ,
, ""
. :
CREATE VIEW Highratings
AS SELECT cnum, rating
FROM Customers
WHERE rating = 300;
.
. , :

INSERT INTO Highratings


VALUES (2018, 200);

228
, 1111

INSERT .
Highratings Customers.
, 300.
. 200,
Customers .
, , ,

.
, WIH OPION
. WIH OPION
Highrating:
CREATE VIEW Highratings
AS SELECT cnum, rating
FROM Customers
WHERE rating = 300
WITH OPTION;
.
WIH OPION " ".
, DML,
, .
, WIH OPION
, , ,
, ,
.



, ,
. , Lodonstaff
:

CREATE VIEW Londonstaff


AS SELECT snum, sname, comm
FROM Salespeople
WHERE city = "London;
, city,
, , city
?
? city,
, , NULL (NULL ,
). , ,
, -

229
21.

, , Londonstaff.
Londonstaff Salespeople,
(
'London', - ).
, , ,
, .
WIH OPION :
CREATE VIEW Londonstaff
AS SELECT snum, sname, comm
FROM Salespeople
WHERE city = London
WITH OPTION;
. :
, , () .
.
, ,

.
,
. .
. Londonstaff :
CREATE VIEW Londonstaff
AS SELECT
FROM Salespeople
WHERE city = 'London'
WITH OPTION;

city,
:

SELECT snum, sname, comm


FROM Londonstaff:

,

WITH OPTION ANSI.
; ,
, ,
. , :

CREATE VIEW Highratings


AS SELECT cnum, rating

230
1111, 1111 ll

FROM Customers
WHERE rating = 300
WITH OPTION;
, 300,
. (
), :

CREATE VIEW Myratings


AS SELECT
FROM Highratings;
,
300:

UPDATE Myratings
S rating = 200
WHERE cnum 2004;
.
WIH OPION ,
,
. ,
, ,
WIH OPION .
, ,
. , Myratings
:

CREATE VIEW Myratings


AS SELECT
FROM Highratings
W OPION;

. WIH OPTION
Myratings. Myratings
, WIH OPTION .
,
Myratings, , Highratings
.
SQL,
. ,
, . (
,
.)

231
21. 1111

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

, " "
SQL-, ,
, .
, , ,
,
.

WIH OPION

, ,
, WIH OPION
, .

,
. ,
, ,
, ( WITH
OPION ).
.
.
, ,
. 22
SQL.

232
SQL
1. ?
#1 CREATE VIEW Dailyorders
AS SELECT DISTINCT cnum, snum, onum, odate
FROM Orders;
#2 CREATE VIEW Custotals
AS SELECT cname, SUM (amt)
FROM Orders, Customers
WHERE Orders.cnum = customers.cnum
GROUP cname;
# CREATE VIEW Thirdorders

AS SELECT *
FROM Dailyorders
WHERE odate = 10/03/1990;
#4 CREATE VIEW Nullcities
AS SELECT snum, sname, city
FROM Salespeople
WHERE city IS NULL
OR sname BEWEEN AND z;
2. Salespeople Comissions.
snum comm.
,
.10 .20.
3. SQL ,
, "CURDATE". CURDATE
SQL- ,
SELECT INSERT.
Orders, Entryorders
Orders. Orders
, CURDA odate,
. Entryorders ,
.

( .)

233
22
lWWrlQi]~



22. ll ocmylla

. 2, SQL
,
.
.
, , , .
(privileges) - ,
. ,
.
SQL - GRANT REVOKE,
.

SQL :
. , , ANSI,
(authorization ID).
, , , ..
. SQL
- , SQL
USER ID, .
( ) , ID
.

r ,
. ,
ID . , ,
ID, , SQL,
.
ID, .
SQL : ID.
SQL ,
,
( , ),
.
ID,
USER.

236
1t

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


SQL, ANSI, 1
(object privileges). ,
.
, ,
, SQL.
, .

, . , , ,
. ,
.
, :

SELECT
.

INSERT
INSERT .
UPDATE
UPDATE .
.

DELETE
DELETE .
REFERENCES
,

237
22. ocmylla

.
. (. 19
.)

, , INDEX
( ), SYNONYM (
, . 23) ALTER ( ALTER
TABLE). SQL
GRAN.

GRANT
, Diane Customers
Adrian .
Diane :

GRANT SELECT ON Customers Adrian;


Adrian Customers.
, - ,
Customers ( Customers
, ,
Customers).
SQL GRAN, ,
GRANT .
Adrian ,
SELECT : Dian (
, Dian SELECT
Adrian).
. Adrian
Sa\espeople, Dian
; :

GRANT INSERT ON Salespeople Diane;


Diane .

, .

GRANT.
, , . , Stephen
SELECT INSERT Orders
Adrian:
GRANT SELECT, INSERT ON Orders Adrian;
Adrian Diane:
GRANT SELECT, INSERT ON Orders Adrian, Diane;

238


.
ANSI, ,
,
, ,
.

.
, UPDA
REFERENCES, , ,
. UPDATE
:

GRANT UPDATE ON Salespeople Diane;


Diane
Salespeople. , Adrian Diane
( comm),
:

GRANT UPDATE (comm) ON Salespeople Diane;


.1
, UPDATE.
; :

GRANT UPDATE (city, comm) ON Salespeople Diane;


REFERENCES .
REFERENCES , ,
.
UPDATE, REFERENCES
. , Diane Stephen
Customers
:

GRANT REFERENCES (cname, cnum) ON Customers Stephen;


Stephen cnum cname
. Stephen
.
(cname, cnum) , , (cnum, cname)
, ,
.
, , Diane
(. 19).
, ,
.
UPDATE, ,
, .
Adrian Diane :

239
22.

GRANT REFERENCES ON Salespeople Diane;


, ,
, .

ALL PUBLIC
SQL GRANT,
: ALL PRIVILEGES ( ALL) PUBLIC. ALL
GRANT
. , Diane Stephen
Customers :

GRANT ALL PRIVILEGES ON Customers Stephen;


( UPDATE REFERENCES .)
:

GRANT ALL ON Customers Stephen;


PUBLIC - , ,
. ""
(PUBLIC), .
SELECT ,
.
Orders , ,
:

GRANT SELECT ON Orders PUBLIC;


,
. , SELECT,
(, REFERENCES, )
.
.
, .
PUBLIC .
, ,
, PUBLIC.
- , -
, , SELECT.

GRANT OPTION
,
. ,
,

240

,
. SQL WITH GRANT
OPTION.
Diane , Adrian
Customers , Adrian
SELECT WIH GRAN
OPTION:
GRANT SELECT ON Customers Adrian
WIH GRANT N;

Adrian
SELECT . :
GRANT SELECT ON Diane.Customers Stephen;
:

GRANT SELECT ON Diane.Customers Stephen


WITH GRANT OPTION;
GRANT N
, ,
GRAN OPION ,
: . (
,
.
.) GRANT N
.

ANSI
CREATE TABLE ,
DROP TABLE ,
GRAN ,
.
REVOE, -
.

REVOKE GRAN,
. , Adrian
INSERT Orders :

REVOKE INSERT ON Orders FROM Adrian;


, GRANT,
:

241
22.

REVOKE INSERT, DELETE ON Customers FROM Adrian, Stephen;


? ,
, , ,
? ,
.
: , ,
, ..
, ,
.



, .
,
, UPDATE REFERENCES,
. , ,
, ,
, , ,.
GRANT.
? , ,
SELECT ,
. ,
INSERT, UPDATE DELETE ,
, .
, ,
, .
,
REFERENCES .
ANSI. (
). ,

.

select . ,
Claire snum
sname Salespeople. ,
:

CREATE VIEW Clairesview


AS SELECT snum, sname
FROM Salespeople;
Claire r SELECT , Salespeople:

242
11

GRANT SELECT ON Clairesview to Claire;


,
, INSERT ,
DELETE .
UPDATE REFERENCES
.

. , ,
. ,
UPDATE ( Customers),
(city = 'London'), Adrian, :
CREATE VIEW Londoncust
AS SELECT
FROM Customers
WHERE city = 'London
WITH OPTION;
UPDATE Adrian:
GRANTUPDATE ON Londoncust Adrian;
UPDATE
, Customers, city
'London'. WIH OPTION Adrian
city.

.
,
. .
, , amt
, :

CREATE VIEW Datetotals


AS SELECT odate, COUNT (), SUM (amt), AVG (amt)
FROM Orders
GROUP odate;
Dine SELECT
Datetotals:
GRANT SELECT ON Datetotals Oiane;
.
, 18, .1
WIH OPION . ,

243
22. .

, city Salespeople
- ,
. city ,
, .
,
city:

CREATE VIEW Curcities


AS SELECT
FROM Salespeople
WHERE city IN ("London, 'Rome', 'San Jose, erlin')

WITH OPTION;
, Salespeople,
Curcities.
:
,

, , . ,
Salespeople ,
.
, ~:
, .1,
, . ,
.
. ,
, :

CREATE VIEW Othercities


AS SELECT
FROM Salespeople
WHERE city NOT IN ('London, 'Rome', 'San Jose', 'Berlin')
WITH OPTION;
SELECT
,

.
,
.

244
11 11

ANSI.
( NSI-
), . ,
,
.
. ,
?
? 1111 (sperusers) -
,

?
ANSI , SQL
,

.
.
, ,
. (system privileges)
(database athorities).
,
( )
( ).
, ,
, ANSI. ,
. ,
, - (DBA -
Database Administrator).

,
CONNECT, RESOURCE DBA. CONNECT
(. 23),
. RESOURCE .
DBA - ,
( ).
( )
. (
SYSADM SYS), ;
, DBA.
SYSADM ,1.
.
( ),
DBA,
, .

245
22. 01l llpa ocmylla

GRANT ,
. DBA.
, DBA
Rodriguez :
GRANT RESOURCE Rodriguez;

Rodriguez?
? DBA
, CONNEC.
IDENIFIED , .
, DBA :

GRANT CONNECT Thelonius IDENTIFIED Redwagon;


Thelonius,
Redwago. , Theloious
, DBA ~~
Redwago. .
, .
, REVOKE
CONNECT, .

.

CONNECT
. DBA.
, ,
RESOURCE. .
CONNECT , ,
,
, ,
. ,
CONNECT. ,
INSERT, .
RESOURCE , CONNECT
.

,
. ,
.
SQL,
, ,
SQL. ,
DBA .

246

,
.

SQL , :
SQL
. GRANT :
:v~ .
WIH GRANT OPION, ,
, .

,
. , ,
SQL, .
23 : ,
. ,

247
SQL
1. Janet (ratings) .

2. Stephen
Orders.

3. INSERT Salespeople
Claire , Claire
.

4. Jerry Customers
, 100 500.
5. Janet Customers,
(Customers), .

( . .)

248
23
~
~

z
acnemSQL
23. SQL

SQL,
, ,
, ,
,
. ,
, ,
, .

,
,
; SQL , .
,
SQL (
SQL). (synonym)- ,
. , ,
(
).
, ,
, . ( SQL
.) Adian
, , Clients Diane.Customers, CREATE
SYNONYM :

CREATE SYNONYM Clients FOR Diane.Customers;


Adrian Clients ,
Diane.Customers. Clients Adrian,
.

n :
SQL .
,
SQL . , ,
, ,

( SQL). ,
, ,
,
, ,

. ,1 , -

250

. ,
N
( 22). ,
. , Adrian
Customers Diane.Customers
:

CREATE SYNONYM Customers FOR Diane.Customers;


SQL, :
Diane.Customers Adrian.Customers.
Customers.

Customers ,
,
. , ,
. ,
(pulic) . ,
Customers Customers, :

CREATE PUBLIC SYNONYM Customers FOR Customers;


Customers,
.
,
, DBA.
Customers , . ,

, .
PUBLIC, , .


DROP
SYNONYM. ,
, DBA.
Client -
Customers, - Adrian

DROP SYNONYM Clients;


, Customers
.

251
23. ~ SQL


, . ,
" , ",
, .
11.-
,
, .
(, )
. , SQL ,
,
.

SQL- ( ,
SQL) , 11.-1
(databasespaces) .-1 (ta/espaces). ~
,
rn ,
. ,
SQL. ,
( dbspaces),
SQL. Dbspaces
CREATE DBSPACE, ACQUIRE DBSPACE CREATE TALESPACE
. dbspace
, dbspaces.
m dbspace.
dbspace Sampletaes :

CREATE DBSPACE Sampletaes

{pctindex 10,

pctfree 25);

pctindex dbspace,
. pctfree dbspace,
,
( ALTER TABLE
, .
).
,

252
?

, , , dbspaces
. Dbspace ,
.
dbspace , .
dbspace Sampletaes
. Diane Sampletaes
:

GRANT RESOURCE ON Sampletaes Diane;



. dbspace, ,
,
. , dbspaces, ,
.
dbspaces, ,
. ,
,

dbspace. ,
, Orders
, Orders .
, dbspace,
, .
Orders , dbspace
.

- ,
.1 .
SQL, , ,
(DML) DROP TABLE,
. ,
.
, (t1ansactio11s).
, SQL.
,
COMMIT WOR ROLLBACK WOR.
COMMIT
, , , ROLLBACK
. COMMIT ROLLBACK.
.

253
23. ~ SQL

, -
COMMIT ROLLBACK, :
WORK;
, :

ROLLBACK WORK;
, :
AUTOCOMMIT.
. ,
, :

SET AUTOCOMMIT ON;


:

S AUTOCOMMIT OFF;
AUTOCOMMI (
.)
(,
),
. ,
, ;

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

, Motika. ,
Salespeople, - .
snum Motika NULL.
,
!. Motika Salespeople:
UPDATE Orders
S snum = NULL

WHERE snum = 1004;

UPDATE Customers
SET snum = 1001
WHERE snum = 1004;
DELETE FROM Salespeople
WHERE snum = 1004;
Motika (,
, ), -

254
SQL 11

.
, .
COMMIT
ROLLBAC.

SQL

, SQL,
. ,
Salespeop\e:
UPDATE Salespeople
S comm = comm2

WHERE sname LIKE RX. ;


Diane :

SiLECT city, AVG (comm)


FROM Salespeople
GROUP ci ty;
, Diane, ,
? ,
.
, ,

.
. , ,
Diane .
Diane
, , , , Diane
.


u.1110. (concurrency), . :

.1 . ,
,

. ,
.

255
23. 1 SQL

Or
, :
, Diae .

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

. ,
.

. ,
. ,
,

, ,
. SQL
uJu.111 (111 controls). ,
,
(,
, COMMIT ROLLBACK).
-
.
,
. SQL-
,
.
, DBA ( )
, . SQL,
SQL.
SQL,
, .11.11 (.11) (locks).
,
. , ,
, (
NOWAIT, ,
, -
).
.
, ,
. ,

256
SQL ;

CREATE D8SPACE ALTER D8SPACE , ,


dbspaces. ,
,
. 8 ,
.

,
, 082 I,
. 18
,
.
082 ,
.

: (share locks)
(exclsive locks). ( S-locks, S-
) .
, .
( X-locks, -)
, ,
. ,
.
. .
.
q (isolation level) ? ,
. 8 082 : -
, ,
. ,
SQL. , ,
.
.

" " (read repeatabllity) ,


, , .

, . ,
, ,
, .
.
, , .
(cursor staility)
. -

257
23. ~ SQL

,
. ,
,
, .
.
, , , .
DB2 - " " (read /).
" " ;
. ,
. ,
,

. . "
" , ."
" ,
,
, .


,
.
. - , , 1024 .

, .
, ,
, ,
, .

. SQL
,
. ,
SQL
.

, , - dbspace.
,

. .

11~

258

( )

(dbspace) (
)

(
)

( SQL
)

, () ,
, .
, , ,
, .

Dbspaces - , .
, ,
.
I ROLLBACK - ,
, COMMIT
ROLLBACK , .
,
. ""
.

259
~-------------------------

SQL
1. Myspace, 15
40
.

2. SELECT Orders
Diane. ,
Orders Diane .

3. , ,
?

4. - ,
?

5. ,
, ,
, ?

( .)

260
24
li1 [1] (U []J (] ~


J1u

SQL
24. SQL

, SQL-aa
. ,
.
, , ..
.

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


ANSI,
SQL- .
-: (syste catalog),
(data dictionary) (syste taes). (
,
,
SQL. , ,
.)
SQL:
. ,
,
; ,
.
, SYSTEM.

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

262

SYSTEM DBA.
.


, :

SYSTEMCATALOG ( )

SYSTEMCOLUNS

SYSTEMTABLES SYSTEMCATALOG
SYSTEMINDEXES

SYSTEMUSERAUTH

SYSTEMTABAUTH

SYSTEMCOLAUTH

SYSTEMSYNONS

DBA SYSTEMCATALOG
Stephen :

GRANT SELECT ON SYSTEMCATALOG Stephen;


Stephen
(, Chris ,
, Adrian
Londoncust).
SELECT tname, owner, numcolumns, type,

FROM SYSTEMCATALOG;
. 24. l.
. ,
- ,
;
: ( ), V ( ).
NULL- , ,

V. , .
SYSTEMCATALOG .

. , ,
SYSTEMCATALOG.

263
24. SQL

. 24.1. SYSTEMCATALOG


SYSTEMCATALOG ,
. ,
SYSTEMTALES. SYSTEMCATALOG
, ; (
), Sa\espeop\e, SYSTEMCATALOG,
SYSTEMTABLES. ,
SYSTEM. ,
:

CREATE VIEW Datataes

AS SELECT *
FROM SYSTEMCATALOG
WHERE owner <> . SYSEM';
()
. .
, ,
, . USER
SQL .1,
,

( ).
:

264

CREATE VIEW Owntaes


AS SELECT
FROM SYSTEMCATALOG
WHERE Owner = USER;

:

GRANT SELECT ON Owntaes PUBLIC;


SELECT
SYSTEMCATALOG, ,
.

SYSTEMCOLUMNS.
SYSTEMCOLUNS
, .
SYSTEMCOLUNS, (..
):

tame cname datatype cnumber tabowner


Salespeople sum integer Diane
Salespeople sname char 2 Diane
Salespeople city char 3 Diane
Salespeople comm decimal 4 Diane
Customers cnum integer 1 Claire
Customers cname char 2 Claire
Customers city char 3 Claire
Customers rating integer 4 Claire
Customers snum integer 5 Claire
Orders onum integer Diane
Orders odate date 2 Diane
Orders amt decimal 3 Diane
Orders cnum integer 4 Diane
Orders snum integer 5 Diane
.
,
, ,
. , tname ( ),
tabowner ( .1 ) cname ( )
(primary key) . datatype ( ) -

265
24. 11 SQL

. cnumber (column number- )


( ) . ,
(length), (precision) (scale) .
, ,
.

SYSTEMCATALOG :

tname owner numcolumns type

SYSTEMCOLUMNS System 8
SQL- ,
, .

SYSTEMCOLUNS, (,
):

CREATE VIEW Owncolumns


AS SELECT
FROM SYSTEMCOLUMNS
WHERE tabowner = USER;
GRANT SELECT ON Owncolumns PUBLIC;


SQL
SYSTEMCATALOG
SYSTEMCOLUNS, . ~
.

COMMENT ON ,
. TABLE
SYSTEMCATALOG, COLUN - SYSTEMCOLUNS.
:

COMMENT ON TABLE Chris.Orders


IS 'Current Customer Orders;
SYSTEMCATALOG.
254 .
, , tname = Orders,
owner = Chris. Orders
SYSTEMCATALOG:

SELECT tname, remarks


FROM SYSTEMCATALOG
WHERE tname = 'Orders

266
J1u.~10 m1

AND owner = 'Chris";


. 24.2.
SYSTEMCOLUNS . :

COMMENT ON COLUMN Orders.onum


IS 'Order #';
SYSTEMCOLUMNS:
SELECT cnumber, datatype, cname, remarks
FROM SYSTEMCOLUMNS
WHERE tname = 'Orders
AND tabowner = 'Chris"
AND cname = 'onum';
. 24.3.
COMMENT ON
. .
. :

COMMENT ON COLUMN Orders.onum


IS '';
.

. 24.2. SYSTEMCATALOG

267
24. SQL

. 24.3. SYSTEMCOLUNS

SYSTEMINDEXES -
SYSTEMINDEXES
:

iname ( )

iowner ,

tname ,

cnumber

tabowner , ,

numcolumns

cposition

isunique ( N)

268

. , ,
salesperson, snum Customers:
SELECT iname, iowner, tname, cnumber, isunique
FROM SYSTEMINDEXES
WHERE iname = salesperson;
. 24.4.

. 24.4. SYSTEMINDEXES

SYSTEMUSERAUTH-

SYSTEMUSERAUTH
:

usemame (authorization ID)

password

resource RESOURCE
dba DBA
, ,
22: CONNECT, RESOURCE DBA.
CONNECT , . -

269
24. SQL

. 24.5. , RESOURCE

resource dba (
r) N ( ).
. ,
,
.

. , ,
RESOURCE, , DBA,
:

SELECT username, dba


FROM SYSTEMUSERAUTH
WHERE resource = v;
. 24.5.

SSU- ,

SYSTEMTABAUTH :

270

username ,

grantor ,

tname ,

owner tname
selauth SELECT
insauth INSERT
delauth DELETE
(
auth); , N G. G ,
.
N.
.
, tname, owner ( ,
, ,
), user grantor .
( ),
. UPDATE
REFERENCES ,
.
, ,
.
.

. , SELECT, INSERT
DELETE, Adrian Customers,
( . 24.6):
SELECT userame, selauth, insauth, delauth
FROM SYSTEMTABAUTH
WHERE grantor = 'Adrian'
AND tname = 'Customers';
, Adrian Claire NSERT SELECT
Customers,
. Norman SELECT, INSERT DELETE,
. Claire DELETE
Customers - ,
.

271
24. 1r SQL

. 24.6. , Adria

SYSTEMCOLAUTH- ,

usemame ,

grantor ,

tname ,

cname , m

owner tname
updauth UPDATE

refautl1 REFERENCES

updautl1 refauth , N G;
N.
.
SYSTEMTABAUTH cname,
, .1 -

272
z

.
, ,
, .
SYSTEMTABAUTH,
, .

. ,
REFERENCES, (
. 24.7):

SELECT w, tname, cname


FROM SYSTEMCOLAUTH
WHERE refauth IN ('', 'G')
AND username = USER
ORDER 1, 2;
, , ,
, ,1 (
).

. 24. 7. ,

273
24. SQL

SYSTEMSYNONS -
SYSTEMSYNONS :

synonym

synowner ,
( PUBLIC)
tname ,

tabowner ,

. , Adrian Clients
Customers, Diane,
Custoers.
Customers ( . 24.8):

SELECT
FROM SYSTEMSYNONS
WHERE tname = 'Customers';

. 24.8. Custoer

274

.
, .
, (
. 24.9):
SELECT a.tname, a.cname, iname, cposition
FROM SYSTEMCOLUMNS , SYSTEMINDEXES

WHERE a.tabowner = b.tabowner


AND a.tname = b.tname
AND a.cnumber = b.cnumber
ORDER 3 DESC,2;
Customers
Salespeople. salesno,
snum; ,
( , ) iname.
custsale .

. 24.9.

275
24. 11 SQL

sum cnum Customers, , snum


, cposition.
.
, :

SELECT *
FROM SYSTEMCOLUMNS
WHERE tname IN
(SELECT tname
FROM SYSTEMCATALOG);
,
.
SYSTEMTABCOLS,
SYSTEMTABLES.

, SQL- ,
. ,
. ,
SYSTEMCATALOG SYSTEMCOLUMNS ( ).
- ,
.
SQL .
SQL
, ;

276
-----------------------------------,

SQL
1. ,
, , ,
.

2. ,
. ,
, ,

3. ,
.

( : .)

277
25
[ [[] [] [i] [] ~ u

SQL
,

( SQL)
25. SQL

SQL
, .
SQL .
SQL ,
.
Pascal, ()
ANSI.

SQL?
SQL ,
, SQL
SQL , .
. SQL
, , SQL,
.
(host program) (
, ).

SQL ?
SQL. SQL
, SQL
SQL-. , q
, if ". the, for ... do
while ... repeat, SQL , :
, ,
. , SQL
,
, -
.

. ,
, , ,
, ,
, . ,
.

, (,
).

280
11 SQL?

SQL -
, ,
SQL,
, r
, r.

SQL?
SQL
, SQL ( EXECute SQL -
SQL). ,
SQL. , ANSI SQL
, (module},
SQL, .1 ,
. SQL,
,
SQL , - ,
ANSI. (
), SQL :
COBOL, Pascal, FORTRAN, PL/I.
.

SQL , -
, nW1 (precompile)
. , J1mwz)
. - (p1ecompiler p1eprocess01),
SQL , .
, , I
.
,
ANSI, SQL-.
( )
. ,
SQL.
,
( ) (
-
).
SQL ,
.
, (access module).
,
SQL-. SQL-
, .

281
25. SQL

, SQL,
. ,
, SQL,
. SQL
, .
, CONNECT ,
.


cSQL
SQL
.
. ANSI SQL
- PUI, Pascal, COBOL, FORTRAN (
).
. , DATE, ANSI; ,
.
, , SQL.

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

SQL DECLARE SECTION ( -~: );

SQL (,
, );

, SQL-
, , SQL- ;

(:),
SQL-.

SQL
,

, .
, , id_num,
salesperson, loc, comm. ,
Salespeople. SQL-:
SOL INSERT INTO Salespeople
VALUES (:id_num, :salesperson, :loc, :comm)

282
11.1111 SQL

. comm
, . ,
. ,
,

SQL . Pascal
PLil - ; COBOL - END-EXEC; FORTRAN
.
,
SQL Pascal. Pascal SQL
.
,
,

while not end-of-file (input) do


begin
readln (id_num, salesperson, loc,comm);
SQL INSERT INTO Salespeople

VALUES (:id_num, :salesperson, :loc, :comm);


end;
Pascal ,
,
Salespeople, ,
, .
, Enter
( ) ( Pascal, readln
).
.
,
, ( ),
.12:
while not end-of-file (input) do
begin
readln (id_num, salesperson, loc, comm);
if comm >= .12 then
SQL INSERT INTO Salespeople

VALUES (:id_num, :salesperson, :loc, :comm);


end;
, comm >= .12,
. , ,
SQL.

283
25. SQL


, SQL-,
SQL DECLARE SECTION
. ,
,

.
SQL BEGIN DECLARE SECTION END DECLARE SECTION,
, , SQL. ,
, :

SQL BEGIN DECLARE SECTION;


Var
id-num: integer;
Salesperson: packed array (1 .. 10) of char;
loc: packed array ( 1 .. 10) of char;
comm: real;
SQL END DECLARE SECTION;

, Pascal, ,
Var - ,
( ) ( ) ; VI -
, ,
(, !
l()).
- Pascal, SQL.


SQL-, SQL
.
- SELECT,
INTO. ,
, !, Salespeople
:

SQL SELECT snum, sname, city, comm


INTO :id_num, :salesperson, :loc, :comm
FROM Salespeople
WHERE snum = 1001;
(, )
, INTO ,

284
SQL

. , lNTO,
;

.

INTO,
. lNTO 1
: .
,

.
. SELECT INTO
, :

,
, , . ,
, , - ,

(. 17 18);

,
GROUP ;

SELECT DISINCT ,
( ,
), :

SQL SELECT DISTINCT snum


INTD :salesnum
FROM Customers
WHERE snum =
(SELECT snum
FROM Salespeople
WHERE sname = otika');
, Salespeople.snae Salespeople.snn
Salespeople Customers.snum
, Salespeople.snum, ,
.

, ,
- ,

. .
,
.
, ,

285
25. SQL

SELECT INTO. ,
. .


SQL
, - , ,
. ,
.
,
. ,
.
, ?
.

- .
SQL - , ,
, .
, SQL ""
- .
(cursor) - , .
- , .
, .
DECLARE CURSOR :

SQL DECLARE CURSOR Londonsales FOR


SELECT
FROM Salespeople
WHERE city = "London;
, .
, ,
- , . ,
, ,
, , ... .
ORDER ,
, , SQL-
.

, ,
:

SQL OPEN CURSOR Londonsales;


,
DECLARE FETCH,
FETCH:

286
SQL

SQL F Londonsales INTO : id_num, : salesperson, : loc, : comm;


,
, . FETCH
.
FETCH , ,
, .::~

. ,

, (
):
Look_at_more := True;
SQL OPEN CURSOR Londonsales;

while Look_at_more do
begin
SOL F Londonsales

INTO :id_num, :Salesperson, :loc, :comm;


writeln (id_num, Salesperson, loc, comm);
writeln (' ? (Y/N)');
readln (response);
if response = 'N' then Look_at_more .- False;
end;
SOL CLOSE CURSOR Londonsales;

Pascal := " ", =


. writeln
.
, writeln r if ". then.
Pascal SQL.
:
Look_at_more true (),
, .
. :
. N (),
, . Look_at_
r response Boolean char
Pascal,
SQL. CLOSE CURSOR,
CLOSE CURSOR.
,

OPEN CURSOR ,
.
, , -

287
25. SQL

. SQL ,
. ,

, .


.
FETCH, FETCH ,
INTO. , ,
,
, , N.

SQLCODE
, ;
.
, SQL- .
SQLCODE (SQLCOD - FORTRAN) .
,
SQL (. ).
SQL , SQL.
:

1. , .
:

) SELECT ,
;

) FETCH ,
, , ,
;

) INSERT ,
( , , ,
, ,
);

d) UPDATE DELETE ,
, , , ,1,
.

SQLCODE 100.
2. , 1
, SQLCODE .

288
SQLCODE

3. . ,
, . SQLCODE
, SQL-
. -
. ,

, .
,
,
.

SQLCODE

, ,
, :
Look_at_more := True;
SQL OPEN CURSOR Lodosales;
while Look_at_more
d SQLCODE = do

begi

SQL F Lodosales
INTO :id_um, :Salesperso, :loc, :comm;
writel (id_um, Salesperso, loc, comm);

writel (' ? (Y/N)');


readl ( respose);
if respose = 'N' the Look_at_more .- False;
d;

SQL CLOSE CURSOR Londosales;

WHENEVER
,
, ,
3. SQL GOTO. ,
,
GOTO ,
SQLCODE.
WHENEVER. :
SQL WHENEVER SQLERROR GOTO Error_hadler;

289
25. SQL

SQL WHENEVER NOT FOUND CONTINUE;


SQLERROR - SQLCODE <; NOT FOUND -
SQLCODE = 100. (
SQLWARNING.) Error_handler -
,

, (GOTO
). ,
(label) Pascal,
COBOL ( "").

,
.

CONTINUE ,
SQLCODE. ,
WHENEVER SQLCODE.
,
- .
, INSERT,
, ,
-, ,

, .
:

SQL WHENEVER NOT FOUND GOTO No_rows;


No_rows - , .
,

SQL WHENEVER NOT FOUND CONTINUE;


,
.
, UPDATE DELETE.
,
, . SQL

:

SQL DELETE FROM Customers

290
11

WHERE rating <


(SELECT AVG (rating)
FROM Customers);
,
, , DELETE
. -, , , :

SQL DECLARE Belowavg CURSOR FOR


SELECT *
FROM Customers
WHERE rating <
(SELECT AVG (rating)
FROM Customers);
,
:

SQL WHENEVER SQLERROR GOTO Error_handler;


SQL OPEN CURSOR Belowavg;

while not SQLCODE = 100 do


begin
SQL FETCH Belowavg INTO :, :, :, :d, :;

SQL DELETE FROM Customers

WHERE CURRENT OF Belwavg;


end;
SQL CLOSE CURSOR Belowavg;

WHERE CURRENT OF , DELETE


, . ,
DELETE , ,
. .
, , . ORDER
UNION, ,
, .
, .
FETCH.
UPDATE .
, 300. -, :
SQL DECLARE CURSOR High_Cust AS
SELECT *
FROM Salespeople
WHERE snum IN
(SELECT snum

291
25. SQL

FROM Customers
WHERE rating = 300);
:
SQL OPEN CURSOR High_cust;
while SQLCODE = do
begin
SQL F High_cust
INTO :id_num, :salesperson, :loc, :comm;
SQL UPDATE Salespeople

SET comm = comm + .01


WHERE CURRENT OF High_cust;
end;
SQL CLOSE CURSOR High_cust;

: ,
, UPDATE
. FOR
UPDATE OF < >. High_cust
comm, :
SQL DECLARE CURSOR High_Cust AS
SELECT *
FROM Salespeople
WHERE snum IN
(SELECT snum
FROM Customers
WHERE rating = 300)
FOR UPDATE OF comm;
,
.

NULL - , SQL.
. NULL
,
NULL-, SQL. NULL-
, ,
, : SQLCODE
,

292

. - .
NULL- ,
. ,
, NULL-.

.

, ,
SQL. ,
(numeric) SQL. ,
NULL- ,
.
SQL ,
, .1 ,
INDICATOR.

. NULL-,
.
,
NULL-. , city comm Salespeople
NOT NULL, SQL
Pasca\ integer, i_a i_b. (
, .
, .)
:

SQL OPEN CURSOR High_cust;


while SOLCODE = do
begi

SQL F High_cust
INTO :id_um, :salesperso,
:loc:i_a, :commINDICATOR:i_b;
if i_a >= d i_b >= the
{ NULLS produced}
SQL UPDATE Salespeople
SET comm = comm + .01
WHERE CURRENT OF High_cust;
Else
{ both NULL}
begi

if i_a < the

writel ('salesperso ,id_um, has city');


if i_b < the

293
25. SQL

wri teln ( salesperson , id_num, has commissions );


end;
{else}
end; {while}
SQL CLOSE CURSOR High_cust;
, INDICATOR,
;
.
, UPDATE ,
NULL-. NULL-, else -
, , ,
NULL-. :
, , WHERE
SQL, - ,
.


NULL-
,
NULL- SQL.
, , if ". then,
, ,
NULL.
. , NULL- city
city, _ i_
city, city
, , ;
. , i_city
. ,
if ". then:
if city = 'London' then
comm := comm + .01
else comm := comm - .01;
, city,
'London', . ,
, . SQL
r:

SOL UPDATE Salespeople


S comm = comm + .01
WHERE ci ty = 'London';

294

SQL UPDATE Salespeople


S comm = comm - .01;

WHERE city <> 'London';


( Pascal , SQL
.) city SQL NULL,
unknown () , ,
comm . ,
, NULL-:

if i_city >= then


begin
if city = 'London then
comm := comm + .01
else comm := comm - .01;
end;
{ begin end }
"
", , city NULL.
.


NULL-.
UPDATE INSERT,
SELECT. ,
NULL-. ,
NULL- city comm Salesperson,
i_a i_b ;
:

SQL INSERT INTO Salespeople


VALUES (:id_num, :salesperson, :loc:i_a, :comm:i_b);
,
SQL ,
.
VARCHAR LONG (. ).
, ,

, .
,

, , . ,

295
25. SQL


> , < .

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

. ANSI SQL
Pascal, FORTRAN, COBOL, PL/1.
. .
SQL :

SQL SQL
.

, SQL,
SQL .

SQL ,
.


INTO ,
.


.
( , ),
( ) (
,

). ,
FETCH :
FETCH.

" ".
, ,

. ORDER UNION,
.
" ".

296

,
, SQL UPDATE
DELETE WHERE CURRENT OF. DELETE UPDATE
,
.

SQLCODE
, SQL.
SQL-.

SQL ,
, SQLCODE
100. , SQLCODE
, ,
SQL-. SQLCODE .

WHENEVER ,
, SQLCODE 100 (NOT
FOUND - ) (SQLERROR -
SQL).
(GOTO <>) "
" (CONTINUE, " ").
" ".

.
-
SQL - , INDICATOR.

- . SQL
NULL ,
, .
,
SQL NULL-,
.

- NULL-
SQL INSERT UPDATE.
.

297
25. SQL

SQL

: ,
, , ,
Pascal ( - ,
, ).
, .
,
Pascal. ,
( /,
).
.

1. , snum
cnum Ordres Custorners
, . Orders
Customers,
snum .
, (
ANSI, ,
) ,
, (
, cnums ,
..). (DECLARE)
, .

2. , ANSI,

"".
?

3. ,
city , .01
,
Barcelona, . 1 ,
San Jose. , ,
London, .02
, ,

298
London, .02.
. ,

London. city
comm NULL-, ,
SQL. : .

( .)

299


31
.

1
1. cnum
2. rating
3. - .
- .

4. , , .

1. ( ) .

2. .

3. (DML, Data Manipulation Language).


4. , SQL, .

1. SELECT onum, amt, odate


FROM Orders;
2. SELECT
FROM Customers
WHERE snum =1001;
3. SELECT city, sname, snum, comm
FROM Salespeople;
4. SELECT rating, cname
FROM Customers
WHERE city ='San Jose';
5. SELECT DISTINCT snum
FROM Orders;

4
1. SELECT FROM Orders WHERE amt >1000;
2. SELECT sname, city
FROM Salespeople
WHERE city ='London'
AND comm >. 10;
3. SELECT
FROM Customers
WHERE rating >100
OR city ='Rome;

301
. J

SELECT
FROM Customers
WHERE NOT rating < =100
OR city = Rome';

SELECT
FROM Customers
WHERE NOT (rating < =100
AND city < >'Rome');
.
4. onum amt odate cnum snum
3001 18.69 10/0311990 2008 1007
3003 767.19 10/03/1990 2001 1001
3005 5160.45 10/03/1990 2003 1002
3009 1713.23 10/04/1990 2002 1003
3007 75.75 10/04/1990 2004 1002
3008 4723.00 10/0511990 2006 1001
3010 1309.95 10/06/1990 2004 1002
3011 9891.88 10/0611990 2006 1001
5. onum amt odate cnum snum
3001 18.69 10/0311990 2008 1007
3003 767.19 10/03/1990 2001 1001
3006 1098.16 10/03/1990 2008 1007
3009 1713.23 10/04/1990 2002 1003
3007 75.75 10/04/1990 2004 1002
3008 4723.00 10/05/1990 2006 1001
3010 1309.95 10/0611990 2004 1002
3011 9891.88 10/06/1990 2006 1001

. SELECT
FROM Salespeople;

5
1. SELECT
FROM Orders
WERE odate IN (10/03/1990, 10/04/1990);

SELECT
FROM Orders

302
. 1<

WHERE odate BEWEEN 10/03/1990 AND 10/04/1990;


2. SELECT
FROM Customers
WHERE snum IN (1001,1004);
3. SELECT *
FROM Customers
WHERE cname BEWEEN . . AND . .;

: , S-,
Hoffinan, '' .
'G',
Giovanni Grass. 'G' ,
'Z' - .

4. SELECT
FROM Custome rs
WHERE cname LIKE . . ;
5. SELECT
FROM Orders
WHERE amt < >
AND (amt IS NOT NULL);

SELECT
FROM Orders
WHERE NOT (amt =
OR amt IS NULL);

6
1. SELECT COUNT( )
FROM Orders
WHERE odate = 10/03/1990;
2. SELECT COUNT (DISTINCT city)
FROM Custome rs;
. SELECT cnum, MIN (amt)

FROM Orders
GROUP cnum;
4. SELECT MIN (cname)
FROM Customers
WHERE cname LIKE 'GX';
5. SELECT city
(rating)
FROM Custome rs

303
. 11

GROUP ci ty;
6. SELECT odate, count (DISTINCT snum)
FROM Orders
GROUP odate;

7
1. SELECT onum, snum, amt.12
FROM rde rs;
2. SELECT For the city, city, the highest rating is ,
(rating)
FROH Customers
GROUP ci ty;
3. SELECT rating, cname, cnum
FROH Customers
ORDER rating DESC;
4. SELECT odate, SUH (amt)
FROH Orders
GROUP odate
ORDER 2 DESC;

8
1. SELECT onum, cname
FROM Orders, Customers
WHERE Customers.cnum = Orders.cnum;
2. SELECT onum, cname, sname
FROM Orders, Customers, Salespeople
WHERE Customers.cnum = Orders.cnum
AND Salespeople.snum = Orders.snum;
3. SELECT cname, sname, comm
FROH Salespeople, Customers
WHERE Salespeople.snum=Customers.snum
AND comm > . 12;
4. SELECT onum,commamt
FROM Salespeople, Orders, Customers
WHERE rating > 100
AND Orders.cnum = Customers.cnum
AND Orders.snum = Salespeople.snum;

9
1. SELECT first.sname, second.sname
FROM Salespeople first, Salespeople second
WHERE first.city = second.city

304
.

AND first.sname < second.sname;


, .
2. SELECT cname, first.onum, second.onum
FROM Orders first, Orders second,Customers
WHERE first.cnum = second.cnum
AND first.cnum = Customers.cnum
AND first.onum < second.onum;
,
, .

3. SELECT a.cname, a.city


FROM Customers , Customers

WHERE . rating = b.rating


AND . cnum = 2001;

10
1. SELECT
FROM Orders
WHERE cnum =
(SELECT cnum
FROM Customers
WHERE cname = c1sneros");

SELECT
FROM Orders
WHERE cnum IN
(SELECT cnum
FROM Customers
WHERE cname = Cisne s ) ;
2. SELECT DISTINCT cname, rating
FROM Customers, Orders
WHERE amt>
(SELECT AVG (amt)
FROM rde s)
AND Orders.cnum = Customers.cnum;
3. SELECT snum, SUM (amt)
FROM Orde s
GROUP snum
HAVING SUM (amt) >
(SELECT (amt)
FROM Orders);

305
.

JJ
1. SELECT cnum, cname
FROM Customers outer
WHERE rating =
(SELECT (rating)
FROM Customers inner
WHERE inner. ci ty = outer. city);
2.
SELECT snum, sname
FROM Salespeople main
WHERE city IN
(SELECT city
FROM Customers inner
WHERE inner.snum < > main.snum);
:
SELECT DISTINCT first.snum, sname
FROM Salespeople first, Customers second
WHERE first.city = second.city
AND first.snum < > second.snum;
,
, -

.
. , city ,
snums . ,
. ,
.
.

12
1. SELECT
FROM Salespeople first
WHERE EXISTS
(SELECT
FROM Customers second
WHERE first.snum = second.snum
AND rating = 300);
2. SELECT a.snum, sname, a.city, comm
FROM Salespeople , Customers
WHERE a.snum = b.snum
AND b.rating = 300;
3. SELECT
FROM Salespeole
WHERE EXISTS

306
.

(SELECT
FROH Customers
WHERE b.city = a.city
AND a.snum < > b.snum);
4. SELECT
FROH Customers
WHERE EXISTS
(SELECT
FROM Orders
WHERE a.snum = b.snum);
AND . snum < > . snum);

13
1. SELECT
FROM Custome rs
WHERE rating > = ANY
(SELECT rating
FROH Customers
WHERE snum = 1002);
2. cnum cname city rating snum
2002 Giovanni Rome 200 1003
2003 Liu San Jose 200 1002
2004 Grass Berlin 300 1002
2008 Cisneros San Jose 300 1007
. SELECT
FROM Salespeople
WHERE city < > ALL
( SELECT city
FROM Custome rs);

SELECT
FROM Salespeople
WHERE NOT city = ANY
(SELECT city
FROM Customers);
4. SELECT
FROM Orders
WHERE amt > ALL
(SELECT amt
FROM Orders , Customers
WHERE a.cnum = b.cnum
AND .city = 'London');

307
. J1

5. SELECT
FROM Orders
WHERE amt >
(SELECT (amt)
FROM Orders , Customers
WHERE a.cnum = b.cnum
AND b.city = 'London');

14
1. SELECT cname, city, rating, 'High Rating
FROM Customers
WHERE rating > = 200
UNION
SELECT cname, city, rating, Low Rating
FROM Customers
WHERE rating <2 00;

SELECT cname, city, rating, 'High Rating'


FROM Customers
WHERE rating > = 200
UNION
SELECT cname, city, rating, Low Rating
FROM Customers
WHERE NOT rating > = 200:

. 'Low Rating' ,
'High Rating'.
2. SELECT cnum, cname
FROM Customers
WHERE 1 <
( SELECT COUN( )
FROM Orders
WHERE a.cnum = b.cnum)
UNION
SELECT snum, sname
FROM Salespeople
WH~RE 1 <
(SELECT COUNT ()
FROM Orders
WHERE a.snum = b.snum)
ORDER 2;
3. SELECT snum
FROM Salespeople

308
. 11

WHERE city = san Jose


UNION
(SELECT cnum
FROM Customers
WHERE city = san Jose
UNION ALL
SELECT onum
FROM Orders
WHERE odate = 10/03/1990);

15
1. INSERT INTO Salespeople (city, cname, comm, cnum)
VALUES (" San Jose ", "Blanco ", NULL, 1100);
2. DELEE FROM Orders WHERE cnum = 2006;
. UPDATE Customers

S rating = rating + 100

WHERE ci ty = 'Rome' ;
4. UPDATE Customers
S snum = 1004

WHERE snum = 1002;

16
1. INSERT INTO Multicust
SELECT
FROM Salespeople
WHERE 1 <
(SELECT COUNT()
FROM Customers
WHERE Customers.snum = Salespeople.snum):
2. DELETE FROM Customers
WHERE NOT EXISTS
(SELECT
FROM Orders
WHERE cnum = Customers.cnum);
. UPDATE Salespeople

SET comm = comm + (comm.2)


WHERE 3000 <
(SELECT SUM (amt)
FROM Orders
WHERE snum = Salespeople.snum);
,
, (comm) 1.0 (100 ).
UPDATE Salespeople

309
.

SET comm = comm + (comm .2)


WHERE 3000 <
(SELECT SUM (amt)
FROM Orders
WHERE snum = Salespeople.snum)
AND comg + (comm.2) < 1.0;
.

17
1. CREATE TABLE Customers
(cnum integer,
cname char(10),
city char(10).
rating intege ,
snum integer);
2. CREATE INDEX Datesearch ON Orders(odate);
( .)
. CREATE UNIQUE INDEX Onumkey ON Orders (onum);
4. CREATE INDEX Mydate ON Orders (snum, odate);
5. CREATE UNIQUE INDEX Combination ON
Customers (snum, rating);

18
1. CREATE TABLE Orders
(onum integer NOT NULL PRIMARY ,

amt decimal,
adate date NOT NULL,
cnum integer NOT NULL,
snum integer NOT NULL,
UNIQUE (snum, cnum));

CREATE TABLE Orders


(onum integer NOT NULL UNIQUE,
amt decimal,
odate date NOT NULL,
cnum integer NOT NULL,
snum integer NOT NULL.
UNIQUE (snum, cnum));
.
2. CREATE TABLE Salespeople
(snum integer NOT NULL PRIMARY ,

310
.

sname char(15) (sname BEWEEN '' AND 'MZ'),


city char(15),
comm decimal NOY NULL DEFAULT = .10);
. CREATE TABLE Orders
(onum integer NOT NULL,
amt decimal,
odate date,
cnum integer NOT NULL,
snum integer NOT NULL,
((cname > sname) AND (onum > cnum))):

19
1. CREATE TABLE Cityorders
(onum integer NOT NULL PRIMARY ,
amt decimal,
cnum integer,
snum integer,
city char(15),
FOREIGN (onum,amt,snum)
REFERENCES Orders (onum, amt, snum)
FOREIGN (cnum, city)
REFERENCES Customers (cnum, city) ) :
2. CREATE TABLE Orders
(onum integer NOT NULL,
amt decimal,
odate date,
cnum integer NOT NULL,
snum integer,
prev integer,
UNIOUE ( cnum, onum),
FOREIGN (cnum, prev) REFERENCES Orders (cnum, onum) ): 9

20
1. CREATE VIEW Highratings
AS SELECT
FROM Customers
WHERE rating =
(SELECT (rating)
FROM Custome rs);
2. CREATE VIEW Citynumber
AS SELECT city, COUNT (DISTINCT snum)
FROM Salespeople
GROUP ci ty;

311
.

. CREATE VIEW Nameorders


AS SELECT sname, AVG (amt), SUM (amt)
FROM Salespeople, Orders
WHERE Salespeople.snum = Orders.snum
GROUP sname;
4. CREATE VIEW Multcustomers
AS SELECT
FROM Salespeople
WHERE 1 <
(SELECT COUNT()
FROM Customers
WHERE . snum = . snum);

21

1. #1 - DISINCT.
#2 , ,
GROUP .
#3 , # 1, ,
, .
#4 .
2. CREATE VIEW Commissions
AS SELECT snum,comm
FROM Salespeople
WHERE comm BEWEEN .10 AND .20
WITH OPTION;
. CREATE TABLE Orders
(onum integer NOT NULL PRIMARY ,
amt decimal,
odate date DEFAUL VALUE = CUROATE,
snum integer,
cnum integer);
CREATE VIEW Entryorders
AS SELECT onum, amt, snum, cnum
FROM Orders;

22
1. GRANT UPDATE (rating) ON Customers Janet;
2. GRANT SELECT ON Orders Stephen WITH GRANT OPTION;
. REVOKE INSERT ON Salespeople FROM Claire;
4. War 1: CREATE VIEW Jerrysview
AS SELECT
FROM Customers
WHERE rating BEWEEN 100 AND 500

312
.

WHITH OPTION;
2: GRANT INSERT, UPDATE ON Jerrysview Jerry;
5. 1: CREATE VIEW Janetsview

AS SELECT
FROM Customes
WHERE ating =
(SELECT MIN (rating)
FROM Customers);
2:GRANT SELECT ON Janetsview Janet;

23
1. CREATE DBSPACE Myspace
(pctindex 15,
pctf ree 40);
2. CREATE SYNONYM Orders FOR Diane.Orders;
3. .

4. .

5. .

24
1. SELECT a.tname, .w, b.cname, b.datatype
FROM SYSTEMCATALOG , SYSTEMCOLUMNS
WHERE a.tname = b.tname
AND .w = b.owner
AND a.numcolumns > 4;
: ,

. .
2. SELECT tname, synowner, COUNT (ALL synonym)
FROM SYTEMSYNONS
GROUP tname, synowner;
3. SELECT COUNT ()
FROM SYSTEMCATALOG
WHERE numcolumns/2 <
(SELECT COUNT (DISTINCT cnumber)
FROM SYSTEMINDEXES
WHERE a.owner = b.tabowner
AND . tname = . tname);

25
1. SQL BEGIN DECLARE SECION;

313
.

SQLCOOE: intege ;

cnum itege;

snum intege;

custum: intege;

salesnum: itege;
SQL ENO DECLARE SECTION;
SQL DECLARE Wg_ds AS CURSOR FOR

SELECT cum, snum


FROM ds
WHERE snum < >
(SELECT sum
FROM Customes
WHERE a.cum = b.cum);
{ SQL.
Odes, Customes.}
SQL DECLARE Cust_assigns AS CURSOR FOR
SELECT cnum,sum
FROM Customes;

sum.}
begin { }
SQL OPEN CURSOR Wg_ds;

while SQLCODE = do
{ Wong_Odes .}
begin
SOL FETCH Wong_Odes INTO
(: cum, : snum);
if SQLCODE=O then
begi

Wg_ds ,
.}

SQL OPEN CURSOR Cust_Assigs;

SOL F Cust_Assigs

INTO (:custum, :salesum);
until :custum = :cum;
FETCH util ... , Cust_Assigs
w , ,

cnum Wong_Orders .}
SQL CLOSE CURSOR Cust_assigns;
{ , .
, ,
salesnum.}
SOL UPDATE Ordes

314
.

S sum = :salesum
WHERE CURRENT OF Wrog_Orders;
d; { SQLCODE = }
end; { SQLCDDE ... }
SQL CLOSE CURSOR Wrog_Orders;

end; { }
2. , ,
onum( Orders Wrong_orders).
UPDATE WHERE onum = :ordemum (
, ordemum) WHERE CURRENT OF
Wrong_Orders. (
):
SQL BEGIN DECLARE SECTION;
SQLCODE: iteger;

oderum iteger;

cum iteger;

sum iteger;

custum: iteger;

salesum: iteger;

SQL END DECLARE SECTION;


SQL DECLARE Wrog_Orders AS CURSOR FOR

SELECT oum, cnum, sum


FROM Orders
WHERE sum < >
(SELECT snum
FROM Customers
WHERE a.cnum = b.cnum);
SQL DECLARE Cust_assigns AS CURSOR FOR

SELECT cnum,sum
FROM Custome rs;
begin {
SQL OPEN CURSOR Wrog_Orders;

while SQLCODE = do { , , Wrog_Orders


}
begi

SQL F Wrog_Orders
INTO (: oderum, : cum, : snum);
if SQLCODE=O then
begin
SOL OPEN CURSOR Cust_Assigns

repeat
SQL FETCH Cust_Assigs

INTO (: custum, : salesnum);


util :custum = :cum;

315
.

SOL CLOSE CURSOR Cust_assigs;



SOL UPDATE ds;

SET sum = :salesum


WHERE CURRENT OF Wg_ds;
d; { if SOLCODE = }
d; { While SOLCODE ... do}

SOL CLOSE CURSOR Wg_ds;

d; { }
. SOL BEGIN DECLARE SECTION;

SOLCODE intege;

newcity packed [1 .. 12] of h;


commull boolean;
cityull l;

esponse cha ;
SOL ENO DECLARE SECTION;

SOL DECLARE CURSOR Salespeso AS


SELECT FROM SALESPEOPLE;
begin { }
SOL OPEN CURSOR Salespeso;

SOL F Salespeso

INTO (:sum, :same, :city:i_cit:comm:i_com);


{ (fetch) . }
while SOLCODE = do
(while) Salespeso.}
begi

if i_com < the commull : = tue;


if i_cit < the cityull : = tue:
, NULL-
if cityull then
begi

wite (" Salespeso


city', sum, ' - ?
(()/N())');
, city (NULL)}
d ( ss):
ss .

d { if citynull }
else { t citynull }
begi

if t commull then
{ ,

commnull NULL
begin

316
. 11

if city='London then comm : = comm - .02


else comm : = comm + .02;
end;
{ " commnull", begin end .
write (' , snum, city,
? (Y/N)');

: , ,
, .
read ( response);
{ response ,

citynull "" "". }

end; { else not citynull }


if response = then
begin
write (' city: );
read (newcity);
if not commnull then
{ , NULL }
case newcity of:
begin
'Barcelona': comm: = comm+.01,
'San Jose':comm: = comm-.01
end; { case and if not commnull}
SQL UPDATE Salespeople
S city=:newcity, comm = : comm:i_com

WHERE CURRENT OF Salesperson;


{ - comm NULL- }
d; { response = , respose<>'Y',

.}
SQL F Salesperson
INTO (:sum,: sname, : ity:i_cit,
: comm: i_com);
{ }
end; { SQLCODE = }
SQL CLOSE CURSOR Salesperson;

end; { }

317

SQL
. SQL

, ANSI, (CHAR)
, :
.

.
( 1).
.

,
, .
, .

TunANSI

ANSI ( ).

()

CHAR , .
(CHARACTER) - ,

.
, , ''.
("),
, .

( NUMERJC)

DEC (DECIMAL) , .. ,
. ,
: .
.
, , .

.
, -

320
TunANSI

.
. ,
, ""
(INTEGER).

NUMERIC DECIMAL, ,

INT (INTEGER) .
DECIMAL,
, .. , .
(
).

SMALLINT INTEGER, ,
, ,
, ( ) ,
INTEGER.

(APPROXIMATE NUMERIC)

FLOAT ,
1.
, .

REAL FLOAT, ,
.
.

DOUBLE REAL, , ,-
PRECISION DOUBLE PRECISION,
( DOUBLE) REAL.

321
. SQL

.1 SQL , SQL
SQL,
(. 25).
SQL, . ,
SQL, ANSI
:

PASCAL, PL/I, COBOL, FORTRAN. -


,

, ANSI, .

PVI
SQL PL/I
CHAR CHAR
DECIMAL FIXED DECIAL

INTEGER FIXED BINARY


FLOAT FLOAT BINARY

COBOL
SQL COBOL
CHAR( <>) PIC (<>)

INTEGER PIC S(<>) USAGE COMPUTATIONAL


NUMERIC PIC S(<u V>) DISPLAY
SIGN LEADING SEPARATE

PASCAL
SQL PASCAL
INTEGER INTEGER
REAL REAL
CHAR(<>) PACKED ARRAY [1 .. <>] OF CHAR

322
11 1111

FORTRAN
SQL FORTRAN
CHARACTER CHARACTER
INTEGER INTEGER
REAL REAL
DOUBLE DOUBLE
PRECISION PRECISION

323

tELIJJrnuLD

SQL
. SQL

SQL,
ANSI, ISO,
SQL, ,
. . ,
.
.

, SQL, .
CHARACTER . ,

. .

DATE () TIME ()
DATE ,
. Orders, :
mm/dd/yyyy. IBM
. , ,
,
. , ,
.
.

ISO (lntemational yyyy-mm-dd 1990-10-31


Standards Organization)

JIS (Japanese yyyy-m-dd 1990-10-31


Industrial Stadard)
EUR (I dd.mm.yyyy 10.31.1990
European Stadard)

, ,
. ,
, ,
, ..
, , date < date ,
.

326

, , ,
(); ,
:

ISO (Intemational hh-mm-ss 21.04.37


Standards Organization)
JIS (Japanese hh-mm-ss 21.04.37
lndustrial Standard)
EUR (I European hh-mm-ss 21.04.37
Standard)
USA (I USA hh.mm / 9.04
Standard)
, TIME,
( , DATE),
. ,
,

(CURDATE CURIME ). USER


, .
?
DATE, , . ,
, IMESTAMP,
- DATE .

STRJNG ( )
ANSI . CHAR.
.1, . ,
, , ;
. ,
, .
, UNION
.

-
VARCHAR LONG VARCHAR ( LONG).
CHAR ,
, ,

VARCHAR , .1
, SQL
. VARCHAR
,
254 2048 VARCHAR 16 LONG.
LONG
(, ) , -

327
. SQL

. VARCHAR ,
.

VARCHAR CHAR.
VARCHAR , ,
, CHAR. , VARCHAR
. ,
,
,
: CHAR VARCHAR. LONG
. , LONG ,
, SQL
. .

FORAT

SQL .
SQL ,
,
, FORAT, SQL
.
FORMAT :

( );

NULL-;

, ,
;

,
, SUM.
-

COMPUTE.
FORMAT ,
, .
FORMAT , FORMAT
. FORMAT:
FORMAT NULL - _ _ _ _ '
FORMAT BTITLE ', ';

FORMAT EXCLUDE (2, );

328

NULL-
'_______ '. ,
, .
.
FORMAT
ORDER
. FORMAT
-,
.
,
. SET FORMAT.
FORAT ,
, .

COLUMN,
FORMAT. :
COLUMN odate FORMAT dd-mon-yy;
1O-Oct-90
.

COMPUTE, , :
SELECT odate, amt
FROM Orders
WHERE snum = 1001
COMPUTE SUM ( amt);
Peel
(odate) (t), - amt.
, COMPUTE .
:

BREAK ON odate;

, odate
. :
COMPUTE SUM OF amt ON odate;
, ON,
BREAK.

NSI- SQL
, : comm *
100.
, . SQL,
,

329
. SQL

SELECT , , ,
, .
, .
,
SELECT, .

ABS(X) (
)

CEIL(X) ,
.

FLOOR(X) ,
.

GREATEST(X,Y) .

LEAST(X,Y) .

MOD(X,Y) .

POWER(X,Y) , .

ROUND(X,Y) .
, .

SIGN(X) , < , -
.

SQRT(X) .


, -
, .

LEFT( <> ,) .

RlGHT(<cmpoa>,X)
.

ASCil(<cmpoa>) ASCII-o,
.

330

CHR(<S->) ,
ASCII-oy.

VALUE (<>) .
,
CHAR VARCHAR,
. VALUE('')
3 INTEGER.
UPPER (<>)
"" (, ) .

LOWER (<>) ""


() .

INICAP (<>)
" ".
PROPER.

LENGTH (<>) .

<>ll<>
, ,

. (11
.)

LPAD (<>,,'*') "*"


,
, ,

RPAD (<> ,,'*') LPAD, ,


.

SUBSTR (<> ,,) ,


.

331
. SQL

.I
"" "".

D(<>) .
MONTH (), YEAR (),
HOUR (), SECOND () ..

WEEKDAY .
(<>)

NVL(<>,) NVL (NULL Value) <>,


<> ,

NULL-, (
) .
NULL- ,
.

INTERSECT ()
MINUS ()

UNION ,
.
INTERSECT () MINUS
(). INERSEPT - ,
.
MINUS - ,
,

, :

SELECT
FROM Salespeople
WHERE city = 'London

332
OUTER JOINS ( )

INTERSECT
SELECT *
FROM Salespeople
WHERE 'London' IN
(SELECT city
FROM Customers
WHERE Customers.snum = Salespeople.snum);
,
, , ,
.

SELECT *
FROM Salespeople
WHERE city='London'
MINUS
SELECT *
FROM Salespeople
WHERE 'London' IN
(SELECT city
FROM Customers
WHERE Customers.snum = Salespeople.snum);
, ,
, ..
, .
MINUS DEFFERENCE.

OUTER JOINS (
)

14 , ,
UNION. , ,
.
"" (+) ,
, , ,

, . ,
, NULL- ,

333
. SQL

. , , ,
(
, ).

SELECT a.snum, sname, cname


FROM Salespeople , Customers

WHERE a.snum=b.snum (+);


UNION:
SELECT a.snum, sname, cname
FROM Salespeople , Customers

WHERE a.snum = b.snum


UNION
SELECT snum, sname, _
FROM Salespeople
WHERE snum NOT IN
(SELECT snum
FROM Customers);
, NULL-
(. FORMAT ).

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

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

334
J1~

S JORNAL ON;
. ,
.
, .
,
.
,
. AUDIT:

AUDIT INSERT ON Salespeople Diane;


ON ,
.
AUDI INSERT AUDIT ALL,
Diane, Salespeople.

335

,
D.

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

SQL
(< d >).

([ and ]).

(... ) ,
.

( 1) ,
, .

( { and }) : , ,
.

(: : =),
.

, (., .. ) :
, .
, ,
(* nonstandard *).
: , ,
ANSI. ,
. , ANSI,
ANSI, . , <>
: - , <>,
- , < >.

SQL
SQL. :
.
- ; SQL ,
, ,
. <> ; ,

338
SQL

<> .
SQL .
, ,
SQL. , <'lt>,
.
; -
.


. , <>
, <> AND OR
<>.
<>
.
.

<~'l> <,1>l<~>l< >

<> - - <> < >

<1>

< > ( )

<> <>[ {< >! <

>} ... ]

:
ANSI,
<>
18 .

<.w >

< > %
<> : ,( )<> .:=+ * - / <> >= <=
<>

<> [ ]

339
D.

: <>

< SQL> (* , .1
*) .


SQL
:

<> SELECT
<>
SELECT,
, , ,

<, <>
> l<><><>
l<><><,
>

<> : +- /*
<> < >
1 <>
1 < >
1 < >
1 < >

<.> <> 1 < 11>

< > USER 1 <,


>

< > <>

< > [<.~t > 1 <>.]< >

<11110 > < > 1 <>

< > < > 1 <>

340
SQL

< > NOT NULL 1 UNIQUE


1 (<>)

1 PRIMARY
1 REFERENCES <l > [( <z >)]

< > UNIQUE (< >)


1 (<>)
1 PRIMARY (< >)
1 FOREIGN (< >)

REFERENCES <m > [(<


>)]

<.: > DEFAULT VALUE = <:,


: >

< > (. ,
, ANSI,
,
).

<.> ,
(. ).

< > <>

<.~1 > <>

<> <>(**)

<> < n.>

< > < >., ..

< > <., : >., ..

< > {< >[<>] }.,..

<> ,
:

<> :: = [NOT]
{ < >

1 < >

1 <NULL->

1 < "">

341
D.

1 < "">

1 < >

1 < >

[AND 1 OR <>]
<> - , ,
, < > <NLL->,
, .
, NULL- .
, , , NULL-
. - AND, OR NOT - r
<>. NOT true () - false, NOT false () - true,
NOT unknown ()- unknown.
AND
AND true false unknown
true true false unkno,vn
false false false false
unknown unknown false unknown
OR
OR true false unknown
true true true true
false true false unknown
unknown true unknown unknown
, :
. AND, ,
(unknown) (true)
(unknown).
. NOT
, AND OR .
<> .

< >

<, > < > <,


> 1 <>

< >

<
>

342
SQL

<
>=
<>
- <:, : > NULL, <11
> unknown. ,
. ,
. <11 >
; .
<:, : >,
. <1111>,
, SELECT,
<:1, : 11> <11 > ,
<1111> .

< "">

<, > [NOT] BEWEEN <,

> AND <, >

"" BETWEEN AND ,


(>= AND <=). "" NOT BE\VEEN AND
, NOT ( BETWEEN AND ). <, :
> , <1111> (**).

< "">

<, > [NOT] IN < > 1


<>

<11 > ,
. <1111>,
<:, : > SELECT (
, ). <11>
- . ,
, <11 > . <11 "">
, <:, : > <11
> , NOT. NOT IN (,) NOT ( IN (,)).

< "">

< > [NOT] LIKE <> [ESCAPE <

>]

343
D. 110

< > - <. >


- (* *).
, < > < >.
<> , ,
< >. <1 > -
- .
, :

<. > <>,


<1 >,
< 1111>.

<.~1.1 11> <>,


<1 >,
<.~t 1111>.

<1 .1 .,111> <>,


<1 11
11>, <.~t
>.

<>,
< >.

, < "">
"", NOT.
"" NOT LIE 'text' NOT ( LIKE 'text').
<NULL->

< > IS [NOT] NULL


< > IS NULL "",
NULL . <
> IS NOT NULL , N(<
> IS NULL).
< >

<, > < >

<><>

<>:: = ANY 1 ALLI SOME


SELECT <> <
, >. , <
> < >. <, >
< 1> <
>. :

344
SQL

<> = ALL <. >


"", < > "
".

<> = ANY , , <,;~


>, "
", < > "".

< > , < >


"", <>= ALL, "" -
.

SOME , ANY.

< > "", "",


.

< >

EXISTS (<>)

<> - , <
> "", -
"".

SQL

SQL.
, ,
.
: , SQL, ,
<SQL-ep.11>, SQL.

345
11 D.

BEGIN DECLARE SECTION


SQL BEGIN DECLARE SECTION <SQL-ep>;

< >

SQL END DECLARE SECTION <SL->;


, SQL-
. SQLCODE
.

CLOSE CURSOR

SOL CLOSE CURSOR < ><SL->;

CURSOR () ,
, .

COMMIT (WORK)

WORK;
,
,

CREA INDEX (* *)

CREATE [UNIQUE] INDEX < >

ON < > (< >);


, .
UNIQUE,
.

346
SQL

CREATE SYNONYM (* *)

CREATE [PUBLIC] SNN<> FOR <>.< >;

.
, .
,
. PUBLIC,
SYSTEM .

CREATE TABLE

CREATE TABLE < >

({< > < >[ <>]

[< > . .. ]
[< >]}., .. < >., .. );
.
. , . < >
, . < >
;
. <.> . <N
> < > ,
. <11 110 > ,
,

(. 17 CREATE TABLE 18 19
, <1 >}.

CREATE VIEW

CREATE VIEW < >

AS <>

[WITH OPTION];
SQL.
<. >,
.
. ,
, <>.

347
D.

W! OPION, <
>, <>.

DECLARE CURSOR

SQL DECLARE < > CURSOR FOR <><SL->

. (. OPEN
CURSOR), , FETCHED.
, , <>,
(. 25).

DELETE

DELETE FROM < >


{ [WHERE <>];}
WHERE CURRENT OF < > <SL->


WHERE , .
WHERE <>, ,
<>. WR- CURRENT OF
< >, , FROM,
, <~ >. WHERE CURRENT OF
SQL
.

EXECSQL

SQL < SOL> <SL->

SQL SQL ,
.

FETCH

SQL FETCH < >

INTO < > <SL->

348
SQL

.1 <>
< . 11..1111>,
. < .
11.111>
.

GRANT
()

GRANT ALL [PRIVILEGES]


1 { SELECT
1 INSERT

DELEE

UPDATE [(< >)]


REFERENCES [(< >)] }., ..
ON < >., ..
PUBLIC < >., ..

[WITH GRANT OPTION];


ALL PRIVILEGES
. PUBLIC
.
( ) ,
ON. REFERENCES < .111
> .
, . UPDATE,
REFERENCES, . GRANT
OPTION .

( )

GRANT DBA
RESOURCE
CONNECT., ..
< >., ..
[IDENTIFIED > >]

CONNECT , . RESOURCE
. DBA
. IDENIFIED CONNECT
.

349
D. 110

INSERT

INSERT INTO < > [ (< >) ]


VALUES (< >) 1 <>;

INSERT ,
. VALUES, ,
< >, , .
, ,
. < .J11 >,
,
.

OPENCURSOR

SQL OPEN CURSOR < > <SL->

OPEN CURSOR , <.1 >.



FETCH ( FETCH).

REVOKE (* *)

REVOKE { ALL [PRIVILEGES]


<>., .. } [ON < >]

FROM { PUBLIC
< >. , .. };
'<>
- , GRANT.
REVOKE ,
GRANT ( ). ON
, .

350
SQL

ROLLBACK (WORK)

ROLLBACK WORK;
,
, .

SELECT

SELECT { [OISTINCT ALL] <, >., .. } 1


[INTO < > (
}]

FROM < >., ..


[WHERE <>]
[GROUP <, >., .. ]
[HAVING <>]
[ORDER <,
> [ASC 1 DESC]., .. ];

(. 3-14).
r .

ALL DISINCT , ALL.


<, > < >,
< >, < >, <>,
,
.

< > ,
, ,
(* , *) . ,
, , .
,
.
<
>.

GROUP , < >,


SELECT, <,
>, ,
< >. <, -

351
D.

> <, . >


SELECT. <
, 111 >
.

VING, <>
, GROUP ,
<> - "",
.

ORDER ,
. < >
<, >,
SELECT. <, >
, < > <
>. < >
, <,
w > SELECT.
ORDER .
,
DESC. < >,
ORDER ,
.

SELECT -
(), . -
:

< >,
-.

< >,
.
-.

- <>,
WHERE; : true, false, unknown.
GROUP , <., >
-, <>
; .
GROUP , -
. , <,
> - .
DISINCT, .

352
SQL

UNION

<> { UNION [ALL] <> }... ;


.
<., 1
> SELECT, , <
, >,
.

UPDATE

UPDATE < >

SET { < >= <, >}., ..


{ [ WHERE <> ] ; }
{ [ WHERE CURRENT OF < > ]
<SL-> ] }

UPDATE ,
SET, <, 11>.
WHERE <>,
, <> "".
WHERE CURRENT OF,
. WHERE
CURRENT OF SQL
. WHERE,
.

WHENEVER

SQL WHENEVER <SL-> <> <SL->

<SL-> : : = SQLERROR 1 NOT FOUND 1 SQLWARNING


( )

<> :: = CONINUE 1 GOTO <> GO <>

<>:: = .

353

w ~ 1] 1] ffil

,
JJ.t


. , 1t

1. Salespeople ()
snum sname city comm
1001 ) London .12
1002 Serres San Jose .13
1004 Motika London .ll
1007 Rifkin Barcelona .15
1003 Axelrod New York .10

2. Customers ()
cnum cname city rating snum
2001 Hoffman London 100 1001
2002 Giovanni Rome 200 1003
2003 Liu San Jose 200 1002
2004 Grass Berlin 300 1002
2006 Clemens London 100 1001
2008 Cisneros San Jose 300 1007
2007 Pereira Rome 100 1004

3: Orders ()
onum amt odate cnum snum
3001 18.69 10/03/1990 2008 1007
3003 767.19 10/03/1990 2001 1001
3002 1900.10 10/03/1990 2007 1004
3005 5160.45 10/03/1990 2003 1002
3006 1098.16 10/03/1990 2008 1007
3009 1713.23 10/04/1990 2002 1003
3007 75.75 10/04/1990 2004 1002
3008 4723.00 10/05/1990 2006 1001
3010 1309.95 10/06/1990 2004 1002
3011 9891.88 10/06/1990 2006 1001

356
F
SQL
F. SQL

SQL

SQL . -
Oracle 1976 ,
SQL 1986 ,
ANSI (the American National Standards Institute) ISO (lnternational
Standards Organization). ANSI ISO,
ISO. 1986
1989 , ,
(referential integrity).
, 86,
SQL, ISO
,
. ISO

. , ,
, . ,
,
,
.
, .
,
r .
ISO SQL 92 .
SQL 92 SQL .
u,
,
, , .
SQL 92, 89, 92,
89
.
, , , -
.
, ,
(flagger) - , (
) SQL, 92.
, . ,
, u. ,
, - .
. , , ,

.

358
SQL

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

SQL, .
, ,
.
, .

SQL ,
. , SQL, SQL-
. , SQL,
. SQL .
, . SQL-
. /
.
, , ,
, ,
.
- ,
.

(), SQL-
SQL 92.

92?
92, , 89.
, ,
.

. (shema) -
,
. SQL
,
(authorization IDs), ,
ID, . ,
, -

359
F. SQL

.
, , ,
.

. :
(base taes) (views).
- () , - , ,
(virtual taes) - ,
. - ,
, .

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

,

. ,
,
.

JOIN.
SELECT, SQL
.
( )
FROM , , ,
, . ,

:

Cross -
, .

Natural , ,
.
, (
, )

360
SQL

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

Inner (outer) ,
, ,
.

Left (outer) , ,
, ,
.
NULL. "outer"
,

Right (outer) , Left:



.

Full .
,
.

Union
-
, .
,
,
. (
UNION,

).


FROM.
READ-ONLY, SCROLLABLE, INSENSIIVE, DYNAMIC.
- ,
. SQL 89 ,
, .
"read-only" ( ). -

361
F. SQL

, ,
.

Sensitivity () ,
, set-at-
a-time item-at-a-time, .
, , () .
, , ,

. , ,
? ,
" !" SQL 92,
(insensitive),
. read-only.
,
. ( ,
, , .
, ,
.)
, (saoll).
, ,
.
( ),
.
, ..
" ".
- ..
SQL, .
, ,
,
, . ,
, ALLOCATE
CURSOR DECLARE CURSOR,
, .
, " ",
.

/. ,
,
(front-end) ,
<> (client),
(''ack-end"), , <>
(server). , ,

.

362
SQL

/? /
,
.
.
.
. SQL
, , ,

,
. ,
(
) .
, ,

.1 .
/ .
.
, , ,
. 92 , SQL-

. -,
,
.
, -.
SQL 92, , ,
: ,
mainframe .

. -
SQL, .
,
(canceled) ("rolled back").
,
, , ,

. I WOR (
) ROLLBACK (
).
, ; ROLLBACK
.
86. SQL 92
:

" " (read l).


, ,

, ,

363
F. SQL

,
.


.
.

,
.

. -
, , .. ,
-. ,
m
, ISO .
: READ UNCOITED, READ ID,
READ REPEAABLE, SERIALIZALE. , ,
" " (read-only}, READ UNCOMMITTED
.

. ,
, ,
. , , ,
. ,

.
: SQL
, .
, .
, ,
. , ,
SQL,
, , .
, , ,
, , ,
, . .
,
(application-owned},
.
, ,
, , -, .
SQL 92 application-owned
SQL. SQL ,
, .

364
SQL

. ,
. "-".
,
.

. SQL-

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

.
- -
.

SQL 92
. , ,
,
.
SET CONNECION.
. ,
/.
SQL
, ,
, , .
" ".

. (catalog)
. (Informatio_
Schema), ,
: , ,
..
"".
, SQL 92,
,
, .

.
SQL SQL-
SQLCODE.

365
F. SQL

JI , r

. :

100 , ,
. ,
, ,
, SQLCODE
100.

,
,

(u ). SQLCODE
" "
, ,
. ,
.
,
SQLSTATE.
, ,
. , : .

. ,
. ,
, ( ).

,
.
GET DIAGNOSICS.

SQL. SQL - SQL, -


, -
, .
.
SQL 92 , ,
, .
.

, ADA MUMPS. 86
, SQL, , -

366
SQL

SQL Pascal, Fortran, COBOL, PL/l. 92


, Ada, MUMPS .

. , E.F.Codd -
, t (domains),
, ,
. ,
.
, ,

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

.
, , .
, ,
, .

11. - ,
, .
:
.

, - ,
, ,

. ,
, , NOT NULL UNIQUE,
,
. FALSE,
.

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

.
. (. ""
).

367
F. SQL


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

1. , ,
.

2. , ,
, .

3. , ,
.

. , ,
. ,

(constraints mode). ( , ,
.)
. lmmediate,
. , 3
.

, ,
. , ,
, -
. , NOT NULL.
,
.
,
,
.
(ciclaity). FOREIGN
, .
, ,
,
. ,
, ,
.

368
SQL

. SQL 86,
.
,
. AL TER ABLE DROP ABLE
-,
CREATE ABLE OSI.
, ALTER,
. OSI
AL TER DROP .
, : , ,
, , , .
. ,
, . ALTER TABLE
, ,
.

. - ,

. .
REVOE,
, GRANT. REVOE
SQL 92. .1 ,
-
.

GRANT USAGE,
: ,
, , . USAGE
,
. INSERT -:
. ISO
SELECT ,
Ifonnation_Schema (_).
.

, .
( )
SQL 92 .

.
, ,
,
. (
), < 1> << 2>
"", "", , 1,
2 NULL.
. , -

369
F. SQL

.
. ,
-
USAGE .

, , .
- .
,
, .
:
. :

, .

, , .

.
, , 11 :09:48.5839.
,
. , :'v!Ory
,
(UCT, Universal Coordinated Time).

SQL 92 (intervals): -
.
J.I . ,
2:00 5:30, 3:30 ( ).
, " 1993 .", -
- " 1993 .".
-, -,
.
, . ,
, ( ), ..
,
.

.
, .
BLOPs (Biary Large Objects - )

, .. BLOPs
.

: .
, ,

370
SQL

. , ,
.
( ),
( ), .

. SQL .
,
,
. .
,
, . 92
CAST,
integer
(character string) .
,
. (
, TRANSLATE CONVERT,
.)

. SQL
~,

,
. : SESSION_USER, CURRENT_USER,
SYSTEM USER.
CURRENT_ USER, USER,
, ,
. ,
. , ,
(. " " ).
CURRENT_USER .
,
SESSION_USER. ( SESSION_USER ,
SQL-
SET SESSION AUTORIZATION).
SYSTEM_USER - ,
. SESSION_USER,

-
.
.

. SQL 92
. -,
,

371
F. SQL

: (CONCA TENA ) ( 11 ),
SUBSTRING, UPPER, LOWER, TRIM, TRANSLATE, CONVERT. -,
, ,
: POSIION, CHAR_LENGTH, OCTET_LENGTH, BI_LENGTH.
CONCATENATE J11 (diadic),
.. .
. , 'Jello ' 11
'Biafra' 'Jello Biafra'. SUBSTRING
, , ,
, , . , SUBSTRING ('Astarte' FROM 2 FOR 4)
'star'. UPPER LOWER
( ) ( ) ;
(fold). TRIM ,
.
, .1, , .
TRANSLATE CONVERT SQL
: .
TRANSLATE .
CONVERT, ,
.
POSIION , ,
POSIION ('star' IN 'Astarte') 2.
, .
, , (8- ,
) . ~ ASCII
EBCDIC , SQL
,
(.1 ) .

. SQL
"=" SQL
"", "" ( NULL-) "",
. SQL-
, - ,
(
). , SQL
, . ,
:

WHERE 1 =

WHERE 1 = AND 2 =5
SQL 92, :

372
SQL

WHERE (1,2) = {3,5)


,
, (row v/
constrctions), .
(.. ).
, ,

,
. :

(1, 7, 8) < (2, , 1)


"", .')' l <2 (
,

).

. SQL 89
( ),
( ). , ,
NULL- ,
,
. (parent key).
UNIQUE PRIMARY ,
.
SQL 92 . -,
, .
( ),
NULL-.
, NULL- ; .1
.
.

, . 110 (referential

trigged actions),
. ,
, ( )
. ON UPDATE
ON DELETE. :

SET NULL. NULL ,


.

SET DEFAULT.
,

. , NULL-
.

CASCADE.
.

373
SELECT
SELECT * 1 { [DISTINCT 1 ALL] < >., ..
FROM {< > [<>] }., ..
[ WHERE <> ]
[ GROUP { < > 1 <> }. , .. ]
[ HAVING <> ]
ORDER { < > 1 <> }. , .. ]

{ UNION [ALL]

SELECT * 1 { [DISINCT ALL] < >., ..


FROM {< > [<>] }., ..
WHERE <> ]
GROUP { < > <>}., .. ]

HAVING <> ]
ORDER {< > 1 <>}., .. ]
} ]".;

, SELECT

< > , . -
.

<11.11 > .

<> , -
.

<> ,
(),
FROM.
< > .

<> .
SELECT, ero
1 .

UPDATE
UPDATE < >
S { 1 } , < > = < >
[ WHERE <>
1 WHERE CURRENT OF < >
( )];
INSERT
INSERT INTO < > [(< >., .. )]
{ VALUES (< >)., .. )}
1 <>;
DELETE
DELETE FROM < >
[ WHERE <>
1 WHERE CURRENT OF < >

( )];

,
J1 n.11
<. .:> , .

<> v SELECT.

~ SELECT.

,
n

1
, , ,

. , ''.

{ } , ,
1 , ., .. .

[] , , .
, ,

.... , ,
n .

CREATE TABLE < >

( {< > < > [<>]

[< > . . . ]} . , .. ) ;
[< >] . , .. ) ;

, CREATE TABLE
.1

<u11 > , .

<u "1> .

< > , .
:
INTEGER, CHARACTER, DECIMAL, NUMERIC,
SMALLINT, FLOAT, REAL, DOUBLE, PRECISION, LONG*, VARCHAR*,
DATE*, TIME*. ("*" ,
SQL).

<> < >.

< > : NOT NULL, UNIQUE, PRIMARY


, (<>), DEFAULT =< >,
REFERENCES
< > [(<. >)].

< > : UNIQUE, PRIMARY ,


(<>), DEFAULT =< >,
REFERENCES <,11
> f(<. >.,")].
SQL
: SQL, -
.
, SQL, -
"*".

ADA* ADD* ALL ALTER*


AND ANY AS ASC AUDIT*
AUTHORIZATION AVG BEGIN BETWEEN L*
* CATALOG* CHAR CHARACTER
CLOSE COBOL COLUMN*
COMMENT* I COMPUTE* CONNECT*
CONINUE COUNT CREATE CURRENT
CURSOR DATABASE* DATE* DBA*
DBSPACE* DEC DEClMAL DECLARE
DEFAUL DELETE DESC DISTINCT
DOUBLE DROP* END ESCAPE
EXISTS FETCH FLOAT
FOR FOREIGN FORAT* FORTRAN
FOUND FROM GO G
GRANT GROUP HAVING IDENIFIED*
IN INDEX* INDICATOR INSERT
INT INTEGER INTO IS
LANGUAGE LIE LONG*
MIN MODIFY* MODULE
NOT NULL NUMERIC OF
ON OPEN OPTION OR
ORDER PASCAL PLI PRECISION
PRIMARY PRIVILEGES PROCEDURE PUBLIC
REAL REFERENCES RESOURCE* REVOKE*
ROLLACK SCHEMA SECTION SELECT
S SMALLINT SOME SQL
SQLCODE SQLERROR SQLWARNING* SUM
SYNONYM* TABLE TABLESPACE* I*
TIMESTAMP* TTILE* UNION
UNIQUE UPDATE USER VALUES
VARCHAR* VIEW WHENEVER WHERE
WITH WOR

Оценить