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

"" -

"Books.Ru - "

Oracle 10g (ISBN 5-8459-1113-3)
- "Books.Ru - ".
- ,
.
.

 Ora le 10g  . 4

32.973.26-018.2.75
27
681.3.07


.

..

..

..


: infowilliamspublishing. om, http://www.williamspublishing. om
115419, , / 783; 03150, , / 152

, ,
, .
15 27 Ora le 10g. : . .  . : .. , 2006. 
704 . : .  . . .
ISBN 5-8459-1113-3 (.)
 , Ora le, .
.
, .
, (
Ora le).
.
.
32.973.26-018.2.75

.

 , , 
, Prenti e Hall, In .
2006 by Pearson
Authorized translation from the English language edition published by Prenti e Hall, Copyright
Edu ation, In ., Upper Saddle River, New Jersey, 07458.
Pearson Prenti e Hall. All rights reserved. This publi ation is prote ted by Copyright and permission should be
obtained from the publisher prior to any prohibited reprodu tion, storage in a retrieval system, or transmission in
any form or by any means, ele troni , me hani al, photo opying, re ording, or likewise. For information regarding
permission(s), write to: Rights and Permissions Department.
Pearson Prenti e HallTM is a trademark of Pearson Edu ation, In .
R is a registered trademark of Pearson pl
Pearson
R is a registered trademark of Pearson Edu ation, In .
Prenti e Hall
Russian language edition published by Williams Publishing House a ording to the Agreement with R&I Enterprises
2006
International, Copyright

ISBN 5-8459-1113-3 (.)


ISBN 0-13-195740-6 (.)

, 2006

by Pearson Edu ation, In , 2006

 Ora le 10g  . 5

21

1.  Ora le 10g

31

2. SQL SQL*Plus

95

3. , ,

153

4.

225

5.

305

6.

393

7. PL/SQL

471

8. Forms Builder

545

9. 

599

10. 
11.

CD1
CD73

12.

CD123

13.

CD171

675

693

 Ora le 10g  . 6

 Ora le 10g  . 7

21
25

, ,

25

(www.prenhall. om/perrypost)

27

27

Ora le

28

28

1.  Ora le 10g

29
30
30

31
31

32

33

35

38

38

41

47

48

Ora le

51

Ora le

53

Ora le

56

Developer Suite

57

61

Ora le

64

SQL*Plus iSQL*Plus

64

Developer Suite

68

Ora le Enterprise Manager

69

72

Redwood Realty

74

Coffee Mer hant

77

Rowing Ventures

79

Broad loth Clothing

83

90

 Ora le 10g  . 8

2. SQL SQL*Plus

95
95

SQL

96

 SQL

97

99

100

SQL*Plus DOS

102

Ora le

102

SQL

103

 SQL

104

SQL*Plus for Windows

105

Ora le

105

SQL

106

 SQL

107

SQL*Plus

110

iSQL*Plus

110

Ora le

110

SQL

111

 SQL

113

iSQL*Plus

113

SQL

113

SQL

115

DLL-

116

DML-

119

TC-

125

DCL-

128

SQL*Plus

130

131

132

133

SQL*Plus

136

137

139

SQL*Plus

140

141

145

145

147

 Ora le 10g  . 9

Redwood Realty

147

Coffee Mer hant

149

Rowing Ventures

150

Broad loth Clothing

152

3. , ,



153
153

154

154

155

156

Ora le

158


Ora le

159
159

160

160

162

163

164

164

Ora le

166

SQL*Plus

173

176

178

178

179

180

186

188

190

191

193

194

195

197

198

200

203

 Ora le 10g  . 10

10

204

207

209

210

211

212

212

213

214

215

216

218

4.

225
225

230

230

232

234

238

240

245

UPDATE

246

CASE

251

CASE

252

254

259

260

262

263

267

268

269

270

273

273

BEFORE

276

AFTER,

280

285

 Ora le 10g  . 11

11

288

291

294

295

5.


305
305

SELECT

306

309

DISTINCT

311

312

313

321

326

 SQL

334

335

358

366

HAVING

368

SQL*Plus

370

382

385

387

6.

393

394

395

404

418

424

441
441

443

447

449

455

 Ora le 10g  . 12

12

455

457

459

461

463

4. Broad loth Clothing

469

7. PL/SQL
PL/SQL

471
471

PL/SQL

472

PL/SQL

473

474

475

476

478

479

Redwood Realty

479

480

SQL*Plus

483

PL/SQL

484

485
487
490

PL/SQL

490

493

FOR

498

IF

501

507

, 

509

509

513

 

515

  

517

520

520

521

525

,

526

528

531

 Ora le 10g  . 13

13

532

533

535

8. Forms Builder


545
545

546

549

 Ora le

551

553

559

562

 

563

564

565

566

569

572

575

577

579

582

582

584

588

590

591

592

593

595

9. 

599

602

605

608

611

, 

612

616

619

623

627

 Ora le 10g  . 14

14

629

629

632

635

639

639

641

644

647

Web- JDeveloper

649

 

650

651

653

JSP

657

660

665

667

668

670

10. 

CD1
CD1

Web

CD2

CD2

Ora le

CD5

CD7


Reports Builder

CD7
CD9

Paper Design Paper Layout

CD13

CD17

CD20

CD22

CD23

CD24

CD25

CD26

CD28

CD29

CD32

CD34

CD36

CD41

 Ora le 10g  . 15

15
CD43

CD44

CD46

CD48

CD49

CD50

CD53

CD53

Web

CD55

CD57

 Web-

CD57

Web-

CD62

CD65

CD66

CD67

CD68

11.

CD73

 ,

CD73

Redwood Realty

CD74

CD76

CD77

CD79

CD82
CD85

CD87

CD88

CD92

 Ora le

CD97
CD100

CD100

CD101

CD103

CD106

 

CD108

Web- Ora le

CD110

HTML-

CD110

 

CD115

CD117

CD118

CD118

 Ora le 10g  . 16

16

12.

CD120

CD123
CD123

CD125

CD126

CD129

CD133

CD134

CD136

CD140
CD142

CD143

CD146

CD147

CD150

CD151

CD152

wrap

CD155

CD155

CD157

CD159

CD161

CD163

CD163

CD164

CD164

CD167

13.

CD171

CD171

Enterprise Manager

Cd173

CD178

CD179

CD180

Undo Redo

CD186

CD187

CD187

 Ora le 10g  . 17

17

CD189

CD191

CD193

SQL-

CD194

Data Pump

CD194

SQL*Loader

CD195

CD199

CD199

CD201

CD204
CD205


( )

CD207


( )

CD209
CD210

CD211

CD217

CD226

CD227

CD230

CD231

CD233

675

693

 Ora le 10g  . 18

 Ora le 10g  . 19

 Ora le 10g  . 20


. 

-. 
, 
. ,
, , ,
Mi rosoft Of e . , , , .
  -  
. , (
), . , ,
.
. 
 .
( ), ( ').
 , 
, Communi ation of the ACM, Management Information Systems Quarterly De ision S ien es.
 , . , ,
.

 Ora le 10g  . 21

, Ora le 10g.
1, , (Database
Management System  DBMS, ) /, Ora le,
, ,
. Ora le Ora le,  SQL*Plus,
Developer Suite Ora le Enterprise Manager. , ,
: Redwood Realty, Coffee Mer hant, Rowing Ventures Broad loth Clothing.
2 SQL  Ora le,
, SQL*Plus for Windows iSQL*Plus.  , ,  ,
Redwood Realty, SQL SQL*Plus iSQL*Plus. ,
SQL*Plus ( SQL*Plus) ,

SQL*Plus.
3 , ,
. , Ora le,
, ,
. , ( ) , , ,
( ).
, , ,
; , .
,
.
4 SQL- (Data
Manipulation Language  DML), .
 , -

 Ora le 10g  . 22

22

,
Ora le . ,  SQL
UPDATE, DELETE.
COMMIT ROLLBACK. ,
DML-. , (trigger) . ,
, ,
, .
5 SELECT  SQL ,
 , . SELECT, FROM,
WHERE ORDER BY, SQL.
, ,
.  Ora le:
,  ,  , , NULL ,
. 
SQL*Plus .
6 SELECT , . , , , , .
 UNION, MINUS INTERSECT, . ,
,
. ,
, . Ora le, 
.
7 . ,
PL/SQL, , . ,  PL/SQL
PL/SQL. , 
.

 Ora le 10g  . 23

23

8  .
.
. , ,
. Forms Builder.
: , .
9 ,
Forms Builder.  ,
 ,
 , , .
 ,
.
10 : .
, .
, .

. ,
, . ,

, , .
, , ,
Web-.
11 ,
.  SQL-. ,
. , ,  
.  .
 .
. , Ora le Appli ation Server ( ).
12 ,
. ,
.
, , . ,
, . ,  ,

 Ora le 10g  . 24

24

- . , , , , -
.
13 .
Enterprise Manager.  . ,
SQL,
, . Ora le,
.
, , , , . ,
  , . , ,
, .

.
,
. , . ,
. ,
- , 
. ,
 , , .
, ( !) .
. , , , / , ,
, ,
. , Ora le . , ..
. , ,  Redwood Realty.
. Coffee Mer hant  . ,
.
- . -

 Ora le 10g  . 25

25

, 
, .
Rowing Ventures, , ( , ,
, ).
Broad loth Clothing  , (
). ,
,  , .
,
, .
, , , Ora le . 
Ora le .

, ,

, . ,
.

. , 4 , , .
. ,
  .
, , .
, ,
, , .
.
( -

 Ora le 10g  . 26

26

, ), .
,
,  ,
, , .
. , Redwood Realty,
. 
, , , .

, .
. Coffee Mer hant ; . Coffee Mer hants
, . Consumers, , 1 587 ,
. Inventory ,
(  Huehuetenango Caravan). Des ription Inventory
 ( 100 ).
Rowing Ventures , ,
 .
.
, . , , ,
 .
Broad loth Clothing, , . , . Broad loth Clothing , Customer
 ; Fa tory,
, ; Complian e, ,
 . Rowing
Ventures,  ,
( ).

 Ora le 10g  . 27

27


(www.prenhall. om/perrypost)

,
Ora le. , , , . ,
- . , , Web www.prenhall. om/perrypost. www.prenhall. om. , Web www.prenhall. om/perrypost  , 1. ,   , .
 , ,
Readwood Realty ( ).
, .


Ora le ( , ). ,
. 13 ,  ( )
Ora le . -.
,
, 17, SQL.
Ora le,
. , ,
. 1
, .
2 SQL SQL*Plus; ,
, . SQL
5 6,
(Data Denition Language  DDL) -

 - files. 

. .

 Ora le 10g  . 28

28

(Data Manipulation Language  DML). 5


2. 3 4,
6, ,
( , ..). , 6 .
7 PL/SQL.
,
, . ,
, 13.
811  , ,
. 12 , 13,
, .

.
Ora le

Ora le 10g,

www.ora le. om, . -

, ,
. , ,  ,
( , ) , . , Ora le
, , , . ,
, -, .

 . ,
. ,
, . ,
(Vladan Jovanovi ), Georgia Southern University
 (Jennifer Kreie), New Mexi o State University
(Karen Nantz), Eastern Illinois University
(Jeremy Smith), Carnegie Mellon University
. (Thomas P. Sturm), University of St.Thomas,
(William Thomas), Juniata College,

 Ora le 10g  . 29

29

 Pearson Prenti e
Hall. (Bob Horan), (Jeannine Ciliotta), (Ana Cordero) (Anne Graydon)
.
!
, , (Nan y) (Sarah),
. .
,
, ! perrypostprenhall. om.
,  , Ora le 10g Web- www.prenhall. om/perrypost.


, , .
, ,
.
, .
. Web-
. ,
, , ,
.
, ,
. .
:
E-mail:
WWW:

infowilliamspublishing. om
http://www.williamspublishing. om

 Ora le 10g  . 30

30

:
:
:

115419, , / 783
03150, , / 152

  .


, Redwood Realty, . , - Web- (www.prenhall. om/perrypost).
, , . ,
.
Redwood Realty ,
.
. , . 
. Notepad (),
 Windows Explorer,
.

 Ora le 10g  . 31


Ora le 10g
. . .



Ora le,
 ,

, (Database Management System  DBMS),


.
. Web-
   . .
,  , .
. ,
, ( ) 
, .
.
.
, Ora le,

 Ora le 10g  . 32

32

1.  Ora le 10g

.
,  ,
, .
, , . ,  . ,
( Ora le), SQL. SQL , - .
Ora le SQL, Ora le SQL. ,
,  ,
.
Ora le Developer Suite, , 
.
. ,  , .
  ,  . , (Database administrator 
DBA), ,
, ,
.


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

 Ora le 10g  . 33

33


, ,
, .
,
, . ,
, - . -
.
.
. ,
 . ,
. , Ora le
() ,
.
Ora le (. 1.1).
, . ()
, .
,
,
. Ora le,  .
, .
,  ,
. , .
() ,
, .
Ora le  Java. , .
,  , . , ,
. , .

 Ora le 10g  . 34

34

1.  Ora le 10g



. 1.1.

,
 .
.  . , ( -
, Ora le
).  ,
 -
.
, , , Web. - ,
. , ,
,
. , , ? ,
, - ,
. , -
, .

 Ora le 10g  . 35

35

Ora le Java, , Web-


Java ( ). ,
, . , ,
Web- Java (http://www.java. om), 
.

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

( ). , -
 (Entity-Relationship Diagram  ERD). UML (Unied Modeling Language  
).
. UML , , , .
.

( ) .
( ) , . , Customer. Customer : CustomerID, Last Name, First Name, Phone, Address City.
, . ,
Customer :

151, 'Jones', 'Mary', '111-2222', '123 Main', 'Eureka'

 Ora le 10g  . 36

36

1.  Ora le 10g

1.1.


Text

Number

Date

Binary

123 Main
Street
, 1.2345 15.32
 .153
,

(
0, ). Ora le

. ,
, ,
,

(, ,  )

13-JUL-2006

Pi ture.jpg

, .
, .. , , .  , , . 1.1.
, , , . ,  ,
Text.
. Number Date , . ,
, . ,  , ,
( ) .
,
Invoi eID.
,
,
.
, ,
.
,  . , - .

 Ora le 10g  . 37

Customer
CustomerID
LastName
Fir stName
Phone
Address
City

37

CustomerOrder

1
*

OrderID
OrderDa te
CustomerID

. 1.2.   -

Customer CustomerOrder

,
. , - Customer Order ( ), ,
( ). Customer Order , OrderID OrderDate. , CustomerID,
Customer. , Customer
Order : 1201, '06-JUN-2006', 151. 151 
CustomerID ( ); , , CustomerID 151 Customer.
. 1.2 , Customer
CustomerOrder. , , . .
, , , CustomerID
CustomerOrder CustomerID Customer.
.  ,
1,  * ().  . ,
, ( ) , CustomerOrder. ,
,  1
, Customer. ,  ,
. .
? ,
, ,
.

. .
, ; ,

 Ora le 10g  . 38

38

1.  Ora le 10g

,
.

, 
 . ,
. . . . (E. F. Codd), ,
, (relation), . (relational) .
,
-. ,
. , , . 1.3 . , ,
. 1.2. ,
, .. . 
, , . ,
 .
 - , (CellPhone, HomePhone ..),
 . ,
CustomerID.
CustomerOrders, ,
 151. CustomerID
, ,  Customers. ,
.

. ,
, Ora le
 .


,  .
. -

 Ora le 10g  . 39

39

CustomerOrders
OrderID

OrderDate

CustomerID

1201

06-JUN-2006

151

1202

06-JUN-2006

155

1203

07-JUN-2006

151

Customer s
CustomerID

LastName

FirstName

Phone

Address

City

151

Jones

Mary

111-2222

123 Main

Eureka

152

Smith

Susan

222-5555

738 Elm

Eureka

153

Brown

David

111-2355

235 East

Eureka

154

Sanchez

Maria

999-3332

351 Ocean

Arcata

155

Steuben

Saul

555-2351

111 Main

Orick

156

Hayworth

Michele

231-3252

761 West

Loleta

. 1.3.

 . 
(), .  .
.
 ,
.
.
, , CustomerID.
, .
, . 
,
. , Ora le , , . ,
Ora le . ,
. 1.3, OrderID ,
 , .
, , , . (
.) , Customer:

 Ora le 10g  . 40

40

1.  Ora le 10g

Student
StudentID
LastName
FirstName
Phone

Participants
1
*

OrganizationID
StudentID
Role

Organization
*

OrganizationID
Title
Advisory
Category

. 1.4.    


. , ? ?  : ,
, - ?
.
. , .
, . ( ) ,
,  , .
 , , , ,
. 1.4. Student 
(, , ). Organization , .
Parti ipants  ,
. Parti ipants OrganizationID StudentID.
, . , , . 1.5.
OrganizationID ,
. StudentID
, . OrganizationID,
,  ,   .
StudentID, , .
OrganizationID StudentID,
 , , , .

 Ora le 10g  . 41

Organization

41

Student

OrganizationID

Title

Advisor

Category

StudentID

LastName

FirstName

Phone

101

Lacrosse

Amerit

Spor ts

12013

Fellini

Federico

111-4444

115

Phi-Beta

Smith

F ra ter nity

12315

Bergman

Ingmar

222-3331

125

Student Council

Antonio

Go v e r nance

12551

Truffaut

Francois

888-2221

23664

Kurosawa

Akiro

999-4491

Participant
OrganizationID

StudentID

Role

101

12013

Captain

101

12551

Member

115

12013

Member

115

23664

President

125

12551

Treasurer

125

12315

Member

. 1.5. , , 


, , .
. . ,
, . ,
,  . .
.
. .  .
. 1.6 - .
 , ? Customer
 - , .
, , CustomerOrder.  .
, ,
.
, .
, , - . ,
 Customer ? . 1.7 . -, ,
Value, -

 Ora le 10g  . 42

42

1.  Ora le 10g

Customer Order
OrderID

Date

CustomerID
First Name Last Name
Phone
Address
City, State Zipcode

ItemID

Price

Description

Quantity

Value

32.95

1526

32.95

Basketball

3921

79.92

Running shoes

79.92

4797

1.59

Racquetballs

4.77

. 1.6. 

. , , , .
, ,
.
,  .
,
.  . . ,
(Customer) 155 ?
 ! : 1577? , ;
, . ,
.
? , ( )!
, .


,
. ,
. ,
, .
. 1.7 .

 Ora le 10g  . 43

43

. ItemID
OrderID,  , . 1.6.
, ,
, . 1.8. ,
, ,
, , . , . 1.8,
. ,
. 1.7, .
. , . 1.7, , ,
. , ,
, . OrderID,
.
 . 1.9. , OrderID,
.
OrderID ItemID - .
, .
.


. 1.9, ,
- . , ItemID 1526,
. -  , -
 , .
- ,
ItemID. , ItemID 1526,
, .
OrderID.

06-JUN-06

06-JUN-06

06-JUN-06

06-JUN-06

1201

1201

1202

1202

Saul

Saul

Mary

Mary

Mary

FName

Steuben

Steuben

Jones

Jones

Jones

LName

555-2351

555-2351

111-2222

111-2222

111-2222

Phone

111 Main

111 Main

123 Main

123 Main

123 Main

Address

Orick

Orick

Eureka

Eureka

Eureka

Cityte

06-JUN-06

06-JUN-06

1201

1202

155

151

CustID

Saul

Mary

FName

Steuben

Jones

LName

555-2351

111-2222

Phone

111 Main

123 Main

Address

Orick

Eureka

City

CA

CA

State

CA

CA

CA

CA

CA

Sta

95022

95001

ZIP

95022

95022

95001

95001

95001

ZIP

15.72

3144

1.59

4797

32.95

79.92

3921

1526

32.95

Price

15.72

32.95

1.59

79.92

32.95

Price

1526

ItemID

3144

1526

4797

3921

1526

ItemID

 -

OrderDate

OrderID

. 1.8.

155

155

151

151

151

CustID

06-JUN-06

1201

. 1.7.

OrderDate

OrderID

Baseball

Basketball

Racquetballs

Running Shoes

Basketball

Description

Baseball

Basketball

Racquetballs

Running Shoes

Basketball

Description

Qty

Qty

 Ora le 10g  . 44

 Ora le 10g  . 45

45

OrderID

OrderDate

CustID

FName

LName

Phone

Address

City

Sta te

ZIP

1201

06-JUN-06

151

Mary

Jones

111-2222

123 Main

Eureka

CA

95001

1202

06-JUN-06

155

Saul

Steuben

555-2351

111 Main

Orick

CA

95022

OrderID

ItemID

Price

Description

Quantity

1201

1526

32.95

Basketball

1201

3921

79.92

Running Shoes

1201

4797

1.59

Racquetballs

1202

1526

32.95

Basketball

1202

3144

15.72

Baseball

. 1.9.

, , , . ,
. , , ,
ItemID, OrderID.
, OrderID, ItemID,
, .
.
? ,
.
-.
, ,
, , .
 , , , :
, ItemID, ,
OrderID ItemID. , ,
.
,  .
,
, . ,
, (. 1.10). OrderItems
Items .
order- ustomer ,
.

 Ora le 10g  . 46

46

1.  Ora le 10g

order-customer
OrderID

OrderDate

CustID

FName

LName

Phone

Address

City

State

ZIP

1201

06-Jun-06

151

Mary

Jones

111-2222

123 Main

Eureka

CA

95001

1202

06-Jun-06

155

Saul

Steuben

555-2351

111 Main

Orick

CA

95022

OrderItems
OrderID

ItemID

1201

1526

1201
1201

Items
Quantity

ItemID

Price

Description

1526

32.95

Basketball

3921

3144

15.72

Baseball

4797

3921

79.92

Running Shoes

1202

1526

4797

1.59

1202

3144

. 1.10. ,

Racquetballs

ItemID

order- ustomer (. . 1.10) .


, . 1.7,
. -  . 
. ,
(, .) CustomerID,
OrderID. CustomerID,
OrderID.
, , , . ,
. CustomerID, ,
. ,
. ,
CustomerID, .
. 1.11.
, . , ,
 ,
, , ,
.

 Ora le 10g  . 47

Order
s

47

Customers

OrderID OrderDate CustomerID

CustomerID FName LName

Phone

1201

06-JUN-06

151

151

Mary

Jones

111-2222 123 Main Eureka CA

1202

06-JUN-06

155

155

Saul

Steuben 555-2351 111 Main Orick

OrderItems

Address City

State ZIP

CA

95001
95022

Items

OrderID

ItemID

Quantity

1201

1526

1201

3921

1201

4797

1202

1526

1202

3144

ItemID Price

Description

1526

32.95

Basketball

3144

15.72

Baseball

3921

79.92

Running Shoes

4797

1.59

. 1.11. ,

Racquetballs

CustomerID

Orders OrderID, (OrderDate) , (CustomerID).


Customers CustomerID,
. Items ItemID, . OrderItems -
  . OrderID ItemID  . ,
.
 .
, .
,
. , , , -
.
  , ,  ,
. . , .


. 1.11 , , .

 Ora le 10g  . 48

48

1.  Ora le 10g

.
, . , ,  . , Orders
CustomerID,
Customers.
 Orders . OrderID , ,
. , , CustomerID
Orders.
, ,
, ..
. - .
, Orders CustomerID.
, CustomerID Customers, Orders (foreign key).
, Ora le, . ,
OrderItems : OrderID ItemID,  .
, Customers Items
. ,
 . , ,
. , , Orders
. ,
OrderItems (. . 1.11) Items,
OrderItems, Ora le . ,
CustomerID, 190,
Customers .
(referential integrity)
, ,
.

-
1990- ,
- , (Obje t-Oriented Programming  OOP).   ,
-

 Ora le 10g  . 49

49

. ,
,
.  .  -
. ,
. ,
Customers, .  . ,
, . ,
(entity),
, , ,  . -
. .
-, - . Ora le
, .  ,
.
- : -,
; -, .
. , , .
, (, , ).
, ? ,
 .
,
. , . , ,
.
, . , ,
, . , - Lo ation,
: Longitude (), latitude ()
altitude ( ). .
Ora le CREATE TYPE, . .

 Ora le 10g  . 50

50

1.  Ora le 10g

Account
AccountID
CustomerID
DateOpened
DateClosed
OpeningBalance

Checking Account
MinimumBalance
InterestRate
MonthlyFee

Savings Account
InterestRate
QuarterlyFee

Student Checking
MaxChecksPerMonth
OverdraftLimit
. 1.12. 

Ora le,
.
-
. ,
,
, . ? ,
. . 1.12
.  A ount , .
, , .
 Che king A ount Savings A ount. ,
A ount. ,
( - , ).
, Student Che king A ount.
 .
Ora le
. ,

A ount. .

 Ora le 10g  . 51

Ora le

51

,
. . . - . , ,
.
,
. , , 
A ountID .
- , . , -
, , CREATE TYPE,
.

Ora le

Ora le , .
, Ora le , , Ora le,  Ora le.
. 1.13. , (Database Administrator  DBA), ,
.  , , , ,
.
,
. ,
.  , ,
, . ,
, . ,
. , PL/SQL ,
.
. , ,

 Ora le 10g  . 52

52

1.  Ora le 10g


Developer
Suite

Oracle


PL/SQL


. 1.13.

. Ora le , Enterprise Manager   ,


Ora le
. ,
. , Ora le
.
 . ,
 , Ora le Enterprise.

. , Ora le 
, , .
Developer Suite.
. Ora le Developer Suite . ,
Developer Suite ,  .

 Ora le 10g  . 53

Ora le

53

Ora le
, Ora le ,
.
(Linux, Windows XP, Unix
.), Ora le.
. , Ora le .
Ora le 10g ,
, Web-. , , 
. , . ,
 , , . Ora le.
,  Ora le Enterprise Manager TCP/IP, . , -
, . Developer Suite , .
.
, ,
, , .
, .
 .
Ora le.
Ora le, .
,
.
, Ora le, .
1. .
Ora le Te hnology
Network (http://otn.ora le. om). Ora le.
2. , , ,
. ,  setup.exe. , Install/Deinstall Produ ts.

 Ora le 10g  . 54

54

1.  Ora le 10g

3. ,
, , . .
 , .
4. , Ora le, ,
2 (. 1.14).
 , Personal Ora le.
, (Global Database
Name) - , ORCL.
5.

. ,
,  .

6. Next .
Install .
. ,
. OK.
7. (. 1.15), ,
. , TCP/IP, .
. ,
,
. ,
Exit.
8. Enterprise Manager. ,
. , . (: http://<server name>:5500/em.)
- ,
.
9. Enterprise Manager, system
, 3.
10. ,
. Administration,
Users Se urity. Create.
, ,
OK.  .
. , ,

 Ora le 10g  . 55

Ora le

. 1.14. Ora le

. 1.15. , URL

55

 Ora le 10g  . 56

56

1.  Ora le 10g

. , Developer Suite, 
.

Ora le
, ,
.
.
, Ora le ,
.
1. , Windows, Ora le.
Windows Start/All Programs/Administrative Tools/ Computer
Management (/ // ),
Servi es () Servi es and Appli ations (
), . 1.16, ,
Ora le. Stop (). ,
Properties () Automati ()
Manual (). .
2. Ora le .
Windows Start/All Programs/Ora le . . . home/Ora le Installation Produ ts/Universal Installer (/ /Ora le . . . home/Ora le Installation Produ ts/Universal Installer). Deinstall Produ ts . . 1.17
. Remove, .
, Yes .
3. .
 . Windows Explorer, Ora leHome . ,
C:\Program Files\Ora le.
4. , Ora le
. .
, . ,
Windows Start/Run (/ ), regedit
<Enter>. , . 1.18,
HKEY_LOCAL_MACHINE, SOFTWARE, ORACLE
<Delete>.

 Ora le 10g  . 57

Ora le

57

. 1.16. Ora le

5. ( HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Servi es HKEY_LOCAL_MACHINE\SYSTEM\ControlSet002\Servi es). ,


, Ora le: Ora leOraDb10g_home1TNSListener,
Ora le.<dbname>, Ora leDBConsole<dbname>, Ora leOraDB10g_home1iSQL*Plus,
Ora leCSServi e, Ora leDBConsole<dbname> Ora leServi e <dbname>.
Edit/Find (/)
, Ora le. , .
6. Start (). Start (), Ora le, , Delete ().
Ora le.
HKEY_LOCAL_MACHINE\SYSTEM\ControlSet002\Control\Session Manager\Environment Advan ed ().
 , Ora le . .
Ora le , .

Developer Suite
Developer Suite . ,

 Ora le 10g  . 58

58

1.  Ora le 10g

. 1.17. Ora le

 Ora le Appli ation Server.


Ora le; , ,  Developer Suite .
,
.
1. Developer Suite. Install. .
Next.
2. Ora le Home (. 1.19).
, (path) (Name) Developer Suite. ,
1 . Next.
3. ,
. Rapid Appli ation Development,

 Ora le 10g  . 59

Ora le

59

Oracle

. 1.18. Ora le

Complete,
. Next.
4. ,
SMTP-. ,
,
. Next.
5. Install.

70% . .
,  . Windows
Ora le, ,
Ora le Developer Suite.
Ora le , .
.
.
Developer Suite. - , -

 Ora le 10g  . 60

60

1.  Ora le 10g

. 1.19.

,
, , . Developer Suite ,
, : Ora le , , .
, Ora le Internet Dire tory Ora le
Names Server, . , ,
, .
Ora le ,
.
1. Start () Windows Ora le Developer Suite/Conguration and
Migration Tools/Net Manager.  Servi eNaming ,
- (. 1.20).
extpro . , ,
.
2. Servi eNaming, Create (
)
. , , ( LabDB).
Next.

 Ora le 10g  . 61

Ora le

61

3. Proto ol TCP/IP-, ,
Next.
4. , . ,
,
, Windows. (1521)
Next.
5. ,
4 .
ORCL, , . 1.21, -
, Next.
6.

. .
Test. , (s ott) , . Change
Login .
Test.

7. ,
 Finish. Net
Manager File/Save Network Conguration,
. Net Manager.
8. ,
.
-, , , ,
. , ,
, .
, Ora le
.


Appli ation Server Forms and Reports Servi es
, -. Appli ation Server
Forms and Reports Servi es , .
, OTN. (  ).
Appli ation Server,
, Appli ation Server  , Developer Suite 
. -

 Ora le 10g  . 62

62

1.  Ora le 10g

. 1.20.  Ora le

, , Developer
Suite . ,  . 
.
 , .
1. OTN Forms and Reports Servi es. , Appli ation Server Forms and Reports Servi es,
.  
setup.exe. Wel ome Next.
2.  , . ,
, ,
OraDB10g_AS. , Next.
3.  . , (

 Ora le 10g  . 63

Ora le

63

. 1.21.

), . , , Next.
4.  Ora10gAS.
, .
(ias_admin) . Next.
5. , , SMTP-,
. , , , , , ,
. 
. Next, 
Install .
6. URL (. 1.22). . 80  Web-.
, .
 . URL
1810 TCP/IP Enterprise Manager . 
( ias_admin , 4).
Appli ation Servi es - . ,
, , 
, .

 Ora le 10g  . 64

64

1.  Ora le 10g

. 1.22.  Forms and Reports Servi es

Ora le

Ora le, , , , , . , ,
, ,
.  Ora le , .
,
. ,
.

SQL*Plus iSQL*Plus
 Ora le, SQL*Plus iSQL*Plus, 
. , - .
Ora le , SQL-.

 Ora le 10g  . 65

Ora le

65

( SQL- , , .)
 Ora le, SQL*Plus. Ch01Data - 
(Ch01Explore.sql).  .
1. SQL*Plus Windows.
. 1.23. Ora le, Appli ation Development SQL Plus.
2. . . 1.24 .
, , . ,
, Host String. Ora le , ,
. OK.
3. SQL*Plus .
SQL, . ,
, Windows Explorer. SQL*Plus

<>\Ch01Explore
<> . . 1.25. Clients . : ClientID,
FirstName, LastName, Category Balan e. Category
(Builder, Banker ..); Balan e  ,
, .
4. , SELECT * FROM Clients; <Enter>. .
, , ,
, SQL*Plus
.
5. Ora le SQL*Plus,

Exit <Enter>.

, Ora le - iSQL*Plus, .
, Web-.
iSQL*Plus Ora le,
.

 Ora le 10g  . 66

66

1.  Ora le 10g

. 1.23. SQL*Plus

. 1.24.

. 1.25.

1. Web-. URL iSQL,


. , , ( <server name>
):

 Ora le 10g  . 67

Ora le

67

http://<server name>:5560/isqlplus
. , URL,
. ,
.

http://127.0.0.1:5560/isqplplus
( , . URL iSQL*Plus.)
2. Ora le Username 
Password. Conne tIdentier .
Login, Workspa e.
3. Workspa e . <Enter> ( SQL*Plus):

SELECT * FROM Clients;


4. Exe ute, . Ora le
16 Clients (. 1.26).
. 1.26 .
,  ,
. , , SQL*Plus iSQL*Plus. SQL*Plus,
 ( Windows).
. SQL*Plus ,
,
. , iSQL*Plus
,  , SQL*Plus. -
SQL*Plus,
.
, SQL-,
.
1. Workspa e, Clear, , . Workspa e
, iSQL*Plus .
2. Workspa e .
Exe ute, .

DROP TABLE Clients PURGE;

 Ora le 10g  . 68

68

1.  Ora le 10g

. 1.26. iSQL*Plus

3. iSQL*Plus, ,
Ora le.
4. .

Developer Suite
Developer Suite Forms Builder Reports
Builder. ,
.  , .

 Ora le 10g  . 69

Ora le

69

,
, ,
. .
. 1.27 ,
Clients. ,  . ,
NextRe ord (

) PreviousRe ord (

), . , Save (
. 
. Enter Query (

),  (,

Banker Category), Exe ute Query ( ).


 , . 
, .
. 1.28 , Clients.
,
Category (Banker, Builder ..).  .
Report Wizard.  Web-. Ora le PDF-, , .
,
 . ,  , . ,  (Clients.fmb) (ClientBalan es.jsp),
- ( , ). , Ora le.
, , ,
 ,
. 8,
.
Ora le Enterprise Manager
Forms Builder Reports Builder
.  . , SQL*Plus iSQL*Plus.
.

 Ora le 10g  . 70

70

1.  Ora le 10g

. 1.27. 

Clients

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

 Ora le 10g  . 71

Ora le

. 1.28.

71

Clients

, Enterprise Manager,  , .
, ( ), .
, Enterprise Manager . Ora le
, System,
. ,
, , ,
Enterprise Manager.
, Enterprise Manager
. ,
, .
.
, Enterprise Manager, .

 Ora le 10g  . 72

72

1.  Ora le 10g

1. TCP/IP, . Web URL . ( ).

http://<server name>:5500/em
2. . Enterprise Manager, 
.
3. (Home, Performan e, Administration Maintenan e) 
 . . 1.29 Administration.
(, Storage Se urity).
4. Users Se urity,
 .
Ora le ,
, System.
, ,
. : ,
. , , .
.
5. Logout ,
.


. Redwood Realty.
 ,  , . () ( ).
, .
. ,
( Redwood Realty);
, , .. ,

 Ora le 10g  . 73

. 1.29.

73

Administration Enterprise Manager

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

 Ora le 10g  . 74

74

1.  Ora le 10g

Redwood Realty
Redwood Realty
, , , Ora le.
Redwood Realty. 1 ,
, . ,
.
. : ,
, ,
, / .
,
. ,
(  ). ,
.
, MLS (Multiple Listing Servi e).
, , .
(, ). , Redwood Realty, ,
. ,  .
. 1.30 , Redwood Realty. , . ( )
. PK
( Primary Key). FK ( Foreign
Key) . (., , CustAgentList . 1.30).
PK, FK. ( 
FK  .)
. 1.30 ,
.
, . Conta tReason , Redwood Realty
1 , .
Ora le,
, / . 
. .

 Ora le 10g  . 75

PK

PK

PK
FK2

Customers
CustomerID
FirstName
LastName
Address
City
State
Zipcode
HomePhone
CellPhone
WorkPhone

Properties
PropertyID
OwnerID
Address
City
State
Zipcode
Bedrooms
Bathrooms
Stories
SqFt
YearBuilt
Zone
LotSize
Latitude
Longitude

ContactReason
ContactReason
Description

CustAgentList
PK,FK2 CustomerID
PK,FK1 AgentID
PK,FK3 ListingID
PK
ContactDate
FK4

PK
FK3
FK5
FK6

PK

75

PK

ContactReason
BidPrice
CommissionRate

Listings
ListingID
PropertyID
ListingAgentID
SaleStatusID
BeginListDate
EndListDate
AskingPrice

FK2

Agents
AgentID
FirstName
LastName
HireDate
BirthDate
Gender
WorkPhone
CellPhone
HomePhone
Title
TaxID
LicenseID
LicenseDate
LicenseExpire
LicenseStatusID

PK

LicenseStatus
LicenseStatusID
StatusText

SaleStatus
SaleStatusID
SaleStatus

. 1.30. Redwood Realty

: Buy (), Sell () Casual (). Customers 2 500 , Redwood Realty .
. Agents, 29 ,
 , Redwood Realty. Listings 502 , .
, . , (AskingPri e).
Properties  2 000 ,
Listings Customers. , Properties,
 502 . 1 492 .  ,
, ,  , ,
, , , , , !
SaleStatus : For Sale ( -

 Ora le 10g  . 76

76

1.  Ora le 10g

), Pending ( ) Sold ().  ,


, SaleStatus Listings (
SaleStatus). Li enseStatus 16 ,
.
Li ensed ( ) Surrendered ( ).
,
. , CustAgentList  ,
, Customers, Agents Listings. CustAgentList , Customers, Listings, Agents
Conta tReason. 1 018 .
, . , BidPri e ,
Conta tReason Buy.
, ,
. ,
, . . 
. , ,
, , ,
.
, . . ,
, . 6% ,
. . 1.31 , . ,
,
 . 
CustomerAgentList.  .
. 1.32 .
, .
,  , ,
, . ,
 .

 Ora le 10g  . 77

77

. 1.31. 

Coffee Mer hant


Coffee Mer hant  , 
. , Web-,
: .
Coffee Mer hant : - Kona Extra Fan y
(), , , , ,   ( ).
- Coffee Mer hant : States,
Consumers, Employees, OrderLines, Orders, Inventory Countries.
-

 Ora le 10g  . 78

78

1.  Ora le 10g

. 1.32.

. States  50 .
, ,
, , ,
URL  Web- ( , , ). Consumers
( 1 500 -).
Employees  22 : , , , . , Coffee Mer hant,
Orders OrderLines. 500 Orders
, ,
, ( ). OrderLines
2 192 , | .
Inventory , Coffee Mer hant
. 128 Inventory :  .
Des ription , , ,

 Ora le 10g  . 79

79

, , , .. Countries, 256 , . Inventory Countries


- . 
, .
, Web- ,
Coffee Mer hant 22 . ,
, , . , , ,
; Web- .
/ .
 Ora le, Web-. ,  Ora le , .
. 1.33  , Coffee
Mer hant.  ,  .
().  ,
 . , Value  
. , , 
, . -,
. ,
. -, , .. .
. 1.34 Coffee
Mer hant. . : ,
. , .
,
.

Rowing Ventures
Rowing Ventures
.
, Rowing Ventures
 , , ,
, .

 Ora le 10g  . 80

80

1.  Ora le 10g

. 1.33.  Coffee Mer hant

Boat , , ,
. Organization ,
, .
, , , , , , .
Ra eTimes , ,
. Ra e . : Club Eights Men Club Fours
Men. Joining Ra e, Ra eTimes Boat , , ,

 Ora le 10g  . 81

81

. 1.34.

. BoatCrew . Person , , .
 Person , 
, ,
(,  ). Join-

 Ora le 10g  . 82

82

1.  Ora le 10g

ing Ra e, Ra eTimes, Boat, BoatCrew Person 


, , .
. , ..
. , ,
. 
, .
Rowing Ventures . ,
, ,
. , , , , 
.
. Web , . ,
. .
() ,  0 Position BoatCrew.
, , , . ,
8.
( ). ,
, , .  (27 ) .
 ( ) .
. ( !)
. 1.35 , .
,
.
( ).
, , ,
, .
 ,
(. 1.36).
 . 
. PDF-
Web-, .

 Ora le 10g  . 83

83

. 1.35.

Broad loth Clothing


Broad loth Clothing  , , . Broad loth Clothing
,
. ,
.  , . Broad loth Clothing
, (),
 . ,

 Ora le 10g  . 84

84

1.  Ora le 10g

. 1.36. 

, , .
. , , , .
.
. 1.37,
. , .
. , ,

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

 Ora le 10g  . 85

85

. 1.37. Broad loth Clothing

 .
, , .
- (. 1.38).
,
. .
Broad loth Clothing . ,
. ,  . 

 Ora le 10g  . 86

86

1.  Ora le 10g

. 1.38. 

, . , Broad loth Clothing ,


.
.  ,
, , .
 . 1.39. .
, , 
.

 Ora le 10g  . 87

87

. 1.39. 

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


()  ,  .
,
. -

 Ora le 10g  . 88

88

1.  Ora le 10g

. , . , ,
, , , , , 
. , , 
, .
Ora le
, .
, ,
, Developer Suite.
(
). Ora le ,
 Windows.
, Ora le,
. Ora le SQL,
SQL*Plus. -,
Web iSQL*Plus. Developer Suite
 .
, .  Ora le,
, Enterprise Manager.
Enterprise Manager, . ,
,
.

 Ora le 10g  . 89







()
()
Developer Suite

Enterprise Manager

 (Entity-Relationship Diagram  ERD)




 

 











(Unied Modeling Language  UML)

89

 Ora le 10g  . 90

90

1.  Ora le 10g

1.
, .
2. ,
.
3. , ,
.
4. Developer Suite ,
, .
5. Enterprise Manager
.

1. ,
, _____.
2. ,
_____ .
3. - , Ora le, _____,
.
4. , , _____ .
5. , _____,
CustomerID
Order , 
Customer.

1. ,  ?
) SQL*Plus
) Enterprise Manager
) Forms Builder
) Reports Builder
) Appli ation Server

 Ora le 10g  . 91

91

2. :

ListingProperty(ListingID, StartDate, AgentID,


PropertyID, Bedrooms, Bathrooms, Size)
(Bedrooms), (Bathrooms) (Size)
PropertyID. :
) .
) .
) .
) .
) 
3.  _____.
) .
) .
) .
)  .
) .
4. Ora le 
) Add/Remove programs ( ) Windows.
) Ora le .
) Ora le Windows.
) Ora le.
) Ora le.
5. ,
,
) .
) .
) .
) .
) .

 Ora le 10g  . 92

92

1.  Ora le 10g

1. Readwood Realty

,
. .
1. .
, Properties, Agents, Listings, Customers
CustomerAgentList, .
2. , (, Conta tReason) - ?
  , , .
, , , .
3.  , , ,
. .
, .
, 
 . ,
.
2. Coffee Mer hant

, .
.
1. . : Orders, OrderLines,
Inventory Consumers.
2. States,
, . . 
,
.

 Ora le 10g  . 93

93

3. .
, .
, 
. . ,
.
3. Rowing Ventures

 , , , .
Web-, , .
. .
, ,
.
4. Broad loth Clothing

 , , , . , . 
,  . ,
. .

.

 Ora le 10g  . 94

 Ora le 10g  . 95

SQL SQL*Plus

. . .

 SQL-
 SQL-
 SQL, Ora le
SQL-
SQL- ,

SQL*Plus 

(Stru tured Query Language  SQL)


, . SQL ..
(E.F. Codd), 1970- System R SQL IBM. Ora le.
SQL  ANSI (Ameri an National Standards Institute  ) ISO (International Standards
Organization  ). ANSI SQL 1986 . 1989 ANSI ISO SQL89 (
SQL1). ANSI SQL92

 Ora le 10g  . 96

96

2. SQL SQL*Plus

(SQL2). ANSI, SQL99 (SQL3),


- .
SQL 
, Ora le, DB2 (IBM), SQL Server (Mi rosoft) MySQL.
SQL  . , , , , , . SQL, 
,
. . , SQL99,
Ora le. ,
SQL , Ora le,
. SQL
, , , (, , ).
, SQL,  .
Ora le ANSI SQL,
SQL, SQL*Plus. SQL*Plus SQL,
Ora le.  SQL*Plus , , ,  ,
,  SQL.
, , Ora le,
SQL*Plus.

SQL
SQL: , DDL- (Data
Denition Language  ), DML- (Data Manipulation
Language  ), TC- (Transa tion Control 
) DCL- (Data Control Language  ).

.
SELECT ( SQL  ,
SQL , ).

 Ora le 10g  . 97

97

DDL-

SQL, , ( ), .
: CREATE, ALTER, DROP, RENAME TRUNCATE. ;  ; (,
); ; .
DML-

,  ,
. DML- INSERT,
UPDATE DELETE .
TC-

, .
COMMIT , . ROLLBACK 
, . SAVEPOINT, , ,
.
DCL-

, , Ora le,
.
GRANT,  REVOKE. , ,

.

 SQL
SQL , .  (, CREATE) , , . SQL,
 SQL*Plus, .
SQL <Enter>. -

 Ora le 10g  . 98

98

2. SQL SQL*Plus

. 2.1.

SQL, SQL*Plus for Windows

,
.
. , ,
SQL*Plus, .
SQL , SQL
,  SQL. . 2.1 SQL SELECT,
 SQL*Plus.
, SQL, ( SQL*Plus ;  ).
(<>, <Tab>
<Enter>). SQL . SQL (, SQL) .
SQL ( , , ) (
). , Inventory.Pri e. ()
(, ).

 Ora le 10g  . 99

99

, , , .
SQL.
<Enter> SQL*Plus SQL.
, .


Ora le .
, SQL SQL*Plus
, , , , . . 2.2 , CREATE TABLE. Conta tReason,
, .
, . SQL*Plus
, 2, (ORA-00922) missing or invalid option ( ).
Ora le . , ,
, . ORA Ora le , (
) . ORA, Ora le Corporation,  ,
.
, . . 2.2 ,
. , ,
, Conta tReason. missing or invalid
option , .
Ora le OTN (Ora le Te hnology Network) Web-. http://ora- ode. om
ORA . ,
, ,
.
 ORA,
, .
1. Web-, http://ora- ode. om <Enter>. Ora le Database Error Code.
2.  3,
.

 Ora le 10g  . 100

100

2. SQL SQL*Plus

. 2.2.

SQL

3. ORA-00922 (
).
4. . -, Ora le , . SQL, ,
2 . .
5. Web-.


SQL, SQL99 SQL. , ( ) SQL,
. : () , , SQL-
 (Call-Level Interfa e  CLI).

, SQL ,
SQL*Plus iSQL*Plus. SQL*Plus , , . ,
. , SQL . SQL , SQL*Plus Ora le,
,  ,
. , SQL*Plus . .

 Ora le 10g  . 101

. 2.3.

101

 Ora le

SQL ,
.
SQL , , .
 . , , SQL ,  .
SQL.
, (linker). , ,
.
SQL (, C++ COBOL). Ora le
SQL
. SQL ,
. SQL
, .
 SQL 
, SQL -

 Ora le 10g  . 102

102

2. SQL SQL*Plus

. ( ).

Ora le:  ;
SQL*Plus Ora le; Web- Ora le
iSQL*Plus.
SQL SQL*Plus ,
, . SQL  , . SQL*Plus 
Ora le , SQL.
SQL*Plus  SQL*Plus. SQL*Plus  SQL Ora le,
 , , .  SQL*Plus.

Ora le SQL*Plus Windows. , SQL*Plus DOS (Disk Operating
System  ). -, ,
SQL*Plus for Windows. -,
iSQL*Plus,  Ora le,
Web. .
, , .

SQL*Plus DOS
Ora le Windows
Windows Ora le , DOS SQL*Plus.
SQL*Plus, Windows (, / ..),
.

Ora le
SQL*Plus DOS, Ora le
SQL. , DOS SQL*Plus
Windows, .

 Ora le 10g  . 103

SQL*Plus DOS

. 2.4.

103

SQL*Plus DOS

1. Start () , Run (),


ommand Open () OK.
DOS.
2.
.

sqlplus <Enter>. -

3. <Enter>.
.
4. ( ) <Enter>.
, SQL>.
5. help des ribe <Enter>,  SQL*Plus (. 2.4).
6. Ora le,
DOS C:\>.

exit <Enter>.

7. DOS,

exit <Enter>.

SQL
SQL*Plus  , Ora le, SQL. Dual, Ora le . -  Ch02SaleStatus.sql
. , .  My Do uments ( ),

 Ora le 10g  . 104

104

2. SQL SQL*Plus

. C (C:\)
Temp (C:\Temp\).
SQL SQL*Plus, DOS.
1. Start () , Run (),
ommand Open () OK.
DOS.
2. SQL*Plus: sqlplus <_>/<> <Enter>, <_> , <>  , . (,
!)
3. SQL*Plus CLEAR SCREEN ( )
<Enter>, .
4. SQL> START, <>,  (Ch02SaleStatus.sql),
Ch02SaleStatus.sql ( ).
<Enter>. , ,  (
) C,
<Enter>:

SQL> START C:\Ch02SaleStatus.sql


5. Ora le Table

reated ( ).

6. DOS  .

 SQL
SQL (, ), , SQL> edit.
Windows Notepad (), ,
.
, SQL SQL*Plus,
DOS, .
1. edit <Enter>. Windows  DOS.
CREATE TABLE,  Ch02SaleStatus.sql.
 afiedt.buf (
 Ora le).
2. , CREATE.
3. --Create a new table <Enter>. , , , Ora le .

 Ora le 10g  . 105

SQL*Plus for Windows

4. File () ,
Yes, .

Exit

105

()

5. SQL ( )
Wrote file afiedt.buf.
6.  SQL, <Enter>. Ora le (ORA00955), , SQL
.
7. exit <Enter>, SQL*Plus.
exit <Enter>, DOS.

SQL*Plus for Windows


SQL*Plus SQL*Plus for Windows, Windows
. , SQL*Plus for Windows
. ,  ,  DOS- Windows-,  .
Ora le SQL*Plus for Windows
SQL> , .
SQL Ora le.
,  .
, SQL,

.
(, -

).
, 

.
SQL, .

Ora le
Ora le SQL*Plus for Windows, .
1. Start (), All Programs () (Windows
XP), Ora le-OraDb10g_home1, Appli ationDevelopment
SQLPlus. LogOn.

 Ora le 10g  . 106

106

2. SQL SQL*Plus

. SQL*Plus, SQLPlus, Send To (), Desktop ( reate


short ut) ( ( )).

2. User Name <Tab>.


Password. , <Tab> ,
, Host String.
. <Enter> , Ora le Log On .
<Enter>.

3. OK, Ora le.


SQL*Plus .
, SQL*Plus
Ora le, SQL*Plus, , . , Ora le (, Personal
Ora le), .
SQL>, , SQL*Plus
. ( SQL*Plus  .)
, ORA-01017: invalid username/password; logon denied, , .
. ORA-12154, ORA12154: TNS: ould not resolve the onne t identifier spe ified,  .
SQL>,
Ora le.
SQL*Plus. <Enter>
, .
SQL*Plus .

SQL
Ora le  Windows SQL*Plus
SQL SQL*Plus.
SaleStatus , .
1. SQL SQL*Plus des ribe
salestatus, -

 Ora le 10g  . 107

SQL*Plus for Windows

107

, <Enter>. Ora le 
salestatus. , ( ).
2. - , INSERT. SQL>
, <Enter> SaleStatus.
, <Enter>
.

INSERT INTO SaleStatus


VALUES (101, 'For Sale');
3. <Enter>, SQL
SQL*Plus, Ora le . Ora le ,
, <Enter>,
( - ). 1 row reated, ,
.
4. SQL, <Enter> , . (
. ,
.)

INSERT INTO SaleStatus VALUES (102, 'Pending');


INSERT INTO SaleStatus VALUES (103, 'Sold');
, SQL*Plus
Ora le  1 row reated (. 2.5).
5. CLEAR SCREEN <Enter>,
SQL*Plus ( SQL*Plus ).

 SQL
, SQL*Plus, , edit SQL> (
SQL DOS).
, , , . SQL ,
. ,
SQL SQL*Plus , ( , Word, ).
SQL,  Windows <Ctrl+A>, 
, Copy (E
)),
SQL*Plus, . , -

 Ora le 10g  . 108

108

2. SQL SQL*Plus

. 2.5.

SQL SQL*Plus

, SQL*Plus. ,  ,
SQL*Plus . Edit SQL*Plus . SQL ( ,
) . , (
) SQL , . , Ora le, SQL*Plus start
 ( ). .
SQL ,
SQL*Plus, .
1. SQL*Plus . <Enter>
(SaleStatus)
.

INSERT INTO SaleStatus


VALUES (101, 'Unknown');
Ora le , ,

 Ora le 10g  . 109

SQL*Plus for Windows

109

. 2.6.

2.
. SQL, , (. 2.6).
3.

Edit

Copy.

. <Ctrl+C>,
.

4. ( Start (), AllPrograms


(), A essories () Notepad ())
.
5. , Edit (), Paste () ( <Ctrl+V>).
SQL .
.
6. :
.

101 104  

7. SQL*Plus:
<Ctrl+A>, , <Ctrl+C>, , SQL*Plus, <Ctrl+V>,
SQL*Plus, <Enter>,
. Ora le
 1 row reated.
8. , File (), Save
(). , 
2, Ch2<>SaleStatus.sql File name ()
( <> ), Save as type ( ),
All Files (), Save.
9. .
SQL*Plus, .

 Ora le 10g  . 110

110

2. SQL SQL*Plus

SQL*Plus
SQL*Plus .

File

Exit

Exit.

SQL>.

SQL*Plus Ora le , .. 
Ora le.

iSQL*Plus
Ora le iSQL*Plus 
Web SQL*Plus. iSQL*Plus URL
http://<_>.<_>:<>/isqlplus/.
<_>  , <> 5560. Ora le ,
iSQL*Plus URL http://127.0.0.1:5560/
isqlplus/.
URL , ,
. . 2.7 iSQL*Plus
PURDUE. ,
iSQL*Plus , Ora le ( PURDUE). , SQL*Plus,
, ()  ,
Login Ora le.
  .

Ora le
, Ora le iSQL*Plus  SQL*Plus Web.
Ora le iSQL*Plus SQL,
.
1. Web- URL Ora le.
URL  . Web-
iSQL*Plus.
2.
.

Username

, <Tab>

 Ora le 10g  . 111

iSQL*Plus

111

. 2.7.

Ora le iSQL*Plus

3. Conne t Identier, <Tab>


 . . 2.7 Web- iSQL*Plus (Ch02) .
4. Login, Ora le. , iSQL*Plus Web-
Workspa e.
5. <Enter>
.

SQL, PL/SQL and SQL*Plus statements,

SQL
SQL SQL*Plus . : , ,
Exe ute.
SQL , .

 Ora le 10g  . 112

112

2. SQL SQL*Plus

. 2.8.

SQL

1. SQL:

SELECT *
FROM SaleStatus;
2. Exe ute, ,
.  : ,
SaleStatus (. 2.8).
, , . , ,
iSQL*Plus Next Page ,
. iSQL*Plus
,  SQL, ,
, ( History); , . Help
, .

 Ora le 10g  . 113

SQL

113

 SQL
SQL SQL*Plus ,
: , ,
, Exe ute, 
. , Save S ript,
,  .
SQL iSQL*Plus, .
1. SaleStatus .
2. <Enter>, ,

ORDER BY SaleStatus DESC


3. Exe ute, .
Ora le SaleStatus .
4. : Save S ript, ,  , Save.
5. , 2,
Ch2<>ReverseSaleStatus.sql, Save as type All Files Save. iSQL*Plus  .
, Load S ript,
Browse , ( ).
6.

Clear,

Workspa e.

iSQL*Plus
iSQL*Plus, .
1. Logout ( ), Ora le. Ora le
, , .
2. Web-.

SQL
,  SQL*Plus, ,

 Ora le 10g  . 114

114

2. SQL SQL*Plus

. 2.9.

DROP TABLE

( ), ,
SQL. SQL, Redwood Realty
SQL
. SQL, SQL, .
Redwood Realty,
SQL.
, 
, .
1. - (, 2) 
Ch02Agents.sql (,
C:\Temp\ C:\My Do uments\SQL\).
2. , SQL*Plus .
.
3. SQL> START
<>   .

<>\Ch02Agents.sql,

4. <Enter>, SQL  . Ora le (. 2.9)


DROP TABLE. , ,
.
5. SQL*Plus CLEAR SCREEN ( ) <Enter>, SQL*Plus .
6.
Ora le.

exit <Enter>, SQL*Plus

 , Agents Li enseStatus, . Agents 29 ,

 Ora le 10g  . 115

SQL

115

, Redwood Realty. Li enseStatus 16 


(Li ensed (), Expired ( ),
Revoked () ..).

SQL
, SQL
.  SQL SELECT.  .
, ( ) , , ,
.
 SELECT. ,  , ;  SQL. ,
(  ).
, ,
([ ) ,  ({ })
, ,
.

SELECT [DISTINCT|ALL {*| [ olumn_expression [AS new_name [,...}


FROM table_name
[JOIN table_name [alias ON (table_ olumn <operator>table_ olumn)...
WHERE sear h_ ondition
[GROUP BY olumn_list [HAVING sear h_ ondition
[ORDER BY olumn_list
, SELECT .
. SELECT
, . ,
Agents ( ):

SELECT * FROM Agents;


, ,
FROM  Ora le Agents.
15 29 . ,
SELECT:

SELECT firstname, lastname, gender


FROM agents
WHERE gender = 'F'
ORDER BY lastname, firstname;
SELECT , Agents
,  . , WHERE -

 Ora le 10g  . 116

116

2. SQL SQL*Plus

 -
(gender = 'F'). ORDER BY
, , . SQL SELECT
iSQL*Plus. SQL*Plus,
, exit SQL>.
SELECT
Agents, .
1. Web-, iSQL*Plus. (
Login.)
2. Workspa e .
M ( ).
, iSQL*Plus .

SELECT firstname, lastname, gender, li ensestatusid


FROM agents
WHERE gender = 'M'
ORDER BY li ensestatusid, lastname
3. Exe ute, Ora le. 15 (. 2.10).
4.

Logout.

( .)

5. .
3.

SELECT;

DLL-
SQL, DDL (Data Denition Language  ), CREATE TABLE, ALTER TABLE
DROP TABLE. DDL-,
, .

CREATE TABLE.
SQL : , .
 ,
( Agents, 
Ch02Agents.sql). , -

 Ora le 10g  . 117

SQL

117

SELECT,

,
Agents
. 2.10.

SELECT

(SELECT). , 6, .
SELECT,
, ,
. CREATE
TABLE .
CREATE TABLE, . ( CREATE TABLE SQL Referen e Ora le Corporation.)

CREATE TABLE table_name


( olumn_name type [NOT NULL
[CONSTRAINT onstraint_definition DEFAULT expression
[, olumn_name type [NOT NULL
[CONSTRAINT onstraint_definition DEFAULT expression...
)
[PRIMARY KEY ( olumn_name [, olumn_name ...)
[FOREIGN KEY ( olumn_name [, olumn_name ...)
REFERENCES foreign_table ( olumn_name [, olumn_name ...)

 Ora le 10g  . 118

118

2. SQL SQL*Plus

CREATE TABLE Redwood


Realty Conta tReason, , .
: Buy (), Casual (), Sell (). Buy
, ; Casual 
  ;
Sell , Redwood Realty . , CREATE TABLE Conta tReason
Conta tReason (,
).

CREATE TABLE Conta tReason


(Conta tReason NVARCHAR2(15) NOT NULL,
Des ription NVARCHAR2(50),
CONSTRAINT pk_Conta tReason PRIMARY KEY (Conta tReason)
);
.
.
. ( onstraint)  ,
. , , Conta tReason, . , ( Buy, Casual Sell 
).
.
Conta tReason NVARCHAR2  15 . (
 , .) NOT NULL , . , Des ription,
50 .


, . ,  : , ,
, . ( )
.
,
. ? !
SQL ALTER TABLE. ,
Employee MobilePhone.

 Ora le 10g  . 119

SQL

119

ALTER TABLE Employee


ADD MobilePhone VARCHAR2(14);
Ora le MobilePhone
NULL. .  . ALTER
TABLE MODIFY DROP COLUMN. ,
SQL YearStarted.

ALTER TABLE Employee


DROP COLUMN YearStarted;
DATE.

ALTER TABLE Employee


Modify YearStarted DATE DEFAULT SYSDATE;
, DEFAULT ,
YearStarted  ,  SYSDATE.
. , , CheapWebHost,
URL Web-. , , . ,
, .
, DROP TABLE,
:

DROP TABLE table_name [CASCADE CONSTRAINTS [PURGE


CASCADE CONSTRAINTS
, , ( ,
; . ), .
PURGE , .
. ,
CheapWebHost .

DROP TABLE CheapWebHost CASCADE CONSTRAINTS PURGE;

DML-
  .
SQL- (Data Manipulation Language 
DML), , 

 Ora le 10g  . 120

120

2. SQL SQL*Plus

. ,  , ,
 .
SQL INSERT, UPDATE DELETE.

INSERT
( ) . INSERT :

INSERT INTO table_name [( olumn_name [, olumn_name ...)


VALUES (value [, value...)

 :

INSERT INTO table_name


VALUES (value, value, value, ...)
,   , .
, INSERT VALUES  .
, ,
NULL. (NULL  , , .)
, INSERT
Li enseStatus, , .

INSERT INTO Li enseStatus


VALUES (1017, 'Rea tivated');
 .

INSERT ,

, ,
.

NULL .

, , , VALUES,
. , , Agents
, , ,  AgentID,  .
INSERT ,
VALUE . SQL*Plus.

 Ora le 10g  . 121

SQL

. 2.11.

121


, .

Agents,

1. SQL*Plus Ora le .
2. SQL> :

INSERT INTO Agents (AgentID, FirstName, LastName)


VALUES (10001, 'Melinda', 'Whitmore');
3. <Enter>, . SQL*Plus Ora le. Ora le 1 row reated, , (. 2.11).
4. , , 
. SQL> :

SELECT AgentID, FirstName, LastName


FROM Agents
WHERE AgentID < 10200;
5. <Enter>, . Ora le , ,
. SQL*Plus  .
  INSERT, SELECT
.  VALUES
SELECT:

INSERT INTO table_name ( olumn_name1, olumn_name2, ...)


SELECT olumn1, olumn2, ...
FROM table_name [WHERE sear h_ ondition
, ,
SELECT
( ).

 Ora le 10g  . 122

122

2. SQL SQL*Plus

WHERE  . ,
Agents, ,
, Agents:
INSERT INTO Agents (AgentID, LastName, FirstName)
SELECT NewAgentNumber, AgentLastName, AgentFirstName
FROM NewlyLi ensedAgents;
, Agent ,
NewAgentNumber, , , ,
 , Agents. ,
.

,  - - .
, , . UPDATE, 
Ora le. UPDATE :

UPDATE table_name
SET olumn_name = expression [, olumn_name = expression, ...
[WHERE sear h_ ondition
, SET , WHERE  .
  . WHERE, ,
.
,  Melinda Whitmore (,
).
UPDATE, .
, .
1. SQL*Plus
.

CLEAR SCREEN,

2. ( <Enter>, ,
<Enter> ).  , 
. , .

 Ora le 10g  . 123

SQL

123

. 2.12.

Agents

( COMMIT .)

UPDATE Agents
SET CellPhone = '(707) 555-8099', Li enseStatusID = 1001
WHERE FirstName = 'Melinda' AND LastName = 'Whitmore';
COMMIT;

3. <Ctrl+A>
, <Ctrl+C>, .
4. SQL*Plus, <Ctrl+V>, SQL*Plus, <Enter>
COMMIT. SQL*Plus , , 3 4. Ora le
: 1 row updated Commit omplete (. 2.12).
CellPhone Li enseStatusID MelindaWhitmore . ( ,
SELECT.)
5. , SQL, , . SQL*Plus .

SQL DELETE.
, . ,
TRUNCATE.  DELETE TRUNCATE ,
, DELETE,
ROLLBACK, .
TRUNCATE, .

 Ora le 10g  . 124

124

2. SQL SQL*Plus

, TRUNCATE , , , , .. DELETE
:

DELETE FROM table_name


[WHERE sear h_ ondition
DELETE FROM , , . WHERE ,
SELECT UPDATE: , . WHERE (
), Ora le . ,
DELETE, TRUNCATE,
.
, DROP TABLE ( ).
Agents, .
1. SQL*Plus, ,
. CLEAR SCREEN <Enter>,
SQL .
2. SQL> ,
Agents (Melinda Whitmore), <Enter>
:

DELETE FROM Agents


WHERE AgentID = 10001;
Ora le 1

row deleted.

3. , MelindaWhitmore ,
 . SQL>
(. 2.13):

SELECT FirstName, LastName


FROM Agents
ORDER BY LastName DESC;
4. , MelindaWhitmore . (,
,
. SQL*Plus, LINESIZE. LINESIZE .)
SQL*Plus. SQL*Plus, ,
, .

 Ora le 10g  . 125

SQL

125


. 2.13.

TC-
TC- SQL : TC 
Transa tion Control ( ). SQL, . ,
.
 SQL,
, .  .
UPDATE , UPDATE .
- UPDATE , 

. ,
, UPDATE 
, .

 Ora le 10g  . 126

126

2. SQL SQL*Plus

COMMIT;
INSERT INTO ...

INSERT INTO ...


DELETE FROM ...
UPDATE ...
COMMIT;

. 2.14.

DDL-, , CREATE TABLE , Ora le ,


, . DML- (INSERT, UPDATE DELETE) , 
SQL*Plus AUTOCOMMIT.  AUTOCOMMIT (SET AUTOCOMMIT ON)
, DML-, .
SQL,
,  , COMMIT. SQL,
, ROLLBACK. , ROLLBACK (
COMMIT), . , Ora le  , . . 2.14
, COMMIT.
, ( ),
 COMMIT,
.
Agents. Ora le
DELETE, .
, ,
Ora le   . ,
DELETE
Agents.
, , COMMIT, .

 Ora le 10g  . 127

SQL

127

1. ROLLBACK, ; ( ) <Enter>. Ora le


Rollba k omplete.
2. SQL> :

SELECT LastName, CellPhone


FROM Agents
ORDER BY LastName DESC;
3. <Enter>, ,
. ,
( ) . , DELETE 
COMMIT.
Agents , , .
, .
1. SQL> .

DELETE FROM Agents


WHERE AgentID = 10001;
2. ,

COMMIT;
Ora le Commit

omplete.

3. Exit <Enter>, Ora le


SQL*Plus.

,
, . ,
.
SAVEPOINT, :

SAVEPOINT savepoint_name

savepoint_name  . ,
,

SAVEPOINT AfterFinan ialUpdates;


DML- SAVEPOINT,
, AfterFinan ialUpdates,

ROLLBACK TO SAVEPOINT AfterFinan ialUpdates;

 Ora le 10g  . 128

128

2. SQL SQL*Plus

DCL-
Ora le .  (, ).
DCL- (Data Control Language  ),
SQL,
.
( ), , . ,
,
 
.
GRANT,  REVOKE.
, , . ,
.
: .
Ora le ( , ..).
(,
).


GRANT, .

GRANT privilege [, privilege [, privilege ... | ALL PRIVILEGES


ON obje t
TO user [IDENTIFIED BY password [, user2 [, user3... | PUBLIC
[WITH GRANT OPTION
, ,
. , , Pine Agents. , Elm
Agents, , Agents.
Pine SQL:

GRANT SELECT ON Pine.Agents TO Elm;

 Ora le 10g  . 129

SQL

129

, , ( ) .
REVOKE :

REVOKE privilege[, privilege[, privilege ... | ALL PRIVILEGES


ON obje t
FROM user [, user2 [, user3 ... | PUBLIC
Elm Agents, Pine, Pine
SQL:

REVOKE SELECT ON Pine.Agents FROM Elm


,
: SELECT, INSERT, UPDATE, DELETE EXECUTE.
: CREATE SESSION, CREATE TABLE, CREATE USER, CREATE VIEW, CREATE SYNONYM .. ,
, SELECT.
Ora le, SQL*Plus, .
, , .
1. , SQL*Plus
.
2. ( ,
):

SELECT *
FROM user_sys_privs;
3. <Enter>, . Ora le
,
, , .
4. exit <Enter>, Ora le
SQL*Plus.
,
( ).
, , .
. 2.15 ,
Woody. , .
( ) 12  .

 Ora le 10g  . 130

130

2. SQL SQL*Plus

. 2.15.

SQL*Plus
SQL*Plus Ora le SQL*Plus, SQL  , ,  , ,
 80 .
(, CLEAR SCREEN). SQL*Plus (
iSQL*Plus) Ora le. SQL*Plus, , START,
 , SQL.
SQL*Plus,
. , SQL*Plus
, , SQL*Plus
 (DESCRIBE DESC), SQL . , SQL*Plus iSQL*Plus. , , ,
SQL*Plus.
. , SQL*Plus <Enter> ,
SQL, .
, , , ..
   ,  <Enter>.
SQL*Plus, .
1. , SQL*Plus ,
.

 Ora le 10g  . 131

SQL*Plus

131

SQL*Plus
SQL*Plus
. 2.16.

SQL*Plus

CONNECT

CLEAR SCREEN, .
3. HELP INDEX ( )
2.

, 50 SQL*Plus.
4.

HELP CONNECT (. 2.16).

HELP CONNECT SQL*Plus  SQL*Plus.


SQL, CREATE TABLE, SQL*Plus , ,
. , ,
SQL, OTN (Ora le Te hni al Network).
, SQL*Plus, SQL*Plus HELP.


SQL*Plus DESCRIBE , . ,  
(  ).
 .

 Ora le 10g  . 132

132

2. SQL SQL*Plus

. 2.17.

, .
1. SQL*Plus

DESCRIBE Agents
Ora le 15

Agents.

DESCRIBE SaleStatus.
3. DESCRIBE Li enseStatus (. 2.17). SQL*Plus.
2.


SQL*Plus ( SQL SQL*Plus,
). , Ora le, ,
. Windows (, <Print S reen>, 
SQL*Plus), ,
Ora le .
SQL*Plus SPOOL,  ,
SQL*Plus ( , , ).

 Ora le 10g  . 133

SQL*Plus

133

iSQL*Plus SPOOL . ( SPOOL


iSQL*Plus, .) SPOOL   , .. , .
HELP SPOOL :

SPOOL [file_name[.ext [CREATE | REPLACE | APPEND | OFF | OUT


, ( OFF OUT),
(, SPO SPOOL).  , ,
SPOOL  ( ).
, , , . , SQL*Plus,
SPOOL OFF.
, .


 , ,
SQL*Plus COLUMN. ,
:

COLUMN [{ olumn_name | expression}[option...


CLEAR, FORMAT, HEADING, JUSTIFY WRAP. COLUMN ,
, ,  ,
, . ,
SQL*Plus AgentID
SQL*Plus.

COLUMN AgentID FORMAT 99999 HEADING 'Agent ID'


,   , ( )
Agent ID. SQL*Plus
. , SQL*Plus,
CLEAR COLUMNS.  . , COLUMN  .
 SQL*Plus SPOOL COLUMN.
SQL*Plus  
, .
1. SQL*Plus .
2. SQL*Plus ( <Enter> ):

 Ora le 10g  . 134

134

2. SQL SQL*Plus

. 2.18.

CLEAR SCREEN
CLEAR COLUMNS
COLUMN AgentID FORMAT 99999 HEADING 'Agent ID'
COLUMN FirstName FORMAT A15 HEADING 'First Name'
COLUMN LastName FORMAT A15 HEADING 'Last Name'
SELECT AgentID, FirstName, LastName
FROM Agents;
3.  
.

Ch2<>FormatColumns.sql

4. SQL*Plus. SQL*Plus COLUMN, SQL SELECT


( <Enter> 
).
5. <Enter>, SELECT 
(. 2.18).

 A15,  FORMAT, ,
-,

 Ora le 10g  . 135

SQL*Plus

135

15 . .  99999 , , SQL*Plus .
, 999, ####,
, , .
-. ,
:

COLUMN AgentID FORMAT 99,999 HEADING 'Agent ID'


  (, 10,041).
,
. , , . , ,
11  .
, , 14 
SQL*Plus PAGESIZE. PAGESIZE , .
,  SQL*Plus SET PAGESIZE
<value>, <value>  . PAGESIZE
50 000.
  SQL, .
1.  FormatColumns ,
CLEAR COLUMNS <Enter>,
. CLEAR SCREEN CLEAR COLUMNS

SET PAGESIZE 45
2. File ()
 SQL*Plus.

Save

(), -

3. SQL*Plus.
4. SQL*Plus <Enter>,
(. 2.19).

SELECT

5. SQL*Plus ,
PAGESIZE :

SET PAGESIZE 14
CLEAR SCREEN
6. , SQL*Plus.
 SQL*Plus,
. .

 Ora le 10g  . 136

136

2. SQL SQL*Plus

. 2.19.

PAGESIZE

SQL*Plus
SQL*Plus (, PAGESIZE) , Ora le SQL*Plus.
, PAGESIZE, 50, LINESIZE ( ), 75. SQL*Plus SQL*Plus .
,  SQL*Plus,
.
1. SQL*Plus ( iSQL*Plus) Options
Environment. SQL*Plus Environment.
2. Set Options , pagesize. ( ) Custom Value.
, 14, ( ,
).

 Ora le 10g  . 137

SQL*Plus

137

PAGESIZE
. 2.20.

SQL*Plus

3. 14, , 45
(. 2.20).
4. sqlprompt.
Custom. , Value
SQL>.
.
5. OK, . Environment .
6. SQL*Plus SHOW PAGESIZE <Enter>, PAGESIZE.
7. LINESIZE? ,
SHOW LINESIZE <Enter>.
, SQL*Plus,
Set Options Environment. ,
, SHOW ALL.


, SQL SQL*Plus .
, SQL*Plus ,
( , ,
SQL*Plus, ). SQL SQL*Plus
 . , ,
.
SQL*Plus,

 Ora le 10g  . 138

138

2. SQL SQL*Plus

START ( )
. 2.21.

 SQL*Plus

 : START . :
 .
 , .
1. , 
matColumns.sql, .

Ch2<> For-

2. SQL*Plus :

START <>\Ch2<>FormatColumns
<>  , 
; <>  , . :

START C:\My Do uments\Ch2ElmFormatColumns


3. <Enter>, START. SQL*Plus
 .
SQL*Plus 3 29   (. 2.21).

. , CLEAR SCREEN,

START,  . .

 Ora le 10g  . 139

SQL*Plus

,
.

139

START  


SQL*Plus ,
SQL. , , SQL.
 SQL. , ,  , , ,
(&). , , SQL*Plus
SELECT:

SELECT AgentID, FirstName, LastName


FROM Agents
WHERE LastName = '&lastnameplease';
SQL*Plus , lastnameplease ( ) Ora le. ,
 .  :  ,  
.  , .
 ,
.
1. SQL*Plus CLEAR
ter>), .

SCREEN ( <En-

2. , SQL,  Ch2<>SubVar.sql:

SELECT AgentID, FirstName, LastName, BirthDate


FROM Agents
WHERE LastName = '&lastnameplease';
3. SQL*Plus .

<>\Ch2<>SubVar
<>  , 
; <>  , .
4. <Enter>,  . SQL*Plus
.
5. Carling ( , ) <Enter>. Ora le
Agent Carling.
6. 3 4.
7. <Enter>

value for lastnameplease Fernandez

 Ora le 10g  . 140

140

2. SQL SQL*Plus

. 2.22.

<Enter>. SQL*Plus Ora le. Ora le


(. 2.22).
8. SQL*Plus:

Exit <Enter>.

9. .

SQL*Plus
SQL*Plus, (). ,
SQL*Plus SQL*Plus, HELP, . SQL*Plus, HELP INDEX.
, (
, , ).
HOST, iSQL*Plus, , SQL*Plus. HOST SQL>
$. .
Windows, DOS . SQL*Plus, Exit
<Enter>. ,  SQL
, HOST, DOS
DIR *.SQL. , DOS, HOST+. , ,
SQL*Plus:

HOST DEL C:\Do s\myfile.sql


SQL*Plus
(DEL)  myfile.sql C:\Do s\, DOS, -

 Ora le 10g  . 141

SQL*Plus

141

SQL*Plus. 
. , , .
TERMOUT PAGESIZE  ,
SQL SQL*Plus. TERMOUT ,
 , 1 row reated.
SET TERMOUT OFF ( SET TERM OFF), ,
 ( ).
SET TERM ON, .
, TERMOUT, , TERMOUT . SQL*Plus FEEDBACK, ,   1 row
reated table dropped. SET FEEDBACK OFF  , SET FEEDBACK ON  . , FEEDBACK
OFF SQL*Plus, , (  ). SET PAGESIZE 0
, ,
 .
 . ,  (--).
(/*),
(*/). ,
REM. , /* */, , .
PROMPT .
 , ,  . ,
, PROMT.

PROMPT Su essfully ran s ript to delete files.


,   SQL SQL*Plus, . ,
START,   .
SQL*Plus  .
 . ,

 Ora le 10g  . 142

142

2. SQL SQL*Plus

, , , (). , Ora le ,
user_tables.
 , 45 , ,
, . (
, DESCRIBE user_tables.)
, , , 
 ,
.
, (
) . , .
 , DESCRIBE
( , ). ,
  ,
. SQL*Plus
, , , . ,
.  ,  ,
SQL*Plus .
,  , .
1. ,  . ,
C:\Temp
C:\.
2. . C:\Do s\ ,  (
; . 2.23):

REM Des ribe my tables


CLEAR SCREEN
-- 
SET FEEDBACK OFF
SET PAGESIZE 0
SPOOL C:\Do s\Des ribe.sql
SELECT 'DESCRIBE' || table_name || ';'
FROM user_tables
WHERE table_name NOT LIKE 'BIN%'
ORDER BY 1;
SPOOL OFF
PROMPT
C:\Do s\Des ribe.sql

 Ora le 10g  . 143

SQL*Plus

143

C:\Docs\

( )

. 2.23.

HOST del C:\Do s\Des ribe.sql


SET PAGESIZE 14
SET FEEDBACK ON
3. File (), Save ().
Save as type ( ), All Files ( ), ,
 . Ch2<>GenS ript.sql
( <> ) File name ( )
Save (). , SQL*Plus
.
4. SQL*Plus , . SQL :

START <>Ch2<>GenS ript

<>  , <>  .

5. <Enter>,  .
, . 2.24.
6. , ,
,  . 4.
7. SQL*Plus.

(Stru tured Query Language  SQL): , (DDL), (DML), (TC)


(DCL). SQL 
. DML INSERT, UPDATE DELETE  . -

 Ora le 10g  . 144

144

2. SQL SQL*Plus

. 2.24.

 

(,
). 
,  DML. , ( )
.
/ Ora le ,
Windows SQL*Plus, Web- iSQL*Plus. ,
SQL*Plus.
 SQL Ora le
SQL*Plus.  , ,  ,  SQL*Plus.
SQL, HELP INDEX SQL.
SQL*Plus, HELP . SQL SQL*Plus
SQL. ,
.
, .

,
.

 Ora le 10g  . 145

145

SQL*Plus

NULL




SQL*Plus




?
1. (Stru tured Query Language  SQL) 
.
2. SQL (Data Manipulation Language 
DML) , .
3. Ora le , SQL*Plus.
4.
FROM.

DELETE

5. , , ,
 ROLLBACK.


1. SQL _____, .
2.

DESCRIBE _____, SQL.

3. , _____.

 Ora le 10g  . 146

146

2. SQL SQL*Plus

4. ,
SERT, DELETE UPDATE, SQL _____.

IN-

5. SQL*Plus _____, ,
 SQL*Plus.

1.  , SQL*Plus?
) PRINT ON.

SPOOL <filename>.
) SPOOL OFF.
) CAPTURE <filename>.

2.  ,
SQL*Plus?
) FORMAT COLUMN OFF.

COLUMN FORMAT CLEAR.


) CLEAR COLUMNS.

) .
3. Li enseStatus (,
)?
) INSERT INTO Li enseStatus VALUES (876,'Out of ommission');

INSERT Li enseStatus VALUES (876,'Out of ommission');


) INSERT FROM Li enseStatus VALUES (876,'Out of ommission');
) CREATE FROM Li enseStatus VALUES (876,'Out of ommission');
4. Danial Silverburg
Agents Redwood Realty?
) DELETE FROM Agents LastName = 'Silverburg' AND FirstName ='Danial';
) DROPFROMAgentsWHERELastName = 'Silverburg' AND FirstName ='Danial';
) DELETE FROM Agents WHERE LastName ='Silverburg' AND FirstName ='Danial';
) DELETE Agents WHERE LastName ='Silverburg' AND FirstName ='Danial';
)

) ) ).
5. 
 , 3 rows deleted?
) SET TERMOUT ON.

 Ora le 10g  . 147

SQL*Plus

2.1. ,

Conta tReason
Buy
Casual

Conta tReason

Offer to buy a property ( )


General ustomer probably looking for properties ( ,
, )
Listing to sell a property ( )

Sell

SET
) SET
) SET
) SET

147

PAGESIZE 0.
SPOOL OFF.
INFORM OFF.
FEEDBACK OFF.

Redwood Realty
Redwood Realty Conta tReason.
: ;
- .
. ,
Conta tReason, . 2.1.
1. Ora le, SQL*Plus.
 Ch2<>RedwoodEOC.sql. Save as
type ( ) All Files ( ).
2. , <Enter>
( <Enter>
):

CREATE TABLE Conta tReason


(Conta tReason NVARCHAR2(15) PRIMARY KEY,
Des ription NVARCHAR2(50));
3. SQL*Plus. <Enter>, CREATE TABLE. Ora le
Table reated.
4. , , . 3.
5. , <Enter>, , INSERT (
<Enter>):

 Ora le 10g  . 148

148

2. SQL SQL*Plus

INSERT INTO Conta tReason VALUES ('Buy',


'Offer to buy a property');
INSERT INTO Conta tReason VALUES ('Casual',
'General ustomer probably looking for
properties');
INSERT INTO Conta tReason VALUES ('Sell',
'Listing to sell a property');
6. Insert, . SQL*Plus.
.
7. , ,
SQL*Plus.
8. COMMIT; ( )
<Enter>, .
9. SQL*Plus
t Don't ):

INSERT ( n

INSERT INTO Conta tReason VALUES


('Don''t know', 'Unknown reason');

10. SQL*Plus <Enter>,


:

SELECT *
FROM Conta tReason;
11. ROLLBACK; ( ) <Enter>,
.
12. ,  , SQL*Plus
( <> (, C:\Main\)):

SPOOL <>Ch2Conta tReasonList.txt


DESCRIBE Conta tReason
SELECT * FROM Conta tReason;
SPOOL OFF

13.  Ch2Conta tReasonList.txt , , , ,


, .
14. SQL*Plus  
( <> ):
HOST del <>\Ch2Conta tReasonList.txt
15. , .

DROP TABLE Conta tReason;


16. SQL*Plus.

 Ora le 10g  . 149

SQL*Plus

149

Coffee Mer hant


Coffee Mer hant ,
. ,
SQL*Plus iSQL*Plus. -
iSQL*Plus (
), SQL*Plus. , ,
States,
50 .
, ,
, , URL
 Web- ( ).
1. SQL*Plus Ora le.
 Ch2<>CoffeeEOC.sql.
Save as type ( ), AllFiles ( ).
2. , <Enter>
(
):

CREATE TABLE States


(StateID NCHAR(2) PRIMARY KEY,
StateName NVARCHAR2(20) NOT NULL,
TaxRate NUMBER(7,4),
Population INTEGER,
LandArea INTEGER,
WebURL NVARCHAR2(50)
);
3.  , 
, . .
4. SQL*Plus <Ctrl+V>, , <Enter>  . Ora le
Table reated.
5. SQL*Plus SQL:

INSERT INTO STATES


VALUES ('AK', 'Alaska', 0, 643786,
571951, 'www.state.ak.us');
CLEAR SCREEN
COLUMN StateID FORMAT A7
COLUMN StateName FORMAT A15
COLUMN TaxRate FORMAT 0.9999
COLUMN Population FORMAT 99,999,999
COLUMN LandArea FORMAT 99,999,999
COLUMN WebURL FORMAT A15
SELECT * FROM STATES;
COMMIT;

 Ora le 10g  . 150

150

2. SQL SQL*Plus

DELETE FROM STATES;


SELECT * FROM STATES;
ROLLBACK;
SELECT * FROM STATES;
6. -  Ch02PopulateStates.sql
. SQL*Plus ,
<Enter>,  . ( <> 
.)

START <>\Ch02PopulateStates.sql
,
Commit omplete.
7.

1 row reated,

Exit, Ora le SQL*Plus.

8. Web-, iSQL*Plus Ora le,


.
9. Preferen es , (
) Interfa e Conguration .
Single page OutputPageSetup,
Number of rows on ea h page 60 (. 2.25). Apply,
Workspa e .
10. Workspa e , <Enter> :

COLUMN Density FORMAT 9,999


SELECT StateID, StateName,
Population/LandArea AS Density
FROM States
ORDER BY Density DESC;
11. Exe ute, :
.
,  .
12. File . Print Print Print, iSQL*Plus
.
13. Logout, .
Web- , .

Rowing Ventures
Rowing Ventures .

 Ora le 10g  . 151

SQL*Plus

151

60
Single page
Interface Configuration
. 2.25.

iSQL*Plus

, Rowing Ventures  ,
, , , , .
SQL*Plus Ora le. -, ,  Ch02RowingVentures.sql.
 Boat Organization. ,  , ,
DESCRIBE Organization DESCRIBE Boat, . , ,
. SELECT Organization: OrganizationID,
Phone .   . ,
BoatID BoatCategory Boat. , BoatID DELETE.

 Ora le 10g  . 152

152

2. SQL SQL*Plus

  (, SPOOL C:\Do s\Myfile.txt) BoatID BoatCategory Boat.


, .   . SQL*Plus.

Broad loth Clothing


 - Broad loth Clothing ,
,
 , . ,
-  Ch02Broad loth.sql
. SQL*Plus, Ora le,
.  , :

SPOOL
<>\<>Ch02Broad lothSpool.txt
<> , <> 
( ). ,
,  Ch02Broad loth.sql,
,
( Fa tory WorkingConditions). DESCRIBE, .
Complian eID ConditionCategory
WorkingConditions. WorkingConditions
, Complian eID 2003.
, SELECT.
 . 
(  ). 
 . Exit SQL*Plus,
SQL*Plus Ora le.
iSQL*Plus, , Fa tory ( ), Web- . Clear, Workspa e
. SELECT,
Fa tory. , Web-.
Clear, . DELETE, Fa tory,
(GMTDifferen e) +8 . Fa tory Web-. Logout,
Ora le, .

 Ora le 10g  . 153

, ,


. . .




Ora le
, Ora le


,

. 
2, ,
. , Ora le , ,
 ,
. ,
, ,
.

 Ora le 10g  . 154

154

3. , ,


, , ,
,  ,
. Ora le  , ,
 , .  ,
, . ,
. 
, . , .
, , , ,
. .
, , ;
, ; ,
.
, . , ,
. , , ,
. , ,
, ? ,
,
, ?
. ,
.
,  .
, .


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

 Ora le 10g  . 155

155

-
,   , 
. 
, . - ( ), .
, ( )  , ,  .
, . , .
.
( ), . , ,  ..
.   .  , ,
 , , appli ant.   ,
. ,
, , , .
 . , , ( 
). , , ,
. , ( ) .
. Redwood Realty, ,
Customers () Properties (),
, . ,  Redwood Realty, ,
 . , Agents
Customers, Customers
, Agents. , 
.  ( ), .
, , , .
:  ,
   .   , A B, .

 Ora le 10g  . 156

156

3. , ,

,
.  ,
, .
, .
, , , . ,
 , , X , Y X.
:
( ), .
,
 , ,
( ).


,
, .
, .
 ,
. 3.1, Customers, Listings SaleStatus (  Redwood Realty).
, . ,
, ; , . ; ,  ( )
.
(.. ),
. , .
, ,
. , . . 3.1
SaleStatusID Listings
SaleStatus. ,
. ( ;
.)

 Ora le 10g  . 157

157

Customers(CustomerID, FirstName, LastName, Address, City, State ,Zipcode, HomePhone, CellPhone,


WorkPhone)
Listings(ListingID, PropertyID, ListingAgentID, SaleStatusID, BeginListDate, EndListDate, AskingPrice)
SaleStatus(SaleStatusID, StatusText)
. 3.1.

 ,
 . , ,
. .
,
.
1 , .
,
.
, .  Redwood Realty. ,
Redwood Realty  ,
Customers  . , , ,
Customers ,
, , ..
 . 3.2. ,
. , , ,
? Redwood Realty , ,
.  ,
,
.
. Customers :
 .
, .
:   , 
. :
, ? :
, .
( SELECT), Ora le
,
( 5).

 Ora le 10g  . 158

158

3. , ,

seller information

property information

Customers (CustomerID, FirstName, LastName, . . . , PropertyAddress, SqFt, Bedrooms, . . .)


. 3.2.

Customers

,
. , Ora le
, Ora le.

. , Web-,
 Ora le .
Google (www.google. om).

. Ora le
 , Mi rosoft A ess, ,
. , , , A ess ,
. Ora le
,
. , Ora le,
. . , ,
( ). /, Ora le, , , ,   .
, ,
(Database Administrator  DBA). Personal
Edition Ora le ,
() , SYSTEM
SYSDBA. , ,
, .  , ,
Personal Ora le .

 Ora le 10g  . 159

Ora le

159

, .
Personal Ora le,  .

.
Ora le

Personal Ora le, , , SYSTEM.


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


, ,
CREATE USER.
. ,  :
, .
CREATE USER :

CREATE USER <_> IDENTIFIED BY <>


[DEFAULT TABLESPACE <tablespa e_name>
[TEMPORARY TABLESPACE <tempspa e_name>
<_>
, <>  . , .
SYSTEM, MANAGER. ,
SYSTEM.
, Ora le ,
.
1. SQL*Plus, Ora le,
.

SYSTEM ,

2. ,
. . !

 Ora le 10g  . 160

160

3. , ,

Chapter3
<Enter>, .

ChangeMe.

CREATE USER Chapter3 IDENTIFIED BY ChangeMe;


3. Ora le User

reated. SQL*Plus .

SYSTEM ,
, , .
; ,
.



 .
Ora le ( ), Ora le,
.
1. SQL
<Enter>. . ,  .
. Ora le .
,
<Shift>.

CONNECT hapter3/ hangeme;


2. Ora le :

ERROR:
ORA-01045: user CHAPTER3 la ks CREATE SESSION privilege;
logon denied
3. Ora le , SQL*Plus,
(. 3.3).


, , ... la ks CREATE SESSION privilege ... ( CREATE SESSION), ,
, .

 Ora le 10g  . 161

Ora le

. 3.3.

161

, ,  Chapter3, . ,
SYSTEM , , . Ora le CREATE SESSION.
. ,
SYSTEM.
, .
1. Ora le system.
SQL . ( system manager.
.)

CONNECT system/manager;
Ora le Conne ted, ,
.
2. CREATE SESSION CREATE TABLE,
, hapter3 :

GRANT CREATE SESSION, CREATE TABLE TO hapter3;


Ora le Grant

su eeded.

3. , . , .

CONNECT hapter3/ hangeme;

 Ora le 10g  . 162

162

3. , ,

4. Ora le Conne ted. SQL*Plus


.


Ora le, , SYSTEM, , . ,
Ora le /. ( ), .
 , ,
.
PASSWORD EXPIRE
ALTER USER. ALTER USER,
, :

ALTER USER <_> IDENTIFIED BY <new_password>


PASSWORD EXPIRE
, hapter3 Columbus Ora le.
,
.
1. SQL*Plus SYSTEM, SQL (, SYSTEM MANAGER):

CONNECT SYSTEM/MANAGER;
2.
:

Chapter3 Columbus, -

ALTER USER Chapter3 IDENTIFIED BY Columbus PASSWORD EXPIRE;


Ora le User

altered.
3. , IDENTIFIED BY, Chapter3 .
4. CONNECT Chapter3/Columbus; <Enter>, Ora le, . Ora le ,
, , .

 Ora le 10g  . 163

Ora le

163


Columbus

. 3.4.

. . 5, ,

.
.

5. New password: , Sherlo k, <Enter>, ( )


<Enter>. ( ), Ora le Password hanged, Conne ted
(. 3.4.). SQL*Plus.
Ora le 
, . , .


, . Ora le SQL*Plus iSQL*Plus,
PASSWORD. SQL*Plus ,
. , .

PASSWORD <Enter>.
2. , Sherlo k, <Enter>.
3. , Time run h ( ), 1.

<Enter>. .
4. Password hanged, , . SQL*Plus .

 Ora le 10g  . 164

164

3. , ,

. 3.5.



Chapter3. ,
, SYSTEM. 
( ).
DROP USER, , .

DROP USER <_> [CASCADE


, , .
1.

SYSTEM.

CONNECT SYSTEM/MANAGER;
2. Chapter3, DROP USER Chapter3 CASCADE; <Enter>. .

. Ora le User dropped (. 3.5).
3. Exit <Enter>, Ora le
SQL*Plus.

.
 ,
. ,
, , , , . , ( )
, . , , Gender
M F ( ) Title Agents Redwood Realty
Broker Salesperson.

 Ora le 10g  . 165

165

( NULL),
. (
.)
Ora le .
1 30 .
.
,  $,

_ () #.

.
, ( -

).
Ora le.

DUAL (  ).

. , Customer, us-

tomer CuStOmEr SQL.

,
. (, ..), , "CreditLimit".
,
, SQL.
, "CreditLimit- CREDITLIMIT  .
"CREDITLIMIT- CreditLimit , Ora le
, . , ,
SQL. SQL .
: , .
2, CREATE TABLE. , :

CREATE TABLE [s hema.table_name


( olumn_name type [NOT NULL
[CONSTRAINT onstraint_definition DEFAULT expression
[, olumn_name type [NOT NULL
[CONSTRAINT onstraint_definition DEFAULT expression...
)
[PRIMARY KEY ( olumn_name [, olumn_name ...)
[FOREIGN KEY ( olumn_name [, olumn_name ...)
REFERENCES foreign_table ( olumn_name [, olumn_name ...)

 Ora le 10g  . 166

166

3. , ,

table_name  , , olumn_name 
, , , , , . ( Ora le
.) , DEFAULT,
NULL, , . Constraint_definition ( ) .
( .) CREATE TABLE , ,
SQL Referen e Ora le Corporation.

Ora le
CREATE TABLE, ,
, . ( ), . Ora le ,
, . -, , Ora le
 . -, Ora le
. ( ), Ora le
 , .
Ora le : , , /
. ,
/.


 ,
, .
 , .
Ora le . 3.1.
CHAR NCHAR  ,
. 
, . ,
CHAR 
, .
 :

<fieldname> CHAR(<length>)
,

Zip ode CHAR(5)

Customer :

 Ora le 10g  . 167

167

3.1. Ora le

Ora le

Fixed
National

NCHAR(size)

Variable

VARCHAR2(size)

Variable
National

NVARCHAR2(size)

Memo

LONG

Fixed

CHAR(size)

 , size , .  2 000
, CHAR,
2 000
, size . 
4 000
, size .
 4 000

2 . ( CLOB.)

VARCHAR2, 4 000 . 
. , 
20 ,  5.
, , . NVARCHAR2 VARCHAR2,
. VARCHAR2
:

<fieldname> VARCHAR2(maximum_size)
Redwood Realty
,
. LastName Agents Redwood
Realty :

LastName NVARCHAR2(30)
30 .
Memo ( LONG) ,
CLOB (  ).

 Ora le 10g  . 168

168

3. , ,

3.2. Ora le

INT, INTEGER,
SMALLINT

Ora le

NUMBER(38)

38 

NUMBER(p,s)

FLOAT, DOUBLE
PRECISION

NUMBER

. p
, s  

38 


 Ora le
NUMBER. , 
, 38 . ( 38  , .) NUMBER ,  . Ora le . 3.2. , Ora le INTEGER, INT,
SMALLINT, DOUBLE PRECISION, FLOAT, REAL, DEC, DECIMAL NUMERIC. NUMBER. ,
:

<fieldname> NUMBER [([ pre ision, [ s ale )


pre ision () ,
, s ale ()   .
 ,
. , :

AmountDue NUMBER(7,2)
99 999,99  , . NUMBER(4,4) ,

.
Integer ,
(.. NUMBER s ale ). , ,
:

AnnualSalary NUMBER(6)
,  999 999,  -999 999.

 Ora le 10g  . 169

169

pre ision, , Ora le . , ,


AnnualSalary , 23986.59,
Ora le ( 23987).
 ,
, . , latitude () longitude () , Properties Redwood Realty, , pre ision 8,
s ale  5, ..
183.5456. latitude, , :

Latitude NUMBER(8,5)
, , .
.  ,
  .
NUMBER .
, ,
, , .
Redwood Realty ,
,  , 4,125.


Ora le : DATE,
INTERVAL TIMESTAMP. DATE  ,
. Ora le 4712 .. 31 4712 ..
 TIMESTAMP, ,
, . DATE, (, 17-OCT-06 11.02.24.00 AM). INTERVAL
( ). Ora le /
. 3.3.
DATE  DD-MON-YY, DD  ;
MON  ; YY   .
HH:MI:SS a.m.  ,
12-  . DATE
, Ora le 12:00:00 A.M.
, .
DATE :

 Ora le 10g  . 170

170

3. , ,

3.3. Ora le

Ora le

DATE

TIMESTAMP(p)

INTERVAL YEAR(p) TO
MONTH
INTERVAL DAY(p) TO
SECOND(f)

,
 , , , ,

, (p)
 / YEAR
, , ,
p   ;
f  
.
(p)

HireDate DATE
DATE Redwood Realty,
, HireDate BirthDate Agents.
TIMESTAMP  .
( Ora le)
 .
. (
TIMESTAMP 4.) TIMESTAMP
:

UpdateTableDateTime TIMESTAMP(2)
() , . , Ora le
, .
INTERVAL YEAR TO MONTH .
/ , . ,
, .
.

+ - <elapsed years>-<elapsed months>


+ - ,
. <elapsed years> ,

 Ora le 10g  . 171

171

<elapsed months>  . . , +06-09


. :

TimeSin eLastOffi eVisit INTERVAL YEAR(2) TO MONTH


,  (, 02 2002). , , , .
INTERVAL DAY TO SECOND , , . :

+ - <days> <hours>:<minutes>:<se onds>


, +06 07:26:51.00 , 6 , 7 , 26 51 . ,
INTERVAL DAY TO SECOND.

TimeTrials INTERVAL DAY TO SECOND



, , . ,
/. ,  SQL*Plus iSQL*Plus. 
 iSQL*Plus, - SQL*Plus.
 DemoDataTypes, , SELECT, , ,
.
,  Ch03DataTypes.sql . , Ora le, .
1. iSQL*Plus Ora le, .
2. Load S ript, Browse File, Look , Ch03DataTypes.sql. Ch03DataTypes.sql
Open.
3. iSQL*Plus Load,
Workspa e. . CREATE TABLE, ,
/.

 Ora le 10g  . 172

172

3. , ,

. 3.6.

Ora le

4. Exe ute, .  , Table dropped


(. 3.6).
5. , Web-, .
6. Logout , iSQL*Plus,
.
, .
.  /.
DATE TIMESTAMP , . ,
, 326 .


, ,  ,
, Word Ex el, -

 Ora le 10g  . 173

173

3.4.

Ora le
BLOB
CLOB
NCLOB

Binary LOB  ,
( 128 )
Chara ter LOB 
( 128 )
National Chara ter LOB 
 Uni ode, 128

LOB- Ora le (Large Obje t   ).


. 3.4.
,
. 3.4,

<imageitemname> BLOB
BLOB . , Ora le
. .  Redwood
Realty  ,
( Listings,
).

SQL*Plus
Redwood Realty Customers.  , ,
. Customers,
. 3.5. .
Redwood Realty,  .
.  , , .  ,
Redwood Realty .

, 
Ch03InitializeRedwood.sql ( ).
Redwood Realty, .

 Ora le 10g  . 174

174

3. , ,

3.5.


CustomerID
FirstName
LastName
Address
City
State
Zip ode
HomePhone
CellPhone
WorkPhone

Integer

Customers

Uni ode,

30

Uni ode,

Uni ode,

Uni ode,

Uni ode,

Uni ode,

Uni ode,

Uni ode,

Uni ode,

30

40
30
20
20
20
20
20

1. SQL*Plus: Start (), All Programs


(), Ora leOraDb10_home1, Appli ation Development SQL Plus.
( SQL Plus ,
.) LogOn.
2. UserName, Password
OK. (,
Personal Ora le.
Host String.)
3. <Enter>, 
:

START <>\Ch03InitializeRedwood.sql
4. , . .
5. SQL*Plus.

 Ora le 10g  . 175

175

Customers. NVARCHAR2, Uni ode


. INTEGER,  ,
 .
Customers, .
1. , - Ora le
SQL*Plus.
2.

CLEAR SCREEN <Enter>, .

3. , <Enter>
.
.
.
. , .
, Ora le ,
. ,
 .

CREATE TABLE Customers


(CustomerID INTEGER,
FirstName NVARCHAR2(30),
LastName NVARCHAR2(30),
Address NVARCHAR2(40),
City NVARCHAR2(30),
State NVARCHAR2(20),
Zip ode NVARCHAR2(20),
HomePhone NVARCHAR2(20),
CellPhone NVARCHAR2(20),
WorkPhone NVARCHAR2(20) );
4. , <Enter>, CREATE
TABLE SQL*Plus Ora le  . Edit SQL, Notepad
(). File (), Save () . / SQL*Plus,
. <Enter>
. Ora le Table reated.
5. Des ribe Customers <Enter>,
, Ora le (. 3.7).
, INTEGER CustomerID
Ora le NUMBER(38).
6. SQL*Plus.

 Ora le 10g  . 176

176

3. , ,

SQL,
Customers

Oracle

. 3.7.

 SQL*Plus


Ora le  (, ,
..) . , Ora le - . SQL COMMENT.
, .  ,
. ,
, , .
Customers, , Ora le. COMMENT,
, :
COMMENT ON TABLE <tablename> IS '<text>'
, COMMENT
.
, , .
1. SQL*Plus Ora le,
.

CLEAR SCREEN,

2. SQL <Enter>, :

 Ora le 10g  . 177

. 3.8.

177

COMMENT ON TABLE Customers IS


who want to buy or sell a property';

'People

Ora le Comment

reated.

3. ,
SQL*Plus SQL. CUSTOMERS ,
.

COLUMN omments FORMAT A40 WORD_WRAPPED


SELECT table_name, omments
FROM user_tab_ omments
WHERE table_name = 'CUSTOMERS';
 . 3.8.
4.

Exit, Ora le, SQL*Plus.

. , State Customers,

COMMENT ON COLUMN Customers.State IS


'The state should ALWAYS be California';
,  (State).
,
. Customers,

SELECT *
FROM user_ ol_ omments
WHERE table_name = 'CUSTOMERS';
 . 3.9.

 Ora le 10g  . 178

178

3. , ,

State

. 3.9.

.
Customers . Ora le .
, ,
() .

SQL , , , . ( )  ,
, . , ,
(PRIMARY KEY),  (FOREIGN KEY ... REFERENCES).

 Ora le 10g  . 179

179

, , ,
;  ,
. , , . ,
(NOT NULL),
(UNIQUE),
(CHECK). , , Gender () M (male  ) F (female 
). , CommissionRate () 1 6
(NOT NULL). , .
, ,
CREATE TABLE .
CREATE TABLE.


, SQL : CHECK, FOREIGN KEY, NOT NULL, PRIMARY KEY
UNIQUE. (
Ora le ). ,
.
,  . , ,
. , :

{pk|fk}_<primary_key_table_name>_<foreign_key_table_name>
, Customer
pk_ ustomer, pk  primary key ( ). (foreign key  fk) Agents, Li enseStatus,
fk_li ensestatus_agents ( , ,
, ). , Ora le
, (
), / .
, :

{nn|un| }_<tablename>_< olumnname>

 Ora le 10g  . 180

180

3. , ,

3.6. 

CHECK
FOREIGN KEY
NOT NULL
PRIMARY KEY
UNIQUE


k
fk
nn
pk
un

k_ ustomers_zip ode
fk_ ustomers_properties
nn_agents_lastname
pk_agentid
un_ onta treason_des ription

, nn_agents_lastname  ,
NOT NULL LastName Agents. , - 30 . ,
k_ ustagentlist_ ommrate  , CommissionRate CustAgentList.
. 3.6,
 .


, , .


, , , (NOT NULL) (UNIQUE).
, . ( ),
, . , ,
, .
.
,
 PRIMARY KEY. , , CustomerID Customers .
CREATE TABLE 

CustomerID INTEGER PRIMARY KEY


, :

CustomerID INTEGER CONSTRAINT pk_ ustomers PRIMARY KEY

 Ora le 10g  . 181

181

 pk, , (  ).
, CREATE
TABLE . , CREATE TABLE, , ,
:

...
WorkPhone NVARCHAR2(20),
CONSTRAINT pk_ ustomers PRIMARY KEY (CustomerID)
);
CREATE TABLE. , CustAgentList Redwood Realty
CustomerID, AgentID, ListingID Conta tDate.
PRIMARY KEY :

...
CommissionRate NUMERIC(4,4),
CONSTRAINT pk_CustAgentList PRIMARY KEY (CustomerID, AgentID,
ListingID, Conta tDate));

,
, , .
,  . .


.
, ,

( ).  
, ,
 . 
, ,
, .
.
,
, .
, ,
( ), CREATE
TABLE ( ).
:

 Ora le 10g  . 182

182

3. , ,

< olumn-name> <datatype> [CONSTRAINT < onstraint-name>


REFERENCES <tablename> [( olumn-name)
[ON DELETE {CASCADE | SET NULL}
ON DELETE , : CASCADE ,
-; CASCADE, Ora le .
, OwnerID, CustomerID Customers.

OwnerID INTEGER CONSTRAINT fk_ ustomers_properties (1)


REFERENCES Customers (CustomerID)
OwnerID INTEGER REFERENCES Customers (CustomerID) (2)
OwnerID INTEGER REFERENCES Customers (3)
, ,
. . OwnerID 
CustomerID. ,
, , Ora le
. ,
, . ,
,
.
UNIQUE .
,
CREATE TABLE  FOREIGN KEY. 
.

[CONSTRAINT < onstraint-name> FOREIGN KEY (< olumn-name>)


REFERENCES <primary-key-tablename> [(primary-key- olumn-name)
[ON DELETE {CASCADE | SET NULL}
< olumn-name> , , <primary-key-tablename>  , , <primary-key- olumn-name> , .
. ,
Li enseStatusID
Li enseStatus.

CREATE TABLE Agents


(AgentID INTEGER PRIMARY KEY,
...

 Ora le 10g  . 183

183

CONSTRAINT fk_li ensestatus_agents FOREIGN KEY (Li enseStatusID)


REFERENCES Li enseStatus(Li enseStatusID)
);
. ,
, PRIMARY KEY.

 
NOT NULL , (
NULL). , , NOT NULL,
,
, . NOT NULL
. ,
.
NOT NULL ,
. , NOT NULL ,

. , NOT NULL
( ).
Properties Redwood Realty
, ,  Address City.
Ora le ,
NOT NULL :

CREATE TABLE Properties


(PropertyID INTEGER PRIMARY KEY,
...
Address NVARCHAR2(30) NOT NULL,
City NVARCHAR2(30) NOT NULL,
State NVARCHAR2(20),
...
NOT NULL ,
, State .
NOT NULL, ,
NOT NULL, DESCRIBE <tablename>. . 3.10 Orders
Coffee Mer hant, NOT NULL. ORDERID , ,
NOT NULL.

 Ora le 10g  . 184

184

3. , ,

NOT NULL Null?

. 3.10.

NOT NULL DESCRIBE


UNIQUE ,
,  . ( ). PRIMARY KEY UNIQUE ,
UNIQUE . UNIQUE
, . , UNIQUE, 
( ). , Li enseStatus Redwood Realty,
,
- (, Li enses Revoked), . ,
( ), ,
StatusText UNIQUE. Ora le ,
, StatusText,

 Ora le 10g  . 185

.
.

185

UNIQUE

CREATE TABLE Li enseStatus


(Li enseStatusID INTEGER PRIMARY KEY,
StatusText NVARCHAR2(25) UNIQUE);


UNIQUE .

CREATE TABLE MovieColle tion


(MovieID INTEGER PRIMARY KEY,
Dire torName VARCHAR2(30) NOT NULL,
CONSTRAINT un_dire torname_movie oll UNIQUE(Dire torName));


, UNIQUE,
. ,
UNIQUE .


CHECK , true .
, . , , Redwood Realty
: 1
6  . ,
Gender M F ( ).
CHECK, : ,
. CHECK ,
true false. ,
Ora le , CHECK, , ( ). :

CONSTRAINT < onstraint-name> CHECK (<logi al expression>)


, Title Agents Broker Salesperson, CREATE TABLE
CHECK:

CREATE TABLE Agents


...
HomePhone NVARCHAR2(20),
Title NVARCHAR2(20) CONSTRAINT k_agents_title
CHECK ((Title = 'Broker') OR (Title = 'Salesperson')),
...
, ,
. , ComRate
1 6 .

 Ora le 10g  . 186

186

3. , ,

3.7. ,

Conta tReason
Des ription


NVARCHAR2
NVARCHAR2

15
50

Conta tReason

PRIMARY KEY

, Ora le  .

...
ComRate NUMBER(4,4) CHECK (ComRate >= 0.01 AND ComRate < 0.06),
...
ComRate , , ,
0.08, Ora le :
ORA-02290: he k onstraint(<yours hema>.SYS_< he kname>) violated
<yours hema> ,
< he kname>  Ora le , CHECK ComRate.


, ,  Ora le SQL*Plus iSQL*Plus.
- 
.  ,
. , SQL*Plus Ora le.
Redwood Realty . Conta tReason. ,
(, , ),
. , , , , ,
. ,
. (
) .
 Conta tReason,
( , ), . 3.7.
Conta tReason, .
1. , SQL*Plus .

 Ora le 10g  . 187

187

SQL

. 3.11.

2. SQL*Plus , . 3.11 ( 
SQL*Plus ). <Enter>,
.
3. ; ( ) , <Enter>,
CREATE TABLE.
. Ora le , CREATE TABLE ( ,
SQL Ora le), Edit, Copy,
Edit () Paste (),
. , :
, SQL*Plus
. <Enter>, .

4. DESCRIBE Conta tReason <Enter>, (. 3.11). ,


Conta tReason NOT NULL, , .
,
( ). Agents . 3.8.
, .
Agents, .

CLEAR SCREEN <Enter>, .


2. CREATE TABLE, . 3.12.

1.


SQL*Plus.

 Ora le 10g  . 188

188

3. , ,

3.8.


AgentID
FirstName
LastName
HireDate
BirthDate
Gender

WorkPhone
CellPhone
HomePhone
Title
TaxID
Li enseID
Li enseDate
Li enseExpire
Li enseStatusID


INTEGER
NVARCHAR2
NVARCHAR2
DATE
DATE
NVARCHAR2
NVARCHAR2
NVARCHAR2
NVARCHAR2
NVARCHAR2
NVARCHAR2
NVARCHAR2
DATE
DATE
INTEGER

Agents

30
30
10
20
20
20
20
20
20

PRIMARY KEY
NOT NULL
NOT NULL

 M F

UNIQUE

3. DESCRIBE Agents <Enter>, NOT NULL.


, , DESCRIBE
NOT NULL. ,
? !


SQL
 , ,
- . ,
, CREATE TABLE :

< olumn-name> < olumn-type> DEFAULT <expression>


, <expression>, ,
 SQL ( ), .
.
Listings, , ,
(Primary Key  PK), NOT NULL . -

 Ora le 10g  . 189

. 3.12.

189

Agents

, INSERT,
.
BeginListDate,  Ora le SYSDATE, , .
, EndListDate ,  180 . ,
 ,
.
Listings,
, .

CLEAR SCREEN <Enter>, .


2. CREATE TABLE, . 3.13.
1.

SQL , SQL*Plus.
3. CREATE TABLE, <Enter>
. Ora le Table reated. ,
, . SQL*Plus .

 Ora le 10g  . 190

190

3. , ,

. 3.13.

, NOT NULL
ListingAgentID. Ora le .

.
- . ALTER TABLE DROP, MODIFY, ADD RENAME.
, ,
, . , ,
, , . ALTER TABLE ,
, (
). . 3.14 , . ,
, .
A, B,
. B. ALTER TABLE  ,
.

 Ora le 10g  . 191

Table A
PK

PrimaryKeyA
ColumnA1
ColumnA2
ColumnA3
ColumnA4
ForeignKeyA

. 3.14.

191

Table B
PK

PrimaryKeyB
ColumnB1
ColumnB2
ForeignKeyB

, -

,
, CREATE TABLE, ALTER TABLE ... ADD. ,
. , , ALTER TABLE
ADD CONSTRAINT ... DISABLE, .
, , .
ALTER TABLE ...
DISABLE CONSTRAINT. ,
ALTER TABLE ... ENABLE CONSTRAINT. , , . ,
. ALTER TABLE
. DISABLE CONSTRAINT.
, PropertyID Listings
, , , ,
. ,
PropertyID , Listings, Li enseStatus, ,
.
Listings. ,
( PropertyID Listings) ,
.

ALTER TABLE Listings DISABLE CONSTRAINT fk_propertieslistings


 ALTER TABLE, , .
, NOT NULL MODIFY CONSTRAINT.

ALTER TABLE <table-name>


ADD CONSTRAINT < onstraint-name> < onstraint-type>
ALTER TABLE <table-name>
MODIFY CONSTRAINT < onstraint-name> NOT NULL

 Ora le 10g  . 192

192

3. , ,

ALTER TABLE <table-name>


ENABLE CONSTRAINT < onstraint-name>
ALTER TABLE <table-name>
DISABLE CONSTRAINT < onstraint-name>
Listings, ,
.
 Agents. , , ListingAgentID Listings ,
AgentID Agents. .
, Listings ,
.
1. <Enter> :

ALTER TABLE Listings


ADD CONSTRAINT fk_agents_listings FOREIGN KEY (ListingAgentID)
REFERENCES Agents (AgentID)
ON DELETE CASCADE;
Ora le Table

altered.

2. Customers, ,
. , ALTER TABLE. , FirstName LastName
NOT NULL, (
NOT NULL
MODIFY).
,
tomers, .

Cus-

1. ( <Enter>
):

ALTER TABLE Customers


ADD CONSTRAINT pk_ ustomers PRIMARY KEY(CustomerID);
Ora le - Table

altered.
2. NOT NULL FirstName,
:

ALTER TABLE Customers


MODIFY FirstName CONSTRAINT nn_ ustomers_fname NOT NULL;
3.
:

NOT NULL LastName,

ALTER TABLE Customers


MODIFY LastName CONSTRAINT nn_ ustomers_lname NOT NULL;

 Ora le 10g  . 193

. 3.15.


. 3.15.

193

ALTER TABLE

ALTER TABLE Ora le


, ,
ALTER TABLE DROP. , . , NOT NULL
LastName Customers

ALTER TABLE Customers DROP CONSTRAINT nn_ ustomers_lname;


Listings,
.

ALTER TABLE Listings DROP PRIMARY KEY;

ALTER TABLE Listings DROP CONSTRAINT pk_listings;


, ,
ALTER TABLE. ,
( ListingAgentID) fk_agents_listings fk_agents_list.

ALTER TABLE Listings RENAME CONSTRAINT fk_agents_listings


TO fk_agents_list;

 Ora le 10g  . 194

194

3. , ,

, () . ,
, , Ora le. .
(.  NOT NULL
Listings.)

, CREATE TABLE,  ALTER TABLE. , ,


BeginListDate Listings, .

ALTER TABLE Listings MODIFY BeginListDate DEFAULT SYSDATE -2;


, DEFAULT
NULL:

ALTER TABLE Listings MODIFY BeginListDate DEFAULT NULL;


, :
Ora le BeginListDate, .
, , , CREATE TABLE .
,  ALTER TABLE.
.
.
 , .
.

, AskingPri e NUMBER(9) NUMBER(12,2),


  :

ALTER TABLE Listings MODIFY AskingPri e NUMBER(12,9);


 (CREATE TABLE ALTER TABLE) (Data Denition Language  DDL),
ROLLBACK. , ALTER TABLE,
.

 Ora le 10g  . 195

195

ContactDate
. 3.16.

,
, .
. Customers
Conta tDate ( ,
). Conta tDate DATE, ;  ,
 SYSDATE.
, .
1. SQL*Plus <Enter>:

ALTER TABLE Customers


ADD Conta tDate DATE DEFAULT SYSDATE;
2. DESCRIBE Customers <Enter>, ,
Ora le (. 3.16).
.
: , . :
( INSERT),

 Ora le 10g  . 196

196

3. , ,

. 3.17.

. , Redwood Realty
,
.
, .
1. ,
<Enter>, ):

HomePhone (

ALTER TABLE Customers


DROP COLUMN HomePhone;
2.  ,
DESCRIBE Customers (. 3.17).
,
COLUMN :

ALTER TABLE Customers


DROP (BirthDate, Gender, WorkPhone);

.

ALTER TABLE RENAME COLUMN, -

. , ,
, .

 Ora le 10g  . 197

197

, Title
Agents AgentTitle. ALTER TABLE:
ALTER TABLE Agents RENAME COLUMN Title TO AgentTitle;


, ,
. ? Ora le. DESCRIBE , ,
 . , -.  ,
. , ,
. , Ora le .

, ,
, .  .
( COLUMN).

ALTER TABLE <table-name> SET UNUSED COLUMN < olumn-name>;


ALTER TABLE <table-name> SET UNUSED (< ol-name1>, ... ,< ol-namen>)
ALTER TABLE DROP COLUMN
DROP UNUSED COLUMNS,  ()
. .
 Li enseID, Li enseDate Li enseExpire Agents.
,
.
1. , SQL*Plus Ora le.
2. DESCRIBE Agents, ,
. Li enseID, Li enseDate
Li enseExpire.
3.
:

ALTER TABLE <Enter>,

ALTER TABLE Agents


SET UNUSED (Li enseID, Li enseDate, Li enseExpire);

 Ora le 10g  . 198

198

3. , ,

. 3.18.

4. DESCRIBE Agents ( <Enter>), , (. 3.18).


5. Exit <Enter>, SQL*Plus (
iSQL*Plus).

. ,
 ,
, DESCRIBE <tablename>.  NOT
NULL. , , , NOT NULL. ,
. 3.19 DESCRIBE Listings.
, NOT NULL,
, ListingID  , ListingAgentID  .

 Ora le 10g  . 199

. 3.19.

199

Listings

 , , , Ora le.

Ora le.  ,
. . ,

. DESCRIBE   , .
Ora le SYS.
, , ,
. , 
(database views).  ,
.
() , ,

 Ora le 10g  . 200

200

3. , ,

.  ,
,  SELECT. , ,
, , .. 
 (need to know), .
: DBA, USER ALL. DBA 
(, ..), ,  . ALL
, ,
,  .
( SELECT 5,  6,
SELECT .)


, 
, , :

SELECT view_ olumnname1, ..., view_ olumnnamek


FROM groupprefix_obje tname;

view_ olumnname  , , groupprefix  USER, DBA ALL; obje tname  , ( , TABLES CONSTRAINTS). ,

SELECT table_name
FROM user_tables;
table_name  ,
user_tables .
, (.. , Ora le
, ). , SELECT
,
( ).

SELECT view_name FROM all_views;


 .
iSQL*Plus SQL*Plus  .
, .

 Ora le 10g  . 201

201

. 3.20.

1. SQL*Plus Ora le, .


2. , <Enter> :

SELECT table_name
FROM user_tables;
Ora le (
).
3. , . SQL*Plus, SELECT,
, <Ctrl+C>,
.
4. <Ctrl+V>, . :

SELECT table_name
FROM user_tables
WHERE table_name NOT LIKE '%BIN%';
5. BIN
.
6.  <>Ch3GetTableNames.sql.
SQL*Plus .

7. SQL*Plus <Enter>,
. Ora le ( ),
. 3.20.

 Ora le 10g  . 202

202

3. , ,

. 3.21.

user_tables

, 
user_tables, DESCRIBE user_tables.
47 , . . 3.21.
 , , . 3.9. ,
SELECT, . .
, . 3.9,
. , ,
, DESCRIBE <viewname>, . !
.

 Ora le 10g  . 203

203

3.9. ,

CONS_COLUMNS
CONSTRAINTS
INDEXES
OBJECTS
SEQUENCES
TAB_COLUMNS
TABLES
USERS
VIEWS

,
,


,
(USER_TAB_COLUMNS  )

(ALL_USERS)


 user_tab_ olumns. ( user 
, .) , SELECT,
user_tab_ olumns. ,
, DESCRIBE
user_tab_ olumns. , 
Listings. , , SQL*Plus
- Ora le.
 , .
1. ( ), <Enter>
. LISTINGS .  . 3.22.

CLEAR SCREEN
COLUMN olumn_name FORMAT A15
COLUMN data_ FORMAT A9
COLUMN nullable FORMAT A8
SELECT olumn_name, data_type, data_length,
data_pre ision, data_s ale, nullable
FROM user_tab_ olumns
WHERE table_name = 'LISTINGS';
2. CLEAR COLUMNS <Enter>,  .

 Ora le 10g  . 204

204

3. , ,

. 3.22.

3.10.

owner
onstraint_name
onstraint_type
table_name
status

user_ onstraints

( )

: P, R, C, U, V O
,
ENABLED DISABLED

.
, DATA_PRECISION ,
AskingPri e
. NULLABLE , NULL.


 .
. , Ora le ,  .
, user_ onstraints,  ,
. . 3.10
user_ onstraints. , ,  . 5,
   6.
, onstraint_type (. . 3.10)
. , ,

 Ora le 10g  . 205

205

: P (primary key); R  (foreign


key); C  ( he k)  ; U  (unique);
V  ; O  . P, R, C U.
, , user_ onstraints,  , .
, , .
1. SQL*Plus , <Enter>
( BIN
%):

CLEAR SCREEN
SET PAGESIZE 20
COLUMN table_name FORMAT A15
COLUMN onstraint_ FORMAT A10
SELECT table_name, onstraint_type, onstraint_name, status
FROM user_ onstraints
WHERE table_name NOT LIKE 'BIN%'
ORDER BY table_name;
Ora le , ,
(. 3.23).
2. SQL*Plus PAGESIZE 
, SQL*Plus:

SET PAGESIZE 14
CLEAR COLUMNS
, SYS (, ).
,
Ora le,
Listings. , ?
, user_ ons_ olumns  ,
. table_name, olumn_name onstraint_name.
, , .
1. SQL*Plus , <Enter>
. LISTINGS
, Ora le (
).

 Ora le 10g  . 206

206

3. , ,

. 3.23.

CLEAR SCREEN
COLUMN table_name FORMAT A15
COLUMN olumn_name FORMAT A15
COLUMN onstraint_name FORMAT A25
SELECT table_name, olumn_name, onstraint_name
FROM user_ ons_ olumns
WHERE table_name = 'LISTINGS';
2. SQL*Plus,

exit <Enter>.

, , . 3.24.
,
ListingAgentD Listings.
,  CHECK, NOT NULL ( 
).
 , ,
,  Ch03TableConstraintsISP.sql,
. ,
iSQL*Plus, 
. iSQL*Plus.
, ,
.
1. iSQL*Plus .

 Ora le 10g  . 207

. 3.24.

207

Listings

2. Load S ript, Browse, File, , 


Ch03TableConstraintsISP.sql.
3. Ch03TableConstraintsISP.sql
Choose le, Open Load, 
Workspa e.
4. Exe ute, . Ora le
 , , ,
(. 3.25).
5. iSQL*Plus .
SQL*Plus, 
Ch03TableConstraints.sql, ,
 SQL*Plus.


, , ,
, .  Customers State .
SELECT. Ora le
, , , user_tab_ omments (
, ). ,
all_tab_ omments , .
, , , .

 Ora le 10g  . 208

208

3. , ,

. 3.25.

,
:

SELECT *
FROM user_tab_ omments
, :

SELECT *
FROM user_ ol_ omments
WHERE
SELECT, .
, SYS,
, .
,  ,
, . ,  (
, ), . SQL*Plus iSQL*Plus,
Ch03TableComments.sql.

 Ora le 10g  . 209

. 3.26.

209

SYS


SYS, .
1. iSQL*Plus Load S ript, Browse. ,  Ch03TableCommentsISP.sql,
Open Load. Workspa e.
2. Exe ute, (. 3.26).
3. Logout, ,
.

. ,

SQL , (DROP) (RENAME) . , , , .

 Ora le 10g  . 210

210

3. , ,

. 3.27.

DROP TABLE,


, , DROP. ,
, . ,
. , . Ora le
,  , ,
. Ora le,
10g, ,
. Ora le 10g ,
. ,
, , PURGE. DROP TABLE :

DROP TABLE <tablename> [CASCADE CONSTRAINTS [PURGE


, , . ,
Agents, Ora le , ListingAgentID
Listings AgentID Agents (. 3.27). ,
. 
: , ,
ALTER TABLE ;
, , ; Ora le ,
CASCADE CONSTRAINTS. , Ora le , ,
, . , , ,
all_dependen ies user_dependen ies.

 Ora le 10g  . 211

211

all_dependen ies, ,  60 000 !


Agents,
CONSTRAINTS  .
, .

CASCADE

1. , SQL*Plus Ora le.


2. :

DROP TABLE Agents;


Ora le (. 3.27).
3. :

DROP TABLE Agents CASCADE CONSTRAINTS;


Ora le Table dropped.


Ora le 10g PURGE DROP TABLE,
, FLASHBACK TABLE. , Ora le
, ,
. ,
, . , ,
DROP TABLE, :

FLASHBACK TABLE <tablename> TO BEFORE DROP;


, ,
user_re y lebin, SHOW RECYCLEBIN,
RECYCLEBIN.
 , ,
.
1. <Enter>, :

COLUMN FORMAT A10


SELECT original_name, obje t_name, type
FROM user_re y lebin;
Ora le , ,
original_name Agents (,
. 3.28).

 Ora le 10g  . 212

212

3. , ,

. 3.28.

Agents, :
FLASHBACK TABLE Agents TO BEFORE DROP;

2.

Ora le Flashba k

omplete.


PURGE, . .

PURGE TABLE <tablename>


:

PURGE RECYCLEBIN
, .
1. <Enter>:

PURGE RECYCLEBIN;
Ora le Re y le

bin purged.


(, ) RENAME;
ALTER TABLE. Ora le ,
, . , ,
( 7),  ( 7) ( 6), . -

 Ora le 10g  . 213

213

, ,
.
:

RENAME <old-tablename> TO <new-tablename>

  ,
, . 
, SELECT.
CREATE TABLE AS SELECT
,  .
.
 CREATE TABLE AS :

CREATE TABLE <new-tablename> AS


SELECT *
FROM <old-tablename>
. , ,
Listings , - .
, .
1. SQL*Plus <Enter>:

CREATE TABLE NewListings AS


SELECT *
FROM Listings;
Ora le Table

reated.

2. ,
SQL*Plus.

DESCRIBE NewListings
 ,
. 3.29.
3. exit <Enter>, Ora le
SQL*Plus.

 Ora le 10g  . 214

214

3. , ,

. 3.29.


 , ,
: ,
, .. 
 .  , .
, .
, . , () ,
. ,
.
; Ora le : , , ,
. - , ALTER TABLE.
 ,
, . , : ,
; ;
,
, .
 Ora le, :
Web-.  SQL*Plus, -

 Ora le 10g  . 215

215

<Enter>, . Web iSQL*Plus ,


SQL SQL*Plus  .
Ora le ,
, , , . DROP TABLE,
( ) , .
FLASHBACK. Ora le PURGE RECYCLEBIN.
















 

NOT NULL

Ora le


 
-

 Ora le 10g  . 216

216

3. , ,

?
1. .
2. , VARCHAR2(30), 30
. , Ora le 30
.
3. , , .
4.   .
5. , NUMBER(7,2), 99,9999.


1. , ,  
_____, .
2. ____ ____ ( )  .
3. _____ , .
4.
Ora le _____.
5. , ,
_____.


1. ,
.
?

 Ora le 10g  . 217

217

MODIFY USER <_> as identified by <>


) CONNECT <_>/<>
) CREATE USER <_>/<>
) CREATE USER <_> IDENTIFIED BY <>
)

) .
2. Quantity, ,
96 785?
) Quantity NUMBER(38)

Quantity NUMBER(7,2)
) Quantity NUMBER(96785)
) Quantity NUMBER(5)

3. ,
.
) PRIMARY KEY

FOREIGN KEY
) CHECK
) UNIQUE

) ) ).
4.
_____.
) .

user_tables Ora le -

) .
) .
) .
) .
5. Ora le . , ,
, .
) CASCADE CONSTRAINTS

DUMP
) PURGE
) FLASHBACK
) NO RECYCLE
)

 Ora le 10g  . 218

218

3. , ,

1. Readwood Realty
 (Robert Stirling), Redwood Realty, , .
CustAgentList Agents, Customers, Conta tReason Listings. ,
 . CustAgentList : CustomerID,
AgentID, ListingID, Conta tDate, Conta tReason, BidPri e CommissionRate. ,
. ,
.  Conta tReason. BidPri e . ,
90 000 800 000. CommissionRate
( ) 2 6 .
1. SQL*Plus Ora le. SQL*Plus
,  (
).
2. , <Enter> . ,  ; ,
. <> , <>  .

REM Drop the NewListings


DROP TABLE NewListings CASCADE CONSTRAINTS;
COLUMN FORMAT A10
SPOOL C:\<>\<>Ch3Redwood.txt
SHOW USER
SELECT original_name, obje t_name, type
FROM user_re y lebin;
SPOOL OFF
REM Permanently eliminate the NewListings
PURGE RECYCLEBIN;
DROP TABLE CustAgentList CASCADE
CONSTRAINTS PURGE;
CREATE TABLE CustAgentList
(CustomerID INTEGER NOT NULL,
AgentID INTEGER NOT NULL,
ListingID INTEGER NOT NULL,
Conta tDate DATE NOT NULL,
Conta tReason NVARCHAR2(15),
BidPri e NUMERIC(9) CONSTRAINT
_ uaglistBidPri e
CHECK ((BidPri e >= 90000) AND (BidPri e <= 800000)),
CommissionRate NUMERIC(4,4) CONSTRAINT
_ uaglistCommRate
CHECK (CommissionRate Between 0.02

 Ora le 10g  . 219

219

AND 0.06),
CONSTRAINT pk_CustAgentList
PRIMARY KEY (CustomerID, AgentID,
ListingID, Conta tDate));
3. , <Enter> . ALTER TABLE .

ALTER TABLE CustAgentList


ADD CONSTRAINT fk_CustAgentList_Cust
FOREIGN KEY (CustomerID)
REFERENCES Customers (CustomerID) ON
DELETE CASCADE;
ALTER TABLE CustAgentList
ADD CONSTRAINT fk_CustAgentList_Agent
FOREIGN KEY (AgentID)
REFERENCES Agents (AgentID) ON DELETE
CASCADE;
ALTER TABLE CustAgentList
ADD CONSTRAINT fk_CustAgentList_Listing
FOREIGN KEY (ListingID)
REFERENCES Listings (ListingID) ON DELETE
CASCADE;
ALTER TABLE CustAgentList
ADD CONSTRAINT fk_CustAgentList_Conta t
FOREIGN KEY (Conta tReason)
REFERENCES Conta tReason (Conta tReason)
ON DELETE CASCADE;
4. , .   ,
, 4 . 2.

COLUMN table_name FORMAT A15


COLUMN olumn_name FORMAT A15
COLUMN onstraint_name FORMAT A25
SPOOL C:\<>\<>Ch3Redwood.txt
APPEND
DESCRIBE CustAgentList
SELECT table_name, olumn_name,
onstraint_name
FROM user_ ons_ olumns
WHERE table_name = 'CUSTAGENTLIST';
SPOOL OFF
CLEAR COLUMNS
5.

exit <Enter>, SQL*Plus.

6. , File (), Open (). ,   <>Ch3Redwood.txt,


<>Ch3Redwood.txt, Open ().
7. <Enter>, .
,  .

 Ora le 10g  . 220

220

3. , ,

8. File () Print (),  .


Yes (),
.

2. Coffee Mer hant


Coffee Mer hant ,
.
SQL*Plus. ,
Inventory,  ; Countries,  . Inventory
,
 .
SQL*Plus SQL, . , SQL*Plus. SQL*Plus.   .  9  . , 
Ch03InitializeCoffee.sql, Coffee Mer hant,
.
1. SQL*Plus Ora le.
2. SPOOL<>\<> Ch3Coffee.txt <Enter>.
<> (, C:\Temp), <>  .
3. :

COLUMN FORMAT A10


SHOW USER
DROP TABLE States;
SELECT original_name, obje t_name, type
FROM user_re y lebin;
PURGE RECYCLEBIN;
4. Countries, <Enter> :

CREATE TABLE Countries


(CountryID INTEGER CONSTRAINT pk_ ountries
PRIMARY KEY,
CountryName NVARCHAR2(40) CONSTRAINT
nn_CountriesCountryName NOT NULL);

 Ora le 10g  . 221

221

5. Inventory, <Enter> (
):

CREATE TABLE Inventory


(InventoryID INTEGER CONSTRAINT
pk_inventory PRIMARY KEY,
Name NVARCHAR2(40) NOT NULL,
Pri e NUMBER(6,2) CONSTRAINT _InvPri e
CHECK (Pri e >=3 AND PRICE <= 400),
OnHand INTEGER CONSTRAINT _InvOnHand
CHECK (OnHand BETWEEN -1000 AND 25000),
Des ription NVARCHAR2(500),
Item NVARCHAR2(1) CONSTRAINT
_InvItemType
CHECK (ItemType = 'C' OR ItemType = 'T'),
CountryID INTEGER CONSTRAINT
fk_CountriesInventory
REFERENCES Countries(CountryID) ON
DELETE CASCADE);
6. , , , Ora le ( ,
<Enter> ):

DESCRIBE Inventory
DESCRIBE Countries
COLUMN table_name FORMAT A15
COLUMN olumn_name FORMAT A15
COLUMN onstraint_name FORMAT A25
SELECT table_name, olumn_name,
onstraint_name
FROM user_ ons_ olumns WHERE table_name =
'INVENTORY';
SPOOL OFF
CLEAR COLUMNS
7.

exit, SQL*Plus Ora le.

8. , File () Open (). ,   <>Ch3Coffee.txt,


<> Ch3Coffee.txt, Open ().
9. File () Print (),  .
.

3. Rowing Ventures
 Rowing Ventures , ,  , ; , , . -

 Ora le 10g  . 222

222

3. , ,

Rowing Ventures, 

Ch03InitializeRowing.sql. -

, . 
<>Ch3Rowing.sql.  BoatCrew Person.
BoatCrew : BoatID, PersonID Position.
BoatID PersonID
BoatCrew. .
Position  0 8, 0 ,
1 8  , . PersonID , PersonID Person.
, Position, . PersonID
, Person,
ALTER TABLE BoatCrew.
Person : PersonID, LastName, FirstName,
Gender, Weight DateOfBirth. PersonID, , . LastName, FirstName Gender
Uni ode, 50.
LastName FirstName ,
. Gender M F ( ), (,
) F. (. .) Weight (
) 85 250. BoatCrew Position
0 indi ates the oxswain ( Position = 0
). , .
  : (SHOW USER); ; BoatCrew (. , WHERE
table_name = 'BOATCREW'.); . (.
, ,
user_ onstraints,
WHERE table_name IN ('BOATCREW', 'PERSON').).   
 , .

4. Broad loth Clothing


Broad loth Clothing , , Customer, .
iSQL*Plus, , 
Ch03InitializeBroad loth.sql. ( Broad loth
Clothing, .)

 Ora le 10g  . 223

3.11.

Customer


CustomerID

Integer

CompanyName
City
Nation
Conta tID
BaseCurren y

String
String
String
Integer
String

223

;  0

; 50 NOT NULL
; 50 NOT NULL
; 50 ()
()
; 50 ()

, table or view does not exist (


), . Customer
, . 3.11. .
, Web- iSQL*Plus Table reated.
BaseCurren y Curren y appropriate for home
ountry (  ). Customers. . user_ onstraints WHERE table_name ='CUSTOMER',
Customer. Web-.
Customer (. ). DESCRIBE Customer Web-.

 Ora le 10g  . 224

 Ora le 10g  . 225



. . .


,


 SQL
,


, -


3 , DDL- Ora le , . CREATE TABLE, Ora le
. ,
DDL-: COMMIT . , DML (Data Manipulation
Language  ), ,
. DML-, -

 Ora le 10g  . 226

226

4.

(Transa tion Control  TC) COMMIT,


 . SELECT, 5 6.
, , (:  ?). DML,
, , (a tion queries), ,
.
, 
.
, , , .
, (trigger),  ,
Ora le INSERT, UPDATE
DELETE .
, , . ,
 , SQL
SQL*Plus.  SQL*Plus, START, .
Windows, . ,
START .
Redwood Realty,
4, .
1. SQL*Plus, , ,
CLEAR SCREEN <Enter>, .
2.  Ch04InitializeRedwood.sql Windows
. (
.)
3. <Enter>,
( <> ).

C:\<>Ch04InitializeRedwood.sql

START C:\<>
Ch04InitializeRedwood, .sql
START .
4. , Redwood Realty,
, :

 Ora le 10g  . 227

Agents table
AgentID
FirstName
LastName
HireDate
BirthDate
Gender
WorkPhone
CellPhone
HomePhone
Title
TaxID
LicenseID
LicenseDate
LicenseExpire
LicenseStatusID

227

LicenseStatus table
( )

LicenseStatusID
StatusText

( )

( )

. 4.1. Redwood Realty

DESCRIBE Li enseStatus
DESCRIBE Agents
, , Redwood
Realty Li enseStatus Agents.
,
. Agents 
Redwood Realty, , , ,
, . Agents, Li enseStatusID, ,
Li enseStatus. Li enseStatusID, Li enseStatus
, . . . 4.1
, / . . 4.1
, Agents. ( ,
Li enseStatus,  Li enseStatusID.) CREATE TABLE, .
DDL SQL*Plus,  Ch04InitializeRedwood.sql
.
,  ,
SQL- INSERT, UPDATE DELETE. , , Ora le,
, Ora le .

 Ora le 10g  . 228

228

4.

4.1.

Agents

()

AgentID

INTEGER

Gender
Title

Li enseStatusID

NVARCHAR2(10) :
M, m, F f
NVARCHAR2(20) :

INTEGER


salesperson ()
broker ()
(

)
;
,

Li enseStatus
(Li enseStatusID)

INSERT,
:
, ;
, ;
, .

INSERT :
INSERT INTO <table-name> [( olumn, olumn,...)
VALUES (expression, expression, ...);
<table-name> ,

. , . - ,
, . , NOT NULL
( ). - , INSERT
.

 Ora le 10g  . 229

229

,
INSERT . , ,
,
CREATE TABLE. ,
NULL. , Li enseStatus , :

INSERT INTO Li enseStatus VALUES (1001, 'Li ensed');


INSERT INTO Li enseStatus VALUES (1004, 'De eased');
INSERT INTO Li enseStatus VALUES (1003, NULL);
, Ora le Li enseStatusID, () 
StatusText. ,
1003: Ora le NULL, ,
StatusText, Li enseStatusID = 1003, . StatusText NVARCHAR2
( ), . ,
, , . , ,
, . ,
'Realtor''s li ense suspended'. ,
.
,
 INSERT INTO:

INSERT INTO <table-name> ( olumn, olumn,...)


VALUES (expression, expression,...);
INSERT ,
, ()
VALUES. INSERT
, CREATE TABLE.
VALUES INSERT
INTO. , , Agents.
, , 
( AgentID).

INSERT INTO AGENTS(AgentID, LastName, FirstName)


VALUES (12345, 'Fro kmeister', 'Robert');
INSERT INTO AGENTS(LastName, TaxID, Li enseStatusID, AgentID)
VALUES ('Kellog', '1234-rtu', NULL, 40335);
INSERT INTO AGENTS(AgentID, Gender, LastName, BirthDate)
VALUES(21334, 'f', 'Pennypa ker', To_Date('04-JUN-1979');
,
. -

 Ora le 10g  . 230

230

4.

. 
.

, , ,
, , .
Agents,
.
1. SQL*Plus Ora le, SQL*Plus Ora le.
2. ,  SQL,
. 
Ch4<>Insert41.sql, :

INSERT INTO Agents (AgentID, FirstName, LastName)


VALUES (10235, 'Tobias', 'Carling');
3. SQL*Plus <Enter>, . Ora le (. 4.2).
4.
SQL*Plus, :

INSERT,

INSERT INTO Agents (LastName, AgentID, Gender, Title, FirstName)


VALUES ('O''Malley', 14556, 'M', 'Salesperson', 'Sean');
. O  O'Malley .
, , , , Ora le
.

, , ,
. , Agent
1006 Li enseStatusID, Li enseStatus , 1006.
 . -, ,
, . -, NULL

 Ora le 10g  . 231

. 4.2.

231

Agents

. , ,
. , :
, ,
, .
Agent , , 
Li enseStatus.
,
, .
1. , .
File () New (), .
 Ch4<>IntegrityTest42.sql ( <> .)
2. SQL ,
SQL*Plus, :

INSERT INTO Agents (AgentID, LastName, Li enseStatusID, Gender)


VALUES (14883, 'Fernandez', 1001, 'F');
Ora le , 1001
Li enseStatus . , Fernandez, , Ora le .
3. NULL, 1001
VALUES.  INSERT
SQL*Plus . Ora le
(. 4.3).

 Ora le 10g  . 232

232

4.

. 4.3.

 INSERT , , , NULL. - Ora le


,
Li enseStatus. ,
.
Li enseStatus. 
NULL Li enseStatusID,
Li enseStatusID, Li enseStatus.

INSERT INTO : (1)


; (2) ,
CREATE TABLE. , , .
 ,
. Li enseStatusID Agents
Li enseStatus,
Li enseStatus .
, , ,
.
1. , SQL*Plus, .
,
SQL*Plus.  .

 Ora le 10g  . 233

233

INSERT INTO Li enseStatus VALUES (1001, 'Li ensed');


INSERT INTO Li enseStatus VALUES (1002, 'Li ensed NBA');
INSERT INTO Li enseStatus VALUES (1003, 'Can eled Offi er');
2. Ora le , .
3.  Ch04InsertLS.sql, , SQL*Plus INSERT. (   START.) Ora le 13
Li enseStatus.
4. SQL*Plus
SQL*Plus.

Exit <Enter>, Ora le

5. Web-, iSQL*Plus. iSQL*Plus, . ( ,


SQL*Plus. , iSQL*Plus , 
, ;
, iSQL*Plus .)
6. SELECT * FROM Li enseStatus Workspa e Exe ute, Li enseStatus
(. 4.4).
7. Logout .
Agents.
INSERT INTO, 
, .
Agents, .
1. SQL*Plus, , .
2.  Ch04InsertAgents.sql,
, START, Agents.  .
 , . 4.5.
. START   .sql 
. Ora le ,  .sql   .

START C:\Ch04InsertAgents
3. Ora le 1 row reated, ,
.  ,
.

 Ora le 10g  . 234

234

4.

. 4.4.

Li enseStatus

4. Agents,
SQL*Plus SQL:

CLEAR SCREEN
SET PAGESIZE 35
COLUMN FirstName FORMAT A10
COLUMN LastName FORMAT A10
COLUMN Gender FORMAT A6
SELECT AgentID, FirstName, LastName, Gender
FROM Agents
ORDER BY LastName;
 , . 4.5.

,
. , ,  /
. ,
INSERT, Ora le . SQL, ,
 , .

 Ora le 10g  . 235

. 4.5.

235

Agents

Ora le  (

)

DD-MON-YY DD-MONYYYY. ,
AgentID BirthDate, INSERT (
 ):

INSERT INTO Agents (AgentID, BirthDate) VALUES (99887,'12-OCT-1970')


INSERT INTO Agents (AgentID, BirthDate) VALUES (55678,'08-JUN-82')
,
.
0 49, , XXI . , 50, , XX . , '20-OCT-35'
20 2035 , '04-JUL-51'  4 1951 . ,
.

, .
  - , , .
 . -

 Ora le 10g  . 236

236

4.

4.2.  ,

MONTH
Month
MM
DD
DDD
DAY
Day
DY
YYYY
YY

FEBRUARY
February
02
15
251
WEDNESDAY
Wednesday
WED
2006
06

,
. DATE,
 ,
 Ora le TO_DATE,
, Ora le.  TO_DATE :
TO_DATE('date string', 'date format model')
date string ,
, date format model   . , INSERT  HireDate AgentID
, , .

INSERT INTO Agents (HireDate, AgentID)


VALUES (TO_DATE('July 14, 2005', 'Month DD, YYYY'), 77777);
INSERT INTO Agents (AgentID, HireDate)
VALUES (88888, TO_DATE('10/17/1946', 'DD/MM/YYYY');
/ 
Ora le, . date format model (/), 
(-) (:), , date string. . 4.2
 .

Agents, .
Agents, .

 Ora le 10g  . 237

237

. 4.6.

1. , SQL*Plus ,
. .
2. SQL,
SQL*Plus, ( , ,
SQL*Plus ):

INSERT INTO Agents(AgentID, HireDate, BirthDate)


VALUES(23456,TO_DATE('January 12, 2004', 'Month DD, YYYY'),
'10-JUN-1981');
INSERT INTO Agents(HireDate, AgentID)
VALUES(TO_DATE('2004/02/03', 'YYYY/MM/DD'),56789);
3. , <Enter>,
SQL. Ora le 1 row reated.
4. ,
( SELECT 5):

SELECT

SELECT AgentID, BirthDate, HireDate


FROM Agents WHERE AgentID > 16000;
 SQL . 4.6.
. SQL*Plus,
.
,
 HH, MI, SS, AM PM, , , (AM PM
). , (, ),
INSERT:

INSERT INTO BoatRa e(Ra eID, BeginTime)


VALUES(1237, TO_DATE('08:20:25 AM', 'HH:MI:SS AM');

 Ora le 10g  . 238

238

4.

, Rowing
Ventures,  
, , .

INSERT SELECT,
.
, ,
.
. ,
. ,
NevadaRealtors Agents. ,
NevadaRealtors AgentID
Agents,  ,
INSERT SELECT
NevadaRealtors, .

INSERT INTO Agents(AgentID, FirstName, LastName, BirthDate,


Li enseID)
SELECT RealtorID, Fname, Lname, DOB, Li Numb
FROM NevadaRealtors
WHERE LOWER(HomeCity) = 'winnemu a';
Ora le NevadaRealtors
, HomeCity ( ) winnemu a.
(Agents)
. NevadaRealtors 14 , Ora le
Agents. 
WHERE. 
INSERT, , , .
Agents ,
.
1.  Ch04NevadaRealtors.sql, , (
):

START C:\Ch04NevadaRealtors
2. SQL*Plus . , , SQL*Plus.
<Enter>, .

 Ora le 10g  . 239

239

. 4.7.

INSERT INTO Agents(AgentID, FirstName, LastName, BirthDate,


Li enseID)
SELECT RealtorID, Fname, Lname, DOB, Li Numb
FROM NevadaRealtors
WHERE LOWER(HomeCity) = 'winnemu a';
, Ora le 5
ated.

rows re-

3. , SQL- SELECT:

SELECT AgentID, FirstName, LastName


FROM Agents WHERE AgentID > 515432;
 . 2 3 . 4.7.
4. , NevadaRealtors, SQL:

DROP TABLE NevadaRealtors CASCADE CONSTRAINTS PURGE;


Ora le , Table dropped.
5. Exit <Enter>, Ora le
SQL*Plus.

 Ora le 10g  . 240

240

4.

(sequen e) ,
,
, . , , , , , . ,
, ,
.  , , ,
 (, ). ,
, .
, INSERT .
, . , , 1027 ,
 .

, SQL CREATE SEQUENCE,



, . CREATE SEQUENCE :

CREATE SEQUENCE <sequen e name>


[START WITH <value>
[INCREMENT BY <value>
[{MAXVALUE <value> | NOMAXVALUE}
[{MINVALUE <value> | NOMINVALUE}
[{CYCLE | NOCYCLE}
[{ORDER | NOORDER}
[{CACHE <value> | NOCACHE};
 ,  
, .
,
.
,  _seq.
START WITH .
, Ora le . INCREMENT BY ,

 Ora le 10g  . 241

241

, Ora le .
( -)
( -) , . MINVALUE MAXVALUE .
MINVALUE , MAXVALUE  INCREMENT BY. CYCLE/NOCYCLE ,
Ora le . CYCLE ,
, NOCYCLE ( )  Ora le .
ORDER , : ,
 .. NOORDER. CACHE/NOCACHE
, Ora le
, ,
. NOCACHE ( ) ,
. , ,
, , ,
, , (
)
,
()
Agents. INSERT, , .
654321, ,  ,
.
, .
1. SQL*Plus, , . . SQL*Plus <Enter>,
SQL*Plus. , .
2. SQL*Plus

CLEAR SCREEN <Enter>.

3. SQL*Plus :

CREATE SEQUENCE AgentID_seq

 Ora le 10g  . 242

242

4.

START WITH 654321


INCREMENT BY 2 NOCACHE NOCYCLE;
4. <Enter>, . Ora le
Sequen e reated.
Ora le , ,

USER_SEQUENCES.
,
, .
1. , 
USER_SEQUENCES:

DESCRIBE user_sequen es
2. :

SELECT sequen e_name, min_value, max_value, last_number


FROM user_sequen es;
3. , . 4.8.
, MAXVALUE AGENTID_SEQ ( Ora le ) 1027  !

AgentID_seq,
, - .  , Agents.
: NEXTVAL CURRVAL. Ora le , , . NEXTVAL (
)  ,
. , CURRVAL ( ). , ,
, , , . () ,
INSERT.

 Ora le 10g  . 243

243

. 4.8.

<sequen ename>.NEXTVAL
, SQL:

<sequen ename>.CURRVAL
Agents ,  .
.
, AgentID_seq, .

INSERT:
INSERT INTO Agents (AgentID,FirstName,LastName)
VALUES (AgentID_seq.NEXTVAL,'Angeli a','Fran is');
INSERT INTO Agents (AgentID,FirstName,LastName)
VALUES (AgentID_seq.NEXTVAL,'Hershel','Wi kstrom');
INSERT INTO Agents (AgentID,FirstName,LastName)
VALUES (AgentID_seq.NEXTVAL,'Bryan','Wilson');

1.

 Ora le 10g  . 244

244

4.

. 4.9. (

CURRVAL)

2. , . .
?

SELECT AgentID, FirstName, LastName


FROM Agents
WHERE AgentID > 123456
ORDER BY AgentID;
3. ,
CURRVAL AgentID_seq:

SELECT AgentID_seq.CURRVAL
FROM DUAL;

DUAL  Ora le ,
. , (, ), - .
DUAL 5.
 SELECT 2 3 . 4.9.
, , AgentID AgentID_seq, 654321, 654323 654325  .  .

 Ora le 10g  . 245

245

ALTER
SEQUENCE. , . ALTER SEQUENCE : START WITH ,
. ,
, MAXVALUE ,
. ALTER
SEQUENCE :

ALTER SEQUENCE <sequen ename>


[INCREMENT BY <value>
[{MAXVALUE <value> | NOMAXVALUE}
[{MINVALUE <value> | NOMINVALUE}
[{CYCLE | NOCYCLE}
[{ORDER | NOORDER}
[{CACHE <value> | NOCACHE};
,
DROP SEQUENCE. :

DROP SEQUENCE <sequen ename>;


AgentID_seq.
, , .
1. SQL:

DROP SEQUENCE AgentID_seq;


Ora le Sequen e dropped.
2. SQL*Plus .


, , 
Redwood Realty, ,  .  Redwood Realty
, .. Li enseExpire.
. ,
, .  , -

 Ora le 10g  . 246

246

4.

4.3. Ora le

=
<> !=
>
<
<=
>=

 . SQL UPDATE.
UPDATE

 SQL UPDATE. UPDATE, 


. UPDATE, (a tion query), :

UPDATE <tablename>
SET < olumnname1> = <expression1>
[, < olumnname2> = <expression2>, ...
[WHERE < ondition>;
<tablename> , , < olumnname>  , <expression> 
, , .
WHERE
. ,
. < ondition> , ,
. WHERE true false. Ora le,
, ,
. 4.3.
Ora le . 4.4.
.
, (Tobias Carling),
NULL,  (. . 4.2) , Title, Li enseDate ..
 , UPDATE, FirstName LastName,


 Ora le 10g  . 247

247

4.4. Ora le

AND

WHERE

True,
true; false 
True,
true; false 

OR
NOT
IN


True,

LIKE

,
, 

True,
()

BETWEEN ...
AND ...

WHERE State = 'MN' AND


Gender ='M'
WHERE Li enseStatusID
= 1001 OR Li enseStatusID = 1002
WHERE NOT State = 'NE'
WHERE City
IN('Ar ata', 'Fortuna',
'Ori k')
WHERE LastName LIKE
'M %'
WHERE SqFt BETWEEN
1500 AND 2000

. , ,
HireDate, BirthDate, Gender Title. ,
,  .
UPDATE,
.
1. SQL*Plus , 
, Carling.
,
. , , SQL*Plus .

CLEAR SCREEN
COLUMN FirstName FORMAT A10
COLUMN LastName FORMAT A10
COLUMN Gender FORMAT A6
SELECT FirstName, LastName, HireDate, BirthDate, Gender, Title
FROM Agents
WHERE FirstName = 'Tobias' AND LastName = 'Carling';
.  , , 
. Ora le
.

 Ora le 10g  . 248

248

4.

2. , , WHERE UPDATE  .
, ,
() . :

UPDATE Agents
SET HireDate = TO_DATE('12/19/2000', 'MM/DD/YYYY'),
BirthDate = TO_DATE('10/19/1975', 'MM/DD/YYYY'),
Gender = 'M',
Title = 'SALESPERSON'
WHERE FirstName = 'Tobias' AND LastName = 'Carling';
Ora le 1

row updated.

3. , SELECT, Carling. SQL*Plus


, 1, .

SELECT FirstName, LastName, HireDate, BirthDate, Gender, Title


FROM Agents
WHERE FirstName = 'Tobias' AND LastName = 'Carling';
(. 4.10), Ora le ,
 . 2.
4. SQL*Plus .
UPDATE . WHERE,
Ora le . , , UPDATE WHERE.
. , , ROLLBACK. ,  ROLLBACK, ,
COMMIT   .
(Lora Allee), Redwood Realty,
, Title Agents . , , , 
(AgentID) 515602, Title
Salesperson (). 
, .
? , - ( ),
. AgentID,
UPDATE WHERE.

 Ora le 10g  . 249

249

. 4.10.

, ,
.
1. SQL*Plus :

UPDATE Agents
SET Title = 'Salesperson'
WHERE AgentID >= 515602;
Ora le 8

rows updated.

Title
,  , Salesperson Broker.
, , . 
UPDATE.
 Ora le UPPER, Title .
UPDATE WHERE,  .
 , .

 Ora le 10g  . 250

250

4.

. 4.11. -

1.

CLEAR SCREEN <Enter>, .

2. .

UPDATE Agents
SET Title = UPPER(Title);
Ora le 40

rows updated.

3. ,
.

SELECT FirstName, LastName, Title


FROM Agents;
,
(. . 4.11).

Title -

 Ora le 10g  . 251

251

CASE

,
. , 
, ( ) .
:
, .
if-then-else, , ,
, CASE. , Ora le
SQL. CASE
:

CASE
WHEN < ondition1> THEN <result1>
WHEN < ondition2> THEN <result2>
...
[ELSE <default result>
END
Ora le WHEN, .
WHEN, ,
true. Ora le , true,
, THEN,
WHEN/THEN. true, Ora le
, ELSE. , ELSE
WHEN true, CASE
NULL.
, SQL ,
CommissionRate.
Paygrade SET, CASE
CommissionRate. , CommissionRate,
Paygrade.

UPDATE AgentPayGrade
SET CommissionRate
CASE
WHEN Paygrade =
WHEN Paygrade =
WHEN Paygrade =
ELSE 0.09
END
WHERE PositionType =

=
1 THEN 0.06
2 THEN 0.075
3 THEN 0.08
'SALESPERSON';

Paygrade - 1, CommissionRate 0.06; Paygrade 2, CommissionRate 0.075, ..

 Ora le 10g  . 252

252

4.

CASE

CASE UPDATE,
,
, . , , -
Redwood Realty ,
. , , 1
2002 , ( Li enseDate). ,
1 2000 . 31 2001 ., 4
( Li enseDate).
, . ,
UPDATE CASE, 
. ,
 Ora le ADD_MONTHS, :

ADD_MONTHS(<date olumn>,<months>)
<date olumn> , ,
, <months>,  , , .
, 60
(5 12 ).
, , CASE,
.
1. , SQL*Plus, ,
,  Ch04ListAgents.sql.
SQL*Plus START C:\Ch04ListAgents <Enter>,
. C:\.
Agents,
Li enseDate (. 4.12).
2. , SQL, SQL*Plus
:

UPDATE Agents
SET Li enseExpire = CASE
WHEN Li enseDate >='01-JAN-02'
THEN ADD_MONTHS(Li enseDate,60)
WHEN Li enseDate >='01-JAN-00'
THEN ADD_MONTHS(Li enseDate,48)
ELSE Li enseExpire
END;
3. Ora le , /, 40 rows
updated, .

 Ora le 10g  . 253

. 4.12.

253

Agents

4. , 
. <Enter> .

START C:\Ch04ListAgents
5. , . 4.12, Li enseExpire (. 4.13). ,
,
. 4.12 4.13. , Li enseExpire David Gagnon 14-MAR-04, ..
Li enseDate.
6. Exit, <Enter>, Ora le,
SQL*Plus.

 Ora le 10g  . 254

254

4.

. 4.13.

Agents

, UPDATE
 . Ora le
. ,  SQL,
SQL . , SQL,
,
.
(substitution variables)
, SQL .
SQL, , Ora le . UPDATE , .
, AnnualBonus Agents, ,

 Ora le 10g  . 255

255

4.5. ,

()

 ()

12
13 24
25 48

0
500
700

49 72
73

1 000
1 500

. 
Redwood Realty. , AnnualBonus,
UPDATE. , , UPDATE
. ,
. UPDATE,
 AnnualBonus, .

UPDATE Agents
SET AnnualBonus = &BonusAmount
WHERE HireDate > &MinHireDate;
,
(&BonusAmount &MinHireDate), SQL*Plus SQL*Plus.
&BonusAmount <Enter>. SQL*Plus &MinHireDate, <Enter>.
SQL*Plus SQL  SQLPlus, Ora le.
Agents, (/) <Enter>. SQL*Plus
, Ora le . .
 (Robert Stirling) , 
Agents, AnnualBonus, , Redwood.
. 4.5.
, Redwood Realty,  Ora le: SYSDATE MONTHS_BETWEEN. SYSDATE ( ), MONTHS_BETWEEN 
.   :

MONTHS_BETWEEN(<date1>, <date2>)
, , ( ).

 Ora le 10g  . 256

256

4.

, , 
.
. ,
Redwood Realty,  MONTHS_BETWEEN :

MONTHS_BETWEEN(SYSDATE, HireDate)
, . , .
, .
1. SQL*Plus, Ora le, CLEAR SCREEN
<Enter>.
2. , 
Ch04<>SubVars.sql, . ( <>
.)
3. :

ALTER TABLE Agents


ADD(AnnualBonus NUMBER(5) DEFAULT 0);
4. SQL*Plus <Enter>,
. Ora le Table altered.
5. UPDATE Alter Table. 
, UPDATE ( ) SQL*Plus
<Enter>, . .

UPDATE Agents
SET AnnualBonus = &BonusAmount
WHERE MONTHS_BETWEEN('01-JAN-2006', HireDate)
BETWEEN &LowMonths AND &HighMonths;
SQL*Plus

BonusAmount.

6. 500 <Enter>. Ora le .


7. 13, <Enter>, 24 <Enter>.
Ora le 3 rows updated (. 4.14).
,  SQL*Plus 500, 13 24 Ora le.
,  UPDATE:

 Ora le 10g  . 257

257

. 4.14. SQL,

UPDATE Agents
SET AnnualBonus = 500
WHERE MONTHS_BETWEEN('01-JAN-2006', HireDate)
BETWEEN 13 24;
SQL*Plus SQL*Plus, ,
. ,
, SQL*Plus SET VERIFY OFF.
SQL -  , (/)
<Enter>.
, ,  SQL, , .
1. SQL*Plus

SET VERIFY OFF <Enter>.

2. / <Enter>, SQL,
.
3. 700, <Enter>,
48 <Enter>.

25, <Enter>,

4. / <Enter>; 1000, <Enter>,


49, <Enter>, 72 <Enter>.
5. / <Enter>; 1500, <Enter>,
73, <Enter>, 678 <Enter>
(. 4.15). ( 678 ,  .)

 Ora le 10g  . 258

258

4.

. 4.15.

6. SET VERIFY ON, , .


7. 
:

Agents, SQL*Plus -

CLEAR SCREEN
SELECT LastName, HireDate, AnnualBonus
FROM Agents
WHERE HireDate IS NOT NULL
ORDER BY HireDate;
8. , AnnualBonus .
HireDate (. 4.16).
9. Exit <Enter>, Ora le
SQL*Plus.
, (AnnualBonus) 
(Robinson) . , 13 , ,
1 2006 . , .
SQL*Plus ACCEPT,  PROMPT, -

 Ora le 10g  . 259

259

. 4.16.

, . ACCEPT
, , , .
ACCEPT PL/SQL, 7,
.


, , DELETE ( DELETE).
( DROP TABLE). , DELETE  SQL ,
. :

DELETE [FROM [s hema.{<tablename> | <view>}


[WHERE < ondition>;

 Ora le 10g  . 260

260

4.

, DELETE ,
. FROM , . s hema
. ( 6.)
WHERE  , .. , , < ondition>. :
DELETE , . ,
WHERE, Ora le
. , , , , - Redwood
Realty! , SQL ROLLBACK, ,  .
, , ( ), Ora le ,
. (referential integrity)  ,
. ,
ON DELETE CASCADE. ,
.
Li enseStatus Agents. Agents Li enseStatusID,
Li enseStatus.
Li enseStatus,
1001, Ora le
Agents, 1001.
, ON DELETE CASCADE,
.

, Agents. .
, .
1. SQL*Plus Ora le ( ),
<Enter>.
2.

DELETE:

DELETE FROM Agents


WHERE FirstName IS NULL AND LastName IS NULL;

CLEAR SCREEN

 Ora le 10g  . 261

261

Ora le - 2 rows deleted.  (FirstName LastName


null). .
3. :

DELETE FROM Agents


WHERE AgentID = 515602;
, Ora le , .
4. :

DELETE FROM Agents


WHERE Title = 'Salesperson';
Ora le 0 rows deleted.
, DELETE ,
. , , .
, Title .
 .
, Redwood Realty , .. , 2006 .
DELETE, , .
, HireDate,
.
1. :

DELETE FROM Agents


WHERE HireDate >= TO_DATE('2006/01/01', 'YYYY/MM/DD');
Ora le .
2. , HireDate (
:

NULL).

DELETE FROM Agents


WHERE HireDate IS NULL;
Ora le (. 4.17), ..
, HireDate NULL.
3. SQL*Plus .

 Ora le 10g  . 262

262

4.

. 4.17.

, , .
WHERE DELETE,
. , Agents,
.

DELETE FROM Agents;


, ,  , Ora le
, . , ,
TC1 - COMMIT,
.
, ,
. , ,
, . , ,
, ,
.
DELETE TRUNCATE TABLE. Ora le , ,
. SQL Server (trun ate) , , .
TRUNCATE TABLE :
1

Transa tion Control  .

 Ora le 10g  . 263

. 4.18.

263

Agents

TRUNCATE TABLE <tablename>;


, ( ON DELETE CASCADE), .
, ALTER TABLE. ,
Agents,

ALTER TABLE Agents


DISABLE CONSTRAINT fk_AgentsLi enseStatusID;

Agents, TRUN-

CATE TABLE.

,
, .
1. SQL*Plus

ALTER TABLE Agents


DISABLE CONSTRAINT fk_AgentsLi enseStatusID;
2. ,

TRUNCATE TABLE Agents;


Ora le Table

trun ated (. 4.18).


SQL MERGE ,  INSERT, UPDATE DELETE.
MERGE :

MERGE INTO <target-tablename>


USING <sour e-tablename or query>
ON (< ondition>)
[WHEN MATCHED THEN UPDATE <set- lause>
[DELETE < ondition>
[WHEN NOT MATCHED THEN INSERT <insert- lause>;

 Ora le 10g  . 264

264

4.

, MERGE,
. , , .
, , . , , ,
, ,  DML2 - INSERT, UPDATE DELETE. , MERGE,
, DML-. MERGE UPDATE INSERT,
 upsert (UPdate + inSERT). , .
MERGE INTO , Ora le .
USING ON ( )
( ),
. WHEN MATCHED ,
USING ON, .. .
WHEN NOT MATCHED , Ora le,
.
, , .
Li enseStatus , . 4.19 (  Li enseStatus). UpdateLi enseStatus (  UpdateLi enseStatus
. 4.19) , Li enseStatus, ( Li enseStatusID Li enseStatus),
, Li enseStatus (.  Li enseStatus MERGE . 4.19).
MERGE, Li enseStatus UpdateLi enseStatus.

MERGE INTO Li enseStatus LS


USING UpdateLi enseStatus ULS
ON (LS.Li enseStatusID = ULS.Li enseStatusID)
WHEN MATCHED THEN UPDATE SET LS.StatusText = ULS.StatusText
WHEN NOT MATCHED THEN INSERT (LS.Li enseStatusID, LS.StatusText)
VALUES (ULS.Li enseStatusID, ULS.StatusText);
LS ULS . (alias) .

Data Manipulation Language  .

 Ora le 10g  . 265

UpdateLicenseStatus
1004 Passed Away
1005 Expired--Fee Not Paid
1101 License Probationary
1105 License Pending
MERGE
LicenseStatus
1001 Licensed
1002 Licensed NBA
1003 Canceled Officer
1004 Deceased
1005 Expired
1006 Government Service
1007 Military Service
1008 Conditional Suspension
1009 Restricted
1010 Revoked
1011 Flag Suspended
1012 Voided
1013 Withheld Denied
1014 17520 FC Suspended
1015 11350.6 W and I Suspended
1016 Surrendered

265

UpdateLicenseStatus MERGE
1001 Licensed
1002 Licensed NBA
1003 Canceled Officer
1004 Passed Away

1005 Expired--Fee Not Paid

1006 Government Service
1007 Military Service
1008 Conditional Suspension
1009 Restricted
1010 Revoked
1011 Flag Suspended
1012 Voided
1013 Withheld Denied
1014 17520 FC Suspended
1015 11350.6 W and I Suspended
1016 Surrendered
1101 License Probationary
1105 License Pending

. 4.19.

MERGE


. LS , Li enseStatus, , ULS 
UpdateLi enseStatus, .
MERGE Ora le ,
. WHEN
NOT Ora le ,
. , WHEN MATCHED DELETE.
(Stirling Leonard) , Li enseStatus.
1004 1005 Passed Away Expired Fee Not Paid Li enseText. , 
 UpdateLi enseStatus
(. . 4.19). , 1101 1105, Li enseStatus, Ora le Li enseStatus.
UpdateLi enseStatus,
MERGE, .

 Ora le 10g  . 266

266

4.

1. SQL*Plus ( CLEAR SCREEN <Enter>). ,


, , Li enseStatus. , ,
SQL*Plus.

CREATE TABLE UpdateLi enseStatus


(Li enseStatusID INTEGER PRIMARY KEY,
StatusText NVARCHAR2(25));
Ora le , : Table

reated.

2. UpdateLi enseStatus INSERT ( <Enter>):

INSERT INTO UpdateLi enseStatus


INSERT INTO UpdateLi enseStatus
'Expired--Fee Not Paid');
INSERT INTO UpdateLi enseStatus
'Li ense Probationary');
INSERT INTO UpdateLi enseStatus

ated.

VALUES (1004, 'Passed Away');


VALUES (1005,
VALUES (1101,
VALUES (1105, 'Li ense Pending');

INSERT Ora le 1 row re-

, , 
UpdateLi enseStatus, MERGE.
MERGE,
, .
1. ,
SQL*Plus ( ,  ):

MERGE INTO Li enseStatus LS


USING UpdateLi enseStatus ULS
ON (LS.Li enseStatusID = ULS.Li enseStatusID)
WHEN MATCHED THEN
UPDATE SET LS.StatusText = ULS.StatusText
WHEN NOT MATCHED THEN
INSERT (LS.Li enseStatusID, LS.StatusText)
VALUES (ULS.Li enseStatusID, ULS.StatusText);
Ora le - 4

rows merged (. 4.20).


2. Li enseStatus,

SELECT *
FROM Li enseStatus
ORDER BY Li enseStatusID;

 Ora le 10g  . 267

. 4.20.

267

MERGE 

3. , . 4.19. , 1101 1105.


1004 1005  .

UpdateLi enseStatus,
DROP TABLE UpdateLi enseStatus;

4.

5. Ora le SQL*Plus,

Exit <Enter>.


,
(Data Manipulation Language  DML),  , .
SQL,
 , . , , -, .
, ( )
, .

 Ora le 10g  . 268

268

4.

( ) . (
) , . , - .
 UPDATE,
, ,
. INSERT, DELETE UPDATE.
COMMIT, ROLLBACK SAVEPOINT.
DML- (, INSERT
DELETE), . , Ora le , (
Ora le)  , . ,
,
. , , ,
. - . , Ora le SQL*Plus
iSQL*Plus, ,  .
.

COMMIT .
Ora le  .
COMMIT ,  . (Data Denition Language 
DDL), CREATE TABLE, CREATE SEQUENCE DROP TABLE,
(Data Control Language  DCL), GRANT, Ora le COMMIT. , ,
 -
COMMIT. , Ora le COMMIT,
. , 
COMMIT 25 12
, Ora le.
DML  ( ommit) , . 
, .
COMMIT :

COMMIT;

 Ora le 10g  . 269

269

E
S

IN

O
P
B

A
T

..
T

..

..
T

E
T
A

..
T

..

..
T

;
IT

M
M

E
S

E
T
E

E
S

IN

IN

E
S

;
IT

M
O

IN

E
L
.

..

. 4.21.

COMMIT . . 4.21
, DML
.

(rollba k) .
,
, .
SQL ROLLBACK,
DML, ,  . ROLLBACK
, , .
, .
, ROLLBACK DML-,
DDL-. DDL- (CREATE TABLE ..)
DROP.
ROLLBACK :

ROLLBACK [WORK [TO [SAVEPOINT <savepoint-name>;


<savepoint-name>  , (  ,  ). ,
ROLLBACK TO SAVEPOINT Alpha23, Alpha23  ,
. :
, , -

;
,

Alpha23 , ;

, ,

 Ora le 10g  . 270

270

4.

ROLLBACK COMMIT SQL,


. , .
Ora le DML.
,

DML.
.
DML, . Ora le -

ROLLBACK , .
COMMIT ROLLBACK.

Ora le.

GRANT.
DDL-, CREATE TABLE.

DCL-,

,
.
, ,
, .
DML-. SAVEPOINT, , :

SAVEPOINT <savepointname>;
ROLLBACK, ,
DML-
. . 4.22 , DML-,
SAVEPOINT ROLLBACK.
. 4.22  .
, SaveFirst, 
 (Carling). 
 (Voss) , SaveSe ond.
(Title) Salesperson ().
ROLLBACK , , .. (Broker). SELECT
, ,
, UPDATE, . ,

 Ora le 10g  . 271

271

. 4.22.

COMMIT () , INSERT.


, Ora le
Agents, TRUNCATE TABLE Agents.
ROLLBACK COMMIT, (Transa tions Control
Language  TCL). ,
, SQL*Plus, . Ora le
, ,
SQL*Plus .
, DML-,
, .
1. SQL*Plus, Ora le SQL*Plus,  ( <Enter>
):

CLEAR SCREEN
COLUMN AgentID FORMAT 999999

 Ora le 10g  . 272

272

4.

COLUMN FirstName FORMAT A12


COLUMN LastName FORMAT A12
COLUMN Gender FORMAT A6
2. :

INSERT INTO Agents (AgentID, FirstName, LastName, Gender)


VALUES (10429, 'Elizabeth', 'Dahlen', NULL);
SAVEPOINT InsertOne;
INSERT INTO Agents (AgentID, FirstName, LastName, Gender)
VALUES (10497, 'Ramanathan', 'Rowe', 'M');
INSERT INTO Agents (AgentID, FirstName, LastName, Gender)
VALUES (10849, 'Heather', 'Sheibani', NULL);
UPDATE Agents SET Gender = 'M' WHERE LastName = 'Dahlen';
SAVEPOINT InsertTwo;
, Ora le 1 row reated
Savepoint reated INSERT SAVEPOINT
.

Agents,
SELECT AgentID, FirstName, LastName, Gender FROM Agents;

3.

,
NULL (. . 4.23).

Gender Sheibani

4. . . InsertOne, ..
, INSERT
UPDATE.

CLEAR SCREEN
ROLLBACK TO InsertOne;
SELECT AgentID, FirstName, LastName, Gender FROM Agents;
5.  , SQL*Plus
:

COMMIT;
SELECT AgentID, FirstName, LastName, Gender FROM Agents;
ROLLBACK;
SELECT AgentID, FirstName, LastName, Gender FROM Agents;
 . 4.24. ,
COMMIT Ora le ,
.
6. Agents, Exit <Enter>, Ora le. , , Ora le
COMMIT:

DELETE FROM Agents;


exit

 Ora le 10g  . 273

. 4.23. 

273

Agents


(trigger) , ( 6), .
() , , ,
. , . ,
, DML- INSERT,
UPDATE DELETE.
, . Ora le  PL/SQL.
Ora le .

, , Ora le .
,

 Ora le 10g  . 274

274

4.

. 4.24.

COMMIT

,
. , , Redwood Realty , Li enseDate
,  . ,
, ,  Agents.
  . , Ora le ,  . Ora le INSERT, UPDATE
DELETE, , ,
 . , , Ora le,  ,
,  , ,
. , ,
 .
, DML.
, ,
, , ,
,  . ,
DML, ,  .

 Ora le 10g  . 275

275

, ,
( ). ,
, (row-level trigger), , DML , (statement-level
trigger). , , , .
, .
, CREATE TRIGGER,
:

CREATE [OR REPLACE TRIGGER <trigger-name>


{BEFORE | AFTER | INSTEAD OF} <trigger-event> ON <table_name>
[FOR EACH ROW [WHEN < ondition>
[DECLARE
<de laration statements>
BEGIN
<trigger-body>
END;
CREATE , ,
REPLACE , . REPLACE ,  . Ora le
. , , , , Ora le
. CREATE TRIGGER.
<trigger name>  . BEFORE AFTER ,
 . INSTEAD OF , DML,
(  6).
. :
<table-name>_{bi|bu|bd|ai|au|ad}_trg, <table-name>  ,
trg  trigger ( ), bi  before insert ( INSERT), bu  before update ( UPDATE), ai  after insert (
INSERT) .. , AFTER UPDATE,
Agents, Agents_au_trg. ,
. ,
,
( ).

 Ora le 10g  . 276

276

4.

. <trigger event> , ; INSERT, UPDATE, DELETE


, OR. <table-name>
, . FOR
EACH ROW , , ,
.. . , , .  WHEN < ondition> ,  .
<trigger-body> PL/SQL, , , .
, . . ,
, (COMMIT, ROLLBACK SAVEPOINT). ,
, .  , .

, .
,  , ( orrelation name), .
.
:OLD ( OLD)
SQL, . , :NEW
( NEW) , SQL, .
, , BEFORE UPDATE
Agents. UPDATE Agents,
BEFORE UPDATE.
:NEW :OLD .
Li enseExpire, , , :OLD.Li enseExpire, ()
 :NEW.Li enseExpire. OLD NEW
INSERT, UPDATE DELETE . 4.6.
BEFORE

(.  )
Agent_ID,
Agents. ,

 Ora le 10g  . 277

277

4.6. ,

SQL

NEW OLD

INSERT

NEW

,
,

UPDATE

OLD
NEW

NULL

OLD

NEW
OLD

NULL

DELETE

,
,

, , INSERT , ..

- . .
BEFORE

, Redwood
Realty Agents, AgentID.
BEFORE INSERT. , ,
.
,
.
1. SQL*Plus, Ora le, :

CLEAR SCREEN
DROP SEQUENCE AgentID_seq;
CREATE SEQUENCE AgentID_seq
START WITH 12345
INCREMENT BY 2;
, DROP Ora le , .
 , ,
, .. . ,
Ora le Sequen e reated.

 Ora le 10g  . 278

278

4.

. 4.25.

BEFORE INSERT

2. , <Enter>
. (/).
<Enter>. Ora le
.

CREATE OR REPLACE TRIGGER Agents_bi_trg


BEFORE INSERT ON Agents FOR EACH ROW
BEGIN
SELECT AgentID_seq.NEXTVAL INTO :NEW.AgentID FROM DUAL;
END;
/
Ora le , , Trigger
ated (. 4.25).

re-

. Ora le Warning: Trigger reated


with ompilation errors (: ), , SELECT.
, . 2.

. ,

SHOW ERRORS TRIGGER <trigger-name>

<trigger-name> .

3. SQL*Plus .

 Ora le 10g  . 279

279

, .
(Agents_bi_trg), BEFORE INSERT. ,
INSERT INTO Agents  , Agents. BEGIN END.
(AgentID_seq.NEXTVAL)
AgentID :NEW. Ora le NEW OLD , INSERT
UPDATE, . SELECT Ora le DUAL,    .
NEW, Ora le , INSERT, NEW
Agents.
BEFORE

, Agents
(  ).
, .
1. SQL*Plus,

INSERT:

INSERT INTO Agents(LastName, FirstName)


VALUES('Mar oux', 'Kai');
INSERT INTO Agents(LastName, FirstName)
VALUES('Silverburg', 'Danial');
INSERT INTO Agents(LastName, FirstName)
VALUES('Taylor', 'Jessi a');
INSERT Ora le 1

row reated.

2. SELECT,
.
, .

SELECT AgentID, FirstName, Lastname


FROM Agents;
 SELECT . 4.26. , ,  12345, 12347 12349.
3. exit <Enter>, Ora le
SQL*Plus.  ,
Ora le.

 Ora le 10g  . 280

280

4.

. 4.26.

Agents

AFTER,

Ora le , . UPDATE, INSERT DELETE


,  .
  (  audit trail) ,
.  ,  (, ), ,
, .
 , UPDATES Agents ( ). ,
Agents, UPDATE Agents, 
, Ora le :
(  INSERT), , , . , , Agents: AgentID, FirstName, LastName,

 Ora le 10g  . 281

281

HireDate, BirthDate, Gender Title. , ,


. 
,
, INSERT DELETE.

, ,  Agents. ,
UPDATE
Agents. AuditAgents.
,  Agents, .
1. SQL*Plus, Ora le, CLEAR
SCREEN.
2. , SQL , SQL*Plus. SQL*Plus. ,
SQL*Plus  .
3. , SQL*Plus , AuditAgents:

CREATE TABLE AuditAgents


(AuditEvent NVARCHAR2(6),
BeforeAfter NVARCHAR2(6),
AuditUsername NVARCHAR2(30),
AuditDate DATE,
AgentID INTEGER,
FirstName NVARCHAR2(30),
LastName NVARCHAR2(30),
HireDate DATE,
BirthDate DATE,
Gender NVARCHAR2(1),
Title NVARCHAR2(11));

4.  , <>
CreateAudit.sql. Save as type ( ) All Files ( ).
AFTER

, , Agents
UPDATE.

 Ora le 10g  . 282

282

4.

.
AFTER UPDATE
Agents, .
1. , SQL*Plus:

CREATE OR REPLACE TRIGGER Agents_au_trg


AFTER UPDATE ON Agents
FOR EACH ROW
BEGIN
INSERT INTO AuditAgents
VALUES ('Update', 'Before', user, sysdate,
:OLD.AgentID, :OLD.FirstName, :OLD.LastName,
:OLD.HireDate, :OLD.BirthDate, :OLD.Gender, :OLD.Title);
INSERT INTO AuditAgents
VALUES ('Update', 'After', NULL, NULL,
:NEW.AgentID, :NEW.FirstName, :NEW.LastName,
:NEW.HireDate, :NEW.BirthDate, :NEW.Gender, :NEW.Title);
END;
2. SQL*Plus <Enter>, END,
(/) <Enter>, .
Ora le Trigger reated (. 4.27).
, .
3. .
.
Agents. Agents. :
,  .
AFTER

AFTER UPDATE, Agents UPDATE. 


Agents, , . AuditAgents,
, AFTER UPDATE. , Agents,
AuditAgents. .
 Agents AFTER UPDATE, .
1. UPDATE. , , SQL*Plus
.  .

 Ora le 10g  . 283

. 4.27.

283

AFTER UPDATE

UPDATE Agents SET Gender = 'M' WHERE AgentID = 12345;


UPDATE Agents SET Title = 'Salesperson'
WHERE LastName = 'Silverburg';
UPDATE Agents SET HireDate = '29-JUN-98' WHERE AgentID = 12349;
Ora le 1

row updated.

2. ,
. 1.
UPDATE Agents SET LastName = 'Wahlberg' WHERE AgentID = 12345;
3. iSQL*Plus,
, exit <Enter>,
Ora le, SQL*Plus. ( ,
SQL*Plus.)
4. , File () Exit ().
Agents . , ,
. AFTER UPDATE

AuditAgents. ,
 Agents.
AuditAgents ,
.

 Ora le 10g  . 284

284

4.

. 4.28.

1. , iSQL*Plus Username.
, <Tab>,
Password Login.
2.

Workspa e :

SELECT * FROM AuditAgents;

3. Exe ute. Ora le


AuditAgents (. 4.28).
4.

Logout, iSQL*Plus, .

. 4.28. Before After .


( G ). , Title.
LastName. ,
TO_CHAR  ,
AuditDate . , 
:
SELECT TO_CHAR(AuditDate, 'MM/DD/YY HH:MM:SS');

 Ora le 10g  . 285

285

. , AFTER UPDATE
BEFORE UPDATE. .

, ,
, ..
, . ,
, .
, , , Ora le. ,
, . : INSERT DELETE
Agents Ora le ,
, . AFTER,
INSERT DELETE, . , Agents BEFORE INSERT AFTER
UPDATE. , Ora le , (INSERT ..),
Ora le , , DML-.
,
.
, Agents, -
Agents INSERT DELETE.
. SQL*Plus, () DBMS_OUTPUT,
( PUT
PUT_LINE). ,
LastName Agents.

BEGIN
DBMS_OUTPUT.PUT_LINE('New LastName value: ' || :NEW.LastName);
END;
, SQL*Plus:

SET SERVEROUTPUT ON
, ,
-  . -

 Ora le 10g  . 286

286

4.

,
. ,
Agents.
, DECLARE
BEGIN. BEGIN .
, INSERT UPDATE, .
1. SQL*Plus. (
). <Enter>.
, SQL*Plus.

CLEAR SCREEN
SET SERVEROUTPUT ON
2. , SQL*Plus <Enter>
END:

CREATE OR REPLACE TRIGGER Agents_adai_trg


AFTER INSERT OR DELETE ON Agents
FOR EACH ROW
DECLARE V_RowCount NUMBER;
BEGIN
SELECT COUNT(*) INTO V_RowCount FROM Agents;
DBMS_OUTPUT.PUT_LINE(V_RowCount || ' rows in Agents');
END;

3. SQL*Plus (/) <Enter>, (. 4.29). Ora le Trigger


reated.
,
. , , .
, .
1. SQL*Plus DELETE,
:
DELETE FROM Agents WHERE Lastname = 'Silverburg';
Ora le .
2. ,

ROLLBACK;
3. .

 Ora le 10g  . 287

287

. 4.29.

, - ,
... AGENTS is mutating, trigger/fun tion may not
see it (... AGENTS , /
). (mutating table) ,  DML, . SQL (, SELECT )
 , ( , , ).
-,
. Ora le . Ora le, ,
,
, ? ,
, ?
, ? ,
. ,
.
 , .
1. FOR EACH ROW , (  .sql) SQL*Plus.
2. (/) <Enter>,
.
3. ,
SQL*Plus:

DELETE FROM Agents WHERE Lastname = 'Silverburg';


.
2 rows in Agents,

 Ora le 10g  . 288

288

4.

. 4.30. ,

DELETE. , ,
, . Ora le
1 row deleted (. 4.30).
4.
.

COMMT <Enter>, -

5. Exit <Enter>, SQL*Plus,


iSQL*Plus.

 ( Ora le)
. 
user_triggers.
all_triggers,  . (, olumn_name
4000 .) ,
user_triggers, . 4.7.

 Ora le 10g  . 289

289

4.7. ,

user_triggers

Trigger_Name
VARCHAR2(30)
Trigger_Type
VARCHAR2(16)
Triggering_Event VARCHAR2(227)
Table_Owner

VARCHAR2(30)

Base_Obje t_Type VARCHAR2(16)


Table_Name

VARCHAR2(30)

Column_Name

VARCHAR2(4000)

Referen ing_Names VARCHAR2(128)


When_Clause
VARCHAR2(4000)
Status
VARCHAR2(8)
Des ription
A tion_Type
Trigger_Body

VARCHAR2(4000)
VARCHAR2(11)
LONG



,
, ,
,
,

OLD NEW
WHEN
, ,


,

. ,

trigger_name, trigger_type, triggering_event, table_name status

.  .

, .
1. iSQL*Plus ( SQL*Plus),
Ora le, ,
Workspa e.

Workspa e :
SELECT Trigger_Name, Triggering_Event, Trigger_Type, Table_Name,
Status
FROM user_triggers;

2.

 (. 4.31).

 Ora le 10g  . 290

290

4.

. 4.31. 

3. iSQL*Plus, Logout, Ora le, .  SQL*Plus,


 .

, . , ALTER TRIGGER. , -
, . ALTER
TRIGGER :

ALTER TRIGGER [s hema.<trigger-name> {ENABLE | DISABLE};


s hema  .
, (
), . , ,
. Agents_au_trg,

 Ora le 10g  . 291

291

( ),
. SQL:

ALTER TRIGGER Agents_au_trg DISABLE;


,

ALTER TRIGGER Agents_au_trg ENABLE;


,
, .
,
DROP, :

DROP TRIGGER [s hema.<trigger-name>


, , . - ,
() SELECT,
.
, , .
1. , SQL*Plus,
:

DROP TRIGGER Agents_au_trg;


DROP TRIGGER Agents_bi_trg;
DROP TRIGGER Agents_adai_trg;
Ora le Trigger

dropped.

2. , ,

SELECT Trigger_Name from user_triggers;


Ora le no
.

rows sele ted, , -

3. exit <Enter>, Ora le,


SQL*Plus.


(Data Manipulation Language  DML)


Ora le INSERT, UPDATE DELETE. SQL INSERT
. ,
. -

 Ora le 10g  . 292

292

4.

( NULL), , , .
. ,
INSERT,
( ) NULL. INSERT ,
, ,
. , ,
.
( ) NULL .
   DD-MON-YY ,  TO_DATE ,
 .
, , . , ,
NEXTVAL,
. CURRVAL
. Ora le Dual, .
UPDATE ( )  .
WHERE ,
, , . , WHERE,
. CASE. Ora le SQL
, Ora le .
DELETE .
WHERE , . , , .
, TRUNCATE TABLE (
).
MERGE  INSERT, UPDATE DELETE
.
:
,
. , , . WHEN MATCHED WHEN
NOT MATCHED.

 Ora le 10g  . 293

293

COMMIT, ROLLBACK SAVEPOINT. COMMIT  DML, ROLLBACK DML, . Ora le, DDL-
(, CREATE TABLE) COMMIT, . , , , .
, () DML.
(, ).
DML-,
INSERT, UPDATE DELETE ( ).
DML.
,
 .
, .







,
,







Dual

 Ora le 10g  . 294

294

4.

1. UPDATE , .
2. ,  
 .
3. .
,
.

COMMIT .
5. BEFORE INSERT (FOR EACH ROW) NULL , OLD NEW
4.

1. INSERT , ,
_____.
2. ,  ,  Ora le
_____.
3. _____ CREATE SEQUENCE , .
4. SQL _____
.
5.  Li enseDate Agents, _____ _____
_____. ,
,
_____ _____ _____.

1.
Agents)?

INSERT ( -

 Ora le 10g  . 295

INSERT INTO
) INSERT INTO
) INSERT INTO
'Gilbert')
) INSERT INTO
)

295

Agents(AgentID, LastName) (96743, 'Gilbert')


Agents VALUES (96743, 'Gilbert')
Agents(AgentID, LastName) VALUES ('96743',

Agents(AgentID, LastName) VALUES (96743, 'Gilbert')


2. DELETE ?
) DELETE FROM Agents
) DELETE FROMAgentsWHENAgentID= 12345
) DELETE Agents WHERE LastName = 'Ellison'
) DELETE FROM Agents WHERE FirstName = 'Leonard' AND
LastName = 'Smythe'
3.  MERGE INTO INSERT, , ?
) WHEN MATCHED THEN UPDATE
) WHEN NOT MATCHED THEN UPDATE
) WHEN MATCHED THEN INSERT
) WHEN NOT MATCHED THEN INSERT
4. Ora le
) .
) .
) .
) .
5. Ora le BEFORE AFTER
INSERT, UPDATE DELETE. ,
, ?
) AUDIT.

INSTEAD OF.
) IN VIEW OF.
) IN REVERSE OF.

1. Readwood Realty

 Robert Stirling , Redwood Realty Customers, 


, .  -

 Ora le 10g  . 296

296

4.

 ,
.  ,
AgentID_seq, Customers,
, ,
, , , Customers. ,
. ,  ,
Ora le.
SQL*Plus Ora le. ,  SQL*Plus.
-  Ch04Problem1.sql . <> 1, <mypath>
, <>  .
.
. - , Ora le, ,
Ora le  ROLLBACK
.

1. , :

START C:\<>\Ch04Problem1.sql
COLUMN CustomerID FORMAT 99999
COLUMN LastName FORMAT A10
COLUMN City FORMAT A10
COLUMN State FORMAT A10
COLUMN Zip ode FORMAT A7
CLEAR SCREEN
SPOOL C:\<mypath>\<>Ch04Redwood.txt
SHOW USER
CREATE SEQUENCE CustomerID_seq
START WITH 34567
INCREMENT BY 2;

Customer .
UPDATE Customers
SET City = 'Ara ta', State = 'California'
WHERE Zip ode = '95570';
SAVEPOINT SaveAfterUpdate;

2.

3. .

INSERT INTO Customers (CustomerID, FirstName, LastName)


VALUES (25599, 'Lora', 'Dietri h');
SAVEPOINT SaveAfterInsert;

 Ora le 10g  . 297

297

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

DELETE FROM Customers;


SELECT CustomerID, LastName, City, State,
Zip ode FROM Customers;
ROLLBACK TO SAVEPOINT SaveAfterInsert;
SELECT CustomerID, LastName, City, State,
Zip ode FROM Customers;
DELETE FROM Customers WHERE WorkPhone IS
NULL;
SELECT CustomerID, LastName, City, State,
Zip ode FROM Customers;
ROLLBACK TO SAVEPOINT SaveAfterUpdate;
SELECT CustomerID, LastName, City, State,
Zip ode FROM Customers;
5. ,
Customers .
. .

CREATE OR REPLACE TRIGGER Customers_bi_trg


BEFORE INSERT ON Customers
FOR EACH ROW
BEGIN
SELECT CustomerID_seq.NEXTVAL INTO
:NEW.CustomerID FROM DUAL;
END;
/
INSERT INTO Customers (LastName, FirstName,
City, State, Zip ode)
VALUES ('Chang', 'Y.F.', 'Ar ata', 'CA', '95518');
INSERT INTO Customers (LastName, FirstName,
City, State, Zip ode)
VALUES ('Newman', 'Ri hard', 'Blue Lake', 'CA', '95525');
INSERT INTO Customers (LastName, FirstName,
City, State, Zip ode)
VALUES ('Nasser', 'Jehad', 'Eureka',
'CA', '95503');
COMMIT;
SELECT CustomerID, LastName, City, State,
Zip ode
FROM Customers ORDER BY CustomerID;
6. , , () ,  .

SPOOL OFF
DROP TRIGGER Customers_bi_trg;
DROP SEQUENCE CustomerID_seq;
DROP SEQUENCE AgentID_seq;

 Ora le 10g  . 298

298

4.

DROP TABLE Agents CASCADE CONSTRAINTS


PURGE;
DROP TABLE AuditAgents CASCADE CONSTRAINTS
PURGE;
DROP TABLE Li enseStatus CASCADE
CONSTRAINTS PURGE;
DROP TABLE Customers CASCADE CONSTRAINTS
PURGE;
7.

exit <Enter>, SQL*Plus.

8. , File (), Open


(). ,   <>Ch04Redwood
.txt, , Open ().
9. <Enter>  ,
, . File (),
Print (),  . .
2. Coffee Mer hant

Coffee Mer hant ,


Employees,  Coffee Mer hant.
AuditEmployees,  , ,
Employees. ,
Employees . :
Before After, Ora le, ,
, EmployeeID LastName
CommissionRate. , Employees.
 AuditEmployees,
, ; ,   , .
SQL*Plus.
,  , SQL*Plus
SQL, .  .  , , ,
 .
, , 
Ch04Problem2.sql,
Coffee Mer hant. , ,
SQL*Plus .

 Ora le 10g  . 299

299

1. SQL*Plus. Ora le,


, Coffee Mer hant AuditEmployees.  Ch04Problem2.sql
<>.

CLEAR SCREEN
START C:\<>\Ch04Problem2.sql
CREATE TABLE AuditEmployees
(BeforeAfter NVARCHAR2(6),
AuditUsername NVARCHAR2(30),
AuditDate DATE,
EmployeeID INTEGER,
LastName NVARCHAR2(30),
Commission Number(4,4)
);
2. , INSERT, UPDATE DELETE Employees. ( Employees . 1
Ch04Problem2.sql.) (/) <Enter>, .

CREATE OR REPLACE TRIGGER


Employees_aiauad_trg
AFTER INSERT OR UPDATE OR DELETE ON
Employees
FOR EACH ROW
BEGIN
INSERT INTO AuditEmployees VALUES
('Before', user, sysdate,
:OLD.EmployeeID, :OLD.LastName,
:OLD.CommissionRate);
INSERT INTO AuditEmployees VALUES
('After', NULL, NULL,
:NEW.EmployeeID, :NEW.LastName,
:NEW.CommissionRate);
END;
/
3.  .

SAVEPOINT BeforeInsert;
INSERT INTO Employees (EmployeeID,
FirstName, LastName, Gender)
VALUES (1528, 'Lu a', 'Pa ioli', 'M');
INSERT INTO Employees (EmployeeID,
FirstName, LastName, Gender)
VALUES (3432, 'Melinda', 'English', 'F');
INSERT INTO Employees (EmployeeID,
FirstName, LastName, Gender)
VALUES (3692, 'Steve', 'Ballmer', 'M');
4. 
, CASE.

 Ora le 10g  . 300

300

4.

SAVEPOINT BeforeUpdate;
UPDATE Employees
SET CommissionRate =
CASE
WHEN CommissionRate
WHEN CommissionRate
WHEN CommissionRate
CommissionRate
ELSE CommissionRate
END;

IS NULL THEN 0.025


< 0.07 THEN CommissionRate * 1.15
BETWEEN 0.07 and 0.09 THEN
* 1.1
* 1.05

5. ,  . DELETE,
 (COMMIT) Employees,
ROLLBACK.

SAVEPOINT BeforeDelete;
DELETE FROM Employees WHERE Gender = 'M';
SELECT COUNT(*) FROM Employees;
ROLLBACK TO SAVEPOINT BeforeDelete;
COMMIT;
SELECT COUNT(*) FROM Employees;
6. SQL*Plus  .
Ora le .
Employees.
, .

SPOOL C:\Ch04Problem2-spoolfile.txt
SHOW USER
SELECT * FROM AuditEmployees;
ALTER TRIGGER Employees_aiauad_trg DISABLE;
DELETE FROM Employees Where Gender = 'F';
SELECT COUNT(*) FROM Employees;
ROLLBACK;
SELECT COUNT(*) FROM Employees;
7.  , ,
, . ,
,  .

SELECT Trigger_Name, Trigger_Type,


Table_Name FROM User_Triggers;
DROP TRIGGER Employees_aiauad_trg;
DROP TABLE AuditEmployees CASCADE
CONSTRAINTS PURGE;
DROP TABLE Employees CASCADE CONSTRAINTS
PURGE;
SPOOL OFF
8. exit <Enter>, Ora le
SQL*Plus.

 Ora le 10g  . 301

301

9.  , SQL*Plus SQL.  ,
<Enter>, , ,  . 
SQL*Plus/SQL   ,
.
3. Rowing Ventures

, -  Person, , , .
, , , .
( )
,  Person. - ( ),
.
SQL,  .
SQL*Plus.  Ch04Problem3.sql, .  , START ( 
). AuditPerson, ,
, /,
Person  PersonID, Weight DateOfBirth. AFTER
(Person_aiauad_trg), , - Person INSERT, UPDATE DELETE.
Person AuditPerson. PersonID ( 1100), LastName, FirstName, Gender, Weight DateOfBirth.
1972 . Weight (Gender = 'M'),
6 .  .
BeforeDeleteFemale. Person, (DateOfBirth) 1971 1972 . .
, .  ,  AuditPerson,
:

SELECT * FROM AuditPerson;


 , , ,
, , .
 AuditPerson Person. . 

 Ora le 10g  . 302

302

4.

4.8.

AuditConta t

OperationType

VARCHAR2(6)

BeforeAfter

VARCHAR(6)

Username

VARCHAR(30)

ModDate
Re ordPK

Date
INTEGER

Re ordLastName

NVARCHAR(20)

, :

UPDATE DELETE

INSERT,

('BEFORE')
('AFTER')
Ora le,

Conta tID , 
LastName

SQL*Plus, . 
. .
SQL*Plus SQL*Plus.
4. Broad loth Clothing

Broad loth Clothing 17 .


Conta t, , 
Conta tID, ,  ( ), ,
..  -, .
, , ,
, , INSERT, UPDATE DELETE, Conta t.
SQL*Plus,  Ch04Problem4.sql SQL*Plus,
Broad loths Clothing.
AuditConta t.
.
, Conta t. ,
. onta t_bu_trg,
,
 ( OperationType UPDATE, BeforeAfter  BEFORE) ( OperationType UPDATE, BeforeAfter  AFTER). onta t_bi_trg,

 Ora le 10g  . 303

303

,
AuditConta t. OperationType INSERT, BeforeAfter  (new);
AuditConta t Ora le, /,
LastName. (.
:NEW.) , onta t_bd_trg, ,
AuditConta t . OperationType DELETE, BeforeAfter  (old),
Ora le, /, LastName
. (. :OLD.)
( , 1234, FirstName LastName.)
DESCRIBE, Conta t. ,
Nation Pakistan Malaysia (
 ). , PrimaryLanguage Chinese ( , ), GMTDifferen e
. . ,
SELECT * FROM AuditConta t;.
, , .
,    , , . , DROP,
, Conta t AuditConta t.

 Ora le 10g  . 304

 Ora le 10g  . 305



. . .

SELECT
SELECT 

SELECT , , 

 SQL
 SQL

avg, ount, max, min sum

HAVING

 SQL*Plus



, , ,   . ,
 .  -

 Ora le 10g  . 306

306

5.


.
(Stru ture Query Language  SQL) SQL*Plus,
,
. SQL,
, , ,  ,  .
 SQL SELECT.
DML-, SELECT .
, , . , , Reedwood
Realty . ,
,
, . , , ,
SELECT.

SELECT

SELECT :
SELECT * FROM <table-name>;
<table-name>  , -

, (*) , Ora le
. ,
. . , ,
, 
. SELECT , , . SELECT
.

SELECT [DISTINCT <sele t-list>


FROM {<table-list>}
[WHERE < onditions>
[GROUP BY <group-by-list>
[HAVING <group- onditions>
[ORDER BY <order-list> [ASC | DESC
[FOR UPDATE <for-update-options>;
DISTINCT
( , ). sele t-list , . ,

 Ora le 10g  . 307

307

, .-. table-list,
FROM,
, .
WHERE  ,
(). GROUP BY .  , group-by-list.
, HAVING,  . ( WHERE .)
ORDER BY
. ASC DESC,
. ,
.
, .
ORDER BY , Ora le .
 FOR UPDATE
. SELECT, FROM, WHERE, HAVING ORDER BY
, SELECT . , Ora le
. ,
 .
Ora le
Customers. City = 'Ar ata' , , . , ORDER BY LastName, ( LastName ) FirstName. Ora le,
, ,
.
SQL , . ,
, , . (
, City Properties, ..
, .)

SELECT FirstName, LastName, Address, City, State, Zip ode


FROM Customers WHERE City = 'Ar ata' ORDER BY LastName, FirstName;
, Redwood Realty.
-, ,  ,

 Ora le 10g  . 308

308

5.

Redwood Realty,
. SQL*Plus  BuildRedwood.sql
, 5, .   , BuildRedwood.sql.
. , Redwood Realty. 
 , , .   ,  ,
.
.
Redwood Realty, .
1. SQL*Plus Ora le. (: 
iSQL*Plus .)
2.  Redwood Realty RedwoodRealty.
<> <Enter> .

START <>\BuildRedwood
, Ora le , , (. 5.1).
, Redwood Realty, SELECT
 .
 , .
1.  <Enter>
. SQL*Plus  ,
.

CLEAR SCREEN
COLUMN FirstName FORMAT A12
COLUMN LastName FORMAT A12
COLUMN Gender FORMAT A6
COLUMN Title FORMAT A11
2. , Agents
( <Enter> , SELECT):

SELECT FirstName, LastName, Gender, Title


FROM Agents;

 Ora le 10g  . 309

. 5.1. , 

309

BuildRedwood

 . 5.2. Agents. .
3. exit <Enter>, Ora le SQL*Plus. SQL*Plus
iSQL*Plus.


, sele t-list SELECT.
SELECT , .
. , 
sele t-list  (*). , ( ),
. , .

 Ora le 10g  . 310

310

5.

. 5.2.

SELECT, ,

iSQL*Plus, iSQL*Plus
, SQL*Plus.
, ,
.
1. , iSQL*Plus. Ora le,
.
2. Workspa e . ( iSQL*Plus
, , . .)

SELECT * FROM Agents;


3. Exe ute, . ,
Workspa e . Ora le
Agents (. 5.3).
 .
4.

Logout,

Ora le, .

 Ora le 10g  . 311

. 5.3.

311

Agents

. iSQL*Plus Ora le, , . , iSQL*Plus ,


.
, Workspa e. -
, iSQL*Plus .

DISTINCT

sele t-list SELECT
DISTINCT, . , ,
SELECT, . ,  ,
(, -

 Ora le 10g  . 312

312

5.

. 5.4.

DISTINCT

). ,
DISTINCT . , ,
, , .
CustAgentList. ,
:

SELECT CommissionRate FROM CustAgentList;


(1018 ),
. , . DISTINCT, ,
, ..

SELECT DISTINCT CommissionRate FROM CustAgentList;


 .  SELECT DESCRIBE CustAgentList
. 5.4. , ,
: 3, 4, 5 6%. ( .)


Ora le, SELECT,
SQL*Plus . ,
.  SQL*Plus : <Ctrl+C>,
. , .
SQL 
SQL*Plus, .

 Ora le 10g  . 313

313

. 5.5. SQL*Plus

1. SQL*Plus, Ora le . <Enter> , Ora le


. SELECT 1018 !

SELECT * FROM CustAgentList


2. , SQL*Plus . <Ctrl+C> (
<Ctrl>, <C> ). SQL*Plus
, ,
SQL> (. 5.5).

 , Ora le , SELECT
WHERE. WHERE
4  UPDATE DELETE. SELECT WHERE . , WHERE,

 Ora le 10g  . 314

314

5.

,
Ora le. , ( true). ,
WHERE :

WHERE < olumn-name> < omparison-operator> <sear h-expression>


olumn-name ,
Ora le. omparison-operator , sear h-expression. ( . 4.3.) sear h-expression  . WHERE
 olumn-name/ omparison-operator/sear h-expression, . ( 4, . 4.4.)


, ,   ,
. WHERE,
Customers.

...WHERE City = 'Blue Lake'


...WHERE CustomerID = 25343
...WHERE FirstName <> 'Randy'
WHERE, SQL*Plus Redwood Realty.
SELECT , , Ora le
, WHERE
, .
, WHERE,
.
1. SQL*Plus, CLEAR SCREEN <Enter>,
SELECT:

SELECT CustomerID, FirstName, LastName


FROM Customers
WHERE LastName = 'Nguyen';
Ora le , CustomerID.
2. :

SELECT OwnerID, Bedrooms, Bathrooms, Stories, SqFt


FROM Properties
WHERE SqFt = 2100;

 Ora le 10g  . 315

315

Ora le  , SqFt (
) 2100.
3. :

SELECT AgentID, LastName, FirstName


FROM Agents
WHERE Title <> 'Salesperson';
Ora le  , ,
Salesperson.
4. . ( linesize
90, .)

SET Linesize 90
SELECT *
FROM CustAgentList
WHERE BidPri e > 350000;
5. , Listings:

SELECT ListingID, BeginListDate, EndListDate, AskingPri e


FROM Listings
WHERE BeginListDate <= '26-JUN-2006';
Ora le , 27
2006 .  . 4 5 . 5.6.


WHERE, , . , WHERE
SELECT Agents.

SELECT LastName
FROM Agents
WHERE Title = 'salesperson'
, Title Agents : ,
. , , salesperson,
SALESPERSON ( ). ( , ,  Ora le.) , , , WHERE,
. ,
.

 Ora le 10g  . 316

316

5.

. 5.6.

WHERE 

, WHERE, . ,  Ora le, ,  DD-MON-YY, DD  ; MON 


; YY   .  , YY, 50, , YY,
50,  .
WHERE,
. ,
,
1 2005 . SELECT
, Li enseExpire '01-JUN-05'.

SELECT FirstName, LastName


FROM Agents
WHERE Li enseExpire >= '01-JUN-05';
,
, , Li enseExpire () (
SYSDATE). SELECT:

 Ora le 10g  . 317

5.1.

 SQL

BETWEEN ... AND


IN
IS NULL
LIKE

317



NULL.
NULL
( )

. 5.7.

BETWEEN ... AND

SELECT FirstName, LastName


FROM Agents
WHERE Li enseExpire < SYSDATE;

SQL
SQL, ,
 ,
, , NULL
. SQL
. 5.1.

BETWEEN

, BETWEEN ... AND, , .


. 5.7 12 , AskingPri e 212 000 235 000 . ,
, ,  ,
.

 Ora le 10g  . 318

318

5.

IN

IN , , .
,
. IN , . ,
 , , , .
WHERE:

WHERE State IN ('Ohio','Indiana','Mi higan','Illinois')


, , ( ):

WHERE State = 'Ohio' OR State = 'Indiana'


OR State = 'Mi higan' OR State = 'Illinois'

LIKE

LIKE . ,
. WHERE LIKE, , -
.
Ora le: % _ ().
(_) ,
(%) ( ) . , (Customers), 
H, :

SELECT FirstName, LastName, Address, City, State


FROM Customers
WHERE LastName LIKE 'H%';
, 955 1 ,
WHERE :

...WHERE ZipCode LIKE '955_1';


, ,  , ESCAPE. ,
, LIKE,
. , , (, ... almost 35% of all people ... ).
, WHERE,
ESCAPE, .
, .

 Ora le 10g  . 319

319

... WHERE StatusText LIKE '\%almost 35% of all people' ESCAPE '\'
. Ora le 10g
, . , . 
 , , .

IS NULL

, NULL
(.. ). ,
( ),  YearBuilt
NULL, Agents, Li enseExpire NULL. NULL ,
, . IS NULL WHERE.
, IS NOT NULL
NULL.
Agents, Li enseExpire .

SELECT FirstName, LastName


FROM Agents
WHERE Li enseExpire IS NULL;


(. 3), : AND, OR
NOT. , WHERE , (AND) (OR).
. NOT
. , ,
, 1999 . (SqFt) 2 000
. ,
. (, SqFt > 2000), , AND,
.
 , Ora le . , ,
WHERE:

WHERE City = 'Ar ata' AND YearBuilt < 2004 OR Bedrooms = 3;

 Ora le 10g  . 320

320

5.

Ora le  .
, .
, .
, <, =, >
. , .
WHERE  City = 'Ar ata'
 true false . YearBuilt = 2004 .. ,
Ora le , true/false
.
NOT, .
AND,  OR. ,
, .

SQL*Plus. ,
, SQL*Plus <Enter>,
. , 
, SQL*Plus.
SELECT,
, .
1. , SQL*Plus Ora le.
2.

CLEAR SCREEN <Enter>.

3. ,  , 1991 2 000
 :

SELECT Address, YearBuilt, SqFt, Bedrooms, Bathrooms


FROM Properties
WHERE City = 'Ar ata' AND YearBuilt > 1991 AND SqFt >= 2000;
4. ,
(Blue Lake) (Trinidad), , Ln. , ( ) , OR. (
 .)

SELECT Address, City


FROM Properties
WHERE City IN ('Blue Lake', 'Trinidad') AND
(Address LIKE '%Ln' OR Address LIKE '%lN'
OR Address LIKE '%LN' OR Address LIKE '%ln');

 Ora le 10g  . 321

321

. 5.8.

5. , SaleStatusID, BeginListDate SellingPri e , (


SaleStatusID 101) 2006 (BeginListDate):

SELECT SaleStatusID, BeginListDate, AskingPri e


FROM Listings
WHERE SaleStatusID = 101
AND BeginListDate BETWEEN '01-JUN-2006' AND '30-JUN-2006';
6.  , ,  (AskingPri e) 130 000.

SELECT SaleStatusID, BeginListDate, AskingPri e


FROM Listings
WHERE SaleStatusID = 101
AND BeginListDate BETWEEN '01-JUN-2006' AND '30-JUN-2006'
AND AskingPri e > 130000;
. 5.8.
.

SELECT . , SELECT ORDER BY,


:

ORDER BY {< olumn-name>|<result- olumn-number>} [ASC|DESC


[NULLS FIRST|NULLS LAST
[,{< olumn-name>|<result- olumn-number>} [ASC|DESC
[NULLS FIRST|NULLS LAST...

 Ora le 10g  . 322

322

5.

olumn-name (,
, ). result- olumnnumber ,
( ). ASC as ending ( ), DESC  des ending ( ). NULLS
FIRST/LAST , NULL 
(  LAST).
, , . ,
, .
, . Ora le
, .

..
. ( ) ( ).  , 
. ,  ASC
DESC . ORDER BY SELECT, Ora le .
. DISTINCT
SELECT, (),
, ORDER BY.
. , ,
 , SELECT, , LastName,  FirstName (
 ). , , , .
SELECT, ( ), ( ) , ,
( ).

SELECT YearBuilt, City, Bedrooms, SqFt


FROM Properties
WHERE YearBuilt > 1989
AND City IN ('Fortuna', 'M Kinleyville', 'Loleta')
AND SqFt >= 2000
ORDER BY City, 3, YearBuilt DESC;

 Ora le 10g  . 323

323

Ora le , , WHERE,
, , ORDER BY, 26 . 3
ORDER BY. olumn-list
(Bedrooms), ( City).
, SELECT
, , ,
, ORDER BY.

NULL ORDER BY
ORDER BY , NULL,

, . , NULL ,
NULLS FIRST. ,
NULL  DESC ASC.

SELECT BidPri e FROM CustAgentList


ORDER BY BidPri e DESC NULLS FIRST;

n 
n  , n ( ) . ,
SalesAmount, .
. ,
SalesAmount ( ), n  ,
. , .
SQL , n  n 
, .
n  ,
() ()
() () .
Ora le ROWNUM,
, . -

 Ora le 10g  . 324

324

5.

ROWNUM .
, ()
< <=. > >= ROWNUM . , ,
AskingPri e Listings, , . , 
.
, ROWNUM
WHERE, .

SELECT AskingPri e
FROM (SELECT AskingPri e
FROM Listings
ORDER BY AskingPri e DESC)
WHERE ROWNUM <= 5;
, . FROM ( );   (inline view).
Listings, AskingPri e  502 .
( ),
. ROWNUM 1 502 ( ). , ,
. WHERE , ROWNUM, .

NULL.
SELECT. ORDER BY.
, ORDER BY,
.
1. , SQL*Plus Ora le. CLEAR
<Enter>, .

SCREEN

2. BidPri e,
, :

CLEAR SCREEN
SELECT BidPri e
FROM (SELECT BidPri e FROM CustAgentList
ORDER BY BidPri e DESC)
WHERE ROWNUM <= 7;
, Ora le !
BidPri e NULL , ,
DESC NULL.

 Ora le 10g  . 325

325


NULL

NULLS LAST
NULL

. 5.9.  n 

3. NULL ,
 .
 NULLS LAST DESC .  .

SELECT BidPri e
FROM (SELECT BidPri e FROM CustAgentList
ORDER BY BidPri e DESC NULLS LAST)
WHERE ROWNUM <= 7;

BidPri e (. 5.9).

Ora le

4.  , .
, () 1969 .
SQL*Plus SQL:

CLEAR SCREEN
COLUMN LastName FORMAT A15
COLUMN FirstName FORMAT A15
SELECT LastName, FirstName, BirthDate
FROM Agents
WHERE BirthDate >= '01-JUL-69'
ORDER BY BirthDate DESC;
5. . 5.10.

 Ora le 10g  . 326

326

5.

,
1969

. 5.10. 

6. SQL*Plus SQL, , : City, Bedrooms Bathrooms.


,
, (Bathrooms)  ( ).

CLEAR SCREEN
COLUMN Address FORMAT A20
COLUMN City FORMAT A14
COLUMN SqFt FORMAT 99,999
SET PAGESIZE 25
SELECT Address, City, Bedrooms, Bathrooms, SqFt
FROM Properties
WHERE SqFT > 3000 AND Bedrooms > 4
ORDER BY City DESC, Bedrooms DESC, Bathrooms;
SET PAGESIZE 14
CLEAR COLUMNS
7. . 5.11.
8. SQL*Plus exit <Enter>,
.



( ), ,
.
, .
,

 Ora le 10g  . 327

327

. 5.11. 

 .
, , (12), (5,00 .)
(60,00 .), ,
,  .. . ,
, .
, SQL SELECT
, . , , (, ), ,
, . ,
, . (, 
.) SQL, ,
,  ,
 .


Ora le  SQL, , , ,
+, -, * /. , -

 Ora le 10g  . 328

328

5.

, 
(, ). , , .
 ,
NUMBER, DATE INTERVAL. ,
, ,
DATE INTERVAL.
 , ,  SQL, , SELECT.
. , (
), DUAL. , DUAL  , ,
.
FROM <_>, DUAL. , , 37,85 .
(6 ) .
SQL:

SELECT 0.06 * 37.85, 1.06 * 37.85


FROM DUAL;
(,  ,  .)
, DUAL ,  .
, NUMERIC, . , SELECT, ,
, ().
Listings, ,
AskingPri e  ,
. , 6 . ,
,
. , AskingPri e
. , ( ).
.

SELECT AskingPri e, AskingPri e*0.06, AskingPri e-(AskingPri e*0.06)


FROM Listings;

 Ora le 10g  . 329

329

 .
SQL*Plus iSQL*Plus Ora le.
SQL , .
1. , , .
,  , .

CLEAR SCREEN
SELECT AskingPri e, AskingPri e*0.05, AskingPri e*0.06,
AskingPri e-(AskingPri e*0.05), AskingPri e-(AskingPri e*0.06)
FROM Listings
WHERE AskingPri e > 350000
ORDER BY AskingPri e DESC;
Ora le ( ).
2. Properties YearBuilt
 .  
Properties (,
 2006).

COLUMN Address FORMAT


COLUMN City FORMAT A7
SELECT Address, City,
FROM (SELECT Address,
FROM Properties
WHERE ROWNUM < 11;

A20
YearBuilt, 2006-YearBuilt
City, YearBuilt, 2006-YearBuilt
ORDER BY 4 DESC)

3. SQL*Plus ( iSQL*Plus). iSQL*Plus,


 . , iSQL*Plus
Ora le.
, . 5.12.
 ( Address
City) SQL*Plus
.


 date . , , 
30, 60 90 .  , .

 Ora le 10g  . 330

330

5.

. 5.12.

SELECT

 . ,
SYSDATE.
, :

SELECT SYSDATE FROM DUAL;


,
, DUAL ,
. , , Li enseExpire , .
SELECT:

SELECT Li enseExpire, Li enseExpire-SYSDATE


FROM Agents
ORDER BY Li enseExpire-SYSDATE DESC;
, 30 365 .
,
.

SELECT Li enseExpire-SYSDATE
FROM Agents
ORDER BY 1 DESC;

 Ora le 10g  . 331

331

1 DESC Ora le


.   , ( ) 365 .
365.24, , , 400.
, (),
() . ,
, . , , . (,
, , 2?) Ora le ,
.
(, HireDate BirthDate date).
,
.

SYSDATE + 27
SYSDATE - HireDate
HireDate - BirthDate
Invoi eDate - 60
SYSDATE - BirthDate


. 5.12 SELECT, , Ora le
. . 5.12, , SELECT Address,
ADDRESS.
SELECT Ora le . . 5.12
2006-YEARBUILT,  . , Ora le , ( olumn alias).
: ( ) ,  ;
ORDER BY;
.
, . , 
( !).  30 ,
( ANSI  )

 Ora le 10g  . 332

332

5.

.
, .
.

SELECT AskingPri e-0.06*AskingPri e AS NET_PRICE,


AskingPri e*0.06 "Commission Rate"
FROM Listings;
,

AS. ANSI
, . , AS SELECT, .

. , , .
, .  ,
.
(| |) , , ,
, . ( , .) ,
Agents . 
( , ..).
, ,  .

SELECT FirstName || LastName


FROM Agents;
, :  (, Ale Baldwin).

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

SELECT FirstName||' '||LastName AS "Name"


FROM Agents
ORDER BY LastName, FirstName;

 Ora le 10g  . 333

333

. 5.13.

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

, ,
. SQL*Plus iSQL*Plus Ora le.
, SELECT, .
1. , ,
:

CLEAR SCREEN
COLUMN "Name" FORMAT A20
COLUMN "Age when hired" FORMAT 99
SELECT FirstName||' '||LastName AS "Name",
(HireDate-BirthDate)/365.24 AS "Age when hired"
FROM Agents
ORDER BY "Age when hired" DESC, LastName, FirstName;
(. 5.13).
(  
 99).
2.  . .

 Ora le 10g  . 334

334

5.

. 5.14. 

COLUMN FirstName FORMAT A15


COLUMN LastName FORMAT A15
SELECT FirstName, LastName, (SYSDATE-BirthDate)/365.24 AS Age
FROM Agents
ORDER BY LastName, FirstName;
, Age ,
(, ).
3. SELECT Age Age
Today ( ). ? Ora le
.
4. :

SELECT LastName||' is '||TO_CHAR((SYSDATE-BirthDate)/365.24)


||' years old today.' AS "How old are sele ted agents?"
FROM Agents
WHERE BirthDate > '31-DEC-1969'
ORDER BY Birthdate;
Ora le  (. 5.14). - ,  TO_CHAR
,
.
5. SQL*Plus iSQL*Plus.

 SQL
Ora le . , ,

 Ora le 10g  . 335

 SQL

335

. Ora le :
.  . 
.   UPPER, ;
, SELECT,   . 
() . ,  AVG
,  SUM  . .


 , SELECT, . , 
WHERE ORDER BY.  Ora le .
. ; -

.
. ;

.
.

.
. .
. -


 , . 
LOWER(),
. , INITCAP(), . ,  SELECT
. ( , ), .
, ,

 Ora le 10g  . 336

336

5.

 ,

SELECT INITCAP(FirstName), INITCAP(LastName)


FROM Agents;
, , ( ) ,  .
 SQL . 5.2.
 SELECT, WHERE ORDER BY, , ,
( ) . ,
 SQL*Plus.
, .
1. SQL*Plus, Ora le, (CLEAR SCREEN)
, , :

SELECT UPPER('ora le') "Upper", LOWER('DATABASE') AS "Lower",


INITCAP('for whom the bell tools') AS "Init ap",
LENGTH('for whom the bell tools') AS "Length"
FROM DUAL;
2. , ,
. 
, (inline view). Ora le .

SELECT Address, LENGTH(Address) AS "Addr. Size"


FROM (SELECT Address, LENGTH(Address)
FROM Customers
ORDER BY 2 DESC)
WHERE ROWNUM < 11;
3. , ( ),  ling.
, .

SELECT SUBSTR(FirstName,1,1) || '. '||LastName AS "Name"


FROM Customers
WHERE UPPER(LastName) LIKE '%LING';
, Ora le ,
. 5.15.
4. ,
 , . ,

 Ora le 10g  . 337

 SQL

5.2.

337

INSTR(<str1>,<str2>)
LENGTH(<str>)
LOWER(<str>)
UPPER(<str>)
INITCAP(<str>)
NVL(<str>, <value>)
NVL2(<str>, <value1>,
<value2>)
REPLACE(<str1>,<str2>,
<str3>)
LPAD(<str>, <width>,
[,<pad string>)

<str1> , <str2>
<str>
<str>,
<str>,
<str>

<value>, <str> NULL;

<str>

<value1>, <str> NULL;

<value2>

TRIM(<str>)

<str1> <str2>. ,
<str2> <str3>
<str> , <width> .
<pad string>
<str> ,
<width> .
<pad string>
 () <str>, <start>.
<length>

LTRIM(<str>)
RTRIM(<str>)

<str>
<str>

RPAD(<str>, <width>,
[,<pad string>)
SUBSTR(<str>, <start> [,
<length)

SQL*Plus
SELECT.)

<str>

PAGESIZE. ( PAGESIZE

SET PAGESIZE 40
SELECT
RPAD(FirstName||' '||LastName,40,'.') AS "Agent",
LPAD(WorkPhone,18) AS "Work Number"
FROM Agents

 Ora le 10g  . 338

338

5.

. 5.15. 

ORDER BY LastName, FirstName;


SET PAGESIZE 14
 UPPER, LPAD ( ) RPAD ( ) . 5.16.


Ora le ,
, .  Ora l , ,
.
 . 5.3. 
( ) Ora le.
 .  , ,
, .   . ,
 .

 Ora le 10g  . 339

 SQL

. 5.16. 

339

LPAD RPAD

. ,  SIGN()
-1, ; 1 
, 0  . SQL*Plus
Ora le. CLEAR SCREEN <Enter>,
, .
, .
1. :

SELECT ABS(-234), CEIL(23.112), MOD(21,6),


ROUND(567.93537,2), SQRT(5280)
FROM DUAL;
2. ,
 TRUNC ROUND. 
. 5.17. , , Ora le,
.

COLUMN LastName FORMAT A15


SELECT LastName,
TRUNC((SYSDATE-BirthDate)/365.25) AS "Age",
ROUND((SYSDATE-BirthDate)/365.25,2) AS "Rounded-2",

 Ora le 10g  . 340

340

5.

5.3.

ABS(<n>)

CEIL(<n>)

,
<n>
,
<n>

FLOOR(<n>)

<n>

ABS(5) = 5
ABS(-58) = 58
CEIL(6.8) = 7
CEIL(-34.5) =-34
FLOOR(78.9) = 78

FLOOR(-3.1) =-4
<n> <m> MOD(7,3) = 1
MOD(29,6) = 5
POWER(<n>,<k>) <n> <k>
POWER(2,3) = 8
POWER(5,2) = 25
ROUND(<n>
<n>, ROUND(7.467,2) =
[,<m>)
<m> . <m> 7.47 ROUND(96.87,, <n>
1) = 100
MOD(<n>,<m>)

SIGN(<n>)
SQRT(<n>)
TRUNC(<n>
[,<k>)


-1, <n> ,
0  , 1  <n>

<n>

SIGN(-23) =-1
SIGN(456) = 1

SQRT(81) = 9
SQRT(7) =
2.645751
<n>,
TRUNC(56.999)
<k> . = 56

TRUNC(789.559,1)
= 789.5

ROUND((SYSDATE-BirthDate)/365.25,3) AS "Rounded-3",
(SYSDATE-BirthDate)/365.25 AS "Full"
FROM Agents WHERE BirthDate < '01-JAN-61'
ORDER BY 5;
3. SQL*Plus .


Ora le  ,
date. ,
, . 5.4.  , (TO_DATE) ()
date, (TO_CHAR) .

 Ora le 10g  . 341

 SQL

341

. 5.17. 

 
 .
, 
, .
1. SQL*Plus,

CLEAR SCREEN.

2. :

SELECT
EXTRACT(YEAR FROM HireDate) AS "Hire Year",
EXTRACT(MONTH FROM HireDate) AS "Hire Month",
EXTRACT(DAY FROM HireDate) AS "Hire Day",
HireDate, ADD_MONTHS(HireDate,5) AS "+5 Months"
FROM Agents
WHERE HireDate > '14-JUN-02'
ORDER BY 1;
3.  , ,
SQL*Plus SQL (,
):

COLUMN "Quarter" FORMAT A7


COLUMN "1 hour later" FORMAT A12
COLUMN "1 Day later" FORMAT A11
SELECT
TO_CHAR(SYSDATE, 'MM/DD/YYYY HH:MM:SS AM') AS "Year time",
TO_CHAR(SYSDATE, 'YYYY') AS "Year",
TO_CHAR(SYSDATE, 'Q') AS "Quarter",
TO_CHAR(SYSDATE+1/24, 'HH24:MM:SS') AS "1 hour later",

 Ora le 10g  . 342

342

5.

5.4.

,

date

ADD_MONTHS(<date>,
<no. of months>) <no. of months>
<date>. (,
<no. of months>
EXTRACT(<ymd>
FROM <date>)
LAST_DAY(<date>)
MONTHS_BETWEEN
(<date1>,<date2>)

NEW_TIME(<date>,
< ur-time-zone>,
<new-time-zone>)
NEXT_DAY(<date>,
<string>)

SYSDATE
TO_CHAR(<date>,
<format>)
TO_DATE(<str>,
<format>)

.)

, ,


,


.
<date1>
,


ADD_MONTHS('10-OCT06', 3)
10-JAN-07
EXTRACT(YEAR FROM

'10-OCT-85')

1985
LAST_DAY(SYSDATE)

MONTHS_BETWEEN
(SYSDATE, HireDate)
,

HireDate

NEW_TIME(SYSDATE,

'PST', 'EST')

,

(<string>),

NEXT_DAY('14-JUN2006', 'Tuesday')


( )
/
, 
<format>
<str>
, <format>

'07-OCT-06' (

<date>


'20-JUN-06'

TO_CHAR(SYSDATE,
'MM/DD/YYYY
HH24:MM;SS')
TO_DATE('10/30/2006'
'MM/DD/YYYY')

TO_CHAR(SYSDATE+1, 'MM/DD/YYYY') AS "1 Day later"


FROM DUAL;

 Ora le 10g  . 343

 SQL

343

, HireDate
HireDate

. 5.18.  

. 5.18 (
- / ).
4. , Redwood Realty.
CLEAR SCREEN <Enter>,
.
:

SET PAGESIZE 40
COLUMN "Unrounded Hire Age" FORMAT A20
SELECT TO_CHAR(BirthDate,'MM/DD/YYYY') AS "Birth date",
TO_CHAR(HireDate,'MM/DD/YYYY') AS "Hire date",
TRUNC(MONTHS_BETWEEN(HireDate, BirthDate)/12,0) AS "Hire Age",
TO_CHAR(MONTHS_BETWEEN(HireDate, BirthDate)/12, '99.99')
AS "Unrounded Hire Age"
FROM Agents ORDER BY 3 DESC;
,
Redwood Realty, . 5.19.
5. , , .
( ) ,
.

 Ora le 10g  . 344

344

5.

. 5.19.  

TRUNC MONTHS_BETWEEN

CLEAR SCREEN
SELECT DISTINCT
(EndListDate-BeginListDate) AS "Market Time: Days",
TRUNC((EndListDate-BeginListDate)/7) AS "Weeks",
FLOOR(MONTHS_BETWEEN(EndListDate,BeginListDate)) AS "Months"
FROM LISTINGS
ORDER BY 1 DESC;
Ora le , .
6. , Li enseExpire, Li enseExpire , . ,
2002
.

SELECT ADD_MONTHS(LAST_DAY(Li enseExpire),-1)+1 AS "Beginning",


Li enseExpire AS "A tual",
LAST_DAY(Li enseExpire) AS "Ending"
FROM Agents
WHERE UPPER(TITLE) = 'BROKER'
AND EXTRACT(YEAR FROM Li enseExpire) < 2002;

 Ora le 10g  . 345

 SQL

345

. 5.20.  /

, . 5 6, . 5.20.
. , ,
, . , SQL*Plus -,
.

7.

exit <Enter>, SQL*Plus.

. ,
. , ( ) , TO_CHAR.
, .

 Ora le 10g  . 346

346

5.


Ora le  , .
 TO_CHAR,
.  TO_DATE
TO_NUMBER. ,    ,
 CLOB (Chara ter Large Obje t).
 , TO_CHAR, TO_DATE
TO_NUMBER, .  ,
Ora le .

TO_CHAR

 TO_CHAR :

TO_CHAR(<date-number> [,<format-string>)
<date-number>  / ,
, <format-string>.
<format-string>,  ,  . ,
HireDate Agents , ,  TO_CHAR SELECT:

SELECT FirstName, LastName, (HireDate, 'Month, DD, YYYY')


FROM Agents;
,
, O tober 03, 1996 August 30, 1995. ,
, Ora le ,
,  DD-MON-YY (, 03-OCT-96).
TO_CHAR  , . ,
 TO_CHAR
BidPri e, ,
.

SELECT TO_CHAR(BidPri e, '$99,999,999.99')


FROM CustAgentList;
, '$99,999,999.99', ,
$, 9 . , ,
( 00). -

 Ora le 10g  . 347

 SQL

5.5.

, 


9
9999

, () 9,999
. ()
0
$
B

99.99
0999
9990
$999,999
B999

MI

9999MI

RN

RN
rn
S9999

EEEE

347

9999S

9.99EEEE

.
 ,  . ,





,
,  . 



,  

,  
 (,
1.78E+03)

,
 .
. 5.5. ,
 .
, ,
SELECT.
 TO_CHAR  , , .
1. SQL*Plus, Ora le, CLEAR SCREEN
<Enter>.
2. ,  BidPri e :

 Ora le 10g  . 348

348

5.

SET PAGESIZE 20
SELECT TO_CHAR(BidPri e, '$99,999,999.00') AS "Bid Pri e"
FROM CustAgentList WHERE BidPri e > 360000;
3. , , . WHERE, , Ora le ( ) . ,
, , LIKE,
.

SELECT TO_CHAR(Longitude, 'S9999.99') AS "S9999.99",


TO_CHAR(Longitude, '000999.99MI') AS "000999.99MI",
TO_CHAR(Latitude, 'S9999.9') AS "S9999.9",
TO_CHAR(Latitude, 'RN') AS "Roman Latitude"
FROM Properties
WHERE UPPER(Address) LIKE '%RIVER%';
 . 2 3 , . 5.21.
4. , , , :

CLEAR SCREEN
SELECT TO_CHAR(345.678,'S99,999.99') AS "S99,999.99",
TO_CHAR(0.00678,'9.999EEEE') AS "9.999EEEE",
TO_CHAR(5280, '09.99') AS "09.99",
TO_CHAR(1256, 'RN') AS "RN: 1,256",
TO_CHAR(1234.5678, '$999,999.00') AS "$999,999.00"
FROM DUAL;
5. , #. ,
 , .
, :

TO_CHAR(5280, '09999.99') AS "09999.99",


6.  . , . 5.22.
7. exit, <Enter>, Ora le,
SQL*Plus.

TO_DATE

TO_DATE . 
, . ,
. 
 / . 
,  
DD-MON-YY DD-MON-YYYY ( ). 
 TO_CHAR.

 Ora le 10g  . 349

 SQL

349

. 5.21. 

TO_DATE(<date-string> [,<format-string>)
<date-string>  , , <format-string>  , Ora le, .
 TO_DATE.

TO_DATE('January 10, 2006', 'Month DD, YYYY')


TO_DATE('10/17/2004 10:27:48', 'MM/DD/YYYY HH24:MI:SS')
TO_DATE('7.14.03', 'MM.DD.YY')
TO_DATE('2006/9/23','YYYY/MM/DD')
TO_DATE('10-OCT-2006')
 .
, ,
(DD), (YYYY).
  ,
. .
, , Ora le
. 
. 5.6. . 5.7
 , .

 Ora le 10g  . 350

350

5.

. 5.22. 

5.6.

,

CC

YYYY, YYY,
YY,Y, RR

MONTH,
Month, MON,
Mon

MM
WW, W

DDD, DD, D

DAY, Day,
DY, Dy


20, 21
1, 2, 3 4

, ,
2006, 006, 06, 6
, .
RR 
, JANUARY,
Jan .
uary, JAN Jan
/

01, 02, . . . , 12
0152; 15
,

,

,

: ,
SUNDAY, Sunday,
, SUN, Sun
,

 Ora le 10g  . 351

 SQL

,

5.7.

HH24, HH

MI

SS

 -



351

/ ; , : .

23, 11
24- 12- 

: 059


. : 059
,
DD-MON-YYYY;

HH:MM:SS;
(, ,
YYYY/MM/DD
, ,
, )
(AM, A.M.)
12:45 P.M.
(PM, P.M.)

AM, A.M., PM,


P.M.
AD, BC, A.D., (BC, B.C.) 1452 B.C.
B.C.
(AD, A.D.)
TH
 1ST, 2ND, 3RD

15TH
SP

DDSP FIFTEEN (
TZR

15)
PST, EST

, . , , , ,
, .
, .
 TO_DATE , .
1. SQL*Plus, Ora le .
2. :

SELECT TO_DATE('January 10, 2006','MONTH DD, YYYY') AS "Date-1",


TO_DATE('10/17/1999 14:23:39', 'MM/DD/YYYY HH24:MI:SS')
AS "Date-2"
FROM DUAL;

 Ora le 10g  . 352

352

5.

,  
Ora le:DD-MON-YY. , ,  .
. , ,
(, ,
, ),
DUAL.

3.  . 2 SELECT,
 TO_DATE  TO_CHAR. ,  /. :

SELECT TO_CHAR(TO_DATE('January 10, 2006','MONTH DD, YYYY'),


'MM/DD/YYYY HH:MI:SS AM') AS "Date-1",
TO_CHAR(TO_DATE('10/17/1999 14:23:39', 'MM/DD/YYYY HH24:MI:SS'),
'Month DD, YYYY HH:MI:SS AM')
AS "Date-2"
FROM DUAL;
 . 5.23.
4. ,
Redwood Realty .

CLEAR SCREEN
SELECT TO_DATE('01/01/'||YearBuilt, 'MM/DD/YYYY') AS "Computed",
YearBuilt AS "Data Col".
FROM Properties
WHERE YearBuilt > 1994 AND UPPER(City) = 'MCKINLEYVILLE';
 . 5.24.
, 01/01 .
5. exit, <Enter>, Ora le,
SQL*Plus.
TO_DATE TO_CHAR .
. INSERT  ,  TO_CHAR
VALUES.

TO_NUMBER
TO_NUMBER :
TO_NUMBER(<str> [,<format-string>)

 Ora le 10g  . 353

 SQL

. 5.23. 

353

TO_DATE

. 5.24.

<str>  (, ..), .  TO_CHAR TO_DATE,


TO_NUMBER , , . 
, . 5.5. , StreetNumber , 400
.

SELECT TO_NUMBER(StreetNumber) + 400


FROM DUAL;


Ora le , .  DECODE, NVL, NVL2, TRANSLATE CASE, .
CASE 4 ( 
CASE), .

 Ora le 10g  . 354

354

5.

DECODE

 Ora le DECODE,
, , IF-THEN-ELSE SQL, PL/SQL. DECODE :

DECODE(<value>, <sear h-value1>, <result1>,


[<sear h-value2>, <result2>,... <default-result>)
<value> <sear h-value1>. <value> <sear hvalue1> , Ora le <result1>.
<sear h-value1> () <sear h-value>/<result>, <value>
<sear h-value>. ,
<sear h-value>.
, <default-result>,
. <default-result> , NULL.  DECODE.
SELECT DECODE(YearInS hool,1,'Freshman',2,'Sophomore',3,'Junior',
4,'Senior')
FROM S hoolData;
SELECT InventoryID, ItemName,
DECODE(InventoryStatus,
0,'Ba kordered',
1,'Available',
2,'Near reorder point',
'Unknown')
FROM Inventory;
DECODE YearInS hool (1, 2, 3
4) Freshman (), Sophomore
(), Junior () Senior () . , ,  .
, , .
InventoryStatus NULL ,
0, 1 2,  DECODE  Unknown.

NVL NVL2

NVL NULL NULL . NVL2


, NULL
, NULL. NVL
:

NVL(<input>,<value>)

 Ora le 10g  . 355

 SQL

355

<input> , <value>  , ,
NULL. , <value> .  <input> ,  , <value>. , ,
Ora le ORA-01722: invalid number.

SELECT NVL(BidPri e,'Null BidPri e') FROM CustAgentList;


,  TO_CHAR BidPri e ,
Null BidPri e  NVL.

SELECT NVL(TO_CHAR(BidPri e),'Null BidPri e')FROM CustAgentList;


NVL  ,
NULL. ,
NULL -
NULL. ,  NVL , ,
, ( 
NULL).  
SELECT, , BasePay (),
NULL, In entive () NULL.

SELECT BasePay+In entive FROM Employees;


,  NVL, , Em+ployees NULL, 0.
, , NULL.
, SELECT :

SELECT BasePay+NVL(In entive,0) AS Compensation FROM Employees;


 NVL2 Ora le , , . :

NVL2(<input>,<value-if-not-null>,<value-if-null>)
,

<input> NULL,  <input> NULL.


 NVL
<input>.
TRANSLATE
TRANSLATE
.
.  TRANSLATE .

 Ora le 10g  . 356

356

5.

TRANSLATE (<string1>,<string- hars-to-repla e>,<repla ement- hars>)


<string1>  , Ora le
. Ora le , , . , , .
. , TRANSLATE
( )
. 
WHERE  ( ). ,
, TaxID,
, 
(XXX-XX-XXXX, ).  TRANSLATE
TaxID, .

SELECT FirstName, LastName, TaxID AS "Bad Format"


FROM Agents
WHERE TRANSLATE(TaxID,'0123456789','XXXXXXXXXX') <> 'XXX-XX-XXXX';
WHERE Ora le TaxID
X  ( , X, ).
TaxID ( ) , XXXXX-XXXX. . TaxID
, .
, . SQL*Plus, Ora le .
.
1. ,
SaleStatusID Listings.
DECODE .

SELECT DISTINCT DECODE(SaleStatusID,


101,'For Sale',
102,'Pending',
103,'Sold',
'UNKNOWN') AS "Status"
FROM Listings;
2. SQL,
Li ensed Something Else Li enseStatusID:

 Ora le 10g  . 357

 SQL

357

SELECT DISTINCT DECODE(Li enseStatusID,


1001,'Li ensed', 'Something Else') AS "Li ense Status"
FROM Agents;
Ora le , ,
, Li ensed, .
DISTINCT, , Ora le.
3. SQL,  (, ).
WHERE.

SELECT
NVL(HomePhone,'unknown') AS "Home Phone",
NVL(CellPhone,'unknown') AS "Cell Phone",
NVL(WorkPhone,'unknown') AS "Work Phone"
FROM Customers WHERE CustomerID < 25030;
 . 5.25.
.
4. Agents, SELECT
. :

CLEAR SCREEN
COMMIT;
UPDATE Agents
SET WorkPhone = TO_CHAR(SYSDATE, 'HH:MI:SS')
WHERE Gender = 'F';
5. TRANSLATE,  WorkPhone,  (ddd) ddd-dddd ( d 
). :

SELECT LastName, WorkPhone FROM Agents


WHERE TRANSLATE(WorkPhone,'9876543210','aaaaaaaaaa') <>
'(aaa) aaa-aaaa';
. 5.26 , .
6. ,
TRANSLATE, :

ROLLBACK;
7. exit, <Enter>, Ora le,
SQL*Plus.

 Ora le 10g  . 358

358

5.

. 5.25. 


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

.
, , 
AVG, COUNT, MAX, MIN SUM (. 5.8). ,  
 GROUP BY SELECT,

 Ora le 10g  . 359

 SQL

. 5.26.

5.8.

AVG(<n>)
COUNT(*)
MAX(<n>)
MIN(<n>)
SUM(<n>)

359

TRANSLATE , 

<n>
,
<n>
<n>
<n>

.
HAVING,  . GROUP
BY HAVING .
 :

Aggregate-fun tion-name([DISTINCT|ALL expression)


ALL - ,
 Ora le
( NULL). DISTINCT 
.

 Ora le 10g  . 360

360

5.

, .

NULL ,
.

COUNT, MAX MIN ,


, /.

DISTINCT .

AVG, MAX MIN


AVG .
- , ,
:

SELECT MIN(AskingPri e), AVG(AskingPri e), MAX(AskingPri e)


FROM Listings;
,

WHERE, ( , ) .
, , , , ,
2007.
,
.

SELECT MIN(AskingPri e), AVG(AskingPri e), MAX(AskingPri e)


FROM Listings
WHERE EndListDate BETWEEN '01-JAN-07' AND '31-MAR-07';
 . , Produ ts, SELECT, Pri e Cost , 
 TO_CHAR:

SELECT TO_CHAR(AVG(Pri e-Cost),'$999.99) AS "Average Profit"


FROM Produ ts;
 AVG  TO_CHAR, Ora le
Produ ts, Pri e Cost NULL, 
  TO_CHAR.
, , ,  MIN
.
, MAX . MIN MAX

 Ora le 10g  . 361

 SQL

361

. , , . ,
,  . A  B ..  MAX ,
  . ,
  ( ) ,
Properties.

SELECT MAX(Address) FROM Properties;


991 Bayside Rd,
, .
  Aaron.

SELECT MIN(LastName) FROM Customers;


, Ora le : ORA-01722: invalid number.
 DISTINCT
. , ,
( ) , Properties.
 :

SELECT AVG(SqFt) FROM Properties; (1)

SELECT AVG(DISTINCT SqFt) FROM Properties; (2)


  1784,57 (),  1874,41.
, SqFt,
, . , , , Bedroom 2, 3, 2,
5, 3 3.  AVG (2+3+2+5+3+3)/6, 3,00.
AVG (DISTINCT) (2+3+5)/3,0 1,67.

 SUM COUNT
SUM , , , . , , Ora le ,
 SUM - .
DISTINCT Ora le
. ALL ( , , ) Ora le .
, SUM WHERE,
. , ,

 Ora le 10g  . 362

362

5.

3% BidPri e.
, 2006 .

SELECT SUM(BidPri e * 0.03)


FROM CustAgentList
WHERE Conta tDate BETWEEN '01-NOV-06' AND '30-NOV-06';
BidPri e ,
0,03 .
Redwood Realty, , 322069,77
( ). WHERE,
 2119914,81 ( ).
COUNT .
 , /.
.

COUNT(*) , ,
NULL.
COUNT(<expression>) ,
, NULL.
COUNT(DISTINCT <expression>) ,
NULL , .
COUNT :
COUNT(* | [|DISTINCT|ALL <expression>)

, , :

COUNT (*)
,
 COUNT:

COUNT(DISTINCT < olumn-name>)

, , , NULL, :

COUNT(< olumn-name>)


, WHERE, , Ora le , . , , ,
Properties Redwood Realty.
, :

SELECT COUNT(*) FROM Properties;


,
* (, 1 7, `wow'):

 Ora le 10g  . 363

 SQL

363

SELECT COUNT(7) FROM Properties;


 2000. Ora le , ,
.., .. Properties 2 000 . , , (Customers)
. (Ori k). , ,
. , ,
. Customers.

SELECT COUNT(*)
FROM Properties
WHERE City = 'Ori k';
DISTINCT  COUNT, .
DISTINCT SELECT, . , DISTINCT
COUNT , Ora le  . ? ,
. SQL*Plus, Ora le
.
 COUNT, .
1. :

SELECT DISTINCT COUNT(City) AS "Unique Cities"


FROM Customers;
? , 2500, 

Customers, , .
2.  .

SELECT COUNT(DISTINCT City) AS "Unique Cities"


FROM Customers;
? ( / , DISTINCT City DISTINCT UPPER(City).) Ora le 9. , City Customers  9 .
3. , , Customers,
WorkPhone? ,
:

SELECT COUNT(*)
FROM Customers
WHERE WorkPhone IS NULL;

 Ora le 10g  . 364

364

5.

. 5.27. 

COUNT

Ora le 1991. , 2 500


1 991 .
4. . , ,
:

Properties?

SELECT COUNT(*) FROM Properties WHERE LOWER(City) = 'ar ata';


5. . (M Kinleyville)
Properties? , . ( ,
,  . , 
.)

SELECT COUNT(*) FROM Properties


WHERE LOWER(City) LIKE 'm k%';
 . 5.27.
6. SQL*Plus ,
  COUNT .
, 4 CASE? CASE .

 Ora le 10g  . 365

 SQL

365

 COUNT
CASE. ,
.

SELECT COUNT(1)
FROM Listings
WHERE AskingPri e BETWEEN 100001 200000;
Ora le 455,
100 001 200 000 . ,
, SELECT,
. , ,
. : -
, ? , .
CASE COUNT,
AskingPri e , .
GROUP BY, , , , 
. , 100 000 .
 : CASE/WHEN/
ELSE  COUNT. CASE 1, AskingPri e
, NULL.
 COUNT ,
, .
, .
, Redwood Realty AskingPri e : 0 600 000
100 000. () ,
600 000 .
COUNT CASE , .
1. SQL*Plus CLEAR SCREEN, <Enter>, :

SELECT
COUNT(CASE WHEN AskingPri e BETWEEN 0 AND 100000
THEN 1 ELSE NULL END) "<= 100000"
FROM Listings;
Ora le 6 <= 100000.
2.  SELECT . 1 .
, , -

 Ora le 10g  . 366

366

5.

. 5.28.

. ( SELECT
, SQL*Plus. ,
, .) , . 5.28.

SELECT
COUNT(CASE WHEN AskingPri e
THEN 1 ELSE NULL END)
COUNT(CASE WHEN AskingPri e
THEN 1 ELSE NULL END)
COUNT(CASE WHEN AskingPri e
THEN 1 ELSE NULL END)
COUNT(CASE WHEN AskingPri e
THEN 1 ELSE NULL END)
COUNT(CASE WHEN AskingPri e
THEN 1 ELSE NULL END)
COUNT(CASE WHEN AskingPri e
THEN 1 ELSE NULL END)
COUNT(CASE WHEN AskingPri e
THEN 1 ELSE NULL END)
FROM Listings;

BETWEEN 0 AND 100000


"<= 100000",
BETWEEN 100001 AND 200000
"<= 200000",
BETWEEN 200001 AND 300000
"<= 300000",
BETWEEN 300001 AND 400000
"<= 400000",
BETWEEN 400001 AND 500000
"<= 500000",
BETWEEN 500001 AND 600000
"<= 600000",
> 600000
"> 700000"

3. SQL*Plus exit, <Enter>, Ora le,


SQL*Plus.


 GROUP BY SELECT,
, 

 Ora le 10g  . 367

 SQL

367

. , .  GROUP
BY , ,
. Ora le
, .
,  GROUP BY, , . ,
Properties,
. , , :

SELECT DISTINCT City FROM Properties ORDER BY City;


Ora le ,
. (Ar ata). , , (
).

SELECT COUNT(*), AVG(SqFt), 'Ar ata' AS "City"


FROM Properties WHERE City = 'Ar ata';
,  . ,
 -
, , Properties
. - .
  GROUP BY SELECT.
,
, SELECT. Ora le , ,
.
SELECT, ,
SELECT.

SELECT City, COUNT(*), AVG(SqFt)


FROM Properties
GROUP BY City;
City SELECT . Ora le City, , .
 GROUP BY, SELECT. ,
, 
SELECT. , ,

 Ora le 10g  . 368

368

5.

SELECT, GROUP BY.


, . , SELECT  City Bedrooms
( ), SELECT .
SELECT City, Bedrooms, COUNT(*), AVG(SqFt)
FROM Properties
GROUP BY City, Bedrooms;
, GROUP BY . , GROUP BY, : GROUP BY ,
, , . ,
GROUP BY,  SELECT.
, ,
Properties.

SELECT Bedrooms, Bathrooms FROM Properties


GROUP BY Bedrooms, Bathrooms;
, Ora le 18 , 2 /1  6 /4 .

HAVING
HAVING SELECT () , . HAVING
GROUP BY , GROUP BY. WHERE, HAVING  , WHERE
 , Ora le  , ,  HAVING. WHERE,
.
HAVING, , ,
, , . -
HAVING  WHERE . HAVING
SELECT :

SELECT ...
FROM ...
WHERE ...
GROUP BY ...
HAVING ...
ORDER BY ...;
HAVING  SELECT. ,  -

 Ora le 10g  . 369

 SQL

369

; HAVING 
. SELECT:

SELECT City, COUNT(*)


FROM Properties
GROUP BY City
HAVING AVG(SqFt) > 1800;
, ,
1800  ( Ora le  ). ,
GROUP BY HAVING.
SQL*Plus.
 
, .
1. SQL*Plus, , Properties
:

SELECT City, COUNT(*)


FROM Properties
GROUP BY City
ORDER BY 2 DESC;
2.  ,
, 5 .

WHERE,

SELECT City, COUNT(*)


FROM Properties
WHERE Bedrooms >= 5
GROUP BY City
ORDER BY 2 DESC;
 . 5.29.
3.  , WHERE HAVING, , 300 . ORDER BY. :

CLEAR SCREEN
SELECT City, COUNT(*)
FROM Properties
GROUP BY City
HAVING COUNT(*) > 300;
4.  Properties .
.

 Ora le 10g  . 370

370

5.

. 5.29.

GROUP BY WHERE

COLUMN AVG(SqFt) FORMAT 9999


COLUMN AVG(YearBuilt) FORMAT 9999
SELECT City, MIN(SqFT), AVG(SqFt), MAX(SqFT), AVG(YearBuilt)
FROM Properties
GROUP BY CITY
ORDER BY City DESC;
 . 3 4 . 5.30.
.
5. CLEAR COLUMNS <Enter>,
.
6.  . exit <Enter>, SQL*Plus.

SQL*Plus

5 iSQL*Plus () SQL*Plus ()
, , . , CLEAR COLUMN

 Ora le 10g  . 371

 SQL

. 5.30. SELECT

371

GROUP BY

COLUMN.
SQL*Plus ( , , SQL) ,  . SQL*Plus
, , ,  , , .
 .  ,
(. 5.9). , SQL*Plus , ,
, Ora le Reports (
10).
, SQL*Plus, . 5.9, ,
SQL*Plus, ,  ,
 . , SQL*Plus
 .  SQL*Plus, ,
 , .
, ,  . ,
SQL*Plus.

 Ora le 10g  . 372

372

5.

5.9.

SQL*Plus


ACCEPT
BREAK
BTITLE
CLEAR
COLUMN

COMPUTE
EXIT [n
LINESIZE
PAGESIZE

PROMPT
SET
SHOW
SPOOL _
SPOOL OFF
SPOOL OUT
TTITLE
UNDERLINE

SQL ( , START)

, 
 

SQL*Plus, , ..


(n 
)

. ,
-


SQL*Plus





, .  (-)

SQL*Plus
SQL*Plus , . , SQL*Plus,
, SQL*Plus , . ,
, SQL*Plus. -

 Ora le 10g  . 373

 SQL

373

SQLPROMPT , ,
SQL> .
, Ora le,
:

SET SQLPROMPT Ora le>


UNDERLINE,
,
( ). , . 5.9
LINESIZE PAGESIZE. , SQL*Plus
( ),
,
. SQL*Plus, SQL*Plus
:

SHOW ALL
SHOW ALL 
. , LINESIZE
80 ( 80 ); PAGESIZE 14.
,
 . , ,
SPOOL ON. , FEEDBACK  OFF, ,
29 rows sele ted. , BTITLE, COLUMN,
TTITLE .. , , , 
, , , SQL*Plus. , SQL*Plus
(
). , , , ,
. ,
 SQL*Plus,
.


SQL*Plus COLUMN  , . ,
HELP COLUMN SQL*Plus, , COLUMN.
:

 Ora le 10g  . 374

374

5.

COLUMN [ olumn-name| olumn-alias [options


< olumn-name> < olumn-alias> SELECT, COLUMN.
SELECT
SELECT, , .
<options> : CLEAR ( ); FORMAT (  );
ON OFF ( ). , WRAPPED, WORD WRAPPED, JUSTIFY NULL. ,
COLUMN  FirstName
Agents.

COLUMN LastName FORMAT A15 HEADING "Last Name"

SELECT
LastName 15 Last Name. ,  AskingPri e Listings.
COLUMN AskingPri e FORMAT $9,999,999 HEADING "Asking|Pri e"
COLUMN AskingPri e: , , Asking Pri e .
, AskingPri e, :
COLUMN AskingPri e OFF
,

COLUMN AskingPri e ON
, , :

COLUMN < olumn-name>


 , :

COLUMN < olumn-name> CLEAR


, .
( ) SQL*Plus Ora le.
 ,
.
1. ,  <>Ch05Report1.sql,
SQL*Plus. SQL*Plus
<Enter> , .

CLEAR SCREEN
SET FEEDBACK OFF
CLEAR COLUMNS
SET LINESIZE 60

 Ora le 10g  . 375

 SQL

375

SET PAGESIZE 40
SET UNDERLINE =
COLUMN Address FORMAT A25 HEADING "Address" TRUNCATED
COLUMN City FORMAT A10 HEADING "City"
COLUMN SqFt FORMAT 9,999 HEADING "Square|Feet"
COLUMN YearBuilt FORMAT 9999 HEADING "Year|Built"
2. SQL*Plus, ,
 (, ),
, ,  ,
SELECT.
3. Properties  ,
 . 1. , , , SQL*Plus :

SELECT Address, City, Sqft, YearBuilt


FROM Properties
WHERE City = 'Loleta'
ORDER BY 2,4 DESC;
4. , SQL*Plus, ,
, ,
. 5.31.
5.

Address, :

COLUMN Address
SQL*Plus 

Address.

6. (, )  ,
, SQL*Plus :

CLEAR COLUMNS
SET LINESIZE 80
SET PAGESIZE 14
SET UNDERLINE '-'
SET FEEDBACK ON
.   .
SQL*Plus  , , SQL*Plus
.

7. exit <Enter>, SQL*Plus


Ora le.
8. , File (), Save (),
. .

 Ora le 10g  . 376

376

5.

. 5.31.

SQL*Plus, 
SQL*Plus, ,
, SQL*Plus. , . 4, 
,
. , SQL,
,  ,
.

,
( Ch05Report1.sql),
. . 5.9, SQL*Plus
TTITLE BTITLE ,
. :

TTITLE | BTITLE [options [text|variable... | [ON|OFF



:

TTITLE OFF

TTITLE ( BTITLE), -

 Ora le 10g  . 377

 SQL

377

, TTITLE ON. ,
LEFT, CENTER RIGHT,
 , .
FORMAT .
, COLUMN. SKIP n ,
. BTITLE TTITLE
SQL*Plus. ,
SQL*Plus SQL.LNO, SQL.PNO SQL.USER. SQL.LNO
; SQL.PNO  ; SQL.USER 
, . TTITLE
.

TTITLE LEFT SQL.USER CENTER 'Properties Available' RIGHT 'Confidential' SKIP 3


, , 
SQL*Plus (, ). ,
TTITLE . SQL*Plus . ?
, , .
Confidential. , SKIP 3 <Enter>. SKIP
. SQL*Plus . TTITLE ( BTITLE),
:

TTITLE OFF
,
BTITLE TTITLE (''):

TTITLE ''

BTITLE, ( PAGESIZE) .

BTITLE CENTER 'Page ' FORMAT 999 SQL.PNO


PAGESIZE, SQL*Plus BTITLE  (
TTITLE). BTITLE
, Page, , . SQL*Plus
LINESIZE. LINESIZE
60, 30.

 Ora le 10g  . 378

378

5.

  Ch05Report1.sql, . SQL*Plus, Ora le 


<>Ch05Report1.sql.  .
  , ,
.

SET FEEDBACK OFF :


TTITLE LEFT 'Properties' CENTER 'Redwood Realty' RIGHT 'Confidential' SKIP 2
BTITLE CENTER 'Page ' FORMAT 99 SQL.PNO
BREAK ON City SKIP PAGE

1.

. BREAK SQL*Plus,
( )
City. ,
, ,  BREAK (  City).

2. , PAGESIZE:

PAGESIZE 22

3.  WHERE SELECT, . (Ori k),


.

WHERE City IN ('Loleta', 'Ori k')


4. , SELECT, Column Address SET FEEDBACK ON.   , . 5.32.
5.  

<>Ch05Report2.sql.

6.   SQL*Plus,
<Enter>, , .
. 5.33. ,
,  . .
7. exit <Enter>, Ora le
SQL*Plus.
, .

HTML- SQL*Plus
SQL*Plus Web, , HTML-. 
Web,  , Web-

 Ora le 10g  . 379

 SQL

379

. 5.32.   ,

Web-. HTML- , 
. , SQL*Plus
SELECT, . SQL*Plus,
HTML-, :

SET MARKUP HTML ON SPOOL ON


 . MARKUP , SQL*Plus HTML. SPOOL ON SQL*Plus   HTML- <html>, <head> <body> ,
. MARKUP 
 HTML-  SPOOL <filename>.
  SQL*Plus SPOOL OFF,
SQL*Plus HTML-, :

SET MARKUP HTML OFF


HTML-,
SQL*Plus HTML- (<td>, <tr>
..). Web- , 
HTML-, . SQL*Plus MARKUP
Web-.
, .
-, Web- ,
SQL*Plus PAGESIZE , . , Web, . -, FEEDBACK OFF, Web- ,

 Ora le 10g  . 380

380

5.

. 5.33. 

354 rows sele ted. ,


SQL*Plus . ,
SQL*Plus, .
 SQL*Plus, ,
, Web-.  (Customers) , .
Customers  
Web  CustomerReport.html. , Web-, Web-.
,  , HTML-, .
1.


<>Ch05WebReport.sql , <>
.

2. , ,

 Ora le 10g  . 381

 SQL

381

,
Web-. ( ACCEPT.)

SET FEEDBACK OFF


CLEAR SCREEN
SELECT DISTINCT City "City Names" FROM Customers ORDER BY City;
PROMPT
ACCEPT ityname PROMPT 'List Customer rows for ity: '
3. :

SET TERMOUT OFFSET VERIFY OFF


SET PAGESIZE 50000
SET MARKUP HTML ON SPOOL ON
SPOOL C:\CustomerReport.html
4. SELECT,
,  & ityname:

SELECT FirstName, LastName, Address, City,


HomePhone, CellPhone, WorkPhone
FROM Customers
WHERE UPPER(City) = UPPER('& ityname')
ORDER BY 2,1;
5. ,
  SQL*Plus :

SPOOL OFF
REM SQL*Plus
SET MARKUP HTML OFF
SET PAGESIZE 14
SET VERIFY ON
SET FEEDBACK ON
SET TERMOUT ON
6.  , File (),
Save ().
7.

File

(),

Exit

(), .

 Web-,
.
 SQL*Plus, Web-, .
1. SQL*Plus, Ora le, 
, .
 <> <>.

START <><>Ch05WebReport

 Ora le 10g  . 382

382

5.

. 5.34. , 

2. ori k <Enter>, . , . 5.34: ,


.
3.

exit <Enter>, SQL*Plus.

4. Web-, Web- CustomerReport.html , Web- , . . . 5.35.


5. , Web-. ,
 CustomerReport.html.


SQL SELECT . (), ,
. , WHERE , . GROUP BY 
; HAVING  , ORDER BY . ,
DISTINCT.
WHERE , , ,  . true, ;  . SQL SQL, BETWEEN ... AND, IN,
IS NULL LIKE, , , NULL,
.

 Ora le 10g  . 383

 SQL

383

. 5.35. Web-, 

ORDER BY, 
SELECT, ,
( ). ORDER BY
, . ORDER BY n   n .
SQL
WHERE.
, , . SQL
, ,
. SQL
. , NVL, , ,
, NULL, ,
.
  ,

 Ora le 10g  . 384

384

5.

, ,
.
, SQL*Plus HTML-, Web-. HTML- MARKUP HTML ON SPOOL ON. ,
SQL*Plus SPOOL,
HTML-, . , 
 .

ASC / DESC

(*)

BETWEEN









GROUP BY

HAVING
IN

LIKE

ORDER BY

 Ora le 10g  . 385

 SQL

385

WHERE

?
1. SELECT
.

FROM.

2. Agent, ,
, IN WHERE,
.
3. , . , ORDER BY
.
4. , AVG,
NULL. , , NULL,
, .
5.
,
SELECT, .


1. _____ 
 .
2.

SELECT

% , LIKE '%month', _____ .

3. ,
_____.
4. ,
FROM _____ _____ ( ).
5. - ,
 _____ ,
, .

 Ora le 10g  . 386

386

5.


1. City,
SELECT?
) ORDER BY.

MAX(BidPri e), 

DISTINCT.
) GROUP BY.
) HAVING.

2. SELECT City FROM Properties;?


) GROUP BY.

UNIQUE.
) NVL.
) DISTINCT.

3. ,
LECT FirstName||LastName FROMAgents;?
) .

SE-

) .
) .
) .
4. 
) .

TO_DATE, ?

) .
) .
) .
5. SQL*Plus ,
SELECT , 15 rows sele ted?
) SQLPROMPT.

PAGESIZE.
) LINESIZE.
) FEEDBACK.

 Ora le 10g  . 387

 SQL

387

1. Readwood Realty
 Redwood Realty .
,  Redwood Realty.
 SQL,
START <s riptfilename>.
 ,  , HTML-, .
, .
1. SQL*Plus Ora le,
. (
), .
2.  , AskingPri e Listings .
AgentID.  <>Ch05Prob1S ript1.sql. 
SQL*Plus . <Your full name here>
.

REM < >


CLEAR COLUMNS
SET PAGESIZE 35
SET FEEDBACK OFF
CLEAR SCREEN
COLUMN Stat1 FORMAT $9,999,999 HEADING
'Minimum'
COLUMN Stat2 FORMAT $9,999,999 HEADING
'Average'
COLUMN Stat3 FORMAT $9,999,999 HEADING
'Maximum'
COLUMN Stat4 FORMAT 999 HEADING 'Count'
SELECT ListingAgentID "Agent ID",
MIN(AskingPri e) AS "Stat1",
AVG(AskingPri e) AS "Stat2",
MAX(AskingPri e) AS "Stat3",
COUNT(AskingPri e) AS "Stat4"
FROM Listings
GROUP BY ListingAgentID
HAVING COUNT(AskingPri e) > 18
ORDER BY "Stat4" DESC;
SET FEEDBACK ON
3. , ,
SQL*Plus .  
<> ( ) <> ( ).

 Ora le 10g  . 388

388

5.

CLEAR COLUMNS
SET PAGESIZE 35
CLEAR SCREEN
COLUMN "Name" FORMAT A25 HEADING
'Agent"s Name'
COLUMN "Age" FORMAT A10 HEADING
'Age at|Hire Date'
SPOOL C:\<>\<>Ch05Prob1Part1.txt
SHOW USER
SELECT LastName||', '|| FirstName AS "Name",
LPAD(TO_CHAR((HireDate-BirthDate)/365.25,
'99'),7) AS "Age"
FROM Agents
WHERE (HireDate-BirthDate)/365.25 > 30.00
ORDER BY 2 DESC;
SPOOL OFF
SET PAGESIZE 14
4.  

<>Ch05Prob1Part1.txt .

5. , . () . .
<> , HTML-.
 <>Ch05Prob1Part2.sql, <>
. .

SET TERMOUT OFF


SET FEEDBACK OFF
SET VERIFY OFF
SET ECHO OFF
TTITLE OFF
CLEAR COLUMNS
SET PAGESIZE 50000
SET MARKUP HTML ON SPOOL ON
SPOOL C:\<>\Ch05Customers.html
SELECT FirstName, LastName, Address, City
FROM Customers
WHERE UPPER(City) = 'BLUE LAKE'
ORDER BY 2,1;
SPOOL OFF
SET MARKUP HTML OFF
SET PAGESIZE 14
SET ECHO ON
SET VERIFY ON
SET FEEDBACK ON
SET TERMOUT ON
6. SQL*Plus , <>
 SQL, <>  ,
<Enter>,  :

START
C:\<>\<>Ch05Prob1Part2.sql

 Ora le 10g  . 389

 SQL

389

7. SQL*Plus ( exit <Enter>), Web (Ch05Customers.html), . 5, Web . Web-


, .
8.  , .

2. Coffee Mer hant


,
Coffee Mer hant. , States.
, , ,
( ). ,  , ,
Web-.  BuildCoffee.sql CoffeeMer hant.
Coffee Mer hant. .
1. SQL*Plus, Ora le, SQL , <> :
START <>\BuildCoffee
2. . SQL*Plus. 
<>Ch05Problem2Part1.sql. <> ( ). <>
 ( , ).

REM 
CLEAR SCREEN
SPOOL <>\<>Ch05Problem2Report1.txt
SHOW USER
SET PAGESIZE 55
CLEAR COLUMNS
COLUMN StateID FORMAT A03
HEADING 'ID'
COLUMN StateName FORMAT A14
HEADING 'State'
COLUMN Population FORMAT 99,999,999
HEADING 'Population'
COLUMN LandArea FORMAT 999,999
HEADING 'Land Area'
COLUMN Density FORMAT 99,999
HEADING 'Density'
SELECT StateID, StateName, Population,
LandArea, Population/LandArea AS Density
FROM States
WHERE UPPER(StateID) <> 'DC'

 Ora le 10g  . 390

390

5.

ORDER BY Density DESC;


SPOOL OFF
3. , SQL*Plus . <> <>.
 15
. .

REM 15
CLEAR SCREEN
SPOOL <>\<>Ch05Problem2Report2.txt
SHOW USER
SELECT StateName, "Length"
FROM (SELECT StateName, LENGTH(StateName)
AS "Length"
FROM STATES
WHERE UPPER(StateID) <> 'DC'
ORDER BY "Length" DESC)
WHERE ROWNUM < 16;
SPOOL OFF
CLEAR COLUMNS
SET PAGESIZE 14
4. , ,
.
SQL*Plus. ,
,  , . Column
 . ,
,  .

REM 
CLEAR SCREEN
SPOOL <>\<>Ch05Problem2Report3.txt
SHOW USER
COLUMN Name FORMAT A33
HEADING 'Coffee Name'
COLUMN OnHand FORMAT 99,999
HEADING 'In Sto k'
COLUMN Pri e FORMAT $999.99
HEADING 'Pri e'
COLUMN TotValue FORMAT $999,999
HEADING 'Total|Value'
SELECT Name, OnHand, Pri e, Pri e*OnHand AS
TotValue
FROM Inventory
WHERE UPPER(ItemType)='C' AND OnHand > 0
ORDER BY 4 DESC;
SPOOL OFF
5. , , . <>Ch05Problem2Part2.sql. . <> <>.
,  .

 Ora le 10g  . 391

 SQL

391

SET TERMOUT OFF


SET FEEDBACK OFF
SET ECHO OFF
CLEAR COLUMNS
SET PAGESIZE 234
SET MARKUP HTML ON SPOOL ON
SPOOL <>\<>Ch05Problem2Part2WebPage.html
SELECT Name, Pri e, OnHand, Des ription,
CASE WHEN ItemType='C' THEN 'Coffee'
ELSE 'Tea' END AS "Type"
FROM Inventory
ORDER BY "Type", Name;
SPOOL OFF
SET MARKUP HTML OFF
SET PAGESIZE 14
SET ECHO ON
SET FEEDBACK ON
SET TERMOUT ON
6. SQL*Plus  .
, <path> , <>  :

START <>\<>Ch05Problem2Part2.sql
7. Web-, . 6, .
8.   ,
SQL*Plus.

3. Rowing Ventures
RowingVentures, , 
,
. ,
RowingVentures  BuildRowing.sql ( - , 5). Rowing Ventures, SQL*Plus
 . , DESCRIBE, , .
,  . 
 .

<>Ch05Problem3Report1.txt, Nation, OrganizationName, Address, City State (


) Organization. BREAK,

.
 , COLUMN, ,
(, Organization ID Address) 15 .

 Ora le 10g  . 392

392

5.

SQL*Plus 25 .
, 10 rows sele ted. TTITLE, :  , 
Organizations by Nation,  . SPOOL
OFF .

<>Ch05Problem3Report2.txt, Person.
FirstName LastName.   , . ,
, . ( MAX(LENGTH(< olumnname>))  .) (TTITLE) () (
). .
 , 1963 .

4. Broad loth Clothing


, Broad loth
 BuildClothing.sql. Broad loth Clothing,
SQL*Plus  . ,
DESCRIBE, .
Conta t Web-.  , Web-
. , .  
. .

Produ tionBat h, , StartDateTime ,


2006 . .

, , -

, ,
,   . , 200.  Shipment.
( , DESCRIBE Shipment.) 
, .

 Ora le 10g  . 393




. . .

,

, ,

UNION, MINUS INTERSECT,


,


,

 Ora le 10g  . 394

394

6.



SQL , . 
,  , 
. ,
. , 5, . , , . (join)
, . 
, .

, ( ).
, Ora le , , .
FROM ( ) WHERE.
, , ,
.
.
(equijoin),
() .
,
.
. . 6.1 . ,
Ora le ,  ID Employees Emp# Sales. , Sales
Emp#, .
Emp#  , Employees, 
- .
( ) - , , ,
. , , 
. , Redwood Realty
. 

 Ora le 10g  . 395

ID

Employees
Name

Sales
Div

1001
1002

Bob
Dawn

Mkt
Sales

1003
1006

Betty
Nancy

Sales
Mkt

1009

Fred

Sales

Employees Sales:
Employees
1002
1002
1003
1003
1009
1009
1009

Dawn
Dawn
Betty
Betty
Fred
Fred
Fred

395

Sales
Sales
Sales
Sales
Sales
Sales
Sales

PK2
21

Emp#
1002

Amount
$2,000

22
23

1002
1003

$2,200
$1,800

24
25

1003
1009

$200
$600

26
27

1009
1009

$750
$425

Sales
21
22
23
24
25
26
27

1002
1002
1003
1003
1009
1009
1009

$2,000
$2,200
$1,800
$200
$600
$750
$425

. 6.1.

.   .
( ),
, .  , ,
. ,
Customers CustAgentList,  CustAgentList Listings. ,
,  , , . , 5
, ,  .
 , ,
.
,

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

 Ora le 10g  . 396

396

6.

,  , .
, , , ,
. (natural join),
. ,
.
( ).

 , ANSI1
SQL99, FROM ,
. ( Ora le,
8, WHERE  .) , ANSI,
.
( -

) ,
.
ANSI SQL99 .
, ,

ANSI, .
 .

USING -

.
, . INNER . , , . ( FROM, SELECT  WHERE,
ORDER BY ..)

SELECT < olumn-list>


FROM <table1> [<alias1> [INNER JOIN <table2> [<alias2>
ON {<table1>|<alias1>}.<join- olumn1> =
{<table2>|<alias2>}.<join- olumn2>;
1

Ameri an National Standards Institute  .

 Ora le 10g  . 397

397

FROM
( )  ON, .   ON ,
, , . ,
, , . , ,  ,
, .
 : Customers
 , Properties  , . Ora le
OwnerID Properties CustomerID Customers.  ( OwnerID)
CustomerID Customers. (Customers) ,
CustomerID/OwnerID. , SELECT, 
.

SELECT FirstName, LastName, Properties.Address, Properties.City


FROM Customers INNER JOIN Properties
ON Customers.CustomerID=Properties.OwnerID;
,
, . (qualied
name) , , .
,
Address City . , Ora le
,  .. , City
. ,
SELECT .
SELECT
ANSI/ISO SQL99. ( Ora le, 9i,  .) , . SELECT,
. (
) WHERE.
 WHERE 
 . ,
.

 Ora le 10g  . 398

398

6.

SELECT FirstName, LastName, Properties.Address, Properties.City


FROM Customers, Properties
WHERE Customers.CustomerID=Properties.OwnerID;
,
, Redwood
Realty. 5, ,
6. ,
,
, . , Redwood Realty, .
Redwood Realty 6, .
1. RedwoodRealty,   BuildRedwood.sql,
, ( 
5). ,
.
2. SQL*Plus Ora le.
3. <path> , 1, :

START <path>\BuildRedwood
Ora le . ,
.
SELECT, , (display olumns), SELECT, FROM,
(sear h olumns), WHERE.
, Ora le
, (join olumns).
,
.

1. SQL*Plus ,
SQL.
2. , 
. , SQL*Plus,
.

CLEAR SCREEN
COLUMN FirstName FORMAT A12
COLUMN LastName FORMAT A12
COLUMN City FORMAT A15
COLUMN Address FORMAT A25

 Ora le 10g  . 399

399


"- "

. 6.2.

Customers Properties

3. , SQL*Plus ,
, . (Ori k):

SELECT FirstName, LastName, Properties.Address, Properties.City


FROM Customers JOIN Properties
ON Customers.CustomerID=Properties.OwnerID
WHERE Properties.City = 'Ori k';
Ora le
tomerID/OwnerID (. 6.2).

Cus-

4. <>Ch06Query01
.sql, <> .
5.  SELECT,
Properties ( ). . .
?

SELECT FirstName, LastName, Address, City


FROM Customers JOIN Properties
ON Customers.CustomerID=Properties.OwnerID
WHERE Properties.City = 'Ori k';
6. SQL*Plus .
. 5 Ora le ,  ,
: ORA-00918: olumn ambiguously defined.
,
, .

 Ora le 10g  . 400

400

6.

FROM :

SELECT < olumn-list>


FROM <table1> [<alias1> [INNER JOIN <table2> [<alias2>
USING (<join- olumn>);
, Ora le
.
, .
1. :

CLEAR SCREEN
SET PAGESIZE 24
SELECT FirstName, LastName, StatusText
FROM Agents JOIN Li enseStatus
USING (Li enseStatusID)
WHERE GENDER = 'M'
ORDER BY 2,1;
2. 1 SQL*Plus .
, , COLUMN, -
FirstName LastName ( ,
SQL*Plus).  . 6.3.
3. SQL*Plus .

 . 
, , , ,
.
. , A , B,
,
. ,
, Ora le . ( ,
,
.) , , .
, ON. :

 Ora le 10g  . 401

401

. 6.3. ,

SELECT < olumn-list>


FROM <table1> [<alias1> NATURAL JOIN <table2> [<alias2>;
Redwood Realty . , CustAgentList Customers
CustomerID. ,
. , Agents Li enseStatus
Li enseStatusID, Properties Listings 
PropertyID. . , Properties Listings,
. , ,
Properties, (AskingPri e)
Listings. PropertyID, ,
. ,
.
,
.
1. .
SQL*Plus . ( <Enter> .)  .

CLEAR SCREEN
SET PAGESIZE 20

 Ora le 10g  . 402

402

6.

. 6.4.

COLUMN Address FORMAT A25 HEADING 'Address'


COLUMN City FORMAT A15 HEADING 'City'
COLUMN Bedrooms FORMAT 99 HEADING 'Br'
COLUMN Bathrooms FORMAT 99 HEADING 'Ba'
COLUMN SqFt FORMAT 9,999 HEADING 'Square|Feet'
COLUMN AskingPri e FORMAT $9,999,999 HEADING 'Asking|Pri e'
BREAK ON CITY SKIP 1
2. SELECT,
. WHERE
. ,
, .

SELECT Address, City, Bedrooms, Bathrooms, SqFt, AskingPri e


FROM Properties NATURAL JOIN Listings
WHERE SqFt >= 2000 AND Bathrooms > 3 AND Bedrooms > 3
ORDER BY City, AskingPri e;
Ora le (. 6.4).
3. SQL*Plus .

.
, Ora le , .
, Ora le ,

 Ora le 10g  . 403

403

, .
, ,
, .
, . (
Ora le, , .
13.)
, Agents, Listing Properties, (), FROM. SELECT
, :

SELECT FirstName, LastName, Address, City,


Bedrooms, Bathrooms, AskingPri e
FROM Agents INNER JOIN Listings
ON AgentID = ListingAgentID INNER JOIN Properties
ON Listings.PropertyID = Properties.PropertyID;
  . , . ,
 Ora le, Agents
Listings, AgentID ListingAgentID.

FROM Agents JOIN Listings


ON AgentID = ListingAgentID

FROM
Listings Properties:

JOIN Properties
ON Listings.PropertyID = Properties.PropertyID;

 . , Listings.PropertyID PropertyID Listings. Properties.PropertyID


PropertyID Properties. , , ,
. ,
, , . ( !) ,
.
FROM, ,
ON. ,
, . ,
ON ,
(. ).

SELECT FirstName, LastName, Address, City,


Bedrooms, Bathrooms, AskingPri e

 Ora le 10g  . 404

404

6.

FROM Agents JOIN Listings JOIN Properties


ON Listings.PropertyID = Properties.PropertyID
ON AgentID = ListingAgentID;
LastName FirstName Agents (
,  ), . .
, ,
.
1. SQL*Plus, ,
SQL*Plus <Enter>, ,
 . ( Agent s COLUMN,
.)

CLEAR SCREEN
CLEAR BREAKS
COLUMN Agent FORMAT A25 HEADING 'Agent''s|Name'
2. , SQL*Plus . Ora le
,
. ,
. 6.5.
SELECT ag.FirstName||' '||ag.LastName AS Agent,

pr.Address, pr.City, li.AskingPri e


FROM Agents ag INNER JOIN Listings li
ON ag.AgentID = li.ListingAgentID
INNER JOIN Properties pr
ON li.PropertyID = pr.PropertyID
WHERE UPPER(pr.City) IN ('ORICK','BLUE LAKE','LOLETA')
ORDER BY pr.City, li.AskingPri e;

3. CLEAR COLUMNS <Enter>, , .


4. , .
5. SQL*Plus. ( SQL*Plus,
 , ..,  .)

, , ( ).
Ora le ( =) , -

 Ora le 10g  . 405

405

. 6.5. 

. ,
.
, BETWEEN, >, < . . ,
, : ,
. . , , ,
NULL.

. ,
.
.

,
, (, ,
).
NULL, . -

 Ora le 10g  . 406

406

6.

ID
1001
1002
1003
1006
1009

Salespersons
Name
Bob
Dawn
Betty
Nancy
Fred

Div
Mkt
Sales
Sales
Mkt
Sales

PK2
21
22
23
24

Sales
Emp#
1002
1002
1003
1003

Amount
$2,000
$2,200
$1,800
$200

Salespersons Sales:
Salespersons
1002
1002
1003
1003
1001
1009
1009

Dawn
Dawn
Betty
Betty
Bob
Nancy
Fred

Sales

Sales
21 1002 $2,000
Sales
22 1002 $2,200
Sales
23 1003 $1,800
Sales
24 1003
$200
Mkt
null null
null
Mkt
null null
null
Sales null null
null

. 6.6.

, , Salesperson,  , Sales,
 (ID) ,  . Salesperson Sales, ,
. , ,
.
, ,
, , NULL  . ,
 , ,  .
,
. 
. 6.6. , , (,
). . 6.7
.
: , .
SELECT , :

 Ora le 10g  . 407

407

Salesperson

Sales

Salesperson


( )

Sales

. 6.7. ,

SELECT < olumn-list>


FROM <table1> [<alias1>
{LEFT|RIGHT|FULL} [OUTER JOIN <table2> [<alias2>
ON {<table1>|<alias1>}.<join- olumn1> =
{<table2>|<alias2>}.<join- olumn2>;
OUTER , , ,
.
, FROM.
, ,
, NULL . ,
RIGHT OUTER JOIN FROM.
, ,
NULL .
, Ora le ,
, , (
).
. ,
, ,
FROM .
, , Redwood Realty
Listings. , ,
, !
.
Redwood Realty , Listings. ,

 Ora le 10g  . 408

408

6.

, Agents. , Listings
, ListingAgentID, AgentID .
Agents , Agents Listings,
.
- , .
1. SQL*Plus, Ora le . ( , SQL*Plus) , Agents. INSERT.

CLEAR SCREEN
REM  .
REM CustAgentList
INSERT INTO Agents(AgentID,FirstName,LastName,Li enseStatusID)
VALUES(23456,'Robert','Sellsmore',1001);
INSERT INTO Agents(AgentID,FirstName,LastName,Li enseStatusID)
VALUES(23471,'Susan','Swarthmore',1001);
INSERT INTO Agents(AgentID,FirstName,LastName,Li enseStatusID)
VALUES(23498,'George','Nagy',1002);
2. COMMIT; <Enter>, 
.
3. , SQL*Plus:

SELECT FirstName, LastName


FROM Agents a LEFT OUTER JOIN CustAgentList
ON a.AgentID = .AgentID
WHERE .ListingID IS NULL
ORDER BY LastName, FirstName;
Ora le  , Listings. ,
. 6.8.
4. ,  .
SQL*Plus  SELECT:

SET PAGESIZE 50
CLEAR SCREEN
SELECT FirstName, LastName, COUNT(ListingID)
FROM Agents a LEFT OUTER JOIN CustAgentList
ON a.AgentID = .AgentID
GROUP BY FirstName, LastName
ORDER BY LastName, FirstName;

 Ora le 10g  . 409

409

. 6.8.

(. 6.9) ,  COUNT
0. , Listings.
5. ,
PAGESIZE:

SET PAGESIZE 14
6. SQL*Plus .

, 
, . , , . (,
ManagerID), , . 
, , EmployeeID.
, FROM SELECT. 
 ,

 Ora le 10g  . 410

410

6.

0

Listings

. 6.9. 

COUNT

, -

. , , 
Employees.

SELECT w.FirstName, w.LastName, s.FirstName, s.LastName


FROM Employees w INNER JOIN Employees s
ON w.ManagerID = s.EmployeeID;
ManagerID (w.ManagerID)  EmployeeID (s.EmployeeID). 
FirstName LastName, FirstName/LastName . Redwood Realty
,
,
Redwood Realty.
Employee ,
.

 Ora le 10g  . 411

411

1. , SQL*Plus Ora le.


2. Windows Explorer , , 6,  Ch06EmpSelfJoin.sql .
3. , 
<path> ( ):

START <path>\Ch06EmpSelfJoin
, ,
EmpSelfJoin .
, . .
, .
1. SQL*Plus :

CLEAR SCREEN
SELECT * FROM EmpSelfJoin;
2.

DESCRIBE EmpSelfJoin
21
. 6.10.

EmpSelfJoin,

 . 6.10. EmployeeID  . (BossID)  . , ,  (Kent Conrad),


EmployeeID, 12765. BossID
10497, , . , 10497 EmployeeID
(John Warner). ,
.  ,
(Bill Frist).  (BossID) 12765.
EmployeeID , ,
.
, (, , ) , ? , ,
. ,

 Ora le 10g  . 412

412

6.

. 6.10.

EmpSelfJoin

. , ,
. ,
,  Ora le, . . 6.11 EmpSelfJoin
, .
e EmpSelfJoin (employee), m  EmpSelfJoin (manager).
:
. SQL
EmpSelfJoin EmployeeID/BossID.
, .
1. , :

CLEAR SCREEN

 Ora le 10g  . 413

413

e.BossID = m.EmployeeID

EmployeeID

BossID

EmpSelfJoin e

EmployeeID

BossID

EmpSelfJoin m

. 6.11. ,

2. , . SQL*Plus . ,
, .

SELECT m.FirstName||' '||m.LastName AS Manager,


e.FirstName||' '||e.LastName AS Employee
FROM EmpSelfJoin e INNER JOIN EmpSelfJoin m
ON e.BossID = m.EmployeeID
ORDER BY m.LastName, e.LastName;
Ora le ;
,  ,
(. 6.12). ! , . 6.10,  21 .
21- ? , . 6.10,
, (John Warner) BossID, NULL,
 Readwood Realty,
. ,
(  ) ,
,   .
, , .
 NVL .

, .

CLEAR SCREEN, .
2.  SELECT
1.

 Ora le 10g  . 414

414

6.

. 6.12.

SELECT
NVL2(m.LastName, m.FirstName||' '||m.LastName,
'Board of Dire tors') AS Manager,
e.FirstName||' '||e.LastName AS Employee
FROM EmpSelfJoin e LEFT OUTER JOIN EmpSelfJoin m
ON e.BossID = m.EmployeeID
ORDER BY m.LastName, e.LastName;
3. EmpSelfJoin, . :

DROP TABLE EmpSelfJoin CASCADE CONSTRAINTS PURGE;


Ora le Table

dropped.

 . 6.13. /
. , Manager John Warner
Board of Dire tors ( ),
 NVL2,  , .. NULL ( ,
 BossID, John Warner ). NVL2  ,
.

 Ora le 10g  . 415

415

BossID NULL,
"Board of Directors"
. 6.13.  ,

SQL FULL OUTER JOIN, ,


, (  NULL ).
.

. , (non-equality join, nonequijoin)  , , . BETWEEN, <, <=, >, >=, <> =.


, , , .
, , , :

 Ora le 10g  . 416

416

6.

SELECT e.LastName e.HireDate


FROM Employees e JOIN CompanyHistory h
ON e.HireDate BETWEEN h.BeginDate AND h.EndDate;

, , , 
, , . 
, .  ,
.
Redwood Realty , (AskingPri e). , , . 
CASE  COUNT ,
. , . ,
. , ,  ,
/ / .
AskingPri e Listings / .  COUNT
. , ,
,  , . ,
, . 6.1. ,
, ,
.
Pri eCat, . 6.1. , ,
.

, .
1. 
.

Ch06Pri eRange.sql

 6

2. , SQL*Plus. CLEAR SCREEN <Enter>,   SQL*Plus.


, <path> 
( ).

 Ora le 10g  . 417

6.1.

417

CategoryID

LowLimit

HighLimit

1000
1010
1020

$0
$50,000
$100,000

$50,000
$100,000
$150,000

1030
1040
1050

$150,000
$200,000
$250,000

$200,000
$250,000
$300,000

1060
1070
1080

$300,000
$350,000
$400,000

$350,000
$400,000
$2,000,000

START <path>\Ch06Pri eRange


Ora le
table reated and populated.

Pri eCat. Pri eCat

3. , :

SELECT * FROM Pri eCat ORDER BY LowLimit;


4. .
SQL*Plus. , , SQL*Plus .

SELECT .LowLimit, .HighLimit, COUNT(AskingPri e) AS "Frequen y"


FROM Listings s JOIN Pri eCat
ON s.AskingPri e >= .LowLimit AND
s.AskingPri e < .HighLimit
GROUP BY .LowLimit, .HighLimit
ORDER BY .LowLimit;
Ora le (. 6.14).
 ON ( >= <).
5. ,
, . ,
NULL (
) . SELECT, JOIN RIGHT OUTER JOIN.
 SELECT.

SELECT .LowLimit, .HighLimit, COUNT(AskingPri e) AS "Frequen y"


FROM Listings s RIGHT OUTER JOIN Pri eCat
ON s.AskingPri e >= .LowLimit AND

 Ora le 10g  . 418

418

6.

6.2.

SQL

UNION

UNION ALL

, ,

,
,
() ,
, ,

INTERSECT
MINUS

s.AskingPri e < .HighLimit


GROUP BY .LowLimit, .HighLimit
ORDER BY .LowLimit;
 , , . 6.15. ,
, .
6. , Pri eCat . , :

DROP TABLE Pri eCat CASCADE CONSTRAINTS PURGE;


7. exit <Enter>, SQL*Plus.
 .

,
SELECT, . SQL
.  SELECT , ,
UNION, UNION ALL, MINUS INTERSECT. . 6.2.
SQL, , ( ompound query),
( omponent query). .

 Ora le 10g  . 419

419

"- "

. 6.14.

Pri eCat

.
.

SELECT .
ORDER BY,

.
(verti al join),  , .
 :

< omponent query>


{UNION | UNION ALL | INTERSECT | MINUS}
< omponent query>
, UNION,
UNION ALL, INTERSECT MINUS. ,
SELECT, ;
SELECTS  ..

 Ora le 10g  . 420

420

6.

. 6.15.

UNION UNION ALL

, 3 ,  . 4 , ,
, . , .  ( , SELECT,
) UNION. ,  ,
.

SELECT Table_Name AS "Obje t", 'Table' AS "Type"


FROM User_Tables
UNION
SELECT Sequen e_Name "Obje t", 'Sequen e' AS "Type"
FROM User_Sequen es
ORDER BY 2,1;
Table (   ).
Sequen e
 . Ora le

 Ora le 10g  . 421

421

, . ,
. , ,  .
, .
1. SQL*Plus, Ora le
.

CLEAR SCREEN,

2. Ch06CreateObje ts.sql ,
. <path>  (
) <Enter>.

START <path>\Ch06CreateObje ts
Ora le .
, UNION .
. , 
, .
UNION , , .
1. , SQL*Plus .

CLEAR SCREEN
SELECT Table_Name AS "Obje t", 'Table' AS "Type"
FROM User_Tables
WHERE Table_Name NOT LIKE 'BIN%'
UNION
SELECT Sequen e_Name "Obje t", 'Sequen e' AS "Type"
FROM User_Sequen es
UNION
SELECT Trigger_Name "Obje t", 'Trigger' AS "Type"
FROM User_Triggers
ORDER BY 2,1;
Ora le : , (. 6.15). , , . ,
WHERE  Ora le, BIN.

 Ora le 10g  . 422

422

6.

. 6.16.

UNION

. , , , , . 6.16, , 12,
.

2. , , Ora le 1:

DROP
DROP
DROP
DROP

SEQUENCE AgentID_seq;
SEQUENCE Customer_seq;
TRIGGER Agents_bi_trg;
TRIGGER Customers_bi_trg;

Ora le .
UNION ALL UNION, . UNION ALL
SELECT ( ).
MINUS

 MINUS , , , , .
, .
, Table1 A, C, D, F, G, H Z,

 Ora le 10g  . 423

423

Table2  A, B, C, H W. SQL
D, F, G Z  Table1, Table2.  (
Table1) A, C H  ,
.

SELECT *
FROM Table1
MINUS
SELECT *
FROM Table2;
MINUS Redwood Realty?
, ,  , , , ,
. ,
. ,  , 
 , . ,
, (
):

SELECT Address, City


FROM Customers
WHERE City = 'Ori k'
MINUS
SELECT Address, City
FROM Properties
WHERE City = 'Ori k';
, Ora le 53 .
, Customers
62 , , , 9 (62 
9 = 53). ,
, ,
.
. ,
: MINUS UNION ALL. ,
GoodTable TestTable  , ( GoodTable). ,
, . , .

(SELECT * FROM GoodTable MINUS SELECT * FROM TestTable)


UNION ALL
(SELECT * FROM TestTable MINUS SELECT * FROM GoodTable);
( , , UNION ALL.)

 Ora le 10g  . 424

424

6.

INTERSECT

INTERSECT , , .. , , . ,
Customers, , Redwood Realty, Properties,
. , , ( Customers) , ( Properties).
, ,
. INTERSECT. , ,  Customers,
Properties,  .. .
SQL*Plus, , .

SELECT Address, City


FROM Customers
WHERE City = 'Ori k'
INTERSECT
SELECT Address, City
FROM Properties
WHERE City = 'Ori k';

 , SQL (
). SELECT, SQL.  . ,
(Agents), Redwood Realty
(Tobias Carling). ,
: (1) ,
(HireDate) Agents; (2) , , HireDate
. DML2 -. ,
() , . , WHERE DELETE .
,
 .
: . SQL .

Data Manipulation Language  .

 Ora le 10g  . 425

425

SQL .
, , SQL , ( orrelated)
, .

,
, .
SQL, :
, SELECT;
FROM; WHERE HAVING. ,
, .

SELECT FirstName, LastName


FROM Agents
WHERE MONTHS_BETWEEN(SYSDATE,BirthDate) >
(SELECT AVG(MONTHS_BETWEEN(SYSDATE,BirthDate))
FROM Agents);
( SELECT
). ( ) .
. WHERE
, .
 , ,
.
, .
1. , SQL*Plus Ora le. ,
.
, SQL*Plus , ,
18   :

CLEAR SCREEN
SELECT Address, YearBuilt
FROM Properties
WHERE City = 'Ar ata' AND
YearBuilt > (SELECT AVG(YearBuilt)
FROM Properties
WHERE City = 'Ar ata')+18
ORDER BY Address;
2. , ,
, (Jessi a Taylor). ( UPPER , /
.)

 Ora le 10g  . 426

426

6.

,
18 ""
""

,
,

. 6.17.

WHERE

COLUMN FirstName FORMAT A10


COLUMN LastName FORMAT A10
COLUMN TITLE FORMAT A11
SELECT FirstName, LastName, Title
FROM Agents
WHERE UPPER(Title) =
(SELECT UPPER(Title)
FROM Agents
WHERE LOWER(FirstName) = 'jessi a'
AND LOWER(LastName) = 'taylor');
 . 6.17.

, . WHERE HAVING. , ( ) IN, ANY


ALL. , IN ,
. ANY

 Ora le 10g  . 427

427

; ALL . , ,
. , , >, >=, <, <=, = <>, ,
. , , , ,
-.

SELECT LastName
FROM Agents
WHERE Birthdate > (SELECT Birthdate FROM Agents
WHERE Gender = 'M');
ORA-01427: single-row subquery returns
more than one row ( ),
,  (>)
, .

IN

IN , , ( ). ,
NOT IN, , . , , , , ,
. (En ino).

SELECT * FROM Parts


WHERE PartNumber IN (SELECT PartNumber FROM Shipments
WHERE SupplierNumber IN
(SELECT SupplierNumber FROM Suppliers
WHERE UPPER(City) = 'ENCINO'));
SupplierNumber ( ), City En ino. SupplierNumber PartNumber Shipments. PartNumber Parts PartNumber Shipments.
Parts , PartNumber
, .
Readwood Realty. 
, ,
.
1. , ,
.
2. , SQL*Plus . SELECT , ,
.

 Ora le 10g  . 428

428

6.

CLEAR SCREEN
SELECT DISTINCT FirstName, LastName
FROM Agents
WHERE AgentID NOT IN (SELECT AgentID FROM CustAgentList);
 CustAgentList, , . AgentID Agents.
, .
3.  , (Ori k).
, , .

SELECT DISTINCT FirstName, LastName


FROM Agents
WHERE AgentID IN (SELECT AgentID FROM CustAgentList
WHERE CustomerID IN (SELECT CustomerID
FROM Customers
WHERE City = 'Ori k'));
4.  ,
 , .. ,
. (: ,
, .
 SQL*Plus .)

SELECT DISTINCT a.FirstName, a.LastName


FROM Agents a JOIN CustAgentList USING (AgentID)
WHERE .Conta tReason = 'Sell'
AND .CustomerID IN (SELECT CustomerID FROM CUSTOMERS
WHERE UPPER(City) = 'ORICK');
 . 6.18.
, ,
. , .

ANY ALL

ANY ALL . ANY ALL


(>, >=, <, <=, = <>).
, , ANY ALL . . 6.3
ANY ALL, .
ANY ALL, Redwood Realty AgentsHR.
 , , ,
, , , , Redwood

 Ora le 10g  . 429

. 6.18.

6.3.

=ANY
<ANY
>ANY
>ALL
<ALL

429

IN

ANY ALL

 , .
, IN
,
,
,
,

Realty. ,
,  ,
. ,  Ch06AgentsHR.sql.

.
Redwood Realty,
.

 Ora le 10g  . 430

430

6.

(
)

. 6.19.

AgentsHR

1. SQL*Plus ,
 Ch06AgentsHR.sql.

<path>

START <path>\Ch06AgentsHR
Ora le  .
2. ,
.

CLEAR SCREEN
DESCRIBE AgentsHR
SELECT * FROM AgentsHR;
, . 6.19.
.
ANY ALL
.
, , ,
10. , , ,

 Ora le 10g  . 431

431

. ,
ALL.
ANY ALL, .
1. , , SQL*Plus :

CLEAR SCREEN
COLUMN BaseSalary FORMAT $9,999
SELECT AgentID, BaseSalary, Team
FROM AgentsHR
WHERE BaseSalary >ALL (SELECT BaseSalary
FROM AgentsHR
WHERE Team = 10);
Ora le , , 5 32
.
2.  , ( ,
), 10, 10. SELECT, >ALL >ANY, WHERE AND Team <> 10
.  SELECT :

SELECT AgentID, BaseSalary, Team


FROM AgentsHR
WHERE BaseSalary >ANY (SELECT BaseSalary modified line
FROM AgentsHR
WHERE Team = 10)
AND Team <> 10; modified line
Ora le , , 10,
(. 6.20).
3. SQL*Plus .
10, ,
. Agents AgentsHR,  , . , SELECT, . ,
,
. ,
, .

 Ora le 10g  . 432

432

6.

. 6.20.

ALL ANY

. 6.21.

ALL

,
, .
1. ,  , SELECT:

 Ora le 10g  . 433

433

CLEAR SCREEN
COLUMN Agent FORMAT A22 HEADING 'Agent''s Name'
COLUMN BaseSalary FORMAT $9,999 HEADING 'Base|Salary'
COLUMN TeamMax FORMAT $9,999 HEADING 'Team 10|Max. Salary'
2. . , .
, SQL*Plus .

SELECT FirstName || ' ' || LastName AS Agent, BaseSalary,


(SELECT MAX(BaseSalary)
FROM AgentsHR
WHERE Team = 10) AS TeamMax, Team
FROM Agents INNER JOIN AgentsHR USING (AgentID)
WHERE BaseSalary >ALL (SELECT BaseSalary
FROM AgentsHR
WHERE Team = 10);
Ora le , . 1 ,
- , ,
. , . 6.21.

CLEAR COLUMNS,  .
4. exit <Enter>, Ora le SQL*Plus.
3.

5. , , .

, .
, Ora le
.
, , . , , ,
,
. , , ,  , ,
, .

SELECT *
FROM AgentsHr outer
WHERE outer.Va ation >
(SELECT AVG(Va ation)
FROM AgentsHR inner
WHERE outer.Team = inner.team)
ORDER BY Team;

 Ora le 10g  . 434

434

6.

 . Ora le
(SELECT *...).
(outer.Team inner.Team). Ora le ,
. Ora le .  ,
, , , . , ,
 , .
1 000 ,
1 000 . , , 1 000 , 
1 000 1 000 = 1 000 000 !
, . , .


:  , , ?
,
. ,
.
. SQL*Plus , . , ,
Listings Properties, AskingPri e City . ,
.
, : (Loleta), (Ori k)
(Trinidad). , -,
194 .
, .
1. , SQL*Plus
.   .

CLEAR SCREEN
COLUMN Address FORMAT A30
COLUMN City FORMAT A16
COLUMN AskingPri e FORMAT $9,999,999

 Ora le 10g  . 435

.

6.22.

435

2. SQL*Plus,
,
:

SELECT PropertyID, p1.Address, p1.City, l1.AskingPri e


FROM Properties p1 INNER JOIN Listings l1 USING(PropertyID)
WHERE l1.AskingPri e >
(SELECT AVG(AskingPri e)
FROM Properties p2 INNER JOIN Listings l2
USING(PropertyID)
WHERE p1.City = p2.City)
AND p1.City IN ('Loleta', 'Ori k', 'Trinidad')
ORDER BY 3 ASC,4 DESC;
. 6.22 ,
.
3.  ,

CLEAR COLUMNS

LECT:
WHERE p1.City = p2.City

WHERE SE-

 , . , AskingPri e .
Proper-

 Ora le 10g  . 436

436

6.

ties Listings, . .
, 
, .
,
, .
,
, 29 .
, , .
1. , SQL*Plus :

CLEAR SCREEN
SET PAGESIZE 30
COLUMN BaseSalary FORMAT $9,999 HEADING 'Base|Salary'
BREAK ON Team SKIP 1
2. :

SELECT Poss.Team, Poss.AgentID, Poss.BaseSalary


FROM AgentsHR Poss
WHERE BaseSalary >
(SELECT AVG(BaseSalary)
FROM AgentsHR Aver
WHERE Poss.Team = Aver.Team)
ORDER BY 1,2;
Ora le , ,
.
. 6.23.
3. , , 
:

CLEAR BREAKS
CLEAR COLUMNS
SET PAGESIZE 14
4. , . , SQL*Plus.

EXISTS

EXISTS ,
. , EXISTS true,
 false. EXISTS , ,

 Ora le 10g  . 437

437

. 6.23. , ,

.
, EXISTS , (.. ).
NOT EXISTS, ,
, . EXISTS  ,
. '1' 'X'
NULL. EXISTS
.

SELECT <outer sele t list>


FROM <outer table>
WHERE EXISTS
(SELECT 'X'
FROM <inner table> inner
WHERE <outer table. olumnname> = <inner table. olumn name>);
EXISTS
, - , , .
EXISTS, , -

 Ora le 10g  . 438

438

6.

 ,
- ,
Listings.
, Redwood Realty  Agents Listings ( , ,
).

SELECT LastName, FirstName, 'No Listings' AS "Situation"


FROM Agents
WHERE NOT EXISTS (SELECT 'X'
FROM Listings
WHERE AgentID = ListingAgentID);
, 'X',
EXISTS ( NOT
EXISTS), . , ,  ,
, . NOT
EXISTS :  , 
AgentID Listings? .
( ).
 ,
EXISTS, .
1. , SQL*Plus Ora le.
CLEAR SCREEN, SQL*Plus.
2. SELECT .
,
.

SELECT COUNT(*) AS "Customers w/o agents"


FROM Customers outer
WHERE NOT EXISTS
(SELECT 'X'
FROM CustAgentList inner
WHERE outer.CustomerID = inner.CustomerID);
1745, , 1 745 , Customers,  ,
Agents.
3. , , ( Properties). . ( ,
.)

SELECT COUNT(LastName) AS "Customers w/o properties"


FROM Customers
WHERE NOT EXISTS
(SELECT 123

 Ora le 10g  . 439

439

FROM Properties
WHERE CustomerID = OwnerID);
123, .
, ,
EXISTS .
4. , Li ensed. 
EXISTS. :

SELECT FirstName, LastName, AgentID


FROM Agents a
WHERE EXISTS (SELECT 'X'
FROM Li enseStatus b
WHERE a.Li enseStatusID = b.Li enseStatusID
AND UPPER(StatusText) <> 'LICENSED')
ORDER BY LastName, FirstName;
, . 6.24.
5. , .
6. exit <Enter>, Ora le
SQL*Plus.

DML-

SELECT, DML3 - ( INSERT, DELETE UPDATE). , DELETE Agents,


Listings. ( ,  ,
!)

DELETE
FROM Agents
WHERE NOT EXISTS
(SELECT 'X' FROM Listings
WHERE AgentID = ListingAgentID);
,  AgentID, ,
ListingAgentID Listings. , . ,
(Susan Swarthmore, George Nagy Robert Sellsmore)
3

Data Manipulation Language  .

 Ora le 10g  . 440

440

6.

,

"Licensed"

. 6.24.

EXISTS

Agents. , - , ,
, .
. , ALTER TABLE SELECT
AgentsHR, , BirthDate Agents.
, ,
 .

REM AgentsHR ,
REM BirthDate:
ALTER TABLE AgentsHR
ADD (BirthDate DATE);
REM
UPDATE AgentsHR hr
SET BirthDate = (SELECT BirthDate
FROM Agents ag
WHERE hr.AgentID = ag.AgentID);

 Ora le 10g  . 441

441


 ,
. ,
, .  . Ora le ,  .
Ora le SELECT
DML-, . , 
, : FROM
SELECT , .
, .  ,
DML-, INSERT, UPDATE DELETE. ( .)
 , . ,  user_tables.
, user_sequen es 
,
user_triggers  .
, . , DML ,
 . , ,
.

SELECT, ,
, .
,
. , .


, , .

 Ora le 10g  . 442

442

6.

. .
 SELECT,  , , 
.

.

, , .

,
, .
, , (dis retionary a ess ontrol). , .
, ,   , ,
,  ..  , 
,  .
, , ,
,  .
, 
 . ( olumn subset view), , , .
. (row subset view)
, . SELECT
WHERE,  ,
.
 : .
,
, GROUP BY DISTINCT
. .
.

 Ora le 10g  . 443

443

() ,
CREATE VIEW. :

CREATE [OR REPLACE [FORCE | NOFORCE VIEW <view-name>


[(< olumn-name>,...,< olumn-name>)
AS <subquery>
[WITH CHECK OPTION [CONSTRAINT < onstraint-name>
[WITH READ ONLY;
 OR REPLACE ,
, ,
Ora le . 
, ,
. FORCE
. NOFORCE ( ) ,
,  . <view-name>
, < olumn-name>  ,
. <subquery>  , . WITH CHECK OPTION , , , , WHERE .
WITH CHECK OPTION, Ora le , ,
. READ ONLY ,
(, ).
< onstraint-name> WITH CHECK OPTION.
.
Agents , , -.
, WITH READ ONLY ( )
Agents.

CREATE OR REPLACE VIEW MaleEmployees


AS SELECT *
FROM Agents
WHERE Gender = 'M'
WITH READ ONLY;
MaleEmployees. , Agents.
SQL*Plus DESCRIBE, .

 Ora le 10g  . 444

444

6.

, , . , CREATE
VIEW Agents.

CREATE OR REPLACE VIEW Employees


AS SELECT AgentID, FirstName, LastName, Title
FROM Agents;

FirstName, LastName Title.

AgentID,

,
. ,
, . SELECT,
, ,
, SELECT,
WHERE, GROUP BY, HAVING ORDER BY. SELECT, Employees,
.

SELECT LastName || ', ' || FirstName, Title


FROM Employees
WHERE Title = 'Broker'
ORDER BY 1;
Employees
 ( ), Title
( Broker). Ora le
.
! , ,
, . SQL*Plus
Ora le. , ,
SQL.
,
.
1.

CLEAR SCREEN <Enter>, .

2. , SQL*Plus ,
Brokers  ,
, (Title) Broker.

CREATE OR REPLACE VIEW Brokers


AS SELECT *
FROM Agents
WHERE UPPER(Title) = 'BROKER'
WITH CHECK OPTION;
Ora le , .

 Ora le 10g  . 445

445

. 6.25.

3. , ,
Broker. ,
NonBrokers:

CREATE OR REPLACE VIEW NonBrokers


AS SELECT AgentID, LastName, FirstName, BirthDate
Gender, HireDate, Title, TaxID
FROM Agents
WHERE UPPER(Title) <> 'BROKER'
WITH CHECK OPTION;
Ora le , .
4. ,  NonBrokers:

DESCRIBE NonBrokers
Ora le NonBrokers. , . 6.25.
5. , , 
:

CLEAR SCREEN
COLUMN FirstName FORMAT A15
COLUMN LastName FORMAT A15
COLUMN Title FORMAT A12
SELECT FirstName, LastName, Title
FROM NonBrokers
ORDER BY LastName, FirstName;
. 6.26.

 Ora le 10g  . 446

446

6.

. 6.26.

NonBrokers

Ora le 23 NonBrokers, Agents.


Title Broker,  . PAGESIZE , 14,
.
 , , VIEW . , .
,
, .
1. SQL*Plus ( ,
SQL*Plus):

CLEAR SCREEN
CREATE OR REPLACE VIEW
Cust (CustNo, LName, FName, Addr, City, St, Zip)
AS SELECT CustomerID, LastName, FirstName,
Address, City, State, Zip ode
FROM Customers;

 Ora le 10g  . 447

447

. 6.27.

2. <Enter>,
 :

DESCRIBE Cust
 . 6.27.
.
3.

exit <Enter>, SQL*Plus Ora le.

DML-, , , . ,
, . WITH READ ONLY, Ora le DML
. DML-,
 WITH READ OPTION. , , , WITH CHECK OPTION, , 
,  . , ,
Brokers, Title Salesperson, Ora le ORA-01402: view
WITH CHECK OPTION where- lause violation (ORA-01402: where WITH CHECK OPTION ).
, , , .

 Ora le 10g  . 448

448

6.

DML-
. WITH READ ONLY, DML : 
. ,  , (1) PRIMARY KEY; (2) NOT NULL;
(3) UNIQUE; (4) FOREIGN KEY (5) WITH CHECK OPTION.
DML- , .
, ,
.
1. SQL*Plus, Ora le , . CLEAR SCREEN,
SQL*Plus.
2. ,
Brokers:

INSERT INTO Brokers (AgentID, LastName, FirstName, Title)


VALUES (87654, 'Foxworthy', 'Robert', 'Salesperson');
Ora le INSERT,
Brokers , Title
Broker.
3. , , Cust,
:

INSERT INTO Cust (CustNo, FName, LName, Addr, City)


VALUES (76543,'Allison', 'Honey utt', '123 Main','Loleta');
4. INSERT (
ZIP). , UPDATE. ( WHERE ,
,
.)

UPDATE Cust
SET St = 'CA', Zip = '95551'
WHERE CustNo = 76543;
5.  , :

COMMIT;
6. , :

SELECT CustNo, FName, LName, City, St, Zip


FROM Cust
WHERE CustNo = 76543;

 Ora le 10g  . 449

449

. 6.28. DML-

7.

DELETE FROM Cust


WHERE CustNo = 76543;
 . 6.28.

CREATE VIEW,
. , , ,
GROUP BY DISTINCT
 .  . ,
,
DML-.

 Ora le 10g  . 450

450

6.

, , ,
DML-.
, Agents, DML. , SQL*Plus.

DML-, .
1. SQL*Plus , ,
Agents, :

CREATE OR REPLACE VIEW AgentView AS


SELECT AgentID, FirstName, LastName,
BirthDate, (SYSDATE-BirthDate)/365.25 AS Age
FROM Agents;
2.  ,
:

UPDATE AgentView
SET BirthDate = TO_DATE('10/17/1962', 'MM/DD/YYYY')
WHERE Agentid = 10041;
Ora le , .
3.  ,
:

UPDATE AgentView
SET AGE = 50 WHERE Agentid = 10041;
Ora le

ORA-01733: virtual olumn not allowed here


4.  , :

ROLLBACK;
. 6.29.
ROLLBACK, , 2.
, CREATE VIEW DDL-. , Ora le
() . , , , .
, , ,
100% . ,
, .

 Ora le 10g  . 451

451

. 6.29.

,
.
, , . .
 , . , ,
. ,  .
, , ( ), . , Ora le  (, , ..) (user_tables,
user_sequen es, user_views ..). ,
MyOwnObje ts, .
,
.
1. SQL*Plus,

CLEAR SCREEN <Enter>.

2. ,
MyOwnObje ts:

CREATE OR REPLACE VIEW MyOwnObje ts (ObjType, ObjName) AS


SELECT 'Table', Table_Name FROM User_Tables

 Ora le 10g  . 452

452

6.

,
,

. 6.30. , ,

WHERE Table_Name NOT LIKE 'BIN%'


UNION
SELECT 'View', View_Name FROM User_Views
UNION
SELECT 'Sequen e', Sequen e_Name FROM User_Sequen es
UNION
SELECT 'Trigger', Trigger_Name FROM User_Triggers;
Ora le ,
.
3. , :

SELECT ObjType, ObjName


FROM MyOwnObje ts
ORDER BY 1,2;
Ora le , ,
, ,  MyOwnView (. 6.30).
, , ,  , -
. , -

 Ora le 10g  . 453

453

, .
, SELECT. ForSale,  , , ,
.  Agents, CustAgentList, Listings,
Properties Customers. CREATE VIEW 
, . , , ,
. , , .
, . ,
SQL*Plus Ora le.
,
, .
1. CLEAR
SQL*Plus.

SCREEN <Enter>,

2. , , :

CREATE OR REPLACE VIEW


ForSale(AgID, AgentFirst, AgentLast, Bid, Ask,
Addr, City, Br, Ba, Ft,
SellerFirst, SellerLast) AS
SELECT
a.AgentID, a.FirstName, a.LastName, b.BidPri e, .AskingPri e,
d.Address, d.City, d.Bedrooms, d.Bathrooms, d.SqFt,
e.FirstName, e.LastName
FROM Agents a
INNER JOIN CustAgentList b ON a.AgentID = b.AgentID
INNER JOIN Listings ON b.ListingID = .ListingID
INNER JOIN Properties d ON .PropertyID = d.PropertyID
INNER JOIN Customers e ON d.OwnerID = e.CustomerID
WHERE b.Conta tReason = 'Sell';
Ora le , .
, , .
3. ForSale, , , (AgID, AgentFirst, Br ..).
SQL*Plus,  :

COLUMN
COLUMN
COLUMN
COLUMN

Asking FORMAT $9,999,999


Addr FORMAT A25
City FORMAT A10
SellerLast FORMAT A14

 Ora le 10g  . 454

454

6.

,

ForSale

. 6.31. ,

COLUMN Br FORMAT 99
COLUMN Ba FORMAT 99
COLUMN Ft FORMAT 9,999
4.
SQL*Plus :

ForSale,

SELECT SellerLast, Ask AS "Asking", Addr, City, Br, Ba, Ft


FROM ForSale
WHERE Ask > 400000
ORDER BY Ask DESC;
Ora le , .  . 6.31.
5. SQL*Plus exit <Enter>, Ora le
SQL*Plus. 
.

 Ora le 10g  . 455

455

, , ,
, . , CoffeeMer hant SELECT Mer handise,
,  CoffeeMer hant. , , (, ).

SELECT *
FROM CoffeeMer hant.Mer handise;
, (
).
, :
, .
.
, CREATE SYNONYM (,
CREATE SYNONYM).
:

CREATE SYNONYM <synonym-name> FOR <qualified-name>;


, , CREATE SYNONYM SELECT
. MyCoffee, :

CREATE SYNONYM MyCoffee FOR CoffeeMer hant.Mer handise;


, SELECT, ,
. , Mer handise, CoffeeMer hant.
12.

SELECT * FROM MyCoffee;


 Ora le . , 
( ) user_views.
all_views 
, , , -

 Ora le 10g  . 456

456

6.

6.4.


view_name
text_length
text

user_views


VARCHAR2(30)
NUMBER

LONG
,

(), . ,
DESCRIBE, , .
user_views . . 6.4 .
.

, .
1. SQL*Plus, Ora le .
2.  ,
 :

SET PAGESIZE 50
COLUMN view_name FORMAT A15
COLUMN text_length FORMAT 99,999
COLUMN text FORMAT A40 WRAPPED
3. ,  :

SELECT view_name, text_length, text


FROM user_views
ORDER BY view_name;
Ora le  (. 6.32).
4. , 
:

CLEAR COLUMNS
SET PAGESIZE 14
, ,
, . 3, . : , , Ora le
, , .

SELECT view_name, text_length, text


FROM all_views
ORDER BY view_name;

 Ora le 10g  . 457

457

. 6.32. 

1 500 .
all_views, WHERE.

DROP VIEW <view-name>



, , .
 , . , MyOwnObje ts,
, . , :

SELECT *
FROM MyOwnObje ts
WHERE LOWER(ObjType) = 'view';

 Ora le 10g  . 458

458

6.

. 6.33.

, ,
, .
1. SQL*Plus :

CLEAR SCREEN
SELECT *
FROM MyOwnObje ts
WHERE LOWER(ObjType) = 'view';
Ora le , .
2. , :

DROP
DROP
DROP
DROP
DROP
DROP

VIEW
VIEW
VIEW
VIEW
VIEW
VIEW

AgentView;
Brokers;
Cust;
ForSale;
MyOwnObje ts;
NonBrokers;

Ora le (. 6.33).
3. exit <Enter>, Ora le
SQL*Plus.

 Ora le 10g  . 459

459

SELECT . FROM ON, ,


. , ( =).
, ,
. ,
FROM.
, .
, .
, ,
NULL. -
, (
).
UNION, UNION ALL, MINUS INTERSECT
SELECT. UNION
UNION ALL , . UNION ALL
, UNION  . MINUS
, SELECT,
. INTERSECT SELECT
, .
,
, ,
. ; . ,
,  .
, FROM ( ), WHERE SELECT (
) DML-, CREATE TABLE.
ANY ALL WHERE,
, - , .
SELECT,
. , .
, .

 Ora le 10g  . 460

460

6.

( ), 
.










EXISTS

 Ora le 10g  . 461

461

1.
.
2.
.
3.
DDL-.

FROM SELECT -

4.  EXISTS TRUE
FALSE.
5.  , ,
, /
.

1. ______ , .
2.
, , ______ .
3. ______ .
4. _____ , , .
5. , ______.

1. FROM
People Sales PeopleID, ?

 Ora le 10g  . 462

462

6.

FROM
) FROM
) FROM
) FROM

People JOIN Sales BY People. PeopleID IN Sales.PeopleID


Sales JOIN People USING PeopleID = PeopleID
People p JOIN Sales s ON PeopleID = PeopleID
Sales JOIN People USING (PeopleID)
2. WHERE Employee Sales,
(EmpID Employee EmployeeID Sales)?
) FROM Employee RIGHT OUTER JOIN Sales ON EmpID = EmployeeID
) FROM Employee LEFT OUTER JOIN Sales ON EmpID = EmployeeID
) FROM Employee FULL OUTER JOIN Sales ON EmpID = EmployeeID
) FROM Employee INNER JOIN Sales ON EmpID = EmployeeID
3. WHERE ,
BaseSalary BaseSalary
)

?
) WHERE

BaseSalary
) WHERE BaseSalary
) WHERE BaseSalary
) WHERE BaseSalary

>= ALL (SELECT BaseSalary FROM AgentsHR).


>=ANY (SELECT BaseSalary FROM AgentsHR).
<=ALL (SELECT BaseSalary FROM AgentsHR).
<=ANY (SELECT BaseSalary FROM AgentsHR).

) .
4. ?
) CREATE OR REPLACE VIEW Someone AS SELECT *

FROM Agents;.
) CREATE VIEW Someone AS SELECT * FROM Agents WHERE AgentID > 13000;.
) CREATE VIEW Someone (F1,F2) AS SELECT HireDate FROM Agents;.
) CREATE VIEW Someone (AgID) AS (SELECT AgentID FROM Agents);.
) .

5. ( ) ?
) SELECT view_name FROM user_views;.

SELECT view_name FROM all_views;.


) SELECT view_name FROM views;.
) SELECT viewname FROM userviews;.

) .

 Ora le 10g  . 463

463

1. Readwood Realty

.
,
Listings. , 1620 , 300 ; 2130  600 ; , 30 ,
1 000 . , .
INNER JOIN, (> < =).
, , ,
SQL*Plus. , .
.  <>Ch06Prob1Part1.sql.
, SQL*Plus . -
 Ch06BuildBonus.sql ( Chapter06) .
.
1. ,
,  <path>
<>.

REM <>
CLEAR SCREEN
START <path>\Ch06BuildBonus.sql
2. ,
BonusS hedule:

SELECT * FROM BonusS hedule;


3. ,  :

SET PAGESIZE 35
COLUMN Bonus FORMAT $9,999
SELECT a.LastName, a.ListCount, b.Bonus
FROM (SELECT AgentID, LastName, COUNT(*) as
ListCount
FROM Agents INNER JOIN Listings
ON AgentID = ListingAgentID
GROUP BY AgentID, LastName) a
INNER JOIN BonusS hedule b
ON a.ListCount BETWEEN b.LowLimit
AND b.HighLimit
ORDER BY b.Bonus DESC, a.LastName ASC;

 Ora le 10g  . 464

464

6.

4. ,
.
<> <path>.

SPOOL <path>\<>Ch06Prob1P1-spool.txt
SHOW USER
/
SPOOL OFF
5.
, :

CLEAR COLUMNS
SET PAGESIZE 14
DROP TABLE BonusS hedule CASCADE CONSTRAINTS
PURGE;
6. , . 
  , 4.
, Redwood Realty,
(SaleStatus)  Sold.  ,
<>Ch06Prob1Part2.sql SQL*Plus, . .
7. ,

<>:

REM <>
CLEAR SCREEN
CLEAR COLUMNS
COLUMN LastName FORMAT A15
COLUMN City FORMAT A15
COLUMN Address FORMAT A20
COLUMN Owner FORMAT A15
8. ,
:

SELECT p.City, p.Address, p.PropertyID,


.LastName AS "Owner"
FROM Properties p INNER JOIN Customers
ON p.OwnerID = .CustomerID
WHERE PropertyID IN
(SELECT PropertyID
FROM Listings
WHERE SaleStatusID = (SELECT
SaleStatusID
FROM SaleStatus
WHERE SaleStatus
= 'Sold')
)
ORDER BY 1,2;
9.  . .

 Ora le 10g  . 465

465

, MyListings, .  ,
<>Ch06Prob1Part3.sql SQL*Plus ( ). .
. <> .

SPOOL <path><>Ch06Prob1Part3-spoolfile.txt
REM <>
CLEAR SCREEN
CREATE OR REPLACE VIEW MyListings (Agent,
Addr, City, Pri e) AS
SELECT LastName, Address, City, AskingPri e
FROM Agents INNER JOIN Listings ON
AgentID = ListingAgentID
INNER JOIN Properties USING (PropertyID)
ORDER BY LastName, City;
REM
DESCRIBE MyListings
REM 
REM
SELECT Agent, City, Pri e
FROM MyListings
WHERE UPPER(Agent) = 'CARLING'
ORDER BY City, Pri e DESC;
1.  , ,  :

DROP VIEW MyListings;


SPOOL OFF
2. , ,   . .
3. SQL*Plus
SQL*Plus.

exit <Enter>, Ora le

2. Coffee Mer hant

Coffee Mer hant .


1. SQL*Plus, Ora le .  <>Ch06Problem2Part1.sql, <> .
,
SQL*Plus . ,
 .
2. Coffee Mer hant,
.  Coffee Mer-

 Ora le 10g  . 466

466

6.

hant , , . ,  BuildCoffee.sql.
( CoffeeMer hant -, .) START, <path>
 BuildCoffee.sql.

START <path>\CoffeeMer hant\BuildCoffee


Ora le Coffee Mer hants .
3.  , ( <> ).

SPOOL C:\<>Ch06Prob2Part1.txt
4. , ,  .
, , ,
. <> .

CLEAR SCREEN
REM <>
COLUMN CNAME FORMAT A20 HEADING 'NAME'
COLUMN Street FORMAT A30
COLUMN City FORMAT A15
COLUMN State FORMAT A5
COLUMN Zip ode FORMAT A5 HEADING 'ZIP'
SET PAGESIZE 40
SELECT a.FirstName || ' ' || a.LastName AS
CNAME, a.Street,
a.City, a.State, a.Zip ode
FROM Consumers a
JOIN Orders b USING (ConsumerID)
JOIN OrderLines USING (OrderID)
JOIN Inventory d USING
(InventoryID)
JOIN Countries e USING (CountryID)
WHERE UPPER(e.CountryName) LIKE 'YEM%'
AND UPPER(d.ItemType) = 'C'
ORDER BY a.Zip ode;
5. SQL*Plus , 
 :

SPOOL OFF
CLEAR COLUMNS
SET PAGESIZE 14
6.
<Ctrl+N>, , 2.

, 2005 .

 Ora le 10g  . 467

467

 .
,  .
1.  , ( <>).

SPOOL C:\<>Ch06Prob2Part2.txt
2. ,  , <> . 
<>Ch06Problem2Part2.sql SQL*Plus .

REM <>
REM (Customers) 2005
CLEAR SCREEN
COLUMN OrderID FORMAT 999999
COLUMN CNAME FORMAT A30
COLUMN Total FORMAT $99,999.99
SET PAGESIZE 80
3. SQL*Plus,
SQL*Plus :

SELECT OrderID, a.FirstName || ' ' ||


a.LastName AS "CNAME",
SUM( .Quantity* .Pri e*(1 .Dis ount)) AS "Total"
FROM Consumers a JOIN Orders b USING
(ConsumerID)
JOIN OrderLines USING
(OrderID)
WHERE b.OrderDate BETWEEN '01-OCT-05' AND
'31-OCT-05'
GROUP BY OrderID, a.FirstName, a.LastName
ORDER BY "Total" DESC;
SPOOL OFF
CLEAR COLUMNS
4. , , .
<Ctrl+N>, .
5. ,
(Hillary Flintsteel). ,
 , <> .
 <>Ch06Problem2Part3.sql
SQL*Plus .

SPOOL C:\<>Ch06Prob2Part3.txt
REM <>
REM ?
CLEAR SCREEN
COLUMN FirstName FORMAT A12
COLUMN LastName FORMAT A12

 Ora le 10g  . 468

468

6.

COLUMN Street FORMAT A27 WORD_WRAP


COLUMN City FORMAT A15 WORD_WRAP
COLUMN State FORMAT A02
COLUMN Zip ode FORMAT A05 HEADING 'ZIP'
SET PAGESIZE 55
6. SQL SQL*Plus
.  SQL*Plus .

SELECT DISTINCT .FirstName, .LastName, .Street,


.City, .State, .Zip ode
FROM Consumers JOIN Orders USING (ConsumerID)
JOIN Employees USING
(EmployeeID)
WHERE EmployeeID = (SELECT EmployeeID
FROM Employees
WHERE LOWER(LastName)
LIKE 'flint%')
ORDER BY .Zip ode;
SPOOL OFF
CLEAR COLUMNS
SET PAGESIZE 14
7. SQL*Plus .
8.   .
3. Rowing Ventures

 SQL .
  ,
.
SQL*Plus, Ora le  BuildRowing.sql,
RowingVentures -. 
-, .
1. , Rowing Ventures Boat,
BoatCrew Person. : BoatCategory, BowNumber, Position, LastName Firstname (
).
BoatCategory, BowNumber Position.
 COLUMN,
 30 , ( )  ,   . Position Cox ()
0.  18. : BoatCategory (SKIP PAGE), BowNumber (SKIP 1
DUP).   .

 Ora le 10g  . 469

469

2. , ,
, (Ted Simpson). (.
BoatCrew Person, ,
, , BoatID.)
, .
  . ,
.
3.  , ( ) RowerOrganizations. , (OrganizationName). : Person, BoatCrew, Boat Organization.
OrganizationName, FirstName LastName ( ). , , ,
. SQL*Plus 
. PAGESIZE 60 (), OrganizationName  40 ,
15 . OrganizationName, LastName FirstName.
 , (14 ) .

4. Broad loth Clothing

SQL*Plus, Ora le  BuildClothing.sql,


Chapter06\Broad loth -.
Conta tInfo   Conta t. ( )
FirstName, LastName, Address, City, State, Nation PrimaryLanguage , Nation China, India, Malaysia Thailand.
,   SQL*Plus, Nation, City LastName 25 .
, , LastName, City
Nation ( ), Nation, City, LastName (
).   .
, Fa toryID, City,
Nation MaxWorkers , ,
MaxWorkers.  . . Fa tory. COLUMN 
City Nation 15 ; 
MaxWorkers  ; . -

 Ora le 10g  . 470

470

6.

Nation, City (  ).  
 . 
.

 Ora le 10g  . 471

PL/SQL

. . .

PL/SQL
PL/SQL
PL/SQL
, ,


 PL/SQL
IF

PL/SQL

PL/SQL  Programming Language/Stru tured Query Language


( / ).
SQL Ora le.
PL/SQL , .
, , , SQL PL/SQL. PL/SQL, Ora le
. PL/SQL PL/SQL ,  . PL/SQL

 Ora le 10g  . 472

472

7. PL/SQL

SQL*Plus

SQL


SQL

SQL


SQL

SQL
PL/SQL
( SQL)

PL/SQL


PL/SQL

PL/SQL
PL/SQL

. 7.1. SQL PL/SQL

 .
PL/SQL ,
, : , ,
. , PL/SQL
- . . 7.1
SQL PL/SQL Ora le.

PL/SQL
SQL ,
 . , ,
SQL ,
.
, .
.


.
.
, .

 Ora le 10g  . 473

PL/SQL

473

SQL  . PL/SQL ,
SQL. Ora le, PL/SQL
.
, , ,  PL/SQL. , PL/SQL (, Ora le).
. SELECT, PL/SQL
, INSERT, UPDATE DELETE. PL/SQL
, SQL (DDL),
CREATE.
PL/SQL .
PL/SQL Ora le SQL. SQL- . PL/SQL, , SQL, , . PL/SQL . PL/SQL,
SELECT BEGIN END.
PL/SQL Ora le.
PL/SQL Ora le. PL/SQL PL/SQL ,
.

PL/SQL
PL/SQL : , . , ,
PL/SQL, . , , , .
 ,
.
. PL/SQL : .
.  PL/SQL,
Ora le .
 PL/SQL, Ora le
. 
.

 Ora le 10g  . 474

474

7. PL/SQL

PL/SQL Ora le,


 SQL;  .sql. .
SQL*Plus  :

START <____PL/SQL>
, , , ,
.
, .
: ,
.
PL/SQL; .
.

[DECLARE}
BEGIN
[EXCEPTION
END;
DECLARE.
, . , . ,
. , PL/SQL ,
PL/SQL. , .
, , PL/SQL ,
. .  BEGIN END.
PL/SQL SQL. SQL
, PL/SQL
, .
EXCEPTION
, . .
, PL/SQL, . , ,
END, .
PL/SQL,

 Ora le 10g  . 475

475

. 7.2. PL/SQL

, .
PL/SQL . 7.2.


 , . 7.2.  . , .
/* */ .
. .
. _,
 v_. , . PL/SQL ,
, :

<_>.<_>%TYPE
. 7.2 , v_first , LastName Agents ( Agents.LastName).
.

<_> [CONSTANT <_> [NOT NULL


[:= | DEFAULT <_>;

 Ora le 10g  . 476

476

7. PL/SQL

, v_taxrate  18.75%.
.

v_taxrate NUMBER (4,4) := .1875;


30 (, 
) . ,
, PL/SQL. , Ora le , ,
. ,
, _, v_ g_. ,
- PL/SQL.
,
. (, 0,1875) ,
,  . ,
. .
PL/SQL: , , (large obje t  LOB).
, 6.
.  ,
. PL/SQL
Boolean. Boolean
TRUE, FALSE NULL.
NOT NULL, ,
, NULL. ,
;
Ora le .
:= ,
.
NULL.


BEGIN .
, ,
PL/SQL. SELECT,  . PL/SQL

 Ora le 10g  . 477

477

 SELECT, INTO, ,
. .
, SELECT .

SELECT
INTO
FROM
WHERE

<__1> [,<__2>, ...


<__1> [, <__2>,...
<_> | <_>
<>;

PL/SQL SELECT, Ora le


, . . . SELECT DML,
, Ora le . . SELECT
, . .
SELECT, , .
.
PL/SQL (. . 7.2) . &AgentLastName :

Enter value for agentlastname:


, (, Carling) <Enter>. WHERE
SELECT. SELECT
. , INTO. ,
Redwood Realty.

(C_date-HireDate)/365.25
Ora le
,  PUT_LINE
DBMS_OUTPUT. PL/SQL.
SQL*Plus, SELECT, PL/SQL, . DBMS_OUTPUT,  .

 Ora le 10g  . 478

478

7. PL/SQL

. PL/SQL, . 7.2, , SQL*Plus SERVEROUTPUT, ON.


SQL SET SERVEROUTPUT ON <Enter>.

 PUT_LINE (. . 7.2) ,
. 
.


PL/SQL
, , . .
. , :

SELECT 12/0
INTO v_numbervar
FROM DUAL;
. PL/SQL EXCEPTION, ,
Ora le PL/SQL . SELECT ,
.
: .
, ,  , . Ora le. PL/SQL. , , . :

<_> EXCEPTION;
,
. RAISE, :

RAISE <_>;
, , ,
.
.
,
Ora le.  ,
NULL.

 Ora le 10g  . 479

479

7.1. Ora le

Ora le

ORA-0001
ORA-1001
ORA-1403

ORA-1422
ORA-1476
ORA-1722

SELECT INTO

(,
, )
, 

ORA-6502
ORA-6511

. 7.1.
OTHERS, . 7.2 , , WHEN
EXCEPTION. , . ,
, PL/SQL.

SQL SQL*Plus. , PL/SQL


, ,
.
, Redwood Realty .

Redwood Realty
PL/SQL, . ,
, . ,
, SQL*Plus. , SQL*Plus
Ora le. . ,
SQL*Plus Ora le.
, .

 Ora le 10g  . 480

480

7. PL/SQL

. 7.3. Redwood Realty

1.  BuildRedwood.sql Redwood Realty.


 . , <_1> .

START <_1>\RedwoodRealty\BuildRedwood
2. .  Ch07AgentsHR 7.
, <_2>
.

START <_2>\Ch07AgentsHR
3. , . 7.3.
4. SQL*Plus.


PL/SQL,
( Windows , , ) .
SQL*Plus .
(  .sql)
START. .

START C:\AnonymousBlo kExample.sql

C:\AnonymousBlo kExample.sql

 Ora le 10g  . 481

481

. ,
. , , ,
-, , , -,
 , -,  PL/SQL.
PL/SQL, ,
Agents . AgentsHR
. , ,
,   Ora le,
, .
, ,
. ( ) SQL*Plus.
PL/SQL, .
1. . ,
( DECLARE)
.

DECLARE
v_AvgSal AgentsHR.BaseSalary%TYPE;
v_AvgVa AgentsHR.Va ation%TYPE;
v_Count INTEGER NOT NULL := 0;
v_AvgSalChar VARCHAR2(15);
2. .
,
. ( ,
BEGIN, .
.)

BEGIN
/* , */
SELECT COUNT(AgentID) INTO v_Count FROM Agents;
/* */
SELECT AVG(BaseSalary), AVG(Va ation)
INTO v_AvgSal, v_AvgVa
FROM AgentsHR;
/*  */
v_AvgSalChar := TO_CHAR(v_AvgSal, '$99,999.99');
/* */
DBMS_OUTPUT.PUT_LINE('Average Salary: '| |v_AvgSalChar);
DBMS_OUTPUT.PUT_LINE('Average Va ation: '| | v_AvgVa | |
' weeks');
DBMS_OUTPUT.PUT_LINE('Based on: '| | v_Count | | ' agents');
3. PL/SQL, .
END . ,

 Ora le 10g  . 482

482

7. PL/SQL

. 7.4. PL/SQL

(/), Ora le ,
SQL*Plus .

EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE(SQLERRM);
END;
/
4. File , Save As, File name , . ( !) <>AnonBlo kOne.sql,
<> ,
Save as type All Files. Save, . . 7.4 PL/SQL, .
5.

File, Exit, .

, .
. ( , PL/SQL, .)
v_AvgSal
, , BaseSalary

 Ora le 10g  . 483

483

AgentsHR. , v_AvgVa
,
Va ation AgentsHR. v_Count , , Agents. ,
v_AvgSalChar  , .
 ,
/* */. SELECT
Agents v_Count.
SELECT BaseSalary Va ation.

. ,   .
.

V_AvgSalChar := TO_CHAR(v_AvgSal, '$99,999.99');


, .
 ,
(SQLERRM), OTHERS. ,
.
, ,  , ,
 .
PL/SQL  , .
END, ,
PL/SQL. Ora le ,
PL/SQL .
SQL*Plus, ,
PL/SQL. ,
.

SQL*Plus
PL/SQL SQL*Plus, . , Ora le
, .

PL/SQL pro edure su essfully ompleted.


PUT_LINE DBMS_OUTPUT (.. ), SERVEROUTPUT SQL*Plus. :

 Ora le 10g  . 484

484

7. PL/SQL

SET SERVEROUTPUT ON
,
.

PL/SQL
PL/SQL,
.
. ;
SQL*Plus Ora le.
PL/SQL,
.
1. . <> , <>  .

START <>\<>AnonBlo kOne


, ,  ,
.
2. , SQL*Plus
OFF. :

SERVEROUTPUT -

SET SERVEROUTPUT ON
3. PL/SQL - 
SQL*Plus, (/) <Enter>, .
, PL/SQL, , (. 7.5).
4. exit <Enter>, Ora le
SQL*Plus.
,

SELECT 
.  ,
. -
AgentsHR. ,
SELECT  .

 Ora le 10g  . 485

485

. 7.5. PL/SQL



 , 
 SQL. ,
PL/SQL .  , SQL*Plus, START, . PL/SQL,
. ,
.
 PL/SQL , Redwood Realty. , ,
,
.
10 14.  ,
( ).
 PL/SQL
.
1. File, Open, Look in,
,  AnonBlo kOne.sql (
, , ). Files of type All Files  Anonblo kOne.sql,
.
2. v_AvgVa v_Count
 :

v_Team AgentsHR.Team%TYPE;
3.  SELECT . v_Team INTO SELECT , SELECT GROUP BY Team -

 Ora le 10g  . 486

486

7. PL/SQL

 PUT_LINE. 
, (
).

DECLARE
v_AvgSal AgentsHR.BaseSalary%TYPE;
v_AvgVa AgentsHR.Va ation%TYPE;
v_Team AgentsHR.Team%TYPE;
/* */
v_Count INTEGER NOT NULL := 0;
v_AvgSalChar VARCHAR2(15);
BEGIN
/* , */
SELECT COUNT(AgentID) INTO v_Count FROM Agents;
/* */
SELECT Team, AVG(BaseSalary), AVG(Va ation) /* */
INTO v_Team, v_AvgSal, v_AvgVa
/* */
FROM AgentsHR
/* */
GROUP BY Team;
/* */
/*  */
v_AvgSalChar := TO_CHAR(v_AvgSal, '$99,999.99');
/* */
DBMS_OUTPUT.PUT_LINE('Team: '| |v_Team); /* */
DBMS_OUTPUT.PUT_LINE('Average Salary: '| |v_AvgSalChar);
DBMS_OUTPUT.PUT_LINE('Average Va ation: '| | v_AvgVa | |
' weeks');
DBMS_OUTPUT.PUT_LINE('Based on: '| | v_Count| | ' agents');
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE(SQLERRM);
END;
/
4.  : File,
Save As. Save as type, All Files,
File name, <>AnonBlo kTwo.sql ( <> ) Save. 
.
5. SQL*Plus ( ), Ora le
.
PL/SQL.

CLEAR SCREEN
SET SERVEROUTPUT ON
6.  PL/SQL, SQL*Plus. .
7. SQL*Plus, ,
(/), <Enter>, . Ora le
,
(. 7.6).

 Ora le 10g  . 487

487

SQLERRM -
,


. 7.6.

... more than the requested number of rows


... (... , ...)? , SELECT ( 12 15 . 7.6) 
PL/SQL , , . , .


, ,
PL/SQL. WHEN OTHERS.
, . . 7.2.  , .

EXCEPTION
WHEN VALUE_ERROR THEN
DBMS_OUTPUT.PUT_LINE('Value error was generated.')
WHEN ZERO_DIVIDE THEN
DBMS_OUTPUT.PUT_LINE('Code performs a divide by zero.')
WHEN TOO_MANY_ROWS THEN
DBMS_OUTPUT.PUT_LINE('Query returned more than one row.')
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error: '| |SQLERRM)
END;

 Ora le 10g  . 488

488

7. PL/SQL

7.2.

ACCESS_INTO_NULL


WHEN CASE ELSE

CASE_NOT_FOUND

CURSOR_ALREADY_OPEN ,
INVALID_CURSOR
, NO_DATA_FOUND
ROWTYPE_MISMATCH
TOO_MANY_ROWS
VALUE_ERROR

ZERO_DIVIDE

,
SELECT INTO
PL/SQL
SELECT INTO
,  ,
, (,
, PL/SQL
)

WHERE ,
WHERE OTHERS .
PL/SQL , . WHEN
, WHEN OTHERS.
, WHEN OTHERS, ,
.
, PL/SQL. , PL/SQL.
SQL*Plus, .
.
1. SQL*Plus:

SET SERVEROUTPUT ON
2.
SQL*Plus. <Enter>, PL/SQL. ,
.

 Ora le 10g  . 489

489

CLEAR SCREEN
DECLARE
v_TeamNo AgentsHR.Team%TYPE;
BEGIN
SELECT Team INTO v_TeamNo FROM AgentsHR;
SELECT 123/0 INTO v_TeamNo FROM DUAL;
SELECT Team INTO v_TeamNo FROM AgentsHR
WHERE Va ation = 20;
EXCEPTION
WHEN ZERO_DIVIDE THEN
DBMS_OUTPUT.PUT_LINE('Divide by zero');
WHEN TOO_MANY_ROWS THEN
DBMS_OUTPUT.PUT_LINE('Multiple rows returned');
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Something else went wrong');
END;
/
Ora le TOO_MANY_ROWS,
Multiple rows returned. Ora le (PL/SQL pro edure su essfully ompleted) .
3. -- , SELECT. . SQL*Plus
<Enter>, .
ZERO_DIVIDE ,
(. 7.7).
4. -- SELECT, .
SQL*Plus <Enter>, 
. OTHERS Something else is wrong.
5. exit <Enter>, Ora le SQL*Plus. PL/SQL,
.
6. , .
, , .
TOO_MANY_ROWS , 
Redwood Realty .

 Ora le 10g  . 490

490

7. PL/SQL

. 7.7. 

, PL/SQL SELECT, ,
. , , ,
Ora le PL/SQL , , . , , SELECT.
PL/SQL, SELECT ,
; . SELECT
. ,  .
. , ,
, .

PL/SQL
PL/SQL ,
 , (
, ). ,
Ora le .
PL/SQL , WHILE FOR.
, . WHILE , .

 Ora le 10g  . 491

491

FOR  .
, ,
 PL/SQL. ,
EXIT EXIT WHEN. :

LOOP

END LOOP;
EXIT . EXIT WHEN
, , , . ,
, v_Counter
15:

v_Counter := 1;
LOOP
v_Counter := v_Counter + 3;
EXIT WHEN v_Counter > 15;
DBMS_OUTPUT.PUT_LINE(v_Counter);
END LOOP;
PL/SQL,
4, 7, 10 13.
LOOP END LOOP,
. , ,
. EXIT EXIT WHEN
. EXIT WHEN, , ,
.
.
EXIT WHEN EXIT, . , , .
, . EXIT WHEN, ,
. , , , , , EXIT WHEN. ,
, .

/* ! */
v_Counter := 1;
LOOP
v_Counter := v_Counter - 1;
EXIT WHEN v_Counter > 15;
DBMS_OUTPUT.PUT_LINE('Infinite loop. Help!');
END LOOP;
WHILE ,
:

WHILE
<>

true.

 Ora le 10g  . 492

492

7. PL/SQL

LOOP

END LOOP:
, ,
v_Counter 15:

v_Counter := 1;
WHILE v_Counter <= 15 LOOP
DBMS_OUTPUT.PUT_LINE(v_Counter);
v_Counter := v_Counter + 3;
END LOOP;
, ,
WHILE (v_Counter <= 15). .
, . , , ,
END LOOP.
FOR  .
. ,
, . . FOR
:

FOR
<_>
IN [REVERSE
<_>
..
<_>
LOOP

END LOOP;
(, REVERSE, ).
( ,
REVERSE), .. . , (,
REVERSE, ). , REVERSE
, , ,
. REVERSE, :
, . FOR,
15 . WHILE
FOR .

 Ora le 10g  . 493

493

FOR v_Counter IN 1..15 LOOP


DBMS_OUTPUT.PUT_LINE(v_Counter);
END LOOP;
v_Counter 1. FOR.
DBMS_OUTPUT, , v_Counter 1 , .. 15.
15, .
, 15.
16. , .. 15.
15, ,
END LOOP.


, ,
. .
SELECT, ,
.

PL/SQL SELECT,
,
. , Ora le ,
. Ora le,
, SELECT. , .
.
1. ,
SELECT.
2.
.

SELECT,

3. . Ora le
(.. ).

 Ora le 10g  . 494

494

7. PL/SQL

4. PL/SQL. , , ,
.
5. . .
, , .

PL/SQL, ,
.  PL/SQL , .
, AnonBlo kTwo.sql,
, .
, SELECT .

, SELECT. ,
. SELECT,
. ,
. PL/SQL, . ,
PL/SQL. :

CURSOR <_>
[ <_>
[RETURN <__>
IS <>
[FOR UPDATE [OF (
<_>
)[NOWAIT;
 Ora le. . , . RETURN
, . SELECT.
.
FOR UPDATE , . NOWAIT
, SELECT
.

 Ora le 10g  . 495

495

,
.
1. ,  .
. File Save As.
Save as type, All Files
File name. <>AnonCursorOne.sql (<>
) Save.
2. , , , :

DECLARE
v_AvgSal AgentsHR.BaseSalary%TYPE;
v_TeamNo AgentsHR.Team%TYPE;
v_AvgSalChar VARCHAR2(15);
3. ,
PL/SQL.
 v_ (  ursor variable).
SELECT.

CURSOR v_TeamCursor IS
SELECT Team, AVG(BaseSalary)
FROM AgentsHR
GROUP BY Team
ORDER BY Team;
BEGIN
, .

PL/SQL, .
OPEN. , SELECT . .
 , PL/SQL.
OPEN .
4. .
.

OPEN v_TeamCursor;
5. , :

DBMS_OUTPUT.PUT_LINE('Team'| |' Average Salary');


.

 Ora le 10g  . 496

496

7. PL/SQL

,
PL/SQL.
FETCH , , , , , .
FETCH . :

FETCH <_>
INTO <_>;
FETCH , INTO
, . .
.
,
. , , , ..
. , . , , .
%NOTFOUND true , FETCH .  ,
%NOTFOUND. ,
, ,
v_TeamCursor%NOTFOUND.
6.  , ,
 . (
, v_TeamNo
v_AvgSalChar .
.)

LOOP
FETCH v_TeamCursor
INTO v_TeamNo, v_AvgSal;
EXIT WHEN v_TeamCursor%NOTFOUND;
v_AvgSalChar := TO_CHAR(v_AvgSal, '$99,999.99');
DBMS_OUTPUT.PUT_LINE(v_TeamNo| |'
'| |v_AvgSalChar);
END LOOP;
.

 Ora le 10g  . 497

497

, ,  .
. . , ,
:

CLOSE <_>;
CLOSE . , , Ora le (. . 7.2):

ORA-01001: invalid ursor


, .
7. ,
.

v_TeamCursor

CLOSE v_TeamCursor;
8. .
PL/SQL.

EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error: '| |SQLERRM);
END;
/
9.  . .
<Ctrl+S>, .
. 7.8 PL/SQL, .
.
10. .
PL/SQL. SQL*Plus, Ora le .
, , , SQL*Plus <Enter>. , .
1. SQL*Plus .
, PL/SQL
.

CLEAR SCREEN
SET SERVEROUTPUT ON

 Ora le 10g  . 498

498

7. PL/SQL

. 7.8. PL/SQL,

2. , AnonCursorOne.sql, . <> , <>  .

START <>\<>AnonCursorOne
, ,
. 7.9. , , PL/SQL pro edure su essfully
ompleted.
3. exit <Enter>, Ora le
SQL*Plus.

FOR
,
, FOR, FOR.
FOR
, FETCH.

 Ora le 10g  . 499

499

. 7.9. PL/SQL,

. , PL/SQL .
:

FOR

FOR
<_>
IN
<_>
LOOP
< 1>;
< 2>; ...
END LOOP;

PL/SQL, FOR .  , , , .
,
PL/SQL, FOR .
. SQL*Plus .
SQL*Plus, . PL/SQL CLEAR SCREEN SET SERVEROUTPUT ON.
 ,
, .
, , .
 FOR .
, FOR
, .
1. .
SQL*Plus.

CLEAR SCREEN
SET SERVEROUTPUT ON
2. .

 Ora le 10g  . 500

500

7. PL/SQL

. , .
, .

DECLARE
CURSOR v_Sellers IS
SELECT a.FirstName, a.LastName, .AskingPri e
FROM Customers a
INNER JOIN Properties b ON a.CustomerID = b.OwnerID
INNER JOIN Listings USING (PropertyID)
INNER JOIN CustAgentList d USING (ListingID)
WHERE UPPER(d.Conta tReason) = 'SELL'
AND a.City = 'Loleta'
ORDER BY LastName, FirstName;
3. , , FOR , ,
,
.

BEGIN
DBMS_OUTPUT.PUT_LINE(RPAD('Seller''s Name',24,' ')|
|'Asking Pri e');
FOR v_Counter IN v_Sellers LOOP
DBMS_OUTPUT.PUT_LINE(
RPAD(v_Counter.FirstName| |' '| |
v_Counter.LastName,30,' ')| |
v_Counter.AskingPri e
);
END LOOP;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No sellers in that ity');
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error of unknown type o urred');
END;
/
RPAD .
. , , , RPAD
, .

4. FOR . , . 13, SQL*Plus <Enter>,


. ,
. , PL/SQL SQL*Plus . 
. 7.10.

 Ora le 10g  . 501

501

FOR

. 7.10.

FOR

5. - , .
.
6. exit <Enter>, Ora le
SQL*Plus.

IF
PL/SQL . ,
( , ).
IF, .
, ,
. CASE, IF, 4. IF, CASE . IF
:

 Ora le 10g  . 502

502

7. PL/SQL

IF <>
THEN
<>;
[ELSIF <>
THEN
<> ;
[ELSE
<> ;
END IF;
 ,
TRUE FALSE. IF THEN. ,
, FALSE NULL, IF .
FALSE, Ora le ELSIF (,
E). ELSIF ,
, THEN. ,
ELSIF, , ,
THEN, . ELSIF ELSE. IF END
IF ( ), . ,
, THEN, ELSIF ELSE.
 IF

, , . PL/SQL,
, , Agents  Franklin.
 , . DML,
UPDATE INSERT, PL/SQL,
, .

DECLARE
v_Tally INTEGER := 0;
BEGIN
SELECT COUNT(*) INTO v_Tally FROM Agents
WHERE LastName = 'Franklin';
IF v_Tally = 0 THEN
INSERT INTO Agents (AgentID, FirstName, LastName,
Li enseStatusID)
VALUES (23456, 'Ben', 'Franklin',1001);
ELSE
DBMS_OUTPUT.PUT_LINE('Agent already in table: not added');
END IF;
END;
SELECT , Agents
 Franklin.  v_Tally. -

 Ora le 10g  . 503

503


. 7.11.

IF

IF , .. , . , TRUE.
( ) FALSE. , , TRUE, Ora le INSERT INTO.
FALSE, , ELSE, .
 , , ELSE .
IF, , , :

IF v_Tally = 0 THEN
INSERT INTO Agents (AgentID, FirstName, LastName,
Li enseStatusID)
VALUES (23456, 'Ben', 'Franklin',1001);
END IF;
, THEN ELSE, . . . 7.11 
IF ELSE.
DML- PL/SQL

SELECT PL/SQL
. PL/SQL ,  ,

 Ora le 10g  . 504

504

7. PL/SQL

7.3. ,

()

()

01,000

10

1,0011,500
1,5012,000
2,0013,000
3,000

8
6
3

.
INSERT, UPDATE DELETE, ,  , 
. , , Redwood Realty
, , ,
, , . , ,
, . , , 3000 ,
. , 600 .
,  , .
, . 7.3.
  , IF. IF
, , .
,
UPDATE, AgentsHR.
, IF. . 7.12 (-),
PL/SQL, IF UPDATE.
, ENTER. , , , 1000. ,
, TRUE, BaseSalary
10% .
 ,
, 1500. ,
BaseSalary 8% . . ,
, .

 Ora le 10g  . 505

505


10%

SalaryRatio <= 1000

SalaryRatio <= 1500


8%

SalaryRatio <= 2000


6%

SalaryRatio <= 3000


3%


. 7.12. , -

, . 
  .
,
, .. .
, ,
.
PL/SQL UPDATE. UPDATE  IF.
: SQL*Plus, Ora le .
 .
PL/SQL,
, .
1. :

CLEAR SCREEN
SET SERVEROUTPUT ON
2. ,  .

 Ora le 10g  . 506

506

7. PL/SQL

DECLARE
v_Amount NUMBER(2,2);
CURSOR v_Agent IS
SELECT AgentID, BaseSalary/Dependents AS SalaryRatio
FROM AgentsHR;
3. :

BEGIN
FOR v_Counter IN v_Agent LOOP
IF v_Counter.SalaryRatio <= 1000 THEN
v_Amount := 0.10;
ELSIF v_Counter.SalaryRatio <= 1500 THEN
v_Amount := 0.08;
ELSIF v_Counter.SalaryRatio <= 2000 THEN
v_Amount := 0.06;
ELSIF v_Counter.SalaryRatio <= 3000 THEN
v_Amount := 0.03;
ELSE
v_Amount := 0;
END IF;
DBMS_OUTPUT.PUT_LINE
(v_Counter.AgentID| | ' updated by '| |
TO_CHAR(v_Amount*100,'99')| |'%'
);
UPDATE AgentsHR
SET BaseSalary = BaseSalary * (1+v_Amount)
WHERE AgentID = v_Counter.AgentID;
END LOOP;
COMMIT;
4. , .
 ;
, . ,
SQL*Plus , ,
<Enter>.

EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error; transa tions rolled ba k');
ROLLBACK;
END;
/
5. , , ,
SQL*Plus. . . 7.13 , SQL*Plus .
6. SQL*Plus, <Enter>, PL/SQL.
. 7.14 ,
AgentID, . .

 Ora le 10g  . 507

507

. 7.13. DML- PL/SQL

7. , SQL*Plus.
, .
- DBMS_OUTPUT
FOR. , ,
Ora le .
FOR ,
BaseSalary . , ,
v_Amount. IF, FOR UPDATE v_Amount . COMMIT,
FOR, . FOR
,
ROLLBACK .

 PL/SQL, . , . , .

 Ora le 10g  . 508

508

7. PL/SQL

. 7.14.

Ora le ,  ,   Ora le. 


PL/SQL, Ora le
.  PL/SQL,
Ora le . SQL*Plus  .
 SQL*Plus PL/SQL.
 SQL.
PL/SQL EXECUTE SQL*Plus.
SQL .
,  ,
, .   . (IN), (OUT) / (IN OUT). IN , ,  ,
. OUT
, ,  ,

 Ora le 10g  . 509

, 

509

. IN OUT
, ,  , .
 , IN. . ,
 ,
.
,  
. , , , .  .  , , 
. ()
 , . ,
, , .

, 

, . ,
,  BirthDate , Redwood
Realty. , , SQL-,
. , , ,
, ,  SELECT. 
, .


 , .  Ora le
, . ,
, :

CREATE [OR REPLACE FUNCTION <_>


[( <_> [IN|OUT|IN OUT
<_> [,... )
RETURN
<_> {IS|AS}
BEGIN
<_>
[EXCEPTION

 Ora le 10g  . 510

510

7. PL/SQL

<___>;
END [ <_> ;
, OR REPLACE . , , 
; .  ,  IN,
OUT IN OUT. . , RETURN
. SQL PL/SQL, , BEGIN END.
EXCEPTION END,
. 
(/); <Enter>
. ,
 . ,  .
, .
, IN. 
. . SQL*Plus,
Ora le . , , ,
 , .
, Ora le  .
, , .
1. SQL*Plus
.

CLEAR SCREEN <Enter>,

2.  :

SET SERVEROUTPUT ON
3.  . ,
.
.

CREATE OR REPLACE FUNCTION Age(InputAgentID IN NUMBER)


RETURN INTEGER AS
v_BirthDate Agents.Birthdate%TYPE;
v_TodaysDate DATE;
v_Age INTEGER
/* */
4.   .
,
<Enter>.

 Ora le 10g  . 511

, 

511

BEGIN
SELECT BirthDate, SYSDATE
INTO v_BirthDate, v_TodaysDate
FROM Agents
WHERE AgentID = InputAgentID;
v_Age := TRUNC((v_TodaysDate-v_BirthDate)/365.25);
RETURN v_Age;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE(SQLERRM);
END Age;
/
5. , ,
<>Ch07AgeFun tion.sql. , <> . ( ,  ,
. ,
.)
6. SQL*Plus.
<Enter>.
Ora le , . , ?
SHOW ERRORS,  , , ,
.
7. SQL*Plus <Enter>,
.

SHOW ERRORS
Ora le ,
(. 7.15).
8. SQL*Plus .
. 7.15 , Ora le BEGIN
, . .
, . , .
:

V_Age INTEGER

, SHOW ERRORS,
 .

Warning: Fun tion reated with ompilation errors.

 Ora le 10g  . 512

512

7. PL/SQL

. 7.15.  ,

, .
, .
1. , .
2. , v_Age,
.
3.   .
4.  SQL*Plus.
5. .
6. SQL*Plus, , ,
<Enter>. Ora le , .
7. SQL*Plus.
Fun tion reated, ,
 . Ora le
 , ,
. ,
.

 Ora le 10g  . 513

, 

513


 ,  Ora le. ,  SELECT.
, FROM DUAL. .
.
, .
1. SQL*Plus .
PAGESIZE .
<Enter>.

SET PAGESIZE 50
CLEAR SCREEN
2. SQL*Plus .
 Age. , , ,
, .

SELECT Age(10041)
FROM DUAL;
Ora le , (
).
3.   Agents,
:

SELECT AgentID, LastName, Age(AgentID) AS "Agent's Age"


FROM Agents;
Ora le  ,  29
(. 7.16). , .  Age  .
4.  Age, :

CLEAR SCREEN
SELECT Age(12345)
FROM DUAL:
Ora le , : ORA-01403: no data found (. 7.17).
5. SQL*Plus .
? Age , 12345.
 , Ora le
. .

 Ora le 10g  . 514

514

7. PL/SQL

. 7.16. ,

. 7.17. 

,
? .   Age
.

 Ora le 10g  . 515

, 

515

 
 Age, ,
, AgentID, .
. AgentID, , , 
,  , . AgentID , 
. ,  , .
AgentID, .
1.  Age, 
<>Ch07AgeFun tion.sql,
.
2.   .

v_Count INTEGER;
3. BEGIN SELECT
( , COUNT
 1, l):

/* AgentID */
SELECT COUNT(1)
INTO v_Count
FROM Agents WHERE AgentID= InputAgentID;
IF v_Count = 0 THEN
v_Age := -1;
ELSE
4. ,
.

SELECT,

5.
:_TRUNC ... .

v_Age

6. , v_Age,
( ):

END IF;
7.  . , , . 7.18. .
, .
8. .

 Ora le 10g  . 516

516

7. PL/SQL

. 7.18.  

Age

.  SQL*Plus
<Enter>, . 
CREATE OR REPLACE FUNCTION, , ,
.
  Age ,
.
1. SQL*Plus

CLEAR SCREEN <Enter>.

2.  SQL*Plus.
<Enter>,  .
Ora le Fun tion reated ( ). ,
, ,
, , , , .
3. . (
- , .)
4. , <Enter>.

 Ora le 10g  . 517

, 

517

Age

Age

. 7.19.  

Age

SELECT Age(10497) AS "Agent that exists" FROM DUAL;


Ora le .
5.    , <Enter>:

SELECT Age(12345) AS "Agent that does not exist" FROM DUAL;


1, . .
.  . 7.19.
6. , SQL*Plus .

  
PL/SQL  ,
p-; (pseudo ode). P-  -

 Ora le 10g  . 518

518

7. PL/SQL

 , , PL/SQL.
 , , PL/SQL  - .
- .
- , C.


 , CREATE
OR REPLACE, Ora le .
, (p-).
 Ora le p- ,
.   .
USER_OBJECTS  . . USER_SOURCE  . USER_ERRORS
 , .
 , .
1. SQL*Plus

CLEAR SCREEN <Enter>.

2.  , ,
:

COLUMN Obje t_Name FORMAT A20


COLUMN Obje t_Type FORMAT A12
SELECT Obje t_Name, Obje t_Type
FROM USER_OBJECTS
WHERE Obje t_Type IN ('FUNCTION', 'PROCEDURE');
Ora le Age  .
 , 
.
3. SQL*Plus,
<Enter>:

SET PAGESIZE 40
CLEAR SCREEN
4.
:

DESCRIBE User_Sour e

User_Sour e, -

 Ora le 10g  . 519

, 

519

User_Source

Age,

. 7.20.

User_Sour e  

5.   Age,
, <Enter>:

SELECT Text
FROM User_Sour e
WHERE Name = 'AGE'
ORDER BY Line;
Ora le 

Age (. 7.20).

6. SQL*Plus .


.  :

DROP FUNCTION <_>;


, . .

 Ora le 10g  . 520

520

7. PL/SQL

1. SQL*Plus <Enter>:

DROP FUNCTION Age;


Ora le

Fun tion dropped ( ).

2. , , SQL*Plus ,   :

SELECT Obje t_Name, Obje t_Type


FROM USER_OBJECTS
WHERE Obje t_Type IN ('FUNCTION', 'PROCEDURE');
Ora le no rows sele ted ( ,  ). ,  .
3. exit <Enter>, Ora le
SQL*Plus.

, SQL PL/SQL,
,
.  .
. ;
.
, . PL/SQL
DECLARE. . .
, . SQL*Plus
CALL.
,  . ,
 .


, ,
PL/SQL, . . p-   . -

 Ora le 10g  . 521

521

SQL- 
Ora le,
SQL-. , . , , , ,
.   , .
, ,
.
. ,
.
. , ,
. 
, . , , INSERT,
. ,
, , .


() ,
. ( ) :

CREATE [OR REPLACE PROCEDURE <_>


[( <_> [IN|OUT|IN OUT
<_> [, ... ) {IS|AS}
[
<__>

BEGIN
<_>
[EXCEPTION
<___>
END [ <_> ;
, OR REPLACE . , ,
; . -

 Ora le 10g  . 522

522

7. PL/SQL

,  IN,
OUT IN OUT. . ,
.  ,
PL/SQL.
DECLARE. SQL PL/SQL, ,
. BEGIN END.
EXCEPTION END,
. , (/); <Enter> .  ,
; SQL*Plus
<Enter>. , .
, .
ForSale,
, . (CityIn) (IN) .
, , .
, SQL, , . ,
WHERE , .
, .
SQL*Plus ; ,
. Ora le ForSale.
1. ,
ON SERVEROUTPUT.

CLEAR SCREEN
SET SERVEROUTPUT ON
2. ,
, . Redwood Realty ,
.

CREATE OR REPLACE PROCEDURE ForSale(CityIn IN VARCHAR2) AS


CURSOR v_Properties IS
SELECT PropertyID,AskingPri e,Address,City

 Ora le 10g  . 523

523

FROM Properties JOIN Listings USING(PropertyID)


JOIN CustAgentList USING(ListingID)
WHERE UPPER(City) = UPPER(CityIn)
AND UPPER(Conta tReason) = 'SELL';
3. . FOR
DBMS_OUTPUT 
. , . , . ,
DBMS_OUTPUT Prop ID
Asking. ,  TO_CHAR
PropertyID , O.

BEGIN
DBMS_OUTPUT.PUT_LINE('Prop ID '| |'Asking '| |'Address');
FOR a IN v_Properties LOOP
DBMS_OUTPUT.PUT_LINE(
TO_CHAR(a.PropertyID,'00000')| |
' '| |TO_CHAR(a.AskingPri e,'$9,999,999')| |
' '| |a.Address| |', '| |INITCAP(CityIn)
);
END LOOP;
4. ,
, .

END, -

EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE(SQLERRM);
END;
/

ForSale , . 7.21.

5. , . ,  .
 <>Ch07ForSale.sql.
6. , . , ,
( ) SQL*Plus.
7. SQL*Plus, <Enter>,
. ,
, ,  . 6 7.
Pro edure reated
(. 7.22).
8. ,
SQL*Plus .

 Ora le 10g  . 524

524

7. PL/SQL

. 7.21.

ForSale

( )

. 7.22. , , -

Pro edure reated , Ora le .


, .
Ora le . ,
, .

 Ora le 10g  . 525

525

. , (, 'Loleta').
DBMS_OUTPUT, BEGIN,  . FOR  ,
Conta tReason SELL ( , , ).
, (CityIn), WHERE , .
, FOR , , ,
 CityIn. DBMS_OUTPUT END LOOP.
INITCAP. TO_CHAR
 . WHERE OTHERS
THEN, . ,
, .


CALL. , EXECUTE 
EXEC. CALL. .
SQL*Plus, .
1. SQL*Plus . , .
, .

SET SERVEROUTPUT ON
CLEAR SCREEN
2. ForSale,  , , .
<Enter>, :

CALL ForSale('loleta');

ForSale  .
3. ForSale,  ,

, .

 Ora le 10g  . 526

526

7. PL/SQL

<Enter>, . (
, ,
.)

CALL ForSale('ORICK');

Call ompleted.

4. ForSale ,  , .

CALL ForSale('Blue Haven');


. .
5. , . , ,
. .
<Enter>.

CALL ForSale('BLUE lake');


. .
6. . ForSale . <Enter>.

CALL ForSale();
Ora le .
(. 7.23).
: , , , ,
,  , , , .

,

 . , SQL*Plus. ( CREATE OR REPLACE. OR REPLACE


, Ora le
, , .)

Ch07ForSale.sql, .
, .
,
, <>Ch07ForSale.sql.

 Ora le 10g  . 527

527

ForSale


. 7.23. 

ForSale

  ,
.
1.  . File Save
As,  <>Ch07ForSale2.sql, <>
.
2.
,
.

v_PropCount INTEGER NOT NULL := 0;


v_PropCount
. ,
.
3.
v_Properties LOOP:

FOR a IN

v_PropCount := v_PropCount + 1;
4.
.

END LOOP: EXCEPTION:

 Ora le 10g  . 528

528

7. PL/SQL

IF v_PropCount = 0 THEN
DBMS_OUTPUT.PUT_LINE
('******No homes for sale in '| |INITCAP(CityIn));
END IF;
5. , .
6. SQL*Plus CLEAR SCREEN <Enter>,
. SQL*Plus
<Enter>,  . Ora le Pro edure reated.
, SHOW ERRORS, <Enter>
.
7. , , ,
. 5.
8.  . ( <Enter>):

CALL ForSale('Loleta');
CALL ForSale('Mit hell');
, . , ,
(. 7.24).
9. , .
10. SQL*Plus, .



,
. , , .


,  , CREATE OR REPLACE, Ora le .


,
(p-).  . USER_OBJECTS
 .
. USER_SOURCE

 Ora le 10g  . 529

529

. 7.24. 

ForSale

 . SQL*Plus , . , , . -
, .
.
 , .
1. SQL*Plus

CLEAR SCREEN <Enter>.

2.  , ,
:

COLUMN Obje t_Name FORMAT A20


COLUMN Obje t_Type FORMAT A12
SELECT Obje t_Name, Obje t_Type
FROM USER_OBJECTS
WHERE Obje t_Type IN ('FUNCTION', 'PROCEDURE');
Ora le ForSale. , . ( ,  Age
. ,  .)

 Ora le 10g  . 530

530

7. PL/SQL

. 7.25.

3. SQL*Plus,
<Enter>:

SET PAGESIZE 40
CLEAR SCREEN
4.  ForSale,
, <Enter>.
FORSALE :

SELECT Text
FROM User_Sour e
WHERE Name = 'FORSALE'
ORDER BY Line;
Ora le 25 ForSale (. 7.25).
5. SQL*Plus .

.
:

DROP PROCEDURE <_>;

 Ora le 10g  . 531

531

, . .
1. SQL*Plus <Enter>:

DROP PROCEDURE ForSale;


Ora le

Pro edure dropped.

2. , Ora le ,
SQL*Plus ,  
.

SELECT Obje t_Name, Obje t_Type


FROM USER_OBJECTS
WHERE Obje t_Type IN ('FUNCTION', 'PROCEDURE');
Ora le no rows sele ted. ,
, .
3. exit <Enter>, Ora le
SQL*Plus.
4. ,
, . ,
.

PL/SQL  , SQL. PL/SQL


() .
. ,
. PL/SQL
: , , ,
BEGIN, .  , ,
. , . PUT_LINE
DBMS_OUTPUT , ,
SQL*Plus SERVEROUTPUT ON.
SQL PL/SQL , .
,
LOOP END LOOP. ,

 Ora le 10g  . 532

532

7. PL/SQL

EXIT EXIT WHEN. EXIT


, EXIT WHEN .
WHILE , TRUE.
, FOR. FOR
, ( ) ( ).
FOR  , ,
, .
PL/SQL , Ora le
. , . ,
. 
. FETCH, ,
. SELECT
, INTO
PL/SQL, Ora le , .
. FOR
, .
PL/SQL:  . , .  . PL/SQL .
, . . , 
. , ,
 , . ,  , . , (p-), . USER_OBJECTS.
 PL/SQL
.  ;
.

END
IN
IN OUT
OUT

P-

 Ora le 10g  . 533

533












()


-





FOR
FOR

1. .
2. PL/SQL ,
FOR.
3. , PL/SQL, , ,
SQL*Plus, PL/SQL
.
4. , .

 Ora le 10g  . 534

534

7. PL/SQL

5. PL/SQL, , ,
PL/SQL.

1. , ,
, _____.
2. , , _____.
3. _____  , ,
SELECT, PL/SQL.
4. PL/SQL  , _____ .
5. , ,
_____ .

1. Ora le _____ PL/SQL, PL/SQL ,  .


) .
) .
) .
) .
2. PL/SQL :
) SELECT .
) , , CREATE
BLE.

TA-

) , , UPDATE.
)  ,

v_Variable := 24;
3. PL/SQL
)  .
)  .
)  .
)  .

 Ora le 10g  . 535

535

4. , , . ?
) FOR.

FOR .
) WHILE.

) .
5. ?
) RUN

EXECUTIVE
) INVOKE
) CALL

1. Readwood Realty

 ,
, .
(- ,
.) MyListings
SQL*Plus iSQL*Plus. :

CALL MyListings('<_>' | 'all');


-. , .
all,
- .
,  : ListingID, , , BidPri e AskingPri e. , .
SQL*Plus Ora le. , MyListings .
 , .
, .
1.
 <>Ch07Prob1Part1.sql.

CREATE OR REPLACE PROCEDURE


MyListings(AgentNameIn IN VARCHAR2) AS
v_Count INTEGER;

 Ora le 10g  . 536

536

7. PL/SQL

CURSOR v_AllAgents IS
SELECT ListingID, d.LastName AS
LISTAGENT,
a.LastName AS BUYAGENT,
BidPri e, AskingPri e
FROM Agents a JOIN CustAgentList b ON
a.AgentID = b.AgentID
JOIN Listings ON b.ListingID =
.ListingID
JOIN Agents d ON
.ListingAgentID = d.AgentID
WHERE UPPER(Conta tReason) = 'BUY'
ORDER BY d.LastName, a.LastName;
CURSOR v_OneAgent IS
SELECT ListingID, d.LastName AS
LISTAGENT,
a.LastName AS BUYAGENT,
BidPri e, AskingPri e
FROM Agents a JOIN CustAgentList b ON
a.AgentID = b.AgentID
JOIN Listings ON b.ListingID =
.ListingID
JOIN Agents d ON
.ListingAgentID = d.AgentID
WHERE UPPER(Conta tReason) = 'BUY'
AND UPPER(d.LastName) =
UPPER(AgentNameIn)
ORDER BY a.LastName;
2. ,  :

BEGIN
/* Agents */
SELECT COUNT(*)
INTO v_Count FROM Agents
WHERE UPPER(LastName) =
UPPER(AgentNameIn);
-- : ALL, ,
IF UPPER(AgentNameIn) = 'ALL' THEN
FOR X IN v_AllAgents LOOP
DBMS_OUTPUT.PUT_LINE(
' '| |X.ListingID| |
' '| |RPAD(X.LISTAGENT,15)| |
' '| |RPAD(X.BUYAGENT,15)| |
' '| |TO_CHAR(X.BidPri e,
'$99,999,99')| |
' '| |TO_CHAR(X.AskingPri e,
'$99,999,99')
);
END LOOP;
ELSE IF v_Count > 0 THEN
FOR X IN v_OneAgent LOOP
DBMS_OUTPUT.PUT_LINE(
' '| |X.ListingID| |
' '| |RPAD(X.LISTAGENT,15)| |
' '| |RPAD(X.BUYAGENT,15)| |

 Ora le 10g  . 537

537

' '| |TO_CHAR(X.BidPri e,


'$99,999,99')| |
' '| |TO_CHAR(X.AskingPri e,
'$99,999,99')

);
END LOOP;
ELSE
DBMS_OUTPUT.PUT_LINE('Invalid Agent
Name');
END IF;
3. .
. <Enter>.

EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE(SQLERRM);
END MyListings;
/
4. , SQL*Plus.
5.  SQL*Plus, <Enter>, .
, SHOW ERRORS, , .
, . 4 5.
6. MyListings, SQL*Plus ( ). , 
SIZE.

SPOOL <>\<>Ch07Prob1Part2.txt
SET SERVEROUTPUT ON SIZE 123456
CALL MyListings('Carling');
CALL MyListings('Quinn');
CALL MyListings('all');
SPOOL OFF

7. MyListings, SQL*Plus :

DROP PROCEDURE MyListings;

8.  MyListings  , . 6.

.  . SQL*Plus
.
2. Coffee Mer hant

 . .  .  ,

 Ora le 10g  . 538

538

7. PL/SQL

.
 .
SQL*Plus, Ora le 
BuildCoffee.sql, CoffeeMer hant. .
1.  SalesTax,
.  .

.  .

SELECT SalesTax('CA')*Pri e*Quantity FROM OrderLines;


,  , .
1. , SQL*Plus. ,
 <Enter>.
, -- .

-- < >
CREATE OR REPLACE FUNCTION
SalesTax(StateInParm IN VARCHAR2)
RETURN NUMBER AS
v_Count NUMBER;
v_TaxRate NUMBER(4,4) := -0.05;
BEGIN
/* */
SELECT COUNT(*)
INTO v_Count
FROM States WHERE UPPER(StateID) =
UPPER(StateInParm)
OR UPPER(StateName) =
UPPER(StateInParm);
IF v_Count > 0 THEN
SELECT TaxRate
INTO v_TaxRate
FROM States
WHERE UPPER(StateID) =
UPPER(StateInParm)
OR UPPER(StateName) =
UPPER(StateInParm);
END IF;
RETURN v_TaxRate;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE(SQLERRM);
END SalesTax;
/

 Ora le 10g  . 539

539

2.  <Enter>. , . 
SHOW ERRORS.  . SQL*Plus .
2. . SQL*Plus.
<> , <>  .
<>  .
3. , 

SalesTax:

SPOOL <> \ <>Ch07Prob2Part2.txt


-- <>
SET SERVEROUTPUT ON
SELECT StateName AS "State",
TO_CHAR(SalesTax(StateID)
*100,'90.99')| |'%' AS "Tax (%)"
FROM States
WHERE Population/LandArea < 20
ORDER BY 2;
SELECT SalesTax('CaLiFoRnIa')*10*24.95 FROM
DUAL;
SELECT SalesTax('NoSu hState') FROM DUAL;
SPOOL OFF
4. , .
3. CustomerOrder,
  .
, , . ,
SQL*Plus. ,
,  ,
(Jerry Peterson).
5. ,
SQL*Plus.

CREATE OR REPLACE PROCEDURE CustomerOrder(


Fname IN VARCHAR2,
Lname IN VARCHAR2) AS
v_Count INTEGER;
CURSOR v_GetOrd IS
SELECT OrderID,OrderDate,Name,
Quantity,Pri e,Dis ount,
TO_CHAR(Pri e*Quantity*(1Dis ount),'$9,999.99') AS
ExtPri e,
TO_CHAR(Pri e*Quantity*(1Dis ount)*SalesTax(State),

 Ora le 10g  . 540

540

7. PL/SQL

'99.99') AS Tax
FROM Consumers a
INNER JOIN Orders
b USING
(ConsumerID)
INNER JOIN OrderLines USING
(OrderID)
INNER JOIN Inventory d USING
(InventoryID)
WHERE LOWER(FirstName) = LOWER(Fname)
AND LOWER(LastName) = LOWER(Lname)
ORDER BY OrderID, LineItem;
BEGIN
/* */
SELECT COUNT(*) INTO v_Count FROM
Consumers
WHERE LOWER(FirstName) = LOWER(Fname) AND
LOWER(LastName) = LOWER(Lname);
CASE
WHEN v_Count > 0 THEN
FOR X IN v_GetOrd LOOP
DBMS_OUTPUT.PUT_LINE(
' '| |X.OrderID| |' '| |X.OrderDate| |
' '| |RPAD(X.Name,20)| |
' '| |TO_CHAR(X.Quantity,'999')| |
' '| |TO_CHAR(X.Pri e,'$9999.99')| |
' '| |X.ExtPri e| |' '| |X.Tax
);
END LOOP;
ELSE
DBMS_OUTPUT.PUT_LINE ('No orders for that ustomer');
END CASE;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE(SQLERRM);
END CustomerOrder;
/

6. <Enter>. ,
. , .
4.
.

CustomerOrder.  

7. , ,
SQL*Plus. , <> ( )
<>.

SPOOL <>\ <>Ch07Prob2Part4.txt


-- <>
SET SERVEROUTPUT ON
CALL CustomerOrder('Jerry','Peterson');
CALL CustomerOrder('Abe','Lin oln');
SPOOL OFF

 Ora le 10g  . 541

541

8. .
:

DROP FUNCTION SalesTax;


DROP PROCEDURE CustomerOrder;
9. SQL*Plus.
3. Rowing Ventures


 .  , .
 .
SQL*Plus, Ora le  BuildRowing.sql, RowingVentures.
.
1. Ra eInfo, : BoatCat BowNo VARCHAR2.
Boat, BoatCrew Person :
BoatCategory, BowNumber, Position, LastName FirstName. (BoatCat) (BowNo),
. BoatCat,
all, BowNo any, .
BoatCategory, BowNumber Position . Position,
Cox ( , FOR). Position . (
TO_CHAR .)
FOR ;
IF/THEN/ELSIF, ,
, . PUT_LINE DBMS_OUTPUT. .
2. Ra eInfo. SQL*Plus. <> , <> .  .

CLEAR SCREEN
SPOOL <>\ <>Ch07Problem3Part1.txt
PROMPT <>
SET PAGESIZE 100
SET SERVEROUTPUT ON
CALL Ra eInfo('ALL','X');
CALL Ra eInfo('Crew-8','3');
SPOOL OFF

 Ora le 10g  . 542

542

7. PL/SQL

3.  ,
.
Ra eInfo. -

obje t_name obje t_type,  .

Ra eInfo.
COLUMN , 20 12
.  ,
, .
4. Broad loth Clothing

. SQL*Plus, Ora le
 BuildClothing.sql, Broad loth  7.
.
 ItemTC, .  C S ( ,
).  . ItemTC
OrderItem Broad loth. , OrderItem
ItemID. 
ItemID?  (OrderQuantity,
SalePri e) ItemID?. ItemTC :

ItemTC(< hara ter>,<itemID_number>)


 , , ItemID
, S, OrderQuantity SalePri e ItemID. ItemID,
OrderQuantity SalePri e  OrderItem.
1.  ItemTC, , ,
. ,
  ,  .
, .

 Ora le 10g  . 543

543

2.

,

Ch07Prob4Part2.txt , . SELECT .
<>

, :

ORA-06503: PL/SQL: Fun tion returned without value


. SELECT
.

-- <>
SET SERVEROUTPUT ON
SELECT ItemTC('S',1017), ItemTC('C',1017)
FROM DUAL;
SELECT ItemTC('C',12345)
FROM DUAL;
SELECT ItemTC(' ',1005), ItemTC('s',1005)
FROM DUAL;
  .

LECT  2 40530.5.

SE-

3.  ,
ItemID :

Order item with ItemID xxxxxx was not found.


,
Ora le, , ItemID ItemID
OrderItem. ( ItemID
xxxxxx . ,
OrderItem.) .
 :

-- <>
SET SERVEROUTPUT ON
SELECT ItemTC('C',12345) FROM DUAL;
DROP FUNCTION ItemTC;
, , SQL*Plus.

 Ora le 10g  . 544

 Ora le 10g  . 545

Forms Builder
. . .


 Ora le
 Forms Builder
 
 



 , 
. ,  ,  .
 ,
. , ,
Redwood Realty, 
CustAgentList. ,
, CustomerID, AgentID ListingID.
, .  ,
 . 
.

 Ora le 10g  . 546

546

8. Forms Builder

 , , .  
. , , 
, .  ,
.  Ora le
Web-.  Web-, Java.


, , : ,
; ,
; , (
, ). ,   , .
 , , .  ,
.
 , . . 8.1  Agent
Redwood Realty.
. , ,
Save,  Agent
. , . ,
. ,   . 
, .
, , . , , , ,
 .
 . . 8.2  Li ense Status,
, .
,
. , . -

 Ora le 10g  . 547

. 8.1.

Agent

547

Redwood Realty -

.

.
.
- , ,
. ,  :  ,
 , . . 8.3 , 
.   ,
.
, , (
 ).
,
. ,  ,
. ,  ,
.

 Ora le 10g  . 548

548

8. Forms Builder

. 8.2.

Li ense Status,

. 8.3.

Agent Conta ts,

 Ora le 10g  . 549

549


, ,
, . Ora le  /,  . 9i  Ora le Web- Java. 
.
 Web , . Web-,
Java. Web-
.
Java. , , Java, Sun (www.java. om). Java
. . ,
,
Java, 
, .
. Forms Builder ,
Windows .
My Computer, Properties
Advan ed Environment Variables.
New : name=FORMS90_HIDE_OBR_PARAMS,
value=False.

. 8.4,  Ora le ,
Forms Builder, Developer Suite. , ;
.  ,
, . 
, 
Web-. 
. .
, ,
. Java- JInitiator, Ora le. ( )
,  Ora-

 Ora le 10g  . 550

550

8. Forms Builder




Name.fmx

Forms Builder
Name.fmb

Web-

Java

. 8.4. Web

le. , . ,
,
.
, .
1. Developer Suite. , , ,
. Net Manager
.
2.  Forms Builder.  . , , .
3.  , OC4J (Ora le
Containers for Java). Start Windows; : Ora le Developer/Forms Developer/Start OC4J Instan e.
4. Web- , , .
ToolsInternet OptionsSe urity. 
. , JInitiator.
5.  Ora le,   .

 Ora le 10g  . 551

551

. 8.5. Ora le,

Forms Builder

 Ora le
 Ora le . ,  ,
. . 8.5 , Forms Builder.
, ,
( anvas). , .
. , ,
.
 .
, ,
. , , .
.

 Ora le 10g  . 552

552

8. Forms Builder

( Obje t Navigator) 
.
. .  . , .
Agents,
.  .
, ,
Obje t Navigator. , .
Obje t Navigator, , , , <Ctrl>.
.
, ,  , , . 
, PL/SQL, . ,
 ,  ,
. .
. ,
, (smart trigger list).
.
Other, , . ,
.
.


Forms Builder
, ,   .
1. SQL*Plus  BuildRedwood.sql.
.

Forms Builder, .
 , ,  , Ora le. -

 Ora le 10g  . 553

553
, ,
, .


Forms Builder, , Ora le.
. , , SQL*Plus. , ,
.  , 
, . ,
. .

( , , ). ,
 , .
 Ora le :
. .
. ,
, .
1. Forms Builder . .
2. .
3. .
4. , .
5. , , .
6. , .
7. , .
8.  , .
, . , , . .
, , .
1. ,
2. .

ToolsData Blo k Wizard.

 Ora le 10g  . 554

554

8. Forms Builder

Browse

. 8.6.

3. ,
4.
.

Browse,

Table or View.

5.

Customers.

6. (),  .
. 8.6 . Customers,
. ,
 Customer,
(. 8.7).

. , ,
.

 Ora le 10g  . 555

555

. 8.7.

7. Next, Customers
Finish. ,
. , ToolsLayout Wizard.
. ,
, ,  . .
1.

New Canvas

Next.

2. , .
Next.
3. . 72, . , , Address, ,
144. Next.

 Ora le 10g  . 556

556

8. Forms Builder

4.  (Form

Tabular) Next.

5. , .  . 
Customers, Next.
Finish.
6. , .
, , , .
,   .
,
. , ,
, .
. . 8.8 ,
. , . , .
,
. , . ,
.
, .  . 
,  (72
). ,
. ,
, ,  12
12 .
, , , , .
 "Form- "Tabular- .  , 
; .  , .  Customer
"Form-, . , . 8.9, , , , .  Customer , .

 Ora le 10g  . 557

557

. 8.8.

   . , . , ,
. Customers. .
. ,
, , , . , , ,
- . ,
 , , 
. , , 
, .
 Customer. ,
 Finish.

 Ora le 10g  . 558

558

8. Forms Builder

. 8.9.

. 8.10  Customer,
. , , ,
, .
. ,
. .
 .
,  . 
Update Layout, Automati ally. , . , ,
, Manually, .
, . ; ,
.

 Ora le 10g  . 559

559

. 8.10. 

Customer


,  , . 
. , , Ora le ,  .
1. Windows OC4J, Start OC4J
Instan e. .
.
.
2.  
, . ,
ProgramRun Form.
3. JInitiator.
(
Windows XP SP2),  ,
.

 Ora le 10g  . 560

560

8. Forms Builder

. 8.11. JInitiator

4.  ,
Exe ute Query QueryExe ute.
.  OC4J, . OC4J 
.

. OC4J, Windows Shutdown OC4J Instan e. OC4J,


.

 Ora le JInitiator. ,
. . 8.11 ,
JInitiator (
). , .
, ,
. ,
. , Forms Builder .

 Ora le 10g  . 561

561

. 8.12.

Customer

. 8.12 , . , .
(Exe ute Query), , .
, ,
, ,
. , Exe ute Query. ,

Next Re ord

Previous

(
), . . , , ,

Re ord

 Ora le 10g  . 562

562

8. Forms Builder

, ( )

Save

. , , -

.  Close
.
. Close
, , , . 
OC4J .


 . , ,
. , ,
 , ,
. , .
 Customer, Exe ute Query.
,
 , , Enter Query
. . 8.13 .
,  , B%.
. SQL WHERE
Lastname LIKE 'B%'.
. , ,
Exe ute Query
. 8.14.
 . ,
Next Re ord
. , ,  B.
;
AND.
, . , , , . ,
, .
 . .  ,
.

 Ora le 10g  . 563

 

563

. 8.13. ,  B

 

 .  ,
.
, .

 Ora le 10g  . 564

564

8. Forms Builder

. 8.14. 


 . , Form Builder,
, . ,
<F2> ToolsLayout Editor.
, .

Customers, .
2. Automati ally Manually. ,
1. 

 Ora le 10g  . 565

 

Property Palette

Layout.

565

Manually Update

, , . 
, .
.
3. WorkPhone .
4. <Ctrl>,
LayoutAlign Components.
5.

Verti ally

Distribute.

6. Align Left
, .
, , , .
, .
, ,
<Ctrl>. . 8.15 , . , ,
, , Distribute,
.
.


. 8.16, ,
. , .
. , , <Tab>. . ,
 Customers  CustomerID FirstName,
LastName .. WorkPhone.  , .
, 
<Tab>.

.

 Ora le 10g  . 566

566

8. Forms Builder

. 8.15.

1.

Obje t Navigator

WINDOW1.

Windows

2.

REDWOOD_REALTY.

 . ,
. ,
. ,
, .


( Property
Palette). Property Palette , .
, ,
. ,
Database  . 

 Ora le 10g  . 567

 

567

. 8.16.

Height Width,
.
, X Y. , ,
. , , , <Ctrl>, .
. 8.17  .
; ,
, .
 . 
 .

 Ora le 10g  . 568

568

8. Forms Builder

. 8.17.  

1. .
Property Palette,
Customers.
, . , Find,
. ;
,
.
2. Find s r. , Property Palette
No Show S roll Bar.
3. ,  .
 10 .

 Ora le 10g  . 569

 

569

,
,
.
4.

Redwood_Realty.

,
. , Windows
. ; ,
.


 , ,
 (, JPEG, GIF
TIFF).  .
.
1. , EditImport,
.
2. , . ,
<Shift>.
<Shift> .
. ,
 ,
.  ,
, .
. 8.18  .
.
 , ,
. , , , ,
 . Redwood Realty
 , . 
.
, .
,
.
 , , ,
.

 Ora le 10g  . 570

570

8. Forms Builder

. 8.18.

1. Listings HousePhoto,
SQL*Plus, :

ALTER TABLE Listings


ADD HousePhoto LONG RAW;
2. Listings
.
null.  ,
. , .

 Ora le 10g  . 571

 

. 8.19.  

571

Listings

3. 
Listings, Listings.
,  Customer.
. 8.19 . ,
 48. , , Manually Update Layout
.  ,  . ,
.
 Ora le . ,
. 
, .
, .

 Ora le 10g  . 572

572

8. Forms Builder

 , ,
, .
,  Listings
, .
, .
.
1.  Text Item
Prompt File to Load.

ImageFileName.

. CmdLoadImage , 2.
Label Load Image.
3. ,
WHEN-BUTTON-PRESSED.

Triggers,

4. PL/SQL :

READ_IMAGE_FILE(:IMAGEFILENAME,'ALL', 'HOUSEPHOTO');
5.
.

, PL/SQL.

6.  . ,  , Load Image.

Save

, .

. 8.20  , .
, , .  , , .


,  Listings 
. , , ,
. , Agents.
, ,
ListingAgentID.
Agents  Listings? 
.  .
,
. ,
. .
 ,
.

 Ora le 10g  . 573

 

573

. 8.20.

1. Listings.
Property Palette. Query Data
Sour e Name.
(Listings).

SELECT. .
(SELECT ListingID, PropertyID, ListingAgentID, SaleStatusID,
BeginListDate, EndListDate, AskingPri e, HousePhoto,
FirstName, LastName
FROM Listings INNER JOIN Agents ON
Listings.ListingAgentID=Agents.AgentID)

2.

 Ora le 10g  . 574

574

8. Forms Builder

8.1.

Name
Prompt
Prompt Atta hment Offset
Database Item
Column Name
Primary Key
Query Only

FIRSTNAME
First Name
7
Yes
FIRSTNAME
No
Yes

,
SQL*Plus. , . ,
Agents FirstName LastName. 
, .
 ,
.
3. Query Data Sour e Columns, More ... .
FIRSTNAME LASTNAME Column Names.
 , Query Data
Sour e Columns. , . FIRSTNAME LASTNAME.
.
.  .
4.  Text Item. FirstName
Name  .

Last-

5. . . 8.1 FIRSTNAME. , Primary


Key No. LASTNAME.
6. , :
Database Item = Yes Column Name = FIRSTNAME.
LASTNAME.
 . , , .
, ,  -
. .
. , -

 Ora le 10g  . 575

 

575

, ,  .
, 
.


, .
. ,
.  .
,  ?
, ,
. ,
.
 (list of values  LOV). 
, , .
, 
. , :
Agents,
Listings.
,
List of Values Wizard, Tools. , SELECT, . 
LOV .
1. ,

ToolsList of Values Wizard.

2. , .
3. SQL-, , , SQL*Plus,
.
4. Query Builder
. ,  SQL SELECT. AgentID, LastName
FirstName. Sort LastName, FirstName
.
5. Query Builder, SQL.
Syntax.
6. LOV, .

Che k

 Ora le 10g  . 576

576

8. Forms Builder

. 8.21. List of Values Wizard

7. : 48 
72 . Automati ally size olumns.
8. Return Item AgentID,
Look up return item ListingAgentID.
.
. 8.21 . , Listings.ListingAgentID AgentID. (AgentID)
 (Listing.ListingAgentID). ,
, LOV ,
ToolsList of Values Wizard.
LOV . , LOV
, LOV  (ListingAgentID).

 Ora le 10g  . 577

 

577

9. List of Agents , 235. ,


20 .
10. 8. (Listings.ListingAgentID) .
11. LOVs LOV LOV_AGENTS. ,
RG_LOV_AGENTS.
12. . , . ,
 .
 List of Values. ,
. ,
<Ctrl+L>.
. 8.22. . Find
. ,  . AgentID, Lastname Firstname.   , Mar oux,
%Mar%. ,  .
, , , ,
.  , LOV
, , . ,
LOV. , .
, :

Go_Item('LISTINGAGENTID');
List_Values;


.
,  , .
Exe ute Query, ,
.  , ,

 Ora le 10g  . 578

578

8. Forms Builder

. 8.22. ,

.  .
.
1. Forms,
LISTINGS. Triggers . WHEN-NEWFORM-INSTANCE, PL/SQL. :

EXECUTE_QUERY;
2. . .

 Ora le 10g  . 579

 

579

 . , .

. , , 
.



. 
. , ; 
.
. ,
 .
 , , ,  ,
. , , ,
, ..
. , , , . , . , ,

. , ,
. , 
, , , .
, , Properties.
, .
1. Forms Builder, , ,
OC4J.
Properties.
2. , ,
Next.
3. Zone, .
Text Item Radio Group.

Item_type

 Ora le 10g  . 580

580

8. Forms Builder

. 8.23.

, Properties .
, .
, Zone Text Item,
, Radio Group. . 8.23 ,
. ,
, .
4. ,
 .
5.  Radio Button. : Name=RAD_R1, Label=Residential R1, Radio Button Value=R1. R2, R3 C1.

 Ora le 10g  . 581

 

581

Zone. . Zone
. : R1  ; R2 R3 
; C1  . , , Redwood Realty
.
.
. Zone, . , ,
. . 8.24 ,
. (RAD_R1);
, .
, (Residential R1). , (R1). , .
, Zone
Properties.
, 
.
Residential R2. Residential R3 Commer ial C1. LayoutAlign Component, .
Zone . Initial Value. ;
R1. ,
.
,
Zone.
6. Obje t Navigator, R1 Initial
Zone.

Value

7. . .
 . ,
R1 , .
, R2. .
, ,
.  . 8.25.

 Ora le 10g  . 582

582

8. Forms Builder

. 8.24.


, , . . . , . (
) 
.


 , , , . ,  , .

 Ora le 10g  . 583

583

. 8.25. 

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

 Ora le 10g  . 584

584

8. Forms Builder

 ,
, (
Next).
1.
.

Li enseStatus

2. .
3. , , 48, , , 176.
4.

Form Tabular.

5. (20) .
. 8.26 ,
. .  , ,
, . .
, .
. 8.27  Li ense Status. 
Exe ute Query, ,  , .
, .
. , , , Save.
 ( 20),
. ,
.


 . .
,
.  . , , ,  ,
, . , .
.  . Sale Item Sale
SaleID.

 Ora le 10g  . 585

585

. 8.26. 

Redwood Realty
 ,  . , : Agents CustAgentList. AgentID.
CustAgentList  . ,
, CustAgentList
, AgentID, CustomerID ListingID. Conta tReason ; . ,
CustomerID ListingID, .
,
,  , , .
,
. ,
. , -

 Ora le 10g  . 586

586

8. Forms Builder

. 8.27. 

Agents.
, ,  .
. 
Agents. , , .
. , , , .
, .

1. , Agents. AgentID, Title, FirstName, LastName WorkPhone.


2. , .
. CustAgentList
.

 Ora le 10g  . 587

3. Create Relationship,
gentList Agents AgentID.

587

CustA-

. CustAgentList . 
Agents.
CustAgentList Agents AgentID. . 8.28
. ,
. , , .
 , Join Condition CUSTAGENTLIST.AGENTID = AGENTS.AGENTID.
, Auto-join data blo ks. Create Relationship.
, .
.
1. 

Auto-join data blo ks.

2.

Create Relationships.

3. .
4.

Agents.

AgentID.
6. Master Item AgentID.
5.

Detail Item


CustAgentList. 
48 . , , . , 10, ,
.
7. , ,
CustAgentList . AgentID,
. , 10,
.
. 8.29 . , , ,
, .
 . ,
 (Agents)   
. , .

 Ora le 10g  . 588

588

8. Forms Builder

. 8.28. 


 ,
. , , , AgentID . ,
.
1.

AgentID .

, CustomerID ListingID
, .
. ,
 ,
, , .
, , ,
, .

 Ora le 10g  . 589

589

. 8.29. 

2. CustAgentList
Data Sour e , :

(SELECT CUSTAGENTLIST.CUSTOMERID, AGENTID, LISTINGID,


CONTACTDATE, CONTACTREASON, BIDPRICE, COMMISSIONRATE,
LASTNAME, FIRSTNAME FROM CUSTAGENTLIST INNER JOIN CUSTOMERS
ON CUSTAGENTLIST.CUSTOMERID=CUSTOMERS.CUSTOMERID)
,

SELECT .

3.  , Query Data Sour e Columns


.
,
.
. Conta tReason,
.

 Ora le 10g  . 590

590

8. Forms Builder

8.2. , ,

Name
Enabled
Column Name
Query Only
Prompt

FIRSTNAME
No
FIRSTNAME
Yes
First Name

4. Conta tReason <Ctrl+C>, . <Ctrl+V>, . FirstName;


, . 8.2.
5. . 4, LastName, FirstName.
6. Enable
.

No,

 . ,
.


 , , , .
. ,
. , .

BidPri e End Justifi ation.


2. FML99G999G999 Format Mask.
1.

. ,  (format mask). <F1> ,


. Ora le
. . , L
. L , (
). ,

 Ora le 10g  . 591

591

8.3. , 


X
A
9
0
YYYY
MM
DD




;
, 
, 

, G , .
, D. , : 123,456.78.
123.456,78.  FM
, , ..
, . FM
; , ,
.
. 8.3,  , , .
 , , . , ZIP-,
, 9- . , .
 .



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

 Ora le 10g  . 592

592

8. Forms Builder

.  Ora le ,
 .
: , ,  .
 , .
, ,
.
 . LOV . 

.
. , Manual,
.
, ,
.  , .
. ,

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


JInitiator
OC4J





 Ora le 10g  . 593

593






( )
(list of values  LOV)




()

1.
.
2.  .
3.  ,
.
4. .
5.  Ora le
.

1. ____ .
2. ,
____ .

SELECT

3. , ,
, ____
Text Item Radio Group.
4. (1/31/2006)
 ____.
5.  ,
____.

 Ora le 10g  . 594

594

8. Forms Builder

1.  Ora le ?
)
)
)
) JInitiator
)
2. 
(, Customer)?
) , .
) 
) 
) LOV
) .
3.   .
) , , .
)  , , .
) , , .
) , , .
)  , , .
4.  .
)  ,  ,
.
) ,
, .
)  ,
, , .
)  ,
, .
)  ,
, .

 Ora le 10g  . 595

595

5.  ,
, :
) .
) Query
SELECT.

Data Sour e Name -

)
) ,
.
)   .

1. Readwood Realty

Redwood ,
.  .
, , , , . ,
. , ,
Listings,
Properties. ,
CustAgentList. . ,
, .
1. Forms Builder .
,
Listings.
2. . ,
.
3. CustAgentList. , ListingID. ,  .
ORDER BY CONTACTDATE. 
ListingA tivity.
4. Listings  Query Re ord Sour e Name ,
 .
, SQL*Plus.

 Ora le 10g  . 596

596

8. Forms Builder

(SELECT ListingID, Listings.PropertyID, ListingAgentID,


SaleStatusID, BeginListDate, EndListDate, AskingPri e,
HousePhoto, Bedrooms, Bathrooms, Stories, SQFT, YearBuilt
FROM Listings INNER JOIN Properties
ON Listings.PropertyID=Properties.PropertyID)
5. Query Data Sour e Columns BEDROOMS, BATHROOMS,
RIES, SQFT YEARBUILT. Number.

STO-

6. . : Name, Enabled (No), Database Item (Yes), Column Name, Query


Only (Yes), Prompt.
7.  Listings LOV Property ID.
LOV. Properties PropertyID, Address, City, Bedrooms,
Bathrooms YearBuilt. . , Return PropertyID
Listings.PropertyID. LOV Re ord Group.Save
.
8. AgentID  Listings.
CustomerID . ,
LOV . AgentID 
List of Values LOV_AGENT.
9.  . , 1990 Year Built.
A tionPrint,  . , ,  ListingA tivity.fmb ,
.
2. Coffee Mer hant

,  Coffee Mer hant. ,


,  ,
. , , (States
Countries)  , . . -
, .
Employee . ,
. ,  Employee
. Consumers Inventory. Orders
OrderLines. - 

 Ora le 10g  . 597

597

 OrderID.
.
1. , 

BuildCoffe.sql.

2. . Forms Builder 
States Countries. ,  , , ,
.
3. , , .  Employees Consumers . ,
, . ,  .
4. Inventory
, , . 
, 
, .
 . ,
 Inventory : ,   . ,
CountryID.
5.  Orders/OrderLines, ,
Orders.
ConsumerID EmployeeID.
6. ,
 OrderLines.  Orders.
7. Orders Query Re ord Sour e
Name. ,  .  Orders ,
.
8. OrderLines Query Re ord
Sour e Name. Name Pri e
Inventory.  
, .
9.  Orders . ,
.

 Ora le 10g  . 598

598

8. Forms Builder

3. Rowing Ventures

,
Rowing Ventures. , . 
Person Organization.  . , , Boat BoatCrew
 . ,
. LOV
. , PersonID, .  Ra eCategory .
, ,
.
 . -
.
, ,
, 
. 
Ra e Ra eTimes.  BoatID LOV. BowNumber Organization
. .
, Boat Organization  LOV .
4. Broad loth Clothing

Broad loth Clothing . , ,


, .
 .  Customer, CustomerOrder, OrderItem, Item Model. 
PrimaryLanguage ColorList. Item Model
, .
, Model , Item 
. , , ,
. , , .

 Ora le 10g  . 599



. . .

 
, 




, Ora le, .


, . 
 .  - , ,
 . 
, . 
.
 Ora le, ,
.
, , Redwood Realty ,
, .

. ,

 Ora le 10g  . 600

600

9. 

Bedrooms

2 3 4 5 or more

Bathrooms

1 1.5 2 2.5 3 4 or more

Sq Feet

1000 2000 3000 4000 or more

Age/Built

1900 1970 1980 1990 2000 or later

Price

Min:

Sale Status

For sale Pending

Max:
Sold
Matches

. 9.1. 

,
, . Forms
Builder, ,
.
 . 
. ; 
. 
. , ,
. 9.1.
 , 
,  Ora le.
,  , , . . 9.2
: , . 8, .
.
, . , , .
,  ,

 Ora le 10g  . 601

 454

601

1
1
2
3

1
1
2
3

. 9.2.  Ora le

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

 Ora le 10g  . 602

602

9. 

  Ora le. :
, ,
. ,  . ,
, .
,
,
.
 Ora le , , , .
.
. 
, , ,
. .

 Redwood Realty
.
1. Forms Builder . Sear h.
2.  Windows Window1 Sear h,
Redwood Realty.
3.  , Canvases
Add ( +).
Sear h_Canvas. , ,
.  .
4. , . Data Blo ks Add.
, , Build a new data blo k manually.
, , Name Sear h_Blo k,
Database Data Blo k  No.

.  , . ,
.

 Ora le 10g  . 603

 454

603

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

( , ,
 , ).  .
.
1. . ,
FRM_BEDROOMS. , , Minimum Bedrooms.
2.  .
, ,
.
RADIO_BEDROOMS.
3. : Name=RAD_BED
_2, Label=2, Radio Button Value=2 Width=20.
4. , , ,
2 3.
,  , 2, 3, 4 5
. . ,
.
5. , LayoutAlign Components,
.
6. . Initial Value=3.
,
,  . , ,
, . -

 Ora le 10g  . 604

604

9. 

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

TXT_MinPri e.
2. : Data Type=Number, Initial Value=0,
Lowest Allowed Value=0, Highest Allowed Value=10000000, Prompt=
Minimum Pri e, Prompt Atta hment Offset=7.
3. TXT_MaxPri e.
1.

Sale Status . SaleStatus .


. , 101 , . .
,
. 101/For Sale.
, ,
. , 
.
.

LIST_SaleStatus.
2. , Elements in List 1.

, .
List Item Value: For Sale  101, Pending  102, Sold  103.
Initial Value , 101.
3. : Prompt=Sale Status, Prompt Atta hment
Offset=7. ,  , (.. ).

 Ora le 10g  . 605

 454

605

. 9.3. , 

 .
  (Sear h.fmb). . 9.3
. ,
, , 
, .

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

CREATE VIEW ListingResults as


SELECT Listings.ListingID,Listings.PropertyID,Listings.SaleStatusID,
Listings.AskingPri e, Listings.EndListDate, Properties.Address,
Properties.City, Properties.State, Properties.Zip ode,

 Ora le 10g  . 606

606

9. 

Properties.Bedrooms, Properties.Bathrooms, Properties.SqFt,


Properties.YearBuilt, Agents.LastName, SaleStatus.SaleStatus
FROM Listings INNER JOIN Properties ON
Listings.PropertyID=Properties.PropertyID
INNER JOIN Agents ON Agents.AgentID=Listings.ListingAgentID
INNER JOIN SaleStatus ON
SaleStatus.SaleStatusID=Listings.SaleStatusID;
,
WHERE. Listings Properties.
, . , 10
. , WHERE ORDER BY. .
, . , : SELECT * FROM ListingResults WHERE rownum<5.
, ,  ,
 ,
.
1.  . ListingResults , SEARCH_BLOCK.   <Tab>. ,
SEARCH_BLOCK . Obje t Navigator.
2. , , : Database Data Blo k=Yes, Query Allowed=Yes Query Data Sour e
Type=Table.
3. Query Data Sour e Name ,
. :

(SELECT ListingID, PropertyID, SaleStatusID, SaleStatus,


AskingPri e, EndListDate, Address, City, State,
Zip ode, Bedrooms, Bathrooms, SqFt, YearBuilt,
LastName FROM ListingResults)
4. Query Data Sour e Columns . , .
5. . : Allowed=No, Update Allowed=No, Delete Allowed=No.
Number of Re ords Displayed=7 Show S rollbar=Yes.

 Ora le 10g  . 607

 454

. 9.4.

607

Query Data Sour e Columns

,
, .
. 9.4 .
, .
, .
 ListingResults
.
1.
ingsResults.

List-

2.   .
: Name=FRM_ListingResults, Layout Data
Blo k=ListingResults, Layout Style=Tabular, Number of Re ords Displayed=7, Show S roll Bar=Yes, Frame Title=Listing Results, Ba kground Color=white.
 , . , 10 . - ,
, ,  .
1.  .
.
2. , (ListingID): Name=ListingID, Data Type=Number, Maximum Length=5,
Database Item=Yes, Column Name= LISTINGID, Width=48, Ba kground
Color=white, Prompt=Listing ID, Prompt Atta hment Edge=Top.

 Ora le 10g  . 608

608

9. 

. 9.5. 

3. ListingID, PropertyID, SaleStatus,


Pri e, Bedrooms, Bathrooms, SQFT, YearBuilt, City Lastname.

Asking-

. 9.5 , . .
,  . ,
PropertyID ListingID, ,
. ,
 , , . 
.

 . , ,
. .  .
, .
. :

 Ora le 10g  . 609

 454

609

 ?
; , ,
.  .  ,
. ,
, . ,
Sear h_Blo k. Sear h,
.
, , .
 ListingResults.
 , , , .
ListingResults. Database WHERE lause.
,
.

(ASKINGPRICE BETWEEN :SEARCH_BLOCK.TXT_MINPRICE AND


:SEARCH_BLOCK.TXT_MAXPRICE)
AND (BEDROOMS >= :SEARCH_BLOCK.RADIO_BEDROOMS)
AND (BATHROOMS >= :SEARCH_BLOCK.RADIO_BATHROOMS)
AND (SQFT >= :SEARCH_BLOCK.RADIO_SQFT)
AND (YEARBUILT >= :SEARCH_BLOCK.RADIO_YEARBUILT)
AND (SALESTATUSID = :SEARCH_BLOCK.LIST_SALESTATUS)
, . .
, . , .
.  , : (AskingPri e BETWEEN :Sear h_Blo k.TXT_MinPri e
AND :Sear h_Blo k.TXT_MaxPri e). -, , , . , 
, . -, . , (Sear h_Blo k),
. (:). ,
, .  ,
, WHERE SQL.

WHERE, , .

 Ora le 10g  . 610

610

9. 

 , ORDER BY. AskingPri e,


, .
,
. WHERE ORDER BY , , , .
  ,
Sear h. WHERE, .
. When-Button-Pressed. Sear h
. .

GO_BLOCK('LISTINGRESULTS');
EXECUTE_QUERY;
(GO_BLOCK)  , ListingResults. , Sear h Sear h_Blo k, ,
ListingResults. ,
. (EXECUTE_QUERY) , ,
ListingResults. 
WHERE.
.
 , .
; , . , (,
20000 ), 
. . 9.6 . ,
.
, . 
, . 
, .
, ,
.
.  WHERE,
 . Sear h
, .
, ,
.

 Ora le 10g  . 611

611

. 9.6.  


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

 Ora le 10g  . 612

612

9. 

, . ,
Redwood Realty ? SQL- .
. ,
, . SQL- ,
, , .  ,
.
. 
. , , , .
.
, , . , , , , . -,
PL/SQL. , 
, Ora le. , ,
.
, .
, 

,  . , , , , ,
.. ,
, .   .
 Ora le, .  . ;
 .
(, ), .
, .
, ,
, ,
. , ,
, .

 Ora le 10g  . 613

1.Pre-Logon
2.On-Logon
3.Post-Logon
4.Pre-Form

613

5.When-Create-Record
6.Pre-Block
7.Pre-Record

8.Pre-Text-Item
9.When-New-Form-Instance
10.When-New-Block-Instance
11.When-New-Record-Instance
12.When-New-Item-Instance
1.Post-Text-Item
2.Post-Record
3.Post-Block

4.Post-Form
5.On-Rollback
6.Pre-Logout
7.On-Logout
8.Post-Logout

. 9.7.

, ,
. . 9.7,
,
. Form Builder
, . , , .
 PRE, ,
 POST , , . , PRE-FORM , 
, POST-FORM  ,  . . 9.7
.
 Ora le  Alert.  ,
 .
 . , .
, , .  .

 Ora le 10g  . 614

614

9. 

.
, ,
, Alert. . ,
.
,
 Agents  Alert.
1. , 
Agents.
2. Alerts Add (
+).
3. : Name=ALERT_
GERS, Title=Trigger Fired, Message=test, Alert Style=Note.

TRIG-

4. , .

DECLARE
btn NUMBER;
BEGIN
SET_ALERT_PROPERTY('ALERT_TRIGGER',ALERT_MESSAGE_TEXT,
'Pre-Form trigger event');
btn := SHOW_ALERT('ALERT_TRIGGER');
END;
5.  . ,
.
Show_Alert , ,
. ,
, .
, . . 9.8 ,
. ,  Post-Item
. ,

Agent ID. , , ,
. , . Post-Text-Item,
, :
, , .
, Ora le When-Validate-Item.
, -

 Ora le 10g  . 615

. 9.8.

615

Post-Item

, . ,
, Alert
, .
 ,
, . ,
.
.

 Ora le 10g  . 616

616

9. 

, , . .
Triggers, , Smart Triggers,
. .
,
.
, .
,  Ora le, ,
, ,
. , , (Update, Insert Delete), .
 :

DECLARE
--
BEGIN
-- PL/SQL
-- ( 
EXCEPTION
-- ,
END;
,
. ,
. ,  .
.
, , PL/SQL.
. , , (IF/ELSE/END), (WHILE ... LOOP FOR ... LOOP). ,
, ,
, ,
,  . . 9.9
PL/SQL, .
,
:=. , , SELECT ... INTO . 

 Ora le 10g  . 617

617

DECLARE
tax NUMBER (9,2); price NUMBER(9,2);
AvgSalary NUMBER(10,3);
tax_rate CONSTANT NUMBER := 0.07;
BEGIN
tax := price * tax_rate;
SELECT Avg(Salary) INTO AvgSalary FROM Employee;
IF (condition)
--statements
ELSE
--statements
END IF;
WHILE (j < 10)
LOOP
j := j + 1;
END LOOP;
EXCEPTION
WHEN (error event) THEN ...
END;

. 9.9. PL/SQL

DML (Insert, Update Delete). , , :Sear h_blo k.Txt_MinPri e.


DML SELECT INTO  .
,
.
 Redwood Realty. 
SaleStatus. , (For Sale,
Pending Sold) .  ,
, ,
. , ,
, 
. , ,
, , . , , , ?
 (
SaleStatusID),  . - , , .
,  .
, .

 Ora le 10g  . 618

618

9. 

, .
, , Pre-Form. ,
, . , .
, , .
1.

SaleStatus.

2. .
3.  .
,
PL/SQL. , . ,
ADD_LIST_ELEMENT.

DECLARE
CURSOR Status IS
SELECT SaleStatusID, SaleStatus
FROM SaleStatus
ORDER BY SaleStatusID;
BEGIN
CLEAR_LIST('LIST_SALESTATUS');
FOR stats IN Status
LOOP
IF (stats.SaleStatusID <> 101) THEN
ADD_LIST_ELEMENT('LIST_SALESTATUS',99,
stats.SaleStatus, TO_CHAR(stats.SaleStatusID));
END IF;
END LOOP;
END;

SELECT. , ORDER BY, -

. . , .
, .
ADD_LIST_ELEMENT  . . ,
( 99 ), ,
, , , (
 TO_CHAR).

101/For Sale. , ,

 Ora le 10g  . 619

619

. , . 9.12, ,
. Clear_List
. , , , IF; , .
,
, ,  SET_ITEM_PROPERTY.  Ora le,
, ,   POPULATE_GROUP_WITH_QUERY,
  POPULATE_LIST, . , ,
, ,
.
,
. . . ,
. , ,
.
, , , .
, , 
. ,  ?
. 9.10,  .  Built-in Pa kages, STANDARD Extensions. ,
, .
. 9.11 ,  ,
. Help <Ctrl+H>.
. Open, . ,
 ,
. ,
, .

, . ,
.  Ora le.

 Ora le 10g  . 620

620

9. 

. 9.10.   Ora le

1. .
2. , 
.
3.  .
Ora le 10g , .
, . ,
. .
, . , ,
.
, .

 Ora le 10g  . 621

621

. 9.11. 

1. 

Sear h Pre-Form.

2. , .
,  Clear_List.
3. , , .
4.  .
, Forms Bbuilder.
,
. ,
.
, , , .
. 9.12 , .
, . ,
, .
 , . ,
.

 Ora le 10g  . 622

622

9. 

. 9.12. , 

, (
) Forms Builder,
. . ,
, , .
, .
.
Wat h . ,
, .
, , , .
. Windows XP ,
Forms Builder, Windows Control Panel,
.

 Ora le 10g  . 623

623

,
. ,
. , .
,  Sear h 
When-Button-Pressed Sear h. Atta h.Debug; ( )
(Go_Blo k) . ,
. Sear h, .  DebugAtta h
. 
, , ,
. .
, ,
Web- Ora le.


, .
, , .
 (runtime error).
, , , , , ,
, . ,
, .
,  Sear h.
(BTN_TEST) . ,
.

DECLARE
i NUMBER;
j NUMBER;
BEGIN
i := 0;
j := 10/i;
END;
 , .
. 9.13 , . ,

 Ora le 10g  . 624

624

9. 

. 9.13. ,

. , .
. , .
.
, .

DECLARE
i NUMBER;
j NUMBER;
BEGIN
i := 0;
j := 10/i;
EXCEPTION
WHEN OTHERS THEN Message(sqlerrm);
END;

 Ora le 10g  . 625

625

EXCEPTION END.
,  . , .
:

EXCEPTION
WHEN ZERO_DIVIDE THEN j:=0;
WHEN OTHERS THEN Message(sqlerrm);
PL/SQL , . WHEN OTHERS, .
,
. , , .
. PL/SQL
, . BEGIN/EXCEPTION/END
, .
.
Test , EXCEPTION,
, Pre-Form.
 EXCEPTION ,
. , ? , , . 
. ON_ERROR .  Sear h  Agents.
1. 

OnError.

2. , : Message('Error Code
is: '|| ERROR_CODE||' DBMS Error Code is: '||DBMS_ERROR_CODE);.
3.  , .
4. A  .
. 9.14 ,
. , . , , . .

IF (ERROR_CODE = 50016) THEN


Message('You must enter numbers only, not letters.');

 Ora le 10g  . 626

626

9. 

. 9.14. , 

ELSE
Message('Error Code is: ' | | ERROR_CODE
| | ' DBMS Error Code is: ' |
| DBMS_ERROR_CODE);
END IF;
. ,  , . ,
DBMS_ERROR_CODE .
. , DBMS_ERROR_CODE = -1400 ,
.

 Ora le 10g  . 627

627

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


. ,
. 
. :
, . 
.  :

DECLARE
i1 NUMBER;
BEGIN
i1 := 5;
IF (i1 > 3) THEN
DECLARE
i2 NUMBER;
BEGIN
i2 := i1 + 10;
END;
END IF;
END;
 .
. (i1 i2). , i1 ,
. . ( END),
. , , ,
. (i2) ,
IF. . END IF, ,
.

 Ora le 10g  . 628

628

9. 

, , ,
. , , , .
 Sear h.

Pre-Text-Item Txt_MinPri e.
:GLOBAL.MinPri e := :TXT_MINPRICE;

1.

Post-Text-Item Txt_MinPri e.
IF (:GLOBAL.MINPRICE <> :TXT_MINPRICE) THEN
Message('Minimum pri e has been hanged.');
END IF;

2.

3.  0 50000.
,  ,
.
, , , ,
Pre-Text-Item, , . , ,
Post-Text-Item , . , (0) ,
, . , .
,
.
 . ,
.
, , , , ,
. ,
, . ,
.
,
. .
 GLOBAL (GLOBAL.), .

 Ora le 10g  . 629

629


, 
, .
. ,
, , , .
,
. ;
,
.
.

, 
.
,
 . , Agents AgentID, . :
, ? ,
,  , , . - 
.
, ,
. Ora le .  , Ora le
. : , , -, ,
, . . .
Insert ,
. Ora le 
. .
, .
. ,

 Ora le 10g  . 630

630

9. 

. , SQL*Plus.
1. , .

SELECT Max(AgentID) FROM Agents;


2. 15521,
. 15522 ,
20000.

CREATE SEQUENCE seq_Agents


INCREMENT BY 1
START WITH 20000;
3. SQL, , . , , .

SELECT seq_Agents.NEXTVAL FROM dual;


20000. 20001 .. , .
.
 .
,
. , .
, ,  , 
. Forms Builder  Agents.
, 8,
 Agents.fmb Agents.fmx ,
, .  , , .
, ,
. , On-Insert,
. , ,
Insert Re ord .
When-Create-Re ord. On-Insert
, Save.

When-Create-Re ord.
2. FileConne t,
1.
.

 Ora le 10g  . 631

631

Agents.
SELECT seq_Agents.NEXTVAL INTO :AGENTS.AGENTID FROM dual;

3. ,

4. Compile, , .
. ,
, , .

5.  ,  .
. 9.15  Agents. , When-Create-Re ord
. ,  , - . , Exe ute Query
 .  . ,
. , , ,
, . When-New-Form-Instan e Exe ute_Query.
,
- .
, , . 9.7. , When-Create-Re ord
Pre-Form When-New-Form-Instan e.
,  .

Pre-Form :GLOBAL.DoNotAdd := 1;.


2. When-New-Form-Instan e
:GLOBAL.DoNotAdd :=0;.
3. When-Create-Re ord IF, , : IF
(:GLOBAL.DoNotAdd = 0) THEN ... .
1.

.  , , .
.

 Ora le 10g  . 632

632

9. 

. 9.15.

AgentID


: 
. , . 
  ,
.
,
 Ora le. .  Agents. , , ,

 Ora le 10g  . 633

633

Gender .
. , . , ,
, .
Li ense Status.
LOV, . , , .
, , Li enseStatus .
.
. ,
, , ,
, .
1.
Date.

HireDate. ,

2. Yes Required, Lowest Allowed


Value 01-Jan-1990, Highest Allowed Value 30-De -2050.
3.  HireDate 1980 . 
 .
4. HireDate
: $$DATE$$. 
, .
.
. 9.16 ,
. , Format Mask,
, .  (Format Mask)
. , ,
 , Format Mask, FM"("999") "999"9
-"9999. , - ( )
.  .
 ,  .
 , .  , , .

 Ora le 10g  . 634

634

9. 

. 9.16. ,

. , , , ,
, .
 .
, , .
,  . ,
- , , , ,
, .
 PL/SQL, .  Ora le When-Validate-Item.
1.
When-Validate-Item.

Birth Date

2. , , .

 Ora le 10g  . 635

635

IF (ADD_MONTHS(:BIRTHDATE, 18*12) > CURRENT_DATE) THEN


Message('The person must be at least 18 years old.');
RAISE FORM_TRIGGER_FAILURE;
END IF;
3.  , Birth Date.
. 9.17 , , 
. , , , 18 . (FORM_TRIGGER_FAILURE),
. ,
:
. PL/SQL,
. IF/THEN. , ,
-, , . :
, ,
. , .

, , . , 
, , .  , . Redwood
Realty  .  Sear h .
, .   (Count
Sum).
,
,
. , ,
,  . , , , ,
.
1.  Sear h ListingResults.

.

 Ora le 10g  . 636

636

9. 

. 9.17. ,

2. , . 9.1.
3. ListingResults. Query
All Re ords Yes.
4. Sear h_Blo k
 .
5. , . 9.2.
6.  .

 Ora le 10g  . 637

9.1.

Name
Enable
Keyboard Navigable
Data Type
Cal ulation Mode
Summary Fun tion
Summarized Blo k
Summarized Item
Database Item
Visible
Width
Height
Prompt
9.2.

637

Mat hCount
No
No
Number
Summary
Count
LISTINGRESULTS
LISTINGID
No
No
5
1
Count

Name
Enabled
Justifi ation
Data Type
Format Mask
Cal ulation Mode
Formula
Database Item
Prompt

Mat hCount
No
Right
Number
9,990
Formula
:LISTINGRESULTS.Mat hCount
No
Number of Mat hes

. , 
, , ,
.

. , Enabled No.
. 9.18 . ,
.

 Ora le 10g  . 638

638

9. 

. 9.18. 

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

 Ora le 10g  . 639

639


, , ,
. ,  . 
. , .
, .   ,
.
, , .
Ora le . . 
. : ,
. , .
 .
 , .
.

,  .  Sear h.
 , .
1. 
Sear h.fmx.

Windows

Sear h.fmb

Sear h2.fmb Sear h2.fmx.


3.  Sear h2.fmb , Test S ope,
. Txt_MinPri e.
4.  Sear h2 , Sear h3
Sear h4, .
2.

Sear h PropertyID ,
, . , , 
. -

 Ora le 10g  . 640

640

9. 

, .
, . ,
.
1. , Sear h_Properties Create (+). Properties
, , , Sear h_Canvas. , ,
; Sear h_Canvas.
2. , .
Properties . , :

Properties.PropertyID= ListingResults.PropertyID.
. , ,
,  Auto Join .

3.
Properties. ,
. , ,
.
4. , . Sear h_Canvas 
. Property. WhenButton-Pressed, :
GO_ITEM('PROPERTIES.PROPERTYID');
, , .
, PropertyID. WhenMouse-DoubleCli k. ,
Property, PropertyID
.
5. Properties ,
Sear h. Sear h.
When-Button-Pressed,
:
GO_ITEM('SEARCH_BLOCK.BTN_SEARCH');
6. .

 Ora le 10g  . 641

641

. 9.19.

. 9.19  .
,  . Sear h, , .
, , . Property.
, , PropertyID.
Sear h, Sear h
. ,
PropertyID, Property,  . ,
, Properties ListingResults.
, .

 , .
? , -

 Ora le 10g  . 642

642

9. 

, ,  ?
, .
.
, .  .
. , , ,
.
, , ,
.
1. ,  Forms Builder.  Sear h. ,
,  Sear h3.fmb.
2. Sta ked Canvas, .
 , ListingID PropertyID.
. 9.20 ,
. Properties.
3. Properties. : Properties.PropertyID=ListingResults.
PropertyID.
4. , Properties.
, ,
.
. , .
 .
5. ,  . Properties ,
,
.
6. Sear h_Blo k  , : Name=CHK_PROPERTY, Label=Property, Value when Che ked=1,
Value when Un he ked=0, Data Type=Number, Initial Value=0, Database
Item=No.
7. ,  Properties , Pre-Form.
( EXCEPTION) :

HIDE_VIEW('PROPERTIES');

 Ora le 10g  . 643

643


Stacked Canvas

. 9.20. ,

When-Che kbox-Changed  .
IF (:CHK_PROPERTY > 0) THEN
SHOW_VIEW('PROPERTIES');
ELSE
HIDE_VIEW('PROPERTIES');
END IF;

8.

9.  
.

Properties, -

. 9.21 . Property , .
, .
, .
, . , ,
Properties ,
 .

 Ora le 10g  . 644

644

9. 

. 9.21.

 . , Windows , .
 . .
, ,
.
. .

 Ora le 10g  . 645

645

- , . .
 Sear h , , .
Sear h_Canvas.
1.  Sear h. 
Sear h4.fmb, .
2. , Canvas Create. : Name=MainTab, Canvas
Type=Tab, Ba kground=white.
3. (). .
Name=PG_SEARCH, Label=Sear h,
 Name=PG_PROPERTY, Label=Property.
. ,
( ) .

4.  Sear h_Canvas
MainTab. , Sear h_Canvas/Graphi s MainTab/PG_Sear h/
Graphi s. .
5. .
. ,
( ,
<Shift>, ). Canvas
Page Canvas MainTab, Page  PG_Sear h.
ListingResults
.  , 
, .
. 9.22 , .
6. ,
, Sear h_Canvas.
, Sear h_Canvas,
, .
7. , MainTab
Sear h_Canvas , , . 

 Ora le 10g  . 646

646

9. 

. ,
 Sear h_Canvas.
8. . , Properties. Property , ,
. , Properties
.

Properties.PropertyID= ListingResults.PropertyID.
9. , MainTab PG_Property.
10.  ,
, . , , Property
PropertyID.
When-Mouse-DoubleCli k PropertyID.
GO_ITEM('PROPERTIES.PROPERTYID ');
. Property, ,
OwnerID. ,
 . , ,
. , .
1. MainTab.
PG_PROPERTY MainTab/Tab Pages. Create, . : Name=PG_OWNER,
label=Owner .
2. ,
Customers. Properties.

Customers.CustomerID= Properties.OwnerID.

3. ,

PG_OWNER.

4. , , OwnerID Property.
GO_ITEM('CUSTOMERS.CUSTOMERID');
. 9.23  . , . PropertyID
Property,  .

 Ora le 10g  . 647

647

Canvas Page

. 9.22.

Canvas Page

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


: ,
. , : ,

 Ora le 10g  . 648

648

9. 

. 9.23. ,

Property Owner

, 
OPEN_FORM. , . ,
. ,
.
 , , .
.  .
. .

 Ora le 10g  . 649

Web- JDeveloper

649

, . , , ,
, , .
, . . .
, .
,  Property ( Owner) .
. ,
  . UPDATE, INSERT DELETE.
 . ,
. .
, ,
.
. 
, .
.
,
.  ,
.  , (
).

Web- JDeveloper
, , , . , 
, . , , , ,
Java JInitiator.  Web ,
. Ora le ,
Web-. , -

 Ora le 10g  . 650

650

9. 

. Java-
HTML-,
.
Java 2 Enterprise Edition (J2EE) .
J2EE  , . Ora le ,
(appli ation development framework  ADF). JDeveloper,
, , .
JDeveloper. ,
Ora le. JDeveloper ,
Web- OTN. Zip- , JDeveloper OTN.
1. OTN JDeveloper.
2.   . .
3.  (/jdev/bin/jdevw.exe),
, Send ToDesktop, .
 

 JDeveloper  . 
. , , ,
, . .
 ,  , , ,
.  , .
,  . , JDeveloper.
, Web-,
 . 
.
.
1. , JDeveloper,
(C:\Temp).

 Ora le 10g  . 651

Web- JDeveloper

651

2.  HousePhoto.jpg, 
, .
3. SQL*Plus Listings ,
 .

ALTER TABLE Listings


ADD PhotoFile NVARCHAR2(250);
4. SQL*Plus,  ;
, .

UPDATE Listings
SET PhotoFile = 'C:\Temp\HousePhoto.jpg'
WHERE ListingID=14979;
Web-,  . ,
. JDeveloper,
. Web- : Appli ation Workspa e;
; Web-, .
, JDeveloper . .
. JDeveloper
. (Appli ation Navigator). .
.
,
. , . .
,
,
. , ,
View .


, . , , ,
 . ,
, , -

 Ora le 10g  . 652

652

9. 

Ora le Net Manager


.
1.  JDeveloper, Conne tions Appli ation Navigator.
2. Database New
Conne tion.

Database

3. , ,
Next.
4. . 9.24 JDeveloper . Conne tion Name Redwood.
5. Next .
6.  Deploy Password Next.
, , ,
. , , ,
, .
,  , .
.
1. , , .
2. SID ,
.
3. Next, Test Conne tion.
4. , Next,
Finish, .
.
, .
, .
,  . jdev\mywork, ,
JDeveloper.
5. Appli ations Appli ation Navigator.
6. Appli ations New
ation Workspa e.
7.

RedwoodRealty.

Appli-

 Ora le 10g  . 653

Web- JDeveloper

653

. 9.24. JDeveloper

Web Appli ation [Default Appli ation


Template OK.

8.

, Appli ation Navigator Model ViewController.


.
-

, , Ora le
Apa he Jakarta.
 (MVC  model-view- ontroller). . 
. Java-, ,
,
.  .
, .

 Ora le 10g  . 654

654

9. 

. 9.25.

.
1.
New.

Model

2. Business Tier Business Components. Items Business Components from Tables. OK .


3. , RedwoodRealty
OK. ,
Next.
, .
. 9.25.
, .
1. , Listings >, Sele ted, . Properties
Next.
2.  , . Next.
3.

Listings Properties,

 Ora le 10g  . 655

Web- JDeveloper

655

,
. Next.
4. RedwoodRealtyAM
Next. Finish.
, , . .
,
, .
, ,
, .
, , .
,  , .
.
.
Listings Properties,
. Bedrooms,
Bathrooms SQFT Properties.  ListingsView .
1.

ListingsView.

2. Entity Obje ts, Listings. >, Sele ted.


Properties.
3.  Attributes Housephoto.
Control Hints. Display Hint Hide.
4. Query, SQL- . Housephoto.
5. ( ) Properties Listings Properties. , INNER JOIN.

SELECT Listings.LISTINGID,
Listings.PROPERTYID,
Listings.LISTINGAGENTID,
Listings.SALESTATUSID,
Listings.BEGINLISTDATE,
Listings.ENDLISTDATE,
Listings.ASKINGPRICE,

 Ora le 10g  . 656

656

9. 

. 9.26.

Properties

Listings.PHOTOFILE,
Properties.BEDROOMS,
Properties.BATHROOMS,
Properties.SQFT,
Properties.YEARBUILT
FROM LISTINGS Listings, PROPERTIES Properties
WHERE Listings.PROPERTYID = Properties.PROPERTYID
6. Order By LISTINGID . Test, .
7. Attributes. Properties,
, Bedrooms, Bathrooms, SQFT YearBuilt. Sele ted, . OK,
.
. 9.26 Attribute, Properties, .
, ,
. , : Entity Obje ts
; Query,
SELECT; Attributes
.

 Ora le 10g  . 657

Web- JDeveloper

657

JSP

Web-
. ,
HTML-
. HTML,
, . .

ViewController New.
Web Tier JavaServer Pages (JSP),
JSP Page Items. OK.
ListingProperty.jsp
OK.
Property Listing. ,
, Heading 1.
Components ,
. CSS.
JDeveloper .

1.
2.
3.
4.

5.

CSS. ,
. 
.
6. , .
HTML Form.
7.  : Listing ID, Asking Pri e, Bedrooms, Bathrooms, SQFT Year Built.
 .
8. Data Controls.  RedwoodRealtyAMDataControl, ListingsView1. Listingid  .
. 
Photofile.
9.  Operations, ListingsView1. Previous . ,
Next.

 Ora le 10g  . 658

658

9. 

HTML

. 9.27.  

10. , ,
 . ,
Paragraph, Heading 1.
. 9.27 .
.
11. , ViewController Run. Next, .
,  ,
 .
. ,  . ,  ,
. <if>, JSTL.
HTML- <img> .

 Ora le 10g  . 659

Web- JDeveloper

659

. 9.28. 

1. . Sour e , . ,
Photofilename. :

< :if test="${bindings.Photofile != ''}"


<img sr =' :out value="${bindings.Photofile}"/>' height="100px'>
</ :if>
2.  . . 9.28 , . ,
. ,
Photofile , .
, , HTML. ,  .
 .
 .

 Ora le 10g  . 660

660

9. 

, . ,
.
, ,
Ora le.
.
  , Where , . ,
.  
.
 . Ora le
. , , WHERE, .
,
.
SQL-. ,
.
. ,
, (, )
. ,
. , SQL-
, , , .
1. Appli ation Navigator RedwoodRealtyAM,
Stru ture  RedwoodRealtyAMImpl.java,
.
2.  
:

publi void setConditions (String sLow, String sHigh,


String nBedrooms, String nBathrooms)
{
ora le.jbo.ViewObje t LVO = findViewObje t("ListingsView1");
String sWhereClause = "SALESTATUSID=101"
+ " AND (BATHROOMS >= " + nBathrooms + ")"
+ " AND (BEDROOMS >= " + nBedrooms + ")";
sWhereClause += " AND (ASKINGPRICE >= " + sLow + ")"
+ " AND (ASKINGPRICE < " + sHigh + ")";

 Ora le 10g  . 661

Web- JDeveloper

661

LVO.setWhereClause(sWhereClause);
LVO.exe uteQuery();

3. RedwoodRealtyAM ,
. Client Interfa e. setConditions >,
Sele ted. OK.
, . JDeveloper ,
. Struts ; ,
Struts Page Flow Diagram.
1. ViewController Open Struts
Page Flow Diagram. Data A tion Component
/ListingProperty.
/setConditions.
2. , , Data Controls.  RedwoodRealtyAMDataControl, Operations.
setConditions Data
A tion.
3. Components, .
Forward. /setConditions,
/ListingProperty.
. 9.29 , /setConditions
/ListingProperty. WHERE
, ListingProperty
.
, -  setConditions. 
, ,
.
1. struts /ListingProperty,
.
2. , . Components. Struts
HTML ( HTML!).

 Ora le 10g  . 662

662

9. 

. 9.29. Struts Page Flow Diagram

3.  JSP- setConditions.do .
OK.
4. 
rooms.

Low Pri e, High Pri e, Bedrooms Bath-

5. HTML. , ,
, Low Pri e. Property Inspe tor
LowPri e , 0.
HighPri e 250000.
6. Radio Button, Bedrooms  1. nBeds 1. ,  1, 2, 3, 4 5 or more. ,
, , . Che ked
true.

 Ora le 10g  . 663

Web- JDeveloper

663

. 9.30. 

7. Bathrooms , ;
nBaths.  Submit
Sear h Value.
8. Sour e
<form>. , :

<form a tion="ListingProperty.do">
. 9.30 . - .
,
, . , . 
. .
,
.

 Ora le 10g  . 664

664

9. 

 
, .
1. , .
2. Stru ture, ,
A tion Mappings, /setConditions.
, : paramNames[0.
3. ( )
value ${param.LowPri e}. ,
,  .
${param.HighPri e}, 
${param.nBeds}  ${param.nBaths}.
5. DiagramRefresh
Diagram from All Pages.
4.

. 9.31, ,  /ListingProperty ( )  /setConditions. , /ListingProperty .


Next Previous, ListingProperty
. .

1.
Run.

ListingProperty

2. , 100000 130000,
Query.
3. Next, ,
.
. 9.32  . 
, . ,
, , .
,
.
. ,  ,
 .
. , ,  .

 Ora le 10g  . 665

665

. 9.31. JSP-


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

 Ora le 10g  . 666

666

9. 

. 9.32. 

Ora le , .
, ,
. Pre-Form, When-Button-Pressed
When-Validate-Item. .
, , , .
, .  , ,
. ,
, EXCEPTION. 
On-Error , . .
.

, , 
. -

 Ora le 10g  . 667

667

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

alert
EXCEPTION





GLOBAL

 Ora le 10g  . 668

668

9. 

( )


WHERE

1. alert  , .
2. 
.
3. , When-Button-Pressed, Pre-Form.
4. .
5. .

1. , , , _____,
.
2.  _____
,  .
3. ,
_____.
4. _____,
, .
5. , , ,
_____.

1. , , PL/SQL, :
) EXCEPTION.

 Ora le 10g  . 669

669

On-Error.
) DECLARE.
)  GLOBAL.
) Pre-Form.
)

2. ,
, :
) GLOBAL.oldValue Pre-Text-Item.

.oldValue Post-Text-Item.
) Sequen e.NEXTVAL Pre-Text-Item.
) GO_ITEM('GLOBAL') Pre-Text-Item.
) Post-Text-Item.
)

3. 
?
) GO_ITEM
) SHOW_ALERT
) ADD_MONTHS
) ADD_LIST_ELEMENT
) SHOW_VIEW
4.  :
) Pre-Form, On-Logon, When-New-Item-Instan e, Pre-Re ord.

When-New-Form-Instan e, Pre-Blo k, When-New-Blo k-Instan e, PostLogon.


) Pre-Logon, Pre-Form, Pre-Blo k, When-New-Blo k-Instan e.
) Pre-Form, Pre-Re ord, Pre-Text-Item, Pre-Blo k, Post-Form.
) When-New-Item-Instan e, When-New-Re ord-Instan e, When-New-Blo kInstan e.

5.  ?
) , .
)  .
)  .
) , .
)  .

 Ora le 10g  . 670

670

9. 

1. Readwood Realty

.
, , .
, , (
, , ) , .  .
 ,  Sear h,
.
1. , ,
(Sear h_Canvas) (Sear h_Blo k). Beginning Date, Ending Date Listing Agent.
, 01-Jan-2006 31- De -2006.
Sale Status (For Sale = 101, Pending = 102, Sold =
103). LOV .

SELECT AGENTS.AGENTID, AGENTS.LASTNAME,


AGENTS.FIRSTNAME FROM AGENTS ORDER BY
AGENTS.LASTNAME, AGENTS.FIRSTNAME
2. SQL PLUS .

CREATE VIEW AgentListings AS SELECT


ListingID, Listings.PropertyID,
ListingAgentID, SaleStatusID,
BeginListDate, EndListDate, AskingPri e,
City FROM Properties INNER JOIN Listings ON
Properties.PropertyID =
Listings.PropertyID;
3. (Listings), .
, : Query All Re ords=Yes,

Query Data Name=


(SELECT ListingID, PropertyID,
ListingAgentID, SaleStatusID,
BeginListDate, EndListDate, City,
AskingPri e FROM AgentListings)
WHERE lause =
(BeginListDate <= :SEARCH_BLOCK.TXT_ENDDATE
AND EndListDate >= :SEARCH_BLOCK.TXT_
BEGINDATE) AND (ListingAgentID =
:SEARCH_BLOCK.TXT_AGENTID) AND
(SaleStatusID =
:SEARCH_BLOCK.LIST_SALESTATUS)
ORDER By = BeginListDate
Query Data Sour e Columns.

 Ora le 10g  . 671

671

4.  ,
Listings. ListingID, PropertyID, BeginListDate, EndListDate AskingPri e.
5. Sear h_Blo k Sear h
GO_BLOCK('LISTINGS'); EXECUTE_QUERY;.
6. , , . , Agent
, .
GO_ITEM('TXT_AGENTID'); LIST_VALUES;.
7. LISTINGS ,
, . 9.23, Prompt
Name=Subtotal, Summary Fun tion=Sum, Summarized Blo k=LISTINGS
Summarized Item=ASKINGPRICE.
8. Sear h_Blo k , Asking Pri e. Name=Subtotal, Justifi ation=Right, Data
Type=Number, Format Mask=$99,999,990, Cal ulation Mode=Formula, Formula=:LISTINGS.Subtotal, Database Item=No, Prompt=Subtotal.
9.  . ,
.
2. Coffee Mer hant

Coffee Mer hant  Order,


 .
, ,
. 
. InventoryID , Inventory.
,
.
1.  Orders.fmb, 8, . , , .  Initial Value=$$DATE$$.
2. . (Main_Canvas)
Inventory.
3. ,
Inventory. , Inventory.InventoryID

 Ora le 10g  . 672

672

9. 

=OrderLines.InventoryID. Inventory. ,
. ,
Inventory_Canvas, , .
4. Pre-Form ,
.   ,
, .

IF (:CHK_INVENTORY > 0) THEN


SHOW_VIEW('INVENTORY');
ELSE
HIDE_VIEW('INVENTORY');
END IF;
5. OrderID, Orders (seq_Orders,
300000) ,
 Orders . , . ,
Pre-Form When-New-Form-Instan e.
6. . . , Name. ItemValue, Cal ulation Mode=Formula Formula = ROUND ((:ORDERLINES.QUANTITY*: ORDERLINES.PRICE) * (1-:ORDERLINES. DISCOUNT),2).
, .
7. , . . , , . 9.23, Name=Subtotal, Summary Fun tion=Sum, Summarized Blo k=ORDERLINES Summarized Item=ITEMVALUE.
8. Orders ,
ItemValue. Name=Subtotal, Justifi ation=Right, Data
Type=Number, Format Mask=FML9G999D00, Cal ulation Mode=Formula, Formula=:ORDERLINES.Subtotal, Database Item=No, Prompt=Subtotal. , Query All Re ords ORDERLINES
Yes.
9.  . ,
.

 Ora le 10g  . 673

673

3. Rowing Ventures

Rowing Ventures .
,
. , ,
.  ,
.
.   Ra eDay. Ra e, Ra eTimes, Boat + BoatCrew, Person Organization.
, 
. Ra e Times . Times. Boat + BoatCrew
, ,
8. Person Organization .  ,
. , , .
 Ra e  , . EXECUTE_QUERY; When-New-Blo k-Instan e
Ra e.
Ra eID.
 , , .
, . 
 .
.
, .
, .
4. Broad loth Clothing

Broad loth Clothing, ,


. 
, .
Shipment ShipmentItem.
 .
.
Item. , , .

 Ora le 10g  . 674

674

9. 

 , .
 . .
 ,
ShipmentID.  Item ,
Item, ModelDes ription Model. Customer , 
. ,
.
, CustomerOrder .
,  .  Shipment Item , OrderID. ,
OrderID, .
. , OrderID , . :

SELECT ALL CUSTOMERORDER.ORDERID,


CUSTOMERORDER.CUSTOMERID,
CUSTOMERORDER.ORDERDATE
FROM CUSTOMERORDER
WHERE CUSTOMERID=:SHIPMENT.CUSTOMERID
ORDER BY CUSTOMERORDER.ORDERDATE
,

OrderID.

BEGIN
SELECT DeliveryAddress,
DeliveryCity,
DeliveryState,
DeliveryPostal,
DeliveryNation
INTO :SHIPMENT.ShipAddress,
:SHIPMENT.ShipCity,
:SHIPMENT.ShipState,
:SHIPMENT.ShipPostal,
:SHIPMENT.ShipNation
FROM CustomerOrder WHERE
OrderID=:SHIPMENTITEM.OrderID;
END;
, , . ,  , .
 , ,
.

 Ora le 10g  . 675


Automati database diagnosti monitor (ADDM). ,  . Enterprise Manager; ADDM
.
COMMIT. , , COMMIT, .
Data Pump. Ora le, .
Developer Suite. ,
-.
DUAL. , Ora le,
. (, ), .
END. , PL/SQL.
Enterprise Manager (EM). Web, .
EXISTS. , .
IN OUT.  , ,
 ,
.
IN.  , 
, 
.
Integer. , .
JInitiator. Java-, Ora le,
Web- .
.
Lightweight dire tory appli ation proto ol (LDAP). ,
.
MetaLink. Ora le. MetaLink
. Enterprise Manager.
NULL. , , .
Ora le Appli ation Server (Ora leAS). ,

 Ora le 10g  . 676

676

 .  .
Ora le Containers for Java (OC4J). Web-,
 . 
OC4J, .
Ora le Internet Dire tory (OID). ,
.
OUT.  ,  .
P-.  .
, PL/SQL.
Se ure so kets layer (SSL). ,  .
SQL A ess Advisor. , SQL- .  ,
.
SQL Tuning Advisor. , SQL-
.
SQL*Loader. Ora le,
,  .CSV.
STANDARD Extensions.  , , .
Unied Modeling Language (UML). ,
.
Very large-s ale database (VLSD). , ,
.
.
.
(Database administrator  DBA). , .
. , PL/SQL
.
. , ,
SQL PL/SQL, .
. , ,
.
. , .

 Ora le 10g  . 677

677

. .
. , , .
, -.
. , 
. .
. , .
. ,   .
. SQL PL/SQL.
. ,  , .
. , .
(virtual private database  VPD). Ora le, . ,
, .
. ,
, NULL.
. , Ora le,
SQL*Loader .  .CSV.
, .
. ,
.
. , , .
. , FROM .
. .
. ,
. , ,
. ,
.
. 
(2NF), , , .
. 
SQL*Plus.

 Ora le 10g  . 678

678

EXCEPTION. ,
, .
 .
GROUP BY. SELECT,
.
HAVING. SELECT,
 .
ORDER BY. SELECT, , .
ROLLBACK. SQL-, , .
WHERE. SELECT,  ,
.
WITH ADMIN OPTION. , SQL GRANT .
. , - PL/SQL.
 . , .
. , , , , 
.
. , SELECT, . .
 (. ).
. , .
 .
. ,
.
. . , ,
,
.
(re overy manager  RMAN). Ora le,
.

 Ora le 10g  . 679

679

. , . 
.
. , , .
. , .
. ,
.
. , .
. . .
, ,
, .
. , .
.
. , .
. , - PL/SQL,
.
. , , ,
.
. INSERT, UPDATE DELETE, .
. ,
.
(*). SELECT ,
.
. (, ..),
.
, "CreditLimit".
. PL/SQL, . .
. ,
(, ,  ).
. ,
.
. , .

 Ora le 10g  . 680

680

 (publi -key infrastru ture  PKI).


.  ,
,  .
. ,
PL/SQL.
(Cas ading style sheet  CSS). , Web-.
. ,
<Alt>, .
. , .
. , , . .
. , .
.
SQL*Plus.  Ora le SQL,  , ,
.
 . , , , ,  .
. PL/SQL , .
. ,
.
. , .
, PL/SQL,
.
- . ,  .
. , ,
.  SELECT.
. ,
.

 Ora le 10g  . 681

681

.  .
. , PL/SQL ,
SELECT.
. ,
, NULL.

(Redundant array of independent


drives  RAID). ,

. .
.
. , .
.
.
. .
.
.   .
. ,
.
. , .
,  .
. .
 . ,
.
.
. SQL
.
. SQL
.
. , .  .
. ,
. . 8.3.
 Ora le.
. ,
.
. .

 Ora le 10g  . 682

682

. 
, .
. , . .
. ,
PL/SQL. , SELECT PL/SQL .
. , .
. ,  ,
.
. , . . , , .
. , , .
. , .
Enterprise Manager.
. , .
. , ,
.
. ,
DML, .
. ,
.
. , INSERT, UPDATE DELETE .
. , , .
.
, .
, . ,
.
. .

, .

 Ora le 10g  . 683

683

. 
, , . .
.  .

.
. , , .
CHECK. , true .
NOT NULL. , NULL.
.  ,
, .
. , ,
, .
.
, .
. ,
, .
 . ,
.

.
.
.
. SQL-
.
. , ; .
. , .
IN. , ,
.
LIKE.
.
. , SELECT .

 Ora le 10g  . 684

684

. ,
  .
. .
. , ,
.
. , .
.
. , , , .
. EXCEPTION.
. , 
 .
DESFORMAT. , 
.
DESTYPE. ,
.
PCTFREE. , . CREATE TABLE , .
PCTUSED. , . CREATE TABLE , Ora le
.
UserID.  <_>/<>
<_>, ,
.
. ,
.  .
. 
, .
. , ,
 .
. ,  .
. , .
 .
. SQL.
.

 Ora le 10g  . 685

685

. SQL*Plus, ,
.
. , PL/SQL.
. , Ora le
. , .
- . .
. . .
. , SQL-, , .
. , . .
. ,  .
. , .
. ,
, , .
. , ,
. , .
. ,
.
, .
. .  , .
. , .
. N- ,
.
. , , NULL.
. .

 Ora le 10g  . 686

686

.
.

. ,
.

. .
, , .
 GLOBAL. .
 ,
. 
.
. , Ora le .
. ,
(SELECT).
. ,
.
. , ,
.
 SQL. .
 .
. PL/SQL, Ora le
.
. , .
. , PL/SQL ,
.
. , SELECT.
. ,
.
. .
. .
. . 
72 , .
 .

.

 Ora le 10g  . 687

687

 . PL/SQL,
DECLARE. ,
.
 .  ,  ,  .
 . , BEGIN END.
 . PL/SQL,
EXCEPTION .
 . ,  .
.
 . , , . 
,
. , ,
, 
.
 . ,
, .
.
. , ,
 .
. .
 . , .
 . ,
.
 . .
 .  ,
.
. ,
.
. Ora le.
. .
. , , .
. , .

 Ora le 10g  . 688

688

 . .

(). , , , , .
. ,
Ora le, , .
. , . , ,
.
. , , , .
. , .
Ora le.  .
. , .
. , , GROUP BY DISTINCT .
. SQL, .
 . Ora le,
,  .
. ,
, .
. SELECT .
. , .
(List of Values  LOV). ,
.
 .
. , FROM
, .
. ,
.
. , .
.  .

 Ora le 10g  . 689

689

. Ora le , .
,
.
. SQL-, .
. , , -.
. , .  , ,
.
Undo. . Rollba k
.
. .   .
. , .
Magi . , ,
.
. , .
. , ;  
.
. SQL,
. , .
. 
(3NF), 2NF
.
. , , .
. , Ora le, , .
. , WHERE.
. WHERE  ,
Ora le .
. ,
; .

 Ora le 10g  . 690

690

. . , 
, .
Redo. ,
.  .
. , .
 .
.
. ,
.
. ,  , .
CSV. ,
. ,
,  .
( ). .
, .
( ). .
,
, .
. ,
.
. ,
.
. , .
. , ( )
.
. . .
,  Customer  Order.
. , .
 .
. , . .
-. , . ,
, , .

 Ora le 10g  . 691

691

. , ,
,
.
. , ,
,
.
,
.
FOR .
FOR. ,
FETCH.
FOR. , .
WHILE. , ,
.
 . ,   Web- , .
.
.
 . .
. ;
.
. ,
 .
. , .
. ,
,
.
.  , ,
.
. , (, ,
) . PL/SQL,
.

 Ora le 10g  . 692

 Ora le 10g  . 693

ADDM Advisor, CD214


ADF, 650
Appli ation development framework, 650

Memory Advisor, CD217


MetaLink, CD200
Mi rosoft A tive Dire tory, CD126
Model-view- ontroller, 653
MVC, 653

Net Manager, 550

Buffer Ca he Advisor, CD217

CSS, 657

Database administrator, CD171


DBA, CD171
Developer Suite, 549; 675
DML-, 503

Enterprise Manager, CD173

FGA, CD163
Fine-Grained Auditing, CD163
Forms Builder, 549; 551

J2EE, 650
Java 2 Enterprise Edition, 650
JDeveloper, 650
JInitiator, 549; 560; 649
JSP, 657

LOV, 575

Obje t Navigator, 552


OC4J, 550
OID, CD197; CD125
Ora le Appli ation Server, CD92
Ora le Containers for Java, 550
Ora le Internet Dire tory, CD97
Ora leAS, CD92
OTN, 650

P-, 520
PGA Advisor, CD217
PKI, CD151
PL/SQL, 471
Publi -key infrastru ture, CD151

Query A ess Advisor, CD217

RAID, CD151
RAISE, 478
Re overy manager, CD209
Report Wizard, CD28
Reports Builder, CD5; CD9

 Ora le 10g  . 694

694

RMAN, CD209

Se ure so kets layer, CD151


Segment Advisor, CD217
Shared Pool Advisor, CD217
SSL, CD151
SQL A ess Advisor, CD217
SQL Tuning Advisor, CD217
SQL*Loader, CD195
SQL*Plus, 483
SunONE Dire tory Server, CD126

Undo Advisor, CD217

Very large-s ale database, CD191


Virtual private database, CD133; CD146
VLSD, CD191
VPD, CD133; CD146

, 32

, 577
, 32, CD271
, 473

, 653
, CD112
 , CD125

, 31
PL/SQL, 473
, 552; 601; CD181
, 476

, CD7

, CD133

, CD146
, 644

, 571
, CD41

, 571
, CD195

, CD182
, 627
, 508
/ , 508
, 525
, 513
, CD22
CASE, 501
IF, 501
, 508

, 476; CD89
 , CD147
, CD2
, CD209

, CD163
, CD62
, CD67
, CD172; CD199
 , CD97
, CD124
, CD102

 Ora le 10g  . 695

, CD97

, CD89

, CD7
, 557
, CD7
, 474
, CD151
, CD123

, 507
, CD79
, CD193
, CD114
 , CD151
, CD87
, CD7
, 635

, CD150
, CD79
, CD189
BEGIN, 474; 476
DECLARE, 474
EXCEPTION, 474; 478;

625

END, 474
, 579
, 546
, 475
, 471
, 474
, CD124
, 477; 490

695

, 590
, 697

, CD178
LOV, 575
, 553
, 553
, CD7
, CD226
, CD2
, CD2
, CD100
, 653; CD29
 , 526
 , 515
, CD155
, CD199
, 287

, 473
, 477
, 490
, CD7

, 627
, 642
, CD190
, CD199
, 487
, 624
, 552
, 610; 612

, CD48

 Ora le 10g  . 696

696


, CD48
, CD48
, 394
, CD129
, 552
, 565
, CD7
, 501
, 546; 547
, 269
, CD151
, 620
, 547; 682
, CD37
, CD1
, 623

, 551
IN OUT, 508
IN, 508
OUT, 508
desformat, CD94
destype, CD94
PCTFREE, CD187
PCTUSED, CD188
, CD43
, 508
, 38
, 579
, CD50
-
, 520
-
, 473
, CD38
, 473
, 254
, 547; 584

, 557
, CD128
, CD44
, CD49

, 613
, 240; 629

, CD182
, CD2
, 620

Web-, CD55
, CD73
, 632
, CD2
, CD38
, 490
LDAP, CD126
, 473; 508

 , CD7
 , 474
 , 507
 , 476
 , 474
, CD191
 , CD131
 , CD192
, CD181
 , CD153
 , 564
 , CD204
 , CD102
, CD128
 , CD76

 Ora le 10g  . 697

, 405
, CD181
Ora le, CD92
, CD150
, CD227
, CD129
, CD43
, 476
, 476
, CD5
, 612
, CD76
, CD36
, 521
, 509
, 476
, CD28
, 575
, CD193

Java, CD110

Web, CD110
, CD108
, 476
Page Layout, CD15
Paper Design, CD13
, CD100
, 31

, 287
, CD61
, 546; 582
Undo, CD182
, CD180
, CD2
, 620

697

, 269
, 268

, 528
, 517; 519
, CD179
wrap, CD155
, CD126

.CSV, CD195
, CD181
Redo, CD186
Undo, CD186
, 268
, CD47
, CD46
, 579
Web, 549

, CD44
, 546
, 601; CD19
CALL_FORM, CD88
OPEN_FORM, CD88
, 473;508

, CD207
, 551; 600
, 639; 644
, 639; 641

, 639
-, CD193

, 263
, CD124

 Ora le 10g  . 698

698

FOR, 490
WHILE, 490
, 490
 , CD151

Default, CD50
, CD49; CD77
, CD150
, CD150
, CD150

, CD193
, 551

, 477; 490

 Ora le 10g  . 699

Ora le 10g

..
..

..

..

.. , ..


101509, . , . , . 43, . 1
15.08.2006. 70x100/16.
Times. .
. . . 56,76. .-. . 38,68.
3000 .
.
CtP
  . . .
197110, -, ., 15.

Оценить