WWW - Kutub.info 4150

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 107

PDF created with pdfFactory Pro trial version www.pdffactory.

com


SQL & SQL * PLUS
( oracle 9i)

8 / 2009

PDF created with pdfFactory Pro trial version www.pdffactory.com

PDF created with pdfFactory Pro trial version www.pdffactory.com

PDF created with pdfFactory Pro trial version www.pdffactory.com

....
:

) ( sql


) (220
.


:
mmah_1550@hotmail.com
mmah_1550@yahoo.com
.
.


-2009-

2
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

4-3

: sql & sql * plus


1 -1 sql * plus

2 -1 sql

3 -1 sql & sql * plus

9-8

4 -1 sql * plus

10

5 -1

13 - 11

6 -1

16 - 13

7 -1

17

: select
1 -2

20 - 19

2 -2

22 - 21

3 -2 )| |(

23 - 22

4 -2 ) ( distinct

24

5 -2 ) ( where

31 - 25

6 -2 ) ( order by

33 - 31

7 -2 ) ( sql functions

52 - 33

8 -2

56 - 52

9 -2 ) ( subquery

61 - 56

10 -2

62

: ) ( Data Manipulating Language


1 -3

68 - 64

2 -3

69 - 68

3 -3

70 - 69

4 -3

71
3

PDF created with pdfFactory Pro trial version www.pdffactory.com

: ) ( transection command
1 -4

73

2 -4

74

: ) ( Data Definition Language


1 -5 ) ( tables

88 - 76

2 -5 ) ( views

90 - 88

3 -5 ) ( sequences

91 - 90

4 -5 ) ( indexes

92 - 91

5 -5 ) ( synonym

93 - 92

6 -5 ) ( users

94 - 93

7 -5 ) ( system

96 - 94

8 -5

97

: ) ( Data Control Language


1 -6

100 - 99

2 -6

100

3 -6

101

102

103

4
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

( sql & sql * plus )

sql & sql * plus

5
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

1-1 ) ( sql
) Sql ( structure query language
: sql .

:
Data Retrieval -1

select DML

( Data Manipulating language ) DML -2


) ( .

Transection command -3

( Data Definition language ) DDL -4

) ( .

( Data Control language ) DCL -5

) (
.

) : ( sql

) American National Standards ( ANSI. Institute

. . - .

- .

6
/

PDF created with pdfFactory Pro trial version www.pdffactory.com


) ( sql .
) ( sql ) ( .
) ( sql ) ; ( .

2-1 sql * plus


) ( sql ) ( sql

CONNECT

DISC

SHOW

GET

. SQL * PLUS

SET

ED

R RUN

.
.

@
START

DEL

SAVE FILENAME

) ( .

SAVE FILENAME
APPEND
SAVE FILENAME
REPLACE
QUIT

.
.
.

EXIT
7

PDF created with pdfFactory Pro trial version www.pdffactory.com

3-1 sql & sql * plus


) ( sql oracle
.

oracle orahome92

Application Development

. sql plus

:
.

sql
.


..... .

8
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

ENTER ) (

.
) * ( :

catalog

:
/ c /

ENTER .

9
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

4-1 sql * plus


save
save

filename.sql
GET

GET


append save


replace save

.sql
:

spool filename.sql
...........
...........

Spool off

. spool off

:
10
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

5-1
:

11
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

tiger
Connect and resource
demo
Connect and resource
manager DBA
:


scott
demo
system

cat catalog .

sql :
/ connect

12
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

DISC ENTER


Not connected .
:

6-1
:
desc describe

) ( emp
) . ( scott

) ( dept

) . ( scott

13
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

select * from

) . ( emp

) . ( dept
.

14
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

) ( emp ) ( dept
:

) : ( deptno .
) : ( dname .
) : ( loc .

) : ( empno .
) : ( ename .
) : ( job .

) : ( mgr .

) : ( hiredate .
) : ( sal .

) : ( comm .

) : ( deptno .
15
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

- ) : ( dual

16
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

7 -1

17
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

( sql & sql * plus )

select

18
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

: select
select :

select * or columns , aliases


from view or table
where condition
order by columns
)*( .

select . - ) . ( ,

1-2
as :

19
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

) ( ename ) ( name as

as
.

20
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

2-2
sql from

) ( ename ) ( name ) ( sal

) ( salary

) ( sal 12 ) ( annual salary .

21
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

) ( ename ) ( name ) ( comm


.

3-2 ) || (
) || ( :

) ( ename ) ( job
) . ( employees
22
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

) ( sal ) ( comm . 10

) ( sal ) ( comm
.

23
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

4-2 ) ( distinct
distinct ) ( .

10 20 30
distinct :

distinct
.
24
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

5-2 where
select

:
.

where

.
:

-1 ) ' ' ( .
-2 .

-3 sql . DD MON YY
:

DD

. 09 :

MON SEP : .
YY

09 : . 2009

where

=!=,>,<,>=,<=,<>,

in , any , all , < any , < all , > all .


between value and value
.
.
.
.
.

)'_like ( ' %
) ' soundex ( ' excerption
is null
and , or , not
25

PDF created with pdfFactory Pro trial version www.pdffactory.com

not :
not between
not like
not in
is not null

) ( where :

) ( ename ) ( job
) ( deptno . 10

) ( ename ) ( sal
. 2000

26
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

) ( ename ) ( sal
2000 . 4000

like
% .
)'( ' m%

. m

) ' ( ' _m%

. m

) ' ( ' %m% . m


) ' ( ' __m% . m
......

. M

. M

M .
27
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

soundex
.

) ( SMOTH SMITH
.

) ( no rows selected
:

and
) ( and :

true
true
false
false


true
false
true
false


true
false
false
false

28
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

2000
. 10

) ( .

or
) ( or :

true
true
false
false


true
false
true
false


true
true
true
false

29
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

2000 10
) ( .

) ( emp

6600 40 .

not
) ( not :

false
true
true
false


) ( JENES ) ( JAMES ) . ( JONES
30
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

1000 . 3000

6-2 order by
ascending descending
. select

) ( deptno
) ( .
31
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

10 ) . ( hiredate

desc :

) ( deptno
desc ) ( .

32
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

) ( deptno
) ( sal .

7-2 )( sql functions


-1

single row function

-2

multiple row function

-1 : single row function .


:

33
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

-1 :

lower

upper

initcap

concat

substr

length

instr

lpad

rpad

trim

ltrim

rtrim

-2 :

.
.
.

round
trunc
mod

34
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

-3 :

sysdate

months_between

add_months

next_day

last_day

round

trunc

new_time

-4 :

to_char

to_number

to_date

:
select . where

35
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

) lower -1 ( :

) upper -2 ( :

) initcap -3 ( :

- .

) concat -4 ( :

36
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

) substr -5 (:
.

) length -6 ( :

) instr -7 ( :

) lpad -8 ( :

8 ) ( maher

) . ( #

37
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

10 ) ( maher
) * ( .

) rpad -9 ( :

8 ) ( maher

) . ( #

) trim -10 ( :

) ( from

.
38
/

PDF created with pdfFactory Pro trial version www.pdffactory.com


) ( trim .

) rtrim -11 ( :

) . ( from

) ltrim -12 ( :

select . where

) round -1 ( :

39
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

. 0

) ( 1- .

) trunc -2 ( :

40
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

) mod -3 ( :

) ( sal ) . ( comm

) sysdate -1 ( :

) month_between -2 ( :

) add_months -3 ( :

41
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

) next_day -4 ( :

) last_day -5 ( :

) round -6 ( :

42
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

) trunc -7 ( :
.

) new_time -8 ( :

) (HST

) ( EST .

43
/

PDF created with pdfFactory Pro trial version www.pdffactory.com


-1 :
)'to_char(date ,'fmt

-2 :
)'to_char( number ,'fmt

-3 :
)'to date ( char ,'fmt

.
44
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

-4 :
)'to_number ( char ,'fmt
:

) nvl -1 ( :

) ( comm

45
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

) translate -2 ( :
:
)translate(string , if , then

) ( if

) ( then .

) ( if
) ( then .

decode -3

translate decode
:
)Decode ( value , if 1 , then 1 , if 2 , then 2 , if 3 , then 3 , else .

46
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

) (10 ) ( ten ) (20 ) ( twenty ) (30 ) ( thirty


) ( not found .

) ( not found ) ( else ) (40


.

) ascii -4 ( :

) ( M,R .

47
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

) chr -5 : ( ascii

) (77,82 . ascii

-2 ) ( : multiple row function


:

) ( .

sum

max,min,greatest,least . .
avg
) ( .
.

count

stddevdevttion

variance

select . where

:
) sum -1 ( :

. 10
48
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

) max -2 ( :

. 10

) min -3 ( :

. 10

) greatest -4 ( :

) z . ( a

49
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

) least -5 ( :

) a . ( z

) avg -6 ( :

. 10

) count -7 ( :

) ( count
) ( deptno ) . ( sal

50
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

- group by .

) ( deptno

) ( sum ) ( where
) . ( where

) ( having
) . ( group by

) : ( having .

51
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

) : ( select

10
8500 .

select ) ( from
) ( where ) ( group by ) ( having ) . ( order by

8-2

. select

: ) ( select .

- :

-1 . equal join

-2 . non-equal join
-3 . outer join

-4 . self join
-5 .

52
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

-1 equal join

.

) ( emp , dept

) ( where ) ( deptno
) . ( dept
:

53
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

-2 non-equal join
.


) . ( salgrade

-3 outer join
.

54
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

40

) ( dept ) (+ ) ( deptno .

-4 self join
.

) ( KING ) (.

-5

) . ( 1

.....

and or .

55
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

) . ( where

9-2 ) ( subquery
:

) : ( WARD

56
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

30 ) . ( ALLEN

- :

-1 ) . ( single row subquery


-2 ) . ( multiple row subquery

-3 ) . ( multiple column subquery


57
/

PDF created with pdfFactory Pro trial version www.pdffactory.com


- .

- .

: select
where having . from

-1 ) : ( single row subquery


) ><. ( <,>,<=,>=.

) . ( WARD

) . ( 7934
58
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

-2 ) : ( multiple-row subquery
) in , =any , <any ,
. ( exist , not exist, >any , =all , <all , >all

-3 ) : ( multiple-column subquery
.

59
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

) ( emp e
. m

) : ( union , intersect , minus


: .
: .
: .

union
intersect
minus

) . ( union

) . ( intersect
60
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

) ( minus
) ( emp ) . ( deptno

40 ) ( dept
) . ( emp

) ( union .

61
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

10 -2
-1 7566
.7369

-2
.30

-3
) . ( SALESMAN , CLERK

62
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

( sql & sql * plus )

:

Data Manipulating Language
( DML)

63
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

) : ( Data Manipulating language


:

-1 ) . ( insert

-2 ) . ( update
-3 ) . ( delete

1-3
) ( insert :

)insert into table name ( column1 , column2 , ..


; )values ( value1 , value2 , ..

:
. insert - .

. - .

- scott

) ( emp2
) : ( emp2

64
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

) . ( dept2

65
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

) (/
) (R :

66
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

). ( emp2

67
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

) ( : substitution variables


) ( sql ) ( sql

) & ( .

2-3
) ( update :
update table name set column1= value , column2= value
; where condition

) . ( MAHER

) ( loc . 50

68
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

8000 8001
8001 :

3-3
) ( delete :
delete from table_name
; where condition

) . ( rollback - .

- .

69
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

) . ( KING

truncate
delete
rollback . delete

70
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

4 -3
-1 : emp

, job = MANAGER , mgr = 7839, = empno = 7777 , ename


hiredate = 15-SEP-09 , sal = 5000 , comm=NULL, deptno = 30 .
-2 ) ( deptno . 20
-3 .

71
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

( sql & sql * plus )

:

Transection command

72
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

1-4 ) ( transaction command


) (
:

.
.

commit
Auto commit
rollback

. commit

. rollback

savepoint

: commit .

: auto commit .

- : rollback

commit
.

: savepoint . rollback
73
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

2 -4
.

74
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

( sql & sql * plus )

:

Data Definition Language

75
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

) : Data Definition Language ( DDL



) ( create ) ( alter ) ( drop .

1-5 ) ( tables
) : ( creating tables ) . ( create


number

date

char

varchar2

2 .
4 .

long
long row

4 clob-blob .
dfile
4 .

- 30 .

) . ( create ) ( # , $ , _ , , - .

- .

38 . 240 . 9 . - .

76
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

create
:
( create table table name
column1
data type
column2
data type

)column(n
;) data type
) ( emp :

) ( empno 4

) ( NOT NULL ) ( ename

15 ) ( job 15

) ( mgr 4 ) ( hiredate
) ( sal

) (5,2 ) ( comm

) ( 5,2
) ( deptno . 2

77
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

) ( emp :

) ( emp :

) ( emp 10 20 .

) ( emp
12 ) . ( annual_salary

78
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

- ) : ( altering table

alter
) ( alter :

-1 .
:
;))1- alter table_name add ( column_name type(size

) . ( dept2

-2 .
:
;))2- alter table_name modify ( column_name type(size

) ( deptno 2 . 3

79
/

PDF created with pdfFactory Pro trial version www.pdffactory.com


) ( .

-3 .
:

; 3- alter table_name drop column column_name

) ( ) . ( comm

-4 .
:
; 4- rename old_name to new_name

) ( emp2 ) . ( emp22

80
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

-5 .
:
; 5- drop table table_name

) ( ) . ( emp22

-6 .
:

; ' 6- comment on table table_name is ' statement

) . ( dept2

) . ( not null . - ) ( .

) ( . - .

-1 .

-2 ) ( oracle server :

- ) _ ( user .

) _ ( all .

- ) _ ( dba

81
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

) : ( constraints .

): ( types of constraints : ( not null ) -1 .


: ( unique ) -2 .

: ( primary key ) -3 )
( .

: ( foreign key ) -4 .
: ( check ) -5 .
: ( default ) -6 .

- :

) ( not null

) ( .

) ( not null ) . ( name

) ( unique

- :

) ( dname .
82
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

- :

) ( dname .

) (primary key

.
- :

) ( dname .

- :

) ( dname

83
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

) ( foreign key

) ( primary key
.

- :

) ( deptno ) . ( primary key

) ( foreign key ) ( deptno


) ( primary key ) ( dept8 .

84
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

- :

) ( deptno ) . ( primary key

) ( foreign key ) ( deptno


) ( primary key ) ( dept9 .

) ( check

.
- :

) ( deptno ) (5 ) (85

85
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

- :

) ( default
:

) ( default ) ( st_age . 25

- :

) ( alter :
alter table table_name
; add constraint constraint_name constraint_type

) ( primary key ) ( dept2 .

) ( foreign key ) ( emp .

86
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

) ( primary key

- .

: :
) ( .

cascade

disable

enable

) ( primary key ) . ( dept2

) ( foreign key ) . ( emp

) ( primary key ) ( .

87
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

) ( dept3 ) ( primary key


.

) ( dept3 ) . ( primary key

- :

) . ( primary key

) . ( foreign key

) ( check ) . ( not null

) . ( unique

) ( emp ) ( dept
) . ( user_constraints

2-5 ) ( views
) : ( creating views

.

88
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

create view view_name as select


; column1 , column2 , from table_name

) ( emp

. 2500

) : ( emp_view

) ( emp_view

89
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

) : ( canceling view ) ( drop :

3-5 ) ( sequences
) : ( creating sequences

) ( primary key .

increment by

Start with

maxvalue

.
.
.
.

cache
nocache
Cycle
nocycle

) ( empno ) ( emp 1

) ( 1 ) ( 50 .
90
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

) : ( altering sequences ) . ( alter

2 ) . (100

) : ( canceling sequences ) . ( drop

) ( emp2 ) . ( empno

4-5 ) ( indexes
) : ( creating indexes :
) . ( dba_indexes
:

create index index_name


; ) on table_name ( column1 , .

) ( dept3 ) . ( deptno
91
/

PDF created with pdfFactory Pro trial version www.pdffactory.com


- 16 .

- ) ( null .

- .

) : ( canceling indexes ) . ( drop

) ( dept3 ) . ( deptno

5-5 ) ( synonym
) : ( creating synonyms :
.

create synonym synonym_name


; for table_name

:
) . ( dept3

92
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

- ) : ( canceling synonyms

) . ( drop

:
) . ( dept3

6-5 ) ( users
- ) : ( creating user

; create user user_name identified by password


) ( system ) . ( sys

93
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

) : ( altering users ). ( the_fire_heart

7-5 ) ( system
: : connect -1


) ( connect
.

94
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

) : resource -2 (

: Dba -3
.

: ) . ( exp_full_database - ) . ( imp_full_database

- ) : ( creating roles

.
.

) ( maher
.

) ( maher .

) ( identified ) ( by :

95
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

: :
:
) ( maher :
.
:
:

96
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

8 -5
-1
-1 :

) ( s_no . ) ( name . - ) ( age .

) ( birth_date . - ) ( grade .

-2 ) ( grade ) . ( mark

-3 ) ( primary key ) . ( s_no

-4 ) ( check ) ( age ) . ( 38-18


-5 .

-2
-1 ) ( user ) . ( maher
-2 .

-3
-1 ) . ( stud
-2 .

-3 .
-4 .

-5 .
-6 .

97
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

( sql & sql * plus )



Data Control Language

98
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

) : Data Control Language ( DCL


) ( users ) . ( roles

1-6
) ( grant :
grant privilege1 , privilege2 , ..
; on table_name to user_name or role_name

:
) ( connect ) ( resource ) ( dba
) . ( the_fire_heart
) ( the_fire_heart

:
) ( create table ) . ( maher
:

-1 :

-2 ) ( maher :

99
/

PDF created with pdfFactory Pro trial version www.pdffactory.com


- .

) ( emp .

2 -6
) ( revoke :
revoke privilege1 , privilege2 , ..
; from table_name to user_name or role_name

) ( connect ) ( resource ) ( dba

) . ( the_fire_heart

) ( create table ) . ( maher

100
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

3 -6
-1 ) ( connect ) ( resource .
-2 ) ( create table ) ( alter table ) ( stud
.

-3 ) ( connect ) ( resource .

-4 ) ( create table ) ( alter table ) ( stud


.

101
/

PDF created with pdfFactory Pro trial version www.pdffactory.com


sql international 9i -1 . oracle

-2 oracle 8i / / .

oracle 9i -3 / / .
-4 ) oracle 9i ( sql & sql * plus & pl/sql / .

-5 ) ( .

102
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

103
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

:
:
1989 - 11 18 :
: -
-
:

104
/

PDF created with pdfFactory Pro trial version www.pdffactory.com

You might also like