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

SAMS

SAMS

T e a c h
Y o u r s e l f

Ben Forta

THIRD EDITION

SAMS

800 East 96th Street, Indianapolis, Indiana, 462

SAMS

3-
""
-
2005

32.973.26-018.2.75
80
681.3.07
""
..
. ..
B.C.
"" :
info@williamspublishing.com, http://www.williamspublishing .com
115419, , / 783, 03150, , / 152.

, .
80

SQL. 10 , 3- . :
. . . : "", 2005.
288 . : . . . .
ISBN 5-8459-0827-2 (.)

SQL .
, ,
, , , , .
10 .

SQL - .
, , IBM DB2, Microsoft Access, Microsoft
SQL Server, MySQL, Oracle, PostgreSQL Sybase Adaptive Server.
32.973.26-018.2.75

.
,
,
, Sams Publishing.
Authorized translation from the English language edition published by Sams
Publishing Copyright 2004
All rights reserved. No part of this book may be reproduced or transmitted in
any form or by any means, electronic or mechanical, including photocopying,
recording or by any information storage retrieval system, without permission from
the Publisher.
Russian language edition is published by Williams Publishing House according
to the Agreement with R&I Enterprises International, Copyright 2005

ISBN 5-8459-0827-2 (.)


ISBN 0-672-32567-5 (.)

"", 2005
Sams Publishing, 2004

1. SQL
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22. SQL
.
.
. SQL
. SQL
. SQL

15
19
29
35
43
51
61
69
79
89
101
113
121
133
145
153
163
169
181
193
203
211
219
235
243
259
265
273
278

?
,

1. SQL


SQL?

2.
SELECT


3.



4.
WHERE
WHERE



5.
WHERE
AND
OR

IN
NOT

6.
LIKE
" " (%)
" " (_)
" " ([ ])

7.



8.





9.

AVG ()
COUNT ()
()
MIN ()
SUMO

51
51
52
53
54
56
57
59
61
61
62
65
66
68
68
69
69
70
74
76
78
79
79
79
81
82
84
87
88
89
89
90
92
93
94
95
96
98
99

10.




SELECT

11.



12.




WHERE

13.

14.


UNION
UNION

101
101
102
104
108
110
111
113
113
114
118
120
121
121
121
123
124
126
128
129
132
133
133
134
135
137
138
142
143
144
145
145
146
146
149
150

15.




16.


17.


NULL



18.


:







19.

151
152
153
153
153
157
158
160
162
163
163
165
167
168
169
169
170
172
174
176
178
179
179
181
181
182
183
185
186
187
190
191
192
193
193
195

10

20.


ROLLBACK
COMMIT

21.




22. SQL







197
198
202
203
203
206
207
207
208
210
211
211
213
214
215
217
217
219
219
220
222
224
225
227
229
231
232

235



Vendors
Products
Customers
Orders
Orderltems

MDB-
Microsoft Access
SQL-

Aqua Data Studio

235
236
236
237
237
238
238
239
240
240

243

244


DB2
Macromedia ColdFusion
Microsoft Access
Microsoft ASP
Microsoft ASP.NET
Microsoft Query
Microsoft SQL Server
MySQL
Oracle
PHP
PostgreSQL
Query Tool
Sybase
ODBC
. SQL
ALTER TABLE
COMMIT
CREATE INDEX
CREATE PROCEDURE
CREATE TABLE
CREATE VIEW
DELETE
DROP
INSERT
INSERT SELECT
ROLLBACK
SELECT
UPDATE
. SQL




. SQL

11
245
245
246
248
249
250
251
252
252
253
253
254
255
255
259
259
260
260
260
261
261
261
262
262
262
262
263
263
265
266
268
269
270
273
278


Macromedia, 20 , , ,
. , ColdFusion Web Application Construction Kit Advanced ColdFusion
Development, Sams Teach Yourself Regular Expressions in
10 Minutes ( .
10 , ""),
Flash, Java, WAP, Windows 2000 . ,

.
Internet, .
, ,
- -.
. -, ,
. ben@forta.com
Web- h t t p : //www. f o r t a. com.


Sams .
(Mike
Stephens) (Mark Renfrow) ,

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

!
, ,
. ,
,
.
, .
.

Web- . , , ,
,
.
, , .
. :
E-mail:
inf o@williamspublishing. com
WWW:
h t t p : //www. williamspublishing. com
:
:
115419, , / 783
: 03150, , / 152

SQL .
, , , Web-
Microsoft Office, SQL .
.
Web-,
SQL. , , .
: .
SQL,
,
.
, ,
SQL.
, , , ,
.
SQL. , ,
, , , , . ,
10 .
SQL
, .
.
SQL-.


,
SQL;
SQL;
SQL ;

SQL - .

16

SQL,
,
(, Database Management System
DBMS). , SQL ,
(
).
IBM DB2
Microsoft Access
Microsoft SQL Server
MySQL
Oracle
PostgreSQL
Sybase Adaptive Server
SQL-
.


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

.
,
.
(*) ,
.
^> , .

17

,
.


- .

"" , .

"" , .

"" , .

SQL
, SQL .


, SQL, ,
. SQL ,
, , ,
.
,
. , , . -
Internet, . ,
, , .
,
PIN- .
,
, ,
. ,
, ,
. ,
.

20

1

.
, , , ,
. SQL,

.


, ( SQL) , .
. ,
, .

( ) .

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

SQL

21

, . , .
.
,
.
,
.
,

"

'

'

'

" "

, , ,
. .
.
.
,
, .

,
. .
, , , .

, , . , ,
, . , , .

22

, (
() , ).

,
.


, .

.
.

,
, .

. , , . ,
, , .

. , , () ( ZIP) .

(,
).

, .

, ,

SQL

23

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

.
, ( ) .

, (,
).


. , .

SQL.
,
. , ,

-. , ,
.

;
. , , , .

24

,
. .

.
?
,
. , .



,
.
, , ,
.
.

( ),
.

( ), , .
.
,
, .

SQL

25


,

, .

, .
.

( NULL).

.

. ( ,
.)
.
. , ,
,
,
(
).
, , 12, " ".

SQL?
SQL Structured Query
Language ( ). SQL .
(,
, , Java

26

Visual Basic), SQL .


. SQL , ,
.
SQL?
SQL , . SQL,
.
SQL .
.
, SQL
; ,
.
SQL.
SQL

SQL,
.
.
,
.
SQL ANSI, ANSI SQL.
, , ANSI SQL.
(PL-SQL, Transact-SQL ..).
ANSI
SQL. , SQL, ,
,

SQL

27


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

, SQL . SQL ,
.


,
SELECT .

SELECT
1, " SQL", SQL .
, SQL .
SELECT.
.

,
SQL. . "
SQL" .
SELECT
,
.

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

30

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


SQL- SELECT:

SELECT prod_name
FROM Products;


SELECT
prod_name Products.
SELECT, FROM , .
:

prod_name
Fish bean bag toy
Bird bean bag toy
Rabbit bean bag toy
8 inch teddy bear
12 inch teddy bear
18 inch teddy bear
Raggedy Ann
King doll
Queen doll

31


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

SELECT, , .
( ) .
.

SQL-
. SQL-
,
. SQL-
,
.


SQL-
( ,-).
, , .
,
, ,
. Sybase Adaptive Server, " "
SQL-, ;.

32

2
SQL-
, SQL- , SELECT, s e l e c t
S e l e c t . SQL-
SQL ,
. :
SQL- ,
, (
).


SELECT. ,
SELECT
.


,
. .

SELECT Products
:

SELECT prod_id, prod_name, prod_price


FROM Products;

, Products
SELECT. ,
. :

prod id
BNBG01
BNBG02
BNBG03
BR01
BR02
BR03
RGAN01
RYL01
RYL02

prod_name
Fish bean bag toyBird bean bag toy
Rabbit bean bag toy
8 inch teddy bear
12 inch teddy bear
18 inch teddy bear
Raggedy Ann
King doll
Queen doll

33

prod_price
3.4900
3 .4900
3 .4900
5.9900
8.9900
11.9900
4.9900
9.4900
9.4900


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


( ), SELECT ,
. "" (*). .

SELECT *
FROM Products;

34

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

* (
,
).
,
, .



.
(
"" ), , .


SQL- SELECT ,
.
, .

,
ORDER BY SELECT
.


, SQL . : .

SELECT prod_name
FROM Products;

prod_name
Fish bean bag toy
Bird bean bag toy
Rabbit bean bag toy
8 inch teddy bear
12 inch teddy bear
18 inch teddy bear
Raggedy Ann
King doll
Queen doll

-
. , .
, -

36

. , ,
.
( ) , .
, ,
.

SQL- ,
, .
.
FROM S E L E C T ,

SELECT ORDER BY.


, .
:

SELECT prod_name
FROM Products
ORDER BY prod_name;

, ORDER BY,
prod_name.
:

prod_name
12 inch teddy bear
18 inch teddy bear
8 inch teddy bear
Bird bean bag toy

37

Fish bean bag toyKing doll


Queen doll
Rabbit bean bag toy
Raggedy Ann
ORDER B Y
O R D E R B Y , S E L E C T .

.

,
ORDER B Y , .

, , .

. ,
,
( ,
). ,

.
,
(, ).
,
,
.

SELECT prod_id, prod_price, prod_name


FROM Products
ORDER BY prod_price, prod_name;

38


prod_id

prod_price

prod_name

BNBG02
BNBG01
BNBG03
RGAN01
BR01
BR02
RYL01
RYL02
BR03

3.4900
3.4900
3.4900
4.9900
5.9900
8.9900
9.4900
9.4900
11.9900

Bird bean bag toy


Fish bean bag toy
Rabbit bean bag toy
Raggedy Ann
8 inch teddy bear
12 inch teddy bear
King doll
Queen doll
18 inch teddv bear

,
,
. , , ,
prod_name,

prod_price. prod_jprice
, prod_name
.

,
( ).
, :

SELECT prod_id, prod_price, prod_name


FROM Products
ORDER BY 2, 3;

prod_id

prod_price

prod_name

BNBG02
BNBG01
BNBG03
RGAN01
BR01
BR02

3 .4900
3.4900
3.4900
4.9900
5.9900
8.9900

Bird bean bag toy


Fish bean bag toy
Rabbit bean bag toy
Raggedy Ann
8 inch teddy bear
12 inch teddy bear


RYL01
RYL02
BR03

9. 4900
9. 4900
11 .9900

39

King d o l l
Queen d o l l
18 inch teddy bear

|
,
.
ORDER BY. ,
SELECT. ORDER BY 2
SELECT,
prod_price. ORDER BY 2, 3 prod_price,
prod_name.

,
. . -, ,
. -,

SELECT (
ORDER BY). , , , SELECT.

,
,
SELECT.

.

( ). , ORDER BY
(
). DESC.

40


( ).

SELECT prod_id, prod_price, prod_name


FROM Products
ORDER BY prod_price DESC;

prod_id

prod_price

prod_name

BR03
RYL01
RYL02
BR02
BR01
RGAN01
BNBG01
BNBG02
BNBG03

11.9900
9.4900
9.4900
8.9900
5.9900
4.9900
3.4900
3.4900
3.4900

18 i n c h t e d d y b e a r
King d o l l
Queen d o l l
12 i n c h t e d d y b e a r
8 inch teddy bear
Raggedy Ann
F i s h bean bag t o y
B i r d bean bag t o y
R a b b i t bean bag t o y

,
?
( ),
:

SELECT prod_id, prod_price, prod_name


FROM Products
ORDER BY prod_price DESC, prod_name;

prod_xd

prod_price

prod_name

BR03
RYL01
RYL02
BR02
BR01
RGAN01
BNBG02
BNBG01
BNBG03

11.9900
9.4900
9.4900
8.9900
5.9900
4.9900
3.4900
3.4900
3.4900

18 inch teddy bear


King doll
Queen doll
12 inch teddy bear
8 inch teddy bear
Raggedy Ann
Bird bean bag toy
Fish bean bag toy
Rabbit bean bag toy

41

DESC ,
. DESC prodjprice,
prod_name. , prod_price , prod_name , .

,
DESC.
, DESC
DESCENDING, . DESC ASC (ASCENDING),
.
AS , ( , ASC, DESC).

,
? ? ,
.
,
.
.
( ,
.)
,
, ORDER BY.
.

42


ORDER BY SELECT.
,
SELECT, .


,
WHERE SELECT .


WHERE


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

SELECT prod_name, prod_price


FROM Products
WHERE prod_price = 3.49;

, , ,
prod_price 3.49:

prod_name

prod_j?rice

Fish bean bag toy

3.4 900

44

Bird bean bag toy


Rabbit bean bag toy

3.4900
3.4 900

: , , . SQL
.
PostgreSQL
PostgreSQL , , SQL-,
. ,
PostgreSQL. , , 3 . 4 9 ""
, WHERE .
~ . 4 9 = d e c i m a l ' 3 . 4 9 ' .

SQL

.
SELECT , ,
.
, .

. , ,
. ,
,
, .

45

WHERE

, ORDER BY
WHERE, , O R D E R B Y WHERE, .
( O R E D E R B Y 3.)


WHERE
WHERE, ,
, .. , . SQL () ,
. 4.1.
4 . 1 . WHERE




NULL

!=
<
<=
!<
>
>=
!>
BETWEEN
I S NULL


, . 4.1, (, <> , ! =).
! < ( )

46

4
, >= ( ).
:
.
, ,
.


.
.
, $10:

SELECT prod_name, prod_price


FROM Products
WHERE prod_price < 10;

prod name

prod__price

Fish bean bag toy


Bird bean bag toy
Rabbit bean bag toy
8 inch teddy bear
12 inch teddy bear
Raggedy Ann
King doll
Queen doll

3.4900
3.4900
3.4900
5.9900
8.9900
4.9900
9.4900
9.4900

, $10 ( ,
, , $10):

SELECT prod_name, prod_price


FROM Products
WHERE prod_price <= 10;


,
DLL01.

47

SELECT vend_id, p r o d _ p r i c e
FROM Products
1
WHERE vend i d
'DLL01 ;

vend_id

prod_name

BRS01
BRS01
BRS01
FNG01
FNG01

8 inch teddy bear


12 inch teddy bear
18 inch teddy bear
King doll
Queen doll


WHERE, , ,
.
. , ,
. .

, !=, <>:

SELECT vend_id, prod_price


FROM Products
WHERE vend id != 'DLL011;

! = <>
= <> .
. , Microsoft Access
<> !=.
,
.

48



BETWEEN.
WHERE, : . ,
BETWEEN , $5
$10, ,
.

BETWEEN ,
$5 $10:

SELECT prod_name, prod_price


FROM Products
WHERE prod_price BETWEEN 5 AND 10;

prod_name

prod_price

8 inch teddy bear


12 inch teddy bear
King doll
Queen doll

5.9900
8.9900
9.4900
9.4900

, BETWEEN
. AND. , .


,
, . , , NULL.

49

NULL

- , ,
, ,
.

SELECT WHERE,
NULL IS NULL.
:

SELECT prod_name
FROM Products
WHERE prod_jprice IS NULL;

( prodjprice , 0), , . Vendors NULL


vend_state NULL, ( ,
):

SELECT vend_id
FROM Vendors
WHERE vend state IS NULL;

vend id
FNG1
JTS01


.
.

50

,
WHERE SELECT. , , ,
, ,
NULL.


, WHERE
. , NOT IN.


WHERE
WHERE, 4,
" ", . SQL,
WHERE. : AND OR.

,

WHERE.
1.

1
statement SQL
(
),
.
(operator). . .

52

AND

, AND
WHERE.
:

SELECT prod_id, prod_price, prod_name


FROM Products
WHERE vend_id = 'DLL01' AND prod_price <=4;


, DLL01, $4 .
WHERE SELECT ,
AND .
AND
, .
DLL01, $4,
. , ,
$4
, . ,
SQL-,
:

Prod id
BNBG02
BNBGO1
BNBG03

prod_price

prod_name

3.4900
3.4900
3.4900

Bird bean bag toyFish bean bag toy


Rabbit bean bag toy

AND
, WHERE

, ,
.

53

OR
OR
AND. OR
, .
OR WHERE,
. ( ,
.)
SELECT:

SELECT prod_name, prod_price


FROM Products
1
WHERE vend id = 'DLL01 OR vend id = 'BRS01

SQL-
, . OR - ,
.
AND, . SQL- :

^ ^ ^

prod name

prod_price

Fish bean bag toy


Bird bean bag toy
Rabbit bean bag toy
8 inch teddy bear
12 inch teddy bear
18 inch teddy bear
Raggedy Ann

3.4900
3.4900
3.4900
5.9900
8.9900
11.9900
4.9900

OR
, WHERE

, , .

54

WHERE
AND OR. ,
.
AND OR
. .
DLL01 BRS01 , $10 .
SELECT
AND OR WHERE:

SELECT prod_name, prod_j?rice


FROM P r o d u c t s
WHERE vend_id = 'DLL01' OR vend_id =
AND p r o d _ p r i c e >= 10;

'BRS01

prod name

prod_price

Fish bean bag toyBird bean bag toyRabbit bean bag toy
18 inch teddy bear
Raggedy Ann

3.4900
3.4900
3.4900
11.9900
4.9900

. $10 ,
, . ?
. SQL ( ) AND, OR. SQL ""
WHERE, :
, $10 ,
BRS01, ,
DLL01 .
, AND
, " " .

one-

55

. SELECT
:

SELECT prod_name, prod_price


FROM Products
1
WHERE (vend_id = 'DLL01 OR vend_id =
AND prod_j?rice >= 10;

'BRS01')

prod_name

prod_price

18 inch teddy bear

11.9900

,
WHERE. , AND
OR, OR . , SQL- :
, DLL01, BRS01,
$10 , , .
WHERE

WHERE
AND OR,
, .
, . , , .

56


IN
IN
, .
, ,
. :

SELECT prod_name, prodjprice


FROM Products
WHERE vend_id IN ('DLL011, 'BRS01!
ORDER BY prod_name

prod name

prod_price

12 inch teddy bear


18 inch teddy bear
8 inch teddy bear
Bird bean bag toy
Fish bean bag toy
Rabbit bean bag toy
Raggedy Ann

8.9900
11.9900
5.9900
3.4900
3.4900
3.4900
4.9900

f ^ T H L 5

SELECT ,
DLL01 BRS01.
IN ,
.
, IN
, OR, . SQL- , :

SELECT prod_name, prod_price


FROM Products
WHERE vend_id = 'DLL01' OR vend_id = 'BRS01
ORDER BY prod_name;

57

prod_name

prod_price

12 inch teddy bear


18 inch teddy bear
8 inch teddy bear
Bird bean bag toy
Fish bean bag toy
Rabbit bean bag toy
Raggedy Ann

8.9900
11.9900
5.9900
3.4900
3.4900
3.4900
4.9900

IN?
.
IN
.
IN
(
).
IN , OR.

IN ,
SELECT, WHERE.
11, " ".
IN
, WHERE

,
,
OR.


NOT
NOT WHERE

58

, . NOT
( ),
. , NOT
,
, .
N

;-,-

; '

:' :

':':

, WHERE

- .

NOT. , , DLL01,
:

SELECT prod_name
FROM Products
WHERE NOT vend_id = 'DLL01 1
ORDER BY prod_name;

prod_name
12 inch teddy bear
18 inch teddy bear
8 inch teddy bear
King doll
Queen doll

NOT ,
.
vend_id, DLL01, .

<>:

SELECT prod_name
FROM Products
WHERE vend_id 'DLL01'
ORDER BY prod_name;

59

prod_name
12 inch teddy bear
18 inch teddy bear
8 inch teddy bear
King doll
Queen doll

NOT? , WHERE,
, . . , , , NOT
IN.
NOT MySQL
NOT,
, MySQL
MySQL NOT

E X I S T S

(..

N O T

E X I S T S ) .

,
WHERE AND OR.
, IN NOT.

, ,
LIKE
.


LIKE
, ,
.
,

. . . , , bean bag?
,
. . , ,
bean bag,
, bean
bag .

62

:ji~~?JU:
^^^

,
.

, ,
.

, WHERE . SQL
.
,
LIKE. ,

, .

? ,
. , L I K E
, .
, ,
SQL.

(),
.

" " (%)


(%). %
. , , Fish, :

63

SELECT prod_id, prod_name


FROM Products
WHERE prod_name LIKE 'Fish%';

prod_id

prod_narae

BNBG01

Fish bean bag toy

'Fish%'.
,
Fish. %
Fish
.
Microsoft Access
Microsoft Access,
* %.

,
.
f ish%' F i s h bean bag t o y .

, . ,
.

SELECT prod_id, prod_name


FROM Products
WHERE prod_name LIKE %bean bag%';

64

prod_id

prod_name

BNBG01
BNBG02
BNBG03

Fish bean bag toy


Bird bean bag toy
Rabbit bean bag toy

' %bean bag%' , bean bag , .



, .
, F :

SELECT prod_name
FROM Products
WHERE prod_name LIKE 'F%yT;

, , %
.

, Microsoft Access,
. , 50 ,
F i s h bean bag t o y (17 ), , , 33
. , SQL-. WHERE
prod_name LIKE ' F l y '
prod_name, F . , ,
F i s h bean bag t o y .

65

% : F%y%',
() .
"" , 8, " ".

" " (_)



(_).
, %, , .
Microsoft Access
Microsoft Access, ? .

SELECT prod_id, prod_name


FROM Products
WHERE prod_name LIKE ' inch teddy bear';

, , % ,
.

prod_id

prod_name

BNBG02
BNBG03

12 inch teddy bear


18 inch teddy bear

WHERE , . , -

66

: 12 18 . 8
inch teddy bear , , . ,
SELECT %,
:

SELECT prod_id, prod_name


FROM Products
WHERE prod_name LIKE '% inch teddy bear

prod_id

prod_name

BNBG01
BNBG02
BNBG03

8 inch teddy bear


12 inch teddy bear
18 inch teddy bear

%, , _
.

" " ([ ])
" " ([ ])
,
,
( ).

, , . Microsoft Access, Microsoft SQL
Server Sybase Adaptive Server. , , .

, , J ,
:

67

SELECT prod_id, prod_name


FROM Customers
WHERE cust_contact LIKE [JM]
ORDER BY cust contact

cust_contact
Jim Jones
John Smith
Michelle Green

WHERE
[ JM] %'.
. [JM]
,
,
.
. %, [ JM], , .
, , . , ,
J (
):
1

SELECT prod_id, prod_name


FROM Customers
WHERE cust_contact LIKE ' [AJM] %
ORDER BY cust contact

Microsoft Access
Microsoft Access
, ,

68

, , NOT.
WHERE.
!
( [ ] ) .
,
, .

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

.

, WHERE.
, ,
.

, ,

.


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

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

, .

70

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

,
, ,
,
.

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


,
, .

71

Vendors
. ,

{) .
,
: vend_name vend_country.
, vend__country
, .
SELECT, ,
, ?

(
) "" .

. SQL SELECT
. "" (+) ( | | ) .
+ | |
Access, SQL Server Sybase
+. DB2, Oracle, PostgreSQL
Sybase | |. .
- | |
, .

"'
( , ):

SELECT vend_name +
FROM Vendors
ORDER BY vend_name;

(' + vend_country + ')

Bear Emporium

(USA

72

Beras R Us
Doll House Inc.
Fun and Games
Furball Inc.
Jouets et ours

(USA
(USA
(England
(USA
(France

)
)
)
)
)

,
I I:

SELECT vend_name || (' || vend_country


FROM Vendors
ORDER BY vend name;

Bear Emporium
Beras R Us
Doll House Inc.
Fun and Games
Furball Inc.
Jouets et ours

(USA
(USA
(USA
(England
(USA
(France

SELECT :
, vend_name;
,
;
, vend_country;
, .
,
SELECT ( ), .
MySQL
MySQL + | | . CONCAT'{),
, .
CONCAT ()
:

73

SELECT GONCAT (vend__name, ' ,


f
vend_country,
')')
MySQL
|, . MySQL || OR, a && AND.

, SELECT. ,
, .
( ) . ,
. SQL-
RTRIMO :

SELECT RTRIM(vend_name)
FROM Vendors
ORDER BY vend name;

(' + RTRIM(vend_country)

+ ')

Bear Emporium (USA)


Beras R Us (USA)
Doll House Inc. (USA)
Fun and Games (England)
Furball Inc. (USA)
Jouets et ours (France)

,
I | :

SELECT RTRIM(vend_name)
FROM V e n d o r s
ORDER BY v e n d name;

Bear Emporium (USA)


Beras R Us (USA)
Doll House Inc. (USA)
Fun and Games (England)
Furball Inc. (USA)
Jouets et ours (France)

RTRIM(vend_country)

])

74

RTRIMO
. RTRIM ()
. , ,
ZIP- .
TRIM

R T R I M () (, , ""
), L T R I M () (
), T R I M ( ""
).


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

, .
SQL .
.
AS. SELECT:

SELECT RTRIM(vend_name)
^AS v e n d _ t i t l e
FROM Vendors
ORDER BY vend name;

vend_title
Bear Emporium (USA)

(' + RTRIM(vend_country) + ')

75

Beras R Us (USA)
Doll House Inc. (USA)
Fun and Games (England)
Furball Inc. (USA)
Jouets et ours (France)

, | | :

SELECT RTRIM(vend_name)
^AS v e n d _ t i t l e
FROM Vendors
ORDER BY vend name;

RTRIM(vend_country)

vend title
Bear Emporium (USA)
Beras R Us (USA)
Doll House Inc. (USA)
Fun and Games (England)
Furball Inc. (USA)
Jouets et ours (France)

SELECT
, ,
AS v e n d _ t i t l e . ,
SQL , , v e n d _ t i t l e . ,
, v e n d _ t i t l e

, .

-.
, (, )
.

76

7

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


" ", , ,
, .


. . Orders
, Order Items
.
SQL- 2 0008:

SELECT prod_id, quantity, item_price


FROM Orderltems
WHERE order nam = 20008;

prod id

quantity

item_price

RGAN01
BR03
BNBG01

5
5
10

4.9900
11.9900
3.4900


BNBG02
BNBG03

10
10

77

3.4900
3 .4900

item_price
, .
( ,
), :

SELECT prod_id,
quantity,
item_price
quantity*item_j?rice AS expanded_price
FROM OrderItems
WHERE order nam = 20008;

prod_id
quantity
expanded_price

item_price

RGAN01
BR03
BNBG01
BNBG02
BNBG03

4.9900
11.9900
3.4900
3.4900
3.4900

5
5
10
10
10

24.9500
59.9500
34.9000
34.9000
34.9000

expanded_price,
, ; : quantity* itera_price.

, .
SQL , . 7.1. ,
. 5, " ",
.
7 . 1 . SQL

78

,
.
. , ,
, .

, ,
. , SQL- .


,
SQL . ,
, .
RTRIM (),
.


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

80

, . ,
, . 8.1 .
8 . 1 .

Access
MID ( ) . DB2, Oracle PostgreSQL SUBSTR () .
MySQL, SQL Server Sybase
SUBSTRING()
Access Oracle ,
. DB2
PostgreSQL
CAST ( ) . MySQL, SQL Server Sybase
CONVERT ()

Access NOW ( ) .
DB2 PostgreSQL CURRENT_DATE.
MySQL
CURDATE () . Oracle SYSDATE.
SQL Server Sybase GETDATE ()

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

, .

81

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


SQL .
;
(,

).
;

(,
).
; (,
).
; , (, ).
, SELECT,
. SELECT (,
WHERE), SQL- (
).

82


RTRIM ()
.
, UPPER ():

SELECT vend_name UPPER(vend_name) AS vend_name_upcase


FROM Vendors
ORDER BY vend name;

vend name

vend_name_upcase

Bear Emporium
Beras R Us
Doll House Inc.
Fun and Games
Furball Inc.
Jouets et ours

BEAR EMPORIUM
BERAS R US
DOLL HOUSE INC.
FUN AND GAMES
FURBALL INC.
JOUETS ET OURS

UPPER ()
, , : ,
Vendors,
,
vend_name_upase.
. 8.2
.
. 8.2 . SOUNDEX , - ,
.
SOUNDEX , , , , . SOUNDEX
SQL, .

83

8.2.

LEFT () ( )

LENGTH (
DATALENGTH () LEN () )

LOWER ()

LTRIM () (LCASE () Access)


RIGHT () (

)
RTRIM()
SOUNDEX ()
UPPER () (UCASE Access)

SOUNDEX

SOUNDEX

S O U N D E X (). Microsoft

Access PostgreSQL,
.

SOUNDEX (). Kids Place


Customers Michelle Green.
,
Michael Green? , , :

SELECT cust_name cust_contract


FROM Customers
WHERE cust contract = 'Michael Green1

84

cust name

cust contract


SOUNDEX (),
, Michael Green:

SELECT cust_name cust_contract


FROM Customers
WHERE SOUNDEX(cust_contract) = SOUNDEX('Michael
1
OGreen ) ;

cust_name
Kids Place

cust_contract
Michelle Green

WHERE
SOUNDEX ()
cust_contact SOUNDEX-.
Michael Green Michelle Green , SOUNDEX- WHERE
.

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

85

SQL.

,
. Orders
. , 2 004 , SQL Server Sybase :

SELECT order_num
FROM Orders
WHERE DATEPART(yy, order_date) = 2004;

order num
20005
20006
20007
20008
20009

Access :

SELECT order_num
FROM Orders
WHERE DATEPART('yyyy*,

order_date) = 2004;

( SQL Server Sybase


Access) DATE PART (), ,
, . DATE PART () : , , , . DATEPARTO
order_column . 2 004 WHERE , .
PostgreSQL,
DATE_PART ():

86

SELECT order_num
FROM Orders
WHERE DATE_PART('year , order_date) = 2004;

MySQL, DATEPARTO,
, . MySQL YEAR () :

SELECT order_num
FROM Orders
WHERE YEAR(order_date) = 2004;

Oracle DATE PART (),


, .
:

SELECT order_num
FROM Orders
WHERE t o number(to c h a r ( o r d e r

date,

YY 1 ))

= 2004;

to_char ()
, to_number()
,
2 004.
BETWEEN:

SELECT order_num
FROM Orders
WHERE o r d e r _ d a t e BETWEEN to_date('01-JAN-2004
AND t o d a t e ( ' 3 1 - D E C - 2 0 0 4 ' ) ;

Oracle to_date ()
.
1 2004, 31 2004. -

87

BETWEEN
, .
SQL Server,
to_date ().
to_date () DATAPART (),
.
Oracle
-- (
) Oracle ,
, to_date.()... .


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

.
, .

. , , ,
.

. . 8.3 .

88

8.3.


ABS ()

COS ()

()

PI ()

SIN ()

SQRT ()

TAN ()

, ,
.

, SQL . ,
, ,
SQL.


, SQL


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

90

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

AVG ()

COUNT ()

()

MIN ()

SUM ()

AVG ()
AVG ()

.

.
AVG () Products:

SELECT AVG(prod_price) AS avg_jprice


FROM P r o d u c t s ;

avg_price
6.823333

91

SELECT, ,
, avg_price,
Products. avg_price
, 7, "
."
AVG () .
,
:

SELECT AVG(prod_price) AS avg_j>rice


FROM Products
WHERE v e n d i d = ' D L L 0 1 1 ;

avg_price
6.8650

SELECT , WHERE. WHERE , vend_id


DLL01, ,
avg_price,
.

AVGO

,
.
,
AVG ( ) .

92

9
NULL
, N U L L , A V G ( ) .

COUNT ()

COUNT () . COUNT ()
,
.
:
COUNT {*)
, NULL
.
COUNT (column) , ,
NULL .
, Customers:

SELECT COUNT(*) AS num_cust


FROM Cu s t ome r s;

num_cust
5

COUNT (*)
. num_cust.
,
:

SELECT COUNT(cust_email) AS num_cust


FROM Customers;

93

num_cust
3

SELECT
COUNT (cust_email) ,
cust_email.
cust_email 3 ( , 3
5 ).
NULL
N U L L
C O U N T ( ) ,

, (*).

()
()
. , :

SELECT MAX(prod_price) AS max_price


FROM Products;

max_price
11.9900

MAX () Products.

94

( )

{)
, ( )
, . (} , , .

NULL
N U L L
( ) .

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

SELECT MIN(prod_price) AS min_price


FROM Products;

min_pnce
3.4900

MIN ()
P r o d u c t s .


M I N ( )

95

W I N
, ( )
, . M I N () , ,
.

N U L L

N U L L

SUMO
SUM() ()
.
, .
Orderltems , . (
quantity)
:

SELECT SUM(quantity) AS item_ordered


FROM Orderltems
WHERE order item = 20005;

item_ordered
200

96

SUM (quantity) , WHERE , .

SELECT SUM(item_price*quantity) AS total_price


FROM OrderIterns
WHERE order item = 20005;

total_price
1648.0000

SUM(item_price*quantity) , WHERE ,
.



, .

N U L L

N U L L
SUM ( ) .

:
ALL -
( ALL );

97

DISTINCT.
ALL

A L L ,
.
D I S T I N C T ,
A L L .

..

.-/"

TO
'

: ' - . -

'

Access
Microsoft Access
D I S T I N C T ,

Access.

AVG ()
, . SELECT,
,
DISTINCT .

SELECT AVG(DISTINCT prod_price) AS avg_price


FROM Products
WHERE v e n d i d = ' L L 0 1 ' ;

avg_price
4.2400


DISTINCT avg_j?rice , . ,
.

98

D I S T I N C T

COUNT (} ,
.
C O U N T ( * ) . , D I S T I N C T , .


M I N () ()

DISTINCT

D I S T I N C T MIN{)
( ) , .
, ,
.

D I S T I N C T A L L ,
,
PERCENT,

.
, ,
.


,
, . SELECT
, . :

99

SELECT COUNT(*) AS num_items,


MIN(prodjprice) AS price_min,
11 (prod_price) AS price_max,
AVG(prod_price) AS proce_avg
FROM Products;

num items

price_min

price__max

price_avg

3.4900

11.9900

6.823333

SELECT

( Products, , ).



,
SQL .


. SQL ,
. ,
,
.

10


,
, . SELECT, GROUP BY
HAVING.


,
SQL . ,
,
, .

, WHERE. , , DLL01:

SELECT COUNT(*) AS num_prods


FROM Products
WHERE v e n d i d = ' D L L O l ' ;

num_j?rods

, ,
? ,
, , , ?

102

10

.

,
.


GROUP BY
SELECT.
:

SELECT vend_id, COUNT(*) AS numjprods


FROM Products
GROUP BY vend id;

vend id

num_prods

BRS01
DLL01
FNG01

SELECT vend_id,
, num_prods, ( COUNT (*)).
GROUP BY vend_id.
num_prods
vend_id,
products. , ,
BRS01 ,
DLL01 , FNG01 2 .
GROUP BY,
,
.
. GROUP BY

, .

103
GROUP BY, , .
GROUP BY
, .
,
,
.

GROUP BY,
. , ,
(
).

GROUP BY, ( ).
SELECT - ,
GROUP BY. .

SQL
GROUP BY , (.. , ).
, ,
SELECT,
GROUP BY.
, ,
NULL,
.
NULL, .

GROUP BY WHERE -
ORDER BY.
A L L

SQL (, Microsoft
SQL Server) -

104

10
A L L GROUP BY.

,
,
(
NULL).
, , A L L . V- .. / [ .
: ..-.;:


SQL
GROUP B Y
SELECT. , GROUP

BY

2,1

/ "" ,
SQL.
, SQL.


GROUP BY, SQL , , . , ,
.
, , ,
.
, WHERE (
, 4, " ").
WHERE
, WHERE ,
. , WHERE " ",
.

105

WHERE? SQL ,
: HAVING. HAVING
WHERE. ,
WHERE,
, HAVING. ,
WHERE , a HAVING .

HAVING


4 5 ,
WHERE ( ).

HAVING. , .
, . .

SELECT cust_id, COUNT(*) AS orders


FROM Orders
GROUP BY cust_id
HAVING COUNT(*) >= 2;

cust_id

orders

1000000001

SELECT
, . HAVING,
COUNT (*) >= 2
.
, WHERE , , .

106

10

HAVING WHERE
: WHERE
, , a H A V I N G

, . ; ,
WHERE, , , , , HAVING.

: WHERE,
HAVING ?
, . , , ,
12 .
,
WHERE, , 12 . HAVING, ,
.
, , ,
4 :

SELECT vend_id, COUNT(*) AS num_prods


FROM Products
WHERE prod_price >= 4
GROUP BY vend_id
HAVING COUNT(*) >= 2;

vend id

num_jprods

BRS01
FNG01

3
2

107

.
SELECT, , , . WHERE
prod_price 4.
vend_id,
HAVING , .
WHERE (, 4 , 4),
.

SELECT vend_id, COUNT(*) AS num_prods


FROM Products
GROUP BY vend_id
HAVING COUNT(*) >= 2;

vend id

num_prods

BRS01
DLL01
FNG01
H A V I N G WHERE
H A V I N G
WHERE,
,
GROUP B Y . ,

H A V I N G GROUP

BY, a

W H E R E

108

10


, GROUP BY ORDER
BY , .
. 10.1.
1 0 . 1 . ORDER BY
GROUP BY

ORDER BY


(

SELECT)

GROUP BY
.


;

,
( )

, . 10.1, . , ,
GROUP BY,
.
, SQL. ,
, GROUP BY,
-.
, ( ), , .
ORDER BY, GROUP BY.

109
ORDER BY
, , GROUP BY, -

ORDER BY. , , . ,
GROUP B Y .

, GROUP BY ORDER BY, . SELECT ,


: , , .

SELECT order_num, COUNT(*] AS items


FROM OrderIterns
GROUP BY order_num
HAVING COUNT(*) >= 3;

order num

items

20006
20007
20008
20009

, , , ORDER BY, :

SELECT order_num, COUNT(*) AS items


FROM OrderIterns
GROUP BY order_num
HAVING COUNT(*) >= 3;
ORDER BY items, order num;

110

10
Access

f f

Microsoft Access
,
. items
( ORDER BY) . ,

ORDER

B Y

C O U N T (*}',

o r d e r _ n u m

ORDER BY 1 , o r d e r num.

order num

items

20006
20009
20007
20008

GROUP BY
(
order_num), COUNT (*)
.
HAVING , . ,
ORDER BY.


SELECT
SELECT . . 10.2 , , ,
.

111
1 0 . 2 . S E L E C T

SELECT

FROM

...

WHERE

GROUP BY

HAVING

ORDER BY

9, " ," , SQL


. ,
GROUP BY
,
. , HAVING ,
, ORDER BY
GROUP BY WHERE HAVING.

11

,
.


SELECT SQL.
SELECT, ,
:
.

- SQL. SELECT.

SQL : , .
, . ? .
MySQL
MySQL, ,
4.1.
MySQL .

114

11

,
, (. ,
). .
O r d e r s () ; , .
O r d e r l t e m s . O r d e r s
.
.
Customers ().
,
, RGAN01.
, ? .
1. ,
RGAN01.
2. ,
,
, .
3. ,
.
. , , SELECT,
WHERE SELECT.
,
-
.
SELECT order_num
, p r o d _ i d
RGAN01.
, :

115

SELECT order_num
FROM OrderIterns
1
WHERE prod_id = 'RGAN01

order_num
20007
20008


, 20007 20008.
IN, 5, " ", SELECT:

SELECT cust_id
FROM Orders
WHERE order num IN (20007,20008);

cust_id
1000000004
1000000005


(, )
. SELECT:

SELECT c u s t _ i d
FROM O r d e r s
WHERE o r d e r _ n u m IN (SELECT o r d e r _ n u m
FROM O r d e r l t e m s
WHERE p r o d _ i d = 'RGAN01');

cust_id
1000000004
1000000005

116

11

,
SELECT " ". .
:
SELECT order_nura FROM o r d e r i t e m s WHERE p r o d _ i d = ' R G A N 0 1 '


, 20007 20008.
WHERE , IN. :
SELECT c u s t _ i d FROM o r d e r s WHERE order_num IN (20007,20008)

, ,

WHERE .
SQL-
S E L E C T , ,

,
.
, , .

, RG7AN01. . SQL, , :

SELECT cust_name, cust_contact


FROM Customers
WHERE cust_id IN ('1000000004', 4000000005');


WHERE :

117

SELECT cust_name, cust_contact


FROM Customers
WHERE cust_id IN (SELECT cust_id
FROM Orders
WHERE order_num IN (SELECT order_num
FROM Orderltems
WHERE prod_id = 'RGAN01'));

cust name

cust contact

Fun4All
The Toy Store

Denise L. Stephens
Kim Howard

SELECT,

SELECT.
, WHERE ,
. ,
WHERE .
.
,
SQL-. ,
,
,
.

SELECT .
.



. -

118

11

.
12, " ",
.

. ,
,
Customers ().
Orders .
,
.
1. Customers.
2.
Orders.
, SELECT COUNT (*)
, WHERE
, . ,
,
1000000001:

SELECT COUNT(*) AS orders


FROM Orders
WHERE cust_id = '1000000001';


COUNT (*) ,
COUNT* . :

SELECT cust_name,
cust state,

119
(SELECT COUNT(*)
FROM Orders
WHERE Orders.cust_id = Customers.cust_id) AS orders
FROM Customers
ORDER BY cust name;

cust name

cust state

orders

Fun4All
Fun4All
Kids Place
The Toy Store
Village Toys

IN
AZ
OH
IL
MI

1
1
0
2

SELECT Customers: cust_name,


c u s t _ s t a t e orders. Orders ; , .
.
,
.
WHERE
WHERE, , . SQL,
cust_id Orders , Customers:
WHERE Orders.cust_id = Customers.cust_id

,
.
cust_id,
Customers Orders.
, cust_id Orders
.
SELECT COUNT(*) FROM O r d e r s WHERE c u s t _ i d = c u s t _ i d


Orders, , :

120

11

SELECT cust_name,
cust_state,
(SELECT COUNT(*)
FROM Orders
WHERE cust_id = cust_id) AS orders
FROM Customers
ORDER BY cust name;

cust name

cust state

orders

Fun4All
Fun4All
Kids Place
The Toy Store
Village Toys

IN
AZ
OH
IL
MI

5
5
5
5
5

SELECT ,
,
.

, ,
,
.
.

, ?
. i
IN WHERE
.
.

12


, ,

SELECT, .



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

.


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

122

12

( ,
)?
.

;

.
(, ), , .
( ,

), ,
.

.
,
,
.
,
,
. ()
( () ).
:
,
. Vendors , ,
.
, ,
, - () .
Products
, , (
Vendors).
Vendors Products. -

123


Vendors .
? .
,
.
,
, Vendors.
.
, ,
, , .
,
.

, .

, . ,
() .



,
. .
, SELECT? : . , ,
(
""). , , , " "
.

124

12

, " " , .
.
,

.
. ,
. : Products , ,
.
(.. ,
Vendors) Products.
( ) ,
, . .


.
, , "" ,
. .

125

SELECT vend_name, prod_name, p r o d _ p r i c e


FROM Vendors, Products
WHERE Vendors.vend i d = Products.vend i d ;

vend name

Doll House Inc.


Doll House Inc.
Doll House Inc.
Bears R Us
Bears R Us
Bears R Us
Doll House Inc.
Fun and Games
Fun and Games

prod_name

prod_j?rice

Fish bean bag toy


Bird bean bag toyRabbit bean bag toy
8 inch teddy bear
12 inch teddy bear
18 inch teddy bear
Raggedy Ann
King doll
Queen doll

3.4900
3.4900
3.4900
5.9900
8.9900
11.9900
4.9900
9.4900
9.4900

. SELECT
, ,
, ,
.
, (prod_name
p r o d _ p r i c e ) ,
(vend_name) .
FROM.
SELECT, , FROM, Vendors P r o d u c t s .
,
SELECT.

WHERE,
v e n d _ i d Vendors
vend_id P r o d u c t s .
,
V e n d o r s . v e n d _ i d P r o d u c t s . v e n d _ i d .
, ,
vend_id, ,
v e n d _ i d . ( ,
). , SELECT .

126

12

( )
, , .

,
,
.

WHERE
WHERE ,
. : SELECT,
" ".
, .
. , , ,
, . WHERE
,
,
.
WHERE

, .

,
. , .

, SELECT .

SELECT vend_name, prod_name, prod_price


FROM Vendors, Products;

vend name

prod_name

prod_price

Bears R Us
Bears R Us
Bears R Us
Bears R Us
Bears R Us
Bears R Us
Bears R Us
Bears R Us
Bears R Us
Bear Emporium
Bear Emporium
Bear Emporium
Bear Emporium
Bear Emporium
Bear Emporium
Bear Emporium
Bear Emporium
Bear Emporium
Doll House Inc.
Doll House Inc.
Doll House Inc.
Doll House Inc.
Doll House Inc.
Doll House Inc.
Doll House Inc.
Doll House Inc.
Doll House Inc.
Furball Inc.
Furball Inc.
Furball Inc.
Furball Inc.
Furball Inc.
Furball Inc.
Furball Inc.
Furball Inc.
Furball Inc.
Fun and Games
Fun and Games
Fun and Games
Fun and Games
Fun and Games
Fun and Games
Fun and Games

8 inch teddy bear


12 inch teddy bear
18 inch teddy bear
Fish bean bag toy
Bird bean bag toy
Rabbit bean bag toy
Raggedy Ann
King doll
Queen doll
8 inch teddy bear
12 inch teddy bear
18 inch teddy bear
Fish bean bag toy
Bird bean bag toy
Rabbit bean bag toy
Raggedy Ann
King doll
Queen doll
8 inch teddy bear
12 inch teddy bear
18 inch teddy bear
Fish bean bag toy
Bird bean bag toy
Rabbit bean bag toy
Raggedy Ann
King doll
Queen doll
8 inch teddy bear
12 inch teddy bear
18 inch teddy bear
Fish bean bag toy
Bird bean bag toy
Rabbit bean bag toy
Raggedy Ann
King doll
Queen doll
8 inch teddy bear
12 inch teddy bear
18 inch teddy bear
Fish bean bag toy
Bird bean bag toy
Rabbit bean bag toy
Raggedy Ann

5.99
8.99
11.99
3.49
3.49
3.49
4.99
9.49
9.49
5.99
8.99
11.99
3.49
3.49
3.49
4.99
9.49
9.49
5.99
8.99
11.99
3.49
3.49
3.49
4.99
9.49
9.49
5.99
8.99
11.99
3.49
3.49
3.49
4.99
9.49
9.49
5.99
8.99
11.99
3.49
3.49
3.49
4.99

127

128 12
Fun and Games
Fun and Games
Jouets et ours
Jouets et ours
Jouets et ours
Jouets et ours
Jouets et ours
Jouets et ours
Jouets et ours
Jouets et ours
Jouets et ours

King doll
Queen doll
8 inch teddy bear
12 inch teddy bear
18 inch teddy bear
Fish bean bag toy
Bird bean bag toy
Rabbit bean bag toy
Raggedy Ann
King d o l l
Queen d o l l

9.49
9.49
5.99
8.99
11.99
3.49
3.49
3.49
4.99

9.49
9.49

,
, ,
. , , , " " (
, ).
WHERE

,
WHERE, , . , ,
WHERE .
, .

,
, .


, ,
. .
, . SELECT , .

129

SELECT vend_name, prod_name, prod_price


FROM Vendors INNER JOIN Products
ON Vendors.vend id = Products.vend id;

SELECT , , FROM .
FROM, INNER JOIN. ON
WHERE. , ON,
, WHERE.
,
.
""
ANSI SQL, I N N E R J O I N .


SQL ,
SELECT. .
, . .
SELECT prod_name, vend_name, prod_price, quantity
FROM Orderltems, Products, Vendors
WHERE Products.vend_id = Vendors.vend_id
AND Orderltems .prod_id = Products .prod__id
AND order num = 20007;

prod_price

prod_name

vend name

18 inch teddy bear


Fish bean bag toy

11.9900
Bears R Us
Doll House Inc. 3.4900

quantity
50
100

130

12

B i r d b e a n bag t o y
Rabbit bean bag t o y
Raggedy Ann

D o l l House I n c . 3.4900
D o l l House I n c . 3.4900
D o l l House I n c . 4 . 9 9 0 0

100
100
50


20 007. Order Items.
,
Products. Vendors , . FROM
, WHERE . WHERE
2 0 007.

,
.
,
. , .


SQL -
,
. , ( ).


11, " ",
SELECT ,
RGAN01:

131

SELECT cust_name, cust_contact


FROM Customers, Orders, Orderltems
WHERE Customers . cust__id = Orders . cust_id
AND Orderltems.order_num = Orders.order_num
1
AND prod_id 'RGANOl ;

cust name

cust contact

Fun4All
The Toy Store

Denise L. Stephens
Kim Howard

11,
.
.
WHERE. , RGANOl.

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

132

12

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

13



. , .

, 7, " " , . :

SELECT RTRIM(vend_name) + ' (' + RTRIM(vend_country)


%+ ' ) ' AS vend_title
FROM Vendors
ORDER BY vend_name;


, SQL .
:
SQL;

SELECT.
SELECT.
, -

134

13

, :

SELECT cust_name, cust_contact


FROM Customers AS C, Orders AS O, Orderltems AS 01
WHERE C.cust_id = O.cust_id
AND OI.order_num = O.order_num
AND prod_id = 'RGAN011;

, FROM
. Customers AS
Customers ..,
Customers. WHERE,
. SELECT, ORDER BY,
.
AS Oracle
Oracle A S .


Oracle, AS
(.. Customers Customers AS ).
,
. , .



, .
: , .

135


SELECT. .
, ,
, .
, ,
,
.
.

SELECT cust_id, cust_name, cust_contact


FROM Customers
WHERE cust_name (SELECT cust_name
FROM Customers
WHERE cust contact = 'Jim Jones');

cust id

cust name

cust contact

1000000003
1000000004

Fun4All
Fun4All

Jim Jones
Denise L. Stephens

. SELECT ,
(cust_name),
.
WHERE ,
, . ( 11, " ".
, .

SELECT cl.cust_id, cl.cust_name, cl.cust_contact


FROM Customers AS cl, Customers AS c2
WHERE cl.cust_name = c2.cust_name
AND c2.cust contact = 'Jim Jones';

136

13

cust

id

1000000003
1000000004

cust name

cust contact

Fun4All
Fun4All

Jim Jones
Denise L. Stephens

AS Oracle
Oracle,
AS.

, ,
,
Customers FROM .
,
Customers ,
" ", Customers
.
.
Customers
1, 2.
. ,
SELECT 1
. ,
,
cust_id, cust_name
cust_contact. , (
). WHERE ,
cust_contact, .

, , , .
, -

137

* . / ,
.


, ,
(.. , ). ( , ) ,
.
,
.
,
.
(SELECT *)
. :

SELECT .*, .order_num, O.order_date, OI.prod_id,


>01 .quantity, OI. item_j?rice
FROM Customers AS C, Orders AS O, Orderltems AS OI
WHERE C.cust_id = O.cust_id
AND OI.order_num = .order_num
1
AND prod_id = 'RGAN01 ;
A S Oracle

Oracle,
AS.


. , .
, ,
, , , -

138

13

, .


, , . , :
, , ;
, , ;
, .
,
.
.

, ,
,
SQL. ,
, ,
, , .

SELECT .
:

SELECT Customers.cust_id, Orders.order_num


FROM Customers INNER JOIN Orders
ON Customers.cust id = Orders.cust id;

139
.
, ,
, .

SELECT Customers.cust_id, Orders.order_num


FROM Customers LEFT OUTER JOIN Orders
ON Customers.cust id = Orders.cust id;

cust id

order num

1000000001
1000000002
1000000003
1000000004
1000000005

20005
NULL
20006
20007
20008

, , SELECT
OUTER JOIN
(
WHERE). , ,
,
, . OUTER
JOIN RIGHT
LEFT, ,
(RIGHT ,
OUTER JOIN, LEFT ,
).

LEFT OUTER JOIN , FROM (
Customers). , ,
(RIGHT OUTER JOIN), .

SELECT Customers.cust_id, Orders.order_num


FROM Customers RIGHT OUTER JOIN Orders
ON Orders.cust id = Customers.cust id;

140

13

SQL Server
.
, , , .

SELECT Customers.cust_id, Orders.order_num


FROM Customers, Orders
WHERE Customers.cust id *= Orders.cust id;

cust id

order_num

1000000001
1000000001
1000000002
1000000003
1000000004
1000000005

20005
20009
NULL
20006
20007
20008


WHERE.
= *= ,
Customers. *=
.
.
, : =*.
,
,
.

SELECT Customers.cust_id, Orders.order_num


FROM Customers, Orders
WHERE Orders.cust_id =* Customers.cust_id;

(
Oracle)
( + ) , .

141

SELECT Customers.cust_id, Orders.order_num


FROM Customers, Orders
WHERE Customers.cust id (+) = Orders.cust id



.
,
. ,
FROM WHERE.

, , ,
, .

,
,
.
,
,
.
:

SELECT Customers.cust_id, Orders.order_num


FROM Orders FULL OUTER JOIN Customers
ON Orders.cust id = Customers.cust id;


Access, MySQL, SQL Server
Sybase.

142

13



9, " ", .
, .
. , .
,
COUNT() .

SELECT Customers.cust_id, COUNT(Orders.order_num) AS


FROM Customers INNER JOIN Orders
ON Customers.cust_id = Orders.cust_id
GROUP BY Customers.cust id;

cust_id

num_ord

1000000001
1000000003
1000000004
1000000005

2
1
1
1

SELECT
INNER JOIN Customers Orders
. GROUP BY , , ,
COUNT (Orders.order_num)
num_ord.
:

143

SELECT Customers.cust_id,

COUNT(Orders.order_num)

AS

FROM Customers LEFT OUTER JOIN Orders


ON Customers.cust_id = Orders.cust_id
GROUP BY Customers.cust id;
AS Oracle
Oracle AS .

cust

id

num ord

1000000001
1000000002
1000000003
1000000004
1000000005


, , . ,
1000000002 ,
0 .



, ,
,
.
,
. , ,

.

144

13

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

.
,
, .
.

, . , , ,
, . ,
, .

14

, UNION
SELECT
.

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

;

.

146

14
WHERE

,
WHERE. , ,
SELECT WHERE
.

SQL UNION. UNION SELECT,


.

UNION
UNION . ,
,
SELECT UNION
.
. , , ,
. Fun4All . , WHERE, , UNION.
, UNION SELECT.
:

147

SELECT cust_name, cust_contact, cust_email


FROM Customers
1
1
WHERE cust state IN (IL ,'IN ,"MI');

EH
cust_name

cust_contact

cust email

Village Toys
Fun4All
The Toy Store

John Smith
Jim Jones
Kim Howard

sales@villagetoys.com
j j ones@fun4all.com
NULL

SELECT cust_name, cust_contact, cust_email


FROM Customers
WHERE cust name = 'Fun4All';

cust name

cust_contact

cust_email

Fun4All
Fun4All

Jim Jones
Denise L. Stephens

j j onesOfun4all.com
dstephens@fun4all.com

SELECT , , ,
IN.
SELECT ,
Fun4All.
, .

SELECT cust_name, cust_contact, cust_email


FROM Customers
WHERE cust_state IN ('IL','IN','MI')
UNION
SELECT cust_name, cust_contact, cust_email
FROM Customers
WHERE cust name = 'Fun4All';

148

14

cust_name

cust_contact

cust_email

Fun4All
Fun4All
Village
The Toy Store

Denise L.Stephens
Jim Jones
Toys John Smith
Kim Howard

dstephens@fun4all.com
j jonesOfun4all.com
sales@villagetoys.com
NULL


SELECT, UNION. UNION SELECT
.
, UNION,
WHERE:

SELECT cust_name, cust_contact, cust_email


FROM Customers
WHERE cust_state IN ('IL1,'IN1,'MI')
OR cust_name = Fun4All';

UNION
, WHERE.

( ), UNION
.
U N I O N

SQL
S E L E C T , U N I O N .

, - .

149
,
, SELECT, -. , ,
,
WHERE UNION. "",
, .
,
.

UNION
, UNION . , ,
.
UNION SELECT,
UNION ( , SELECT, UNION).
UNION
, ( , ).
.
,
, (,
).
,

.

150

14



" UNION" SELECT. ,
, ,
SELECT , .
SELECT UNION, , .
UNION
( ,
, WHERE SELECT).
Fun4All
SELECT. UNION, .
UNION ,
. ,
, UNION ALL UNION.
:

SELECT cust_name, cust_contact, cust_email


FROM Customers
WHERE cust_state IN ('IL','IN1,'MI')
UNION ALL
SELECT cust_name, cust_contact, cust_email
FROM Customers
WHERE cust name = 'Fun4All';

cust_name

cust_contact

Village Toys
Fun4All
The Toy
Fun4All
Fun4All

John Smith
Jim Jones
Store Kim Howard
Jim Jones
Denise L.Stephens

cust_email
sales@villagetoys.com
jjones@fun4all.com
NULL
jjones@fun4all.com
dstephens@fun4all.com

151

UNION ALL
.
, .
U N I O N WHERE

,
U N I O N , WHERE. U N I O N A L L
U N I O N , ,

WHERE. ( ),

U N I O N

A L L ,

W H E R E .



SELECT
ORDER BY. UNION ORDER
,
SELECT.
, ,
ORDER BY .
, UNION:

SELECT cust_name, cust_contact, cust_email


FROM Customers
WHERE cust_state IN ('IL1,IN1,'MI)
UNION
SELECT cust_name, cust_contact, cust_email
FROM Customers
WHERE cust_name = cFun4All'
ORDER BY cust name, cust contact;

152

14

cust_name

cust_contact

cust_email

Fun4All
Fun4All
The Toy Store
Village Toys

Denise L.Stephens
Jim Jones
Kim Howard
John Smith

dstephens@fun4all.cor
jjones@fun4all.com
NULL
salesOvillagetoys.cor

UNION ORDEI
BY SELECT.
ORDER BY
SELECT, !
,
SELECT.


U N I O N . E X C E P T ( M I N U S )

, ,
, I N T E R S E C T -

, .
UNION ,
.

, SELECT UNION.
UNION,
, . UNION
WHERE
.

15


,
, INSERT SQL.


, SELECT SQL ( 14 ). SQL
, .
INSERT. ( .)
, INSERT
() . :
;
;
.
.
INSERT
INSERT -.
I N S E R T , , .




INSERT.

154

15

, .
:

INSERT INTO Customers


VALUES('1000000006 ' ,
1
Toy Land',
1
4 2 3 Any Street ,
1
New York',
1
'NY ,
'Hill' ,
1
USA',
NULL,
NULL);


. , , VALUES, .
-
(,
cust_contact cust_email ),
NULL (,
). , .
I N T O

SQL
INSERT
INTO.
,
. ,
.

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

155

, SQL, ,
. , .
(, , )
INSERT :

INSERT INTO Customers(cust_id,


cust_name,
cust_address,
cust_city,
cust_state,
cust_ZIP,
cust_country,
cust_contact,
cust_email)
VALUES(1000000006',
1
Toy Land,
4 2 3 Any Street1 ,
1
New York,
'NY' ,
11111' ,
USA1 ,
NULL,
NULL) ;

,
INSERT,
,
.
,
VALUES. VALUES ,
..
,
VALUES , ,
, . :
, INSERT
.

156

15

INSERT
( , ),
. , :

INSERT INTO Customers(cust_id,


cust_contact,
cust_email,
cust_name,
cust_address,
cust_city,
cust_state,
cust_ZIP,
VALUES('1000000006,
NULL,
NULL,
1
Toy Land',
1
'123 Any Street ,
1
New York',
'NY' ,
1
11111',


, I N S E R T

.
,
,
.

V A L U E S

, I N S E R T , VALUES.
,
. , -
, . ,
, .

157


INSERT
. ,
.
,
.
:

INSERT INTO Customers(cust_id,


cust_name,
cust_address,
cust_city,
cust_state,
cust_ZIP,
cust_country)
VALUES('1000000006 ' ,
1
Toy Land',
4 2 3 Any S t r e e t 1 ,
1
New Y o r k ' ,
'NY1 ,
'11111',
1
USA') ;

, ,
, cust_contact
cust_email. ,
INSERT.
INSERT .

INSERT,
.
:
NULL ( - ).

158

15
. , ,
, .

, NULL
, , ,
.


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


CustNew Customers. CustNew.
CustNew ,
Customers, . CustNew , c u s t _ i d , Customers (
I N S E R T ,
).

159

INSERT INTO Customers(cust_id, cust_contact,


cust_email,
cust_name,
cust_address,
cust_city,
cust_state,
cust_ZIP,
cust_country)
SELECT cust_id,
cust_contact,
cust_email,
cust_name,
cust_address,
cust_city,
cust_state,
cust_ZIP,
cust_country
FROM CustNew;


CustNew Customers
INSERT SELECT. ,
, SELECT CustNew.
SELECT .
? , CustNew. , (
, ). , Customers.
INSERT

SELECT


I N S E R T SELECT. -

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

160

15

SELECT, INSERT SELECT, WHERE ,


.

I N S E R T . ,
I N S E R T .
INSERT SELECT,



SELECT,
INSERT.



, INSERT .
-
( " "),
SELECT INTO.
D B 2
DB2 S E L E C T I N T O .

INSERT SELECT, ,
SELECT INTO ( , , , ).
INSERT SELECT SELECT INTO
SELECT I N T O
I N S E R T S E L E C T ,

, .

161


SELECT INTO:

SELECT *
INTO CustCopy
FROM Customers;

SELECT
CustCopy
Customers.
SELECT *,
Customers
CustCopy ( ).
,
, * ().
MySQL Oracle
:

CREATE TABLE CustCopy AS


SELECT *
FROM Customers;

SELECT INTO .

SELECT, WHERE GROUP BY.

.
, .

162

15

SELECT

I N T O -

SQL. , SQL
, .



I N S E R T ?

, .

,
. INSERT
, . INSERT SELECT
SELECT INTO .
, UPDATE DELETE.

16

,
UPDATE DELETE,
.


() - UPDATE.
:
;
.
.
WHERE

UPDATE ,
. , UPDATE,
.

UPDATE
U P D A T E

- . , ,
.

164

16

UPDATE , :
, ;
;
, ,
.
. ,
1000000005 ,
. :

UPDATE Customers
SET cust_email = 'kim@thetoystore.com'
WHERE cust_id = '1000000005';

UPDATE ,
.
Customers. SET, . SET
cust_eraail:
SET cust_email = 'kim@thetoystore.com'

UPDATE WHERE,
, . WHERE
Customers,
( !) ; , , , .

:

UPDATE Customers
SET cust_contact = 'Sam Roberts',
cust_email 'sam@toyland.com'
WHERE cust_id = '1000000006';


SET, -
(
). , cust_contact
cust email, 1000000006.

165
UPDATE
UPDATE , , SELECT.
11, " ",

.

FROM
SQL FROM UPDATE, -


. , .

,
NULL ( NULL).
:

UPDATE Customers
SET cust_email = NULL
WHERE cust_id = '1000000005';

NULL
"" cust email.



DELETE.
:
;
.

166

16
WHERE
DELETE ,
. , DELETE, .
DELETE

D E L E T E

- . , ,
.
.
, UPDATE . ( ) , DELETE .

Customers:

DELETE FROM Customers


WHERE cust_id = '1000000006';

DELETE FROM ,
, . WHERE , ,
. , 1000000006.
WHERE ,
.
FROM
SQL D E L E T E FROM.

, . ,
SQL- .

167
DELETE . ,
. UPDATE.
,
D E L E T E

, .

, DELETE.
TRUNCATE

T A B L E , -

,
( ).



UPDATE DELETE, , WHERE,
.
WHERE, UPDATE DELETE
. , UPDATE WHERE, . , DELETE WHERE,
.
,
SQL.
UPDATE DELETE
WHERE, .

168

16
, ( 12, " ", , ),
WHERE , . ( ,
.)
WHERE UPDATE DELETE,
SELECT, ,
,
WHERE.

(. 12),
,
.
, UPDATE DELETE WHERE. , .

, SQL
.
, UPDATE
DELETE, ,
.

, UPDATE DELETE .
, ,
. , WHERE UPDATE DELETE,
,
, .

17




, .


SQL
,
, .
.

, .
SQL.

SQL CREATE TABLE. , ,
, SQL. ;
(

).

170

17

CREATE

TABLE


SQL.
,
.

, , , .

.

:. .

CREATE TABLE -

(. , " ").


CREATE
TABLE, :
;
CREATE TABLE;
,
;
, .
SQL Products, :

CREATE TABLE Products


(
prod_id CHAR(10) NOT NULL,
vend_id CHAR(10) NOT NULL,
prod_name CHAR(254) NOT NULL,
prod_price DECIMAL(8,2) NOT NULL,

171
prod_desc VARCHAR(1000) NULL

,
CREATE TABLE.
( )
. .
.

( ), . ( 1, " SQL", ,
. , , " SQL",
.)
" ", .
, CREATE
TABLE ,
.
Oracle, PostgreSQL, SQL Server Sybase ,
, MySQL VARCHAR
t e x t . DB2 NULL
.
SQL
(. ).

SQL.

, .
SQL ,
. CREATE
TABLE SQL. ,
. SQL
, .

172

17

,
, . , SQL , ( ), ,
.

NULL
4, " ", ,
NULL.
, ,
. , NULL,
,
. ,
NULL, . , - .

(NULL), (NOT NULL), .
:

CREATE TABLE Orders


(
order_num INTEGER NOT NULL,
order_date DATETIME NOT NULL,
cust id CHAR(10) NOT NULL

Orders,
. Orders : (order number),

173

( o r d e r d a t e ) (customer
ID). , NOT NULL,
.
,
.
, , NULL NOT NULL:

CREATE TABLE Vendors


(
vend_id CHAR(10) NULL,
vend_name CHAR(50) NOT NULL,
vend_address CHAR(50),
vend_city CHAR(50),
vend_state CHAR(5),
vend_ZIP CHAR(10),
vend_country CHAR(50)

Vendors,
. ,
NOT NULL (.. NULL).
NULL, NOT
NULL. NULL ,
, NOT NULL, NULL.
N U L L
NOT N U L L
N U L L . . DB2
N U L L ; ,

. ,
.

174

17
N U L L

1 ,
, .
, ,
.
NULL
, NULL . NULL ;
. ' ' (
, ),
"" NOT
NULL. ; .
NULL
N U L L , .


SQL , ,
- .
DEFAULT CREATE
TABLE.
:

CREATE TABLE Orderltems


(
order_num INTEGER NOT NULL,
order_item INTEGER NOT NULL,
prod_id CHAR(10) NOT NULL,
quantity INTEGER NOT NULL DEFAULT 1,
item_j>rice DECIMAL (8 ,2) NOT NULL

175

Order I t ems, , . ( Orders.)


q u a n t i t y () .
DEFAULT 1 , 1, .

. ,

, . , MySQL DEFAULT CURRENT_DATE ( ) , Oracle
DEFAULT SYSDATE, SQL Server
DEFAULT GETDATE ( ) . , ,
, .
. 17.1 (
,
).
1 7 . 1 .

Access
DB2
MySQL
Oracle
PostgreSQL
SQL Server
Sybase

NOW()
CURRENT_DATE
CURRENT_DATE()
SYSDATE
CURRENT_DATE
GETDATE()
GETDATE()

DEFAULT N U L L

DEFAULT NULL.
DEFAULT ,
.

176

17


,
ALTER TABLE.
, , , .
ALTER TABLE.


,
. ,

,
.


,
, (
NULL DEFAULT).


.

.

, , .
, , .
, ,
.
ALTER
TABLE, .

, , ALTER TABLE. (
,
.)

, .
, ,
.

177

ALTER TABLE Vendors


ADD vend_j>hone CHAR(20);

Vendors , vend_phone. .
, ,
, , . (, .)

ALTER TABLE Vendors


DROP COLUMN vend_phone;

.

.
INSERT SELECT (.
15, " ,"
)
.
.
,
.
( ).
, .
, , , .

178

17
A L T E R
A L T E R

TABLE

T A B L E

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


(
, ) .
DROP TABLE:

DROP TABLE CustCopy;

CustCopy (
15).
,
.

, , . DROP

T A B L E ,

,
,
. , .

179



-. , .
DB2, MySQL, Oracle PostgreSQL
RENAME. SQL Server Sybase
sp_rename. .
, . ,
.


SQL. CREATE TABLE
, ALTER TABLE
( , ), DROP TABLE
.
. ,

.

18

, , .
,
SQL, .


.
, ,
, ,
.
MySQL
MySQL
( MySQL 5).
.

, ,
. 12,
" ", SELECT
:

SELECT cust_name, cust_contact


FROM Customers, Orders, Orderltems
WHERE Customers.cust id = Orders.cust id

182 18
AND Orderltems .order__num = Orders . order_num
AND prod_id = 'RGAN011;


,
, ,
,
.
(
), WHERE ]
.
, <


ProductCustomers. ]
:

SELECT cust_name, cust_contact


FROM ProductCustomers
WHERE prod_id = 'RGAN01';

, ]
. ProductCustomers !
, - !
. , (
.


.

!
.
:
SQL;
.
,
,
;

183

;
. ,
;
. , ,
.
, , . SELECT, ,

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

, , ,
.
, . , , .

:
, -

184

18

. ,
,
,
,
*
,
.
, , !
. (
, - ]
.)
!
, .
, ]
.
.
; !

. *

. (
,
,
.)

ORDER BY .
,

,
(. 7, "
,"
).
. ]

.
]
, . (
,
, ,
.

185

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

,
-
,
,
.


, , ( , ), ,
.
CREATE
VIEW. CREATE TABLE,
CREATE VIEW
, .

DROP.

;.

DROP

VIEW

186

18
( ) ,
DROP, .




SQL, .
:

CREATE VIEW ProductCustomers AS


SELECT cust_name, cust_contact, prod_id
FROM Customers, Orders, Orderltems
WHERE Customers.cust_id = Orders.cust_id
AND Orderltems.order num = Orders.order num;

,
P r o d u c t C u s t o m e r s , ,
- .
SELECT * FROM P r o d u c t C u s t o m e r s ,
, .
SQL Server
SQL, Microsoft
SQL Server
CREATE

VIEW.

,
RGAN01, :

SELECT cust_name, cust_contact


FROM ProductCustomers
WHERE prod_id = 'RGAN01';

187

cust name

cust contact

Fun4All
The Toy Store

Denise L. Stephens
Kim Howard

WHERE.
,
WHERE WHERE ,
.
, SQL. ,
SQL , .

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

, . SELECT
(. 7, " ")
:

188

18

SELECT RTRIM(vend_name) + ' (' + RTRIM(vend__country)


+ ' )' AS vend_title
FROM Vendors
ORDER BY vend name;

vend t i t l e
Bear Emporium (USA)
Bears R Us (USA)
Doll House Inc. (USA)
Fun and Games (England)
Furball Inc. (USA)
Jouets et ours (France)

,
| | ( 7):

SELECT RTRIM(vend_name) j | ' (' |


^RTRIM(vend_country) || ' )' AS vend_title
FROM Vendors
ORDER BY vend name;

vendtitle
Bear Emporium (USA)
Bears R Us (USA)
Doll House Inc. (USA)
Fun and Games (England)
Furball Inc. (USA)
Jouets et ours (France)

, . , ,
.
:

CREATE VIEW VendorLocations AS SELECT RTRIM(vend_name)


(' + RTRIM(vend_country)

189
+ ')' AS v e n d _ t i t l e
FROM Vendors;

, I I:

CREATE VIEW VendorLocations AS


SELECT RTRIM(vend_name) || ' (' | |
^RTRIM(vend_country) || ' ) ' AS vend_title
FROM Vendors;

,
,
SELECT. ,
,
:

SELECT *
FROM VendorLocations;

vend title
Bear Emporium (USA)
Bears R Us (USA)
Doll House Inc. (USA)
Fun and Games (England)
Furball Inc. (USA)
Jouets et ours (France)
S E L E C T
, , ,
.
? SELECT, SELECT
.

190

18



WHERE. ,
CustomerEMailList
, ,
.
, :

CREATE VIEW CustomerEMailList AS


SELECT cust_id, cust_name, cust_email
FROM Customers
WHERE cust email IS NOT NULL;

, e-mail, ,
.
WHERE ,
NULL c u s t _ e r a a i l , .
CustomerEMailList .

SELECT *
FROM CustomerEMailList;

cust id

cust name

cust email

1000000001
1000000003
1000000004

Village Toys
Fun4All
Fun4All

sales@villagetoys.com
jjones@fun4all.com
dstephens@fun4all.com

WHERE

WHERE

, ( )
.

191



.
SELECT, 7. :

SELECT prod_id,
quantity,
item_price,
quantity*item_jprice AS expanded_price
FROM OrderItems
WHERE order num = 20008;

prod_id

quantity

item_price

expanded_price

RGAN01
BR03
BNBG01
BNBG02
BNBG03

5
5
10
10
10

4.9900
11.9900
3.4900
3.4900
3.4900

24.9500
59.9500
34.9000
34.9000
34.9000

CREATE VIEW OrderltemsExpanded AS


SELECT order_num,
prod_id,
quantity,
item_price,
quantity*item_price AS expanded_price
FROM Orderltems;

2 0008
( ), :

FROM SELECT *
OrderltemsExpanded
WHERE order num = 20008;

192

18

order_num

prod_id

quantity

item_price

expanded_price

20008
20008
20008
20008
20008

RGAN01
BR03
BNBG01
BNBG02
BNBG03

5
5
10
10
10

4.99
11.99
3.49
3.49
3.49

24.95
59.95
34.90
34.90
34.90

, , . , .

. , ,
. SQL- SELECT
,
.

19

, ,
.
, .

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

194

19

, , cyri
.
SQL
. SQL-,
, <
, ( )
, , .
? !
SQL
.
, :
(
, ).
. ( ), . , .
Access MySQL
Access.
, ,
MySQL v4.x ( ) ( MySQL 5).


,

. ,
.
, ,

. ,
Oracle SQL Server.

195



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

, ,
.

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

, . .
SQL , . , ,

.

196

19

, : ,
. ,
. SQL- , .

. -
.
,
( ) ,

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


, , ,
. .
,
, .

197

,
, .
SQL
EXECUTE , . :

EXECUTE AddNewProduct ('JTS01' ,


'Stuffed Eiffel Tower',
6.49,
'Plush stuffed toy with the
La Tour Eiffel in red white and blue')


AddNewProduct;
Products. AddNewProduct
: (
Vendors), , . (
). Products
.
Products , : prod_id,
.
?
,
(
).
, .
,
.

198

19

, .

Products,
.
. , .
, ,
.
,
^ .
,
.
, SELECT.
, .


,
. ,
, :
,
, .
Oracle:

CREATE PROCEDURE MailingListCount


(ListCount OUT NUMBER)
IS
BEGIN
SELECT * FROM Customers
WHERE NOT cust_email IS NULL;
ListCount := SQL%ROWCOUNT;
END;

199

, ListCount. , . OUT . Oracle


IN ( ), OUT
( ) INOUT ( ). BEGIN END, ,
,
SELECT. ListCount , .
Microsoft SQL:

CREATE PROCEDURE MailingListCount


AS
DECLARE @cnt INTEGER
SELECT @cnt = COUNT(*)
FROM Customers
WHERE NOT cust_email IS NULL;
RETURN @cnt;

. ,
, SQL Server . DECLARE @cnt (
SQL Server @). SELECT,
, COUNT ().
, RETURN
RETURN
@cnt.
, Orders.
SQL Server, , :

200 19

CREATE PROCEDURE NewOrder @cust_id CHAR(10)


AS
--
DECLARE @order_num INTEGER
--
SELECT @order_num=MAX(order_num)
FROM Orders
Determine next order number
SELECT @order_num=@order_num+l
--
INSERT INTO Orders(order_num order_date, cust_id)
VALUES(@order_num, GETDATE() @cust id)
--
RETURN order nura;


Orders.
, . ,
,
. . (
()) (
SELECT). INSERT ,
( GETDATE ()) . , (
) RETURN
@order_nura. , ,
.

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

201


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

SQL Server:

CREATE PROCEDURE NewOrder @cust_id CHAR(10)


AS
--
INSERT INTO Orders(cust_id)
VALUES(@cust_id)
--
SELECT order num = IDENTITY;


O r d e r s .
. ; SQL Server I d e n t i t y (
, Auto Number
Sequences). , :
, .
( GETDATE ()), . ,
? SQL
Serer
IDENTITY, (
SELECT).
,
. , , , .

202

19

,
.
, ,
. , ,
. .

20


, COMMIT ROLLBACK .

, SQL
.
12, " ", ,
. , , .
, , ,
-
.
Orders,
18- , .
, Orderltems
. () , (. 1, "
SQL"). , ,
, .

.
1. ,
. , .

204

20

2. .
3. Orders,
() .
4. ,
Orders.
5. Orderltems
, Orders ( Products ).
, - (, , , , )
.
?
, ,
Orders
. .
.
,
.
, Orders, , Orderltems? .
:
Orderltems?
,
.
?
, . , SQL,
, .. ,
. ,

205

,
( ).
,
() . , () ,
.
, ,
.
1. ,
. , .
2. .
3. .
4. Orders.
5. Orders
, .
6. ,
Orders
7. Orderlteras
.
8.
Order Items ,
Orderltems .
:
(Transaction). SQL.
(Rollback). SQL (
"").
(Commit). SQL .
(Savepoint).
,
( ). " ".

206

2 0
?
INSERT, UPDATE DELETE.
S E L E C T .
( .)
CREATE DROP.

, (),
.


, , , .

,
, .
, .

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

BEGIN TRANSACTION
COMMIT TRANSACTION

,
BEGIN TRANSACTION COMMIT TRANSACTION,

, .
MySQL :

207

START TRANSACTION

PostgreSQL ANSI SQL:

BEGIN;

ROLLBACK
ROLLBACK () SQL, :

DELETE FROM Orders;


ROLLBACK;

,
ROLLBACK, DELETE.
, ,
, ,
DELETE ( INSERT UPDATE) .

COMMIT
SQL
.
( ) .
. ,
. .

208

20

COMMIT.
SQL Server:

BEGIN TRANSACTION
DELETE Orderltems WHERE order_num = 12345
DELETE Orders WHERE order_num = 12345
COMMIT TRANSACTION

SQL Server 12345 .


, Orders
Orderltems, ,
. COMMIT ,
. ,
, - , ,
.
Oracle,
:

DELETE Orderltems WHERE order_num = 12345;


DELETE Orders WHERE order_num = 12345;
COMMIT;


ROLLBACK COMMIT .
,
.
, , ,
.
, ,
Orders . Customers
( ).
-

209

. , ,
,
.
SQL
(savepoints). MySQL
Oracle SAVEPOINT:

SAVEPOINT deletel;

SQL Server Sybase :

SAVE TRANSACTION deletel;


, , ,
, "",
.
, SQL Server
:

ROLLBACK TRANSACTION deletel;

MySQL Oracle :

ROLLBACK TO deletel;

SQL Server:

BEGIN TRANSACTION
INSERT INTO Customers(cust_id, cust_name)
VALUES(4000000010', 'Toys Emporium');
SAVE TRANSACTION StartOrder;
INSERT INTO Orders(order_num, order_date, cust_id)
VALUES(20100,'2001/12/1','1000000010');
IF @@ERROR 0 ROLLBACK TRANSACTION StartOrder;
INSERT INTO Orderltems(order_num, order_item,
(
i>prod_id, quantity, item_price)
VALUES(20010, 1, 'BR011, 100, 5.49);
IF @@ERROR <> 0 ROLLBACK TRANSACTION StartOrder;
INSERT INTO Orderltems(order_num, order_item,
4>prod_id, quantity, item_price)
1
VALUES(20010, 2, 'BR03 , 100, 10.99);

210 20
IF @@ERR0R <> 0 ROLLBACK TRANSACTION StartOrderj
COMMIT TRANSACTION

, !
INSERT, .
INSERT,
- INSERT ,
SQL Server
@@ERROR. (
.) @@ERROR , oi
, ,
. , COMMIT .
,
SQL-, , ,
. ?
,
.


SQL, (
).
COMMIT ROLLBACK .
,
.

21

,
.


SQL , . ,
SQL-,
. SELECT ,
10 .
.

, SQL-.


.
. , ,
SELECT, ,
,
SELECT. , "" ()
, .

212

21
MySQL
, ,
MySQL (
MySQL 5).


.
.

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

, .
, ]
,
(, !
), .

( "" ]
), !
i
.

, - (Indexed Sequential Access
Method, ISAM) ( Btrieve
dBASE). SQL , HbixTnnalSAM.

213

,
, .
Web-
,
, Web- (, ASP, ColdFusion, PHP JSP). , "-"
Web-, ,
. ,
,
, .



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

214

21

,
, .
,
.


DECLARE,
.
DECLARE SELEd
WHERE :
. , ,
, ,
,
,
.
DB2, SQL Server i
Sybase:

DECLARE CustCursor CURSOR


FOR
SELECT * FROM Customers
WHERE cust email IS NULL

Oracle PostgreSQL:

DECLARE CURSOR CustCursor


IS
SELECT * FROM Customers
WHERE cust email IS NULL


DECLARE *
CustCursor. SELECT ,
, *
( NULL).
, ,
.

215


OPEN
i^JRSOR, , { :
)PEN CURSOR CustCursor

OPEN CURSOR ipoc,


.
FETCH. FETCH , ,
( , ). Oracle ().

DECLARE TYPE CustCursor IS REF CURSOR


RETURN Customers%ROWTYPE;
DECLARE CustRecord Customers%ROWTYPE
3EGIN
OPEN CustCursor;
FETCH CustCursor INTO CustRecord;
CLOSE CustCursor;
END;

FETCH (
) , CustRecord.
.
( Oracle) :

DECLARE TYPE CustCursor IS REF CURSOR


^ RETURN CuStomers%ROWTYPE;
DECLARE CustRecord Customers%ROWTYPE
BEGIN
OPEN CustCursor;
LOOP
FETCH CustCursor INTO CustRecord;
EXIT WHEN CustCursor%NOTFOUND;

216 21
END LOOP;
CLOSE CustCursor;
END;

,
FETCH
, CustRecord.
, FETCH
LOOP, *
EXIT WHEN CustCursor%NOTFOUND ,
( ),
.
, (
. . . co6ci
.
,
Microsoft SQL Server:

DECLARE @cust_id CHAR(10),


@cust_name CHAR(50),
@cust_address CHAR(50),
@cust_city CHAR(50),
@cust_state CHAR(5),
@cust_ZIP CHAR(10),
@cust_country CHAR(50),
@cust_contact CHAR(50),
@cust_email CHAR(255),
OPEN CustCursor
FETCH NEXT FROM CustCursor
INTO @cust_id, @cust_name, @cust_address,
@cust_city, @cust_state, @cust_ZIP,
@cust_country, @cust_contact, @cust_email
WHILE @@FETCH_STATUS = 0
BEGIN
FETCH NEXT FROM CustCursor
INTO @cust_id, @cust_name, @cust_address,
@cust_city, @cust_state, @cust_ZIP,
@cust_country, @cust_contact, @cust_email
END
CLOSE CustCursor

217


, FETCH
.
WHILE , WHILE @@FETCH_STATUS = 0
( )
.
. . . . .


, . , ( SQL Server) , , , .
DB2, Oracle
PostgreSQL:

CLOSE CustCursor

Microsoft SQL Server:

CLOSE CustCursor
DEALLOCATE CURSOR CustCursor

CLOSE; , ,
.
,
OPEN.

, . , , , ,
.
.

22

SQL
: , .


SQL,
.
, .
,
. ,
, ,
,
, .
(
).
, , . , Orders
, Order Items , , , Orderltems, Orders. , , Orders,
Customers.
,
(

220

22

SELECT , ,
),
.
,
, , ( ).
UPDATE DELETE.
, .
.

, .


, . ( CREATE TABLE
ALTER TABLE, 17,
" ").

,

. , ,
, , .
,
.


1, "
SQL". ,
, ( -

SQL

221

) . , ( ) , .
. ,
.

, .
.
- . (
NULL.)
, ,
.
. - , -
.
:

CREATE TABLE Vendors


(
vend_id CHAR(10) NOT NULL PRIMARY KEY,
vend_name CHAR(50) NOT NULL,
vend_address CHAR(50) NULL,
vend_city CHAR(50) NULL,
vend_state CHAR(5) NULL,
vend_ZIP CHAR(10) NULL
vend_country CHAR(50) NULL

PRIMARY KEY, vend_id .

222

22

ALTER TABLE Vendors


ADD CONSTRAINT PRIMARY KEY (vend id);

, CONSTRAINT.
CREATE
TABLE ALTER TABLE.


,
,
.
. , , .
Orders , . Customers.
Orders Customers .
Customers; . Orders; .
Orders, , .
,
( ).
,
Orders, Customers.
.
, Customers,
,
Customers.

SQL

223

CREATE TABLE Orders


(
order_num INTEGER NOT NULL PRIMARY KEY,
order_date DATETIME NOT NULL,
cust_id CHAR(10) NOT NULL REFERENCES
"^Customers (cust id)

,
REFERENCES , cust_id
cust_id Customers.
CONSTRAINT ALTER TABLE:

ALTER TABLE Customers


ADD CONSTRAINT
FOREIGN KEY (cust id) REFERENCES Customers

(cust id)


,
, . ,
, . , , .

( , ,
). ,
.

224

22
, . ,

. ,
Customers, .



( ).
, .

,
.
NULL.
.

.
, .

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

SQL 225

( ,
),
UNIQUE ,
.
: UNIQUE
CONSTRAINT.


,
( ) .
:
, 0 () ( 0 ).
,
, , .
, "" F.
(. 1) ,
.
.

Order I t ems ,
, 0:
CREATE TABLE Orderltems
order_num INTEGER NOT NULL,
order_item INTEGER NOT NULL,
prod_id CHAR(10) NOT NULL,

226

22

quantity INTEGER NOT NULL CHECK


4> (quantity > 0) ,
item_jjrice MONEY NOT NULL

( ) , .
,
F, ALTER TABLE:

ADD CONSTRAINT CHECK ( g e n d e r LIKE ' [MF] ' )



. ,
( ) . ,
, gender ();
,
,
,
F (, , NULL, ).
. , ( ),
,
. , .

SQL 227



. ,

(- "index") .
,
.

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

. , ,
.
, ,
. , ,
,
? ,

228

2 2

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

.
, .

, , ,
. ,

.

.
. ,

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

. ,
.

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

SQL 2 2 9

,
.
CREATE INDEX
( ).
Products:

CREATE INDEX prod_name_ind


ON PRODUCTS (prod_name);

.
prod_name_ind
CREATE INDEX. ON
, ,
, ( ),
.

,
.
: ,
- , .
;


, . , ,
(),
.
INSERT, UPDATE DELETE
( - )
.

230

2 2
MySQL
, MySQL
( MySQL 5.1).

( SQL) . ,
INSERT O r d e r s ,
,
O r d e r s . , ,
INSERT UPDATE Customers,
.
,
:
INSERT;


UPDATE;
DELETE.
, , .
:
(,

INSERT UPDATE);

, ,
- (,

);

(,
,
, );
/.

SQL 231

, , , . .
, c u s t _ s t a t e Customers
INSERT
UPDATE.

SQL Server:

CREATE TRIGGER customer_state


ON Customers
FOR INSERT, UPDATE
AS
UPDATE Customers
SET cust_state = Upper(cust_state)
WHERE Customers.cust_id = inserted.cust_id;

Oracle PostgreSQL:

CREATE TRIGGER customer_state


AFTER INSERT OR UPDATE
FOR EACH ROW
BEGIN
UPDATE Customers
SET cust_state = Upper(cust_state)
WHERE Customers.cust_id = :OLD.cust_id
END;

,
, ,
, , .


, ,
. ,
,

232

22

,
.
. , ,
,
.
, , .

:

( , ..);

;
( ,
..);
;
,
;
.
SQL GRANT REVOKE, , GRANT
REVOKE.

, SQL. , -

SQL 233

,
,
. ,
. , .

SQL
. ,
, , SQL-.

.

. ,
, ( ).

, , .
:
;
;
;
.

( -

236

). .

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

Vendors
Vendors ,
.
,
(vend_id) .
. .1. columns

vend_id
vend_name
vend_address
vend_city
vend_state
vend_ZIP
vend_country

" (ID)




ZIP-

2 3 7

.
vend_id.

Products
Products , . ( prod_id) vend_id ( ).
.2. Products

prod_id
vend_id

(ID)
( vend_id Vendors)
prod_name

prod_price
prod_desc

.
prod_id.

vend_id, vend_id Vendors.

Customers
Customers
. ( cust_id).
.. Customers

cust_id
cust_name
cust_address
cust_city

238


. .

cust_state
cust_ZIP
cust_country
cust_contact
cust_email


ZIP-


.
cust_id.

Orders
Orders ( ). ( order_num).
cust_id (
Customers).
.4. Orders


order_num
order_date
cust_id
, ( cust_id Customers)
.
order_nura.

cust_id, cust_id Customers.

Orderltems
Orderltems ,
. Orders

2 3 9
Orderltems.

( , ..).
order_num ( Orders). , (
Products).
.5. Orderltems

order_num

( order_num
Orders)
order_item ( )
prod_id
(
prod_id Products)
quantity

item_price
.
order_num order_item.

order_num, order_num Orders,
prod_id, prod_id Products.


,
. , ,
Web-
http://www.forta.com/books/0672325675/.

240

MDB-
Microsoft Access
MDB-
Microsoft Access . , - .
MDB- Access ODBC,
, ASP ColdFusion.

SQL-
,
(
Access). SQL-
http://www.forta.com/books/0672325675/.
.
c r e a t e . t x t , SQL-,
( ).
p o p u l a t e . t x t , SQL-
INSERT, .
SQL, ,
, ,
, . , .
, ,
:
IBM DB2;
Microsoft SQL Server;
MySQL;
Oracle;
PostgreSQL;
Sybase Adaptive Server.

241


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

1, " SQL," SQL


, . , , SQL-.

SQL
.

, SQL . , ?

, . .
Windows, ,
Microsoft Query.
.
Windows
Query Tool (George Poulose).
Web-
http://www.forta.com/books/0672325667/.
Aqua Data Studio Java-,
Windows, Linux, Unix, Mac OSX .
Web- h t t p : //www. f o r t a .
com/books/0672325667/.
.
Web- .

244

Aqua Data
Studio
Aqua Data Studio SQL-, Java.

( ODBC1). - SQL Aqua Data Studio, .
1. Aqua Data Studio.
2. , . Select
Register Server.
3. , , ( Generic ODBC
Microsoft Access
ODBC ,
ODBC, ).
. .
.
4. .
5. Query Analyzer, Query
Analyzer Server <Ctrl+Q>.
6. SQL- ( ).
7. SQL-, Execute
Query, <Ctrl+E>,
Execute ( ).
8. .

1
ODBC (. Open DataBase Connectivity) , Windows Windows NT,
,
. . .

245

DB2
DB2 IBM .
,
SQL.
Java Command Center
.
1. Command Center.
2. Script.
3. SQL Script.
4. Execute Script Execute, .
5.
.
Results, .
6. Command Center SQL-, SQL Assist.
Interactive.

Macromedia
ColdFusion
ColdFusion Macromedia
Web-.
ColdFusion , ().
SQL-, , , Web-. :
1. - ColdFusion, (Data Source). ColdFusion
Administrator Web- (
ColdFusion, ).

246

2. ColdFusion (
CFM).
3.
CFML <CFQUERY>
</CFQUERY> .
, NAME,
DATASOURCE.
4. SQL-
<CFQUERY> </CFQUERY>.
5. <CFDUMP> <CFOUTPUT>
.
6. - Web-.
7. , Web-.

Microsoft
Access
Microsoft Access
, ,
, a Access (Query Designer), SQL.
,
SQL- .
Access
SQL ODBC,
SQL-
. , .
1. Microsoft Access.
( ) . ,
.
2.
,
(
).

2 4 7

3. . , .
4. SQL .
5. SQL .
6. SQL,
( ). , .
7. ( SQL SQL-) .


SQL.
Microsoft Access
, Access SQL- ODBC.

, Access.
, .
1. Microsoft Access
ODBC, ODBC. (. ).
2. Microsoft Access.
( ) . -
.
3.
,
(
).
4. . , .
5. SQL, .

248

6. , .
7. ODBC, (...), ,
ODBC.
8. ,
.
9. .
SELECT ( ,
), .
SQL,
(, INSERT, UPDATE DELETE), .
10. SQL .
11. SQL,
(
).

, Access. MDB- Access, - , .

Microsoft ASP
Microsoft ASP ,
Web-.
SQL
ASP, , , Web-. , SQL
ASP.

2 4 9

1. ASP
ODBC, , ODBC,
.
2. ASP ( ASP),
.
3. Server.CreateObject ADODB. Connection.
4. Open ODBC.
5. SQL Execute .
. Set
.
6. ,
<% Do While NOT EOF %>.
7. Web-.
8. , Web.

Microsoft
ASP.NET
Microsoft ASP.NET Web- .NET. SQL
ASP.NET, , , . , .
1. . aspx.
2. ,
SqlConnectionO OleDbConnectionO .
3.

SqlCommand ()

OleDbCommand () .

250

4. DataReader,
ExecuteReader.
5. , , .
6. Web-.
7. , Web.

Microsoft Query
Microsoft Query SQL-,
SQL
ODBC. Microsoft Query Microsoft, a
.
MS-Query
MS-Query Microsoft (,
Office), . ,
^^ , .
( ,
.) , ,
MSQRY32.EXE MSQUERY.EXE.

Microsoft Query .
1. Microsoft Query ODBC
, ,
ODBC (
).
2.
Microsoft Query, -

251

. ,
, .
3.
SQL. SQL.

4. , ODBC. , ,
. , .
5. SQL SQL.
6. ,
SQL .

Microsoft SQL
Server
Microsoft SQL Server Windows
, SQL Query
Analyzer.
SQL , SQL- .

SQL Query Analyzer.
1. SQL Query Analyzer ( Microsoft SQL Server).
2.
.
SQL Server ( ,
).
3. ,
.
4. SQL-
Execute Query (
), . (
<F5> Execute
Query.)

252

5.
SQL.
6. ,

.

MySQL
MySQL
, mysql.
, SQL.
mysql, .
1. mysql, . , , - -, .
2. mysql > USE _, ,
.
3. SQL- mysql >,
, (;).
.
4. \h ,
, \s (
MySQL).
5. \q mysql.

Oracle
Oracle Java ,
Enterprise Manager. ,
SQL*Plus Worksheet.
, .

253

1. SQL*Plus Worksheet ( ,
Oracle Enterprise Manager).
2. . , .
3. SQL Worksheet . SQL- .
4. SQL,
Execute ( ). .


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

.

PostgreSQL
PostgreSQL ,
psql.
, SQL. psql, .
1. psql, . , psql _ (PostgreSQL
USE).
2. SQL- =>,
, (;). .

254

3. \?, , .
4. \h, SQL, \h

SQL (, \h SELECT).
5. \q, psql.

Query Tool
Query Tool ,
(George Poulose).
SQL
ODBC. ( ADO2.)
Query Tool
Query Tool
Internet. , Web : h t t p : / / w w w . f o r t a . c o m /
books/0672321289/.

Query Tool,
:
1. Query Tool ODBC
, ODBC
,
(. ,
).
2. Query Tool,
. .
3. ODBC, . 2

ADO (. ActiveX Data Objects)


, ,
. . .

255

, New,
.
, .
4. SQL .
5. Execute ( ), SQL
.
( <F5>
Execute Query.)

Sybase
Sybase Adaptive Server Java-
SQL Advantage. Query
Analyzer, Microsoft SQL
Server ( ). SQL Advantage, :
1. SQL Advantage.
2. ,
.
3. , .
4. SQL-.
5. ,
Execute, Execute Query
Query <Ctrl+E>.
6. ( )
.


ODBC
ODBC,
ODBC
ODBC.

256

ODBC , , ,
, .
ODBC
, .
ODBC .
ODBC ,
.
,
. -,
. -, (
). ,
ODBC, SQL.
ODBC . ODBC (ODBC Data Sources).
ODBC , (
)
,
( , ..).
ODBC ,
ODBC- . ODBC , .

ODBC,
, .
, .

ODBC
ODBC- Windows. - ODBC, .

257

1. ODBC- Windows.
2. ODBC
(
, ),
DSN,
.
3. ,
.
4. , .
,

Microsoft. .
, , .
5.
,
, .

, .


SQL

,
SQL.
, .
,
.
:
" | "
, NULL | NOT NULL NULL NOT NULL.
,
, [, ], .

.
.

ALTER TABLE
ALTER TABLE
. , CREATE TABLE. 17, "
".

ALTER TABLE

260

ADD|DROP _ _ [NULL|NOT
^NULL] [CONSTRAINTS] ,
ADD|DROP _ _ [NULL|NOT
^NULL] [CONSTRAINTS] ,

COMMIT
COMMIT
. 20, " ".

COMMIT [TRANSACTION];

CREATE INDEX
CREATE INDEX .
22, " SQL".

CREATE INDEX _
ON _ (_, . . . ) ;

CREATE PROCEDURE
CREATE PROCEDURE
. 19, " ".
Oracle , .

CREATE PROCEDURE _
^t> []
AS
SQL statement;

[]

SQL 261

CREATE TABLE
CREATE TABLE . , ALTER TABLE.
17.

CREATE TABLE _
(
_ _ [NULL|NOT NULL]
"*> [CONSTRAINTS] ,
_ _[NULL|NOT NULL]
[CONSTRAINTS] ,

CREATE VIEW
CREATE VIEW .
18, " ".

CREATE VIEW _
SELECT _,
...
FROM t a b l e s , . . .
[WHERE . . . ]
[GROUP BY . . . ]
[HAVING . . . ] ;

AS

DELETE
DELETE . 16, " ".

DELETE FROM
[WHERE

.. .] ;

262

DROP
DROP
(, , ..). 17 18.

DROP INDEX|PROCEDURE|TABLE|VIEW
%__ \ _\ _\ _;

INSERT
INSERT .
15, " ".

INSERT INTO _ [(_,


VALUES{, ...) ;

...)]

INSERT SELECT
INSERT SELECT SELECT . 15.

INSERT INTO _ [(_, ...)]


SELECT _, ... FROM _, ...
[WHERE . . . ] ;

ROLLBACK
ROLLBACK .
20.

ROLLBACK [

_];

SQL 263

ROLLBACK TRANSACTION;

SELECT
SELECT
( ). 2, " "; 3, " ";
4, " ". ( 2 14
SELECT.)

SELECT
_,
FROM _,
.
[WHERE . . . ]
[UNION . . . ]
[GROUP BY . . . ]
[HAVING . . . ]
[ORDER BY . . . ] ;

UPDATE
UPDATE
.
16.

UPDATE _
SET __ =
[WHERE . . . ] ;


SQL

1, " SQL",
, ,
.
.

, . , .
. /
, .
. , 1
10, 10 2.
( , ,
). ,
.

.

.
. ( ,
.)

, , , , .

266



,
. ,
. ,

. ,
.


, . ,
, , , ZIP-.

(. .1).
,
,
.
, 30
, , 11 .
, .
,
. ,
, 30 ,
30 (
).

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

SQL

267

? : . ,
. ,
( ). (
22, " SQL".)
. 1.

CHAR

,
1-255 .

NCHAR

CHAR,
Unicode. (
)

NVARCHAR

TEXT, Unicode. ( )

TEXT ( LONG,
MEMO,
VARCHAR)



.

, ZIP (
),
. ZIP- 01234
, 1234. .

268


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



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

BIT
DECIMAL ( NUMERIC)
FLOAT ( NUMBER)
INT ( INTEGER)
REAL
SMALLINT
TINYINT

, 0 1,


4- , -2147483648
2147483647
4-
2- , -32768 32767
1- , 0 255

SQL

269


, .

.
MONEY CURRENCY.

DECIMAL, ,
.


,
(. .). , , .
..

DATE

DATETIME ( TIMESTAMP)

SMALLDATETIME


(
)

TIME

270



,
.
204-12-30
Dec 3 0 t h , 2004, .

.

ODBC

, ODBC
,
ODBC. ODBC
:
{d
'2004-12-30'}
,
{ t ' 2 1 : 4 6 : 2 9 ' } { t s '200412-30 21:46 -.29'} . SQL- ODBC,
,
.


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

SQL 2 7 1
.4.

BINARY


(
255 8000 ,
)

LONG RAW

RAW (
BINARY)


255

VARBINARY


(
255 8000 ,
)


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

SQL

SQL
, SQL.
,
,
, .
.
,
. .
, .
SQL;
, .
.
,
,
.
ABORT
ACTIVE

ADD

ABSOLUTE

ACTION
AFTER

ALL

ALLOCATE

ALTER

ANALYZE

AND
AS

ANY

ARE
ASCENDING

ASSERTION

AT

AUTHORIZATION

AUTO

AUTO-INCREMENT

AUTOINC

AVG

BACKUP

AS

274

BEFORE
BIGINT
BLOB
BREAK
BY
CALL
CASE
CHANGE
CHARACTER_LENGTH
CLOSE
COALESCE
COLUMNS
COMMITTED
CONDITIONAL
CONNECTION
CONTAINING
CONTINUE
COPY
CROSS
CURRENT
CURRENTJTIMESTAMP
DATABASE
DATETIME
DEALLOCATE
DECIMAL
DELETE
DESCENDING
DISK
DIV
DOUBLE
DUMP
ENCLOSED
ERROREXIT
EXCEPT

BEGIN
BINARY
BOOLEAN
BROWSE
BYTES
CASCADE
CAST
CHAR
CHECK
CLUSTER
COLLATE
COMMENT
COMPUTE
CONFIRM
CONSTRAINT
CONTAINS
CONTROLROW
COUNT
CSTRING
CURRENT_DATE
CURRENTJJSER
DATABASES
DAY
DEBUG
DECLARE
DENY
DESCRIBE
DISTINCT
DO
DROP
ELSE
END
ESCAPE
EXCEPTION

BETWEEN
BIT
BOTH
BULK
CACHE
CASCADED
CATALOG
CHARACTER
CHECKPOINT
CLUSTERED
COLUMN
COMMIT
COMPUTED
CONNECT
CONSTRAINTS
CONTAINSTABLE
CONVERT
CREATE
CUBE
CURRENT_TIME
CURSOR
DATE
DBCC
DEC
DEFAULT
DESC
DISCONNECT
DISTRIBUTED
DOMAIN
DUMMY
ELSEIF
ERRLVL
ESCAPED
EXEC

SQL
EXECUTE
EXPLAIN
EXTRACT
FIELD
FILLFACTOR
FLOPPY
FOREIGN
FREETEXTTABLE
FUNCTION
GLOBAL
GRANT
HOLDLOCK
IF
INDEX
INNER
INSENSITIVE
INTEGER
INTO
JOIN
LANGUAGE
LEFT
LIKE
LINES
LOCAL
LONG
MATCH
MESSAGE
MIRROREXIT
MONTH
NATIONAL
NEXT
NOCHECK
NOT
NUMERIC

EXISTS
EXTEND
FALSE
FIELDS
FILTER
FOR
FOUND
FROM
GENERATOR
GO
GROUP
HOUR
IN
INDICATOR
INOUT
INSERT
INTERSECT
IS
KEY
LAST
LENGTH
LIMIT
LISTEN
LOCK
LOWER
MAX
MIN
MODULE
MOVE
NATURAL
NEW
NONCLUSTERED
NULL
OF

EXIT
EXTERNAL
FETCH
FILE
FLOAT
FORCE
FREETEXT
FULL
GET
GOTO
HAVING
IDENTITY
INACTIVE
INFILE
INPUT
INT
INTERVAL
ISOLATION
KILL
LEADING
LEVEL
LINENO
LOAD
LOGFILE
MANUAL
MERGE
MINUTE
MONEY
NAMES
NCHAR
NO
NONE
NULLIF
OFF

275

276

OFFSET
ONCE
OPTION
OUTER
OVERFLOW
PAGE
PARTIAL
PERM
PLAN
PREPARE
PRIOR
PROCEDURE
PUBLIC
READ
REFERENCES
RENAME
REPLICATION

OFFSETS
ONLY
OR
OUTPUT
OVERLAPS
PAGES
PASSWORD
PERMANENT
POSITION
PRIMARY
PRIVILEGES
PROCESSEXIT
PURGE
READTEXT
REGEXP
REPEAT
REQUIRE

RESERVING
RESTRICT
RETURNS
ROLLBACK
RULE
SCHEMA
SEGMENT
SEPARATOR
SET
SHARED
SINGULAR
SNAPSHOT
SPACE
SQLERROR
STARTS
SUM
TABLES

RESET
RETAIN
REVOKE
ROLLUP
SAVE
SECOND
SELECT
SEQUENCE
SETUSER
SHOW
SIZE
SOME
SQL
STABILITY
STATISTICS
SUSPEND
TAPE

ON
OPEN
ORDER
OVER
PAD
PARAMETER
PERCENT
PIPE
PRECISION
PRINT
PROC
PROTECTED
RAISERROR
REAL
RELATIVE
REPLACE
RESERV
RESTORE
RETURN
RIGHT
ROWCOUNT
SAVEPOINT
SECTION
SENSITIVE
SESSIONJJSER
SHADOW
SHUTDOWN
SMALLINT
SORT
SQLCODE
STARTING
SUBSTRING
TABLE
TEMP

SQL
TEMPORARY
THEN
TO
TRAN
TRIGGER
TRUNCATE
UNIQUE
UPDATETEXT
USE
VALUE
VARIABLE
VIEW
WAITFOR
WHILE
WRITE
YEAR

TEXT
TIME
TOP
TRANSACTION
TRIM
UNCOMMITTED
UNTIL
UPPER
USER
VALUES
VARYING
VOLUME
WHEN
WITH
WRITETEXT
ZONE

TEXTSIZE
TIMESTAMP
TRAILING
TRANSLATE
TRUE
UNION
UPDATE
USAGE
USING
VARCHAR
VERBOSE
WAIT
WHERE
WORK
XOR

277


Q
ANSI SQL, 26
Aqua Data Studio, 243, 244

Query Designer, 246


Query Tool, 243, 254

ColdFusion, 245
Command Center, 245

SQL, 25
ANSI, 26
, 26
, 26
SQL Advantage, 255
Structured Query Language, 25
Sybase Adaptive Server, 255

D
Data Source ODBC, 255
DB2, 245

E
Enterprise Manager, 252

M
Macromedia, 245
Microsoft ASP, 248
Microsoft ASP.NET, 249
Microsoft Query, 243, 250
Microsoft SQL Server, 251
MySQL, 252

ODBC, 256
Oracle, 252

PHP, 253
PostgreSQL, 44, 253
psql, 253

ALL, 96
DISTINCT, 97


, 231
, 20
, 122
ISAM, 212

, 138
, 222
, 128

, 33
, 32
, 30
, 69, 70

, 102
, 104
, 34

, 270
, 153
, 101
, 31
,
22
, 35
, 266
, 23
, 33
, 268
, 126

, 158
, 153
, 160
, 153
, 157

, 137
3
, 24
, 113
, 113
, 145
, 113
, 145
, 273

, 227
, 229

279

-
, 212
ODBC, 256
, 101

, 47
, 224

, 222
, 24, 122, 220
, 29, 273
AND, 52, 87
AS, 74, 134
ASC, 41
BETWEEN, 48
DEFAULT, 174
DESC, 41
FROM, 166
IN, 56, 57
INTO, 154
NOT, 57, 58
ON, 229
OR, 53
OUT, 199
PRIMARY KEY, 221
REFERENCES, 223
UNIQUE, 225
, 80
, 145
, 160
, 43
, 211
, 217
, 215
, 214

,
140

280

, 123
, 62
, 62
, 66
, 65
ODBC, 256

, 207

, 163
, 121
, 138
, 140
, 141
, 140
, 128
, 137
, 129
, 128
, 128
, 219
, 225
, 224

ALTER TABLE, 176, 259


CLOSE, 217
COMMIT, 207, 260
CREATE INDEX, 229, 260
CREATE PROCEDURE, 260
CREATE TABLE, 169, 259,
261
CREATE VIEW, 185, 261
DECLARE, 214
DELETE, 165, 261
DROP, 185, 262
DROP TABLE, 178
EXCEPT, 152
EXECUTE, 197
FETCH, 215

GRANT, 232
INSERT, 153, 262
INSERT SELECT, 158, 262
INTERSECT, 152
LIKE, 61
MINUS, 152
OPEN CURSOR, 215
RENAME, 179
REVOKE, 232
ROLLBACK, 207, 262
SAVEPOINT, 209
SELECT, 29, 263
SELECT INTO, 160
SQL, , 259
TRUNCATE TABLE, 167
UNION, 146
UNION ALL, 150
UPDATE, 163, 263
WHERE, 51
, 31

, 140
,
32

,140
, 45

!=, 47
, 47
, 205
, 205

, 24, 122, 220


, 221
, 179
,
128
, 113
, 70
, 69, 70
, 22


,
141

, 126

, 226

, 54
,
140
, 62, 98
, 36
ALL, 103
FROM, 43
GROUP BY, 102
HAVING, 105
ORDER BY, 36
SET, 164
VALUES, 156
WHERE, 43
, 43
, 181
, 185
, 185

, 48
, 46
, 48
, 44
, 46
, 74, 133
, 133
, 134

, 121

, 135

, 34
, 34
, 55

281

, 273
, 273
, 145

, 229
, 214
, 169
, 198

,
39
, 35
,
37
, 37
, 38

, 151
, 142
,
96
, 22, 70

, 126
, 76
, 23
, 266
, 266
, 20
, 22

, 21
, 179
, 121
, 169
, 162
, 21
, 178
, 21
, 23, 265
, 269

282

, 270
, 269
, 266
, 226
, 23
, 267
, 268

, 205
, 205, 209
, 203
, 206
, 229

, 165
, 178

, 205
, 207
, 208

SQL, 44
, 44
,
114

, 51
, 79
AVGQ, 90

COUNT(), 92
DATAPART(), 87
LTRIM(), 74
(), 93
MIN(), 94
RTRIM(), 73
SOUNDEX, 82
SUM(), 95
TRIM0, 74
UPPER0, 82
, 90

, 84
,
82
,
87
, 90
X
, 179,
193

, 62


, 25

SQL,
10

..
.
..
. .

"".
101509, , . , . 43, . 1.
29.04.2005. 848/32.
Times. .
. . . 15,1. .-. . 9,9.
3000 . 1645.
" "
,
.
197110, -, ., 15.

MySQL

- ISBN: 5-8459-0769-1
,
,
MySQL. MySQL
,
,
Web. , -
,
MySQL, - MySQL
.

h t t p : / / w w w . w i l l i a m s p u b l i s h i n g . c o m

M Y S Q L .

MySQL AB

MySQL
-!? !3*&&,

www.williamspublishing.com

ISBN 5 - 8 4 5 9 - 0 8 0 5 - 1

MySQL



.

,
API-,

, MySQL

Web,
.


,

MySQL. ,
,
,
,
,
,


.


Web-
,

.

M Y S Q L


MySQL AB

"

MySQL

www.williamspublishing.com

ISBN

5-8459-0804-3

,

MySQL AB,
SQL,

,

SQL MySQL.
.
,
,
, ,
,
MySQL;
,

.
, MySQL



.

,
API-,

, MySQL

Web-,
.

Web-
,

.

Оценить