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

Oracle Press

oirv/

ORACLE

Oracle Press TM

Oracle PL/SQL 101

Christopher Allen

Osborne/McGraw-Hill
: ' ..- , . - . , -

'."'*,./'-;

'.- , ' > ' . :

101: Oracle PL/SQL

: .

""

Oracle PL/SQL 101


Christopher Allen
Copyright 2001 by The McGraw-Hill Companies, Inc. All rights reserved.
Osborne/McGraw-Hill
2600 Tenth Street
Berkeley, California 94710
U.S.A.
ISBN 0-07-212606-X
:

101: Oracle PL/SQL



.
.
.
.
"", 2001
. : OAI (03)
: 070612 30.09.97.
ISBN 5-85582-139-0

20.10.2001 70 100/16
1
. . 23 3200 725

"". 123557 , . ., .40, . 2
: (095) 259-01-62
WWW.LORY-PRESS.RU
" "
109044, , . . 18

, MCI ^Mv*.
,
SQL PL/SQL .
,
. , ,
plsqll 01 yahoo.com. , , ,
. , , .
SQL


Oracle Press.
, , , Oracle
Press , .
.
, , , ,
. . ,
.


'

vi
vii
vii

1
?

/
/
?

.
,

?


-
,

SQL
SQL

3
4
4
5
5
5
7
7
7
8
9
9
10

10
10
11
11
11
12
12
13
13


, , i, ,


' >,

. .

. >:,, ! : -,.

' "-:' '." -:' 1 ':/'.]

~j ^^:.. I, ;

2 :






13
14
14
14
16
17

'

19
20
20
21
21
23
25
25

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

.' : .< ,


Oracle
Oracle
Oracle
, -
NULL NOT NULL

null-
,





: ; < ,.
?


25
26

31
. 31
34
36
39
39
42
42
45
46
46
47
47
48
49
50
51
52
52
55
57

xi
3

59


60

61

61

i r , ; ;v 53

, . . . . ; . . - .,.- , ]
63

64

65

;65

67
null-
68

69

.; V?
69

70

./,, .,, , 72
DUAL
, ,
74

75

77
,
77

77

77

78

79
DML
79
*
S2

84

84

. . . , ' : . ' : , - .
.
.' . '-.
' i
: ,. - ! .-'*

86

87
;

. - . ; ' ' ' - ;.

4 SQL'Plus


EDIT,-. :

CHANGE

89
,

90
90
91
92
S>2
94
95

xii

SQL*Plus
SQL*Plus
SQL*Plus


SQL*Plus

97
97
98
100
100
101
102

102
102
103
103




SQL


103
105
107
108
108
109
109


ACCEPT

110
111

111
112
113

' -

SQL
5 SQL


SYSDATE
USER
USERENV


ROUND
TRUNC

.
117
119
119
119
120
121

121
122
123

12 4

xiii
UPPER, LOWER INITCAP
LENGTH
SUBSTR
INSTR
LTRIMnRTRIM


SYSDATE TRUNC
ADD_MONTHS
LAST_DAY
MONTHS_BETWEEN

124
126
127
129
133

136
136
137
139
141


TO_CHAR

142
142

151

TO_DATE

DECODE
NVL
:

SQL-


SUM
COUNT
AVG
MIN
MAX


GROUP BY

HAVING


''"'.-'' '

147

152
156
156
158

160
161
162
163
163
163

164
166
166
;~
167
168

171
173

174
176
177
178

*-

178
179

xiv

,_.,..

:
?
*

181
182
182

NOT NULL
UNIQUE
CHECK

190
192

193
194
194
196
198


SELECT,



UNION

202
207
208
209


?
,


,

211
211
211
212
214
215
216
217
218

UNION ALL
INTERSECT
MINUS

**,'* ,-"'' .'

'-

182
183
186

"'

' "

'"

-,''

*t

'".

"

'''

' " . ' . "

; '

''-

- . . " - ' . ,..; .

199

210
211
211

' " ' ' . " ,

7 Oracle

INSERT

221
225
226
228
229

XV




null-




JV

, ,

236



Oracle


III
PL/SQL
8 PL/SQL
PL/SQL?
PL/SQL
SQL, PL/SQL SQL*Plus:
,
,

SQL-

229
229
230
231
232
233
235
235
235
236
236
237
239
239
241
242
242

242
242
244
245
246

..
251
252
255,
256
257
257
258
259
259

xvi

PL/SQL




PL/SQL-

PL/SQL
PL/SQL
PL/SQL


PL/SQL
IF

LOOP
WHILE
FOR


PL/SQL
OPEN, FETCH CLOSE
FOR
WHERE CURRENT OF

9 PL/SQL

PL/SQL Oracle

PL/SQL

259
260
261
261
262
263
264
266
266
267
267
269
269
270
272
272
274
275

275
276
277
278
278
279

280
282
282
284
286
288
289
290

293
298
299
300

XVII

DML PL/SQL,



TIMING

PL/SQL




,
ODBC
- '
ODBC-
ODBC-
Access
Oracle Excel

305
310
311
311

- '

312
315
321
323
324
327
327
328
328
333
333
335

338
338
339
341

! " ?" . ,
: . ,
, , . , ,
. , .

?

( ). ()
-, .
. ,
. ,
, .
, " ",
, .
, ,
.
.
- Web-
. ( .)
,
, , ,
. , , AltaVista Yahoo!
, Web-.

. ,
( ) ( ).
: , .
(spreadsheet),
(, , ..)
. ,
:
EMPLOYEE_ID FIRST_NAME LAST_NAME SALARY HIRE_DATE
1024
2048
3072
4096

Scott Campbell 63000


Linda
Hammond 68000
Dave
Anthony 69000
Tiff
Berlin 66000

17-FEB-98
15-JAN-99
11-APR-OO
24-DEC-01

, , : .
! ,

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

/
, , . .
(row),
(record). ,
. , . ,
. ,
, , . ( .)

/
. , . . (columns) .
,
(field). - (,
).
. ,
3x5. ,
. .
, ,
, , . . 1.1 , .
,
. . 1.2 ,
.


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

(555)234-5678

, ,

. 1.1. ,

, ,
(555)234-5678 !! net '

.3 : * .
(555)3 5-6789 *avel!rtg>r iai1.net
'
4 1C
(555) 4i 6-7890 jbndslrupfc mail. ret : Mr mooove
_J|
iUM
5 '

'(555)51 7-8901 :mking@>m ii


pa.v
\s]
:
. j
J" :|
| .
6

(555) 6; 8-9012 sschltmm Smaii net , i HE
^
2

"< > ' ^1/' '/

' .

' >lDi

. 1.2. ,


,
. , .


(
, ), , . 65 536 .
, . (, Web-, , - "Oracle".
1 200 000 , .
, Oracle;
, !) ,
.
!



,

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


, , .
, , ,
. . -

? , ,
.
. , .
, ,
. , ,
, .
. , ,
, . , ,
, ( ).
, 1000
, . ,
Oracle (role).
, , . ,

, , , . ,
, ,
,
, . . ,
" " . ,
.
:
, .


( , , , ),

.
(relationships) .
.
, ,
. PRODUCT
\\ , , , , , . ,
.

, .
, .
ORDER , ,
. , ,
.
: , ,
. ,
ORDER . ? ,
, ,
PRODUCT. , , ,
PRODUCT. ,
,
.
, , (relational database).
,
. , 6. , , ,
. (flat file). ,
.

,
, , : , .
,
.
(constraints), .. ,
, .
, , ,
, : , , , , PRODUCT,

. "" .


1. ?
2. : , , , ,
, , .
3.
?

10

1

1. . , .

2. , ,
(
).
3. , .
4. , . .
5. .
6. , .
"" .
7. , .
"" .
8. " e-mail"
, .

?
, PL/SQL, . ,
- , , .
, PL/SQL.


Oracle (DBA Oracle database administrator) SQL , PL/SQL ( SQL) . ,
SQL, PL/SQL. Oracle ,
(graphic user interface, GUI), . , , SQL,
, GUI-. SQL , , Oracle
,
SQL PL/SQL.

11


,
Java, C++ Oracle Forms Developer, ,
SQL- . , , .
SQL . , Oracle, ,
Oracle Java, , C++ COBOL.

-

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

,


. . : ,
, . , ,
,
, , , . , . , , ,
,
.
.
, , , . . ,
, , , .
Web-
, , . ( ,
. , -

12

.)
- : ,
.

SQL

. SQL . 1969 . - . IBM Derivability, Redundancy, and
Consistency of Relations Stored in Large Data Banks.
, , . "
" . 1970 . A Relational Model of Data for Large Shared Data Banks ACM (Association of
Computer Machinery). , , 1974 .

(), System R. IBM Journal ofR&D 1976 ., IBM
Structured English QUEry Language (SEQUEL, ).
Structured Query Language (SQL, (sequel) "S-Q-L").
SQL 1979
Oracle ( Relational Software Inc.).
1986 .
(ANSI), SQL
ANSI . 135-1986. (ISO) ISO 9075-1987.
SQL 1992 1999 . , ANSI/ISO/IEC
9051-1-1999 - 9051-5-1999.
SQL .
, , SQL
. , , SQL,
,
.
, SQL , , .

SQL
SQL , . :
(Data Definition)

13
j

(Data Manipulation)
(Data Control)
(Data Retrieval)
(Transaction Control)
2
. ,
.
!


, Oracle, . , , ,
. . SQL : CREATE,
ALTER, DROP, RENAME TRUNCATE.
,
(Data Definition Language, DDL).


, . ? ,
. SQL IN SERT,
. , , UPDATE, , DELETE.
(Data
Manipulation Language, DML).


. ( ,
, -, , , " ?".)
, , .
( ).
EMPLOYEE. , ,
. .

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

14


, . SELECT, ,
. ,
, SQL , , Java C++.


SQL DML ,
. (, DML? , "
SQL" .) DML
COMMIT
, ROLLBACK ("").
: DML, . ,
, , . ,
SAVEPOINT.

( ).
() -,
.
, Web- , .
. ,
( ) ( ).
: , .
,
(, , ..)
.
, , : .
( ), . ,
, .

, . , ,

15

(record). ,
.
.
(columns) .
, (field). - ,
.
,
, ,
,
, . ,
, , , .
SQL .
, ,
SQL. Java, , C++ COBOL SQL , . - SQL
, , , - . ,
, SQL , .
SQLooe , - .
1969 . SQL
.
: , , , . (DDL)
; CREATE, ALTER,
DROP, RENAME TRUNCATE.
(DML) ; INSERT, UPDATE
DELETE. (DCL) ,
.
, , .
, . DCL GRANT REVOKE.
SELECT,
.
SQL COMMIT, ; ROLLBACK,
, SAVEPOINT,
DML.
2 . 725

16

1. ?
A.
B.
C.
. Web- ,

E.
F.
2. ?
.
'.
C.
D.
3. .


(, )

,
,
,

,
(,
)

4. ,
?
A.
B. PC
C.
D.

17

E.
F. , , .
5. ?
A. . .
B.
C. . .
D.
6. SQL
.
SQL

(DDL)

GRANT REVOKE

(DML)

SELECT

* (DCL)

CREATE, ALTER,
DROP, RENAME
TRUNCATE

COMMIT, ROLLBACK
nSAVEPOINT

INSERT, UPDATE
DELETE


1. , , D, F ; ;
Web- , ; .

, ,
.
, , .
2.

D ,
.
. , .
3.

18

,
(,
)


(, )

,
,

4. , , D, , F ;
; ; ;
,
, .

, ,

.
PC , Oracle
Unix, Linux, Windows
NT . PC ,
, ,
.
5.

. .

- .
1969 . "" .
6.

SQL

(DDL)

CREATE, ALTER,
DROP, RENAME
TRUNCATE

(DML)

INSERT, UPDATE
DELETE

(DCL)

GRANT REVOKE

SELECT

COMMIT, ROLLBACK
SAVEPOINT

...

.. ---

....

'

20 ;'.._

, . , , , ,
. ;
, . .
, ,
.


, ,
. ;
, . , .
.
,
SQL*Plus , Oracle, , *
.
, , ,
SQL*Plus . (
Oracle SQL* Plus .
, Oracle Si
for Windows NT Starter Kit (Oracle Press, 2000) Oracle.)
, , a SQL*Plus
SQL>, .


,
, : ,
. ,
, .
CREATE TABLE. , ,
SQL> :
CREATE TABLE plsqll01_test_l '(
first_name CHAR(15),
last_name CHAR (20)

, ENTER. , . 2.1.
SQL* Plus , . ( , ,
, .) Oracle!
, . .
.

Be Ed Sech Options *':SQL> CREATE TflBLE plsqUDI test_1 (

2
3

first_nane CHflR(15),
last_nane CHAR(2)

s ;

.Table created.

SQL>
'';'.,

'

^y
. 2.1. CREATE TABLE


INSERT.
SQL>:
INSERT INTO plsqll01_test_l VALUES ('Jane', 'Smith');''

, ENTER. , . 2.2.
. ,
SQL> ENTER:
INSERT INTO plsq!101_test_l VALUES ('Cristopher', 'Alien*);'

. ,
INSERT . ? .

, ,
ENTER. SQL-. SOL*Plus , ENTER
.


, :
SELECT * FROM plsql!01_test_l;

,
. 2.3.

22

A. Oiacle SQL'Plus
File Search Options Help
SQL> CREATE TABLE plsq!101 test_1 (
2
first_name CHAR(15),
3
last_name CHAR(20)

5 ;

>

Table created.

'

i -.'.

'.

SQL>
1
SQL> INSERT INTO plsql1B1_test_1 UALUES ('Jane , 'Smith');
1 row created.
SQL>

. 2.2. INSERT

* Oiacle SQL'Plu
File Edit Search f l p t i o n s Help
. .
SQL> CREATE TABLE plsql1B1_test 1 (
2
first_naroe CHAR(15),
3
last_nane CHAR(20)
4
)
5 ;

Table created.
SQL>
SQL> INSERT INTO plsql101_test_1 UflLUES ('Jane', 'Snith');
1 row created.
SQL> INSERT INTO plsq!1B1_test_1 UALUES ('Christopher', 'Allen');
1 row created.
SQL>
SQL> SELECT FROM plsqll01_test_1;
FIRST NAME

LAST NAME

Jane
Christopher

Smith
Allen

SQL> |

. 2.3. SELECT

23


,
.

! !

, .
:
DROP TABLE plsql!01_test_l;

, . 2.4.
HI3E3

*. Oracle SQL-Plus
File Edit Search Options Help
SQL> CREflTE TABLE plsqllB1_test_1 (
first_nane CHAR(15),
3
last name CHfiR(2B)
it
5

Table created.
SQL>
SQL> INSERT INTO plsqllB1_test_1 UALUES ('Jane1, 'Smith1);
1 row created.

,.,!.

.-,

SQL> INSERT INTO plsqll81_test_1 UALUES ('Christopher1, 'Allen1);


1 row created.
SQL>
SQL> SELECT * FROM plsqll01_test_1;
FIRST NOME

LAST NAME

Jane
Christopher

Smith
flllen

SQL> DROP TABLE plsqll01_test_1;

Table dropped.
SJL>

. 2.4. DROP TABLE


.
, ;
SELECT * FROM plsqllOl test_l;

24

, . 2.5.
. Oracle .
, . ( , .) (*)
, .
, ,
,
(PLSQL101_TEST_1) .
* IJmr.le SfJI.'HIus

1 urch Qptions
SQL> TABLE plsqlt01 test_1 (

2
3

;:
5 ;

first CHflR(i5),

list MM

..

CHAR(20)

Table created.
SQL>
SQL> INSERT INTO plsql101_test_1 UflLUES ('Jane', 'Snith');
1 row created.
SQL> INSERT INTO plsq!101_test_1 UHLUES ('Christopher', 'Allen');
1 row created.
SQL>
SQL> SELECT FROM plsql181_test_1;
FIRST NAME

LAST

Jane
Christopher

Snith
Allen

SQL> DROP TABLE plsq!101_test_1;


Table dropped.
SQL> SELECT * FROM plsqliei test 1;
SELECT FROM plsq!101_test~1

ERROR at line 1:
ORA-60942: table or view does not exist
SO.LH

. 2.5.

25

. : , . ! .
, . ,
. , .


,
. , ,
, ,
. , :
, , ,

,

, , ;


,
. 7.


, . , ,
.

. ,
,
. (
SQL.)
30 .
,
(_). ( ,
,
, ,
, .):

26

2
.
,
.

.
.
Oracle ;
, .
,
. ,
. (
.)
.
Oracle, a
,
. , ,
. , ,
2.1.

~jp

,
Oracle, , ,
. , Accounts Payable
(" ")

.
,
. , PRODUCT
,
. ,
( 7),
, : "PURCHASE
ORDER PRODUCT...".
TABLE DATA .
, ,
, , .
.

27

2.1. Oracle,

ACCESS

ACCOUNT

ACTIVATE

ADD

ADMIN

AFTER

ALL_ROWS

ALLOCATE

ALTER

ANALYZE

AND

ANY

ARCHIVE

ARCHIVELOG

ARRAY

AS

ASC

AT

AUDIT

AUTHENTICATED

AUTHORIZATION

AUTOEXTEND

AUTOMATIC

BACKUP

BECOME

BEFORE

BEGIN

BETWEEN

BFILE

BITMAP

BLOB

BLOCK

BODY

BY

CACHE

CACHE
INSTANCES

CANCEL

CASCADE

CAST

CFILE

CHAINED

CHANGE

CHAR

CHAR_CS

CHARACTER

CHECKPOINT

CHECK

CHOOSE

CHUNK

CLEAR

CLOB

CLONE

CLOSED,
CACHED
OPEN_
CURSORS

CLUSTER

COALESCE

COLUMN

COLUMNS

CLOSE
'
COMMENT

COMMIT

COMMITTED

COMPATIBILITY

COMPILE

COMPOSITE
LIMIT

COMPRESS

COMPUTE

CONNECT

CONNECT.TIME

CONSTRAINTS CONSTRAINT

CONTENTS

CONTINUE

CONTROLFILE

CONVERT

COST

COUNT

CPU_PER_
CALL

CPU_PER
SESSION

CREATE

CURRENT

CURRENT
SCHEMA

CURRENT,
USER

CURSOR

CYCLE

DANGLING

DATABASE

DATAFILE

DATAFILES

DATAOBJNO

DATE

DBA

DEALLOCATE

DEBUG

DEC

DECIMAL

DECLARE

DEFAULT

DEFERRABLE

DEFERRED

DEGREE

DELETE

DEREF

DESC

DIRECTORY

DISCONNECT

DISABLE

DISMOUNT

DISTINCT

DISTRIBUTED

DML

DOUBLE

DROP

DUMP

EACH

ELSE

ENABLE

END

ENFORCE

ENTRY

ESCAPE

ESTIMATE

EVENTS

EXCEPTIONS

EXCHANGE

'

COMPLETE

28
2.1 ()
EXCLUDING

EXCLUSIVE

EXECUTE

EXEMPT

EXISTS

EXPIRE

EXPLAIN

EXTENT

EXTENTS

EXTERNALLY

FAILED
LOGIN
ATTEMPTS

FALSE

FAST

FILE

FIRST.ROWS

FLAGGER

FLOAT

FLUSH

FOR

FORCE

FOREIGN

FREEUST

FREEUSTS

FROM

FULL -.n.

FUNCTION

GLOBAL

GLOBAL.NAME

GLOBALLY

GRANT

GROUP

GROUPS

HASH

HASHKEYS

HAVING

HEADER

HEAP

IDENTIFIED

IDLEJIME

IF

IMMEDIATE

IN

INCLUDING

INCREMENT

IND_ PARTITION

INDEX

INDEXED

INDEXES

INDICATOR

INITIAL

INITIALLY

INITRANS

INSERT

INSTANCE

INSTANCES

INSTEAD

INT

INTEGER

INTERMEDIATE

INTERSECT

INTO

IS

ISOLATION

ISOLATION
LEVEL

KEEP

KEY

KILL

LAYER

LESS

LEVEL

LIBRARY

LIKE

LIMIT

LINK

UST

LOB

LOCAL

LOCK

LOG ov

LOGFILE

LOGGING

LOGICAL
READS_PER_

LOGICAL
READS PER
CALL

LONG

-\v.?- i

MANAGE

MASTER

MAX

MAXARCHLOGS

MAXDATARLES

MAXEXTENTS

MAXINSTANCES

MAXLOGRLES

MAXLOGHISTORY

MAXLOGMEMBERS

MAXSIZE

MAXTRANS

MAXVAUJE

MEMBER

MIN

MINEXTENTS

MINIMUM

MINUS

MINVALUE

MODE

MODIFY

MOUNT

MOVE

MTS
DISPATCHERS

MULTISET,

NATIONAL

NCHAR

NCHAR.CS

NCLOB

NEEDED

NESTED

NETWORK

NEW

NEXT

NLS.CHA

NLS_

:
2.1 ()

29
' ' : 404 '" "'

NLS ISO
CURRENCY

NLS
LANGUAGE

NLS NUMERIC
CHARACTERS

NLS.SORT

NLS
TERRITORY

NOARCHIVELOG

NOAUDIT

NOCACHE

NOCOMPRESS

NOCYCLE

NOFORCE

NOLOGGING

NONE

NOMAXVALUE

NOMINVALUE

NOORDER

NOOVERIDE

NOPARALLEL

NORESETLOGS

NOREVERSE

NORMAL

NOS SPECIAL
CHARS

NOSORT

NOT

NOTHING

NOWAIT

NULL

NUMBER

NUMERIC

NVARCHAR2

OBJECT

OBJNO

OBJNO_REUSE

OF

OFF

OFFLINE

OID

OIDINDEX

OLD

ON

ONLINE

ONLY

OPCODE

OPEN

OPTIMAL

OPTIMIZER
GOAL

OPTION

OR

ORDER

OVERFLOW

ORGANIZATION

OWN

PARTITION

PASSWORD

PASSWORD
UFEJIME

PASSWORD
LQCKJIME

PASSWORD
REUSEJWX

PASSWORD
VERIFY
FUNCTION

PASSWORD
GRACEJIME

PCTINCREASE

PCTTHRESHOLD

PERCENT

PCTVERSION

PERMANENT

PLAN

PLSQL
DEBUG

POST
TRANSACTION

PRECISION

PRESERVE

PRIMARY

PRIOR

PRIVATE

PRIVATESGA

PRIVILEGE

PRIVILEGES

PROCEDURE

PROFILE

PUBLIC

PURGE ~

QUEUE

QUOTA

RANGE

RAW

RBA

READ

REAL

REBUILD

RECOVER

RECOVERABLE

RECOVERY

REF

REFRESH

REFERENCES

REFERENCING

RENAME

REPLACE

RESET

RESIZE

RESETLOGS

RESOURCE

RESTRICTED

RETURN

RETURNING

REUSE

REVERSE

REVOKE

ROLE

ROLES

ROLLBACK

ROW

ROWID

ROWLABa

ROWNUM

ROWS

RULE

PCTFREE

' PACKAGE

PARALLEL
.. :,-...' :,;..
PASSWORD
REUSEJIME
PCTUSED
'-r'

30

2.1 (.)
SAMPLE
if.-.

SAVEPOINT

SCAN
INSTANCES

SCHEMA

SCN

SCOPE

SD.ALL

SDJNHIBIT

SD_SHOW

SEG_BLOCK

SEG_FILE

SEGMENT

SELECT

SEQUENCE

SERIALIZABLE

SESSION

SESSION
CACHED
CURSORS

SESSIONS
PER USER
SIZE
I
SPECIFICATION

'

SET

SHARE

SHARED

SHARED_POOL

SHRINK

SKIM
UNUSABLE
.INDEXES

SMALLINT

SNAPSHOT

SOME

SORT

SPLIT

SQLJRACE

SQLCODE

SQLERROR

STANDBY

START

STOP

STATISTICS

STORAGE

STRUCTURE

STORE

SUM

STATEMENT
ID
SUCCESSFUL

SWITCH

SYNONYM

SYSDATE

SYSDBA

SYSOPER

SYSTEM

TABLE

TABLES

TABLESPACE

TABNO

TEMPORARY

THAN

THE

THEN

TABLESPACE
NO
' "
THREAD

TIME

TIMESTAMP

TO

TOPLEVEL

TRACE

TRACING

TRANSACTION

TRANSITIONAL

TRIGGER

TRIGGERS

TRUE

TRUNCATE

TX

TYPE

UBA

UID

UNARCHIVED

UNDER

UNDO

UNION

UNIQUE

UNLIMITED

UNLOCK

UNRECOVERABLE

UNTIL

UNUSABLE

UNUSED

UPDATABLE

UPDATE

USAGE

USE

USER

USING

VALIDATE

VALIDATION

VALUE

VALUES

VARCHAR

VARCHAR2

VARRAY

VARYING

VIEW

WHEN

WHENEVER

WHERE

WITH

WITHOUT

WORK

WRITE

XID

31



. Oracle ,
, , , . .
. , . ,
. ,
.

Oracle
, .
, .
, , , .
, . , -
. (, , , , .)
,
? , . .
:
(800)555-1212
, , , .
(zip-) 12345-6789
(Social Security Numbers, SSN) 123-45-6789.
,
, .. .

,
? , -
(, ). ,
.
.

Oracle ,
.
, ; CHAR ( "character").
CHAR, -

32

, . ( ):
CREATE TABLE _ (_ CHAR());

,
CHAR. , , .' ,
, , , . , ,
. , , , .
"". ""
, .
, .

CHAR . , 1.
.
, 1.
VARCHAR2 ( "variable-length character"). , CHAR,
, . ?
CHAR , , ,
, .
. , "George",
CHAR(IO), "George" . , CHAR
, , , , .
VARCHARxpamiT ,
. VARCHAR( 10), "George",
. VARCHAR2
, . , , . .

VARCHAR2, VARCHAR? .
VARCHAR ,
Oracle VARCHAR2. Oracle , VARCHAR
, ,
VARCHAR. VARCHAR2.

33

, SQL*Plus :
CREATE TABLE plsqll01_test_2 (
name VARCHAR2 (.20) ,
gender CHAR(l)

INSERT INTO plsqll01_test_2 VALUES ('George1, 'M');


INSERT INTO plsql!01_test_2 VALUES ('Jane1, 'F');
SELECT * FROM plsq!101_test_2;
DROP TABLE plsql!01_test_2;

, SQL*Plus ,
. 2.6.

-
Edit Search Option* Help
SQL> CREATE TABLE plsq!1B1_test 2 (
2
name
UARCHAR2(20),
3
gender CHAR(1)
*
)

5 ;

Table created.
SQL>

SQL> INSERT INTO plsq!101_test_2 UALUES ('George 1 , 'M');

1 row created.

'

- '

-, :

':'' " ' , "

SQL> INSERT INTO plsqliei_test_2 UALUES ('Jane1. 'F');

1 row created.

SQL>

SQL> SELECT FROM plsqllB1_test_2;


NflHE

George
Jane

SQL>
SQL> DROP TABLE plsq!101_test_2;

Table dropped.
SQL>

. 2.6. CHAR VARCHAR2

34

, , INSERT . SQL-,
. .
, :
LONG. 2 147 483 647 (2 ) .
: LONG .
, , Oracle.
,


(string),
" ". , , "" "" . ASCII,
American Standard Code for Information Interchange (
). ASCII
,
,
,
. "", .
"ASCII", , , , . , Microsoft Word
( .doc) ASCII-, File \ Save As Text
Only, ASCII. ( ,
. , .) ASCII- .

Oracle

, , NUMBER.- NUMBER
, .
:
.
, .
. , , :
CREATE TABLE plsq!101_product (
product_name VARCHAR2(25),
product_price NUMBER(4,2)

:
INSERT
INSERT
INSERT
INSERT

INTO
INTO
INTO
INTO

plsql!01_product
plsql!01_product
plsql!01_product
plsql!01_product

VALUES
VALUES
VALUES
VALUES

('Product
('Product
('Product
('Product

35
Name
Name
Name
Name

1',
2',
3',
4',

1);
2.5);
50.75);
99.99);

SELECT * FROM plsql!01_product;


DROP TABLE plsql!01_product;
, , . 2.7.

Oiacle SQL'Plus
File dit Search Options Help
SQL> CREATE TABLE plsqllB1_product (
2
product_nane UARCHAR2(25),
3
product_price NUMBER (it, 2)
*
)

5 ;
Table created.
SQL>
SQL> INSERT INTO plsql101_prodiict UflLUES ('Product Name 1', 1);
1 row created.
SQL> INSERT INTO plsqllByproduct UflLUES ('Product Name 2 ' , 2.5);

1 row created.
SQL> INSERT INTO plsqll81_product UflLUES ('Product Name 3', 50.75);
1 row created.
SQL> INSERT INTO plsqll81_product UflLUES ('Product Name V , 99.99);

1 row created.
SQL>
SQL> SELECT * FROM plsqll Byproduct;
PRODUCT NAME

PRODUCT PRICE

Product Name 1
Product; Name 2

Product Name 3
Product Name

1
2.5
50.75
99.99

SQL>
SQL> DROP TABLE plsqll81_product;

Table dropped.
SQL>

. 2.7. NUMBER

36

, NUMBER :
'\]__,

____)

NUMBER : 999999999999999999999999999 999 999 999 990 000


000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000
000 000 000 000 000 000 000. 127.
, , , Oracle,
, .

Oracle

. ,
, :
January 15, 2002
February 15, 2002
March 15,2002
, , ,
"F", February, "J" (January)
"" (March). , , . :
01-15-2002
02-15-2002
03-15-2002
. , , ,
, .
, . :
;v 01-15-2010
02-15-2005
03-15-2000
, ,
, ,
, .
"02", ,
"01", , .
, . :
2010-01-15
2005-02-15
2000-03-15
.
:

:
2000-03-15
2005-02-15
2010-01-15

37

;/

, , , . . ,
15 ,
30 .

. , , .
(, ),
. (date math).
, , , .
, , , . - .
,
, .
, .
: .
, 1, 2 . . ,
. Oracle , 1 4712 . ..
(, '08-MAY-2004') . , Oracle ,
, , , .
.
,
Oracle, :
CREATE TABLE plsq!101_purchase (
product_name VARCHAR2 (25) ,
product_price NUMBER(4,2),
purchase_date DATE
\

INSERT INTO plsql!01_purchase VALUES


('Product Name 1', 1, '5-NOV-OO ' ) ;
INSERT INTO plsql!01_purchase VALUES
('Product Name 2', 2.5, ' 29-JUN-01 ' ) ;
INSERT INTO plsq!101_purchase VALUES
('Product Name 3', 50.75, ' 10-DEC-02 ' )

38
INSERT INTO plsql!01_purchase VALUES
('Product Name 4', 99.99, '31-AUG-03');
SELECT * FROM plsqllOljpurchase;

, . 2.8.

SQL- ,
.

* Oiacle SQL'Plus
File Edit Search Options Help
SQL> CREflTE TftBLE plsql101_purchase (
2
product_name UARCHAR2(25),

3
4
5
6 ;

product_price NUMBtR(4,2),
purchase_date
)

Table created.
SQL>
SQL> INSERT INTO plsqll01_purchase UflLUES
2
('Product Name 1', 1, 'S-NOU-ee1);

1 row created.
SQL> INSERT INTO plsqll81_purchase UftLUES
2
('Product Name 2', 2.5, '29-JUN-B1' );

1 row created.
SQL> INSERT INTO plsqllB1_purchase UflLUES
2
('Product Name 3', SB.75, 'IB-DEC-eZ1);
1 row created.
SQL> INSERT INTO plsql1B1_purcHase UflLUES
2
('Product Name V, 99.99, '31-1-');
1 row created.
SQL>
SQL> SELECT * FROM plsqllB1_purcnase;
PRODUCT

Product
Product
Product
Product

Name
Name
Name
Name

PRODUCT PRICE PURCHASE

1
2
3
4

1
2.5
58.75
99.99

05-NOU-00
29-JUN-B1
18-DEC-B2
31-flUG-B3

SQL> |

. 2.8. DATE

39

,
. , - 29 2002 . , Oracle , 2002 .
, , 29 .
DATE .
, , ( ,
). ,
54321, 54321.5 (.5 ,
). 6:00
54321.25, 18:00 54321.75. . , 15:16,
.636111111 . ( 6 .)


, ...
. , , . - ,
. ,
. ,
, Oracle , . DESCRIBE ( DESC)
:
DESC _
, . , ,
.
, DESC, :
DESC plsql!01_purchase

. 2.9.
DESC : Name, Null? . Name .
Null? ,
.

NULL NOT NULL


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

40

Oioclt! SQL-Plus

Ete Ed Search Options 1


SQL> DESC plsqliei_purchase
Name

Null?

PRODUCT_NA
PRODUCT_PRICE
PURCHflSE_DOTE

Type
UflRCHflR2(25)
NUMBER(4,2)
DATE

SQL>

. 2.9. DESCRIBE
. , , .
, , (null).
null; , , , . .
,
. , : ,
, .
, . :

{, )
. . ,
.

41

, ? , . ,
, . ,
-
. ,
,
, .
, . , , ,
, , .
, ,
CREATE TABLE. ( ;
3.)
"NOT NULL" . ,
, SQL*Plus :
DROP TABLE plsql!01_purchase;
CREATE TABLE plsql!01_purchase (
product_name VARCHAR2 (25) NOT NULL,
product_price NUMBER(4,2) NOT NULL,

purchase_date DATE

r';>
.;., .,

i
, . 2.10. ,
, NOT NULL,
, .
.
-

* Oracle SQL-Plus
File Edit Search Options Help
SQL> DROP TABLE plsql101_purchase;

Table dropped.
SQL>
SQL> TABLE plsq11B1_purchase (

3
i
5

6 ;

product_nane

UftRCHflR2(25) NOT NULL,

product_price NUHBER(I,2)
purchase_date DATE
)

NOT NULL,

Table created.

SQL>

-dJ
. 2.10. NOT NULL

42



, . . , ,
, , .

null-
, null- , .
, null- .
.
, INSERT
"NULL" . ,
(PLSQL101_PURCHASE) , . ,
:
INSERT INTO plsql!01_purchase VALUES ('Product Name 1', 1, NULL).;
SELECT * FROM plsqll01_purchase;

. 2.11. : SQL*Plus SELECT, .

;*. Oracle SQL-Plus


File Edit Search Options Help
.
SQL> INSERT INTO plsql1B1_purchase UALUES ('Product Name 1', 1, NULL);
1 row created.
SQL>
SQL> SELECT FROM plsqllB1_purchase;
PRODUCT_NflME
Product Name 1

PRODUCTJMUCE PURCHftSE_
1
'

SQL>

. 2.11. null-

43

, , null-, , NOT NULL,


. ? , , . :
, . :
INSERT INTO plsql!01_purchase VALUES
(NULL, 2.5, '29-JUN-01');
INSERT INTO plsql!01_purchase VALUES
('Product Name 3', NULL, '10-DEC-02');
INSERT INTO plsql!01_purchase VALUES
(NULL, NULL, '31-AUG-03');
SELECT * FROM plsqll01_purchase;

, . 2.12.
INSERT , , null- , NOT NULL.
Oracle SQl'Plus
fife dit Search Qplions H*
SQL> INSERT INTO plsqll01^purchase UALUES
2
(NULL, 2.5, '29-JUN-ei');
INSERT INTO plsqll01_purchase UALUES

jj
-J

ERROR at line 1:
ORO-01itOO: cannot insert NULL into' ( l PLSQLiai"."PLsqL101_PURCHflSE". ll PRODUCT_NAME")
SQL> INSERT INTO plsqll01_purchase UALUES
2
('Product Name 3 1 , null, 'lO-DEC-BZ 1 );
INSERT INTO plsqll01 purchase UALUES
*
ERROR at line 1:
OHfl-01!00: cannot insert NULL into ( 11 PLSQL181' 1 ."PLSQL101_PURCHASE". >1 PRODUCTJ > RICE")

SQL> INSERT INTO plsqll01 purchase UALUES


2
(NULL, NULL, 'ai-flUG-BS 1 );
INSERT INTO plsqllei_purchase UflLUES
*
ERROR at line 1:
ORft-OUtOO: cannot insert NULL into ("PLSQL101"."PLSQLiei_PURCHASE".lIPROOUCT_HftllE")

:
SQL>
SQL> SELECT FROM plsq111 purchase;
PRODUCT_NAME
Product Name 1

'

'

PRODUCT_PRICE PURCHASE_
1

SQL> |

. 2.12. null- NOT NULL


,

44

null- , NULL , .
INSERT,
, .
INSERT , , . INSERT , Oracle ,
, , . ,
,
.
, , :
INSERT INTO plsql!01_purchase (product_name, product_price)
VALUES ('Product Name 2', 2.5);
INSERT INTO plsql!01_purchase (product_name, product_price)
VALUES ('Product Name 3', 50.75);
INSERT INTO plsql!01_purchase (product_price, product_name)
VALUES (99.99, 'ProductName 4');
SELECT * FROM plsqll01_purchase;

, . 2.13,
,+ Oiacle SQL'Plus
0e fid ech Qptiora Help
SQL>
QL> INSERT
INSERT INTO
INTO plsq!1B1_purchase
plsqll
(product_nane,
2
UflLUES Product Name 2 ' , 2.5);

product_prlce)

1 row created.
SQL> INSERT INTO plsqll 6"1_purcnase
(product , product price)
2
UflLUES ('Product Name 3 1 , 58.75)

1 row created.
SQL> INSERT INTO plsq!101_purchase (product_price, product_nane)
2
UflLUES (99.99. 'Product Name 4');
' '
.

..''-..,,'.
1 row created.
SQL>
SQL> SELECT * FROH plsqllB1_purchase;
PRODUCT_NflME

Product Name 1
Product Name 2

Product Name 3

Product Name i

PRODUCT_PRICE PURCHASE.

1
2.5

58.75

99.99

SQL>

JJJ

. 2.13. null-

jj

45

,
INSERT . , , , .
, , INSERT,
.


, . , Oracle
. , Oracle ,
, , . , ,
:
' * ' ' - ' < - .

' .

INSERT INTO plsql!01_purchase VALUES


('Fifth Product's Name', 25, '05-MAY-03');

Oracle , . 2.14.
* Otacle SQL'Plu

13!

! Search Qptiora Help


.'
SQL> INSERT INTO plsq!101_purchase UflLUES

('Fifth Product1s Name', 25, '05-MftV-03');

ERROR:
ORfl-61756: quoted string not properly terminated

SQL>

. 2.14. ,
, . ,
: SET SCAN OFF INSERT
,
. ,
:
,
SET SCAN OFF
INSERT INTO plsql!01_purchase VALUES
('Fifth Product''s Name', 25, '05--');
SET SCAN ON

, :
SELECT * FROM plsqll01_purchase;

46

,
. 2.15.
Oracle SQL'Plu
File Edit Seaich flptions Help
SQL> SET SCftN OFF
SQL>
SQL> INSERT INTO plsqliai_purchase UftLUES
1
2
('Fifth Product 's Name', 25, 'OS-MAY-OS');
1 row created.
SQL>
SQL> SET SCAN ON
SQL>
SQL> SELECT * FROM plsq!1B1_purchase;
PRODUCT NAME

PRODUCT PRICE PURCHASE

Product Name 1
Product Name 2
Product Name 3
Product Name 4
Fifth Product's Name

1
2.5
58.75
99.99
25 85-MflV-03

SQL>

jJJ

. 2.15.

,
SELECT, . , , ,
,
. ? .


,
.
SELECT,
"*", .
, :
"

.1'*;.., SELECT product_name FROM plsql!01_purchase;

47

, . 2.16.
,
SELECT .
* Oracle SQL-Plus
File Edit Search flptkms Help
SQL> SELECT product_narae FROM plsqll01_purchase;
PRODUCT NftME
Product Name 1
Product Name 2
Product Name 3
Product Nane 4
Fifth Product's Nane
SQL>

.dJ

. 2.16.
. ,


PLSQLl01_PURCHASE :
SELECT product_name, purchase_date FROM plsq!101_purchase;


, , . SELECT , .
, PLSQL101_PURCHASE
, :
SELECT purchase_date, product_price, product_name
FROM plsql!01_purchase;
, . 2.17. , PLSQL101_PURCHASE ,
- .


. , , ,
, 7%,
, ( ). SQL.
SQL-, .
3 . 725

48

fte < Search Options Help : ;

',;.:;

;:

SQL> SELECT purchase_date, product_price, product_nane


2 FROM
plsql1B1~purchase;

,...*

PURCHASE. PRODUCT_PRICE PRODUCTJfflME

05--03

1 Product Name 1
2.5 Product Name 2
58.75 Product Name
99.99 Product Name 4
25 Fifth Product's Name

SQL>
<| J

. 2.17. ,
, ,
PLSQL101_PURCHASE 15%.
:
SELECT product_name, product_price * 1.15 FROM plsqll01_purijhase;
. 2.18.
!

* Oracle SQL-Plus
file Edit Search Option? Help
SQL> SELECT product_nane, product_price * 1.15 FROM plsql101_purchase;
PRODUCT NflME

Product Name 1
Product 2
Product 3
Product Nam 4
Fifth Product's Nan

PRODUCT PRICE*1.15

1.15
2.875

58.3625

114.9885

28.75

SQL>

. 2.18. : 15% .

, ,
(operators). ,
. Oracle , , .
(*). /, -

'

- .

49
.

...

-.,

f <,..*-, Wc* -

+ -, . ,
, . ,
.
DROP TABLE plsql!01_purchase;

CREATE TABLE plsql!01_purchase (


product_name VARCHAR2(25) ,
product_price NUMBER(4,2),
sales_tax NUMBER(4,2),
purchase_date DATE,
salesperson VARCHAR2(3)

INSERT INTO plsql!01_purchase VALUES


1
('Product Name I , 1, .08, '5-NOV-OO', 'AB');
INSERT INTO plsql!01_purchase VALUES
('Product Name 2', 2.5, .21, '29-JUN-01', ',CD');
INSERT INTO plsql!01_purchase VALUES
1
('Product Name 3', 50.75, 4.19, 40-DEC-02 , 'EF');
INSERT INTO plsql!01_purchase VALUES
1
('Product Name 4', 99.99, 8.25, '31-AUG-Q3 , 'GH');
SELECT product_name, product_price + sales_ta'x
FROM plsqllOljpurchase;
SELECT product name, 100 - product_price
FROM plsqll01_purchase;
SELECT product_name, sales_tax / product_price ..
FROM plsqllOljpurchase;

SELECT , . 2.19.

,
.
Oracle 7,
. Oracle 8
, SQL-
.

Oracle (expression) .
, , NULL, (, " 100" , ,
), , . :
SELECT product_name, product_price * 2 + 10 FROM plsqllOljpurchase;

50

1-IOiX
* Oracle SQL'Plus
File Edit Search Options Help
SQL> SELECT product name, product_price + sales_tax FROM plsql181_purchase; _j

PRODUCT
Product
Product
Product
Product

Name
Name
Name
Name

PRODUCT PRICE+SflLES TUX

1.88
2.71
51. 94

1
2
3
4

SQL> SELECT product naroe, 180 - product_price FROM plsql1B1_purchase;


PRODUCT NflME
Product
Product
Product
Product

Name
Name
Name
Name

180-PRODUCT PRICE

99
97.5

1
2
3
4

49.25

.81

SQL> SELECT productjnane, sales_tax / product_price FROM plsql181_purchase;


PRODUCT NflME
Product
Product
Product
Product

Name
Name
Name
Name

SftLES /PRODUCT PRICE


1
2
3
4

.88
.084
.082561576
.882588251

SQL> |

. 2.19. SELECT
: PRODUCT_NAME PRODUCT_PRICE
* 2 + 10.
: " Oracle , ?"
(operator precedence).

, Oracle
, . , , . , .
,
, - . , .
, ,
.
:
SELECT product_name,

(product_price * 2)

+ 10 FROM plsql!01_purchase;

51


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

SELECT, (||). , product_name salesperson:
SELECT product_name || salesperson FROM plsq!101_purchase;

,
, - . , .
, .
SQL-, . ,
, :
SELECT product_name I | ' was sold b y ' I | salesperson
FROM plsql!01_purchase;

, . 2.20.
, ,
(literal). , ,
, , .
31

* Oiacle SQL-Plus
File Edit Search Options Help
SQL> SELECT product_name || ' was sold by ' | | salesperson
2 FROM
plsq!101 ^purchase;

^
^
^

^
^
^

^
^
^

^
^

^
^

PRODUCT _NftME | | 'UASSOLDBV | | SALESPERSON


Product
Product
Product
Product

Name
Name
Name
Name

1
2
3
4

was
was
uas
Mas

sold
sold
sold
sold

by
by
by
by

ftB
CD
EF
CH

'

SQL>

. 2.20.
SELECT

52


,
" ". .
SELECT, , .
. SQL , ,
SELECT. , , ( ) , .
, ,
SELECT:
SELECT product_name || ' was sold by ' I I salesperson SOLDBY
FROM plsql!01_purchase;

-, , (alias). SOLDBY ,
.
,
. ( , Oracle
, .)
, , :
SELECT product_name || ' was sold by ' || salesperson "Sold By"
FROM plsql!01_purchase;

, . 2.21.
A Oracle SQL-Plus
file * Search Qptions Help
SQL> SELECT product_nane 11 was sold by ' || salesperson "Sold By1'
2 FROM
plsq!1B1_purchase;

Sold By
Product
Product
Product
Product

Mane
Name
Name
Name

1
2
3
4

was sold by ftB


was sold by CD
was sold by EF
was sold by GH

SQL>

. 2.21.

SQL. , , ,
.

53

30 , ;
,
, . .
. Oracle .

. ,
(, CREATE) (, ROW).
, . , , Oracle
"Invalid table name" "Invalid column name".
, , , (, AP_ADMIN ADMIN).
, - ,
.
,-,
, .
;
. ( , ^
Oracle, .)
. .
,
: , EMPLOYEE,
EMPLOYEES. , TABLE
DATA, , ,
. . Oracle
, , , , . .
.
, .
, , , .
, . , -
.
, ,
.
, , , . ,
.
: ^
.
CREATE TABLE CHAR. CHAR
, .

54

. , CHAR ,
, , , .
, VARCHAR2. ,
, 2000 (
VARCHAR2), LONG, .
. SQL-
. ,
, ( , ), ASCJI-. (ASCII American Standard
Code for Information Interchange.) ASCII " ",
.
Oracle
NUMBER.
, , .
, , 999 999 999 999 999 999 999 999 999
999 999 999 990 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000
000 000 000 000 000 000 000 000 000 000 000 000.
Oracle DATE. (, , )
.
(, '08-MAY-2004') .
, Oracle ,
, , , . .
DATE . , , ,
7 . Oracle ; , - 29 2002
. , Oracle , 2002 . , , 29 .
, ,
. , 33333, 18:00 33333.75 (.75
, 18:00 75% ).
Oracle , DESC . , CREATE TABLE
NOT NULL ( ),

.

55

, null-, ,
NULL SQL-, .
INSERT , , .
SQL*Plus, INSERT SET SCAN OFF.
, SET SCAN ON, .
.
, , SELECT , . ,
, SELECT.
, ,
SELECT ,
. , , .
, , .. , Oracle . ,
, .
, . . ,
, .
, ( , ),
SELECT (||).
, ||'' ||.
. , , .
. ,
!

1. ?

A. , SQL-
B. , Oracle

'

C. , , ,

D. ,
.

56

2
2. ?
A. CREATE TABLE _
__1 _,
__2 _
;

B. CREATE TABLE _
FROM

__1 _,
__2 _

'. '7 '

C. CREATE TABLE _ (
__1 _,
__2 _

3. ?
A.
B.
C.

. . . . ''

. ' . '. : .,1

'

D.
E.
4. -
?
SELECT first_name | |
" " II

;,'.;

last_name
"Full Name"

FROM

plsql!01_person;

A. 1

-.B.2;-;;,
.
D. 4
: E. 5' .. i . .-: .,, :

F.

;,j4^ ,: ; . -: j

:
5. SQL-?
. +

.]

D. *
F-/
6. ?
A. SELECT _ ALIAS
FROM _;
B. SELECT _
FROM _;
C. SELECT
FROM _;
D. ASSIGN _;


1 . .
, , ,

'

"':'

'

'

'

.*".

( )
, .
2. .

CREATE TABLE _ (
__1 _,
__2 _

" ", . 2.6, .


3. D.

.
, .

4. .

- , 2. , , .

58

, .
Oracle
.
5...

],{

*,().
6. .

+, -,

SELECT _
FROM _\

,
SELECT.

60.

.._.

'.::''.- ; ..- : '.- ' : ' * . . - - .

--

.
,
,
( ). ,
, , , DML, , INSERT,
UPDATE DELETE.



, , .
(: " ?" " , 100 30 ?").
SELECT WHERE. (conditions), .
:
SELECT FROM _ WHERE ();
, ,
;
30 ,
100 . .
, PLSQLIOIJPRODUCT, , :
DROP. TABLE plsqliaijproduct;
CREATE' plsqll01_product (
product_name VARCHAR2(25),
product_price NUMBER(4,2),
quantity_on_hand NUMBER(5,0),
last_stockdate DATE

INSERT INTO plsql!01_product VALUES


('Small Widget', 99, 1, '15-JAN-03');
INSERT INTO plsql!01_product VALUES
('Medium Wodget1, 75, 1000, '15-JAN-02');
INSERT INTO plsql!01_product VALUES
,,,rtr.; . J'Cjtirpme. Phoobar'.,.50, 100,, ' 15-JAN-03') ;
INSERT INTO plsq!101_product VALUES
('Round Chrome Snaphoo', 25, 10000, null);

61



. , , ( )
, , .

' ' " ' . , , : f- - - !

:; .

, 1, :
SELECT * FROM plsql!01_product
WHERE quantity_on_hand = 1;
. 3.1. , , , 25.
,
. , ,
, :
SELECT * FROM plsql'101_ptoduct
WHERE quantity_on_hand < 500;
. 3^2.
,
500. ,
,
", ". , , :
SELECT * FROM plsql!01_product
WHERE quantity_on_hand < 1000;
SELECT * FROM plsql!01_product
WHERE quantity_on_hand <= 1000;
* Ur.iclB SQL-Plus
ie Edit Search Qptions Help
SQL> SELECT F R O M plsqll Byproduct
2 WHERE quantity_on_hand - 1;
PRODUCT
Snail Widget

PRODUCT_PRICE QUflNTITV_OH_HAND LflST_STOC

99

1 15-JAH-03

SQL>

-iLJ

. .1. ,

62

TTTnTxl

& Oracle SQL-Plus


File Edit Search Options Help
SQL> SELECT * FROM plsqll 01 product
2 WHERE quantity_on_hand < 500;
PRODUCT_NflME

PBODUCT_PRICE QUflNTITV_ON HfiND LflST STOC

Snail Widget
Chrome Phoobar

99
58

1 15-JflH-83
100 15-JflN-l03

SIJL>

'

'
:

:'

:
;

: - . . ,

'-.'

::'..

'

I I

. .2. ,

. 3.3. Medium Wodget, 1000. , ,
1000, Medium WodgetB .
, ,
, ", " ", ". :

Oracle SQL'Plus
File dit Search Options Help
SQL> SELECT FROM plsql181_product
2 WHERE quantitp_on_hand < 1000;
PRODUCT NAME
Small Widget
Chrome Phoobar

PRODUCT_PRICE qUHNTITV_ON_HflND LflST_STOC


99
SO

1 15-JflN-03
100 15-JflN-03

SQL>
SQL> SELECT * FROM plsqll01_product
2 WHERE quantity_on_hand <= 1000;
PRODUCT NAME
Small Widget
Medium Wodget
Chrome Phoobar

PRODUCT_PRICE QUftNTITY_ON_HflND LflST_STOC

99
75
50

1 15-JRN-03
1000 15-JRN-02
100 15-JRH-03

SQL>

. 3.3. ,

63

SELECT * FROM plsql!01_product


WHERE quantity_on_hand > 1000;
SELECT * FROM plsql!01_product
WHERE quantity on hand >= 1000;

'

,.'

, . , . ,
: , .
: "AND". , :
SELECT * FROM plsq,1101_product
WHERE product_price >= 50
AND
product_price <= 100

AND
. Oracle , , : BETWEEN.
:
SELECT * FROM plsqllOljprdduct
WHERE product_price BETWEEN 50 AND 100;

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

, ? ", " ", " OR AND


. :
SELECT * FROM plsqll01_product '
WHERE product_price < 50
OR
product_price > 100
'"


BETWEEN, "NOT", :
SELECT * FROM plsql!01_product
WHERE product_price NOT BETWEEN 50 AND 100;

64

,
. . "<>",
, , , . ,
, :
SELECT * FROM PLSQK101_PRODUCT
WHERE PRODUCT_PRICE<> 99;


. Oracle : "!=" "". "" " ". :
SELECT * FROM PLSQL101_PRODUCT
WHERE PRODUCT_PRICE = 99;
SELECT * FROM PLSQL101_PRODUCT
WHERE PRODUCT_PRICE != 99;

, "=" ""
"!=" , Oracle , "=". ,
, .

, , , , ,
.
( ):
SELECT * FROM product
WHERE COLOR = 'Red1
OR
COLOR = 'Green 1
OR
COLOR = 'White'
/

, . ,
IN, (
):
SELECT * FROM product
WHERE COLOR IN, ('Red', 'Green', 'White')
/

, IN , , , . , IN
PLSQL101_PRODUCT. :

65

SELECT * FROM plsql!01_product


WHERE product_price IN (50, 99);

. ;..:ru


, , WHERE
, . , , WHERE,
( SQL-, ). :
SELECT * FROM,plsqll01_product
WHERE product_name = 'Small Widget';

, . 3.4.
IN, :
SELECT * FROM plsql!01_product
1
WHERE product_name = IN ('Small Widget', 'Round Chrome Snaphoo );

,
, "Chrome" (,
"Chrome Phoobar" "Round Chrome Snaphoo"). (wildcards), .
PLSQL101_PRODUCT,
"Chrome", :
SELECT * FROM plsqllOljproduct
WHERE product_name LIKE 'Chrome%';

Oiacle SQL'Plu
file Edit Search Options Help
SQL> SELECT * FROM plsqll B1 jroduct
2 WHERE product_nane - 'Small W i d g e t 1 ;
PRODUCT

Small Widget

PRODUCT_PRIGE QUflNTITV_ON_HflND LftST_STOC


1 15-JflN-C

SQL>

. .4. ,

66

, "Chrome" (%).
, , , : "
, ".
,
, "Chrome" . ,
"Chrome", . , "Chrome":
SELECT * FROM plsql!01_product
WHERE product_name LIKE '%Chrome%';

,
. 3.5. :
Oracle (
SELECT, select SeLeCt), ,
, . "Chrome"
, "chrome" "CHROME". 5 , .
% ,
% .
, .
(_).
. , "W", "d":

. Oracle SQL'Plus
File Edit Search Qptions Help
SQL> SELECT * FROM plsqH 81_product
2 WHERE product_name LIKE ' C h r o n e V ;
PRODUCT NAME

PRODUCT_PRICE QUftNTITV_OH_HflND LftST_STOC

50

Chrome Phoobar

100 15-JHN-03

SQL>
SJJL> SELECT * FROM plsqll 01_product
2 WHERE product_name LIKE '
PRODUCT NAME
Chrome Phoobar
Round Chrome Snaphoo

PRODUCT_PRICE QUftNTITV_ON_HflND LfiST_STOC

50
25

100 15-JflN-03
18000

SQL>

. .5.

67

SELECT * FROM plsql!01_product


WHERE product_name LIKE '%W_d%';



, . ,
,
, , .
, PLSQL101_PRODUCT , 15 2003 ., :
SELECT * FROM plsql!01_product
WHERE last_stock_date = '15-JAN-03';

, . 3.6.
Oracle , :
SELECT * FROM plsql!01_product
WHERE last_stock_date = 45-JAN-2003';

,
, , ", "
", ". :
SELECT * FROM plsql!01_product
WHERE last stock date > '31-DEC-02';

, ,

BETWEEN:
./
SELECT * FROM plsql!01_product
WHERE last_stock_date BETWEEN 'Ol-JAN-03' and '31-DEC-03';

NOT, ,
:

A. Oiacle SQL-Plus
File Edit Search Options Help
SQL> SELECT * FROM plsql101_product
2 WHERE last_stock_date 'IS-Jfl
PRODUCT NAME
Snail Widget
Chrome Phoobar

PRODUCT_PRICE QUflNTITY_ON_HftND LftST_STOC

99
SO

SQL>

JJLJ

. 3.6.

1 1S-JflN-B3
188 15-JflN-83

3
SELECT * FROM plsql!01_product
WHERE last stock date NOT BETWEEN 'Ol-JAN-031 and '31-DEC-03';

null-
, ,
,
. ,
null- , WHERE. Null- , , null.
null, WHERE IS NULL, :
SELECT * FROM plsql!01_product
WHERE last_stock_date IS NULL;

, , IS NOT NULL:
SELECT * FROM plsql!01_product
WHERE last_stock_date IS NOT NULL;

,
. 3.7.
IS NULL IS NOT NULL ; , .

Je * ech Qpfor Help


SQL> SELECT FROM plsqll01_prodUCt
2 WHERE last_stock_date IS HULL;
PRODUCT
Round Chrome Snaphoo

PRODUCT_PRICE QUftHTITV_OH_HAHD LflST_STOC


25

1B800

SQL>
SQL> SELECT * FROM plsqll 01_prodJCt
2 WHERE last_stoch_date IS NOT NULL;
PRODUCT

Small Widget
Medium Wodget
Chrome Phoobar

PRODUCT_PRICE QUflHTITY_ON_HflND LAST_STOC

99
75
SO

SQL>

. .7. null-

1 1S-JflN-03
1880 1S-JftN-02
180 15-JftN-83

69


, . , ,
.
,
, / . ,
.
, .
^ . . Oracle
, . (
, )
,
.


SELECT ORDER BY. , Oracle .
ORDER BY :
SELECT * FROM _ 1&_',

,,,,

, :
SELECT * FROM plsql!01_product ORDER BY product jprice;

. 3.8.
Otacle SQL'Plus
file Edit Search Qptions Help
SQL> SELECT * FROM plsqll81 product ORDER BY product_price;
PRODUCT

Round Chrome Snaphoo


Chrome Phoobar
Medium Uodget
Small Widget

PRODUCT_PRICE QUfiNTITY_ON_HflND LftST_STOC

25
58
75
99

10000
180 15-JflH-03
1000 1S-JflN-02
1 1S-JBN-03

. .8.

70

, SELECT
.

PLSQL101_PRODUCT

LAST_STOCK_DATE, :
.
Oracle , ? ORDER BY
SELECT .

, ORDER
BY, .
() ,

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

_; SELECT * FROM plsql!01_product


ORDER BY last_stock_date, product_name;

,
. 3.9. : .
.
: . -

A Oracle SQL-Plus
File Edit Search Options Help
SQL> SELECT FROM plsqll81_product
2 ORDER BV last_stoch_date, product_narae;
/PRODUCT_NflME
Medium Uodget
Chrome Phoobar
Snail Midget
Round Chrome Snaphoo

PRODUCT_PRICE QUflHTITV_ON_HflND LflST_STOC

75
58
99
25

SQL>

. .9.

1888 15-JftN-82
188 15-JflH-83
1 15-JflN-83

71

.
: , , , , . , :
,
( ), ..
PLSQL101_PRODUCT. :
SELECT

last_stock_date,
product_narae,
product_price,
quantity_on_hand
FROM
plsql!01_product
ORDER BY last_stock_date,
product_name

, . 3.10.
,
. , . , PLSQL101_PRODUCT ,
:

SELECT * FROM plsqllOl product ORDER BY product_price DESC;

HI3QI

4l Oracle SQL'Plus
file Edit Search Qptions Help
SQL> SELECT
last_stock_date,
product_name,
product_price,
quantity_on_hand
FROM
plsqll01_product

ORDER BV last_stock_date,
product_nane

LflST_STOC PRODUCTJiflME
15-JAN-02 Medium Uodget
15-JftN-03 Chrome Phoobar
15-JAN-03 Small Widget
Round Chrome Snaphoo

PRODUCT_PRICE QUflNTITV_ON_HflHD

75
50
99
25

1000
100

10000

SQL>

. 3.10. ,

72

, . :
I SELECT product_name FROM plsql!01_product ORDER BY quantity_on_hand;

. , , , .
, , ,
, .

, , ,
. , , ,
, . , , . , ,
. : "
?" " ?"
, ,
. ,
. , :
DROP TABLE plsql!01_purchase;
CREATE TABLE plsql!01_purchase (
product_name VARCHAR2(25),
quantity
NUMBER(4,2),
purchase_date DATE,
salesperson VARCHAR2(3)

INSERT INTO plsqll01_purchase VALUES


('Small Widget', 1, '14-JUL-03', 'CA');
INSERT INTO plsql!01_purchase VALUES
('Medium Wodget', 75, '14-JUL-03', 'BB')
INSERT INTO plsql!01_purchase VALUES
('Chrome Phoobar', 2, 44-JUL-03', 'GA').
INSERT INTO plsql!01_purchase VALUES
('Small Widget1, 8,. ' 15-JUL-03 ' , ''GA');
>JSERT-INTO
plsqll&l_purchase VALUES
;
('Medium Wodget', 20, '15-JUL-03', 'LB'),
NSERT INTO plsqlldl_purchase VALUES

73

('Chrome Phoobar', 2, 46-JUL-031, '');


INSERT INTO plsql!01_purchase VALUES
('Round Snaphoo', 25, '16-JUL-03', 'LB');
INSERT INTO plsql!01_purchase VALUES
('Chrome Phoobar', 2, '17-JUL-031, 'BB' );
;
SELECT DISTINCT, :
SELECT
FROM
ORDER BY

product_name
plsql!01_purchase
product name;

SELECT DISTINCT product_name


FROM
plsql!01_purchase
ORDER BY
product_name;
, , -

. 3.11.

1 |< Sea* Cptions Help

"

SQL> SELECT
product_name
2 FROM
plsqll81_purchase
3 ORDER BV product_nane;

'^

PRODUCTJfflME
Chrome Pboobar
Chrome Phoobar
Chrome Phoobar
Medium Uodget
Medium Uodget
Round Snaphoo
Small Widget
Small Widget
8 rows selected.

SQL>
SQL> SELECT DISTINCT product_name
2 FROM
plsqll81_purchase
3 ORDER BV
product_nane;
PRODUCT

Chrone Phoobar
Medium Uodget
Round Snaphoo
Small Widget
SQL>

. 3.11.

74

UNIQUE DISTINCT, :
SELECT UNIQUE product_name
FROM
plsql!01_purchase
ORDER BY
product_name;

DISTINCT UNIQUE ,
DISTINCT , .

. , ,
, :
SELECT DISTINCT
FROM
WHERE
ORDER BY

salesperson
plsql!01_purchase
purchase_date BETWEEN 'Ol-JUL-03' AND 45-JUL-031
salesperson;

DUAL
2 ,
, SELECT. (
, SALES_TAX):
SELECT
FROM
SELECT
FROM
SELECT
FROM

product_name, product_price + sales_tax


plsq!101_purchase;
product_name, 100 - product_price
plsqll01_purchase;
product_name, sales_tax / product_price
plsql!01_purchase;

SELECT , , ..
. , 18 5 :
SELECT 18*1.05 FROM plsql!01_purchase;

, ,
. ,
. Oracle
Oracle
DUAL, . ,
, :
DESC DUAL;
SELECT * FROM DUAL;

, DUAL ( DUMMY)
( X). DUAL .
"" , .
, :


SELECT 18*1.05 FROM DUAL;

75
. " . '

18.9,
. 3.12.
5
DUAL, , ,
.
,
,


Oracle .
UPDATE, :
UPDATE _ SET _ = _
WHERE ;
A Oiacle SQL'Plu
file Edit Search flptions Help
SQL> SELECT 18*1.85 FROM plsql181_purchase;
18*1.85
18.9
18.9
18.9
18.9
18.9
18.9
18.9
18.9

8 rows selected.
SQL> DESC DUAL;

Null?

Type
UftRCHflR2(1)

DUMMV
SQL> SELECT * FROM DUAL;
D
X

SQL> SELECT 18*1.85 FROM DUAL;


18*1.85
18.9

SQL> I

. 3.12. DUAL

76

, PLSQL101_PURCHASE "Small Widget" "Large Widget", :


SELECT * -FROM plsq!101_purchase;
UPDATE plsqll01_purehase t ;
SET
product_name = 'Large Widget'
WHERE product_name = 'Small Widget';
SELECT * FROM plsqllOljourchase;
PLSQL101_PURCHASEBce .
, . 3.13.

11

,-t Oracle SQL-Plus


8 'Search Options Help
SQL> SELECT FROM plsql101_purchase;
QUflNTITY PURCHASE_ SAL

PRODUCT NAME

Small Widget
Medium Uodget
Chrome Phoobar
Small Widget
Medium Wodget
Chrome Phoobar
Round Snaphoo
Chrome Phoobar

1
75
2
8
20
2
25
2

14-JUL-03
1U-JUL-83
-U-JUL-B3
15-JIIL-83
15-JUL-03
16-JUL-03
16-JUL-83
17-JUL-03

Cfl
BB
GO
Gfl
LB

LB
88

8 rows selected.
SQL>

SQL> UPDATE plsqliei_purchase


2 SET
product name - 'Large Widget1
3 WHERE productfnane - Small Widget';
2 rows updated.
SQL>
SQL> SELECT

FROM plsqliei_purchase;

PRODUCT NftHE

Large Uidget
Medium Wodget
Chrome Phoobar
Large Uidget
Medium Wodget
Chrome Phoobar
Round Snaphoo
Chrome Phoobar

QUflNTITV PURCHASE, SAL

1 ni-jUL-
75
2
8
20
2
25
2

1U-JUL-03 BB
1I|-JUL-B3 GA
15-JUL-03 GA
15-JUL-03 LB
16-JUL-03 CA
16-JUL-03 LB
17-JUL-03 BB

8 rows selected.
SO,L> |

. 3.13.

77

WHERE UPDATE.
, !


, , . DELETE
:
DELETE FROM _ WHERE ;
, .
, !


PLSQL101_PURCHASE. ,
, 15 2003 .:
SELECT * FROM plsqll01_purchase;

DELETE FROM plsql!01_purchase


WHERE purchase_date > 45-JUL-031;
SELECT * FROM plsqll01_purchase;

. 3.14.
,
( ). , ,
"Large Widget":

DELETE FROM plsqllOl_purchase


WHERE product_name = 'Large W i d g e t ' ;

, ,
Wodget Phoobar, 15 .


. : DELETE,
WHERE, TRUNCATE ().

, , :

DELETE FROM _;
, : , Oracle ,

78

* Oiacle SQL-Plus
Fte Edit Search Qptions 1
SQL> SELECT * FROM plsql181_purchase;
PRODUCT

NflME

Large Widget
Hediun Uodget
Chrome Phoobar
Large Widget
Medium Uodget
Chrome Phoobar
Round Snaphoo
Chrome Phoobar

QUANTITV PURCHftSE_ SflL


1 Ht-JUL-83 Cft
75 1U-JUL-83 BB
2 m-JUL-83 Gfl
8 15-JUL-ea Gfl
28 15-JUL-B3 LB
2 16-JUL-03
25 16-JUL-83 LB
2 17-JUL-B3 BB

8 rows selected.
SQL>
SQL> DELETE FROM plsqllB1_purchase
1
2 WHERE purchase_date > 'IS-JUL-OS ;
3 rows deleted.
SQL>
SQL> SELECT FROM plsql181_purchase;
PRODUCT_NftME
Large Widget
Medium Uodget
Chrome Phoobar
Large Midget
Mediun Uodget

QUANTITV PURCHftSE_ SflL


1
75
2
8
28

1U-JUL-83
1H-JUL-B3
Ht-JUL-83
15-JUL-B3
15-JUL-83

Cfl
BB
Gfl
Gfl
LB

SQL>

. 3.14.
WHERE.
, . ,
,
TRUNCATE.

TRUNCATE . Oracle , , . ,
, .
TRUNCATE :
TRUNCATE TABLE _;
, PLSQL101_PURCHASE,
, :

79

^ TRUNCATE TABLE pisql!01_purchase;


SELECT * FROM plsqll01_purchase;

TRUNCATE !
!


, , , , , .
, , .
, , Oracle
. : , (undo), Oracle.

DML
, , Oracle
. , ;
SELECT, .
.
, , , .

PLSQL101_PURCHASE, , :
CREATE TABLE plsql!01_purchase (
product_name VARCHAR2(25),
quantity
NUMBER(4,2),
purchase_date DATE,
salesperson
VARCHAR2(3)

'

Oracle ROLLBACK ().


, Oracle,
. , ,
:
'3 INSERT INTO plsql!01_purchase VALUES
('Small Widget1, 1, 44-JUL-03', 'CA');
INSERT INTO plsql!01_purchase VALUES
('Medium Wodget', 75, 44-JUL-03', 'BB');
SELECT * FROM plsql!01_purchase;
ROLLBACK;
SELECT * FROM plsql!01_purchase;

4 . 725

'

80

. 3.15. , SELECT ,
. ROLLBACK ,
. , .
ROLLBACK . , SAVEPOINT, .
SAVEPOINT :
SAVEPOINT __;
? , .
, - , ,
.

Be Edit Search Qptiora Help

SQL> INSERT INTO plsqll01_purchase VALUES


2
('Small Widget 1 , 1, 'lii-JUL-1. ' C O ' ) ;
1 row created.

SQL> INSERT INTO plsqll01_purchase VALUES


2
('Medium Wodgef . 75, '14-JUL-03*. 'BB');
1 row created.
SQL>
SQL>
SQL> SELECT * FROM plsqll01_purchase;
PRODUCTJMHE

QUANTI TV PURCHASE. SAL

Snail Uidget
Medium Uodget

1 1U-JUL-83 CA
75 14-JUL-03 BB

SQL>
SQL> ROLLBACK;

Rollback complete.
SQL>
SQL> SELECT * FROM plsqllM_purchase;
no rows selected,

SQL>

. 3.15.

81

, ,
.
,
, SELECT. :
INSERT INTO plsqll01_purchase VALUES
1
('Small Widget , I, 44-JUL-03', 'CA');
SAVEPOINT a;
INSERT INTO pls'qll01_purchase VALUES
('Medium Wodget', 75, 44-JUL-03', 'BB');
SAVEPOINT sp_2;
INSERT INTO plsql!01_purchase VALUES
('Chrome Phoobar', 2, 44-JUL-03', 'GA'); '
SAVEPOINT third;
INSERT INTO plsql!01_purchase VALUES
('Small Widget', 8, '15-JUL-03' , 'GA');
SAVEPOINT final_sp;
^,..,,,. :
INSERT INTO plsql!01_purchase VALUES
.-.,- ,:, ,
1
('Medium Wodget', 20, '15-JUL-03 , 'LB');
SELECT * FROM plsqllOl purchase-;';, \.
ROLLBACK TO"final_sp;
SELECT * FROM plsql!01_purchase; ...,, ..
ROLLBACK TO third;
SELECT * FROM plsql!01_purchase;
ROLLBACK TO sp_2;
SELECT * FROM plsql!01_purchase;
ROLLBACK TO a;
SELECT * FROM plsqllOljpurchase;
ROLLBACK;
SELECT * FROM plsql!01_purchase;

i. ,-

,
. ,
,
. ,
. ,
, .
,
: 30 ,
.
, , ,
. COMMIT ().
( ), ,
, . , , :
INSERT, INTO,,plsqllul_puxchase VALUES
('Small Widget', 1, '14-JUL-03', 'CA');
SAVEPOINT A;

82

INSERT INTO plsql!01_purchase VALUES


('Medium Wodget', 75, 44-JUL-03', 'BB');
SAVEPOINT B;
INSERT INTO plsql!01_purchase VALUES
1
('Chrome Phoobar', 2, '14-JUL-03 , 'GA');
SAVEPOINT C;
INSERT INTO plsql!01_purchase VALUES '
('Small Widget', 8, 45-JUL-03', 'GA');
SAVEPOINT D;
INSERT INTO plsqllOljpurchase VALUES
('Medium Wodget', 20, '15-JUL-03', 'LB');
COMMIT;
ROLLBACK TO D;
SELECT * FROM plsql!01_purchase;
, . 3.16. ,
ROLLBACK : Oracle ,
"D" , . ' , Oracle " " .


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

SQL'Plus
SELECT * FROM plsql101_purchase;


SQL*Plus
SELECT * FROM plsq!101_purchase;

INSERT INTO plsql101 jDurchase VALUES


('Round Snaphoo', 5, '16-JUL-031, 'CA');

SELECT * FROM plsql101 purchase;

COMMIT;

SELECT * FROM plsq!101_purchase;

, . 3.17.

83


| I I" 11111 1|;|.|

Oiacle SQL'Plu
File Edit Search Options Help
SQL> INSERT INTO plsqll81_purchase UflLUES
1
2
('Snail Widget', 1, 'lij-JUL- , 'Cfl');

fj

1 row created.
SQL> SAUEPOINT ;

Sauepoint created.
SQL> INSERT INTO plsqll01_purchase VALUES
2
Medium Uodgef , 75, 1i-JUL-e3 , 'BB');
1 row created.
SQL> SAUEPOINT B;
Sauepoint created.
SQL> INSERT INTO plsql101_purchase VALUES
1
2
('Chrome Phoobar', 2, 'Ui-JUL-03 , 'Gfl');
1 row created.
SQL> SAUEPOINT C;
Sauepoint created.
SQL> INSERT INTO plsqll81 purchase UALUES
2
('Small Widget'7 8, '15-JUL-03', 'EA');
1 row created.
SQL> SAUEPOINT D;
Sauepoint created.
SQL> INSERT INTO plsqll81_purchase UALUES
2
(-Medium Wodgef , 28, 'IS-JUL-', 1);

1 row created.
SQL>
SQL> COMMIT;
Commit complete.
SQL>
SQL> ROLLBACK TO D;
ROLLBACK TO D
*
ERROR at line 1:
..
ORA-81086: sauepoint D' neuer established

SQL>
SQL> SELECT FROM plsqll81 purchase;
PROOUCT_NAME
Snail Widget
Medium Wodget
Chrome Phoobar
Small Widget
Medium Uodget

QUANTITV PURCHASE_ SAL


1
75
2
8
2

1i-JUL-03
14-JUL-83
14-JUL-03
15-JUL-B3
1S-JUL-83

CA
BB
GA
GA
LB

SQL> |

. 3.16.

, .,

84
_0_1

-M-H_u_3L

SgL> select fro PLSQL1 .PURCHASE;


PROOUCT.NAHE

QUANTITV PURCHASE. SAL

SHll Widget
Hediun Wodget
Chrone Phoobar
SHll Widget
Medium Wodget

1
75
2
1
21

sgL>
sgi>
SQL>
sgi>
sqi>

H-JUL-
1-JUL-3
H-JUL-II3
15-JUL-n
15-JUL-03

;
;:

Eh . S*" awioni 'fcW>'

SQL>
SOL>

. : ' ' . - . ' ' .

sgL>

PROOUCT.NAHE

sgL>

SQL) insert into PLSQL1H PURCHASE ualue (


2
'Round SnaphoC, 5, '16-JU--2M3- . 'CA');

sgL>
sgt>
sgi>

;;

IQL>
SQL>

1 ro created.

gUANTITV PURCHASE. SAL


1
75
2
1
21

SQL>

..........
,

, . ,

__

SHll Widget
Nediu Wodget
Chron* Phoobar
SHll Widget
Hediun Wodget

1
75
2
1
21

.' ;

:
.- ;

PLSgL1H_PURCHRSE:

'RODUCT NAME

QUANTITY PURCHASE. SAL

SHll Widget
HediuH Wodget
Phoobar
SHll Widget
Nediu Wodget
Round Snaphoo

' - ' '" ( *


,

1 1J.-JUL-03 CA
75 K-JUL-n BB
2 1-JUL-3 GA
1 15-JUL-03 CA
21 15-JUL-03 LI
5 16-JUL-03 CA

6 rows selected.

f.

CA
II
CA
CA
LI

$gt>
SQL>

SQL> seltct FroH

H-JUL-
1-JUL--3
-U-JUL-n
15-JUL-U
15-JUL-n
. I, ,

\4L>

sgL>

.ii

'/ .

QUANTITY PURCHASE. SAL


. .. .
.
.
.
.
.

Comit couple tt.

:^

CA
BB
CA
CA
LI

SQ->

PRUDUCT.NAME
.
.
.
.
.
.
.
.
.
.
.
.
.
.

sgL> con.it;

1J.-JUL-03
1-JUL-3
1-JUL-e3
15-JUL-B3
15-JUL-.3

SQL> select Fro rLSQL1l1.PURCHASE;

SL>

sgL>
sgL>
ML
sgL>

> ' ' ''.' :

sgi> select i Fran PLSQI1I1_P-RCHASE;

sgi>
sgL>

JQL>

.: ' .

HP

SHll Widget
Mediun Wodget
Cliron Phoobar
SHll widget
H-diun Wodget

SPL>
'SL>

j
. '

',

SQL)
SQL>
SQL>

CA
IB
CR
CA
LI

. ,;; "

sgL>
sgi>

. :'' .-:

: /- :

;fi

->
I I

.;'S"

"".;",>

':-

'

"-.-.".

.iH

. 3.17. COMMIT


, COMMIT.
. Oracle , , .. . ,
DDL (, CREATE TABLE DROP TABLE)

. Oracle ( SQL*Plus)
.

. ,
WHERE S ELECT

85

, Oracle. , =, !=, >, <, , >=, <=, BETWEEN


NOT. , AND, "/", OR. , ,
OR .
"_" ( ) "%" ( ), LIKE. ,
, , WHERE
IS NULL IS NOT NULL.
SELECT ,
, ,
ORDER BY. ( ),
. (, ORDER BY
, ,
.) ,
, , . . , .
, , ,
. , DISTINCT SELECT.
, , DUAL.
, UPDATE. , ,
, ,
. WHERE ,
! DELETE: ) WHERE,
. , TRUNCATE TABLE, ;'
Oracle , " ". ROLLBACK, DML (INSERT, UPDATE DELETE),
.
COMMIT.
DDL ( , CREATE DROP),
SQL*Plus.

, , DML . SAVEPOINT,
, ROLLBACK. ;

86

.. .

.
, Oracle,
. ( . , - , ,
, .
, .)
SQL*Plus. ,
, , .
.

1. " "?
A. DML, Oracle , .
B. , Oracle ,
.
C. DML, Oracle , .
D. , , Oracle
.
2. ?
A. WHERE'Smith1

. ' ' .

B. WHERE 'Job_Descriptiori = 'Manager'

C. WHERE SaLaRy - SYSDATE


D. WHERE LASTJSTAME BETWEEN'K' AND 9
E. WHERE HIREDATE BETWEEN '02-JAN-02' AND 'Ol-JAN-011
F. WHERE PRICE LIKE 10%
3. ?
A. , COMMIT, ROLLBACK
B. ROLLBACK, , COMMIT
C. SAVEPOINT, , ROLLBACK,
ROLLBACK, COMMIT
D. ROLLBACK, , COMMIT, COMMIT
4.
?


"

, ;;";

87
. ' / . . . ; - ' ' - :

- "

.?,*
. _, %

.?,_
D. ?,%
E. %,

,'

'

. ',

- : , ' ; , '

F.M

.*,?

. *, %
5.
?
UPDATE _
WHERE _ =
SET _ _
ORDER BY _

A. 1
B. 2

C. 3
D. 4
E.


1 . . DML, Oracle , .

SAVEPOINT
DML,
ROLLBACK.
2. .

WHERE HIREDATE BETWEEN '02-JAN-02' AND 'Ol-JAN-

"Smith" .
.
, (), , ( , "salary" , ,
). D , BETWEEN, ,
. F
LIKE , .

3
3. , , .

, COMMIT, ROLLBACK SAVEPOINT,


, ROLLBACK, ROLLBACK, COMMIT
ROLLBACK, , COMMIT, COMMIT

ROLLBACK
COMMIT, . ROLLBACK. D
.
4. .

_, %

" ", .

5. . ,V*; i-
UPDATE SET
WHERE.

'

!.-

;-

$ &$?

SQL'Plus

90

D ,
SQL*Plus. ,
; SQL*Plus "
", ; SQL*Plus , ; , SELECT;
; , ,
, . , ! , ,
.


SQL- ,
. SQL*Plus

, .
, , ,
.


SQL-,
, , SQL- ,
. , ( )- ..
. SQL> EDIT (
, ED) SQL* Plus , ,
SQL-. , ""
SQL*Plus, .

. ,
" EDIT". ,
SQL-.
DROP TABLE plsqlldl_product;
CREATE TABLE plsqll,01_product '.{.
product_name
VARCHAR2 (-25) ,
product_price
NUMBER(4,2),
quantity_on_hand NUMBER(5,0),
last_stock_date DATE

SQL* Plus

91

INSERT INTO plsql!01_product VALUES


('Small Widget', 99, 1, '15-JAN-03');
INSERT INTO plsql!01_product VALUES
('Medium Wodget1, 75, 1000, 45-JAN-02 ' ) ;
INSERT INTO plsql!01_product VALUES
('Chrome Phoobar', 50, 100, '15-JAN-03');
INSERT INTO plsql!01_product VALUES
('Round Chrome Snaphoo1, 25, 10000, null);'
;,-::,
DROP TABLE plsqllOljpurchase;
CREATE TABLE pisql!01_purchase (
product_name
VARCHAR2(25),
salesperson
VARCHAR2(3),
purchase_date DATE,
quantity
NUMBER(4,2)

INSERT INTO plsql!0.1_purchase VALUES


('Small Widget', 'CA', '14-JUL-03', 1);
INSERT INTO plsql!01_purchase VALUES
('Medium Wodget', 'BB1, '14-JUL-03', 75);
INSERT INTO plsql!01_purchase VALUES
('Chrome Phoobar', 'GA', 44-JUL-03', 2);
INSERT INTO plsqll01_purchase VALUES
('Small Widget', 'GA', '15-JUL-03', 8);
INSERT INTO plsql!01_purchase VALUES
('Medium Wodget1, 'LB1, 45-JUL-03', 20);
INSERT INTO plsql!01_purchase VALUES
('Round Snaphoo', 'CA1, 46-JUL-031, 5);

EDIT

, EDIT, :
1. . , .
SELECT
FROM
WHERE

product_nmae
lsqll01_prod U ct
quantity_on_hand >= 100
AND
last_stock_date IS NOT NULL
ORDER BY product_name;
P

2. SQL*Plus "ORA-00942: table or view


does not exist". Oracle , ,
, .
,
, ...
3. edit ENTER. , , . 4.1. (

92

afiedt.bul - Notepad

ffc Ed Swtch Help


SELECT
FROM
WHERE

product_nnae
plsq!101_produtc
quantity_on_hand >- 101
AND

last_stock_date IS NOT NULL


ORDER BV product name

. 4.1. EDIT SQL-


. , Windows- Notepad, Unix- ED
VI.)
afiedit.buf. , ' .
4. 4 1, 2.'
5. ( ,
File | Exit). .
, ( )
, SQL*Plus. Yes
.
6. SQL*Plus.
, (/)
ENTER.
7. , , .


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

SQL-,
.

CHANGE

, . :

93

SQL*Plus
1. . ,
.
SELECT product_nmae FROM plsql!0l_product;

*''-

2. ,
PRODUCT_NMAE (*). , , .
3. :
change/nmae/name

ENTER, CHANGE.
, SQL*Plus , .
4. (/)
ENTER.
5. , . 4.2.
* Oiocle SQL-Plus

Ed* Search "Options "


.;.:.'". ' :
SQL> SELECT product nnae FROM plsql! Byproduct;
SELECT product_nnae FROM plsql181_product
*
ERROR at line 1:

ORA-0B9QJ: Invalid colunn name

SQL> change/nnae/name

1* SELECT product_nane FROM plsql1B1 product


SQL> /
PRODUCT NflME

Snail Widget
Hediun Wodget
Chrome Phoobar
Round Chrome Snaphoo
SQL> |

. 4.2. CHANGE
, CHANGE
. CHANGE
SQL; SQL*Plus.

"CHANGE", "".
; "" .

94

4
CHANGE :
C[HANGE] _ _
\_ _]

. , , ( ,
.) , . ENTER, ,
. - , , .
. SQL-, ,
CHANGE. SQL- .

EDIT CHANGE .
CHANGE , , EDIT. ,
,

, CHANGE
.
CHANGE, , .
CHANGE. SQL* Plus,
.
, . , ,
:
SEbECT
FROM
WHERE

product_nmae
plsql!01_produtc
quantity_on_hand >= 100
AND
last_stock_date IS NOT NULL
ORDER BY product_name;

c/ma/am
2

c/tc/ct

. 4.3. , SQL*Plus CHANGE-.

SQL* Plus

95

Oracle SQL'Plus
File Edit Seaich Qptions Help
SQL> SELECT
pi-oduct_nmae
2 FROM
plsql181_pi-odutc

3 WHERE
t

quantity_on hand >- 188


AND

5
last_stock_date IS NOT NULL
6 ORDER BV product_nane;
FROM
plsqliei_produtc

ERROR at line 2:
-091|2: table or uieu does not exist
SQL>
SQL>
1*
SQL>
1
SQL>
2
SQL>
2*
SQL>

1
SELECT
c/na/an
SELECT
2
FROM
c/tc/ct
FROM
/

product_nmae
product_name
plsql1B1_produtc
plsqll01_product

PRODUCT NfltlE

Chrome Phoobar
Medium Uodget
SQL>

. 4.. CHANGE


SQL-, ,
. CHANGE EDIT
,
SQL*Plus. . SQL*Plus , (paste) SQL>. ,
, :
SELECT * FROM plsql!01_product;
UPDATE plsql!01_product
SET
product_name = 'Large Widget1
WHERE product_name = 'Small Widget';
UPDATE SELECT. . "S" "SELECT".
, -

4
. , Edit SQL*Plus.
, Windows. Edit | Paste,
SQL*Plus. ENTER . , . 4.4.
Paste (shortcuts) . , SQL*Plus Windows, ,
CTRL, "" , CTRL-V .
.
UPDATE, . :
1. "U" "UPDATE"
.
2. .
3.
UPDATE.
.* Oiacle SQL-Plus
File Edit Search Options Help
SQL> SELECT * FROM plsqll01_product;
PRODUCT_NAME

PRODUCT_PRICE QUAHTITV_OH_HAHO LAST_STOC

Snail Uidget
Medium Uodget
Chrome Phoobar

99
75
50

Round phrone Snaphoo

25

1 15-JAN-03
1000 15-JAN-02
100 15-JAN-03

10000

SQL>

SQL> UPDATE pisqnoi_product


2

SET

product name = 'Large Uidget'

3 WHERE producO>ane - 'Snail Uidget';

1 row updated.
SQL> SELECT * FROM plsqll Byproduct;
PRODUCT_HAME

PRODUCT_PRICE QUANTITV_ON_HAND LAST_STOC

Large Uidget

99

1 i5-JflN-B3

Medium Uodget
Chrome Phoobar
Round Chrome Snaphoo

75
50
25

1000 15-JAN-02
100 15-JAN-03
10000

SQL> |

. 4.4. SQL-

SQL*Plus

97

4. , .
SQLX
5. ENTER
SQL> ( "2").
6. "S" "SET"
UPDATE.
.' .
'.
7. .
8. ,
"L" "Large".
"Large", "L".
9. ,
.
.
10. Small .
11. UPDATE
, .
12. ENTER
SQL>.
13.
UPDATE, "Small" "Large".
14. ENTER .
,
SQL*Plus.
, , SQL>.

SQL*Plus
, , , SQL-.
SQL*Plus , ? . SHIFT, , , DELETE. ,
. 4.5. ., SQL*Plus SQL>.

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

98

Oracle SQL-Plus
Are you sure you want to clear the Screen and the Screen Buffet?

Cancel

O'K"

. 4.5. SQL*Plus

SQL*Plus
SQL*Plus, Options | Environment. Environment (),
. 4.6. , ,
SQL*Plus. SQL*Plus 100 1000 . Environment
1000 ( ),
2000 ( , , , ).
,
, SQL*Plus, .
, 1000 Buffer Width
2000 Buffer Length.
Environment, Set Options: linesize ( ) pagesize ( ). Linesize ,
SQL*Plus . ,
Environment

Set Options
arraysize
autocommit
autoprint
autotrace
blockterminator
cmdsep
colsep
compatibility
neat
copycommit
copytypecheck

'U

jraiuu
Delault
current

Buffer Width:

J100

Buffer Length:

J1000

j
,

....

i
1

On

f Off
1
1

Cancel

. 4.6. SQL*PluS

OK

SQL* Plus

99

, SQL*Plus
,
. . 4.7 .
, ...
. SQL*Plus , , , .
linesize Buffer Width, .
Linesize , , Buffer Width
, . , , , .
linesize Buffer Width,
Set Options , linesize. ,
Current Value.
Value . 1000.
, Environment.

Oiacle SQL-Plus
File Edit Search Options Help
SQL> select * from PLSQL101_PRODUCT;
PRODUCT

PRODUCT_PRICE QUaNTITV_ON_HflND

LflST_STOC

99

Small Widget
15-JflN-03

'',':
Medium Uodget
15-JflN-02

Chrome Phoobar
15-JflN-83
PRODUCT_HftME

75

1000
'

50

"

100

PBODUCT_PRICE QUflNTITY_ON_HAND

LflST_STOC

Round Chrome Snaphoo

25

SQL>

>

. 4.7.

100

,
. 4.8, . 4.7.
, pagesize, ,
SELECT , SQL*Plus . ,
. pagesize 9999,
. , 9999 pagesize.
Environment,
, .


, Set Options Environment,
SQL*Plus.
SQL> :
SET LINESIZE 1000
SET PAGESIZE 9999


SQL*Plus ,
. ,
SQL*Plus,
SQL*Plus. ,
:
1. Oracle (Oracle home)
.
* Oracle SQL'Plus
>'
File Edit Sech Qpttons Help
SQL> select from PLSQL101_PRODuCT;
PRODUCT_NAME
Small Widget
Medium Uodget
Chrome Phoobar
Round Chrome Snaphoo

.d

PRODUCT_PRICE QUANTITV_ON_HAND LflST_STOC


99
75
50
25

1 15-JAN-03
1000 15-JAN-02
100 15-JAN-03
10000

SQL>

-iLJ

. 4.8. linesize

SQL*Plus

101

Oracle. Windows NT,


Windows Explorer (Start | Programs | Windows NT Explorer) , "OraNT". SQL*Plus
Windows 95,98, 2000 Windows, , "ORAWIN95".
.
2. SQL*Plus :
STORE_SET :1_!\8\1. APPEND

:1_
Oracle . ,
,
SQL*Plus Oracle.
STORE SQL*Plus , , login.sql,
DBS Oracle,
SQL*Plus. ,
SQL*Plus.


SQL*Plus
, , SQL*Plus
.
, , , .
SQL*Plus.
COLUMN. STORE, Oracle. SQL*Plus.
COLU MN ,
SQL. , SQL*Plus.
SQL*Plus,
.
COLUMN, ,
. :
INSERT INTO plsqll01_product VALUES (
'Extra Huge Mega Phoobar +',
9.95,
1234,

102


:

, ..

,
.

COLUMN, ,
:
COLUMN _ FORMAT _
_ ,
. : ,
! COLUMN , , .
, , , ,
, .
_ . "9" ,
, "."
. (
, "D". .)
, COLUMN, :
SELECT * FROM plsqll01_product;
COLUMN product_price FORMAT 9999.99
SELECT * FROM plsqll01_product;

, . 4.9.
, PRODUCT_PRICE , COLUMN .

(group separator) , , .. . QUANTITY_ON_HAND


PLSQL101_PRODUCT , , .
COLUMN, ;
. :
COLUMN quantity_on_hand FORMAT 99,999
SELECT * FROM plsql!01_product;

SQL*Plus

103

* Oiacle SQL'Plus

BflLoOL^J

^ite Edit Search Options Help !


SQL> SELECT FROM plsq!101_product ;
PRODUCT_HAME

PRODUCT_PRICE QUANTITV_ON_HflND LAST_STOC

Small Widget
Medium Uodget
Chrome Phoobar
Round Chrome Snaphoo

99
75
50
25

1 15-JflN-03
1008 15-JAH-02
100 15-JAH-03
10000

. ,
'

SQL>
SQL> COLUMN product price FORMAT 9999.99
SQL>
SQL> SELECT * FROM plsqll Byproduct ;
PRODUCT_NAME
Small Uidget
Medium Wodget
Chrome Phoobar
Round Chrome Snaphoo

PRODUCT_PRICE QUANTITV_ON_HflND LAST_STOC


99. BB
75.08
50.08
25. OB

1 15-JAN-83
1000 15-JAN-02
188 15-JAN-03
18888

SQL>
.

'.

..

.-

,.

'

&"

^iJ

-~J M

. 4.9. COLUMN

QUANTITY_ON_HAND
.

, .
, ($)
PRODUCT_PRICE:
COLUMN product_price FORMAT $99.99
SELECT * FROM plsqllOljproduct;
-.

4.1 ,
. , . RN
.


SQL*Plus ,

. , -
. SQL*Plus
. COLUMN :

104

4.1.
;

$9999

,()

9,999

. ()

99.99

Ml

9999MI

(-)

(+) (-)

PR

9999PR

()

99D99

9G999

999


ISO

L999

RN rn

RN

( 1 3999)

0999

COLUMN _ FORMAT Ann WORD_WRAP


, , _ , .
, , ( ) .
"" "-" ("alphanumeric").
:
SELECT * FROM plsqllOljproduct;
COLUMN product_name FORMAT AID WORD_WRAP
SELECT * FROM plsqll01_product;

SQL*Plus

105

, . : " ". .
, 30, 40,
50,100 . , ?
? , .
, , SQL*Plus.


, . ,
,
. COLUMN. :
COLUMN _ HEADING '_'JUSTIFY LEFT

COLUMN _ HEADING '_'JUSTIFY CENTER


*

COLUMN _ HEADING '_ 'JUSTIFY RIGHT


, . ( | ) _
. , : , , .
:
SELECT * FROM plsqll01_product;
COLUMN product_name HEADING 'Product I Name' JUSTIFY CENTER
SELECT * FROM plsqll01_product;

COLUMN, . , , . 4.10.
SELECT * FROM plsqllOljproduct;
COLUMN product_name FORMAT A10 WORD_WRAP HEADING 'Name'
JUSTIFY CENTER
COLUMN product_price FORMAT $99.99 HEADING 'Price' JUSTIFY RIGHT
COLUMN quantity_on_hand FORMAT 99,999 HEADING 'On|Hand'
JUSTIFY RIGHT
COLUMN last_stock_date HEADING 'Last|Stock|Date' JUSTIFY RIGHT
SELECT * FROM plsql!01_product;

. ,.

106

Oiacle SQL-Plus
File Edit Seach Options Help
SQL> SELECT * FROM plsq!1B1_prodUCt;
Product
Name

PRODUCTJ>RICE QUANTITV_ON_HAND LAST_STOC

Snail
Widget

$99.00

1 15-JAN-B3

Mediun
Wodget

$75.

1,888 1S-JflN-82

Chrome

$58.80

188 15-JAN-B3

Round

$25.80

Phoobar
Chrome
Snaphoo

1(1,880

SQL>
SQL>
SQL>
SQL>
SQL>
SQL>
SQL>

COLUMN
COLUMN
COLUMN
COLUMN

product nane FORMAT A10 WORD_URAP HEADING 'Name' JUSTIFV CENTER


product_price FORMAT $99.99 HEADING 'Price1 JUSTIFV RIGHT
quantity_on_hand FORMAT 99,999 HEADING 'On|Hand' JUSTIFV RIGHT
last_stock_date HEADING 'Last(Stock(Date' JUSTIFV RIGHT

SELECT FROM plsq!181_product;

Nane

Price

On
Hand

Last

Stock
Date

Small
Widget

$99.BB

1 15-JAN-B3

Mediun
Uodget

$75.88

1,888 1S-JAN-02

Chrome
Phoobar

$58.88

188 15-JAN-B3

Round
Chrome
Snaphoo

$25.BO

18,088

SQL>
JJJ

. 4.10. COLUMN
, COLUMN, :
COLUMN _ OFF
, PLSQL101_PRODUCT
, , :

SQL'Plus
COLUMN
COLUMN
COLUMN
COLUMN

107

product_name OFF
product_price OFF
quantity_on_hand OFF
last_stock_date OFF

SELECT * FROM plsqllOl product;



(spooling) .
SQL*Plus,
.
( SQL*Plus,
, ,
. ,
, .)
SPOOL :
SPOOL __
(, .sql .). , .1st. ,
(path), .. ,
. , BIN Oracle.
, ,
. , SPOOL ,
SQL*Plus Oracle.
SPOOL c:\plsqll01_test.prn
SELECT * FROM plsql!01_product;
SELECT * FROM plsql!01_purchase;
SPOOL OFF
\

SQL *Plus Unix,


:
/uO 1/user/plsql 101_test. prn
( , Unix
.)
Windows Explorer File Manager , plsqll01_test.prn.
, , SQL*Plus .

108

SQL

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


(script file) .
. ( Save As
"text only", .) EDIT SQL*Plus,
. , , :
1. SQL*Plus :
EDIT c:\plsqll01_test.sql

Unix
.
,
.

2. , plsq!101_test.sql :
CREATE TABLE plsql!01_temp (
first_name VARCHAR2(15),
last_name VARCHAR2(25)

INSERT INTO plsqll01_temp VALUES ( ' J o e ' , ' S m i t h ' ) ;


INSERT INTO plsq!101_temp VALUES ( ' J a n e ' , ' M i l l e r ' ) ;
SELECT * FROM plsq!101_temp;
DROP TABLE plsql!01_temp;

3. .
Yes.

SQL*Plus

109

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


SQL*Plus .
"at" (@). SQL>
:

@:\plsqll01_test
. 4.11. *
, .sql. , , .sql.


,
. (variables). ,

mwss

- Oiacle SQL-Plus
File dit

Search

Options

Help

SQL> EDIT c:\plsql101_test.sql

SQL> Bc:\plsqliei_test
Table created.

1 row created.
1 row created.
FIRST NAME

LAST NAME

Joe
Jane

Smith
Miller

Table dropped.
SQL>

. 4.11. SQL

110

. ( , . (hard-coded),
.) SQL :
ACCEPT.

(substitution variable)
. , , plsql 101_test2.sql :
SET VERIFY OFF
SELECT product_name, quantity, purchase_date
FROM
plsql!01_purchase
WHERE quantity >= &minimum_quantity_sold

SET VERIFY ON
. minimum_quantity_sold ("Enter value for mmimum_quantity_sold"). WHERE. 20
, . ( (/) ENTER)
5, , .
SET VERIFY OFF SETVERIFY ON . SQL*Plus SELECT,
, ,
.

. , SQL . ,
, ,
. :
SET VERIFY OFF
SELECT product_name, quantity, purchase_date
FROM
plsql!01_purchase
WHERE purchase_date = '&date_you_want_to_select'
SET VERIFY ON

, . , , .

SQL* Plus

111

plsqll01_test3.sql
. ( 14-JUL-03, 15-JUL-03
16-JUL-03.)
, , . BETWEEN
.

ACCEPT

, , SQL*Plus , , . ACCEPT,
. :
accept _prompt ''
, ,
plsq!101_test4.sql :

SET VERIFY OFF


SET ECHO OFF
ACCEPT v_earliest_date
PROMPT 'Earliest date you would like to see? (dd-mmm-yy): '
ACCEPT v_latest_date
PROMPT 'Thank you. Latest date you would like .to see?
(dd-mmm-yy): '
SELECT product_name, quantity, purchase_date
FROM
plsql!01_purchase
WHERE purchase_date BETWEEN '&v_earliest_date' AND '&v_latest_date'
ORDER BY product_name, quantity
SET VERIFY ON
SET ECHO ON

: SET ECHO OFF SET


ECHO ON. SET ECHO
. , ACCEPT, .

, SQL*Plus. ,
. ED, , CHANGE,
. , ,
, SQL*Plus .
5 . 725

112

, , SQL*Plus, SHIFT-DELETE. SQL*Plus,


Options | Environment, ,
SQL*Plus. , STORE.
COLUMN, , ,
. ,
SQL*Plus, SPOOL.

SQL
.sql, . SQL*Plus @,
, . ,
ACCEPT, ( )
.
. !
, .
. , SQL.

1. ED SQL*Plus?
A. Oracle
B. ,
C. EDIT
D. VI
2. ?
A. COLUMN
B. SET LINESIZE
C. SPOOL
D. SET PAGESIZE
3.
?
SELECT product_name, quantity, purchase_date
FROM
plsql!01_purchase
WHERE quantity <= &maximum_quantity_sold;

A. 1

'

'. ' ' '.' :

'

:.-/:' H7 '

SQL*Plus

113

. 2
C. 3
D.
4.
?
SET VERIFY OFF
".;
SET ECHO OFF
ACCEPT v_earliest_date PROMPT 'Earliest date? (dd-mmm-yy):. '. ACCEPT v_latest_date PROMPT 'Latest date? (dd-mmm-yy): ' ,.
SELECT product_name, quantity, purchase_date
FROM plsql!01_purchase
WHERE purchase_date BETWEEN '&earliest_date' AND 'Sla-'
test_date'
ORDER BY product_name, quantity;
SET VERIFY ON
SET ECHO ON

A. 1
B. 3
C. 5
'

'

- :

-'

--

- .

D. 7

E. 9


1. .

Oracle . ED
,
.
2. .

SPOOL

SPOOL
. .
3. .

,
SELECT, & . , (maximum_quantity_
sold) . . .

4. D.

, ,
, ACCEPT.


SQL

- .


SQL

118 _

SQL,
Oracle , . 6 , , 7
SQL, .
,
SQL-. -, SQL-. ,
, . , (,
),
(,
). , ,
SQL, , SQL .
: . , .
, .
,
,
. SQL-.
DROP TABLE plsql!01_product;
CREATE TABLE plsql!01_product (
product_name
VARCHAR2 ( 2 5 ) ,
product_price
NUMBER (4, 2),
quantity_on_hand NUMBER (5, 0) ,
laststockdate
DATE

INSERT INTO plsql!01_product VALUES


('Small Widget', 99, 1, ' 15-JAN-03 ' ) ;
INSERT INTO plsql!01_product VALUES
('Medium Wodget', 75, 1000, ' 15-JAN-02 ' ) ;
INSERT INTO plsql!01_product VALUES
('Chrome Phoobar ' , 50, 100, ' 15-JAN-03 ' ) ;
INSERT INTO plsql!01_product VALUES
('Round Chrome Snaphoo', 25, 10000, null);
.INSERT INTO plsq!101_product VALUES
('Extra Huge Mega Phoobar +', 9.95, 1234, ' 15-JAN-04 ' ) ;
DROP TABLE plsql!01_purchase;
CREATE TABLE plsql!01_purchase (
product_name
VARCHAR2 (25) ,
salesperson
VARCHAR2(3),

SQL
purchase_date
quantity

119

DATE,
NUMBER(4,2)

INSERT INTO plsql!01_purchase VALUES


1
('Small Widget , 'CA', '14-JUL-03', 1);
INSERT INTO plsql!01_purchase VALUES
('Medium Wodget', 'BB', '14-JUL-03', 75);
INSERT INTO plsql!01_purchase VALUES
('Chrome Phoobar', 'GA', 44-JUL-03', 2);
INSERT INTO plsql!01_purchase VALUES
1
('Small Widget , 'GA', '15-JUL-03', 8);
INSERT INTO plsq!101_purchase VALUES
('Medium Wodget', 'LB', '15-JUL-03', 20);
INSERT INTO plsql!01_purchase VALUES
('Round Snaphoo', 'CA', '16-JUL-03', 5);



,
. :

.
()


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

SYSDATE

SYSDATE . , ,
Oracle, , . , , :
SELECT SYSDATE FROM DUAL;

120

. DML. , SYSDATE INSERT, ,


:
INSERT INTO plsql!01_purchase VALUES':
1
('Small Widget , 10, sysdate, 'SH');

PLSQL101_PURCHASE SELECT, , PURCHASE_DATE. (


, ^ , .)
, INSERT. :
INSERT INTO plsql'101_purchase VALUES
('Medium Wodget', 15, sysdate-14, 'SH');
INSERT INTO plsql!01_purchase VALUES
('Round Snaphoo', 25, sysdate-7, 'SH');
INSERT INTO plsq!101_purchase VALUES
('Chrome Phoobar', 10, sysdate+7, 'SH');

, ,
, . , 7 SYSDATE, , .
SYSDATE , . ,
30 ,
:
SELECT * FROM plsql!01_purchase
WHERE purchase_date .BETWEEN (SYSDATE-30) AND SYSDATE;

. 5.1.
' SYSDATE . , , ,
. ,
, :
DELETE FROM plsql!01_purchase
WHERE SALESPERSON = 'SH1;

USER
USER, ,
.
, USERENV. ,
, .. , , . ; , , 9. ,
, . ,
.

SQL

121

A Oiacle SQL'Plus
Je d Search Qptions Help
SQL> SELECT FROM plsqllB1_purchase
2 UHERE purchase_date BETWEEN (SVSDATE-30) ftND SVSDfiTE;
PRODUCT

QUflNTITV PURCHASE. SflL

Snail Uidget
Medium Uodget
Round Snaphoo

10 05-JUL-BO SH
15 21-JUN-B8 SH
25 28-JUN-Oe SH

SQL>

. 5.1. SYSDATE WHERE


USER Oracle,
, . ,
:
SELECT USER FROM DUAL;

, SQL*Plus. , , ,
,
.

USERENV

USERENV , . , .
, , :
SELECT USERENV('TERMINAL') FROM DUAL;

.
USER ,
. SYSDATE,
.


, . .
, ,
. .
,
, .

122

ROUND

ROUND .
:
ROUND(exodHoe_3Ha4emie,

____)

ROUND ( , ) , . SELECT,
, .
. PLSQL101JPRODUCT . ( )
. , ROUND,
:
SELECT
FROM
SELECT
FROM

product_name, product_price
plsql!01_product;
product_name, ROUND(product_price,
plsql!01_product;

0)

, . 5.2.

ROUND.
, , . , 2. * Oracle SQL-Plus
File Edit Seaich Options Help
SQL> SELECT product_nane, product_price
2 FROM
plsql1B1_product;
PRODUCT NflHE
Small Widget
Medium Wodget
Chrome Phoobar
Round Chrome Snaphoo
Extra Huge Mega Phoobar

PRODUCT PRICE

99
75
50
25
9.95

SQL>
SQL> SELECT productname, ROUND(product_price, 0)
2 FROM
plsqll Byproduct;
PRODUCT_HflME
Small Widget
Medium Wodget
Chrome Phoobar
Round Chrome Snaphoo
Extra Huge Mega Phoobar +
SQL> |

. 5.2. ROUND

ROUND(PRODUCT_PRICE,8)
99
75
se
25
10

SQL

123

,
, . , ROUND
, ,
, , . .
,
ROUND, , ,
, . ,
,
DUAL. , , , ROUND, . 5.1.
SELECT
SELECT
SELECT
SELECT
SELECT
SELECT
SELECT
SELECT

ROUND(1234.5678,
ROUND(1234.5678,
ROUND(1234.5678,
ROUND(1234.5678,
ROUND(1234.5678,
ROUND(1234.5678,
ROUND(1234.5678,
ROUND(1234.5678,

4) FROM DUAL;
3) FROM DUAL;
2) FROM DUAL;
1} FROM DUAL;
0) FROM DUAL;
-1) FROM DUAL;
-2) FROM DUAL;
-3) FROM DUAL;

5.1. ROUND
ROUND

ROUND(1234.5678,4)

1234.5678

ROUND( 1234.5678, 3)

1234.568

ROUND( 1234.5678, 2)

1234.57

ROUND( 1234.5678,1)

1234.6

ROUND( 1234.5678,0)

1235

ROUND(1234.5678, -1)

1230

ROUNDf 1234.5678,-2)

1200

ROUNDf 1234.5678,-3}

1000

TRUNC
TRUNC , .
, 5 . 1,
. , , .
5.2.

124
SELECT
SELECT
SELECT
SELECT
SELECT
SELECT
SELECT
SELECT

5
TRUNC(1234 .5678,
TRUNC (1234 .5678,
TRUNC(1234 .5678,
TRUNC(1234 .5678,
TRUNC(1234 .5678,
TRUNC(l-234 .5678,
TRUNC(1234 .5678,
TRUNC(1234.5678,

4) FROM DUAL;
3) FROM DUAL;
2) FROM DUAL;
1) FROM DUAL;
0) FROM DUAL;
-1) FROM DUAL;
-2) FROM DUAL;

-3) FROM DUAL;

5.2. TRUNC
TRUNC

TRUNC( 1234.5678,4)

1234.5678

TRUNC( 1234.5678,3)

1234.567

TRUNC( 1234.5678, 2)

1234.56

TRUNC( 1234.5678,1)

1234.5

TRUNC(1234.5678,0)

1234

TRUNC(1234.5678,-1)

1230

TRUNC( 1234.5678, -2)

1200

TRUNC( 1234.5678, -3)

1000


, Oracle (character
functions), . : ( ,
); ; , , . ,
.

UPPER, LOWER INITCAP


. , :
SELECT UPPER(product_name) FROM plsqll01_product;
SELECT LOWER(product_name) FROM plsq!101_product;
SELECT INITCAP(productjname) FROM plsqll01_product;
. 5.3. , INITCAP ,
.
:

SQL
A Oiacle SQL'Plus
ite iearch Qptions fclelp
SQL> SELECT UPPER(product_nane) FROM plsq!101_product;

125

UPPER(PHODUCTJtt ME)
SMflLL UIDGET
MEDIUM UODGET
CHROME PHOOBflR
ROUND CHROME SNAPHOO
EXTRA HUGE MEGA PHOOBAR +
SQL> SELECT LOWER(product_nane) FROM plsql101_product;
LOWER(PRODUCT_NAHE)

snail midget
medium wodget
chrome phoobar
round chrome snaphoo
extra huge mega phoobar +
SQL> SELECT IHITCAP(product_nane) FROM plsql101_product;
INITCAP(PRODUCT_NAME)

Small Widget
Medium Wodget
Chrome Phoobar
Round Chrome Snaphoo
Extra Huge Mega Phoobar +
SQL>

. 5.3. UPPER, LOWER INITCAP


SELECT INITCAP('this TEXT hAd UNpredictABLE caSE1) FROM DUAL;:
, , UPPER. SELECT,
, . UPPER,
. ,
: , . , UPPER
, .
. 5.4.
UPDATE
SET
WHERE
SELECT
WHERE

plsqll01_product
product_name = 'chrome phoobar'
product_name = 'Chrome Phoobar';
* FROM plsql!01_product
UPPER(product_name) LIKE '%PHOOBAR%';

126

, Oiacle SQL-Plus
File

Edit

Search

Options Help

SQL> SELECT * FROM plsqllB1_prodUCt


2 WHERE
UPPER(product_name) LIKE '
PRODUCT NflHE

PRODUCT_PRICE QUflNTITV_OH_HflND LflST_STOC

chrome phoobar

Extra Huge Mega Phoobar

50
9.95

188 15-Jf)N-03
1234 15-JflN-84

SQL>

. 5.4. UPPER
UPPER ,
, , . ,
. 5.4, - .
PRODUCTJStAMEK , INITCAP. :
SELECT INITCAP(product_name),

product_pr,ic,e,
quantity_on_hand,
FROM
WHERE

last_stock_date
plsql!01_product
UPPER(product_name) LIKE '%PHOOBAR%';

. 5.5,
, :
UPDATE plsqll01_product
SET
product_name = 'Chrome Phoobar'
WHERE product_name = 'chrome phoobar';

LENGTH

, . LENGTH. , , ,
PLSQL10 l_PRODUCT, .
,
25 15 . , ,
. , , :
SELECT
product_name, LENGTH(product_name) NAME_LENGTH
FROM
plsql!01_product
WHERE
LENGTH(product_name) > 15
ORDER BY product_name;

127

SQL
Oracle SQL'Plus
File Edit Seach Options Help
SQL> SELECT INITCAP(product_name),
2
product price,
3
quantity_on_hand,
<t
last_stock_date
5 FROM
plsql101_product
6 WHERE UPPER(product_nane) LIKE 'tPHOOBftRV;
INITCRP(PRDDUCT_NflME)

S3

PRODUCT_PRICE QUBNTITV_uN_HflND LflST_STOC

Chrome Phoobar
Extra Huge Mega Phoobar

50
9.95

1S-JflN-03
1234 15-JfiN-e4

SQL>

. 5.5. INITCAP

NAME_LENGTH,
.
LENGTH . , ,
.

SUBSTR

Oracle, , . ,

ITEMJD

LA-101
LA-102
LA-
LA-104
NY-101
NY-102
NY-
NY-104
, , :
MANUFACTURERJ.OCATION

MANUFACTURERJTEM.NUMBER

LA
LA
LA

101
102
103

128

LA
NY
NY
NY
NY

5
104
101
102
103
104

(string),
(substring).
ITEMJD . (parsing) .
Oracle SUBSTR.
, ,
.
.
ITEM_ID .
, .
, . , , :
CREATE TABLE. , :
CREATE TABLE plsqll01_old_ltem (
item_id CHAR(20),
item_desc CHAR(25)

INSERT INTO plsq!101_old_item VALUES


('LA-101', 'Can, Small');
INSERT INTO plsqll01_old_item VALUES
('LA-102', 'Can, Large');
INSERT INTO plsq!101_old_item VALUES
('LA-', 'Bottle, Small1);
INSERT INTO plsqll01_old_item VALUES
('LA-104', 'Bottle, Large');
INSERT INTO plsq!101_old_item VALUES
('NY-101', 'Box, Small');
INSERT INTO plsq!101_old_item VALUES
('NY-102', 'Box, Large');
INSERT INTO plsq!101_old_item VALUES
('NY-', 'Shipping Carton, Small');
INSERT INTO plsq!101_old_item VALUES
('NY-104', 'Shipping Carton, Large');

, ,
SUBSTR. :
$\8_, __,
^)

SQL

129

_ , . __ ,
, _ ,
.
, :
SELECT SUBSTR(item_id, 1, 2) MFGR_LOCATION,
SUBSTR(item_id, 4, 3) ITEM_NUMBER,
item_desc
FROM
plsq!101_old_item

, . 5.6.
,
. ,
, .

INSTR

SUBSTR, , ,
. , , , ITEM_DESC PLSQL101_OLD_ITEM,
.. , . ,
, .
Oiacle SQL-Plus

file Edit S^ch Qptjons |


SQL> SELECT SUBSTR(itemjld. 1. 2) MFGR_LOCflTIOH,
2
SUBSTR(item_id, 4, 3) ITEH_NUHBER,

3
4 FROM

5 ;

iten_desc
plsql101_old item

MF ITE ITEM DESC


Lfl 1B1 Can, Snail
LA 102 Can, Large

Lfl 183 Bottle, Small

Lfl
NY
NV
NY
NV

104 Bottle, Large


101 Box, Snail
162 Box, Large
103 Shipping Carton, Snail
18U shipping Carton. Large

8 rows selected.
SQL> |

Jl

. 5.6. SUBSTR

130

, ( ), .
INSTR.
INSTR ,
. ,
, , .
INSTR :
INST~R(ucxodHbtu_meKcm, __, __)

, _ , , . __
, , __ , ( , 1).
INSTR ITEM_DESC PLSQL101_OLD_ITEMHa . , ,
, . ,
, .
INSTR ITEM_DESC PLSQL101_OLD_ITEM, , .
:
SELECT item_desc,
INSTR (itera_desc,
i i
/ i

FROM

1
)
plsqll01_old_item;

. 5.7, INSTR ,
.
SUBSTR, , , . , 1 INSTR.
:
SELECT item_desc,
SUBSTR (item_desc,
1,
INSTR (item_desc,

FROM

) -1
. )
plsql!01_old_item;

. 5.8. ,

.

131

SQL

Oracle SQL'Plus
File Edit Search Qptbns tjelp
SQL> SELECT item_desc,
2
INSTR(iten_desc,

3
U
5

1
)

..,

FROM

plsql101_old_iten;

ITEM DESC

INSTR(ITEM_DESC,V ,1)

Can, Snail
Can, Large
Battle, Small
Bottle, Large
Box, Snail
Box, Large
Shipping Carton, Small
Shipping Carton, Large

*
7
7
<t
4
16
16

8 rows selected.
SQL>

. 5.7. , INSTR

* Oracle SQL'Plus
Fte Edit Search Options Help
SQL> SELECT item_desc,
2
SUBSTR(item_desc,

1,

4
5
6

7
8
9

IHSTR(iten desc,
1

) -1

FROM

plsql1B1_old_iten;

ITEM DESC

SUBSTR(ITEM_OESC,1,INSTR(

Can, Small
Can, Large
Bottle, Snail
Bottle, Large
Box, Snail
Box, Large
Shipping Carton, Snail
Shipping Carton, Large

Can
Can
Bottle
Bottle
Box
Box
Shipping Carton
Shipping carton

8 rows selected.
SQL> |

iLJ
. 5.8.

-IE

132

. (nesting) . , .
.
ITEM_DESC . , , SUBSTR.
INSTR, .
, 2 , INSTR.
, :
SELECT item_desc,
SUBSTR(item_desc,
INSTR(item_desc,
i t

1
FROM

) +2,
99
)
plsqll01_old_item;

'

,
, . , SUBSTR 99.
, , , .
SUBSTR: , .

SUBSTR, , .
SELECT item_desc,
SUBSTR(item_desc,
1,
INSTR(item_desc,
I

1
) -1
) CATEGORY,
SUBSTR(item_desc,
INSTR(item_desc,
t

I
I

1
) +2,

FROM

99
) ITEM_SIZE
plsql!01_old_item;

. 5.9.
, , ,
.
, ,

SQL

133

Bra

* Oiacle SQL'Plus
File Edit Search Qplions Help
SQL> SELECT item desc,
2
SUBSTR(item desc,
3
1.
4
INSTR(item desc,

6
7
8

10
11

1
) -1
) CATEGORY,

SU8STR(item desc,

IHSTR(item desc.

12

13
14
15

) +2,

16 FROM

99
) ITEM SIZE

plsq!101_old_itero;

ITEM_DESC

CftTEGORY

Can, Small
Can, Large
Bottle, Small
Bottle, Large
Box, Small
Box, Large

Can
Can
Bottle
Bottle
Box
Box

Shipping Carton, Small

Shipping Carton, Large


8 rows selected.

Shipping Carton

Shipping Carton

ITEM_SIZE

Small

Large
Small
Large

Small

Large
Small
Large

, . . . , . . ,

SQL> |

.iLJ

26.

. 5.9.
. Oracle , , , ,
:
SELECT item_desc,
SUBSTR(item_desc, 1, INSTR(item_desc, ',', 1) -1) CATEGORY,
SUBSTR(item_desc, INSTR(item_desc, ',', 1) +2, 99) ITEM SIZE
FROM
plsql!01_old_item;


, .
LTRIM RTRIM
, ,
, . . 5.10.

134

IJH* Oracle SQL'Plus


File Ed* Search Options Help
SQL> SELECT 'Item ' | |
2
item_id ||
3
' is described as a ' | |
*
item_desc | |
5
'.' "Item Description Sentence"
6 FROM
plsql101_old_itera;

Item Description Sentence


Item
Item
Item
Item
Item
Item
Item
Item

Lfl-101
Lfl-1 02
Lfl-1 03
Lfl-1 04
NV-101
NV-1 02
NV-103
NV-1 Oil

is
is
is
is
is
is
is
is

described
described
described
described
described
described
described
described

as
as
as
as
as
as
as
as

a
a
a
a
a
a
a
a

Can, Small
Can, Large
Bottle, Small
Bottle, Large
Box, Small
Box, Large
Shipping Carton, Small
Shipping Carton, Large

8 rows selected.
SQL> |
jJJ

--

j5
1

. 5.10. CHAR

SELECT 'Item ' | |


item_id | |
' is described as a ' | |
item_desc | I
' . ' "Item Description Sentence"
FROM
plsq!101_old_item;

ITEM_ID ITEM_DESC ?
, CHAR. ,
VARCHAR2 CHAR? VARCHAR2 , CHAR . ,
-CHAR ,
. , . , .
. (trimming),
Oracle : LTRIM RTRIM. LTRIM , RTRIM . :
(_)

SQL

135

ITEM_ID ITEM_DESC . RTRIM.


, :
SELECT 'Item ' ||
RTRIM(item_id) ||
' is described as a ' ||
RTRIM(item_desc) ||
'.' "Item Description Sentence"
FROM
plsq!101_old_item;

,
. 5.11.
, .
SUBSTR INSTR RTRIM
PLSQL101_OLD_ITEM, ,
. 5.12.
, . 5.12, . - .
, . , .
, .
- . . , , . 5.12. ,

*. Oracle SQL'Plus
file dit Search Options Help
SQL> SELECT 'Item

5
6

'

| |

RTRIM(item_id) ||

2
3
FROM

' is described as a
RTRIM(iten_desc) ||
'. "Item Description Sentence"
plsql101_old_iten;

Item Description Sentence


Item
Item
Item

LA-101 is described as a Can, Small.


LA-1D2 is described as a Can, Large.
LA- is described as a Bottle, Small.

Item
Item
Item
Item

NV-101
NV-102
NV-103
NV-1B4

Item Lfl-184 is described as a Bottle, Large.


is
is
is
is

described
described
described
described

as
as
as
as

a
a
a
a

Box, Snail.
Box, Large.
Shipping Carton, Small.
Shipping Carton, Large.

8 rows selected.
SQL>

. 5.11.

136

File Edit Seated Option: Help


Item ID sentence

The
The
The
The
The
The
The
The

Iten
Iten
Iten
Iten
Iten
Iten
Iten
Iten

ID
ID
ID
ID
ID
ID
ID
ID

for
for
for
for
for
for
For
for

Snail
Large
Snail
Large

Can is: 1.-101.


Can is: Ld-102.
Bottle is: Lft-183.
Bottle is: LA-181.

Small Box is: NY-101.

Large Box is: NV-102.


Snail Shipping Carton is: NV-1Q3.
Large Shipping Carton is: HY-104.

8 rows selected.
SQL>

. 5.12. , !
, .
, , . , .
, .
, , .


Oracle , . ,
, . ,
, .

SYSDATE TRUNC
, ,
:
INSERT INTO plsql!01_product VALUES
('Square Zinculator', 45, 1, SYSDATE);
SELECT * FROM plsql!01_product;

. , . , dd-mmm-yy
( ):
SELECT * FROM plsql!01_product
WHERE last_stock_date = 'dd-mrm-yy';

,
! , ,
?

SQL

137

, ,
SYSDATE, a SYSDATE ,
. ( , ), , . 1 1.4 ,
: ,
.
: TRUNC, WHERE . TRUNC LAST_STOCK_DATE,
SELECT:
SELECT * FROM plsql!01_product
WHERE TRUNC(last_stock_date) = 'dd-mim-yy';

, .
, , .
, , . , , INSERT
SYSDATE TRUNC.
DELETE FROM plsql!01_product
WHERE product_name = 'Square Zinculator';
INSERT INTO plsql!01_product VALUES
('Square Zinculator', 45, 1, trunc(sysdate));
SELECT *' FROM plsql!01_product
WHERE last_stock_date = 'dd-mmm-yy';

TRUNC
? . , , . ,
,
, . , ,
( ) ,
SYSDATE .

ADD_MONTHS

ADD_MONTHS , , (
). :
_(8('/_/', _)
_ ( TRUNC(SYSDATE)) . _
, , -

138

. (
.)
, ,
:
SELECT ADD_MONTHS(SYSDATE,1) FROM DUAL;
SELECT ADD_MONTHS(SYSDATE,12) FROM DUAL;

ADD_MONTHS "", ,
, . , , . 5.13:
SELECT
SELECT
SELECT
SELECT

ADD_MONTHS('28-NOV-00',
1
ADD_MONTHS('29-NOV-OO ,
ADD_MONTHS('30-NOV-00',
ADD_MONTHS('31-DEC-00',

1) FROM DUAL;
1) FROM DUAL;
1) FROM DUAL;
-1) FROM DUAL;

,
"31 ". "31 ", 30 .
ADD_MONTHS
. . ,
ADD_MONTHS .
Oiacle SQL-Plus
fife dk !Seach
SQL> SELECT
flDD_HONTHS('28-NOU-OO',

1) FROM DUftL;

ftDD_MOMTH

28-DEC-OO
SQL> SELECT

flDD_MONTHS('29-NOU-B8',

1) FROM DUAL;

ADDJTOHTH
29-DEC-80
SQL> SELECT ftDD_MONTHS('--', 1) FROH DUflL;
ADDJ40KTH
31-DEC-OB
SQL> SELECT flDD_MONTHS('31-DEC-OO', -1) FROM DUAL;
ADD_HONTH
-NOU-OO
SQL>

. 5.13. ADD_MONTHS

SQL

139

, , ,
- - . ,
INSERT UPDATE '
ADD_MONTHS(TRUNC(SYSDATE),

1)

LAST_DAY

LAST_DAY , : , .
:
, , , ,,,, , ,
LAST_DAY(9a/na)
, , SYSDATE
. , :
SELECT
SELECT
SELECT
SELECT

LAST_DAY(SYSDATE) FROM DUAL;


LAST_DAY('01-JAN-02') FROM DUAL;
LAST~DAY('15-JAN-02') FROM DUAL;
LAST_DAY('31-JAN-02') FROM DUAL;

. ,
, . ,
, 1 , 30 ,
1 . : , , ?
: 1 ,
LAST_DAY. , ,
, .
:
CREATE TABLE plsql!01_person (
person_code VARCHAR2(3),
first_name VARCHAR2(15),
last_name
VARCHAR2(20),
hiredate
DATE

INSERT INTO plsql!01_person VALUES


CCA', 'Charlene1, 'Atlas', 'Ol-FEB-02');
INSERT INTO plsql!01_person VALUES
('GA', 'Gary', 'Anderson', '15-FEB-02');
INSERT INTO plsql!01_person VALUES
( 'BB', 'Bobby' , 'Barkenhagen1;, '28-FEB-02 ') ;
INSERT INTO plsql!01_person VALUES
('LB', 'Laren', 'Baxter', 'Ol-MAR-02');

140

, , LAST_DAY. SELECT, , LAST_DAY


, :
\

SELECT first_name,
last_name,
hire_date,
LAST_DAY(hire_date)+1 INSURANCE_START_DATE
,FROM
plsqlldljperson;

, ,
. 5.14.

t Oracle SQLTIus
File Edit Search Options Help
SQL> SELECT f irst_nane ,
2
lastjname,
3
hire date,
*
LAST~DflV(hire_date)+1
plsqll81_person;
5 FROM

d
INSURflNCE_START_DATE

FIRST NAME

LftST_NflME

HIRE DATE INSURANCE

Charlene
Gary
Bobby
Laren

Atlas
Anderson
Barkenhagen
Baxter

B1-FEB-82
15-FE8-82
28-FEB-82
ei-HflR-82

81-MAR-82
81-MAR-B2
B1-MAR-B2
81-APR-82

SQL>

. 5.14. LAST_DAY

,
. : PLSQL101_PRODUCT . , ,
, .
,
ADD_MONTHS, : . ,
,
. ?
, : 18_
.
:

141

SQL
LAST_DAY(
ADD_MONTHS(
____,
_

+1
, ,
LAST_STOCK_D ATE . , , ,
.
. 5.15.
SELECT product_name,
quantity_on_hand,'
last_stock_date,
LAST_DAY(ADD_MONTHS(last_stock_date, 3))+l RESTOCK_DATE
FROM plsql!01_product
WHERE quantity_on_hand <= 100
ORDER BY product_name ;

MONTHS_BETWEEN

MONTH_BETWEEN ,
, . :
MONTHS_BETWEEN(/c0e4HOH_daffJfl, _)

*. Oiacle SQL-Plus
Je Ed Search Options Help
SQL> SELECT product_nane,
2
quantity_on_hand,

last_stock_date,

FROM

LAST DftV(ftDD_MONTHS(last_stOCk_date, 3))+1 RESTOCK_DflTE


plsqll81_product

ORDER BV product_nane;

6 WHERE quantity_on_hand <= 100


PRODUCT

NftME

Chrome Phoobar
Snail Widget
Square Zinculator

QUANTITV_ON_HAND LAST_STOC RESTOCKJ)


100 15-JAN-03 01-HAV-03
1 15-JAN-03 01-MAV-03
1 09-JUL-OO 01-NOU-OO

SQL> |

-iLJ

. 5.15.

142

, , . , ,
PLSQL101_PRODUCT, :
SELECT product_name,
last_stock_date,
MONTHS_BETWEEN(SYSDATE, last_stock_date) STOCK_MONTHS
FROM
plsqll01_product;

,
MONTH_BETWEEN, ROUND:
SELECT product_name,
last_stock_date,
ROUND(MONTHS_BETWEEN(SYSDATE, last_stock_date),0) STOCK_MONTHS
FROM
plsql!01_product;

, , (
):
SELECT MONTHS_BETWEEN(SYSDATE, _) FROM DUAL;

, ? ,
- .


(data conversion)
,
, . Oracle
, :
, .
.
, , , .

TO_CHAR
TO_CHAR , .
, , . , ,
SQL*Plus.
, , - ; , ,
. : ,
, . TO_CHAR
.

SQL

143

TO_CHAR,
,
:
_(_, _)
_ ,
, . _
, , .
TO_CHAR .
SELECT TO_CHAR(SYSDATE, .'MM-DD-YYYY HH24:MI:SS') NOW
FROM DUAL;

, SQL*Plus, ,
_ , (-!)
SYSDATE. ,
.
5.3. , ,
Oracle .
DD, , , . , Oracle "1ST" "1",
"2ND" "2", .. , ,
:
SELECT TO_CHAR(SYSDATE,
FROM DUAL;

'MONTH DDTH 1 )

SP . :
SELECT TO_CHAR(SYSDATE, 'MONTH DDSP')
FROM DUAL;

Oracle "st", "nd", "rd" "th". :


SELECT TO_CHAR(SYSDATE,
FROM DUAL;

'MONTH DDSPTH 1 )

, . ,
MONTH ,
, . , , "" .
. "" :

6 . 725

144

5.3. ,
TCLCHAR

' '
AD
.

( )

AM
..

( )

.L.

( )

SCC

( ); "S" . .
"-"

(1-7)

DAY

DD

'

'

,'.. .

'

.'

(1-31)

:-

",

'

..

DDD

(1-366)

DY

( ,
- )

( ,
- )

(1-12)

12
24
IW

(1-12)
(0-23)
(1 -52 1 -53) ISO

SQL

145

5.3 ()

IYYY

ISO

IYY

(, ) ISO

- - ...

IY

. -

; , 1 4712 .
.. J

Ml .

(0-59)

(01-12; = 01)

MON

MONTH

P.M.

. . - ; . : : .1

,
( )
/

(1,2,3,4; - = 1)

RM

(I-XII; = I)

RR

, ,
, < 50 >= 50;
, >= 50
< 50

RRRR

. , RR. ,

SS

(0-59)

SSSSS

(0-86399)
' : ' ' . . . ' . , '

WW

(1 -53),

(1 -5),

Y.YYY

' /:'

146

(.)

5.3

YEAR
SYEAR

.
; "S" .. "-"

YYYY
SYYYY

; "S" ..
"-"

YYY
YY
Y

(, )


(, ""
"th").
SP
, .
:
SELECT TO_CHAR(SYSDATE,
FROM DUAL;

'MONTH DDTH 1 )

, RTRIM. , ,
, ,
: , . ,
, .
SELECT TO_CHAR(SYSDATE,
I

'MONTH')

||

' I I

TO_CHAR(SYSDATE,
FROM DUAL;

'DDTH')

.
TO_CHAR RTRIM, :
SELECT RTRIM(TO_CHAR(SYSDATE, ' M O N T H ' ) )
I
I
I I
TO_CHAR(SYSDATE, ' D D T H ' )
FROM DUAL;

||

.
, .
INITCAP . SELECT, , :

SQL

147

SELECT INITCAP(RTRIM(TO_CHAR(SYSDATE, 'MONTH'))) I I


i ||
TO_CHAR(SYSDATE, ' DDTH ' )
FROM DUAL;

. , , . :
LOWER, :
SELECT INITCAP(RTRIM(TO_CHAR(SYSDATE, ' M O N T H ' ) ) )
' ' I I
LOWER ( TO_CHAR ( S YSDATE , ' DDTH ' ) )
FROM DUAL;

I I

,
, :
SELECT TO_CHAR(SYSDATE,
FROM DUAL;

'MONTH DDSPTH')


TO_CHAR
. , ,
, :
i . :j SELECT * FROM plsqll01_product;

,
.
SELECT TO_CHAR. :
SELECT product_name,
TO_CHAR(product_price, '$9,9-99.00') "Price",
quant ity_on_hand,
last_stock_date
FROM
plsql!01_product;

, . 5. 16.
, , .
5.4. , SELECT, QUANTITY_ON_HAND
PLSQL101_PRODUCT . LAST_STOCK_DATE. ,
. 5.17. , LAST_STOCK_DATE , QUANTITY_ON_HAND.
, .

TO_DATE

TO_DATE (/ ) /. ,
,

148
5.4.

$9999

,()

9,999

.()

99.99

Ml

9999MI

"-"

S9999

"+" ( ) "-" ( )

9999PR

< >

99D99

PR

999

ISO

L999

RN rn

RN


(
1 3999)

0999

9990

Oracle (DD-MON-YY), .
:
_(_, _)
TO_DATE ,
TO_CHAR. , , 5.5.

149

SQL
5.5.

' '
AD
A.D.

( )
( )

..

..

( )

(1-7)

DAY

DO

(1-31)

ODD

(1-366)

DY

HH

(1-12)

HH24

(0-23)

; , 1 4712 .
.. J

Ml

(0-59)

MM

(01-12; = 01)

MON

MONTH

PM
P.M.

( )

'

...;

150

5.5

()

RM

(I-XII; = I)

RR

, ,
, < 50 >= 50;
, >= 50
< 50

RRRR

. , RR. ,

SS

(0-59)

SSSSS

(0-86399)

Y.YYY

YYYY
' SYYYY
YYY
YY
Y

'

; "S" .. "-"
/
.
(, )

, TO_DATE
,
. 5.18.
SELECT product_name,
product_price,
quantity_on_hand,
TO_CHAR(last_stock_date, 'MM-DD-YYYY H H 2 4 : M I ' ) "Last Stocked"
FROM plsql!01_product;
UPDATE plsql!01_product
SET
last_stock_date = TO_DATE('December 31, 2002, 11:30 P . M . ' ,
'Month dd, Y Y Y Y , HH:MI P . M . ' )
WHERE product_name LIKE '%Zinc%';
SELECT product_name,
product_price,
quantity_on_hand,
TO_CHAR(last_stock_date, 'MM-DD-YYYY HH24:MI') "Last Stocked"
FROM plsql!01_product;

151

SQL

J: Oracle SQL-Plus
File Edit Search Options Help
SQL> SELECT product name,
2
TO_CHAR(product_price, '$9,999.88') "Price",
3
quantity on_hand,
4
last stock date
5 FROM
plsq!181_product;
PRODUCT_NftME
Small Widget
Medium Wodget
Chrome Phoobar
Round Chrome Snaphoo
Extra Huge Mega Phoobar +
Square Zinculator

Price

-1 x|

QUANTITV_ON_HAHD LAST_STOC

$99.88
$75.88
$58.88
$25.88
$9.95
$45.88

1
1888
100
18888
1234
1

15-JAN-83
15-JAN-82
15-JAN-83
15-JAN-84
89-JUL-88

6 rows selected.
SQL>

iLJ

. 5.16. TO_CHAR

I' 'Iv|

*. Oracle SQL-Plus
File dit Search Qptions Help
PRODUCT_NAME
Small Widget
Medium Wodget
Chrome Phoobar
Round Chrome Snaphoo
Extra Huge Mega Phoobar +
Square Zinculator
6 rows selected.

Price
$99 .88
$75 .88
$58 .88
$25 .88
$9 .95
$45 .88

On Hand Last Stocked


1
1,888
188
18,000
1,234
1

JAN 15, 2883


JAN 15, 2882
JAM 15, 2883

JAN 15, 2884


JUL 89, 2888
,

SQL>
jJJ

. 5.17. ,
TO_CHAR


SQL-,
, . ,
. , .

152

A Oracle SQL-Plus
fte Ed* !ech ::1
SQL> SELECT product_nane,

2
3
it
5 FROM

RES
r

;;;

'

product_price.
quantity_on hand,
TO CHARUast stock date, 'MM-DD-YVVV 24:) "Last Stocked"
plsql101_product;

PRODUCT_NftME

Small Widget
Medium Wodget
Chrone Phoobar
Round Chrome Snaphoo
Extra Huge Mega Phoobar
Square Zinculator

PRODUCT_PRICE QUfiNTITV_ON_HflND Last Stocked

99
75
50
25
9.95
45

1
1B00
100
10000
. 1234
1

01-15-2003 BO:08
01-15-2802 00:00
01-15-2003 00:00
01-15-2004 00:00
07-09-2000 00:00

6 rows selected.
SQL>
SQL> UPDATE plsqHQl product
2 SET
last_stock_date - TO DATECDecember 31, 2002, 11:30 P.M.'.
3
'Month dd, VVVV, HH:MI P.M.')
4 WHERE productjiame LIKE '%ZincV;
1 row updated.
SQL>

SQL> SELECT product_name.


2
product_price,
3
quantity_on_hand,
4
TO CHAR(last stock date, 'MM-DO-vvvv HH24:MI') "Last Stocked"
5 FROM plsql101_product;

PRODUCT NAME

Small Widget
Medium Wodget
Chrome Phoobar
Round Chrbme Snaphoo
Extra Huge Mega Phoobar
Square Zinculator

PRODUCT_PRICE QUAHTITV_ON_HAND Last Stocked

99
75
50
25
9.95
45

1
1000
100
10000
1234
1

01-15-2003 00:00
01-15-2002 00:00
01-15-2003 00:00
01-15-2004 00:00
12-31-2002 23:3B

rows selected.
SQL>

. 5.18.

TO_DATE

DECODE
SQL, , PL/SQL, ,
, SQL , "goto", if-then-else. SQL

SQL

153

, , . SQL
, , - if-then-else: DECODE.
, "" "". : "",
"", "", "D".
"", "", "" "D" . ,
.
DECODE :
DECODE(ucmo4HUK_exodHbtx_daHHbix,
'___1, __ 1,
__2, __2,
__, __,
[___ __]
)
:
__ (
), / .
,
, , .
(
).
/ ,
, -
. ,
DECODE , (
else if-then-else).
( ),
, ,
- , .
DECODE, ,
PLSQL101_OLD_ITEM . ,
, , . ,
1
"NY ,
("Eastern"), "LA" ("Western") . , , - ,
SUBSTR, . ,
DECODE . .
. 5.19.

154

* Oiacle SQL-Plus

File Edit Search 'Options Help


SQL> SELECT DECODE(SUBSTR(iten_id, 1,2),
2
'Lfl 1 , 'Western' ,
'NV , 'Eastern' ,
3
4
5
6
7

FROM

-d
-J

'* Unknown '


) "Region",
SUBSTR(iten_id, 4,3) "Item ID",
item desc
plsql181_old_iten;

Region

Ite ITEMJDESC

Western
Western
Western
Western
Eastern
Eastern
Eastern
Eastern

181 Can, Snail


182 Can, Large
183 Bottle, Snail
184 Bottle, Large
181 Box, Snail
182 Box, Large

183 Shipping Carton, Snail


104 Shipping Carton, Large

8 rows selected.
SQL> |

'

jJJ

jJ^

. 5.19. DECODE
SELECT DECODE(SUBSTR(item_id, I, 2),
'LA', 'Western',
'NY', 'Eastern',
'* Unknown *'
) "Region",
SUBSTR(item_id, 4,3) "Item ID",
item_desc
FROM
plsqll01_old_item;

, DECODE , ,
. ,
. :
,
.
Oracle,

YYYYMMDD.

,
.
SUBSTR

SQL

1 55

.
PATIENTJD.

(Male, ), F (Female, ), (Other,
-) U (Unknown, ).
1 , 2, 3 4.
, DECODE.
,
. ,
, .
DECODE -
/
,
( ).
DECODE
.
,
/ .
LTRIM RTRIM .

. ,
- , .
, ,
. ,
:
SOCIAL_SECURITY_NUMBER

AGEJTYPE

AGE_AT_ENTRY
<

222334444

65

333445555

18

444556666

555667777

10

AGE_MOS

AGE_DAYS

:
PATIENTJD

AGE_YRS

111-22-3333

28

222-33-4444

65

333-44-5555

18

444-55-6666

555-66-7777 ,

10

156

'.,.

:

select to_char(EXIT_DATE, 'YYYYMMDD')
DISCHARGE_DATE,
substr(SOCIAL_SECURITY_NUMBER,1,3) I -'I I
substr(SOCIAL_SECURITY_NUMBER,4,2) I | ' - ' H
substr(SOCIAL_SECURITY_NUMBER,6,4)
decode(GENDER,
'M', '!',
'F', '2',
'0', '3',
1
U', '4')
substr(ZIP_CODE, 1,5)
decode(Itrimfrtrim(SITE) ) ,
'300' 'LA1
'350'
'
1
'LA
---'4 10'
'420'
''
1
'4,40'
'LA
'*N/A*')
Itrimfrtrim(PHYSICIAN))
to_char(DATE_OF_BIRTH, ' Y Y Y Y M M D D ' )
; .
decode(AGE_TYPE,
' Y 1 , AGE_AT_ENTRY,
! l
)
decode(AGE_TYPE,
' M ' , AGE_AT_ENTRY,
1
')
decode(AGEJTYPE,
' D ' , AGE_AT_ENTRY,

'')

PATIENT_ID,

GENDER,
ZIPCODE,

COUNTY,
PHYSICIAN
DOB,

AGE_YRS,
AGE_MOS,

AGE DAYS,

from ENCOUNTER

DECODE. AGEJTYPE , "Y", AGE_AT_ENTRY AGE_YRS .


"Y" AGEJTYPE AGE_YRS .
, AGEJTYPE "" "D" AGE_AT_ENTRYB AGE_MOS
AGE_DAYS, . DECODE : -,
DECODE, -, DECODE , , .

NVL
NVL , :
null-, .
. :

157

SQL
~]-.(^,__1)

, _
. __11 , : (.. ),
.
NVL , .
, . 5.20.
SELECT product_name,
last_stock_date
FROM plsqllOljproduct;
SELECT product_name,
NVL(last_stock_date, 'Ol-JAN-2001') "Last Stocked"
FROM plsqll01_product;

* Oiacle SOL'HIus

0e EA Search Dions' 1
" :
SQL> SELECT product_name,
2
last stock_date
3 FROM
plsql101_product;
PRODUCT NAME

LAST STOC

Small Widget
15-JAN-03
Medium Uodget
15-JAN-02
Chrome Phoobar
1S-JAN-03
Round Chrome Snaphoo
Extra Huge Mega Phoobar * IS-JAN-OH
Square Zinculator
31-DEC-02
rows selected.
SQL>
SQL> SELECT product name,
2
NUL(last_stock date, 1ei-JAH-2BOl1) "Last Stocked"
3 FROM plsql101_product;
PRODUCT NAME

Last Stoc

Small Widget
Medium Wodget
Chrome Phoobar
Round Chrome Snaphoo
Extra Huge Mega Phoobar
Square Zinculator

15-JAN-03
15-JAN-02
15-JAN-03
01-JAN-01
15-JAN-Oil
31-DEC-02

6 rous selected.
SQL> |

. 5.20. NVL null-

158

, NVL LAST_STOCK_DATE
. null , :
'
SELECT product_name,
NVL(last_stock_date, TRUNC(SYSDATE)) "Last Stocked"
FROM
plsqll01_product;

Jf
p < g NVL .
.
NVL : , _
__11 . _ , __11 , ,. , "N/A"
null-, "N/A" . ,
_, , .
null-
,
TO_CHAR, .
.
, . . 5.21.
SELECT product_name,
NVL
last_stock_date, 'N/A') "Last Stocked"
FROM
plsqll01_product;
SELECT prod'-uct_name,
NVL
TO_CHAR(last_stock_date), 'N/A') "Last Stocked"
FROM
plsqll01_product;

SQL-
4 , SQL-.
, , ,
. SQL- , ,
. Oracle, .
.
, , .
, , .
, ,
.

159

SQL
Oracle SQL-Plus
fjle Ed Seatch Options Help
SQL> SELECT product_nane,
2
NUL(last_stock_date, 'N/fi') "Last Stocked"
3 FROM
plsql101_product;
HUL(last_stock_date, 'N/fl') "Last Stocked"

ERROR at line 2:
ORA-01858: a non-numeric character was found where a numeric was expected

SQL>
SQL> SELECT product_nane,

NUL(TO_CHflH(last_stock_date), 'H/fl') "Last Stocked"

FROM

plsqltei_product;

PRODUCT NAME

Last Stoc

'

Small Midget
15-JflN-03
Medium Wodget
15-JHN-B2
Chrome Phoobar
15-JflN-03
Round Chrome Snaphoo
H/fl
Extra Huge Mega Phoobar + IS-JAN-eft
Square Zinculator
31-DEC-B2
rows selected.
SQL>

. 5.21. NVL
, . , , Oracle . , , :
SELECT * FROM plsql!01_product;
-- . Oracle .
SELECT *, FROM plsql!01_purchase;

. 5.22.
, .
[

>
, -^


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

,
,
"/*", "*/". . :

160

* 1 SfJl'Plus
Je d Search Options JHelp
SQL> SELECT FROM plsql101_product;
PBODUCT_NftME

PRODUCT_PRICE QUflNTITV_OH_HflND LAST_STOC


99
75
50
25

Small Widget
Medium Uodget
Chrome Phoobar
Round Chrome Snaphoo
Extra Huge Mega Phoobar
Square Zinculator

9.95

45

1
1000
100
10000
1234
1

15- JAN- 03
15-JftN-B2
15-JAN-83

15-JflN-flJt
31 -DEC- 02

6 rows selected.
SQL> This line will be ignored. Oracle will not try to run it.
SQL> SELECT FROM plsql1D1_purchase;
PRODUCT_NAME

Small Widget
Medium Uodget
Chrome Phoobar
Small Widget
Medium Uodget
Round Snaphoo

QUANTITY PURCHASE. SAL


1
75
2
8
20
5

14-JUL-03 CA
14-JUL-03 BB
14-JUL-03 GA
15-JUL-03 GA
15-JUL-03 LB
16-JUL-03 CA

6 rous selected.
SQL>

. 5.22. SQL-
.
PL/SQL 101 Oracle Press.
*/

SELECT * FROM plsql!01_product;


SELECT * FROM plsql!01_purchase;
. 5.23.
' '. '



, , . Oracle (group
functions), . , - ,
, . ,

SQL

161

* Oracle SQL'Plus

ile Edit Seatch Oplions Help


SQL> /

DOOThis script is designed to shou how multiple-line commenting works.


DOOIt is used in the PL/SQL 1191 book by Oracle Press.
BOO*/
SQL>
SQL> SELECT FROM plsql1fl1_product;
PRODUCT NftHE

Snail Widget
Medium Uodget
Chrome Phoobar
Round Chrome Snaphoo
Extra Huge Mega Phoobar +
Square Zinculator

PRODUCT_PRICE QUflHTITV_OH_HflHD Lfl$T_STOC

99
75
50
25
9.95
45

rows selected.

1 15-JftN-e3
10QO 15-JSN-82
160 1S-JflN-03

ieeee

1234 15-JflN-B4
1 31-DEC-82

....-,,.

SQL>
SQL> SELECT FROM plsq!101_purchase;
PRODUCT

NftME

Small Widget
Medium Uodget
Chrome Phoobar
Snail Widget
Medium Uodget
Round Snaphoo

QUfiHTITV PURCHASE. SflL


1
75
2
8
26
5

14-JUL-B3
14-JUL-83 BB
14-JUL-03 Gfi
15-JUL-B3 Gfl
15-JUL-Q3 LB
16-JUL-B3

6 rows selected.
SQL>

. 5.23. SQL-
, , , , .
.

, , , , .
, , .

SUM

SUM . ,
, :
SELECT * FROM plsql!01_purchase;
SELECT SUM(quantity) FROM plsqll01_purchase;

162

COUNT

COUNT, , . , , . , , - ,
:
SELECT COUNT(*) FROM plsqllOljpurchase;

, , ,
. "*" , Oracle .
, ,
. Oracle
, ,
. -
, COUNT :
SELECT COUNT(product_name) FROM plsqll01_purchase;

, , . , COUNT ,
:
SELECT COUNT(1) FROM plsqll01_purchase;

, Oracle "1" . "Hi There";


, .
, .
COUNT :
, , , - .
, null-
. ,
;
,
; ,
LAST_STOCK_DATE; ,
. ,
- .
. 5.24.
SELECT
SELECT
SELECT
SELECT
FROM

COUNT(1) FROM plsqll01_product;


COUNT(product_name) FROM plsql!01_product; '
COUNT(last_stock_date) FROM plsqll01_product;
COUNT(last_stock_date) / COUNT(product_name)
"Populated Records"
plsql!01_product;

163

SQL
f, Oracle SQL-Plus
File Edit Search flptions Help
SQL> SELECT COUHT(1) FROM plsql101_product;

COUHT(1)

6
SQL>
SQL> SELECT COUNT(product_nane) FROM plsql181_product;
COUNT (PRODUCTJWME)
6

SQL>
SQL> SELECT COUNT(last_stock_date) FROH plsq!101_product;
CuUNT(LflST_STOCK_DflTE)
_

SQL>
SQL> SELECT COUNT(last stoch_date) / COUNT(product_naine) "Populated Records"
2 FROM plsql1B1_product;
Populated Records
.833333333
SQL>

. 5.24. COUNT

AVG
AVO .
, " \" - ; . ,
PLSQL101_PRODUCT, :
SELECT AVG(product_price) FROM plsql!01_product;

MIN
MIN , . , ,
PLSQL101_PRODUCT, :
SELECT MIN(product_price) FROM plsql!01_product;

MAX

,
. , -

164

PLSQL101_PRODUCT,
:
SELECT MAX(product_price) FROM plsql!01_product;

MAX . , . ,
,
. ,
, :
fjgjgea DESC plsq!101_purchase;
SELECT MAX(LENGTH(product_name)) FROM plsqll01_purchase;


GROUP BY
, ,
. GROUP BY
SELECT, :
SELECT * FROM plsql!01_purchase;
SELECT product_name, SUM(quantity)
FROM
plsqll01_purchase
GROUP BY product_name;

. 5.25. , GROUP BY , .
SELECT.
SELECT . , , ,
,
. , .
, PRODUCT_NAME
SUBSTR.
SELECT SUBSTR(product_name, 1, 15) "Product",
SUM(quantity) "Total Sold",
AVG(quantity) "Average",
COUNT(quantity) "Transactions",
MIN(quantity) "Fewest",
MAX(quantity) "Most"
FROM
plsqllGlJpurchase
GROUP BY product_name;

. 5.26.

SQL

165

* Oiacle SQL'Plus

MI-

File E dit Search Options Help


SQL> SELECT FROM plsq!1B1_purchase;
PRODUCT NAME

QUANTITV PURCHASE_ SAL

Small Widget
Medium Uodget
Chrome Phoobar
Small Widget
Medium Uodget
Round Snaphoo

1
75
2
8
20
5

1J-JUL-03
Ut-JUL-03
U-JUL-03
1S-JUL-B3
15-JUL-83
16-JUL-03

BB
GA
GA
LB
CA

rows selected.
SQL>
SQL> SELECT product_nane, SUM(quantity)
2 FROM
plsqltei_purchase
3 GROUP BV product_nane;
PRODUCT

SUM(QUANTITV)

NftME

Chrome Phoobar
Medium Uodget
Round Snaphoo
Small Widget

95
5
9

SQL>

. 5.25. GROUP BY

' < esc

lA-Oracle SQL-Plus' ;>>-9?>11->: -.,-.. ${.; .,;;: r.~. .-.:>,-.^ - '. , : ' - . - fie Edit Search Options Help
SQL> SELECT SUBSTR(product name. 1. 15) "Product".
2
SUM(quantity) "Total Sold",
3
fiUG
(quantity) "Average",
H
COUNT(quantity) "Transactions",
5
HI N( quantity) "Fewest",
6
MflX( quantity) "Most"
7 FROM
plsql101 purchase
8 GROUP BV pruduct_nane;
Product
Chrome Phoobar
Medium Uodget
Round Snaphoo
Small Widget

Total Sold
2
95
5
9

Average Transactions
2
47.5
5
4.5

1
2
1
2

J
.

Fewest

Most

2
2
5
1

2
75
5

SQL>

AJ

. 5.26.
SELECT

166


HAVING
, WHERE , SELECT. (
3.) WHERE
: , ,
.
:
. ,

.
, , . HAVING.
. WHERE, ,
HAVING . ,
, ( , , ,
EDIT
HAVING):
-I

.: SELECT SUBSTR(product_name, 1, 15) "Product",


SUM(quantity) "Total Sold",
AVG(quantity) "Average",
COUNT(quantity) "Transactions",
MIN(quantity) "Fewest",
MAX (quantity) "Most".
FROM
plsql!01_purchase
GROUP BY product_name;
HAVING SUM(quantity) < 5;

, SELECT
.
? HAVING, :
SELECT SUBSTR(product_name, I, 15) "Product",
SUM(quantity) "Total Sold",
AVG(quantity) "Average",
COUNT(quantity) "Transactions",
MIN(quantity) "Fewest",
MAX(quantity) "Most"
FROM
plsql!01_purchase
GROUP BY product_name;
HAVING SUM(quantity) >= 5;

. SQL-,

SQL

167

(SYSDATE, USER USERENV), (ROUND TRUNC),


(UPPER, LOWER, INITCAP, LENGTH, SUBSTR, INSTR,
LTRIM RTRIM), (ADD_MONTHS,
LAST_DAY MONTH_BETWEEN),
(TO_CHAR TO_DATE), , DECODE NVL. (SUM, COUNT, AVG, MIN MAX)
GROUP BY,
SELECT.
, SELECT HAVING.
. , ,
,
.

1.
?
INSERT INTO plsql!01_product (
product_name,
product_price,
quantity_on_hand,
last_stock_date )
VALUES (
'New P r o d u c t ' ,
1.95,
10,
TO_CHAR(USER) )
''

A. 1
B.2
C. 7
,

D. 10
E.

2. ?
A. ROUND(4.5, 0) < TRUNC(4.5,0)
B. ROUND(4.1, 0) < TRUNC(4.2, 0)
C. ROUND(8.9, 0) > TRUNC(8.9,0)
D. ROUND(8.9, 1) > TRUNC(8.95, 2)

168

3. DECOt)E?
DECODE('B',
'A', 'One',
1
' , 'Five',
, 'Nine',
1
' , 'Fifteen',
1
'U , 'Twenty-one',
'N/A'
)
A. One
B. Two
C. Five
D. N/A
4. SUBSTR,
'Psychic trance;, Medium's ITEM_DESC?
SUBSTR(item_desc,
INSTR(item_desc,
.i
.t i

99

1
)+2,

)
A. Medium

>

B. Psychic trance
C. Psychic trance, Medium
5. 2002 ?

A. SELECT ADD_MONTHS(LAST_DAY(14-OCT-02'), 1)
FROM DUAL;
B. SELECT ADD_MONTHS(LAST_DAYC15-OCT-02'), -1)
FROM DUAL;
C. SELECT ADD_MONTHS(LAST_DAY(16-OCT-02'), 2)
FROM DUAL;
D. SELECT ADD_MONTHS(LAST_DAY(17-OCT-02'),-2)
FROM DUAL;


1.D. 10

TO_CHAR
,
.

SQL
2. .

169

ROUND(8.9, 0) > TRUNC(8.9, 0)

5 < 4, . 4 < 4, . D 8.9 > 8.95, ...


, . 9 > 8, .

3. D.

N/A

') ,
DECODE;
"else" .
, 'N/A'.
4. A.

Medium

" ",
.

5. .

LAST_DAY,

31 2002 . ,
, 31 2002 .

, ....

172

, SQL, . ,
, . , ,
,
, .
,
. , , .
,
, , .
DROP TABLE plsql!01_person;
CREATE TABLE plsqll01_person (
person_code VARCHAR2(3),
first_name
VARCHAR2(15),
last_name
VARCHAR2(20),
hiredate
DATE

INSERT INTO plsql!01_person VALUES


CCA', 'Charlene', 'Atlas', 'Ol-FEB-02');
INSERT INTO plsql!01_person VALUES
('GA', 'Gary', 'Anderson', '15-FEB-02');
INSERT INTO plsql!01_person VALUES
CBB', 'Bobby", 'Barkenhagen', '28-FEB-02');
INSERT INTO plsql!01_person VALUES
CLB', 'Laren', 'Baxter', 'Ol-MAR-02');
DROP TABLE plsql!01_product;
CREATE TABLE plsql!01_product (
product_name
VARCHAR2(25),
product_price
NUMBER(4,2),
quantity_on_hand NUMBER(5,0),
laststockdate
DATE

INSERT INTO plsql!01_product VALUES


('Small Widget', 99, 1, '15-JAN-03');
INSERT INTO plsqllOl product VALUES
('Medium Wodget', 75, 1000, 45-JAN-02');
INSERT INTO plsql!01_product VALUES
1
('Chrome Phoobar , 50, 100, '15-JAN-03');
INSERT INTO plsql!01_product VALUES
('Round Chrome Snaphoo', 25, 10000, null);
INSERT INTO plsql!01_product VALUES
('Extra Huge Mega Phoobar +', 9.95, 1234, ' 15-JAN-04'.) ;
INSERT INTO plsql!01_product VALUES ('Square Zinculator',
45, 1, TO_DATE('December 31, 2002, 11:30 P.M.',

173

'Month dd, YYYY, HH:MI P.M.')

DROP TABLE plsql!01_purchase;


CREATE TABLE plsqll01_purchase (
product_name VARCHAR2(25),
salesperson
VARCHAR2(3),
purchase_date DATE,
. quantity
NUMBER(4,2)

INSERT INTO plsql!01_purchase VALUES


1
('Small Widget , 'CA', 44-JUL-03', 1);
INSERT INTO plsqllOl purchase VALUES
('Medium Wodget', 'BB', '14-JUL-03', 75);
INSERT INTO plsql!01_purchase VALUES
1
('Chrome Phoobar', 'GA', '14-JUL-03 , 2);
INSERT INTO plsql!01_purchase VALUES
('Small Widget', 'GA', '15-JUL-03', 8);
INSERT INTO plsql!01_purchase VALUES
('Medium Wodget', 'LB', '15-JUL-03',.20);
INSERT INTO plsql!01_purchase VALUES
('Round Snaphoo', 'CA', 46-JUL-03', 5);
DROP TABLE plsql!01_old_item;
CREATE TABLE plsql!01_old_item (
item id CHAR(20),
itenTdesc CHAR(25)

INSERT INTO plsql!01_old item VALUES


('LA-1011, 'Can, Small');
INSERT INTO plsql!01_old_item VALUES
('LA-102', 'Can, Large');
INSERT INTO plsq!101_old_item VALUES
('LA-', 'Bottle, Small');
INSERT INTO plsql!01_old_item VALUES
('LA-104', 'Bottle, Large');
INSERT INTO plsqllOl old item VALUES
('NY-101', 'Box, Small');
INSERT INTO plsq!101_old_item VALUES
('NY-102', 'Box, Large');
INSERT INTO plsq!101_old_item VALUES
('NY-', 'Shipping Carton, Small');
INSERT INTO plsq!101_old_item VALUES
('NY-104', 'Shipping Carton, Large1);

. <

, (), .

174

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


.
,
Oracle (
)
, . Oracle , ,
.
.
. , Oracle
, ( ,
, ). , (
), , ,
. , SQL-, .
(, , 8.)
18.9 .
0.6 . 31 !
. 6.1 ,
PLSQL101_PERSON.
PERSON_CODE. ,
PERSON_CODE, .
, .
ROWNUM, , .

PLSQL101 PERSON

PERSON CODE

PERSON.CODE

F1RST.NAME

LASTNAME

HIRE_DATE

Chariene

Adas

'01-Feb-02

GA

Gary

Anderson

15-Feb-02

CA

3
1

Bobby

01-Feb-02

GA

Laren

Barkenhagen
Baxter

01-Feb-02

LB

. 6.1.

PERSON.CODE
BB

ROWNUM

175

. 6.2 PLSQL101_
PURCHASE . , . ? PLSQL101_
PURCHASE . , : . ,
, ,
.
, , ,
,
.
, Oracle
. INSERT, UPDATE DELETE
,
SELECT , . - ,
.
, .

PLSQL101 PURCHASE
PRODUCT.NAME
Small Widget
Medium Wodget

QUANTITY PURCHASEJWE SALESPERSON


1
H-Jul-03 CA
75
14-Jul-03 BB

Chrome Phoobar

14-Jul-03 GA
14-Jul-03 GA

Small Widget
Medium Wodget

20

Round Snaphoo

14-JuMB IB
14-Jul-03 CA

SALESPERSON

PRODUCT NAME
PRODUCT.NAME
Chrome Phoobar

ROWNUM

BB

SALESPERSON-

ROWNUM

Medium Wodget

CA

Medium Wodget

CA

Round Snaphoo

GA

Small Widget
Small Widget

1
4

GA
LB

. 6.2.
7 . 725

176

:
DROP INDEX _;

?
,
. , SELECT,
UPDATE DELETE ,
.

INSERT; .
? , .
, Oracle , , ..
. ( ).
, , ..
, . , . ,
,
. , , , .
, . ,
, , ,
. ?
, .
, , ,
, , , .
, . ,
, .
, . .
. 6.1 , DML . SELECT, ,

UPDATE DELETE. . , , , ,

.

177

6.1. DML

SELECT
(50 )

18.9

0.6

31. 5

UPDATE
(50 )

19.7

0.5

39.4

DELETE
(50 )

19.6

0.06

326.7


.
:
' ' - . '

-' -

'

' . - ' . < : .

,-' ' '

CREATE INDEX _ ON _(_);


, ,
:
CREATE INDEX _ ON _ (
__,
__

, , . 6. 1 , :
CREATE INDEX plsqll01_person_code_index
ON plsql!01_person (person_code) ; i


PERSON_CODE WHERE. , ' . :
CREATE INDEX plsqll01_person_name_index
ON plsqll01__person (last_name, first_name) ;

(composite index), (concatenated index).


. , WHERE , , ,
. Oracle ,

178

WHERE , .
, ,
, ,
WHERE. .

,
Oracle, 32.
, , . 6.2.


, , , ,
, .
,
. Oracle , .
; .

*-

, Oracle , *-
(*-), *- . 6.3.
*- Oracle () , -
(leaf blocks) . -
(branch blocks), - .
^ "*

. 6.3 - .

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

*- , Oracle , . , , , Oracle
. (, , 6.1, *-.) , ,
, . -

179
ROWNUM

UST.NAME

Norton

Gutwirth

Trumble

Fletcher

Zoraster

6
7

Allen

Smith

Moss

- <

Allen-7
Retcher-4

Gutwirth 2
Moss - 6

Norton - 1
Smith - 8

Trumble - 3
Zoraster - 5

. 6.. *-
, , Oracle
, . *-, Oracle 31 .
, Oracle . 6.2 ,
*- , .
*- , , ,
. (, "")
.

*-
, ,
. , "", ,
: "" (""), "F" ("") "U" ("Unknown"

180

6.2. ,
*-

1000000000

500000000

250000000

125000000

62500000

6
7

31250000

7812500

15625000
.,'---

3906250

10

1 953 125

11

976563

12

488 281

13

244141

14

122070

15

61035

16

30518

17

15259

18

7629

19

3815

20

1907

21

954

22

477

23

238

24

119

25

60

26

30

27

15

28

29

31

'

181

"").
*- , "
", *-
, . . . 6.4.

(cardinality)
, .
(,
" "/" ") .
(,
) .
SELECT, WHERE
,
. : -,
( , ), -, " 1"
"" .
; "BITMAP", :
CREATE BITMAP INDEX_ ON _(_)',

, ,
, . "" ,

_ROWNUM_ IAST:NAME;
1
Norton


""
SENDS*

Gutwirth

3
4

Trumble
Fletcher

Zoraster
Moss
Allen
Smith

6
7
8

ROWNUM
1
2

FEMALE;
1

UNKNOWN' "'
1
1
1

3
4
5

6
7

F
M

MALE

. 6.

182

,
, .
, .

?
(constraint) , , Oracle .
. . - INSERT
UPDATE , Oracle ,
.


.


""
.
. ..
,:
.
;

NOT NULL
2, CREATE TABLE, null-.
, :
CREATE TABLE plsql!01_purchase (
product_name VARCHAR2(25) NOT NULL,
product_price NUMBER(4,2} NOT NULL,
purchase_date DATE

,
null- . NOT
NULL :
ALTER TABLE _ MODIFY (_ NOT NULL);
. PLSQL101_PERSON FIRST_NAME LAST_NAME; ,
.
, . 6.5:
ALTER TABLE plsql!01_person MODIFY (first_name NOT NULL);
ALTER TABLE plsql!01_person MODIFY (last_name NOT NULL);
.
, . 6.6.
INSERT INTO plsql!01_person VALUES (
'XL1, 'Xaviera1, NULL, 45-NOV-03'
.'


A Oracle SQL'Plus
Be Edit Search flptions Help
SQL> flLTER TflBLE plsqll81_person MODIFY (first_nane HOT HULL);

183

Table altered.
SQL> BLTER TflBLE plsq!101_person MODIFV (last_name NOT NULL);

Table altered.

SQL>

. 6.5.

A Oiacle SQL'Plus
File Edit 2each Qpfons Help
SQL> INSERT INTO plsqllB1_person UflLUES (
2
' X L 1 , 'Xauiera', NULL, '15-NOU-831
3
)

* ;

INSERT INTO plsqll01_person UflLUES (


*

ERROR at line 1:
-11*: cannot insert NULL into (l>PLSQL1fl1"."PLSQL101_PERSON".'fLflST_Hfll1E")
SQL>

. 6.6. NOT NULL


( ), Oracle , . ( 8 , ,
.)

UNIQUE

,
PLSQL101_PERSON . -

184

. , ? . , ,
, ; ,
. :
- ,
.

,
. , , .
, "" .
, ,
. ,
PLSQLIOI^PERSON, , , . ,
. ,
, .
, ,
, .
, ,
, :
ALTER TABLE _
ADD CONSTRAINT _ UNIQUE
(_) ;
, .
. 6.7.
ALTER TABLE plsqllOljperson
ADD . CONSTANT. plsqllQl_person_un;Lque .UNIQUE ,(
first_name,
last_name,
hiredate '


, . 6.8.
INSERT INTO plsqll01_person VALUES' ('
'LN', 'Linda', 'Norton', ' Ol-JUN-03 ' ) ;
INSERT INTO plsqll01_person VALUES (< ;
'NL', 'Linda', 'Norton1, ' Ol-JUN-03 ' ) ;

185

Oracle SOL'Plu
* Search Options Help
SQL> ALTER TABLE plsq!101_person
2 ADD CONSTRAINT plsql101_person_unlque UNIQUE (

3
4
5

first_nane,
last name.
hire~date

Table altered.
SQL>

. 6.7.

Oiitcle SQL-Plus

Je dit Search Qptions *


SQL> INSERT INTO plsqll01_person UALUES (
2
' L N ' , 'Linda 1 , Norton*. B1-JUN-B31);

'

1 row created.
SQL>
SQL> INSERT INTO plsqll81 person UALUES (
2
-NL 1 . -Linda', TNorton', 81-JUN-B3>);
INSERT INTO plsqll01_person UALUES (

ERROR at line 1:
ORA-oeOBI: unique constraint (PLSQL1B1.PLSQL101_PERSON_UNIQUE) uiolated
SQL>

. 6.8.
, ,
, , ,
, .
, ,
(PLSQL101_PERSON_UNIQUE). , ( 30 , ).
,
.

186

, Oracle (unique index). ,


ALTER TABLE...ADD CONSTRAINT, . Oracle
. , Oracle . ,
. , ALTER
TABLE...ADD CONSTRAINT .

CHECK

(check constraint) ,
, Oracle
. . , ,
, ,
. ,
.

:
ALTER TABLE _ ADD
CONSTRAINT [_]
1_ )

_, , , . _ .
, Oracle ,
- . ,

, ,
.
_ ,
. , , .
,
WHERE SELECT.
.
PLSQL101_PURCHASE . ( ""
30 2000 .
.) :
,
.
AND.

187

,
( , ,
,
null- ).
TO_CHAR.
(, , )
Oracle,

. "",
.
("")
("JAN"), "F" ".
, ,
, ( )
.
,
. 6.9.
* Oiacle SQL-Plus
File Edit Search Options Help
SQL> ftLTER TftBLE plsql1B1_purchase ROD (

2
3

5
6
7
8

Table

CONSTRAINT reasonable_date CHECK(


purchase_date IS NOT NOLL
AND
TO_CHflR(purchase_date, VYVV-MM-DD 1 ) >- '20-06-'

altered.

SQL>
SQL> INSERT INTO plsql1B1 purchase UALOES (
2
'Small Midget 1 . 10, '28-FEB-OB', 'GO');
INSERT INTO plsql101_purchase UALOES (

ERROR at line 1:
ORft-02290: check constraint (PLSQL101.REflSONflBLEJJflTE) violated
SQL>

JJJ

. 6.9.
ALTER TABLE plsql!01_purchase ADD (
CONSTRAINT reasonable_date CHECK(
purchase_date IS NOT NULL

188
AND

TO_CHAR(purchase_date, ' YYYY-MM-DD' ) >= '2000-06-30'

INSERT INTO plsql!01_purchase VALUES (


'Small Widget', 'GA', '28-FEB-OO'

10);

, null-; , , ,
. Oracle
. IS NOT NULL,
null- . , ,
. 6.10.

.+ Oracle SQL-Plus

ie Ed 'Search- flptioris Help r:


'
' ,: '
, " ' :
SQL> ALTER TABLE plsqlioi_product ADD (
2
CONSTRAINT reasonable stock_date CHECK(
3
TO_CHAR(last_stock_date, ' VVYY-MM-DD ' ) >- '2801-12-31'

Table altered.
SQL>
SQL> INSERT INTO plsql101_product UALUES (
2
'Anodized Franifier1. 49, 5, NULL)

3 ;

1 row created.
SQL>
SQL> INSERT INTO plsql101 product UALUES (

3 ;

'Spring-Loaded Pit Puller 1 , 49, 5, '30-DEC-01')

'Spring-Loaded Pit Puller', 49, 5, '3B-DEC-ei')

ERROR at line 2:
ORft-82298: check constraint (PLSQL1D1.REASONABLE_STOCK_DATE) violated
SQL>
SQL>

. 6.10. ,
null-

189

ALTER TABLE plsql!01_product ADD {


CONSTRAINT reasonable_stock_date CHECK(
TO_CHAR(last_stock_date, 'YYYY-MM-DD') >= '2001-12-31'
)
)
INSERT INTO plsql!01_product VALUES (
'Anodized Framifier', 49, 5, NULL)
/

INSERT INTO plsq!101_product VALUES {


'Spring-Loaded Pit Puller', 49, 5, '30-DEC-01')
f

, , . , , ;
. -
NULL, . (
, .
,
.)
CREATE TABLE plsql!01_person (
person_code
VARCHAR2(3) NULL,
first_name
VARCHAR2(15) NOT NULL,
last_name
VARCHAR2(20) NOT NULL,
hire_date
DATE NULL
CONSTRAINT reasonable_liire_date CHECK (
TO_CHAR(hire_date, 'YYYY-MM-DD') >= 4930-01-01'
)
)
CREATE TABLE plsql!01_product (
product_name
VARCHAR2(25) NULL,
product_price
NUMBER(4,2) NULL
CONSTRAINT validjorice CHECK "(
product_price BETWEEN 0 AND 10000
),
quantity_on_hand
NUMBER(5) NULL
CONSTRAINT positive_quantity CHECK (
quantity_on_hand >= 0
),
last_stock_date
DATE NULL
CONSTRAINT reasonable_stock_date CHECK (
TO_CHAR(last_stock_date, 'YYYY-MM-DD1)
>= '2001-12-31'

190

, ,
. " " , . ,
. ,
EMPLOYEE, ,
,
25%:
ALTER TABLE employee ADD (CONSTRAINT realistic_current_salary CHECK (
salary BETWEEN prior_j/ear_salary AND (prior_year_salary*l . 25) )



, , . ,
,
,
Oracle. ALTER TABLE, , .
,
.
:
ALTER TABLE ^ DISABLE CONSTRAINT _;
:
ALTER TABLE _ ENABLE CONSTRAINT _;
, ,
. ,
LAST_STOCK_DATE
. , .
, , .
. . 6.11.
INSERT INTO plsq!101_product VALUES (
'Red Snaphoo 1 , 1.95, 10, '30-DEC-01')

ALTER TABLE plsql!01_product DISABLE

CONSTRAINT reasonable_stock_date;

INSERT INTO plsql!01_product VALUES (


'Red Snaphoo', 1.95, 10, ' 3 0 - D E C - 0 1 ' )

191

ALTER TABLE plsql!01_product ENABLE CONSTRAINT


reasonable stock date;
UPDATE plsql!01_product
SET
last_stock_date = '31-DEC-01'
WHERE last_stock_date = '-30-DEC-01';
ALTER TABLE plsql!01_product ENABLE CONSTRAINT
reasonable stock date;

Oiacle SQL-Plus

HHBi

File Edit Search Qpfons Help


SQL> IHSERT INTO plsqll01 product UflLUES (
1
1
2
'Red Snaphoo , T.95, 10, '30-DEC-01 )
3 ;
INSERT INTO plsqll01_product UflLUES (

ERROR at line 1:
;;

ORA-02290: check constraint (PLSQL1B1.REflSONfiBLE_STOCK_DflTE) violated


. . - .

SQL>
SQL> flLTER TABLE plsqll01_product DISflBLE CONSTRAINT reasonable_stock_date;

Table altered.
SQL> INSERT INTO plsql101_product UflLUES (
2
'Red Snaphoo', 1.95, 10, '3B-DEC-011)

1 row created.

SQL>
SQL> ALTER TABLE plsqll01_product ENABLE CONSTRAINT reasonable_stock_date;
ALTER TABLE plsqll01_product ENABLE CONSTRAINT reasonable_stock_date
*
ERROR at line 1:
ORA-B2293: cannot enable (PLSQL101.REASONABLE_STOCK_DATE) - check constraint
violated

.
SQL>
SQL> UPDATE plsqll B1_product

2 SET
last stock_date = '31-DEC-01'
3 WHERE lasOtock_date = '-DEC-ei';

1 row updated.
SQL>
SQL> ALTER TABLE plsqll 01 product ENABLE CONSTRAINT reasonable_stock_date;

Table altered.
SQL>

. 6.11.

192



, ,
. , NULL NOT NULL
. , null-, :
ALTER TABLE _ MODIFY (_ NULL);
, null-, :
ALTER TABLE _ MODIFY (_ NOT NULL);
, , . 6.12:
ALTER TABLE plsql!01_person MODIFY (first_name NULL);
ALTER TABLE plsql!01_person MODIFY (last_name NULL);


ite Edit Search Options Help
SQL> ALTER TABLE plsql101_person MODIFV (firstjiane NULL);

Table altered.
SQL> ALTER TABLE plsqll01_person MODIFV (last_nane NULL);

Table altered.
SQL>

. 6.12. NULL
-:
'
-'' '
,
:
ALTER TABLE _ DROP CONSTRAINT _;

,
. ,
, , .
.
. 6.13.

_______

193
1-

ie:(Jt Search Qptos: Help


:'
SQL> INSERT INTOplsql101_product UALUES (
2
'Blue Snaphoo'. 1.95, 10, 3D-DEC-01 1 )

3 ;
Blue Snaphoo', 1.95, 18, 3B-DEC-B1')
*

ERROR at line 2:
-0229: check constraint (PLSQL1B1.REASONABLE_STOCK_DATE) violated
SQL>
SQL> ALTER TABLE plsq!1B1_product DROP CONSTRAINT reasonable_stock_date;

Table altered.
SQL>
SQL> INSERT INTO plsql101_product
UALUES (
1
1
2
'Blue Snaphoo . 1.95, 1B. '3B-DEC-B1 )
3 ;

-,'

1 row created.

' , ' ; , -

'

"

'

j -

SQL> |

. 6.13.
INSERT INTO plsqll01_product VALUES (
'Blue Snaphoo1, 1.95, 10, '30-DEC-01')

ALTER TABLE plsql!01_product DROP CONSTRAINT reasonable_stock_date;


INSERT INTO plsql!01_product VALUES (
'Blue Snaphoo', 1.95, 10, '30-DEC-01')


?

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

194

(, -) .
, - ? ,
SQL*Plus,
Microsoft Access Excel.
, , , ,
. , , , ,
1800- . : - ,
. , .
, ,
. , . ,
. ,
,
.

,
, .
,

, .


, ,
, , . , ,
.


, ,
.
PLSQL101_PRODUCT

PLSQL101_PURCHASE, PLSQL101_PERSON SALESPERSON PLSQL101_PURCHASE.


. , , .

195

. 6.14 .
, , , ,
,
, . , . 6.14, PLSQL101_ (parent), PLSQL101_PURCHASE (child).
"/" PLSQL101_PERSON
PLSQL101_PURCHASE.
. ,
PLSQL101_PERSON
PLSQL101_PURCHASE. , PLSQL101_PURCHASE , ,
. ,
" " (one-to-many).
, .
, . ""
" ".
. . 6.14 IDEF1X, "IDEF" "Integration DEFinition for Information Modeling", a "IX" , ( IDEF,
).
PLSQL101 PRODUCT
PRODUCT_NAME
PRODUCT PRICE
QUANTITY ON HAND
LAST STOCK DATE

PLSQL101 PERSON
PERSON_CODE
RRST NAME
LAST NAME
HIRE DATE

PLSQL101 PURCHASE
PRODUCT NAME
SALESPERSON
PURCHASE DATE
QUANTITY

. 6.14. PL/SQL 101 ( IDEF1X)

196

IE, "Information Engineering". , . 6.15, " "


("Crow's Foot") - ,
"" " ".
, , . 6.14 6.15,
"-" (Entity Relationship Diagrams, ERD). (data models).
.
' . N .



, :
( ),
.
(
) ,
.
, PLSQL101_PRODUCT . ( ) . (primary key) .
. PLSQL101_PERSON .
PLSQL101_PRODUCT
PRODUCT NAME
PRODUCT PRICE
QUANTITY ON HAND
LAST STOCK DATE

PLSQL101 .PERSON
PERSON CODE

FIRST NAME
LAST NAME
HIRE'DATE

II

AA

PLSQL101 PURCHASE
PRODUCT NAME
SALESPERSON
PURCHASE DATE
QUANTITY
. 6.15. PL/SQL 101 ( IE)

197

. ( ),
, , , , . .
, ,
. .
, . (
), ,
. ,
, ^) . , , ,
, ,
.
,
, (foreign keys).
. , PLSQL101_PURCHASE PLSQL101_PRODUCT. ,
PLSQL101_PURCHASE
PLSQL101_PURCHASE. , "(FK)".
PLSQL101 PRODUCT
PRODUCT NAME
PRODUCT PRICE
QUANTITY ON HAND
LAST STOCK DATE

PLSQL101 PERSON
PERSON.CODE
FIRST NAME
LAST NAME
HIRE DATE

*'*.
PLSQL101 PURCHASE
PRODUCT NAME (FK)
SALESPERSON (FK)
PURCHASE DATE
QUANTITY

198

, ,
.
.

, , ,
:
ALTER TABLE _
ADD PRIMARY KEY (_&_1, __2,...);

j||

> *""

, Oracle
. ,
, WHERE
.

PLSQL101_PRODUCT,
. 6.16:
ALTER TABLE plsql!01_product
ADD PRIMARY KEY (product_name);

:
ALTER TABLE plsql!01_person
ADD PRIMARY KEY (person_code);
ALTER TABLE plsql!01_purchase
ADD PRIMARY KEY (product_name,
salesperson,
purchase_date

f. Oracle SQL'Plus
File Edit Search Qptions Help
SQL> ALTER TABLE plsqll Byproduct
2 ADD PRIMARV KEV (product_name);

Table altered.
SQL>

. 6.16.

199

. , ,
, PLSQL101_PRODUCT,
:
CREATE TABLE plsql!01_product2 (
product_name
VARCHAR2(25) PRIMARY KEY,
product_price
NUMBER (4,2),
quantity_on_hand
NUMBER (5, 0) ,
last stock date
DATE


,
.
.
, Oracle
, .
: , ,
, .
, .
. , . 6, 1 7 ,
:
SELECT product_name
FROM
plsql!01_product
ORDER BY product_name;
SELECT DISTINCT product_name
FROM
plsql!01_purchase
ORDER BY product_name;

, 80101_.8
"Round Snaphoo", PLSQL101_PRODUCT.
, .
, , Oracle.
:
ALTER TABLE __
ADD CONSTRAINT _
FOREIGN KEY (___)
REFERENCES __

200

.* OiacleSUL'Plus
file dil Search flptions Help
SQL> SELECT product_name
2 FROM
plsq!1B1_product

S3

3 ORDER BV product_name;
PRODUCT

NftME

Anodized FraniFier
Blue Snaphoo
Chrome Phoobar
Extra Huge Hega Phoobar +
Mediun Uodget
Red Snaphoo
Round Chrome Snaphoo
Small Widget
Square Zinculator
9 rows selected.
SQL>
SQL> SELECT DISTINCT product name
2 FROM plsql101_purchase
3 ORDER BV product_name;
PRODUCT_NAME

Chrome Phoobar
Medium Uodget
Round Snaphoo
Snail Widget
SQL>

. 6.17. ,

:
ALTER TABLE plsql!01_purchase
ADD CONSTRAINT plsql!01_purchase_fk_product
FOREIGN KEY (product_name)
REFERENCES plsqll01_product
r

, , . 6.18. "parent
key not found" ,
, . , .
, :

201

* Oiacle SOL'Plus

file dir Sch Option, HetfS;,'

SQL> ALTER TABLE plsqll61 purchase

2 ADD CONSTRAINT plsqlToi_purchase_fk_prodiict


3
FOREIGN KEV (product )
4
REFERENCES plsqll81 product;
ALTER TABLE plsqllB1_purchase

-J
:

ERROR at line 1:

ORA-02298: cannot enable (PLSQL181.PLSQL101_PURCHASE_FK_PRODUCT) - parent key

not found

SQL>

. 6.18.

UPDATE plsql!01_purchase
SET
product_name = 'Round Chrome Snaphoo'
WHERE product_name - 'Round Snaphoo';
ALTER TABLE plsql!01_purchase
ADD CONSTRAINT plsql!01_purchase_fk_product
FOREIGN KEY. (product_name)
REFERENCES
plsql!01_product;

,
:
INSERT INTO plsql!01_purchase VALUES (
'Small Widgee', 'CA', '17-JUL-03', 1)

. 6.19. , , . ,
, :
INSERT INTO plsqlldl_purchase VALUES (
'Small Widget', 'CA1, '17-JUL-03', 1)

,
PLSQL101_PURCHASE,
PLSQL101_PERSON SALESPERSON.

202

* Oiacle SQL-Plus
Fife

Edk

Search

fiptions

Help

SQL> INSERT INTO plsql1B1_purcliase UftLUES (


2
'Snail Uidgee', 'Cfl 1 , 17-JUL-831. 1)

ERROR at line 3:
ORfl-02291: integrity constraint <PLSqL101.PLSQLiei_PURCHflSE_FK_PRODUCT)
violated - parent
key
f
not found

SQL>

. 6.19.

,
ALTER TABLE
,
.
, Oracle .
,
, .

SELECT,


, , , . , , ,
. ,
. SELECT, ,
:
SELECT __1._,
__2. _
FROM __1,
__2
WHERE __._ =
__._
5

, , SELECT ,

203

.'

:
__1, __2, __
__. . , , :
, , ; , , (
). WHERE, ,
, . SELECT "/", Oracle ,
.
,
. ,
:
-

SELECT plsql!01_purchase.product_name,
plsqll01_person.last_name,
plsql!01_person.first_name,
pisql!01_purchase.quantity
FROM
plsql!01_purchase,
plsql!01_person
WHERE plsql!01_person.person_code = plsql!01_purchase.salesperson

. 6.20.
, , (join). ,
, : SELECT WHERE, ,
.
, Oracle ,
. (
, .) ,
WHERE,
. 6.21.
SELECT plsqll01_purchase.product_name,
plsqll01_person.last_name,
plsql!01_person.first_name,
plsql!01_purchase.quantity
FROM
plsql!01_purchase,
plsql!01_person
i

PLSQL101_PERSON
PLSQL101_PURCHASE 35

204

'

'

I -

-i

* Oracle SQL'Plus
Je d* Search Qptions Help
SQL> SELECT plsqll U1_purchase . produc t_name ,
2
plsqll
fl1_per
son.
las
t_nane,

3
plsql101_person.first_nane,
4
plsql101 purchase. quantity
5 FROM plsqll 01_purchase,
6
plsqll 01_person
7 WHERE plsqll B1_person.person_code - plsql101_purchase. salesperson

'

.d
J
f'-

8 ;
PRODUCT_HAME

LAST_NAME

FIRST_NAME

Small Widget
Round Chrome Snaphoo
Small Widget
Chrome Phoobar
Small Widget
Medium Wodget
Medium Wodget

Atlas
Atlas
Atlas
Anderson
Anderson
Barkenhagen
Baxter

Charlene
Charlene
Charlene
Gary
Gary
Bobby
Laren

7 rows selected.

QUANTITY
1
5
1
Z

75
20
"'' - '"'

SQL> |

'

' "''

;-' ' '

"; >

. 6.20. SELECT,
. ,
WHERE, .
SELECT . : ()
/ , () WHERE SELECT.
, .
. , , :
SELECT plsq!101_purchase.product_name,
plsql!01_product.product_price,
pisql!01_purchase.quantity,
plsql!01_person.last_name
FROM
plsql!01_product,
plsql!01_person,
plsqll01_purchase
WHERE plsqll01_product.product_name = plsql!01_purchase.product_name
and
plsql!01_person.person_code = plsql!01_purchase.salesperson

.
SELECT, , -

205

J-* tlrocle SQL'Plus

!e dit Seech fipdoru Help


SQL> SELECT plsql101_purchase.product_name,

plsql101_person.last_nari)e.

plsql1B1_person.First_name,

FROM

i 7 ;

plsql101~purchase. quantity
plsql101_purchase,

plsq!101 person

PRODUCT_NAME

LflSTJMME

FIRST_NflME

Small Widget
Medium Wodget
Chrone Phoobar
Small Widget

Atlas
Atlas
Atlas
Atlas
Atlas
Atlas
Atlas
Anderson
Anderson
Anderson
Anderson
Anderson
Anderson
Anderson
Barkenhagen
Barkenhagen
Barkenhagen
Barkenhagen
Barkenhagen
Barkenhagen
Barkenhagen
Baxter
Baxter
Baxter

Charlene
Charlene
Charlene
Charlene
Charlene
Charlene
Charlene
Garu
Garu
Gary
Gary
Gary

HediuR Wodget

Round Chrome Snaphoo


Small Widget
Small Widget
Medium Wodget

Chrone Phoobar
Small Widget
Medium Wodget

Round Chrone Snaphoo

Small Widget
Small Widget
Medium Wodget
Chrone Phoobar
Small Widget
Medium Wodget
Round Chrone Snaphoo
Small Widget
Small Widget
Medium Wodget
Chrome Phoobar

Small Widget

Medium Wodget
Round Chrome Snaphoo
Small Widget

Small Widget
Medium Wodget
Chrome Phoobar
Small Widget
Medium Wodget
Round Chrome Snaphoo
Snail Widget

Baxter

Baxter
Baxter
Baxter
Morton

Norton
Norton

Norton
Morton
Morton
Norton

QUflNTITV

75

, . 2; .;:

20
5
1

,
75
2

8
21
5
1'
1

Gary

Gary

Bobby
Bobby
Bobby
Bobby
Bobby
Bobby
Bobby

75

2
8
20
5
1
1
75
2
8
20
5
1
1
75
2
8
20
5
1

Laren
Laren
Laren
Laren
Laren
Laren
Laren
Linda

Linda
Linda
Linda
Linda
Linda
Linda

., -

35 rows selected.
SQL>

.iLJ

. 6.21. WHERE

jf*

206

6
_

,
.
, . (alias)
.
FROM. ,
:
SELECT c.product_name,
.product_price,
.quantity,
b.last_name
FROM

plsql!01_product a,
plsqll01_person b,
plsql!01_purchase

WHERE

a.product_name = .product_name
and
b.person_code = .salesperson

, ,
, a, b . , , .
.
, . .
.
SELECT purc.product_name,
prod.product_price,
pure.quantity,
pers.last_name
FROM
plsqll01_product prod,
plsql!01_person pers,
plsqll01_purchase pure
WHERE prod.product_name = purc.product_name
and
pers.person_code = pure.salesperson
i.

,
, , . , Oracle , , ,
.
: -,
( Oracle , -

207

, ), -,
.


, ,
:
SELECT product_name FROM plsql!01_product ORDER BY product_name;
SELECT prod.product_name,
prod.product_price,
pure.purchase_date,
pure.quantity
FROM
plsql!01_product prod,
plsql!01_purchase pure
WHERE prod.product_name = purc.product_name
ORDER BY prod.product_name;

, ,
. , WHERE
SELECT , , ,
.
, , . ()
,
. , Oracle, .
(+) WHERE. :
SELECT product_name FROM plsql!01_product ORDER BY product_name;
SELECT prod.product_name,
prod.product_price,
purc.purchasejdate,
pure.quantity
FROM plsql!01_product prod,
plsq!101_purchase pure
WHERE prod.product_name = purc.product_name (+.)
ORDER BY prod.product_name;
,
, . (outer join). (+),
__._ =
__._ WHERE, ,
,
. , (+) ,
. , (+,).
8 . 725

208

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


, ,
.
, , ,
, , .
(join operators), SELECT.
; 6.3.
, .

1
UNION

UNION AT All

6.3. .

UNION

SELECT;

UNION ALL

SELECT;

INTERSECT

, , SELECT

MINUS

,
SELECT, ,

209

, , .
.
CREATE TABLE plsqll01_purchase_'archive (
product_name
VARCHAR2 (25) ,
salesperson
VARCHAR2(3),
purchase_date
DATE,
quantity
NUMBER (4, 2)

INSERT INTO plsql!01_purchase_archive VALUES


('Round Snaphoo', 'BB', ' 21-JUN-01 ' , 10);
INSERT INTO plsqll01_purchase_archive VALUES
('Large Harf linger ', 'GA', '22-JUN-01', 50);
INSERT INTO plsql!01_purchase_archive VALUES
( 'Medium Wodget', 'LB', '23-JUN-01', 20);
INSERT INTO plsqll01_purchase_archive VALUES
('Small Widget' , 'ZZ', '24-JUN-02 '-, 80);
INSERT INTO plsql!01_purchase_archive VALUES
. ('Chrome Phoobar', 'CA', '25-JUN-02', 2) ;
INSERT INTO plsql!01_purchase_archive VALUES
('Small Widget', 'JT', '26-JUN-02', 50);

UNION
UNION
. ,
, ,
, .
, ,
. 6.22:
SELECT product_name FROM plsql!01_purchase
ORDER BY product_name;
SELECT product_name FROM plsq!101_purchase_archive
ORDER BY product_name;
SELECT product_name FROM plsql!01_purchase
UNION
SELECT product__name FROM plsql!01_purchase_archive
ORDER BY product_name;

, , . , 801_.8
"Round Chrome Snaphoo", PLSQL101_
PURCHASE_ARCHIVE, a "Large Harflinger" ,
. , UNION,
, ,
.
8*

210
* Otacle SQL-Plus
te dit Search flptions H<*
SQL> SELECT product_name F R O M plsqll repurchase
2 ORDER BV product_nane;
PRODUCT
Chrome Phoobar
Medium Uodget
Medium Uodget
Round Chrome Snaphoo
Small Midget
Snail Widget
Small Widget
7 rows selected.
SQL> SELECT product_name FROM plsqliai_purchase_archiue
2 ORDER BV product_name;
PRODUCT
Chrome Phoobar
Large HarFlinger
Medium Uodget
Round Snaphoo
Small Midget
Snail Midget

6 rows selected.
SQL>
SQL>
2
3
4

SELECT product_nane FROM plsq!101_purchase


UNION
SELECT product_nane FROM plsql181_purchase_archiue
ORDER BV product_nane;

PRODUCT NOME
Chrome Phoobar
Large HarFlinger
Medium Uodget
Round Chrome Snaphoo
Round Snaphoo
Small Widget
rows selected.
SQL>

-iLJ

. 6.22. UNION

UNION ALL
UNION ALL
UNION, ,
. , , :
SELECT product_name FROM plsql!01_purchase
UNION ALL
SELECT product_name FROM plsql!01_purchase_archive
ORDER BY product_name;

_____

211

,
.

INTERSECT
INTERSECT ,
. ,
. , ,
. :
SELECT product_name FROM plsq!101_purchase
INTERSECT
SELECT product_name FROM plsql!01_purchase_archive
ORDER BY product name;
'

'

' ' - '

'

'

MINUS

MINUS INTERSECT.
, . , ,
, , . :
SELECT product_name FROM plsql!01_purchase
MINUS
SELECT product_name FROM plsql!01_purchase_archive
ORDER BY product_name;


, , ,
.

?
SELECT,
SELECT, UPDATE DELETE. FROM WHERE .
. Oracle , . " " "
, ,
- ".

,

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

212

. ,
, , , .

.


. ,
Small Widget - ,
. , ,
. , Small
Widget, WHERE. Oracle ,
. WHERE - .
SELECT,
Small Widget, .
,
. 6.23:
SELECT *
FROM
plsqllOl_J>roduct

WHERE last_stock_date = (
SELECT last_stock_date
FROM

plsg!101_product

WHERE product_name = 'Small Widget'

, SELECT,
, Red Snaphoo.

ite d Search flptiohs


SQL> SELECT "'

2 FROM
3 WHERE
4
5
6

7
e ;

H*

plsql1B1 product
last_stock date - (
SELECT last stock date
FROM plsqltBI product
WHERE product name - 'Snail Widget

PRODUCT_NAHE

PRODUCT_PR1CE QUflNTITV_ON_HflND LfiST_STOC

Snail Widget

99

1 15-JAH-03

Chrone Phoobar

SB

100 15-JftN-B3

SQL>

. 6.23. ,

213

. 6.24.
1.95 WHERE;
, Red Snaphoo.

, . ,
Small Widget,
,
. WHERE .
( ,
, .)
(single-row subquery), . ( ,
.)

. , ,
. , , . ( ,
, , , , , , .)
, , SELECT
, .
:
SELECT * FROM plsql!01_purchase
WHERE salesperson = (
SELECT person_code
FROM
plsq!101_person
WHERE first_name = 'Gary' AND last_name = 'Anderson'

* Oiacle SQL-Plus
!e fid eqh Options H*'':'

; ;

d
PRODUCT_HflME
Red Snaphoo
Blue Snaphoo

PRODUCT_PRICE QUflHTITV_ON_HflHO LftST_STOC


1.95
1.95

10 31-DEC-01
10 30-DEC-Q1

SQL>
SQL>
SQL>

. 6.24. ,

214

, . , :
SELECT *
FROM
plsqll01_product
WHERE product_price > (
SELECT AVG(product_price)
FROM
plsql!01_product


, (multirow subquery)
, . ; IN.
, , .
, ,
. . 6.25:
SELECT *
FROM
plsql!01_purchase
ORDER BY product_narae;
SELECT *
FROM
plsql!01_product
WHERE product_name NOT IN (
SELECT DISTINCT product_name
FROM
plsql!01_purchase

ORDER BY product_name

, UPDATE DELETE. , 10%


, . UPDATE, WHERE , , :
SELECT * FROM plsql!01_product;
UPDATE plsqllOl_product
SET
product_price = product_price * .;9
WHERE product_name NOT IN (
SELECT DISTINCT product_name
FROM plsql:101_purchase
SELECT * FROM plsql!01_product;

215

is

* Oiacle SQf Plus


File Edit Jearch Options Help
SQL> SELECT *
2 FROM plsql1B1_purchase
3 ORDER BV pi-oduct_name;
PRODUCT_NflHE

SflL PURCHflSE_

Chrome Phoobar
Medium Uodget
Mediun Uodget
Round Chrome Snaphoo
Small Widget
Small Widget
Snail Widget

BB
LB

14-JUL-83
14-JUL-B3
15-JUL-B3
16-JUL-B3
14-JUL-83
15-JUL-03
17-JUL-03

QUflNTITV

2
75
20
S
1
8
1

7 rows selected.
SQL>
SQL> SELECT *
2 FROM
plsquei product
3 WHERE product name HOT IN (
4
SELECT DISTINCT product name
5
FROM
plsq!1B1 purchase

4
)
7 ORDER BV product
8
'
PRODUCT_NfiM
flnodized FramiFier
Blue Snaphoo
Extra Huge Mega Phoobar *
Red Snaphoo
Square Zinculator

:,.

. '

PRODUCT_PRICE qUflNTITV_DH_HflNO LflST_STOC

49
1.95
9.95
1.95
45

10
1234
10
1

3B-DEC-01
l5-JflN-0i
31-OEC-01
31-OEC-82

SQL>

. 6.25.

,
, . , ?

,

, , . . ,
PLSQL10 1_PURCHASE, . ( , PL/SQL,
. ; .)

216

SELECT *
FROM
plsql!01_purchase
ORDER BY product_name, purchase_date;
SELECT
FROM
WHERE
IN

*
plsqllOi_purchase
(pro'duct_name, purchase_date)
(SELECT product_name, MAX(purchase_date)
FROM
plsql!01_purchase
GROUP BY product_name

Oracle. :

, , , . , Oracle .
INSERT, UPDATE DELETE , SELECT
, .
- , .
, , .
,
. INSERT; ,
(). , .
, .
*- . *- Oracle .
, , , .
, , ,
, /.
*-.
,
,
. ,
,
. , . -

217

INSERT UPDATE , Oracle , .


, , . , , .

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

1. ?
A. INSERT
B. UPDATE
C. SELECT
D. DELETE
2.
?
CREATE INDEX plsqll01_purchase_pk ON plsqll01_purchase (
product_name,
salesperson,
purchase_date
);
A. 1
B-2

D.4

E.
3.
?
.

218

6
/

B. *-
C.
D.
4. , , , ?
A. CREATE INDEX _ ON _(_)\
B. ALTER TABLE _ MODIFY (_ NOT NULL);
C. ALTER TABLE _ ADD CONSTRAINT _
UNIQUE (_);
D. ALTER TABLE _ ADD CONSTRAINT _
CHECK/_ ,);
E. ALTER TABLE _ ADD CONSTRAINT
FOREIGN KEY (_) REFERENCES __ ;
5. , 1 , 2 ?
SELECT
FROM

_ _ 1._ _1,
_2.__2
_ _1,
_2

A. 1.
B. 2.
C. .
D. .
E. , 1 , 2.


1. .

INSERT

, ,
, INSERT
- .
, ,
.
2. .

" ",
CREATE INDEX.


3..

219

*-


; .
, ..
. D ("") , *-,
.
4. .

ALTER TABLE _
ADD CONSTRAINT _
FOREIGN KEY (_) REFERENCES _ ;

"REFERENCES" .
.
5. D.

SELECT
, WHERE, ,
, . 1 1
2 2, 5*10, .. 50 .

'



Oracle

222

,
Oracle SQL. , ; ; Oracle;
, . ,
, PL/SQL.
, , ,
. ( ,
" ".)
DROP
DROP
DROP
DROP
DROP

TABLE
TABLE
TABLE
TABLE
TABLE

plsql!01_purchase;
plsql!01_product;
plsql!01_person;
plsqll01_old_item;
plsqll01_purchase_archive; '

CREATE TABLE plsql!01_persbn (


person_,code VARCHAR2O) PRIMARY' KEY,
first_name
VARCHAR2(15),
last_name
VARCHAR2(20),
hire_date
DATE

)
;
CREATE INDEX plsql!01_person_name_index
ON plsql!01_person(last_name, first_name);

ALTER TABLE plsql!01_person


ADD CONSTRAINT plsql!01_person_unique UNIQUE (
first_name,
last name,
hire_date

INSERT INTO plsql!01_person VALUES


CCA', 'Charlene', 'Atlas', 'Ol-FEB-02') ;
INSERT INTO plsql!01_person VALUES
('GA', 'Gary', 'Anderson', '15-FEB-02') ;
INSERT INTO plsql!01_person VALUES
('BB', 'Bobby', 'Barkenhagen', '28-FEB-02');
INSERT INTO plsql!01_person VALUES
('LB', 'Laren', 'Baxter', 'Ol-MAR-02');
INSERT INTO plsql!01_person VALUES (
'LN', 'Linda1, 'Norton', 'Ol-JUN-03');
CREATE TABLE plsqll01_product (
product_name
VARCHAR2(25) PRIMARY KEY,
product_price
NUMBER(4,2),
quantity_on_hand
NUMBER(5,0),

. :

Oracle
laststock_date

223

DATE

ALTER TABLE plsql!01_product ADD CONSTRAINT positive_quantity CHECK(


quantity_on_hand IS NOT NULL
AND
quantity_on_hand >= 0

INSERT INTO plsql!01_product VALUES


1
('Small Widget , 99, 1, 45-JAN-03 ' ) ;
INSERT INTO plsql!01_product VALUES
('Medium Wodget', 75, 1000, ' 15-JAN-02 ' ) ;
INSERT INTO plsql!01_product VALUES
1
('Chrome Phoobar , 50, 100, ' 15-JAN-03 ' ) ;
INSERT INTO plsql!01_product VALUES
1
('Round Chrome Snaphoo , 25, 10000, null);
INSERT INTO plsql!01_product VALUES
('Extra Huge Mega Phoobar +', 9.95, 1234, ' 15-JAN-04 ' ) ;
INSERT INTO plsql!01_product VALUES ('Square Zinculator',
45, 1, TO_DATE (' December 31, 2002, 11:30 P.M.',
'Month dd, YYYY, HH-.MI P.M.')

INSERT INTO plsql!01_product VALUES (


'Anodized Framifier', 49, 5, NULL);
INSERT INTO plsql!01_product VALUES (
1
Red Snaphoo ', 1.95, 10, ' 31-DEC-01 ' ) ;
INSERT INTO plsql!01_product VALUES (
'Blue Snaphoo', 1.95, 10, '30-DEC-01')
CREATE TABLE pisql!01_purchase (
product_name
VARCHAR2(25),
salesperson
VARCHAR2(3),
purchase_date
DATE,
quantity
NUMBER(4,2)

ALTER TABLE plsql!01_purchase


ADD PRIMARY KEY (product_name,
salesperson,
purchase_date

ALTER TABLE plsql!01_purchase ADD CONSTRAINT- reasonable_date CHECK(


purchase_date IS NOT NULL
AND
TO_CHAR(purchase_date, 'YYYY-MM-DD1) >= '2000-06-30'

224

ALTER TABLE plsql!01_purchase


ADD CONSTRAINT plsql!01_purchase_fk_product FOREIGN KEY
(product_name) REFERENCES plsql!01_product;
ALTER TABLE plsqll01_purchase
ADD CONSTRAINT plsql!01_purchase_fk_person FOREIGN KEY
(salesperson) REFERENCES plsql!01_person;
CREATE INDEX plsql!01_purchase_product
ON plsql!01_purchase(product_name);
CREATE INDEX plsql!01_purchase_salesperson
ON plsql!01_purchase(salesperson);
INSERT INTO plsql!01_purchase VALUES
1
('Small Widget', 'CA', '14-JUL-03 , 1) ;
INSERT INTO plsql!01_purchase VALUES
1
1
('Medium Wodget', 'BB , '14-JUL-03 , 75);
INSERT INTO plsql!01_purchase VALUES
('Chrome Phoobar', 'GA', 44-JUL-03', 2) ;
INSERT INTO plsql!01_purchase VALUES
1
('Small Widget , 'GA', 45-JUL-03', 8);
INSERT INTO plsql!01_purchase VALUES
('Medium Wodget', 'LB', 45-JUL-03', 20);
INSERT INTO plsql!01_purchase VALUES
('Round Chrome Snaphoo', 'CA', 46-JUL-03', 5);
INSERT INTO plsql!01_purchase VALUES (
'Small Widget', 'CA', 47-JUL-03', 1)
UPDATE plsql!01_product
SET
product_price = product_price * .9
WHERE product_name NOT IN (
SELECT DISTINCT product_name
FROM plsqllOl purchase

CREATE TABLE plsq!101_old_item (


item_id
CHAR (20),
item_desc
CHAR (25)

INSERT INTO plsq!101_old_item VALUES


('LA-1011, 'Can, Small');
INSERT INTO plsql!01_old_item VALUES
('LA-102', 'Can, Large');
INSERT INTO plsql!01_old_item VALUES
('LA-1, 'Bottle, Small');

Oracle

225

INSERT INTO plsq!101_old_item VALUES


CLA-104', 'Bottle, Large');
INSERT INTO plsq!101_old_item VALUES
1
CNY-101', 'Box, Small );
INSERT INTO plsq!101_old_item VALUES
('NY-102', 'Box, Large');
INSERT INTO plsq!101_old_item VALUES
('NY-', 'Shipping Carton, Small')
INSERT INTO plsq!101_old_item VALUES
CNY-104', 'Shipping Carton, Large')
CREATE TABLE plsqll01_purchase_archive (
product_name
VARCHAR2(25),
salesperson
VARCHAR2(3),
purchase_date
DATE,
quantity
NUMBER(4,2)

INSERT INTO plsq!101_purchase_archive VALUES


('Round Snaphoo', 'BB', '21-JUN-01', 10);
INSERT INTO plsql!01_purchase_archive VALUES
1
('Large Harflinger', 'GA', '22-JUN-01 , 50);
INSERT INTO plsql!01_purchase_archive VALUES
('Medium Wodget', 'LB', '23-JUN-01', 20);
INSERT INTO plsql!01_purchase_archive VALUES
('Small Widget', 'ZZ1, '24-JUN-021, 80);
INSERT INTO plsq!101_purchase_archive VALUES
('Chrome Phoobar1, 'CA', '25-JUN-02', 2);
INSERT-INTO plsql!01_purchase_archive VALUES
('Small Widget1, 'JT', '26-JUN-02', 50);


DML,
, : .
:
,
SQL,
.
.
,
.
,
, UPPER, LOWER, LTRIM, RTRIM, SUBSTR,
INSTR, TO_CHAR DECODE.

(data warehouse)

226

?
, ,
COUNT, AVG, MIN GROUP BY.
,
SQL-.



,
,
.

, .
, ,
,
.

INSERT

INSERT , .
,
:
;< -_ CREATE TABLE plsqllQl_purchase_log (
purchase_date
DATE,
product_name
VARCHAR2(25),
product_price
NUMBER(4,2),
quantity
NUMBER(4,2),
sales_first_name
VARCHAR2(15),
sales_last_name
VARCHAR2(2D)
)

PLSQL101_PURCHASE_LOG "" : , ,
, .
: "
Red Snaphoo?" , ,
,
,
.
, , , .
INSERT, PERSON, PRODUCT
PURCHASE. :
INSERT _ (
SELECT

227

Oracle

SELECT SELECT,
, .
PLSQL101.
. 7.1.
INSERT INTO plsqll01_purchase_log (
SELECT purc.purchase_date,
prod.product_name,
prod.product_price,
pure.quantity,
pers.first_name,
pers.last_name
FROM
plsql!01_product prod,
plsql!01_person pers,
plsql!01_purchase pure
WHERE prod.product_name = purc.product_name
AND

pers.person_code = pure.salesperson

SELECT * FROM plsqll01_purchase_log;

Fie

dit

Search

2
3
ft
5
6
7
8
9
10
11

Options

Help

12

BHD

13

pers. person code - pure. salesperson

1
15 ;

^l

SELECT pure. pur chase_date.


prod.product_nane.
prod.product_price,
pure. quantity ,
pers.First_name,
pers. las t_nane
FROM
plsqllfll .product prod.
plsq!101_person
pers.
plsql101_purchase pure
WHERE prod. product name * pure .product_nane

7 rows created.
SQL>
SQL> SELECT FROM plsq!101_purchase_log;
PURCHASED PROOUCT_MAME
1it-JUL-B3
1H-JUL-03
14-JUL-83
15-JUL-83
1S-JUL-03
16-JUL-03
17-JUL-G3

Small Uidget
Medium Wodget
Chrome Phoobar
Snail Widget
Medium Wodget
Round Chrone Snaphoo
Snail Widget

PROOUCT_PRICE
99
75
50
99
75
25
99

quflHTITY S*LES_FIRST_NftM S
1
75
2
8
28
5
1

Charlene
Bobby
Gary
Gary
Laren
Charlene
Charlene

7 rows selected.
SQL>

. 7.1.

228

, PLSQL101_PURCHASE_LOG

PLSQL101_PURCHASE.




, .
, .
CREATE TABLE :
CREATE TABLE __ AS
SELECT
i
SELECT
SELECT, . , , . 7.2.
El

I *. Omcle SOL'Plus

file dil Sech Uptons


SQL> CREATE TABLE plsqll 01_purchase_log2 AS
2
SELECT purc.purcbase_date,
3
prod.product_nane,
U
prod.product_price,
5
pure. quantity,
6
pers.first_nane,
7
pers.last_nane
8
FROM plsqll 01_product prod,
9
plsqll 81 person pers,
11
plsq!101_purchase pure
11
WHERE prod. product name - pure. product nane
12
AND
13
pers.person_code - pure. salesperson

:';'.,

.1

Table created.
SQL>
SQL> SELECT FROM plsqll 01_purchase_log2;
PURCHASE. PRODUCT_NAME

14-JUL-B3 Snail Widget


14-JUL-D3 Medium Uodget
14-JUL-03 Chrome Phoobar
15-JUL-83 Snail Widget
15-JUL-83 Medium Wodget
16-JUL-B3 Round Chrome Snaphoo
17-JUL-03 Snail Widget

7 rows selected.
SQL>
JJ

PRODUCT_PRICE

99
75

51

99
75
25
99

QUANTITY FIRST_NAME

1 charlene

75 Bobby
2 Gary

8 Gary

20 Laren
S Gharlene
1 Charlene

L
A

A
A

A
A

'

. 7.2.

Oracle

229

CREATE TABLE plsql!01_purchase_log2 AS


SELECT pure.purchase_date,
prod.product_name,
prod.product_price,
pure.quantity,

pers.first_name,
pers.last_name
FROM
plsql!01_product prod,
plsqll01_person pers,
plsql!01_purchase pure
WHERE prod.product_name = purc.product_name
AND
pers.person_code = pure.salesperson
SELECT * FROM plsqll01_purchase_log2;


. . :
RENAME __ __; ,
, :
RENAME plsql!01_purchase_log2 TO plsql!01_log;

-,
. , . , .
,
null- .

. .
:
ALTER TABLE _
ADD __ [NOT NULL]
s

,
PLSQL101_LOG. ,
. 7.3.
DESC plsql!01_log
ALTER TABLE plsqll01_log
ADD data_load_date VARCHAR2(8);
DESC plsql!01_log

230

.* Oracle SQL-Plus
File Edk Search Qptions tidp
SQL> DESC plsqUei log
Name

Hlil 13

Null?

Type

DATE
UARCHAR2(25)
NUMBER (it, 2)
NUMBER(it,2)
UARCHAR2(15)
UARCHAR2(2Q)

PURCHASE DATE
PRODUCT NAME
PRODUCT PRICE
qUANTITV
FIRST NAME
LftST_NAME
SQL>
SQL> ALTER TABLE plsqlllH log
2 ADD data_load_date UARCHAR2(8) NULL;
Table altered.
SQL>
SQL> DESC plsql101 log
Name
PURCHASE DATE
PRODUCT NAME
PRODUCT PRICE
qUANTITV
FIRST NAME
LAST NAME
DATA_LOAD_DATE

Null?

Type
DATE
UARCHftR2(25)
NUMBER(lt,2)
HUMBER(4,2)
UARCHAR2(15)
UflRCHftR2(2B)
UARCHAR2(8)

SQL>

. 7..


, : , , , ?
: .
, , :
I
ALTER TABLE _
MODIFY _ __
PLSQL101_LOG,
, . 7.4:
DESC plsqI101_log
ALTER TABLE plsql!01_log
MODIFY data_load_date DATE;
DESC plsql!01_log

231

Oracle

* Uiacle SQL-Plus
File Edit Search Options Help
SQL> DESC plsq!101 log

Null?

PURCHASE DATE
PRODUCT NAME
PRODUCT PRICE
QUANTITY
FIRST NAME
LAST NftME
DATA_LOAD_DATE

Type
DATE
UARCHAR2(25)
HBHBER(4.2)
NUMBER(ll,2)
UARCHAR2(15)
UARCHAR2(20)
UARCHAR2(8)

SQL>
SQL> ALTER TABLE plsql1B1 log
2 MODIFY data_load_date DATE;

'

'

'

Table altered.

SQL>
SQL> DESC plsql101 log
Name

- -

Null?

PURCHASE DATE
PRODUCT NAME
PRODUCT PRICE
QUANTITY
FIRST NAME
LAST NAME
DATA_LOAD_DATE

Type
DATE
UARCHAR2(25)
NUM8ER(l|,2)
HUMBER(it,2)
UARCHAR2(15)
UARCHAR2(20)
DATE

SQL>

jJJ

. 7.4.

null-

,
, . , null-, , , . (,
NOT NULL NULL.)
:
ALTER TABLE _
MODIFY _ NOT NULL
,
,
. . 7.5.
UPDATE plsql!01_log SET data_load_date = 45-DEC-2003';
DESC plsql!01_log
ALTER TABLE plsql!01_log MODIFY data_load_date NOT NULL;
DESC plsql!01_log

232

file Edit Search Options Help


SQL> UPDATE plsql101_log SET data_load_date -

>

15--23

7 rows updated.
SQL>

SQL> DESC plsql101 log


Name

Null?

DATE

f>URCHASE_DATE
PRODUCT_NAME

UARCHAR2(2S)

PRODUCTlPRICE

NUMBER(1,2)
NUMBEHC.,2)

QUANTITY
FIRST_HflME
LAST_NflME

UARCHAR2(15)
UARCHAR2(2D)
DATE

DATfl_LOAD_DATE
SQL>

SQL> ALTER TABLE plsqliei_log HODIFV data_load_date NOT NULL;


Table altered.
SQL>
SQL> DESC plsql101 log
Name
PURCHASE_DATE
PRODUCT_NAME
PRODUCT_PRICE
QUANTITY
FIRST_NAME
LAST_NAME
DATfl_LOAD_DATE

Null?

Type

DATE
UARCHAR2(2S)
NUMBER(4,2)
NUMHER(U,2)
UARCHAR2(15)
UARCHAR2(2e)
NOT NULL DATE

SQL> |

. 7.5. null-

(view) : , , Oracle , .
, Oracle , , ,
. , . , .
? . ,
.
,
, ,
.
,
, ^. ,

Oracle

233

, . .
:
WHERE,
.
, . , , ,
, . , ,
. ,
, ID101, ID205, ID3322 .. , , ,
,
, .
, . ; ,
.


.
SELECT, . :
CREATE OR REPLACE VIEW _ AS
SELECT
j
, : OR REPLACE.
,
. (,
.)
, ,
. 7.6:
SELECT * FROM plsql!01_purchase;
CREATE
SELECT
FROM
WHEJ^E

OR REPLACE VIEW plsql!01_sales_by_atlas_v AS


*
plsql!01_purchase
salesperson = 'CA'

SELECT * FROM plsqllOl_ sales_ by


j_ atlas_v;

234

Eh * Search Option Help


SQL> SELECT FROM plsql1U1_purchasf> ;

_i

PRODUCTJMHE

SALESPERSON

PURCHASE_

Snail Widget
Hediun Wodget
Chrome Phoobar
Snail Widget
Hediun Wodget
Round Chrome Snaphoo
Snail Widget

CA
OB
GA
GA
LB
CA
CA

1*-JUL-83
1"1-JUL-03
14-JUL-83
1S-JUL-03
15-JUL-83
16-JUL-03
17-JUL-03

QUANTITV

1
75
2
8
20
5
1

7 rows selected.
SQL>
SQL>
2

u
5

CREATE
SELECT
FROM
WHERE
;

OR REPLACE VIEW plsqll 01 sales by atlas AS


*
plsqll 01_purchase
salesperson - *CA'

Uien created.
SQL>
SQL> SELECT FRON plsqll 01_sales_by_atlas_u;
PRODUCTJMHE

SALESPERSON

PURCHASE_

Snail Widget
Round Chrone Snaphoo
Snail Widget

CA
CA
CA

11I-JUL-03
16-JUL-03
17-JUL-03

QUANTITV
1
5
: . 1 ;'i

SOL>
' . :

. 7.6.
, :
CREATE OR REPLACE VIEW plsql!01_sales_per_person_v AS'
SELECT pers.first_name I I ' ' I I pers.last_name SALESPERSON,
purc.product_name,
purc.purchase_date,
pure.quantity
FROM
plsqllOl^person pers,
plsql!01_purchase pure
WHERE pers.person_code = pure.salesperson (+)

SELECT * FROM plsql!01_sales_per_person_v


ORDER BY salesperson, product_name, purchase_date;

, ORDER BY
SELECT, ,
. Oracle 8/
ORDER BY. 8/ ,
WHERE ORDER BY, SELECT.

Oracle

235


, (
, ;
, ,.> ). , ,
:
DROP VIEW _;
:
DROP VIEW plsqll01_sales_per_person_v;


Oracle .
.
. , , CREATE VIEW
.

/V
, SQL 1,10
100 , , , .
, - .
,
.
,
.
, ,
, . ( ) \
.
WHERE. SELECT , , Oracle '5, 50 500 , WHERE,
.
, , :
SELECT __1 [, __2...]
FROM _
WHERE ROWNUM <= __
ORDER BY _
'
'.- . ' - . , ' ,
,
, , ,

236

. , , , .

81 ,

ORDER BY.
CREATE OR REPLACE VIEW plsq!101_overstocked_items AS

SELECT product_na!tie, quantity_on__hand


FROM
plsql!01_product
WHERE ROWNUM <= 3

ORDER BY quant.ity_on_hand
SELECT * FROM plsqll01_overstocked_Items;


Oracle,
. , Oracle.

.
. Oracle ,
(sequences), ,
. , , .


:
CREATE SEQUENCE _',
, 1
1 . . .
, :
CREATE SEQUENCE _
[INCREMENT BY _]
[START WITH _]
[MAXVALUE _]
[MINVALUE _]
[CYCLE]

Oracle

237

INCREMENT BY , 1. 28
( ,
!). , .
START WITH ,
1 . , , ,
.
MAXVALUE MINVALUE , . CYCLE, .
1 . ,
.
CREATE SEQUENCE plsqll01_test_seq;

, . "" CURRVAL NEXTVAL,


. NEXTVAL
.
, ,
. 7.7:
SELECT plsql!01_test_seq.nextval FROM DUAL;
SELECT plsq!101_test_seq.nextval FROM DUAL;
SELECT plsq!101_test_seq.nextval FROM DUAL;

. INSERT, .
. 7.8.
CREATE TABLE plsql!01_test (
record_id
NUMBER (18, 0),
record_text VARCHAR2 (10)
);
INSERT INTO plsql!01_test VALUES (
plsql!01_test_seq.nextval,
'Record A'
INSERT INTO plsql!01_test VALUES (
plsql!01_test_seq.nextval,
'Record B'

) ;
SELECT * FROM plsq!101_test;

238

Fie d Starch Sffaa Help


SQL> CREATE SEQUENCE plsql191_test_seq;
Sequence created.
SQL>
SQL> SELECT plsqlin_test_seq.nextual FROM DUAL;
NEXTUAL

SQL> SELECT plsqliei_test_seq.nextual FROM DUAL;


NEXTUAL

2
SQL> SELECT plsql1B1_test_seq.nextual FROM DUAL;
NEXTUAL

3
SQL> |

. 7.7.

Efc Ed Search Options Help


SQL> CREATE TABLE plsq!101_test (
2
record_id
NUM8ER(18.B),
3
record_text UflRCHAR2(1B)

):

Table created.
SQL> INSERT INTO plsql1B1_test UALUES (
2
plsq!101_test seq.nextval,
3
'Record A'

* );

1 row created.
SQL> INSERT INTO plsql1B1_test UALUES (
2
plsql1t1_test seq.nextual,
3
'Record B'

* );

1 row created.
SQL> SELECT FROM plsqlle1_test;
RECORD ID RECORD TEX
t Record A
5 Record
SQL>

^J

. 7.8.

Oracle

239

,
Oracle , .
.
,
.
, , INSERT.
,
INSERT.
9.

. , ,
,
.
, , , .
:
ALTER SEQUENCE
[INCREMENT BY _]
[MAXVALUE - \ NOMAXVALUE]
[MINVALUE __ \ NOMINVALUE]
[CYCLE | NOCYCLE]
5


. 7.9:
ALTER SEQUENCE plsql!01_test_seq
MAXVALUE 10

SELECT
SELECT
SELECT
SELECT
SELECT
SELECT

plsqll01_test_seq.nextval
plsq!101_test_seq.nextval
plsqll01_test_seq.nextval
plsql!01_test_seq.nextval
plsql!01_test_seq.nextval
plsql!01_test_seq.nextval

FROM
FROM
FROM
FROM
FROM
FROM

DUAL;
DUAL;
DUAL;
DUAL;
DUAL;
DUAL;

(synonym) Oracle ,
. ,
, , ,
, .
,
.
9 . 725

240

SQL> ALTER SEQUENCE plsql1S1 test_seq


2 MftXUflLUE 11

8 ;

Sequence altered.
SQL>

SQL> SELECT plsqlH1_test.seq.nextval FROH OUflL;


NEXTUflL

SQL> SELECT plsql1_test_seq.nextual FROH DUflL;


KEXTUAL

7
SQL> SELECT plsql101_test_seq.nextwal FROH DUflL;
HEXTUflL
' ________ __

, > .

SQL> SELECT plsql1B1_test_seq.nextual FROH DUflL;


NEXTUflL

9
SQL> SELECT plsql1B1_test_seq.nextual FROM DUflL;
NEXTUflL

11
SQL> SELECT plsql1Q1 test seq.nextual FROH DUflL;
SELECT plsql1B1_test_seq.nextual FROH DUAL
*

ERROR at line 1:

ORfl-MBM: sequence PLSQL1*1_TEST_SEQ. NEXTUflL exceeds HAXUALUE and cannot be i


SQL> f

. 7.9.
- ? : ,
, .
,
.
Oracle, , , ,
, :
SELECT * __.__1;

241

Oracle

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

:
CREATE [PUPLIC] SYNONYM _
FOR
, ,
. . 7.10 , .
SELECT * FROM prod;
CREATE SYNONYM prod FOR plsql!01_product;
SELECT * FROM prod;

file Edit Search Qptbns 1


SQL> SELECT FROH prod;
SELECT > FROM prod
ERROR at line 1:

ORA-00942: table or vie* does not exist


SQL>
SQL> SVNONVH prod FOR plsql101_product; .

Synonyn created.
SQL>
SQL> SELECT * FROH prod;
PRODUCTJttHE

PRODUCT_PRICE QUAHTITY_ON_HftNO LftST_STOC

Snail Widget
Medium Wodget
Chrome Phoobar
Round Chrome Snaphoo
Extra Huge Mega Phoobar *
Square Zinculator
Anodized Franifier
Red Snaphoo
Blue Snaphoo

99
75
51
25
1.96
41.5

1.76
1.76

15-JAN-03
15-JAH-02
110 15-JAN-B3
1MH
1234 15-JAH-04
1 31-DEC-02
5
11 31-DEC-B1
II 38-DEC-01

9 rows selected.
SQL>

oLJ

. 7.10.
;

9*

242

,
, . :
CREATE PUBLIC SYNONYM plsqll01_prqduct FOR plsqll01_product;

Oracle
.
. :
DROP [PUBLIC] SYNONYM _;
(PROD), :
DROP SYNONYM prod;

:
DROP PUBLIC SYNONYM plsql!01_product;

'

Oracle
, , Oracle : , , , , , .. , Oracle
(data dictionary).
, .
, , , , , ,
.

DICT.
. (
, , WHERE, .) , ,
. 7.11.
SELECT table_name, SUBSTR(comments, 1, 45)
FROM
diet
WHERE SUBSTR(comments, 1, 7) <> 'Synonym'
f



Oracle ,
( ,

243

Oracle

mmm\

Jt Oiacle SQL'Plus
File

Edit Search

Options Help

USER_REPPRIORITV_GROUP
USER_REPPROP
USER_REPRESuLUTION
USER_REPRESOLUTION_METHOD
USER_REPRESOLUTION STflTISTICS
USER_REPRESOL_STATS_CONTROL
USER_REPSCHEMA
USER_REPSITES
USER_RESOURCE_LIMITS
USER_ROLE_PRIUS
USER_SEGMENTS
USEH_SEQUEHCES
USER_SNflPSHOTS
USER_SNflPSHOT_LOGS
USER_SHftPSHOT_REFRESH_TIMES
USER_SOURCE
USER_SYNONYMS
USER_SVS_PRIUS
USER_TftBLES
USERJTABLESPACES
USER_TAB_COLUNNS
USER_TAB_COL_STATISTICS
USER_TBB_COHMEHTS
USER_TfiB_HISTBGRflMS
USER_TflB_PRIUS
USEB_rBB_PRIUS_MADE
USER_TflB_PBIUS_RECD
USERJTRIGGERS
USER_TRIGGER_COLS
USER_TS_QUOTflS
USER_TVPES
USER_TVPE_flTTRS
USER_TVPE_METHODS
USER_UPDATABLE_COLUI1NS
USERJJSERS
USERJJIEWS
flUOIT_flCTIONS
COLUMN_PRIUILEGES
DICTIONflRV
DICT_COLUMNS
GLOBAL NAME
INDEXjUSTOGHIW
INDEX_STATS
HLS_DATABflSE_PnRflMETERS

NLS_INSTANCE_PARAMETERS
HLS_SESSION_PARAMETERS
RESOURCE_COST
ROLE_ROLE_PRIUS
ROLE_SYS PRIUS
RDLE_TAB~PRIUS
SESSION_PRIUS
SESS10N_ROLES
TABLE PRIUILEGES

Information about user's priority groups


Propagation information about the current use
Description of all conflict resolutions for u
All conflict resolution methods accessible to
Statistics for conflict resolutions for user"
Information about statistics collection for
N-way replication information about the curre
K-uay replication information about the curre
Display resource limit of the user
Roles granted to current user
Storage allocated for all database segments
Description of the user's own SEQUENCES
Snapshots the user can look at
All snapshot logs owned by the user
Snapshots and their last refresh tines for ea
Source of stored objects accessible to the us
The user's priuate synonyms
System privileges granted to current user
Description of the user's own relational tabl
Description of accessible tablespaces
Columns of user's tables, views and clusters
Columns of user's tables, uiews and clusters
Comments on the tables and uiews owned by the
Histograms on columns of user's tables
Grants on objects for which the user is the
All grants on objects owned by the user
Grants on objects for which the user is the g
Triggers owned by the user
Column usage in user's triggers
Tablespace quotas for the user
Description of the user's own types
Description of attributes of the user's own t
Description of methods of the user's own type
Description of updatable columns
Information about the current user
Description of the user's own uiews
Description table for audit trail action type
Grants on columns for which the user is the g
Description of data dictionary tables and uie
Description of columns in data dictionary tab
global database name
statistics on keys with repeat count
statistics on the b-tree
Permanent NLS parameters of the database
NLS parameters of the instance
NLS parameters of the user session
Cost for each resource
Roles which are granted to roles
System privileges granted to roles
Table privileges granted to roles
Privileges which the user currently has set
Roles which the user currently has enabled.
Grants on objects for which the user is the g

286 rows selected.


SQL>

. 7.11. Oracle

244

,
, ). , .
:
SELECT table_name FROM user_tables;
:
SELECT view_name FROM user_views;
, , .

,
SQL. , INSERT, SELECT ,
.
CREATE TABLE,
SELECT.
RENAME __ __, ,
, null- . , , ,
.

.
, ,
.
Oracle,
. CREATE
SEQUENCE , , , ,
. INSERT,
, nextval.
, , , Oracle , , . ,
,
.
Oracle, ,
, .

Oracle

245

, .
, USER_TABLES
USER_VIEWS.
, SQL. , PL/SQL
SQL, Oracle. PL/SQL ,
.

1 . PRODUCT p
PRODUCT.ARCHIVE?
A. INSERT INTO product (
SELECT *
FROM product_archive

B. COPY * FROM product TO product_archive;


C. CREATE TABLE product_archive AS
SELECT * FROM product;
D. INSERT INTO product_archive (SELECT * FROM product);
2. ?

' i .

'.".;

\ !.'

A. RENAME _ __;
B. RENAME _ __;
C. RENAME TABLE _ __;
D. RENAME TABLE _ __\
3.
NEW_COLUMN 1?
A. ALTER TABLE tab 1
ADD new_column VARCHAR2(10) NOT NULL;
B. ADD new_column VARCHAR2(10) NOT NULL TO tabl;
C. ALTER tabl
ADD new_column VARCHAR2(10) NOT NULL;
D. ADD new_column VARCHAR2(10) NOT NULL TO TABLE tabl;
4. ?
. ,
, .

246

?
B. .
C.
.
D.
.

5. ?
A. CREATE SEQUENCE new_seql NOMAXVALUE;
B. CREATE SEQUENCE 2new_seq START WITH 2;
C. CREATE SEQUENCE new3_seq MIN 1 MAX 100 CYCLE;
D. CREATE SEQUENCE new_4seq INCREMENT BY -1;
6. ?
A. .
B. .
C. .
D. , .


1. , D.

CREATE TABLE product_archive AS


SELECT * FROM product;
INSERT INTO product_archive (SELECT * FROM product); ;

,
- .
. D
PRODUCT
PRODUCT_ARCHIVE.
,
2. .
RENAME _ __;
SQL-,
, .
.
3. A.

ALTER TABLE tab 1


ADD new_column VARCHAR2(10) NOT NULL;

" ",
ALTER TABLE.
4. D.

D ,
, .
.

Oracle
5. , .

247

CREATE SEQUENCE 2new_seq START WITH 2;


CREATE SEQUENCE new3_seq MIN 1 MAX 100 CYCLE;

,
(
). - ,

MINVALUE MAXVALUE, MIN . ,
D - ,
,
.
6. , D. ,
,

"",
.

III


PL/SQL


'

PL/SQL

252

. ,
SQL. , ! . , SQL
.

PL/SQL?
, SQL
. : SQL
(Structured Query Language, ), .
SQL, , SQL- .
. Oracle PL/SQL. "Programming Language for SQL".
PL/SQL.
SQL, 'PL/SQL SQL*Plus,
PL/SQL- , PL/SQL , .
, .

,
. SQL-:
DROP
DROP
DROP
DROP
DROP

TABLE
TABLE
TABLE
TABLE
TABLE

plsqllOljourchase;
plsql!01_product;
plsqllOljoerson;
plsqll01_old_item;
plsql!01_purchase_archive;

CREATE TABLE plsql!01_person (


person_code VARCHAR2P) PRIMARY KEY,
first_name VARCHAR2 (15) ,
las t_name
VARCHAR2 (20),
hire_date
DATE

CREATE INDEX plsqll01_person_name_index


ON plsql!01_person(last_name, first_name) ;
ALTER TABLE plsql!01_person
ADD CONSTRAINT plsql!01_person_unique UNIQUE (
first_name,

PL/SQL

253

last_name,
hiredate

INSERT INTO plsql!01_person VALUES


CCA', 'Charlene', 'Atlas', 'Ol-FEB-02');
INSERT INTO plsql!01_person VALUES
1
('GA', 'Gary , 'Anderson', 45-FEB-02.') ;
INSERT INTO plsql!01_person VALUES
1
1
CBB', 'Bobby', 'Barkenhagen , '28-FEB-02 );
INSERT INTO plsql!01_person VALUES
1
('LB', 'Laren', 'Baxter , 'Ol-MAR-02');
INSERT INTO plsql!01_person VALUES {
'LN', 'Linda',~ 'Norton', 'Ol-JUN-03');
CREATE TABLE plsql!01_product (
product_name
VARCHAR2(25) PRIMARY KEY,
product_price
NUMBER(4,2),
quantity_on_hand
NUMBER(5,0),
laststockdate
DATE

ALTER TABLE plsql!01_product ADD CONSTRAINT positive_quantity 'CHECK(


quantity_on_hand IS NOT NULL
AND
quantity_on_hand >= 0

INSERT INTO plsql!01_product VALUES


;'/;
('Small Widget', 99, 1, ' 15-JAN-03 ' ) ;
INSERT INTO plsql!01_product VALUES
( 'Medium Wodget' , 75, 1000, ' 15-JAN-02 ' ) ;
INSERT INTO plsql!01_product VALUES
('Chrome Phoobar1, 50, 100, ' 15-JAN-03' ) ;
INSERT INTO plsql!01_product VALUES
('Round Chrome Snaphoo1, 25, 10000, null);
INSERT INTO plsql!01_product VALUES
(Extra Huge Mega Phoobar +', 9.95, 1234, ' 15-JAN-04 ' ) ;
INSERT INTO plsql!01_product VALUES ('Square Zinculator1,
45, 1, TO_DATE (' December 31, 2002, 11:30 P.M.',
'Month dd, YYYY, HH:MI P.M.')
INSERT INT6 plsql!01_product VALUES (
Anodized Framifier1, 49, 5, NULL) ;
INSERT INTO plsql!01_product VALUES (
1
Red Snaphoo ', 1.95, 10, ' 31-DEC-01 ' ) ;
INSERT INTO plsql!01_product VALUES (
'Blue Snaphoo', 1.95, 10, '30-DEC-01')

254

CREATE TABLE plsql!01_purchase (


product_name
VARCHAR2(25),
salesperson
VARCHAR2(3),
purchase_date
DATE,
quantity
NUMBER(4,2)

ALTER TABLE plsql!01_purchase


ADD PRIMARY KEY (product_name,
salesperson,
purchase_date

ALTER TABLE plsql!01_purchase ADD CONSTRAINT reasonable_date CHECK I


purchase_date IS NOT NULL
AND
1
TO_CHAR(purchase_date, ' YYYY-MM-DD ) >- '2000-06-30'

ALTER TABLE plsql!01_purchase


ADD CONSTRAINT plsql!01_purchase_f k_product FOREIGN KEY
(product_name) REFERENCES plsql!01_product;
ALTER TABLE plsql!01_purchase
ADD CONSTRAINT plsql!01_purchase_f k_person FOREIGN KEY
(salesperson) REFERENCES plsql!01_person;
CREATE INDEX plsql!01_purchase_product
ON plsql!01_purchase (product_name) ;
CREATE INDEX plsql!01_purchase_salesperson
ON plsql!01_purchase (salesperson) ;
INSERT INTO plsql!01_purchase VALUES
('Small Widget', 'CA1, 44-JUL-03', 1);
INSERT INTO plsqllOljpurchase VALUES ('Medium Wodget ' , 'BB1, '14-JUL-03', 75);
INSERT INTO plsql!01_purchase VALUES
('Chrome Phoobar', 'GA', 44-JUL-031, 2);
INSERT INTO plsq!101_purchase VALUES
('Small Widget1, 'GA', ' 15-JUL-03 ' , 8) ;
INSERT INTO plsq!101_purchase VALUES
( 'Medium Wodget' , 'LB', '15-JUL-03', 20);
INSERT INTO plsql!01_purchase VALUES
('Round Chrome Snaphoo ' , 'CA', 46-JUL-03', 5) ;
INSERT INTO plsql!01_purchase VALUES (
'Small Widget1, 'CA', '17-JUL-031, 1)
UPDATE plsqll.01_product
SET
product_price = product_price * .9

PL/SQL

255

WHERE product_name NOT IN (


SELECT DISTINCT product_name
FROM plsql!01_purchase

CREATE TABLE plsq!101_old_item (


item_id
CHAR(20),
item_desc
CHAR(25)

INSERT INTO plsql!01_old_item VALUES


CLA- 101', 'Can, Small1);
INSERT INTO plsq!101_old_item VALUES
('LA- 102', 'Can, Large');
INSERT INTO plsq!101_old_item VALUES
( 'LA-103', 'Bottle, Small');
INSERT INTO plsqll01_old_item VALUES
CLA- 104', 'Bottle, Large');
INSERT INTO plsqll01_old_item VALUES
CNY- 101', Box, Small');
INSERT INTO plsq!101_old_item VALUES
( 'NY-102', 'Box, Large');
INSERT INTO plsq!101_old_item VALUES
CNY- 103', 'Shipping Carton, Small1);
INSERT INTO plsq!101_old_item VALUES
('NY- 104', 'Shipping Carton, Large');
CREATE TABLE plsql!01_purchase_archive (
product_name
VARCHAR2(25),
salesperson
VARCHAR2(3),
purchase_date
DATE,
quantity
NUMBER(4,2)

INSERT INTO plsql!01_purchase_archive VALUES


('Round Snaphoo', 'BB', '21-JUN-01', 10);
INSERT INTO plsq!101_purchase_archive VALUES
('Large Harflinger', 'GA1, '22-JUN-011, 50);
INSERT INTO plsql!01_purchase_archive VALUES
('Medium Wodget', 'LB', '23-JUN-01', 20);
INSERT INTO plsql!01_purchase_archive VALUES
('Small Widget', 'ZZ1, '24-JUN-02', 80);
INSERT INTO plsql!01_purchase_archive VALUES
('Chrome Phoobar', 'CA', '25-JUN-02', 2);
INSERT INTO plsql!01_purchase_archive VALUES
('Small Widget 1 , 'JT', '26-JUN-02', 50);

PL/SQL
PL/SQL , .

256

(packages) PL/SQL .
,
, PL/SQL
(triggers), , .
(stored procedures) PL/SQL
, . PL/SQL
.
PL/SQL ,
, SQL,
SQL- . Oracle. , PL/SQL
SQL, . PL/SQL
SQL
, . SQL-
, " ",
.
,
, PL/SQL, SQL SQL*Plus.

SQL, PL/SQL SQL*Plus:


. , ( )
. . . .
, .
: - , -
(, ) ..
.
, . , (
: " "),
.
. ,
Oracle , a SQL*Plus , (, ) , .. .
"" "": SQL PL/SQL. ,
SQL*Plus , ,
. , , , SQL*Plus
, , .

PL/SQL

257

,
SQL*Plus, .
.
, , PL/SQL
"" . PL/SQL , , . .
, . ,
, SQL PL/SQL , . ,
. , SQL PL/SQL
SQL*Plus ( ) .

,
, PL/SQL
- (.. ,
).
Oracle : , .

,
PL/SQL.
. ,
.
SQL-
.
PL/SQL, ,
SQL*Plus.
.
: . (procedure specification)
. (formal parameters) (formal arguments).
, (actual) .
. (, ;
, .)
rtm_ytd_reports

. .

258
\'

..,-... increase_prices (percent_increase NUMBER)

.
PERCENT_INCREASE. , : NUMBER.
\ '

increase_salary_find_tax

(increase_percent
sal
tax
)

IN
IN

NUMBER := 7,
OUT NUMBER,
OUT NUMBER

. IN
,
. OUT ,
, . IN OUT
, ,
.
INCREASE_PERCENT
(default value), 7, := 7 .
, ,
7% ,
.

.
, NUMBER,
HOHeNUMBER(10,2).

(procedure body) PL/SQL-. , PL/SQL, , .


PL/SQL PL/SQL: . , ,
SQL-.
, . :
calcjpercent (value_l NUMBER,
value_2 NUMBER) return NUMBER

,
VALUE_1 VALUE_2. SQL- :
INSERT INTO employee VALUES (3000, CALC PERCENT(300, 3000));

PL/SQL

259

PL/SQL, , ,
(triggering event). , , INSERT, UPDATE DELETE; DDL; ;
; .
PL/SQL :
. Oracle
.
.

.
.


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

.
'

PL/SQL
(basic block) PL/SQL.
PL/SQL, , . , , .
PL/SQL : (header
section), (declaration section),
(execution section) (exception section).
(anonumous block) PL/SQL ,
, , . SQL*Plus , PL/SQL. , ,
. ,
.
.
-, ,
, . ,
SQL*Plus , :
set serveroutput on

**

260


. 8.1.
DECLARE
Num_a NUMBER := 6;
Num_b NUMBER;
BEGIN
Numjb := 0;
Num_a := Num_a / Num_b ;
Num_b := 7;
dbms_output .put_line ( ' Value of Num_b ' || Num_b) ;
EXCEPTION
WHEN ZERO_DIVIDE
THEN
dbms_output.put_line ( 'Trying to divide by zero');
dbms_output.put_line ( ' Value of Num_a ' I I Num_a) ;
dbms_output.put_line ( ' Value of Num_b ' || Num_b) ;
END;


,
. , , -

A Oracle SQL-Plus

file Edit Search ptions |


SQL> set serueroutput on
SQL> DECLflRE
2
nun_a NUMBER := 6;

3 nun b NUMBER;
4 BEGIN
5 nun b
6
nun_a / num_b;
7 nun_b - 7;
8 dbns_output.put_line( ' Ualue of nun_b ' || num_b) ;
9 EXCEPTION
10
WHEN 2ERO_DIUIDE
11
THEN
12
dbms_output.put_line('
_
_
Trying to diuide by zero1);
13
dbns_output.put_line(' Ualue of nun_a
nun_a);
14
dbms_output.put_line( ' Ualue of nun_b
nun_b);
15 END;
16

Trying to divide by zero


Ualue of nun_a
Ualue of nun_b 0
PL/SQL procedure successfully completed.
SQL> |

. 8.1. PL/SQL

PL/SQL

261

, .
, . .
,
, .
DECLARE. , ,
DECLARE:
just_a_label
DECLARE

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


.
,
BEGIN. , , ,
, PL/SQL,
.
. PL/SQL .
PL/SQL , , , , .
, . 8.1 PL/SQL,
Num_a Num_b, Num_a , 6.
, ,
, . ,
, .
,
SQL*Plus Num_a
PL/SQL. Num_a Num_b .
PL/SQL,
Num_a Num_b .
, , ,
, , . , , (scope).
Num_a Num_b , .
.


BEGIN EXCEPTION, -

262

, END,
.
PL/SQL-, . .
BEGIN
PL/'SQL-
[ ]
END [ }',
PL/SQL (:=). ,
. . Num_b .
Num_a Num_a, Num_b. ,
Num_a .
Num_b 7.


PL/SQL- ,
. (exceptions). , ,
, , . , ,
. ,
.
PL/SQL , (exception handling). , ,
. , ,
.
EXCEPTION
WHEN _
THEN
,
WHEN _
THEN
,
EXCEPTION . WHEN
_, ,
. ,
SQL*Plus. , -

PL/SQL

263

, 9. DBMS_OUTPUT PUT_LINE
Oracle; SQL*Plus.
, , , , . ,
7 Num_b . , Num_b .
, , (exception handling).
, , ,
, PL/SQL , , (raising exception).
PL/SQL, .
PL/SQL ZERO_DIVIDE.
, PL/SQL.

PL/SQL-
, PL/SQL- . . :
CREATE PROCEDURE my_f irst_proc IS
greetings VARCHAR2 (20) ;
BEGIN
greetings := 'Hello World';
dbms_output .put_line (greetings) ;
END my_f irst_proc;
/

:
CREATE PROCEDURE _ IS _
,
, , .
PROCEDURE FUNCTION:
CREATE FUNCTION _ IS _
(/) PL/SQL,
. , CREATE OR REPLACE CREATE. .
.
CREATE OR REPLACE _ IS _
, SQL*Plus:
.;'

'

..

set serveroutput on
EXECUTE my_first_proc;

264

SET SERVEROUTPUT ON .
EXECUTE . , . . 8.2.
BEGIN

my_first_proc;
END;


, .
. . .
*. Oracle SQL'Plus
File Edit Search Options Help
SQL> set serueroutput on
SQL> CREATE PROCEDURE my_first_proc

2 IS
3
greetings UflRCHflR2(20);
4 BEGIN
5
greetings := 'Hello World 1 ;
6
dbms_output.put_line(greetings);
7 END ny_f irst_proc;

8 /

Procedure created.
SQL> EXECUTE ny_first_proc;
Hello World
PL/SQL procedure successfully completed.
SQL> BEGIN
2
ny_first_proc;

3 END;
>t /

Hello World
PL/SQL procedure successfully completed.
SQL>
JU

. 8.2. PL/SQL- "Hello World"

mmmi
J

PL/SQL

265



.

, .
-
, :
_();

_;
: ,
.
,

, .
, , , .
:
.
_(__ 1,
__2,
__1*) ;
,
, .
,
,
:
_(__1 => __1,
__2=> __2,

(named notation)
. (positional), .
. , ,

266

. .
, .

PL/SQL

PL/SQL. . ,
. () . , , , ,
. ,
, PL/SQL
NUMBER, CHAR VARCHAR2.
, .
. , ? , . ,
. , ,
.
. , PL/SQL-.

PL/SQL
PL/SQL :
_ _ [[NOTNULL] := __\;
_ _ [[NOT NULL] DEFAULT
__];
_ PL/SQL. PL/SQL :
30
( ).
, 0 9, (_),
($) (#).
.
PL/SQL SQL,
. ,
BEGIN,
PL/SQL.
_ SQL PL/SQL. .
NOT NULL , .
, .

PL/SQL

______^__

267

, . .
SQL NUMBER, VARCHAR2 DATE. PL/SQL , SQL.
PL/SQL, Oracle.
. >;

PL/SQL
;
_ _ CONSTANT := ;
,
. . , ,
PL/SQL , . , PL/SQL .

. -,
, PL/SQL.
. :
_ := ;
-, PL/SQL-
, IN OUT OUT. . ,
. . 8.3.
CREATE PROCEDURE hike_priees (oldjprice NUMBER,
percent_hike NUMBER := 5,
new_price OUT NUMBER)
IS
BEGIN
new_price := old_price + old_price * percent_hike / 100;
END hike_prices;
/
DECLARE
price_to_hike NUMBER(6,2) := 20;
hiked_price NUMBER(6,2) := 0;
BEGIN
dbms_output.put_line('Price before hike ' I I price_to_hike);
dbms_output.put_line('hiked_price before hike ' I I ..
hiked_price);
hike_prices (old_price => price_to_hike,

268
new_price => hiked_price) ;
dbms_output.put_line ( 'price_to_hike after hike ' |
price_to_hike) ;
dhms_output .put_line ( 'hiked_price after hike ' [|
hiked_price) ;

END;

. ,
. 8.4.
DECLARE
product_quant
NUMBER;
BEGIN
SELECT
quantity_on_hand
INTO
product_quant
FROM
plsq!101_product
WHERE
product_name = 'Small Widget';
dbms_output .put_line ('Small Widget ' I I product_quant) ;
END;

*OfacleSQlilPlufile Ed Jeaich * IMP


SQL> set serueroutpyt on
SQL> CREATE PROCEDURE hike_prices (old_price NUHBER,

percentjiike NUMBER :- 5,
new_pcice OUT NUMBER)

IS
BEGIN

new_price
END hike prices;

old_price * old_price percent__hike / 100;

Procedure created.
SQL> DECLARE

2
3

BEGIN

9
1
11 END;
12

price to hike NUH8ER(6,2) := 20;


hikedjrice HUMBER(6,2) := U;
dbras_output.put_line("Price before hike || price_to_hike);
dbms~output.put_line('hiked_price before hike ' 11 hiked_price);
hike_prices (old_price > price_to_hike,
neu_price => hiked_price);
dtins_output.put_line( 'price_to_hike after hike ' || price_to_hike);
dbns_output.put_line{'hiked_price after hike ' hiked_price);

Price before hike 28


hiked_price before hike D
price_to hike after hike 20
hiked_price after hike 21
PL/SQL procedure successfully completed.
SQL>

. 8.3. PL/SQL

269

PL/SQL
* Oiacle SQL'Plus
Efe & Sech Qptions
SQL> set serveroutput on
SQL> DECLARE
2
product quant NUMBER;
3 BEGIN
4
SELECT quantity_on_hand
5
INTO
product quant
6
FROM
plsqUOl product
7
WHERE
product name - 'Small Widget';
8
9
dons output. put line ('Snail Widget
10 END;
11 /
Small Widget
1

^
^

!| product_quant);

PL/SQL procedure successfully completed.


SQL>

. 8.4. PL/SQL
SQL
product_quant , Small
Widget.


PL/SQL-npo. , ,
, .. , .
, Small Widget ,
, Medium Wodget. , , .
.
-
.

PL/SQL
, . ,
, 5%, , 10%. , .
(conditional processing) . .

270

,
. , ,
.
,
. (iterative operation).
PL/SQL .

(program flow), (flow of execution).

IF
IF :
IF _1 THEN
_1;
[ELSIF _2^
_2',]
[ELSE
_\]
END IF;
_1...
PL/SQL-. , . , .
,
. 8.5.
.
- .
( ).
CREATE FUNCTION compute_discounts (order_amt NUMBER)
RETURN NUMBER IS
small_order_amt NUMBER := 400;
large_order_amt NUMBER := 1000;
small_disct NUMBER := 1;
large_disct NUMBER := 5;
BEGIN
IF (order_amt < large_order_arat
AND
order_amt >= small_order_amt)
THEN
RETURN (order_amt * small_disct / 100) ;
ELSIF (order_amt >= large_order_amt)
THEN
RETURN (order_amt * large_disct / 100);
ELSE
N
RETURN(0);
END IF;
END compute_discounts;

PL/SQL

271

Oiacle SQL-Plus
File Edit Seated Options Help
SQL> CREATE FUNCTION compute_discounts (order_ant NUMBER)
2 RETURN NUMBER

|x

-l]

IS

4
5
6

small_order_amt NUMBER := 400;


large_order_amt NUMBER :- 1000;
sroall_disct NUMBER := 1;
large_disct NUMBER :- 5;

7
8

BEGIN

9
H
11
12
13
1*
15
16
17
18
19

IF (order_amt < large_order_amt

AND
order_amt >= small_order_amt)
THEN
RETURN (order_amt small_disct / 1BO);
ELSIF (order_amt >- large_order_amt)
THEN
RETURN (order_amt large_disct / 180);
ELSE
RETURN(O);

END IF;

20

END compute_discounts;

21

Function created.
SQL> DECLARE

2
3
4
5

BEGIN

7
8
9

dbms_output.put_line
dbms_output.put_line
dbms_output.put_line
dbms_output.put_line
dbns output.put_line

10
11
12
.13

END;
/

Order

20

tiny NUMBER := 20;


med NUMBER := 600;
big NUMBER := 4550;
wrong NUMBER := -35;

AND

(' Order
(tiny ||
(med |j '
(big j| '
(wrong ||

AND
Discount ');
'|| compute discounts(tiny)) ;
|| compute_discounts (med));
I I compute discounts (big));
' || conpute_discounts (wrong));

Discount

ao 6
4550

227.5

-35
PL/SQL procedure successfully completed.
SQL>

. 8.5. IF
I % 400 1000,
5% 1000.
( ) . , , .
, .
, ,
, , .
, order_amt ( ), ,
. ,
10 . 725

272

, . set serveroutput on. ,: . 8.5.


I DECLARE
tiny NUMBER := 20;
med NUMBER := 600;
big NUMBER := 4550;
wrong NUMBER := -35;
BEGIN

dbms_output.put_line (' Order AND Discount ');

dbms_output.put_line (tiny || ' ' I f compute_discounts(tiny));


dbms_output.put_line (med || ' ' I I compute_discounts (med));
dbms output.put_line (big I I ' ' || compute_discounts (big));
dbms_output.put_line (wrong || ' ' I I
compute_discounts (wrong));

END;
/

PL/SQL . PL/SQL.
.

LOOP

LOOP :
_
LOOP
;
EXIT _ [WHEN _}\
',
END LOOP;
WHEN
, _ (.. ). ,
. "", EXIT , , END LOOP.
WHEN , LOOP EXIT . , WHEN, . ,
.

. 8.6. .
, set serveroutput, .

273

PL/SQL

File dit Search Qptions

Help

SQL> set serueroutput on


SQL> DECLflRE
2
just_a_nun NUMBER := 1;
3

BEGIN

*
5
6
7
8
9
10
11 END;
12 /
1
2
3
ll
5
6
7
8
9
10

just a loop
LOOP
dbns_output .put_line( just_a_nun) ;
EXIT just a loop
WHEN (just_a_num >= 10);

just a nun := just a nun + 1;


END LOOP; "

: ' - ""

' ;

'"

'-' :'

'

'

:'''..
. ,

' ' '

'

- '

'

'

' '

' '".-.

PL/SQL procedure successfully completed.


SQL>
JJ

. . . . . . , ' . . ....

: ^

. 8.6.
DECLARE
just_a_num NUMBER := 1;;
BEGIN

just_a_loop
LOOP
dbms^output ,put_line (just_a_num) ;
EXIT just_a_loop
WHEN (just_a_num >= 10);
just_a_num := just_a_num + 1;
END LOOP;

END;

^__/ 1.
10 .

274

WHILE
WHILE. , , . WHILE
:
WHILE _
LOOP
;
END LOOP;
WHILE,
. 8.7:
* Oracle SQL'Plus
File Edit Search Options Help
SQL> set serveroutput on
SQL> DECLARE

BEGIN

i
5
6
7
8
9 END;
10 /

just_a_num NUMBER := 1;

WHILE (just_a_num <= 18)


LOOP
dbns_output.put_line(just_a_num);
just_a_num := just_a_num + 1;
END LOOP;

1
2
3

5
6
7
8
9
10

PL/SQL procedure successfully completed.


SQL>

. 8.7. WHILE

PL/SQL
DECLARE

275
-

just_a_num NUMBER := 1;
BEGIN
WHILE (just_a_num <= 10) LOOP
dbms_output .put_li'ne (just_a_num) ;
just_a_num := just_a_num + 1;
END LOOP;

END;
/

WHILE .
"", .

FOR

FOR -, (loop index).


, , ,
. ,
. FOR :
FOR IN [REVERSE] ^ .. :,
LOOP
;
END LOOP;
FOR, ,
. . 8.8.
BEGIN
FOR just_a_num IN 1..10
LOOP
dbms_output.put_line(just_a_num);
END LOOP;

END;
/

, ( ), FOR REVERSE.
10 1.

PL/SQL,
PL/SQL SQL. "" "
". PL/SQL,
SQL- SELECT. ,
SQL-. . OPEN,
FETCH.
CLOSE.
, , . , , , .

276

.+. Oracle SQL'PlL


File dil Search Qptions Help
SQL> set serueroutput on
SQL> BEGIN
2
FOR just_a_nun IN 1..10
3
LOOP
1
5
6
7
1 '
2

dbms_output.put line(just_a_num);
END LOOP;
END;
/

3
k
S
6
7

9
18

PL/SQL procedure successfully completed.


SQL>

-iJJ

. 8.8. FOR
COMMIT ROLLBACK
.
SQL-, PL/SQL, ,
(implicit), . , (explicit) , .. , .
, . , , .

PL/SQL :
CURSOR _ [([_1 [, _2...])]
[RETURN ]
IS
_select
[FOR UPDATE
[OF ___1
[, ___2...]

PL/SQL

277

, ,
(IN). OUT
IN OUT , . ^.
WHERE SELECT.
,
SELECT. PL/SQL
. __ , ,
, . , SELECT
, , ,
,
. FOR UPDATE ,
SELECT .
, .
, . 8.1.
8.1.

_%180

, .
_ , TRUE

_%\/

,
SELECT

_%

, . ,
TRUE

_%

FOUND.
, TRUE

PL/SQL

PL/SQL ,
- , ,
.
PL/SQL . , .
_._, . , ;
, .
(table-based) ,
.
, SELECT * FROM
_
, ,
_.

278

(cursor-based)
,
SELECT.
(programmer-defined)
.

OPEN, FETCH CLOSE

,
:
OPEN ;
. : " * , -

. .

. . . . . . . . , ,

. . .

FETCH _ INTO ___',


CLOSE _;
,
SELECT . FETCH
PL/SQL, , PL/SQL,
.
, FOUND NOTFOUND, . ,
. FOUND NOTFOUND
FETCH.

. , . , ;
. , FOR. PL/SQL , .

FOR

FOR :
:

' : -

'

FOR _ IN _ LOOP
',
END LOOP;
_.
_
PL/SQL, . , .
.
, in valid cursor ( ). ,
, . , ,
, .

PL/SQL

279

WHERE CURRENT OF

,
WHERE CURRENT OF _
, , WHERE UPDATE DELETE. ,
PLSQL101_PRODUCT 3%. . 8.9.

1 * Oracle SQL-Plus . - * * <


Fe dit Seach flpUons Help
Small Widget
Medium Uodget
Chrome Phoobar
Round Chrome Snaphoo
Extra Huge Mega Phoobar *
Square Zinculator
flnodized Franifier
Red Snaphoo
Blue Snaphoo
1
/
9 rows selected.

99
75
SO
ZS
8.96
40.5
W.1
1.76
1 .76

SQL> DECLflRE
2
CURSOR product cur IS
3
SELECT FROM plsqUOl product
>t
FOR UPDflTE OF product price;
5 BEGIN
6
FOR product rec IN product cur
7
LOOP
8
UPDflTE plsqll Byproduct
9
SET product price - (product rec. product price
10
,
WHERE CURRENT OF product cur;
11
END LOOP;
12 END;

13 /

i :,

0.97)

'.

PL/SQL procedure successfully completed.


SQL> SELECT product name, product price
2 FROM
plsq!1B1 product;
PROOUCT_NflNE
Snail Midget
Medium Uodget
Chrome Phoobar
Round Chrome Snaphoo
Extra Huge Mega Phoobar *
Square Zinculator
Anodized Framif ier ,
Red Snaphoo
Blue Snaphoo
1
- '- 9 rows selected.

PRODUCT_PRICE
96.03
72.75
48.5
24.25
8.69
39.29
42.78
1.71
1 .71
' , *'

<

'.

. 8.9. FOR WHERE CURRENT OF

280

SELECT product_hame, product_price


FROM
plsq!101_product;
DECLARE
CURSOR product_cur IS
SELECT * FROM plsql!01_product
FOR UPDATE OF product_price;
BEGIN
FOR product_rec IN product_cur
LOOP
UPDATE plsql!01_product
SET product_price = (product_rec.product_price * 0.97)
WHERE CURRENT OF product_cur;
END LOOP;
END;
/
- '
,
SELECT product_name, product_price
FROM plsqllOljproduct;


,
, (nested loops).
.
-- ,
.
-- : . ,
-- .
/* : PLSQL101_PRODUCT
PLSQL101_PURCHASE PRODUCT_NAME.
. ,
.
*/
CREATE OR REPLACE PROCEDURE do_commissions IS
commission_rate
NUMBER := 2
total_sale
NUMBER := 0
current_person
CHAR(3) := ' '
next_person
CHAR(3)
quantity_sold
NUMBER := 0
item_price
NUMBER := 0
CURSOR sales_cur IS
-SELECT tabl.salesperson, ..... .
,
tabl.quantity,
tab2.product_price
FROM
plsqll01_purchase tabl,
plsql!01_product tab2
WHERE tabl.product_name ="tab2.product_name;
ORDER BY salesperson;
BEGIN
OPEN sales_cur;
LOOP
FETCH sales cur INTO

PL/SQL
WHILE

281
next_person, quantity_sold, item_price;
(next_person = current_person
AND
sales_cur%FOUND)

LOOP
total_sale :=
total_sale + (quantity_sold * item_price);
FETCH sales_cur INTO
next_person, quantity_sold, item_price;
END LOOP;
IF (sales_cur%FOUND)
THEN
IF (current_person != next_person)
THEN
IF (current_person != ' ' )
THEN
dbms_output.put_line
(current_person ||
1
' II
total_sale ||

.... ' ' I I


total sale * cbmmission_rate'
/ 100);
END IF;
total_sale := quantity_sold * item_price;
current_person := next_person;
END IF;
ELSE IF (current_person != ' ')
THEN
dbms_output.put_line(current_person ||
1
' II
total_sale I I
1
' II
total_sale * commission_rate
/ 100);
END IF;
END IF;
EXIT WHEN sales_cur%NOTFOUND;
END LOOP;
CLOSE sales_cur;
END do_commissions;
/

, SELECT. PLSQL101_PURCHASE,
PLSQL101_PRODUCT. . , ,
, .

WHILE. ( ),
,

282

IF. , FETCH
- . , current person
next_person . , , ..
. , current_person
, IF .
totaljsale
. next_person current_person.
FETCH ,
.
next_person , current_person, ,
.
WHILE , , ,
. , .
, .
current_person . IF
.
commission_rate.
,
. 8.10. PLSQL101_PURCHASE,
DO_COMMISSIONS ,
, .
SELECT tabl.salesperson,
tabl.quantity,
tab2.product_price
FROM
plsql!01_purchase tabl,
plsql!01_product tab2
WHERE tabl.product_name = tab2.product_name
ORDER BY salesperson;
EXECUTE do_commissions;


. , PL/SQL.
.
1

'

'

'

"

'

, wot . , .

PL/SQL

283

Oracle SQL'Plus
File dit Search Options Help

61
62
63
64
65

END IF;
END IF;

EXIT WHEN sales cur%NOTFOUND;


END LOOP;

66
CLOSE sales_cur;
67 END do_commissions;
68

Procedure created.
SQL>
2
3
4
5
6
7
8
SflL

BB

GA
GA
LB

SELECT

FROM
WHERE
ORDER BV

tabl.salesperson,
tabl. quantity,
tab2.product_price
plsq!181_purchase tabl,
plsq!181_product tab2
tab1.product_nane = tab2.product_naroe
salesperson;

QUflNTITV PRODUCT_PRICE

75
1
5
1
8
2
28

72.75
96.83
24.25
96.83
96.03
48.5
72.75

7 rows selected.
SQL> EXECUTE do_commissions;
BB 5456.25 189.125
CA 313.31 6.2662
GA 865.24 17.3848
LB 1455 29.1
PL/SQL procedure successfully completed.
SQL>
A-l

^^__^___

. 8.10.
, ,
, .
, PL/SQL (enclosing basic block), .. ,
, . ,
, -

284

, , .
, ,

(cleanup), , . , ,
ROLLBACK.
,
, . , /.

ZERO_DIVIDE, ,
PL/SQL. ,
PL/SQL Oracle. 8.2 .
PL/SQL
. SQLCODE, . , ORA,
, .
, . ,
SQLERRM.
SQLCODE, SQLERRM. : .

SQLCODE SQLERRM. . 8.11.
DECLARE
Num_a NUMBER := 6;
Num_b NUMBER;
BEGIN
Num_b := 0;
Num_a := Num_a / Num_b;
Num b := 7;
dbms_output.put_line(' Value of Num_b
EXCEPTION
WHEN ZERO_DIVIDE THEN
DECLARE
err_num .NUMBER
err_msg VARCHAR2(512)
BEGtN
dbms_output.put_line('ORA Error
dbms_.output.put_line('ORA Error
dbms_output.put_line(' Value of
dbms_output.put_line(' Value of
END;
END;

' I I Num_b);

:= SQLCODE;
:= SQLERRM;
Number ' || err_nuia ) ;
message ' || err_msg) ;
Num_a ' || Num_a);
Num_b ' || Num_b);

PL/SQL

285

8.2.

CURSOR.ALREADYOPEN

DUPVAL.ONJNDEX

,
, ,

INVAUD.CURSOR
* ' ' . '

' ' .'

FETCH
,

NO DATA FOUND

SELECT INTO, SELECT (


,
)

PROGRAM_ERROR

. ,
Oracle

STORAGE.ERROR

TIME_OUT_ON_RESOURCE

TOO_MANY_ROWS

SELECT INTO PL/SQL

VAUUE_ERROR

PL/SQL
,

ZERO.DMDE

OTHERS

, ,
. ,
,
,

286

Oracle SQL-Plus
ie Edit Search Options Help
SQL> set serueroutput on
SQL> DECLARE
2
nun a NUMBER :=' 6;
3
num b NUMBER;
4 BEGIN
5
num_d :- 0;

6
7
8
9

nun a :* nun a / num b;


_ : 7;
dbns output. put line(' Ualue of num b
EXCEPTION

11
11
12
13
11
15
16
17
18
19
,28
21 END;
22 /

WHEN ZERO D I U I D E
THEN
DECLARE

BEGIN

END;

|| nun b);

err nun NUMBER ;= SQLCODE;


err msg UflRCHHR2(512) := SQLERRM;
dbns output. put_line('ORn Error
dbms_output.put_line( 'ORft Error
dbms output .put line( ' Ualue of
dbms output. put line(' Ualue of

Number ' | | err_nun ) ;


message ' I I err_sg);
num__a ' || num_a);
nun b ' 1 1 num_b) ;

ORA Error Number -1476


Error message ORA-Q1b76: diuisor is equal to zero
Ualue of nun_a 6
Ualue of num_b 0
PL/SQL procedure successfully completed.

SQL>

. 8.11. SQLCODE SQLERRM

,
PL/SQL ,
.

PL/SQL.
.
. 8.12.
set serveroutput on
DECLARE
quantity!'NUMBER := -2;
quantity2 NUMBER := 3;
total NUMBER := 0;
quantity_must_positive EXCEPTION;
FUNCTION find_cost (quant NUMBER) .RETURN NUMBER IS
i BEGIN
IF (quant > 0)
THEN
"ELSE

END IF;

RETURN (quant * 2 0 ) ;
'
RAISE quantity_must_pqsitive;

PL/SQL

287

END find_cost;
BEGIN
total := find_cost(quantity2);
total := total + find_cost(quantityl);
EXCEPTION
WHEN quantity_must_positive
THEN
dbms_output.put_line('Total until now: ' || total);
dbms_output.put_line('Tried to use negative quantity ');
END;
/

. ,
. find_cost , . ,
, , .

, . * Oracle SQL-Plus
Fife Edil Search Options Help
SQL> set serueroutput on
SQL> DECLARE
2
quantityl NUMBER - -2;
3
quantity NUMBER - 3;
4
total NUMBER :=
5
quantity_must_positiue EXCEPTION;
6
FUNCTION find_cost (quant NUMBER) RETURN NUMBER IS
7
BEGIN
8
IF (quant > 0)
9
THEN
10
RETURN(quant * 20);
11
ELSE
12
RAISE quantity nust_positiue;

13
END IF;
END find_cost;
14
15 BEGIN
16
total := Find_cost (quantity2);
total := total + find_cost(quantitj|1);
17
18 EXCEPTION
19
WHEN quantity_must_positiue
20
THEN
21
dbns_output.put_line('Total until now: ' || total);
22
dbms_output.put_line('Tried to use negatiue quantity ');
23 END;
24

Total until now: 6B


Tried to use negative quantity
PL/SQL procedure successfully completed.
SQL>

. 8.12. ,

288

, , . ,
, ,
,
. , ,
.

PL/SQL
, SQL.
PL/SQL.
.
PL/SQL.
PL/SQL . ,
, , , .
. .
, , , , , ; ,
. PL/SQL.
, . , , .
,
/.
, ,
, .
.
IF . LOOP, WHILE FOR
. SQL, , ,
. FOR .
,
, . ,
.
,
9. , PL/SQL. ,
.

PL/SQL

289

1. PL/SQL
?
A. .
B. ,
.
,
- ,
,
.
C. , OUT IN
OUT, .
D. , WHERE
SQL- SELECT.

' . ' . '

2. ?

' - , . - . . - .

. '- ;

outerjblock
DECLARE
scope_num NUMBER' := 3;
... BEGIN
DECLARE

BEGIN

scope_num NUMBER : = 6;
Num_a NUMBER := outer_block.scope_num;
dbms_output.put_line(seope_num);
dbms_output.put_line(Num_a);

END;
dbms_output.put_line(scope_nura);
END;

A. 6 3 3
B.

C. 333

D. 6 3 6
3. IF ?
A. ,
TRUE.
.
'/::'.

' - ' * ' / . ; ' - ' - ^ - ; -

'

. ' V': '

-, '

B. .
,
.

as

290

4.
?
A. LOOP
B. WHILE
. - ' . - . . / v

C. FOR

'-:

...

,-

'

D. FOR
5. ?
A. , ,
, .
B. ,
,
,
.
C.
,
(,
), ,
.
D. ,
, ,
IN OUT OUT
.


1., .

, . D
,
,
WHERE.
, ,
.
PL/SQL.
2. .

633

.
scope_num
scope_num. ,
scope_num 6. scope_num
,
scope_num
3.
scope_num ,
Num_a.

PL/SQL
3. .

291

IF, ELSE ELSEIF


,
. .
, .
4. A.

LOOP

LOOP
, .
.
5. .

, ,

.
, ,
, , .
.


PL/SQL

294

PL/SQL.
PL/SQL- , .
. PL/SQL
Oracle . , ,
Oracle Microsoft Access Excel.
, , .
. , . ,
.
- .
8, 9. ,
, 8.
, 8,
.
SQL-.
-- 9.

========================

PERSON =================================

DROP TABLE plsql!01_person;


CREATE TABLE plsql!01_person (
person_code VARCHAR2(3) PRIMARY KEY,
first_name
VARCHAR2(15),
last_name
VARCHAR2(20),
hire_date
DATE

CREATE INDEX plsq!101_person_name_index


ON plsqll01_person(last_name, first_name);
ALTER TABLE plsql!01_person
ADD CONSTRAINT plsqll01_persorv_unique UNIQUE (
first_name,
last_name,
hire_date

INSERT INTO plsql!01_person VALUES


CCA', 'Charlene', 'Atlas', 'Ol-FEB-02') ;
INSERT INTO plsqll01_person VALUES
1
CGA , 'Gary', 'Andersen', 45-FEB-02' ) ;
INSERT INTO plsql!01_person VALUES
('BB', 'Bobby1, 'Barkenhagen', '28-FEB-02')
INSERT INTO plsql!01_person VALUES

PL/SQL
('LB', 'Laren', 'Baxter', ' Ol-MAR-02 ') ;
INSERT INTO plsqH01_person VALUES (
1
'LN', 'Linda , 'Norton', 'Ol-JUN-03');
================= PRODUCT ========================
DROP TABLE plsql!01_product;
CREATE TABLE plsql!01_product (
product_name
VARCHAR2(25) PRIMARY KEY,
product_price
NUMBER(4,2),
quantity_on_hand
NUMBER(5,0),
last_stock_date
DATE

ALTER TABLE plsql!01_product ADD (


CONSTRAINT positive_quantity CHECK(
;quantity_on_hand IS NOT NULL
.'
AND
' quantity_on_hand >= 0

INSERT INTO plsql!01_product VALUES


('Small Widget', 99, 1, ' 15-JAN-03 ' ) ;
INSERT INTO plsql!01_product VALUES
( 'Medium Wodget', 75, 1000, ' 15-JAN-02 ' ) ;
INSERT INTO plsql!01_product VALUES
('Chrome Phoobar', 50, 100, ' 15-JAN-03 ' ) ;
INSERT INTO plsql!01_product VALUES
('Round Chrome Snaphoo', 25, 10000, null);
INSERT INTO plsq!101_product VALUES
('Extra Huge Mega Phoobar +', 9.95, 1234, ' 15-JAN-04 ' ) ;
INSERT INTO plsql!01_product VALUES ('Square Zinculator1,
45, 1, TOJ3ATE (' December 31, 2002, 11:30 P.M.',
'Month dd, YYYY, HH:MI P.M.')
INSERT INTO plsql!01_product VALUES (
'Anodized Framifier', 49, 5, NULL) ;
INSERT INTO plsql!01_product VALUES (
'Red Snaphoo', 1.95, 10, ' 31-DEC-01 ' ) ;
INSERT INTO plsql!01_product VALUES (
'Blue Snaphoo', 1.95, 10, '30-DEC-01')
=================== PURCHASE ========
DROP TABLE plsql!01_purchase;
CREATE TABLE plsql!01_purchase (
product_name
VARCHAR2(25),
salesperson
VARCHAR2(3),
purchase_date
DATE,
quantity
NUMBER(4,2)

295

296

ALTER TABLE plsql!01_purchase


ADD PRIMARY KEY (product_name,
salesperson,
purchase_date

ALTER TABLE plsql!01_purchase ADD (


CONSTRAINT reasonable_date CHECK(
purchase_date IS NOT NULL
AND
TO_CHAR(purchase_date, 'YYYY-MM-DD') >= '2000-06-30'

ALTER TABLE plsq!101_purchase


ADD CONSTRAINT plsql!01_purchase_fk_product FOREIGN KEY
(product_name) REFERENCES plsql!01_product;
ALTER TABLE plsql!01_purchase
ADD CONSTRAINT plsql!01_purchase_fk_pers'on FOREIGN KEY
(salesperson) REFERENCES plsql!01_person;
CREATE INDEX plsqll01_purchase_product
ON plsql!01_purchase(product_name);
CREATE INDEX plsql!01_purchase_salesperson
on plsql!01_purchase(salesperson);
INSERT INTO plsql!01_purchase VALUES
('Small Widget', 'CA', 44-JUL-03', 1);
INSERT INTO plsql!01_purchase VALUES
('Medium Wodgef, 'BB', 44-JUL-031, 75);
INSERT INTO plsql!01_purchase VALUES
('Chrome Phoobar1, 'GA1, '14-JUL-031, 2);
INSERT INTO plsql!01_purchase VALUES
('Small Widget', 'GA', 45-JUL-03', 8);
INSERT INTO plsql!01_purchase VALUES
('Medium Wodgef, 'LB', 45-JUL-03', 20);
INSERT INTO plsql!01_purchase VALUES
('Round Chrome Snaphoo', 'CA', 46-JUL-03', 5);
INSERT INTO plsql!01_purchase VALUES
('Small Widget', 'CA', 47-JUL-03', 1)
UPDATE plsql!01_product
SET
product_price = product_price * .9
WHERE product_name NOT IN (
SELECT DISTINCT product_name

PL/SQL

297

FROM plsql!01_purchase

-- =========================== OLD_ITEM
DROP TABLE plsql!01_old_item;
CREATE TABLE plsql!01_old_item (
item_id
CHAR(20),
item_desc CHAR(25)

INSERT INTO plsq!101_old_item VALUES


CLA-1011, 'Can, Small');
INSERT INTO plsq!101_old_item VALUES
('LA-102', 'Can, Large');
INSERT INTO plsq!101_old_item VALUES
CLA-103', 'Bottle, Small');
INSERT INTO plsqll01_old_item VALUES
('LA-104', 'Bottle, Large');
INSERT INTO plsq!101_old_item VALUES
ONY-101, 'Box, Small');
INSERT INTO plsql!01_old_item VALUES
('NY-1021, 'Box, Large1);
INSERT INTO plsq!101_old_item VALUES
('NY-', 'Shipping Carton, Small');
INSERT INTO plsq!101_old_item VALUES
('NY-104', 'Shipping Carton, Large1);
========================== PURCHASE_ARCHIVE
DROP TABLE plsq!101_purchase_archive;
CREATE TABLE plsql!01_purchase_archive (
product_name
VARCHAR2 (25) ,
salesperson
VARCHAR2(3),
purchase_date
DATE,
quantity
NUMBER (4, 2)

======================

NSERT INTO plsq!101_purchase_archive VALUES


('Round Snaphoo', 'BB', '21-JUN-01', 10);
INSERT INTO plsql!01_purchase_archive VALUES
('Large Harflinger ' ,' 'GA', '22-JUN-01', 50)
INSERT INTO plsql!01_purchase_archive VALUES
('Medium Wodget', 'LB', '23-JUN-01', 20);
INSERT INTO plsqll01_purchase_archive. VALUES
('Small Widget', 'ZZ', '24-JUN-02', 80);
INSERT INTO plsql!01_purchase_archive VALUES
('Chrome Phoobar', 'CA', '25-JUN-02', 2);
INSERT INTO plsql!01_purchase_archive VALUES
('Small Widget', 'JT', '26-JUN-02', 50);
==================
DECLARE

( for update)

===

298

9
CURSOR product_cur IS
SELECT * FROM plsqll01_product
FOR UPDATE OF product_price;

BEGIN
FOR product_rec IN product_cur
LOOP

UPDATE plsql!01_product
SET product_price = (product_rec.product_price.* 0.97)

WHERE CURRENT OF product_cur;


END LOOP;
END;
/'.


,
, , .
,
. , , ,
. ,
(conventions), .
, , ,
. ,
COMPUTE_DISCOUNTH3 8, , .
,
, ,
. . .
, , .
,
. ,
,
, , . , ,
.
, , . , ,
, - ( , ) .
, , :
SQL PL/SQL .

PL/SQL

299

.
SQL-
. ,
SELECT (SELECT) ,
, ..
PL/SQL.
.
.
.
,
, : , 30- , ( ,
, , -,
). , ,
. ,

PL/SQL
Oracle
.
PL/SQL . , PL/SQL
. , .
.
.
. , , , ,
.
.
PL/SQL-. ( ,
, ,) , ^ ,
, SQL,
, , PL/SQL. '
,
PL/SQL. ,
, .
PL/SQL SQL . ,
; (explicit).
, PL/SQL , (implicit). (
.)

300


PL/SQL
8 PL/S QL.
, PL/SQL. ,
PL/SQL . , , ,
.
, . , .
, . , , . ,
PL/SQL
. , , . ,
, ,
.
, .
.
PL/SQL , (anchored), , , PL/SQL
,
. PL/SQL , , , , (invalid).
,
. ,
. , , ,
, ,
.
, ,
; , , ; , , . PL/SQL :
PL/SQL :
___.__%;
,
:
_
, :
_

_%;

PL/SQL

301

FOR ,
PL/SQL . . , 8 , product_rec.
, , .
, PL/SQL
. :
TYPE __ IS RECORD
(__1 __1,
__2 __2,

:
_ __
.
SQL- SQL* Plus. . . 9.1.
/* (performance)

. Status
.
SET SERVEROUTPUT ON
DECLARE
performance_type IS RECORD
(person_code
plsq!101_person.person_code%TYPE,
person_name
plsq!101_person . last_name%TYPE,
current_sales
NUMBER (8, 2),
perform_percent
NUMBER (8,1),
status
varchar2(30)
one_perform performance_type;
CURSOR person_cur IS
SELECT *
FROM
plsql!01_person;
/*
.
a_person.
, current_sales
. (,
) , perform_percent
.
*/
PROCEDURE current_performance
(a_person plsql!01_person%ROWTYPE,
a_perform OUT performance_type)

302

is

CURSOR history_cur (person varchar2) IS


SELECT AVG(tab2.product_price * tabl.quantity)
avg_order
FROM
plsql!01_purchase_archive tabl,
plsql!01_product tab2'
WHERE tabl.product_name = tab2.product_name
GROUP BY tabl.salesperson
HAVING tabl.salesperson = person;
hist_rec history_cur%ROWTYPE;
current_avg_sales NUMBER(8,2) := 0;

BEGIN
a_perform.person_code := a_person.person_code;
a_perform.person_name := a_person.last_name;
a_perform.status := NULL;
BEGIN
SELECT SUM(tb!2.product_price * tbll.quantity),
AVG
tb!2.product_price * tbll.quantity)
INTO
a_perform.current_sales,
current_avg_sales
FROM
plsql!01_purchase tbll,
plsql!01_product tb!2
WHERE tbll.product_name = tb!2.product_name
GROUP BY tbll.salesperson
HAVING tbll.salesperson = a_person.person_code;
EXCEPTION
WHEN NO_DATA_FOUND
THEN
a_perform.status := 'Current purchases exception';
a_perform.current_sales := 0;
END;
OPEN history_cur (a_person.person_code);
FETCH history_cur INTO hist_rec;
IF (history_cur%NOTFOUND)
THEN
a_perform.perform_percent := 0;
IF (a_perform.status IS NULL)
THEN
a_perform.status := 'Erroneous or no history';
END IF;
ELSE
a_perform.perform_percent :=
100 * (current_avg_sales.hist_rec.avg_order)/
hist_rec.avg_order;
a_perform.status := 'All fine';
END IF;
CLOSE history_cur;
EXCEPTION
WHEN NO DATA FOUND

PL/SQL

, -.t'a&meaKwwmteM-Wtt&f^:-

.
.

THEN

303
-^1^-11^:--1011;.. >

,:^.
a_perform.status := 'Exceptions found';:
END current_performance;

BEGIN
FOR person_rec IN person_cur
LOOP
current_performance(person_rec,.,one_perform);
dbms_output.put_line(one_perform.person_code I !
1 1
II
one_perform.person_name
I
I
I
' II ,
one_perform.current_sales I I
I

II
one_perform.perform_percent |
''II
one_perform.status);
END LOOP;

END;
/
SELECT
SELECT
SELECT
SELECT

*
*
*
*

FROM
FROM
FROM
FROM

plsql!01_person;
plsqll01_purchase;
plsql!01_purchase_archive;
plsqll01_product;

, . PERFORMANCEJTYPE. %TYPE
, PLSQL101_PERSON, ,
, ! ONE_PERFORM, ,
PLSQL101_PERSON.
CURRENT_PERFORMANCE .
, PLSQL101_PERSON, , PERSON_CUR. ,
PERSON_CUR.
; ;
, A_PERFORM, OUT, ,
.
HISTORY_CUR,

PLSQL101_PRODUCT

PLSQL101_PURCHASE_ARCHIVE,
. () ,
HISTORY_CUR. ,
. HIST_RBC

HISTORY_CUR.
CURRENT_AVG_SALES
.
, A_PERFORM.
NULL.
11 . 725

304
1

- ni.-irlr- 1 4'liK

File dil

Gft
BB
LB
LN

Search

Options

Help

ntlas 313.31 7.7 11 fine


Anderson 865.24 a Erroneous or no history
Barkenhagen 5456.25 Erroneous or no history
Baxter 1U55 0 fill fine
Norton 0 0 Current purchases exception

-d

PL/SQL procedure successfully completed.


SQL> SELECT * FROM plsql101_person;

;..'

PER FIHSTJffiME

LftST_NftME

HIRE_DflTE

Charlene
GA Gary
BB Bobby
LB Laren
LN Linda

Atlas
Anderson
Barkenhagen
Baxter
Norton

01-FEB-02
15-FEB-D2
28-FEB-02
01-HAR-02
01-JUN-03

SQL> SELECT FROM plsqll B1_pur chase ;


PRODUCT_NAHE

SAL PURCHASE.

Snail Widget
Medium Wodget
Chrome Phoobar
Snail Widget
Medium Wodget
Round Chrome Snaphoo
Snail Midget
.. ;.
i
'
7 rows selected.

CA
BB
GA
GA
LB
CA
CA

;...
QUANTITY

14-JUL-B3
14-JUL-B3
11-JUL-B3
15-JUL-B3
15-JUL-83
16-JUL-B3
17-JUI-B3
,

1
75
2
8
28
5
1
. i '.-.-

' '

SQL> SELECT FROM plsqll 81_purchase_archiue;


PRODUCT_NAME

SAL PURCHASE.

Round Snaphoo
Large Harflinger
Medium Wodget
Snail Widget
Chrome Phoobar
Snail Widget

BB
GA
LB
ZZ
CA
JT

21-JUN-B1
22-JUN-S1
23-JUN-B1
24-JUN-B2
2S-JUN-B2
26-JUN-B2

QUANTITY
18
5B
28
88
2
58

6 rows selected.
"/':

I !"

'

'

'.

SQL> SELECT FROM plsqll 81_product;


PRODUCT_NAHE

PRODUCT_PRICE QUANTITV_ON_HAND LAST_STOC

Snail Widget
Medium Wodget
Chrome Phoobar
Round Chrome Snaphoo
Extra Huge Mega Phoobar +
Square Zinculator
Anodized Framifier
Red Snaphoo
Blue Snaphoo

96. B3
72.75
48.5
24.25
8.69
39.29
42.78
1.71
1.71

1 15-JAN-B3
1B8B 15-JAN-B2
188 1S-JAN-83
108BB
1234 1S-JAN-B4
1 31-DEC-B2
5
18 31 -DEC- 81
18 38-DEC-81

'.'

!
(

9 rows selected.
,'.

SQL>

JU
. 9.1. , ,

PL/SQL

305

, . '
, A_PERSON. ,
, . ,
. ,
.
HISTORY_CUR .
, , . AVG_ORDER.
, . , .
,
. PLSQL101_PRODUCT
(Large Harflinger Round Snaphoo).
"erroneous or no history", "no history". ,
A_PERFORM.PERFORM_PERCENT.
HISTORY_CUR .

NO_DATA_FOUND, .
PERSON_CUR, .
SELECT ,
, , ,
.
.
. , . . , .
- , .,.,..;

DML PL/SQL,
,
. . , ^
SELECT, INTO. , PL/SQL.
CURRENT_AVG_SALES A_PERFORM.CURRENT_SALES. SELECT ,
. ,
" TOO_MANY_ROWS. PL/SQL -

9
, "SQL". ,
SQL-. , SQL%FOUND , SELECT - .
SELECT SQL%FOUND
.
,
. , INSERT PL/SQL.
. 9.2.

-11

I Oracle SQL'Plus
File

Edit

Search

Options

i
j

Help

SQL> DECLflRE

2
3
t
5
6

quant NUMBER := 20;


BEGIN
INSERT INTO plsq!1B1 purchase
VALUES (-Medium Wodget 1 ,
'LN1,
18-flUG-021,
i.
8
quant);
9
IF (SQUNOTFOUND)
10
THEN
11
dbms output. put line(' Insert error?*');
12
END IF;

13 END;
1* /

PL/SQL procedure successfully completed.


SQL> SELECT * FROM plsqll B1_purchase ;
PRODUCT
Small Widget
Medium Wodget
Chrome Phoobar
Small Widget
Medium Wodget
Round Chrome Snaphoo
Small Widget
Medium Wodget

SftL PURCHftSE_

QUftNTITV

14-JUL-03
BB 14-JUL-03
14-JUL-03
Gfl 15-JUL-03
LB 15-JUL-B3
16-JUL-03
17-JUL-03
LN 18-ftUG-B2

1
75
2
8
20
S
1
20

\
j
.

8 rows selected.
SQL> |
' ! - *

jJJ

" ! - '

'"' "

. . . . .

. . . , . . ,

. 9.2. PL/SQL

PL/SQL

307

DECLARE
quant NUMBER := 20;
BEGIN
INSERT INTO plsqll01_purchase
VALUES ('Medium Wodget', :
'LN' ,
48-AUG-02',
quant);
IF (SQL%NOTFOUND)
THEN
dbms_output.put_line('Insert error?!');
END I,F;
END;

;,

SELECT * FROM plsql!01_purchase;

PL/SQL.
, , . , SQL PL/SQL
.
PLSQL101_PRODUCT,
SQL-. . 9.3.
INSERT INTO plsql!01_product
VALUES ('Large H a r f l i n g e r 1 ,
21,
100,
'29-AUG-01') ;
INSERT INTO plsql!01_product
VALUES ('Round Snaphoo',
12,
144,
'21-JUL-011);
SELECT * FROM plsql!01_product;
. "

u
'"-. '

:-;

'-

.,-.;.

PL/SQL. . . 9.4.
CREATE OR REPLACE PROCEDURE update_prod (
prod_rec plsqll01_product%ROWTYPE
) IS
BEGIN
UPDATE plsql!01_product
SET
last_stock_date = prod_rec.last_stock_date,.
quantity_on_hand = quantity_on_hand
+
prod_rec.quantity_on_hand
WHERE product_name = prod_rec..product_naine;
END' upda-te_p-rod;
/
DECLARE
':, '. . : , ; :
,
,
a plsql!01_product%ROWTYPE;
BEGIN

. .

9
;ra..prod\jct_name' := ''Small Widget'
v
'a'.produet_pri6e := '87;
a . quantity_on_hand := 31; ,
a.last_Stock_date := TO_DATE,( i'23
update_prod (a) ;

END;
SELECT * FROM plsqll01_product;

, . DML- SQL
PL/SQL . , , .
A Oracle SQL'Plus
Be Edit Search Qptions Help
SQL> INSERT INTO plsqll81 product
2 VALUES ('Large Harflinger1.;..-,
3
21,
4
100,
5
29-AUG-01 1 );

1 row created.
SQL>
SQL> INSERT INTO plsq!101_product ,
2 UflLUES CRound Snaphoo 1 ,
3
12,

144,

. '21-JUL-OI1)!

1 row created.
SQL> SELECT FROM plsql181_product;
PHODUCTJWME

Snail Widget
Hediun Wodget ._ , :
Chrome Phoobar :
Round Chrome. Snaphoo
Extra Huge Nega Phoobar
Square Zinculator
Anodized FramiFier
Red Snaphoo
Blue Snaphoo
Large HarFlinger
Round Snaphoo

PRODUCT_PRICE QUANTITV_ONJWND LAST^STOC

96.03
72.75
.48 .5
24.25
8.69
39.29
42.78
1.71
1.71
21
12

1 15-JAN-03
1000 15-JAN-02
100 15-JAN-03
10000
1234 1S-JAN-04
1 31-DEC-02
5
10 31-DEC-01
10 3Q-DEC-01
100 29-AUG-01
144 21-JUL-01

1l""rp'iis selected.
SQL>

. 9.3. PLSQL101_PRODUGT

309

PL/SQL
Jf * Oiacle SQL-Plus
file Edit Search Qpliohs Help
SQL> CREATE OR REPLACE PROCEDURE update prod

2
3
4

5
6
7

. . 8.
9
10
11

I 12

prod rec plsql101 product%ROWTVPE


) IS

BEGIN

UPDATE plsq!181_product
SET
last_stock_date prod_rec.last_stock date,
quantity_on_hand - quantity_on_hand
. ' '
' +
prod_rec.quantity on_ham|
WHERE product_name - prod_rec.product_name;
END update prod;

.
.

;
-

i Procedure created.

SQL> DECLARE

2
3

a plsq!181 product%ROWTVPE;
^ '

BEGIN

'

'

4
a.product_name :- 'Small Widget';
5
a .product_price := 87;
6
a. quantity on hand := 31;
7
a.last_stoch_date : TO_DATE('23-NOU-01');
8
update prod(a);
9 END;
10 /
PL/SQL procedure successfully completed.

- '

'

''
" t
:'

I
-

SQL> SELECT FROM plsq!101_product;


PRODUCT_NAME

PRODUCT_PRICE QUANTITV_ON_HAND LAST_STOC

Snail Widget
Medium Wodget
Chrome Phoobar
Round Chrome Snaphoo
Extra Huge Mega Phoobar +
Square Zinculator
Anodized Framifier
Red Snaphoo
Blue Snaphoo
Large Harflinger
! Round Snaphoo

', .

/'

96.03
72.75
48.5
24.25
8.69
39.29
42.78
1.71
1.71
21
12

32
1000
100
18000
1234
1

23-NOU-01
15- JAN- 82
15-JAN-03
15-JAN-04
31 -DEC- 02

5
10
10
100
144

31-DEC-01
30-DEC-01
29-AUG-01
21-JUL-01

11 rows selected.
SQL>

<U

' '

iG

. 9.4. PL/SQL

.
-, , SQL-. . . 9.5.

>

9
_

-t Oracle SQL-Plus
File Edit Search Options Help
'SqL> Set serveroutput oii
S(JL>
i
SflL> BEGIN
f
;; ,
.2.;
DELETE FROM plsqliei_product
'3
WHERE
product_name = 'junk';
4
IF (SQUHOTFOUND)
5
THEN
6
dbns_output.put_line('No such product');
7
END IF;
S END;

c..-fi / ;v<-<H:r;::::
Ho such product

HBO
.1
J

;.::,(;.

PL/SQL
procedure
successfully completed.
r
;
]
:
:
K5 Hv ii21 ' . * ': . - ' ' ^ - - - ^ : ' -. . ' ' ' :r: .' :";..','. -''''' .'.'
SQL> |

.:

- .

JLLJ

. 9.5. PL/SQL-
set s e r ve r ou{.pijt . on .

, , <..

BEGIN
DELETE FROM plsqll01_product .
WHERE product_name = 'junk';
IF (SQL%NOTFOUND)
THEN
dbms_output.put_line ( 'No such product');
END IF;
END;


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

PL/SQL

311


, , . . ,
. .



, , SQL-.
, 5000 ,
SYSDATE. 5000. . . 9.6.


: ,
..

, . , ,
. 9. 6 ,
SYSDATE.
DROP TABLE plsq!101_timetab CASCADE CONSTRAINTS;
CREATE TABLE plsqll01_timetab (
cl
NUMBER NOT NULL,
c2
VARCHAR2(30) NULL,
c3
DATE NULL

CREATE OR REPLACE PROCEDURE test_time IS


maxloops NUMBER := 5000;
loopcount NUMBER (6,0) := 0;
starttime CHAR(5);
endtime CHAR (5) ;
/*
, ,
.
*/
runtime NUMBER;
processrate NUMBER (20, 10) ;
BEGIN
starttime := TO_CHAR( SYSDATE, 'SSSSS');
LOOP
loopcount := loopcount +1;
INSERT INTO plsql!01_timetab (Cl, C2, C3)
VALUES (loopcount, 'TEST ENTRY', SYSDATE);
COMMIT;
IF loopcount >= maxloops THEN

312

9
EXIT;
END IF;
END LOOP;
COMMIT;
endtime := TO_CHAR(SYSDATE,'SSSSS');
runtime := TO_NUMBER(endtime) - TO_NUMBER(starttime);
dbms_output.put_line(runtime || ' seconds');
processrate := maxloops / runtime;
INSERT INTO plsqll01_timetab (Cl, C2, C3) VALUES
(loopcount+1,
TO_CHAR(processrate, '9999999999') | I
'records per second',
SYSDATE

);
END test_time;

/ :

EXECUTE test_time;
SELECT * FROM plsql!01_timetab
WHERE cl > 5000;

, . . . 9.7.
TRUNCATE TABLE plsql!01_timetab;
COMMIT;
SET SERVEROUTPUT ON
BEGIN
FOR trial_count IN 1..10
LOOP
test_time;
COMMIT;
END LOOP;
END;
/
SELECT *
FROM
plsqll01_timetab
WHERE cl > 5000ORDER BY c3;

TIMING

, TIMING. , , . . 9.8.
7470. , TIMING
START TIMING STOP 7.47 .
TIMING START;
EXECUTE test_time;
COMMIT;
TIMING STOP;

PL/SQL

3*3

4" Otacle SOL'Plus


file Edit eaich Qptions
Table created.

KWI3

SQL> CREATE OR REPLACE PROCEDURE test_tirae IS


2
naxloops NUMBER :- 5000;
3
loopcount NUMBER(6,e) := ;
it
starttime CHAR(5) ;
5
endtine CHflR(5) ;
6
/* Note that since the start and end tines are defined in terms
of the number of seconds since midnight, this routine will not
work if the run time crosses ouer midnight.

I9

runtime NUMBER;
H
processrate NUMBER (2 8, 18) ;
11
12 BEGIN
1
13
starttine :- TO CHAR(SVSDATE, 'SSSSS ) ;
14
LOOP
15
loopcount :- loopcount +1 ;
16
INSERT INTO plsqUOl timetab (C1, C2,C3)
17
UALUES (loopcount, 'TEST ENTRY 1 . SYSDATE);
COMMIT;
18
IF loopcount >= naxloops THEN
19
EXIT;'
2*
21
.': '. END IF; . f,,r.n: , .*.,. ! ,M,
-;/r. >.-,-;
22
END LOOP;
23
COMMIT;
21
endtine := TO_CHflR(SYSDATE, 'SSSSS1 ) ;
25
runtime :- TO_NUMBER(endtine)-TO_NUMBEH(starttime);
26
dbms_output.put_line( runtime || ' seconds' );
27
processrate := naxloops / runtime;
28
INSERT INTO plsql101_timetab (C1, CZ, C3) UALUES
29
(loopcount+1 ,

TO_CHAR(processrate, ' 9999999999 ' ) 1 1 ' records per second1,


31
SVSDATE
32
);
33 END test time;
34
Procedure created.
SQL> EXECUTE test_time;
6 seconds
PL/SQL procedure successfully completed.
SQL> SELECT FROM plsqliei_timetab
2 WHERE C1 > 5BOO;
1 2
5

C3

i '

833 records per second 22-OCT-OO

SQL>

. 9.6. PL/SQL-

314

-.*

esc

* Oiacle SQL-Plus
File di( Seatch Options Help
SQL> TRUNCATE TABLE plsql181_tinetab;

" ' " i!

'Table truncated.

. : - . .

-V ,

'

.-

SQL> COMMIT;

Commit complete.

;,..'->-.:
- ' " - ' ' .

SQL> SET SERUEROUTPUT ON


SQL> BEGIN

.'

2
FOR trial count IN 1..18
3
LOOP
4
test time;
5
COMMIT;
6
END LOOP;
7 END;
8 /
7 seconds
8 seconds
7 seconds'
7 seconds
7 seconds
6 seconds
7 seconds
8 seconds
7 seconds
8 seconds

--
."

.v - .. .

' ' ' ' '.

'

-.

.;

'-.;".-

PL/SQL procedure successfully completed.


SQL> SELECT
2 FROM
3
4

'-',''.

*
plsql181 timetab

'

WHERE
C1 > 5080
ORDER BV c3;

.-'.''

. '

' .

_; -

: - . , ' ! " ' "


, "

'i

;;. '

: ' .

-_,
.

C3

714 records
625 records
714 records
714 records
714 records
833 records
714 records
625 records
714 records
625 records

per
per
per
per
per
per
per
per
per
per

18 rows selected.

second
second
second
second
second
second
second
second
second
second

22-OCT-OO
22-OCT-OO
22-OCT-08
22-OCT-88
22-OCT-OO
22-OCT-80
22-OCT-08
22-OCT-OO
22-OCT-80
22-OCT-08

' , " ' < ;' , " -

SQL>

-dJ

..

C1 C2

5081
5001
5801
5881
5881
5001
5081
5801
5881
5881

' :

s
.

.. .',-,*. ' ;

>
'

/'

-.'.''

: V

. 9.7. ,

: 4

'

: , ' ' 'A

PL/SQL

315

* Oiacle SQL-Plus
ite * Search , Options Help
SQL> TIMING STftRT;
SQL> EXECUTE test_tine;
7 seconds

PL/SQL procedure successfully completed.


SQL> COMMIT;
Commit

complete.

SQL> TIMING STOP;


real: 7470
SQL>

. 9.8. INSERT TIMING

PL/SQL
? ,
. , ,
. "" , ,
. PL/SQL ,
(packages) , , .
, .
.
, , - .
? . ,
. , . .
, ,
.
:
CREATE PACKAGE _ IS
[ __]
.^, .[]
[ __]
END [_]',

316

:
CREATE OR REPLACE PACKAGE BODY ^ IS
[ ]
[ ] ,
[ __ ]
BEGIN
[ ]
[EXCEPTION]
[ ]
END [^];
,.
, ,
. , ,
.
, , ,
, , . (
CREATE OR REPLACE) , , .
, , (package variables).
. - , Oracle , ,
.
; , , , .
.
, , - ,
.
, ..
_._.
dbms_output.put_line dbms_output,
Oracle. _ .
, . . SQL-, ,
. . 9.9 SQL*Plus.
CREATE OR REPLACE PACKAGE plsqll01_pack IS ''''
DATE_LOADED DATE; . -
/* (performance)

. Status
.

PL/SQL

317
\

*/

TYPE pkg_perform_type IS RECORD


(person_code
plsql!01_person.person_code%TYPE,
per son_name
char ( 12 ) ,
current_sales
NUMBER(8,2),
perform_percent NUMBER(8,1),
status
char (30)
);
CURSOR PKG_PER_CUR RETURN plsql!01_person%ROWTYPE;
/* .
.
( ) .
*/
FUNCTION pkg_comp_discounts (order_amt NUMBER) RETURN NUMBER*

/*
.
a_person.
, current_sales .
(,
), perform percent .!!
*/
PROCEDURE pkg_compute_perform
(a_person plsqll01_person%ROWTYPE,
.,,-,.,
a_perform OUT pkg_perform_type) ;
.,.,
END plsql!01_pack;
CREATE OR REPLACE PACKAGE
small_order_amt
large_order_amt
small_disct
large_disct N

BODY plsql!01_pack IS
NUMBER (8, 2) := 400;
NUMBER (8, 2) := 1000;
NUMBER (4, 2) := 1;
UMBER (4, 2) := 5;

CURSOR PKG_PER_CUR
RETURN plsql!01_person%ROWTYPE
IS
SELECT *
FROM
plsqllOljperson;
-/':..,..,

,-

FUNCTION pkg_comp_discounts (order_amt NUMBER)


RETURN1 NUMBER IS
BEGIN
IF (order_amt < large_order_amt

AND"

order_amt >- small_order_amt)


THEN
RETURN (order_amt * small_disct / 100) ;
ELSIF (order_amt >= large_order_amt)
THEN
RETURN (order_amt * large_disct / 10Q) ;
ELSE
RETURN (0);

318

9
END IF;
END pkg_comp_discounts;
PROCEDURE pkg_compute_perform
(a_person plsql!01_person%ROWTYPE,
a_perform OUT pkg_perform_type)
IS
hist_ord_avg NUMBER(8,2) := 0;
current_avg_sales NUMBER(8,2) := 0;
BEGIN
a_perform.person_code : a_person.person_code;
a_perform.person_name := a_person.last_name;
a_perform.status := NULL;
..--

BEGIN
SELECT SUM(tb!2.product_price * tbll.quantity),
AVG(tbl2.product_price * tbll.quantity)
INTO
a_perform.current_sales,
current_avg_sales
FROM
plsql!01_purchase tbll,
plsql!01_product tb!2
WHERE tbll,product_name = tb!2.product_name
GROUP BY tbll.salesperson
HAVING tbll.salesperson = a_person.person_code;
EXCEPTION
WHEN NO_DATA_FOUND
THEN
a_perform.status :=
'Current purchases exception';
a_perform.current_sales := 0;
END;
BEGIN
SELECT AVG(tab2.product_price * tabl.quantity)
avg_order
INTO
hist ord_avg
FROM
plsqllOl purchase_archive tabl,
plsql!01_product tab2
WHERE tabl.product_name = tab2.product_name
GROUP BY tabl.salesperson
HAVING tabl.salesperson = a_person.person_code;
a_perform.perform_percent :
100 * (current_avg_sales-hist_ord_avg) / hist_ord_avg;
a_perform.status := 'All fine';
EXCEPTION
WHEN NO_DATA_FOUND
THEN
a_perform.perform percent := 0;
IF (a_perform.status IS NULL)
THEN

PL/SQL

319
a_perform.status :=
'Erroneous or no history1.;
END IF;

END;
EXCEPTION
WHEN NO_DATA_FOUND
THEN
a_perform.status := 'Exceptions found1;'
END pkg_compute_perform;

BEGIN
/* */
DATE_LOADED := SYSDATE;
END plsql!01_pack;
DECLARE
one_perform plsql!01_pack.pkg_perform_type;
cursale char(8);
disct char(8);
perf char(8);
,
BEGIN
dbms output.put line('Code' ||
' ' II
'Last Name 1 I I
'.'II
'Total Sales' ||
1 1
I I
'Discounts' ||
' ' I I
'Performance%' ||
I
'" I I
'Errors?');
FOR person_rec IN plsql!01_pack.PKG_PER_CUR
LOOP
plsq!101_pack.pkg_compute_perform(person_rec,
one_perform);
cursale := TO_CHAR(one_perform.current_sales);
disct := TO_CHAR(plsqll01_pack.pkg_comp_discounts
(one_perform.current_sales));
perf := TO_CHAR(one_perform.perform_percent);
dbms_output.put_line(one_perform.person_code ||
' ' I I
one_perform.person_name ||
I I
.11
cursale ||
1
' II
disct ,| |
perf

I
one perform.status);
END LOOP;

12 . 725

320

dbms_output.put_line('Pkg load date seconds ' ||


1
TO_CHAR(plsqll01_pack.DATE_LOADED, SSSSS ));
dbms_output.put_line('System date seconds ' ||
TO CHAR(SYSDATE, ' SSSSS'));
END;
/

mma

* Oracle SOL'Plus

:
:
:
FJe Ed Search ^ptioni **>
'
:;' , * V.' 'x'^-'d
FOR person_rec IN plsql101_pack.pkg per_cur
19
20
LOOP
21
plsq!101_pack.pkg_conpute_perf orn(person_rec, one_perform) ;

22
23
24
25
26
27
28
29
3D
31
32
33
34
35
36
37
38
39
40

cursale :- TO_CHAR(one_perforn.current_sales);
disct
:- TO_CHAR(plsql101_pack.pkg_conp_discounts
(one_perforn.cui*rent_sales));
perf
:- TO_CHftR(one_pet-forn.perforn_percent);
dbns_output.put_line(one_perforn.person_code | |
1
' II
one_perf oi-n.pei-son_nane 1 1
' ' II
cursale 1 1
disct
perf
END LOOP;

one_perforn. status) ;

dbns_output.put line('Pkg load date seconds ' ||

42
TO_CHftR ( plsqii Ol_pack .date.loaded , ' SSSSS ' ) > ;
43
44
dbns_output.put_line{' System date seconds ' ||
45
TO_CHflR(SVSDftTE, 'SSSSS'));
46
47 END;
48

Code Last Name Total Sales Discounts


Atlas
313.31
0
Gfl Anderson
865.24
8.6524
BB Barkenhagen
5456.25 272.8125
LB Baxter
1455
72.75
LN Norton
1455
72.75
Pkg load date seconds 39571
System date seconds 39571

Performance^ Errors?
7.7
All fine
-58.8
All fine
4446.9
All fine
All fine
Erroneous or no history

PL/SQL procedure successfully completed.


SQL>

-iLJ

. 9.9.

PL/SQL

321

, PKG_PER_CUR
. CHAR "" : .
,
. DATE_LOADED . , .
, - .

8.
, .
, ,
, (triggering event).
, :
DML
DDL
,
,
PL/SQL :
. Oracle
.
.

.
:
, ,
PL/SQL.
.
.
.
,

.
. ,

, . ,
,
12*

322

9

, , .


. ,
, .
-.
, ,
, ,
,
.
,
, ,
.
. ,
,
.
. ,
,
"Small Widget" .
, , , . , ,
.
, , . ,
, ,
.
, ,
, . , , ,
20- .
.
20- , .
, ,
(table triggers). .
:
CREATE OR REPLACE __
_
ON _
[WHEN _]
[FOR EACH ROW]

PL/SQL

323

[DECLARE
]
BEGIN

[EXCEPTION
WHEN _
THEN...]
END _',
, ,
.


_ , :
(BEFORE) (AFTER) (
). BEFORE, - , . .
, , BEFORE- (BEFORE trigger).
AFTER,
, .
.
. AFTER- (AFTER trigger).
_ INSERT, UPDATE'
DELETE.
^ , .
FOR EACH ROW , . (row triggers).
FOR EACH ROW , . (statement trigger),
.
DECLARE END _
PL/SQL.
OR. :
DELETE OR INSERT _
UPDATE :
UPDATE _1, _2,...
UPDATE PL/SQL
"new" "old", . , :._ , . "old" "new"
.

324


,
. . . 9.10.
. NULL.
ALTER TABLE plsql!01_purchase
ADD ORDER_NUMBER NUMBER(10);
.
CREATE TABLE plsql!01_audit
(ORDER_NUMBER "
NUMBER(10),
person_code
VARCHAR2(3),
user_name
CHAR(30),
user_machine
CHAR(20),
change_in_quant
NUMBER(5),
transaction_time
DATE,
FOREIGN KEY (person_code) REFERENCES plsql!01_person);

CREATE SEQUENCE order_num_seq;
CREATE OR REPLACE TRIGGER audit_trigger
BEFORE INSERT OR UPDATE ON plsql!01_purchase
FOR EACH ROW
DECLARE
no_name_change EXCEPTION;
quant_change NUMBER(5) := 0;
BEGIN
/* He product_name .
.
*/
IF (UPDATING
AND
(:NEW.product_name <> :OLD.product_name))
THEN
RAISE no_name_change;
END IF;
/* ,
, .
*/
IF (((UPDATING)
AND
(:OLD.ORDER_NUMBER IS NULL))
OR
((INSERTING)
AND
(:NEW.ORDER_NUMBER IS NULL)))
THEN
SELECT order_num_seq.NEXTVAL
INTO
:NEW.ORDER_NUMBER
FROM
dual;
END IF;

PL/SQL

325

/* ,
,
.
.
*/
IF (UPDATING)
THEN
quant_change := : NEW. quantity-: OLD. quantity;
ELSE
quant_change := : NEW. quantity;
END IF;
INSERT INTO plsql!01_audit
VALUES
( : NEW . ORDER_NUMBER,
: NEW . salesperson,
USER,
USERENV ( ' TERMINAL ' ) ,
quant_change,
SYSDATE) ;
EXCEPTION
WHEN no_name_change
THEN
dbms_output . put_line
('Change of product name not allowed');
dbms_output . put_line
('Aborting and resetting to old values');
:NEW.product_name
:= :OLD.product_name;
:NEW. salesperson
:= :OLD. salesperson;
:NEW.ORDER_NUMBER := : OLD.ORDER_NUMBER;
: NEW. quantity
:= : OLD. quantity;
END audit_trigger;
SELECT * FROM plsql!01_purchase;
SELECT * FROM plsql!01_audit;
INSERT INTO plsql!01_purchase
VALUES ('Round Snaphoo', 'LN', ' 15-NOV-02 ' , 2, NULL) ;
SELECT * FROM plsql!01_purchase WHERE salesperson = 'LN';
SELECT * FROM plsql!01_audit;
UPDATE plsql!01_purchase SET salesperson = 'LB'
WHERE salesperson = 'CA' AND quantity = 1;
SELECT * FROM plsq!101_j>urchase WHERE salesperson =.'CA';
SELECT * FROM plsqll01~audit;
UPDATE plsql!01_purchase SET quantity = 20
WHERE salesperson = 'BB';
SELECT * FROM plsql!01_purchase WHERE salesperson = 'BB';
SELECT * FROM plsql!01_audit;
UPDATE plsql!01_purchase SET product_name = 'Round Snaphoo'
WHERE salesperson = 'BB';
SELECT * FROM plsq!101_purchase WHERE salesperson = 'BB';
SELECT * FROM plsq!101_audi.t;

326

fc. < Seech fiptiorn


SQL> SELECT * FROM plsqllB1_purchase WHERE salesperson - 'LN';
PRODUCTJMHE

SAL PURCHASE.

Medium Wodget
Round Snaphoo

LN
LN

QUANTITV ORDER_NUMBER

18-AUG-82
15-NOU-G2

28
2

12

SQL> SELECT FROM plsqll01 a u d i t ;


ORDER_NUMBCR PER USER_NAHE
12 LN

USER MHCHINE

PLSQL181

CHANGE IN QUHNT TRANSACTI

ALPHANERD

2 22-OCT-B8

SQL> UPDATE plsqll81 purchase SET salesperson -


2
WHERE salesperson * 'CO' AND quantity - 1;
2 rows updated.
SQL> SELECT FROM plsqllB1_purchase WHERE salesperson - ' C A ' ;
PRODUCTJMHE

SAL PURCHASE.

Round Chrome Snaphoo

CR

QUAHTITV ORDER_NUIER

16-JUL-03

SQL> SELECT FROM plsqllB1_audit;


ORDERJWHBER PER USER_NAHE
12 LN
13 LB
1* LB

USER_MACHINE

PLSQL1B1
PLSQL101
PLSQL181

CHANGE_IN_QUANT TRANSACTI

ALPHANERD
ALPHANERD
ALPHAHERD

2 22-OCT-B8
8 22--8
8 22-OCT-8B

SQL> UPDATE plsqllB1_purchase SET quantity - 28 UHERE salesperson - 'BB';


1 row updated.

SQL> SELECT FROM plsqllB1_purchase WHERE salesperson - ' B B ' ;


PRODUCT JHHME

SAL PURCHASE.

Medium Wodget

BB

1U-JUL-03

QUANTITY ORDERJWMBER
28

15

SQL> SELECT * FROM plsqllB1_audit;


DRDER_NUHBER PER USER_NAHE
12
13
14
15

USER_HACHINE

LN PLSQL1B1
LB PLSQL1B1
LB PLSQL1B1
BB PLSQL1B1

CHAHGE_IN_QUANT TRANSACTI

ALPHANERD
ALPHANERD
ALPHANERD
ALPHANERD

2
8
8
-55

22-OCT-BB
22-OCT-B8
22-OCT-D8
22-OCT-88

SQL> UPDATE plsq!181 purchase SET product_nane - 'Round Snaphoo'


2
WHERE salesperson - 'BB';
Change OF product name not allowed
Aborting and resetting to old values
1 row updated.
SQL> SELECT FROM plsqllB1_purchase UHERE salesperson - '88';
PRODUCT_NAHE

SAL PURCHASE.

Hediun Wodget

BB

Ht-JUL-03

QUANTITV ORDER_HUMBER

2B

15

SQL> SELECT FROM plsqllB1_audit;


ORDER_HUH8ER PER USER_NANE
12
13
14
15

LN
LB
LB
BB

PLSQL1B1
PLSQL181
PLSQL1B1
PLSQL1B1

USER_MACHINE
ALPHANERD
ALPHANERD
ALPHANERD
ALPHANERD

CHAHGE_IN_QUANT TRANSACTI
2
8
8
-55

SQL>

. 9.10. ,

22--
22-OCT-BB
22-OCT-BB
22-OCT-88

PL/SQL

327

, .
ORDER_NUMBER. , .
USER,
USERENV, TERMINAL1. , ,
. product_name
,
, .
.
UPDATING INSERTING , .

PLSQL101_PURCHASEn .
, .


, , . CREATE
OR REPLACE TRIGGER.
, :
DROP TRIGGER _;
, :
ALTER TRIGGER _ DISABLE;
ALTER TRIGGER _ ENABLE;

,
:
, DML,
.
.
, INSERT,
. INSERT
, NULL.
, UPDATE,
. BEFORE,
AFTER-.
, DELETE,
.
, NULL.
:new .
, ORA-4084.

328

ROLLBACK, COMMIT
SAVEPOINT.

, ,
.

, , ..
, .
,
, " ".
,
Oracle , ,
, , .
,
,
.
,
.
.
INSERT, UPDATE DELETE
.

ODBC
Oracle SQL*Plus.
, , Oracle Microsoft Access Excel. , ,
. ODBC-. , Windows.
ODBC "Open Database Connectivity" ("
"). ,
Access Excel . ODBC
Oracle, SQL*Plus.
ODBC .

ODBC-
ODBC- :



PL/SQL

329

, . Oracle, ,
SQL*Plus. . 9.11
, .

IPLSQLIOI

User Name:
Password:

|your_ho8t_name|

Host Suing:

OK

Cancel

. 9.11. SQL'Plus,
ODBC-
ODBC-, :
1. Microsoft ODBC Administrator. Start,
Programs Oracle.
Microsoft ODBC Data
Source Administrator. ,
. 9.12.
t ' Data Souice Adminisltatoi
.

UserDSN | S^tomDSN | FileDSN | Drivers | Tracing j .Connection ** | About |


Jser Data Sources:
Name
| Drivei
EJJSSJ2SI
Microsoft dBase Driver (".dbf)
DeluxeCD
Microsoft Access Olivet (*.mdb)
Excel Files
.
Microsoft Excel Drivei |".xls)
FoxPioFtes
Microsoft FoxPro Driver (".dbf)
MS Access 7.0 Database Microsoft Access Oliver (".mdbl
MS Access 37 Database Microsoft Access Driver (".mdbj
Paradox Files
Microsoft Paradox Driver (N.db)
Text Files
Microsoft Text Driver ('.bit; ".csvj
Visual FoxPro Database Microsoft Visual FoxPro Driver
Visual FoxPro Tables
Microofl Visual FoxPro Oliver

Ajjd... ,

1.

Bemove
Configure...

yBEfl An ODBC User data source stores informatbn about how to conned to
r^pJ the indicated data provider. AUser data source is only visible to you.
^^^ end can onfci be used on the current machine.

OK.

Cancel

Apply

. 9.12. Microsoft ODBC Data Source Administrator

Help

330

2. ,
ODBC- ,
System DSN . ODBC-
, User DSN.
3. Add. Create
New Data Source, . 9.13.
Oracle ODBC Driver Finish.
4. OracleS ODBC
Driver Setup, . 9.14. Data
Source Name. ,
ODBC-.
ODBC- .
, Data Source Name PLSQL101.
5. Service Name. Oracle,
SQL*Plus.
PLSQL101.
6.
ODBC Data Source Administrator. Data
Sources .
7. , ODBC Data
Source Administrator.
8. ODBC-,
Oracle Start
Oracle ODBC Test. ,
. 9.15.
Create New Data Source
Delect a driver lor which you want to set up a data source.
Name
Microsoft Excel Driver ('.xls)
Microsoft FoxPro Driver f.dbf]
Microsoft ODBC lor Oracle
Microsoft Paradox Driver (".do)
Microsoft Text Driver (".txt; ".csv)
Microsoft Visual FoxPro Driver
Oracle ODBC Driver for Rdb
SQL Server

. 9.13.

331

PL/SQL
^1<1
^

OracleS ODBC Driver Setup

OK

Data Source Name: II


Description:

Cancel 1
Help

Service Name:

UserlD:

l'

Connect to database in Read only mode

"

Prefetch Count:

'

'

Enable Thread Safety


Enable Failover

. L-

flption:

F
F R etry Count JT5

Delay:

fio

eL&n

fo

., '

. 9.14. ODBC- Oracle


SOiaclH ODBC 32Bit Test - IQUERY1I
|g Fife Edit yiew

tfndow

BwSet

QjagjBl ^Nffll <S|?I


I

Connect..

Disconnect I

______________
All Tables

I User Tables

E:earte...

Ready

. 9.15. ODBC-

(NextRowSet

332

9. Connect, Select Data


Source, . 9.16.
10. Machine Data Source
ODBC-. ,
, .
11. Oracle ODBC Driver Connect
(,
, , ,
SQL*Plus).
PLSQL101.
.
12. Connect Disconnect, All Tables,
User Tables Execute. User Tables.
(. . 9.17).
Oracle ODBC Test .
Next Row Set,
RowSet 1100.
13. Oracle ODBC Test,
Disconnect, File | Exit.
Select Data Source
Fife Data Source: Machine Data Source |
Data Source Name
MS Access 7.0 Database
MS Access 97 Database
ORCL_ODBC
Paradox Files
{'^!
Text Files
Visual FoxPro Database
Visual FoxPro abtes

<i ' ; g j jii

I Type I Description
Use
Use
Syst em
Use
Syst em
Use
Use
U se

-J

I >

. . .-

Hew...

A Machine Data Source is specific to this machine, and cannot be shared.


"User" data sources are specific to a user on this machine. "System" data
sources can be used by all users on this machine, or by a system-wide service.

OK

Cancel

Help

. 9.16. ODBC

PL/SQL

333

& * * Wn*> flowSet belp

,1^1 X)

QloSJHJ ] 45|tf|
Connect.

Disconnect

All Tables

1 User Tables 1

Execute...

Next Row Set 1

to* I

1
2
3
H
S

TBBLE_CflI

TflBLE_SCHEM

<Null>
<Null>
<Mull>
<Null>
<Hull>

TABLE.MHE

PLSQL181
PLSQL1I1
PLSQL1I1
PLSQL101
PLSQL1B1

. :
^

PLSQL1H AUDIT
PLSQL1I1~LOG
PLSQL101~OLD ITEH
PLSQLH1 PERSON
PLSQUtl .PRODUCT

,' -

Rea* .^

:;

\:

. . .

. ,

i . . .

; . . . ! ' : |NUM|:. :, j

. 9.17. ODBC-
ODBC-
Oracle! , .

ODBC-
, , ODBC-: Access
Excel.

Access

Oracle
Access. Access
. ,
, .
Access, .
1. Access.
Access PLSQL101.mdb.
2. File | Get External Data | Link.
File Open.
3. Files of Type .
ODBC Databases.
ODBC Select Data Source (
. 9.16).

334

4. Machine Data Source.


ODBC ( PLSQL101), ,
.
5.
.
6. Oracle,
ODBC-. ,
;
. ,
, .
PLSQL101.
Windows- Access
CTRL+
SHIFT+.
.
7.
, Save Password.
8. . Access
.
, ,
. ,
PLSQL101_AUDIT , Access
, . 9.18.
,
, ,
,
Access. ,
Access,
, .
Select Unique Record Identifier
0elds in table 'PLSQL101_PLSQL101_AUDIT':
ORDER NUMBER
PE~RSON_Coi5e~
USER_NAME
USER_MACHINE
CHANGE_IN_QUANT
TRANSACTION TIME

OK
Cancel

To ensure data integrity and to update records, you


must choose a field or Fields that uniquely identify each
record. Select up to ten fields.

. 9.18.
,

PL/SQL

335

9. , ,
. 9.19. ,
.
; Oracle .
10. Access, .
, PLSQL101_PURCHASE
, . 9.20.

Oracle Excel

Access Excel Oracle : Oracle Excel, . Excel .


"" Oracle Excel, :
1. Excel.
2. Data | Get External Data | Create New Query.
Choose Data Source,
. 9.21.
3. (
PLSQL101), , .
4.
.
5. Oracle,
ODBC-.
;
*, Micfoioll Access - [PLSUL10! : Ualabase)

Hl*tE3

II 9 &ft View [nsert lools aM* H*

STables

^Queries

Sperms

-IfflX

Reports

3 Macros

$ Modules

KSQ1101_PL5Q1101_AUDITJ

gl"

RSQL101_PL5QL101_OLDJTEM

pK)gn

H PLSQll01^PLSQI.mi_PERSON
PLSQI.101J>L5QLmi_PROOUCT
aSQL101_PLSQL101_PURCMASE
> PLSQC101_PLSQL101_PURCHASE_ARCHIVE
> PL5QL101J>L5QL101_TIMETAB

ie

eed,

. 9.19. Access

p-f f

!:

336
ull Access -IPLSQL1U1 PLSQL101 PUflCHASE
1 FJe Edit " Insert Format Records Idols Window

PRODUCTJIAHE

v | *

I SALESPERSON | PURCHASE_DATE | QUANTITY | ORDER

Medium Wodget
Chrome Phoobar
Small Widget
Medium Wodget
Round Chrome Snaphoo
Small Widget
Medium Wodget

CA
BB
GA
GA
LB
CA
CA
LN

7/14/2003
7/14/2003
7/14/2003
7/15/2003
7/15/2003
7/16/2003
7/17/2003
8/18/2002

{DaTisheetVIm

. 9.20. Oracle Access


Choose Data Source

OK

Databases I Queries |
MS Access 97 Database'
ORa_ODBC"
Paradox Files (not sharable)
Paiadox Files*
PLSQL101"
) Files (not sharable]
Text FJes"
Visual FoxPro DatabaseVisual FoxPro Tables"
Gp|

Cancel
Browse...

F Use the Query Wizard to create/edit queries

. 9.21. Excel
. ,
, , . , , ,
. PLSQL101_PURCHASE Columns in Your Query. ,
. 9.22, Next .
6. ,
WHERE SQL- SELECT. ,
, , ,
, Next
.

PL/SQL

337

What columns of data do you want to include in your query?


Columns in your query:

Available tabtes and columns:


6! PLSQL101 .PRODUCT

jj.

PRODUCT NAME

>

PISQL1
01
.PURCHASE

PLSQL101 PURCHASE ARCHIVE -J

ffl PLSQL101_PURCHASE_ARCHIVE
PLSQL10lIPURCHASE_LOG
-J

SALESPERSON
PURCHASE DATE
QUANTITY
ORDER NUMBER

^review of data in selected column:

Gp|

Pjeviev* Now j

< ack

^eKt>

Cared

. 9.22. Oracle Excel


7.
. Next.
8. Return
Data to Microsoft Excel Finish.
9. ,
Oracle. .
10. Oracle Excel,
. 9.23.

-iJlx't

JpFJe * Bew insert Format tools E*a Window Help

]|D * [sa *la ft ^ j , o. . | 4 f | Z /. ' "iiHl f " jj^


jJArial

- 10

Al

1
1
2
3
A
5
6
7
8
9

_J

'||H

7 II * S | % i

*| E- * , '

PRODUCT1 NAME
Small Widget
Medium Wodget
Chrome Phoobar
Small Widget
Medium Wodget
Round Chrome Snaphoo
Small Widget
Medium Wodget

SAL
CA
BB
GA
GA
LB
CA
CA
LN

J_

ESPERSON PURCH"ASE DATE QUANTITY ORDER NUMBER


7/14/20030:00
7/1 4/2003 0:00
7/14/20030:00
7/1 5/2003 0:00
7/15/20030:00
7/16/2003 0:00
7/17/20030:00
8/18/20020:00

RT

1
75
2
8
20
5
1
20

10
11
12

'

13 x]

--

:9&tk\ 1

-1

16,

17
Jt^ >| S**MI / ' '

~~ i < i

Ready

. 9.23. Oracle, Excel

- 1

>1

!~~~~ /s

338

, , , . ,
.
, DML SQL
PL/SQL SQL-. , . . PL/SQL, a
PL/SQL WHERE.
PL/SQL ,
. %ROWTYPE %TYPE ,
.
TIMING , .
SYSDATE.
PL/SQL, PL/SQL-,
. .
, , , .
-,
, . .
DML.
, . , ,
.
ODBC- Oracle.
ODBC- ; , . ODBC-
Microsoft: Access Excel.
! PL/SQL , ,
. !

1. DML SQL PL/SQL ?


A. DML SQL :
.
B. DML SQL PL/SQL.
C.
DML, .

PL/SQL

339

D. PL/SQL DML SQL


PL/SQL.
2. :
A. .
B.
.
C. ,
.
D. .
3. ?
A. .
,

B. ,
,
.
C.

, .
D. , ,
,
.

4. ?
A. .
B.

PL/SQL.

C.
, , ,
.
D. ,
.


1. A, D.

, ;
,
DML PL/SQL. ,
.
2. , .

340

,
, D .
,
,
.
.
3. . .

,
. .
4. , .

D . Oracle ,
,
.

342

, . ,
.
after- (after trigger) , ,
. , .
before- (before trigger) ,
- .
null . , . Null ,
.
ODBC Open Database Connectivity ( ). ,
.
PL/SQL SQL. , Oracle.
(anonymous block) PL/SQL
, .
(cursor attributes) , ,
, ,
- .
(database) .
(basic block) PL/SQL
PL/SQL. , ,
, .
- (branch blocks) *- , - . -
.
- (leaf blocks) *- Oracle , , . .
-.
(spooling)
.

343

, (nested blocks, nested loops)


() PL/SQL, ().
(foreign key) ()
.
(raising exception) , PL/SQL .
( RAISE ) , .
(expression) ,
. : 3 + 4
sales_tax / product.
(date math) , .
(parent table) , "-", ( )
() .
(group.)
SQL-.

(group values) ( ), . HAVING .


(group functions) Oracle,
, SUM, AVG, MIN, , ..
(Cartesian product) WHERE, .
"-" (Entity Relationship Diagram, ERD) , .
("").

(""), ().
(hard-coded)
.
(record)

, .

(record) PL/SQL PL/SQL, PL/SQL.

344

(populated) . ,
.
(default value) , , . ,
,
.
.

(named notation)
, _ => _.
, .
(loop index) , FOR
. FOR- IN-
FOR.
(exception) , . ,
.
(iterative processing)
. , .
(concatenation)
,
.
(constant)
, 6/3).

(-

(check constraint) , , ,
.
(literal) ,
,
.
(multirow subquery)
.

, -

(data models) . "-".

345

(modular)
(),
.
, (result set, active set)
, DML SQL (SELECT, INSERT, DELETE
UPDATE).
(implicit cursor) , DML PL/SQL.
(exception handler)
PL/SQL,
. ,
.
(trimming)
.
\

(object privileges)
, ..
(constraints)
, , .
(single-row subquery)
.

, -

(statement trigger) , ( ).
(operators)
, , .
(join operators) ,

SQL- ( SELECT) . UNION, MINUS, INTERSECT .


(package) PL/SQL PL/SQL,
, PL/SQL. (), .
(variable)
. .
(substitution variable) SQL-, , .

346

(package variables) ,
.
, . . ,
.
(flat file) ,
, , . , sales_order
.
(substring)

(child table) "-", .


(positional notation)

, . ,
.
(field) ,
-.
(sequence)
, . ,
; .
(view)
.

(data conversion)

.
, .
(privilege)
( ,
), . , , .
, , (anchored type, dynamic type)
PL/SQL,
( %ROWTYPE ), ( _%).
(application)
, .

347

(operator precedence) . , 3 - 4 / 2 ,
2, , 1.
(procedure) PL/SQL PL/SQL,
PL/SQL,
. , , .
(column alias) -, SQL-. , "Sold By"
salesperson:
. ' - .

""', ' ' . - " '-.";, yf, -;:;

SELECT product_name || ' was sold by ' |I salesperson "Sold By"


FROM
plsql!01_purchase;

(table alias) , SQL- .


(path)

, .

(parsing)

(group separator)
, .. .

(modes of parameters) , , ,
, . IN
( ), OUT ( ) IN OUT (-).
(relational database) ,
.

. .

(role) .
.
(relationship)
, . , product_id
sales_order product_catalog. product_id sales_order , product_id
product_catalog.

348

"-" (parent-child relationship)


, ()
().
" " (one-to-many relationship) ,
.
(character functions)
.

Oracle, -

(synonym) ,
.
(syntax)
.

.
(system privilege)
, .
(data dictionary) , ( , Oracle) .
(composite index)
.

(specification)
, PL/SQL.
(procedure specification)
PL/SQL
.
(package specification)

PL/SQL , , ,
, .
(function specification)

PL/SQL ,
.
(row)
(string)

.
.

(row trigger) , , .

349

(column) , (, ).
''''.-

'

* '

(concatenated index) . .
(table)
,
(, ).
(table trigger) , .
(package body) , , , , .
(datatype) ,
(NUMBER), (CHAR) ..
(trigger)
PL/SQL,
.
INSTEAD OF , , , .
(trigger action) . , ,
.
, (trigger event, trigger
statement)
(, DML), .
(trigger restriction) ,
, . , .
(unique index)
.

, Oracle -

(conditional processing)

.
(actual parameters)
,
. ,
.
(formal parameters) , .

.

350

(function)
, () () ,
.
(function) PL/SQL PL/SQL, PL/SQL, . ,
.
(control flow, execution flow) .
. ,
, (flow control statements).
PL/SQL IF LOOP.
(wildcard) , .
SQL- %.
,
"Chrome".
SELECT * FROM plsq!101_product
WHERE product_name LIKE 'Chrome%';

(Julian dates) , , .
Oracle 1 4712 . .. . , 54321.5 54 332-
1 4712 . ..
(explicit cursor), , CURSOR ... IS. -,
PL/SQL SQL. ,
SELECT.
PL/SQL.

PL/SQL
SQL PL/SQL Oracle
, SQL
PL/SQL.
: ,
.
" ".
, , "",
"", "", "" "", , ,
, SQL*Plus,
SQL- , , PL/SQL.
:
,
SQL- , ,


PL/SQL

PL/SQL
-

Oracle
Oracle. ,
SQL PL/SQL.

"'
www.lory-press.ru

Oracle Press
ITION