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

1

INTRODUCTION
SQL is divided into the following Data Definition Language (DDL) Data Manipulation Language (DML) Data Ret ieval Language (DRL) T ansa!tion Cont ol Language (TCL) Data Cont ol Language (DCL)

DDL "" ! eate# alte # d op# t un!ate# ena$e DML "" inse t# update# delete DRL "" sele!t TCL "" !o$$it# oll%a!&# savepoint DCL "" g ant# evo&e CR'(T' T()L' S*NT(+ C eate ta%le ,table_name- (col1 datatype1, col2 datatype2 coln datatypen). '/0
SQL-

! eate ta%le student (no nu$%e (1)# na$e va !ha (23)# $a &s nu$%e (4)).

INS'RT This will %e used to inse t the e!o ds into ta%le5 6e have two $ethods to inse t5 a) )7 value $ethod )7 add ess $ethod

USIN8 9(LU' M'T:OD

S7nta/0 inse t into ,table_name) values (value1, value2, value3 . Valuen).

2
'/0
SQLSQL-

inse t into student values (2# ;sudha;# 233). inse t into student values (1# ;sað# 133).

To inse t a new e!o d again 7ou have to t7pe enti e inse t !o$$and# if the e a e lot of e!o ds this will %e diffi!ult5 This will %e avoided %7 using add ess $ethod5 %)

USIN8 (DDR'SS M'T:OD

S7nta/0 inse t into ,table_name) values (&col1, &col2, &col3 . &coln). This will p o$pt 7ou fo the values %ut fo eve 7 inse t 7ou have to use fo wa d slash5 '/0
SQL-

inse t into student values (<no# =<na$e=# <$a &s).

'nte value fo no0 2 'nte value fo na$e0 >agan 'nte value fo $a &s0 433 old new 20 inse t into student values(<no# =<na$e=# <$a &s) 20 inse t into student values(2# =>agan=# 433) ?

SQL-

'nte value fo no0 1 'nte value fo na$e0 Na en 'nte value fo $a &s0 @33 old new !) 20 inse t into student values(<no# =<na$e=# <$a &s) 20 inse t into student values(1# =Na en=# @33)

INS'RTIN8 D(T( INTO SA'CIBI'D COLUMNS USIN8 9(LU' M'T:OD

S7nta/0 inse t into ,table_name)(col1, col2, col3 Coln) values (value1, value2, value3 .

3
Valuen). '/0
SQLSQL-

inse t into student (no# na$e) values (4# ;Ra$esh;). inse t into student (no# na$e) values (@# ;Madhu;).

d)

INS'RTIN8 D(T( INTO SA'CIBI'D COLUMNS USIN8 (DDR'SS M'T:OD

S7nta/0 inse t into ,table_name)(col1, col2, col3 coln) values (&col1, &col2 .&coln); This will p o$pt 7ou fo the values %ut fo eve 7 inse t 7ou have to use fo wa d slash5 '/0
SQL-

inse t into student (no# na$e) values (<no# =<na$e=).

'nte value fo no0 C 'nte value fo na$e0 9isu old new 20 inse t into student (no# na$e) values(<no# =<na$e=) 20 inse t into student (no# na$e) values(C# =9isu=) ?

SQL-

'nte value fo no0 D 'nte value fo na$e0 Rattu old new 20 inse t into student (no# na$e) values(<no# =<na$e=) 20 inse t into student (no# na$e) values(D# =Rattu=)

S'L'CTIN8 D(T( S7nta/0 Sele!t E f o$ ,table_name-. o Sele!t col1, col2, coln f o$ ,table_name-. '/0
SQL-

"" he e E indi!ates all !olu$ns

sele!t E f o$ student. NO N(M' M(RFS

4
""" """""" 2 1 2 1 4 @ C D Sudha Sa&eth >agan Na en Ra$esh Madhu 9isu Rattu """""""" 233 133 433 @33

SQL-

sele!t no# na$e# $a &s f o$ student. NO N(M' """ """""" 2 1 2 1 4 @ C D Sudha Sa&eth >agan Na en Ra$esh Madhu 9isu Rattu M(RFS """""""" 233 133 433 @33

SQL-

sele!t no# na$e f o$ student. NO N(M' """ """"""" 2 1 2 1 4 @ C D Sudha Sa&eth >agan Na en Ra$esh Madhu 9isu Rattu

CONDITION(L S'L'CTIONS (ND OA'R(TORS


6e have two !lauses used in this 6he e O de %7

USIN8 6:'R' S7nta/0 sele!t E f o$ ,table_name- whe e ,condition-. the following a e the diffe ent t7pes of ope ato s used in whe e !lause5 ( ith$eti! ope ato s Co$pa ison ope ato s Logi!al ope ato s ( ith$eti! ope ato s G# "# E# ? Co$pa ison ope ato s a) H# IH# -# ,# -H# ,H# ,%etween# not %etween in# not in null# not null li&e Logi!al ope ato s (nd O not "" lowest p e!eden!e "" highest p e!eden!e

USIN8

H# -# ,# -H# ,H# IH# ,-

'/0
SQL-

sele!t E f o$ student whe e no H 1.

NO N(M' """ """"""" 1 1 Sa&eth Na en

M(RFS """"""""" 133 @33

SQL-

sele!t E f o$ student whe e no , 1. NO N(M' """ """"""" 2 2 Sudha >agan M(RFS """""""""" 233 433

SQL-

sele!t E f o$ student whe e no - 1. NO N(M' """ """"""" 4 @ C D Ra$esh Madhu 9isu Rattu M(RFS """"""""""

SQL-

sele!t E f o$ student whe e no ,H 1. NO N(M' """ """"""" 2 1 2 1 Sudha Sa&eth >agan Na en M(RFS """""""""" 233 133 433 @33

SQL-

sele!t E f o$ student whe e no -H 1. NO N(M' """ """"""" M(RFS """""""""

7
1 1 4 @ C D Sa&eth Na en Ra$esh Madhu 9isu Rattu 133 @33

SQL-

sele!t E f o$ student whe e no IH 1. NO N(M' """ """"""" 2 2 4 @ C D Sudha >agan Ra$esh Madhu 9isu Rattu M(RFS """""""""" 233 433

SQL-

sele!t E f o$ student whe e no ,- 1. NO N(M' """ """"""" 2 2 4 @ C D Sudha >agan Ra$esh Madhu 9isu Rattu M(RFS """""""""" 233 433

%)

USIN8 (ND

This will gives the output when all the !onditions %e!o$e t ue5 S7nta/0 sele!t E f o$ ,table_name- whe e ,condition1- and ,condition2- and 55

8
,conditionn-. '/0
SQL-

sele!t E f o$ student whe e no H 1 and $a &s -H 133. NO N(M' """ """"""" 1 1 Sa&eth Na en M(RFS """""""" 133 @33

!)

USIN8 OR

This will gives the output when eithe of the !onditions %e!o$e t ue5 S7nta/0 sele!t E f o$ ,table_name- whe e ,condition1- and ,condition2- o 55 ,conditionn-. '/0
SQL-

sele!t E f o$ student whe e no H 1 o $a &s -H 133. NO N(M' """ """"""" 1 2 1 Sa&eth >agan Na en M(RFS """"""""" 133 433 @33

d)

USIN8 )'T6''N

This will gives the output %ased on the !olu$n and its lowe %ound# uppe %ound5 S7nta/0 sele!t E f o$ ,table_name- whe e ,col- %etween ,lower bound- and ,upper bound-. '/0
SQL-

sele!t E f o$ student whe e $a &s %etween 133 and @33.

NO N(M' """ """"""" 1 2 1 e) Sa&eth >agan Na en

M(RFS """"""""" 133 433 @33

USIN8 NOT )'T6''N

This will gives the output %ased on the !olu$n whi!h values a e not in its lowe %ound# uppe %ound5 S7nta/0 sele!t E f o$ ,table_name- whe e ,col- not %etween ,lower bound- and ,upper bound-. '/0
SQL-

sele!t E f o$ student whe e $a &s not %etween 133 and @33. NO N(M' """ """"""" 2 Sudha M(RFS """"""""" 233

f)

USIN8 IN

This will gives the output %ased on the !olu$n and its list of values spe!ified5 S7nta/0 sele!t E f o$ ,table_name- whe e ,col- in ( value1, value2, value3 valuen). '/0
SQL-

sele!t E f o$ student whe e no in (2# 1# 4). NO N(M' """ """"""" 2 Sudha M(RFS """"""""" 233

10
1 2 1 4 g) Sa&eth >agan Na en Ra$esh 133 433 @33

USIN8 NOT IN

This will gives the output %ased on the !olu$n whi!h values a e not in the list of values spe!ified5 S7nta/0 sele!t E f o$ ,table_name- whe e ,col- not in ( value1, value2, value3 valuen). '/0
SQL-

sele!t E f o$ student whe e no not in (2# 1# 4). NO N(M' """ """"""" @ C D Madhu 9isu Rattu M(RFS """""""""

h)

USIN8 NULL

This will gives the output %ased on the null values in the spe!ified !olu$n5 S7nta/0 sele!t E f o$ ,table_name- whe e ,col- is null. '/0
SQL-

sele!t E f o$ student whe e $a &s is null. NO N(M' """ """"""" 4 Ra$esh M(RFS """""""""

11
@ C D i) Madhu 9isu Rattu

USIN8 NOT NULL

This will gives the output %ased on the not null values in the spe!ified !olu$n5 S7nta/0 sele!t E f o$ ,table_name- whe e ,col- is not null. '/0
SQL-

sele!t E f o$ student whe e $a &s is not null. NO N(M' """ """"""" 2 1 2 1 Sudha Sa&eth >agan Na en M(RFS """"""""" 233 133 433 @33

J)

USIN8 LIF'

This will %e used to sea !h th ough the ows of data%ase !olu$n %ased on the patte n 7ou spe!if75 S7nta/0 sele!t E f o$ ,table_name- whe e ,col- li&e ,pattern-. '/0 i) This will give the ows whose $a &s a e 2335 sele!t E f o$ student whe e $a &s li&e 233. NO N(M' """ """"""" M(RFS """""""""

SQL-

12
2 Sudha 233

ii) This will give the ows whose na$e sta t with KS;5 sele!t E f o$ student whe e na$e li&e =SL=. NO N(M' """ """"""" 2 1 Sudha Sa&eth M(RFS """"""""" 233 133

SQL-

iii) This will give the ows whose na$e ends with Kh;5 sele!t E f o$ student whe e na$e li&e =Lh=. NO N(M' """ """"""" 1 4 Sa&eth Ra$esh M(RFS """"""""" 133

SQL-

i9) This will give the ows whose na$e;s se!ond lette sta t with Ka;5 sele!t E f o$ student whe e na$e li&e =MaL=. NO N(M' """ """"""" 1 2 1 4 @ D Sa&eth >agan Na en Ra$esh Madhu Rattu M(RFS """""""" 133 433 @33

SQL-

9) This will give the ows whose na$e;s thi d lette sta t with Kd;5 sele!t E f o$ student whe e na$e li&e =MMdL=.

SQL-

13

NO N(M' """ """"""" 2 @ Sudha Madhu

M(RFS """"""""" 233

9i) This will give the ows whose na$e;s se!ond lette sta t with Kt; f o$ ending5 sele!t E f o$ student whe e na$e li&e =LMtL=. NO N(M' """ """"""" 1 D Sa&eth Rattu M(RFS """"""""" 133

SQL-

9ii) This will give the ows whose na$e;s thi d lette sta t with Ke; f o$ ending5 sele!t E f o$ student whe e na$e li&e =LeMML=. NO N(M' """ """"""" 1 4 Sa&eth Ra$esh M(RFS """"""""" 133

SQL-

9iii) This will give the ows whose na$e !otains 1 a;s5 sele!t E f o$ student whe e na$e li&e =LaL a L=. NO N(M' """ """"""" 2 >agan M(RFS """""""""" 433

SQL-

E *ou have to spe!if7 the patte ns in li e using unde s!o e ( M )5

14

USIN8 ORD'R )* This will %e used to o de ing the !olu$ns data (as!ending o des!ending)5 S7nta/0 Sele!t E f o$ ,table_name- o de %7 ,col- des!. )7 default o a!le will use as!ending o de 5 If 7ou want output in des!ending o de 7ou have to use de!c &e7wo d afte the !olu$n5 '/0
SQL-

sele!t E f o$ student o de %7 no. NO N(M' """ """"""" 2 2 1 1 4 @ C D Sudha >agan Sa&eth Na en Ra$esh Madhu 9isu Rattu M(RFS """"""""" 233 433 133 @33

SQL-

sele!t E f o$ student o de %7 no des!. NO N(M' """ """"""" D Rattu C 9isu @ Madhu 4 Ra$esh 1 Sa&eth 1 Na en 2 Sudha 133 @33 233 M(RFS """""""""

15
2 >agan 433

USIN8 DML
USIN8 UAD(T' This !an %e used to $odif7 the ta%le data5 S7nta/0 Update ,table_name- set ,col1- H value2# ,col2- H value1 whe e ,condition-. '/0
SQL-

update student set $a &s H C33.

If 7ou a e not spe!if7ing an7 !ondition this will update enti e ta%le5 update student set $a &s H C33 whe e no H 1. update student set $a &s H C33# na$e H =9enu= whe e no H 2.

SQLSQL-

USIN8 D'L'T' This !an %e used to delete the ta%le data te$po a il75 S7nta/0 Delete ,table_name- whe e ,condition-. '/0
SQL-

delete student.

If 7ou a e not spe!if7ing an7 !ondition this will delete enti e ta%le5 delete student whe e no H 1.

SQL-

16

USIN8 DDL
USIN8 (LT'R This !an %e used to add o a) e$ove !olu$ns and to $odif7 the p e!ision of the datat7pe5

(DDIN8 COLUMN

S7nta/0 alte ta%le ,table_name- add ,col datatype-. '/0


SQL-

alte ta%le student add sdo% date.

%)

R'MO9IN8 COLUMN

S7nta/0 alte ta%le ,table_name- d op ,col datatype-. '/0


SQL-

alte ta%le student d op !olu$n sdo%.

!)

INCR'(SIN8 OR D'CR'(SIN8 AR'CISION OB ( COLUMN

S7nta/0 alte ta%le ,table_name- $odif7 ,col datatype-. '/0


SQL-

alte ta%le student $odif7 $a &s nu$%e (C).

E To de! ease p e!ision the !olu$n should %e e$pt75 d)

M(FIN8 COLUMN UNUS'D

S7nta/0 alte ta%le ,table_name- set unused !olu$n ,col-.

17
'/0
SQL-

alte ta%le student set unused !olu$n $a &s.

'ven though the !olu$n is unused still it will o!!up7 $e$o 75 d)

DROAAIN8 UNUS'D COLUMNS

S7nta/0 alte ta%le ,table_name- d op unused !olu$ns. '/0


SQL-

alte ta%le student d op unused !olu$ns.

E *ou !an not d op individual unused !olu$ns of a ta%le5 e)

R'N(MIN8 COLUMN

S7nta/0 alte ta%le ,table_name- ena$e !olu$n ,old_col_name- to ,new_col_name-. '/0


SQL-

alte ta%le student ena$e !olu$n $a &s to s$a &s.

USIN8 TRUNC(T' This !an %e used to delete the enti e ta%le data pe $anentl75 S7nta/0 t un!ate ta%le ,table_name-. '/0
SQL-

t un!ate ta%le student.

USIN8 DROA This will %e used to d op the data%ase o%Je!t.

18
S7nta/0 D op ta%le ,table_name-. '/0
SQL-

d op ta%le student.

USIN8 R'N(M' This will %e used to ena$e the data%ase o%Je!t. S7nta/0 ena$e ,oldMtable_name- to ,new_table_name-. '/0
SQL-

ena$e student to stud.

19

USIN8 TCL
USIN8 COMMIT This will %e used to save the wo &5 Co$$it is of two t7pes5 a) I$pli!it '/pli!it

IMALICIT

This will %e issued %7 o a!le inte nall7 in two situations5 %) 6hen an7 DDL ope ation is pe fo $ed5 6hen 7ou a e e/iting f o$ SQL E ALUS5

'+ALICIT

This will %e issued %7 the use 5 S7nta/0 Co$$it o !o$$it wo &. E 6hen eve 7ou !o$$itted then the t ansa!tion was !o$pleted5 USIN8 ROLL)(CF This will undo the ope ation5 This will %e applied in two $ethods5 S7nta/0 Roll o O Roll%a!& o oll%a!& wo &. oll wo &. Upto p evious !o$$it Upto p evious oll%a!&

20
E 6hile p o!ess is going on# if suddenl7 powe goes then o a!le will oll%a!& the t ansa!tion5 USIN8 S(9'AOINT *ou !an use savepoints to oll%a!& po tions of 7ou !u ent set of t ansa!tions5 S7nta/0 Savepoint ,!avepoint_name-. '/0
SQLSQLSQLSQLSQLSQLSQLSQL-

savepoint s2. inse t into student values(2# Ka;# 233). savepoint s1. inse t into student values(1# K%;# 133). savepoint s4. inse t into student values(4# K!;# 433). savepoint s@. inse t into student values(@# Kd;# @33).

)efo e oll%a!& sele!t E f o$ student. NO N(M' """ """"""" 2 1 4 @ a % ! d M(RFS """""""""" 233 133 433 @33

SQL-

SQL-

oll%a!& to savepoint s4. O oll%a!& to s4.

SQL-

This will oll%a!& last two e!o ds5

21
SQL-

sele!t E f o$ student. NO N(M' """ """"""" 2 1 a % M(RFS """""""""" 233 133

22

USIN8 DCL
DCL !o$$ands a e used to g anting and evo&ing the pe $issions5 USIN8 8R(NT This is used to g ant the p ivileges to othe use s5 S7nta/0 8 ant ,privile"e!- on ,ob#ect_name- to ,u!er_name- Nwith g ant optionO. '/0
SQLSQLSQL-

g ant sele!t on student to sudha. g ant all on student to sudha.

"" 7ou !an give individual p ivilege "" 7ou !an give set of p ivileges "" 7ou !an give all p ivileges

g ant sele!t# inse t on student to sudha.

The sudha use has to use dot $ethod to a!!ess the o%Je!t5
SQL-

sele!t E f o$ sa&eth5student.

The sudha use !an not g ant pe $ission on student ta%le to othe use s5 To get this t7pe of option use the following5
SQL-

g ant all on student to sudha with g ant option.

Now sudha use also g ant pe $issions on student ta%le5 USIN8 R'9OF' This is used to evo&e the p ivileges f o$ the use s to whi!h 7ou g anted the p ivileges5 S7nta/0 Revo&e ,privile"e!- on ,ob#ect_name- f o$ ,u!er_name-. '/0
SQLSQLSQL-

evo&e sele!t on student fo $ sudha. evo&e all on student f o$ sudha.

"" 7ou !an evo&e individual p ivilege "" 7ou !an evo&e set of p ivileges

evo&e sele!t# inse t on student f o$ sudha.

"" 7ou !an evo&e all p ivileges

23

USIN8 (LI(S'S
CR'(T' 6IT: S'L'CT 6e !an ! eate a ta%le using e/isting ta%le Nalong with dataO5 S7nta/0 C eate ta%le ,new_table_name- Ncol1, col2, col3 ... colnO as sele!t E f o$ ,old_table_name-. '/0
SQL-

! eate ta%le student2 as sele!t E f o$ student.

C eating ta%le with 7ou own !olu$n na$es5


SQL-

! eate ta%le student1(sno# sna$e# s$a &s) as sele!t E f o$ student.

C eating ta%le with spe!ified !olu$ns5


SQL-

! eate ta%le student4 as sele!t no#na$e f o$ student.

C eating ta%le with out ta%le data5


SQL-

! eate ta%le student1(sno# sna$e# s$a &s) as sele!t E f o$ student whe e 2 H 1.

In the a%ove whe e !lause give an7 !ondition whi!h does not satisf75 INS'RT 6IT: S'L'CT Using this we !an inse t e/isting ta%le data to a anothe ta%le in a single t ip5 )ut the ta%le st u!tu e should %e sa$e5 S7nta/0 Inse t into ,table1- sele!t E f o$ ,table2-. '/0
SQL-

inse t into student2 sele!t E f o$ student.

Inse ting data into spe!ified !olu$ns


SQL-

inse t into student2(no# na$e) sele!t no# na$e f o$ student.

24
COLUMN (LI(S'S S7nta/0 $elect %or"inal_col& %alia!_name& 'rom %tableMna$e-. '/0
SQL-

sele!t no sno f o$ student. o sele!t no PsnoQ f o$ student.

SQL-

T()L' (LI(S'S If 7ou a e using ta%le aliases 7ou !an use dot $ethod to the !olu$ns5 S7nta/0 Sele!t ,alia!_name-5,col1-# ,alia!_name-5,col2- R ,alia!_name-5,coln- f o$ ,table_name- ,alia!_name-. '/0
SQL-

sele!t s5no# s5na$e f o$ student s.

25

USIN8 M'R8'
M'R8' *ou !an use $e ge !o$$and to pe fo $ inse t and update in a single !o$$and5 '/0
SQL-

Me ge into student2 s2 Using (sele!t EB o$ student1) s1 On(s25noHs15no) 6hen $at!hed then Update set $a &s H s15$a &s 6hen not $at!hed then Inse t (s25no#s25na$e#s25$a &s) 9alues(s15no#s15na$e#s15$a &s).

In the a%ove the two ta%les a e with the sa$e st u!tu e %ut we !an $e ge diffe ent st u!tu ed ta%les also %ut the datat7pe of the !olu$ns should $at!h5 (ssu$e that student2 has !olu$ns li&e no#na$e#$a &s and student1 has !olu$ns li&e no# na$e# hno# !it75 Me ge into student2 s2 Using (sele!t EB o$ student1) s1 On(s25noHs15no) 6hen $at!hed then Update set $a &s H s15hno 6hen not $at!hed then Inse t (s25no#s25na$e#s25$a &s) 9alues(s15no#s15na$e#s15hno).

SQL-

26

MULTI)L' INS'RTS
6e have ta%le !alled D'AT with the following !olu$ns and data D'ATNO """""""" 23 13 43 @3 a) DN(M' """""""" a!!ounting esea !h sales ope ations LOC """" new 7o & dallas Chi!ago %oston

CR'(T' STUD'NT T()L'

SQL-

C eate ta%le student(no nu$%e (1)#na$e va !ha (1)#$a &s nu$%e (4)).

%)

MULTI INS'RT 6IT: (LL BI'LDS

SQL-

Inse t all Into student values(2#;a;#233) Into student values(1#;%;#133) Into student values(4#;!;#433) Sele!t Ef o$ dept whe e deptnoH23.

"" This inse ts 4 ows !)

MULTI INS'RT 6IT: SA'CIBI'D BI'LDS

SQL-

inse t all Into student (no#na$e) values(@#;d;) Into student(na$e#$a &s) values(;e;#@33) Into student values(4#;!;#433) Sele!t Ef o$ dept whe e deptnoH23.

"" This inse ts 4 ows

27
d)
MULTI INS'RT 6IT: DUALIC(T' RO6S

SQL-

inse t all Into student values(2#;a;#233) Into student values(1#;%;#133) Into student values(4#;!;#433) Sele!t Ef o$ dept whe e deptno - 23.

"" This inse ts S ows %e!ause in the sele!t state$ent et ieves 4 e!o ds (4 inse ts fo ea!h ow et ieved) e)

MULTI INS'RT 6IT: CONDITIONS )(S'D

SQL-

Inse t all 6hen deptno - 23 then Into student2 values(2#;a;#233) 6hen dna$e H KS(L'S; then Into student1 values(1#;%;#133) 6hen lo! H KN'6 *ORF; then Into student4 values(4#;!;#433) Sele!t Ef o$ dept whe e deptno-23.

"" This inse ts @ ows %e!ause the fi st !ondition satisfied 4 ti$es# se!ond !ondition satisfied on!e and the last none5 f)

MULTI INS'RT 6IT: CONDITIONS )(S'D (ND 'LS'

SQL-

Inse t all 6hen deptno - 233 then Into student2 values(2#;a;#233) 6hen dna$e H KS; then Into student1 values(1#;%;#133) 6hen lo! H KN'6 *ORF; then Into student4 values(4#;!;#433) 'lse Into student values(@#;d;#@33)

28
Sele!t Ef o$ dept whe e deptno-23. "" This inse ts 4 e!o ds %e!ause the else satisfied 4 ti$es g)

MULTI INS'RT 6IT: CONDITIONS )(S'D (ND BIRST

SQL-

Inse t fi st 6hen deptno H 13 then Into student2 values(2#;a;#233) 6hen dna$e H KR'S'(RC:; then Into student1 values(1#;%;#133) 6hen lo! H KN'6 *ORF; then Into student4 values(4#;!;#433) Sele!t Ef o$ dept whe e deptnoH13.

"" This inse ts 2 e!o d %e!ause the fi st !lause avoid to !he!& the e$aining !onditions on!e the !ondition is satisfied5 h)

MULTI INS'RT 6IT: CONDITIONS )(S'D# BIRST (ND 'LS'

SQL-

Inse t fi st 6hen deptno H 43 then Into student2 values(2#;a;#233) 6hen dna$e H KR; then Into student1 values(1#;%;#133) 6hen lo! H KN'6 *ORF; then Into student4 values(4#;!;#433) 'lse Into student values(@#;d;#@33) Sele!t Ef o$ dept whe e deptnoH13.

"" This inse ts 2 e!o d %e!ause the else !lause satisfied on!e

29
i)
MULTI INS'RT 6IT: MULTI)L' T()L'S

SQL-

Inse t all Into student2 values(2#;a;#233) Into student1 values(1#;%;#133) Into student4 values(4#;!;#433) Sele!t Ef o$ dept whe e deptnoH23.

"" This inse ts 4 ows EE *ou !an use $ulti ta%les with spe!ified fields# with dupli!ate ows# with !onditions# with fi st and else !lauses5

30

BUNCTIONS
Bun!tions !an %e !atego iTed as follows5 Single ow fun!tions 8 oup fun!tions

SIN8L' RO6 BUNCTIONS Single ow fun!tions !an %e !atego iTed into five5 These will %e applied fo ea!h ow and p odu!es individual output fo ea!h ow5 Nu$e i! fun!tions St ing fun!tions Date fun!tions Mis!ellaneous fun!tions Conve sion fun!tions

NUM'RIC BUNCTIONS

(%s Sign SU t Mod Nvl Aowe '/p Ln Log Ceil Bloo Round T un& )itand 8 eatest Least

31
a)
()S

Coales!e

(%solute value is the $easu e of the $agnitude of value5 (%solute value is alwa7s a positive nu$%e 5 S7nta/0 a%s (value) '/0
SQL-

sele!t a%s(C)# a%s("C)# a%s(3)# a%s(null) f o$ dual. ()S(C) C ()S("C) C ()S(3) ()S(NULL) """""""""" """"""""""""" 3

"""""""""" """"""""""

%)

SI8N

Sign gives the sign of a value5 S7nta/0 sign (value) '/0


SQL-

sele!t sign(C)# sign("C)# sign(3)# sign(null) f o$ dual. SI8N(C) """""""""" 2 SI8N("C) """""""""" "2 SI8N(3) SI8N(NULL) """""""""" """""""""""""" 3

!)

SQRT

This will give the sUua e oot of the given value5 S7nta/0 sU t (value) '/0 "" he e value $ust %e positive5

32
SQL-

sele!t sU t(@)# sU t(3)# sU t(null)# sU t(2) f o$ dual. SQRT(@) """""""""" 1 SQRT(3) SQRT(NULL) """""""""" """"""""""""""" 3 SQRT(2) """""""""" 2

d)

MOD

This will give the e$ainde 5 S7nta/0 $od (value, divi!or) '/0
SQL-

sele!t $od(V#@)# $od(2#C)# $od(null#null)# $od(3#3)# $od("V#@) f o$ dual. MOD(V#@) """""""""""" 4 MOD(2#C) MOD(NULL#NULL) """""""""" """"""""""""""""""""" 2 MOD(3#3) MOD("V#@) """"""""""" """"""""""""" 3 "4

e)

N9L

This will su%stitutes the spe!ified value in the pla!e of null values5 S7nta/0 nvl (null_col, replacement_value) '/0
SQL-

sele!t E f o$ student. NO N(M' """ """"""" 2 1 4 a % ! M(RFS """"""""" 233 133

"" he e fo 4 d ow $a &s value is null

SQL-

sele!t no# na$e# nvl($a &s#433) f o$ student. NO N(M' N9L(M(RFS#433)

33
""" """"""" """"""""""""""""""""" 2 1 4 a % ! 233 133 433

SQL-

sele!t nvl(2#1)# nvl(1#4)# nvl(@#4)# nvl(C#@) f o$ dual. N9L(2#1) """""""""" 2 N9L(1#4) """""""""" 1 N9L(@#4) """""""""" @ N9L(C#@) """""""""" C

SQL-

sele!t nvl(3#3)# nvl(2#2)# nvl(null#null)# nvl(@#@) f o$ dual. N9L(3#3) """""""""" 3 N9L(2#2) N9L(null#null) N9L(@#@) """""""""" """"""""""""""""" 2 """""""""" @

f)

AO6'R

Aowe is the a%ilit7 to aise a value to a given e/ponent5 S7nta/0 powe (value, e(ponent) '/0
SQL-

sele!t powe (1#C)# powe (3#3)# powe (2#2)# powe (null#null)# powe (1#"C) f o$ dual. AO6'R(1#C) AO6'R(3#3) AO6'R(2#2) AO6'R(NULL#NULL) AO6'R(1#"C) """""""""""""" """""""""""""" """"" """"""""" """"""""""""""""""""""" """"""""""""""" 41 2 2 53421C

g)

'+A

This will aise e value to the give powe 5

34
S7nta/0 e/p (value) '/0
SQL-

sele!t e/p(2)# e/p(1)# e/p(3)# e/p(null)# e/p("1) f o$ dual. '+A(2) """""""" '+A(1) """"""""" '+A(3) '+A(NULL) """""""" """"""""""""" 2 '+A("1) """""""""" 524C44C1W4

15V2W1W2W4 V54WS3CD2 h)

LN

This is %ased on natu al o %ase e loga ith$5 S7nta/0 ln (value) '/0


SQL-

"" he e value $ust %e g eate than Te o whi!h is positive onl75

sele!t ln(2)# ln(1)# ln(null) f o$ dual. LN(2) """"""" 3 LN(1) """"""" LN(NULL) """"""""""""

5DS42@V2W2

Ln and '/p a e e!ip o!al to ea!h othe 5


'+A LN

(4) H 1353WCC4DS

(1353WCC4DS) H 4

i)

LO8

This is %ased on 23 %ased loga ith$5 S7nta/0 log (23# value) "" he e value $ust %e g eate than Te o whi!h is positive onl75 '/0
SQL-

sele!t log(23#233)# log(23#1)# log(23#2)# log(23#null) f o$ dual. LO8(23#233) LO8(23#1) LO8(23#2) LO8(23#NULL)

35
""""""""""""""" 1 (value) H """"""""""" 543231SSSD ('+A(2)# value) """""""""""" """"""""""""""""" 3

LN

LO8

SQL-

sele!t ln(4)# log(e/p(2)#4) f o$ dual. LN(4) """"""" LO8('+A(2)#4) """"""""""""""""" 253SWD211S

253SWD211S J)

C'IL

This will p odu!e a whole nu$%e that is g eate than o eUual to the spe!ified value5 S7nta/0 !eil (value) '/0
SQL-

sele!t !eil(C)# !eil(C52)# !eil("C)# !eil( "C52)# !eil(3)# !eil(null) f o$ dual. C'IL(C) C'IL(C52) """"""""" """"""""""" C D C'IL("C) C'IL("C52) """""""""" """""""""""" "C "C C'IL(3) C'IL(NULL) """""""" """""""""""""" 3

&)

BLOOR

This will p odu!e a whole nu$%e that is less than o eUual to the spe!ified value5 S7nta/0 floo (value) '/0
SQL-

sele!t floo (C)# floo (C52)# floo ("C)# floo ( "C52)# floo (3)# floo (null) f o$ dual. BLOOR(C) BLOOR(C52) BLOOR("C) BLOOR("C52) """"""""""" """"""""""""" """""""""""" """""""""""""" BLOOR(3) BLOOR(NULL) """"""""""" """"""""""""""""

36
C l)
ROUND

"C

"D

This will ounds nu$%e s to a given nu$%e of digits of p e!ision5 S7nta/0 ound (value, preci!ion) '/0
SQL-

sele!t ound(214514@C)# ound(214514@C#1)# ound(214514C@#1) f o$ dual.

ROUND(214514@C) ROUND(214514@C#3) ROUND(214514@C#1) ROUND(214514C@#1) """"""""""""""""""""" 214


SQL-

"""""""""""""""""""""""" """"""""""""""""""""""" """"""""""""""""""""""" 214 214514 21451@

sele!t ound(214514@C#"2)# ound(214514@C#"1)# ound(214514@C#"4)# ound(214514@C#"@) f o$ dual.

ROUND(214514@C#"2) ROUND(214514@C#"1) ROUND(214514@C#"4) ROUND(214514@C#"@) """""""""""""""""""""""" """"""""""""""""""""""""" """""""""""""""""""""""" 213 233 3 """""""""""""""""""""""" 3

SQL-

sele!t ound(214#3)# ound(214#2)# ound(214#1) f o$ dual. ROUND(214#3) ROUND(214#2) ROUND(214#1) """"""""""""""""" """"""""""""""""" """""""""""""""" 214 214 214

SQL-

sele!t ound("214#3)# ound("214#2)# ound("214#1) f o$ dual. ROUND("214#3) ROUND("214#2) ROUND("214#1) """""""""""""""""" """"""""""""""""" "214 "214 """"""""""""""""""" "214

SQL-

sele!t ound(214#"2)# ound(214#"1)# ound(214#"4)# ound("214#"2)# ound( "214#"1)# ound("214#"4) f o$ dual.

37

ROUND(214#"2) ROUND(214#"1) ROUND(214#"4) ROUND("214#"2) ROUND("214#"1) ROUND("214#"4) """"""""""""" """"""""""""" """"""""""""" """""""""""""" """""""""""""" """""""""""""""""""""""""" 213 233 3 "213 "233 3

SQL- sele!t ound(null#null)# ound(3#3)# ound(2#2)# ound("2#"2)# ound("1#"1) f o$ dual. ROUND(NULL#NULL) ROUND(3#3) ROUND(2#2) ROUND("2#"2) ROUND("1#"1) """"""""""""""""""""""" """""""""""""" """""""""""""" """""""""""""""" """""""""""""""" 3 $) 2 3 3

TRUNC

This will t un!ates o !hops off digits of p e!ision f o$ a nu$%e 5 S7nta/0 t un! (value, preci!ion) '/0
SQL-

sele!t t un!(214514@C)# t un!(214514@C#1)# t un!(214514C@#1) f o$ dual. TRUNC(214514@C) TRUNC(214514@C#1) TRUNC(214514C@#1) """"""""""""""""""""" """"""""""""""""""""""" """"""""""""""""""""""" 214 214514 214514

SQL-

sele!t t un!(214514@C#"2)# t un!(214514@C#"1)# t un!(214514@C#"4)# t un!(214514@C#"@) f o$ dual.

TRUNC(214514@C#"2) TRUNC(214514@C#"1) TRUNC(214514@C#"4) TRUNC(214514@C#"@) """""""""""""""""""""""" """""""""""""""""""""""" 213 233 """"""""""""""""""""""" """""""""""""""""""""""" 3 3

SQL-

sele!t t un!(214#3)# t un!(214#2)# t un!(214#1) f o$ dual.

38

TRUNC(214#3) TRUNC(214#2) TRUNC(214#1) """""""""""""""" 214 """""""""""""""" """"""""""""""""" 214 214

SQL-

sele!t t un!("214#3)# t un!("214#2)# t un!("214#1) f o$ dual. TRUNC("214#3) TRUNC("214#2) TRUNC("214#1) """"""""""""""""" "214 """"""""""""""""" """"""""""""""""" "214 "214

SQL-

sele!t t un!(214#"2)# t un!(214#"1)# t un!(214#"4)# t un!("214#"2)# t un!( "214#1)# t un!("214#"4) f o$ dual.

TRUNC(214#"2) TRUNC(214#"1) TRUNC(214#"4) TRUNC("214#"2) TRUNC("214#1) TRUNC(" 214#"4) """"""""""""" """"""""""""" """"""""""""" """""""""""""" """"""""""""" """"""""""""""""""""""""""""""""" 213 233 3 "213 "214 3

SQL-

sele!t t un!(null#null)# t un!(3#3)# t un!(2#2)# t un!("2#"2)# t un!("1#"1) f o$ dual. TRUNC(NULL#NULL) TRUNC(3#3) TRUNC(2#2) TRUNC("2#"2) TRUNC("1#"1) """"""""""""""""""""""" """"""""""""" """"""""""""" """"""""""""""" """""""""""""""" 3 2 3 3

n)

)IT(ND

This will pe fo $ %itwise and ope ation5 S7nta/0 %itand (value1, value2) '/0
SQL-

sele!t %itand(1#4)# %itand(3#3)# %itand(2#2)# %itand(null#null)# %itand("1#"4)

39
f o$ dual. )IT(ND(1#4) )IT(ND(3#3) )IT(ND(2#2) )IT(ND(NULL#NULL) )IT(ND("1#"4) """""""""""""" """"""""""""""" """""""""""""" 1 o) 3 2 """""""""""""""""""""""" """"""""""""""""" "@

8R'(T'ST

This will give the g eatest nu$%e 5 S7nta/0 g eatest (value1, value2, value3 valuen) '/0
SQL-

sele!t g eatest(2# 1# 4)# g eatest("2# "1# "4) f o$ dual. 8R'(T'ST(2#1#4) 8R'(T'ST("2#"1#"4) """""""""""""""""""" """"""""""""""""""""""" 4 "2

p)
L'(ST

If all the values a e Te os then it will displa7 Te o5 If all the pa a$ete s a e nulls then it will displa7 nothing5 If an7 of the pa a$ete s is null it will displa7 nothing5

This will give the least nu$%e 5 S7nta/0 least (value1, value2, value3 valuen) '/0
SQL-

sele!t least(2# 1# 4)# least("2# "1# "4) f o$ dual. L'(ST(2#1#4) 2 L'(ST("2#"1#"4) "4

"""""""""""""""""""" """"""""""""""""""""""" If all the values a e Te os then it will displa7 Te o5 If all the pa a$ete s a e nulls then it will displa7 nothing5

40
U) If an7 of the pa a$ete s is null it will displa7 nothing5

CO(L'SC'

This will etu n fi st non"null value5 S7nta/0 !oales!e (value1, value2, value3 valuen) '/0
SQL-

sele!t !oales!e(2#1#4)# !oales!e(null#1#null#C) f o$ dual. CO(L'SC'(2#1#4) CO(L'SC'(NULL#1#NULL#C) """"""""""""""""""" 2 """"""""""""""""""""""""""""""" 1

STRIN8 BUNCTIONS

Init!ap Uppe Lowe Length Rpad Lpad Lt i$ Rt i$ T i$ T anslate Repla!e Sounde/ Con!at ( K XX K Con!atenation ope ato ) (s!ii Ch Su%st Inst De!ode 8 eatest Least

41
a)
INITC(A

Coales!e

This will !apitaliTe the initial lette of the st ing5 S7nta/0 init!ap (!trin") '/0
SQL-

sele!t init!ap(=!o$pute =) f o$ dual. INITC(A """"""""""" Co$pute

%)

UAA'R

This will !onve t the st ing into uppe !ase5 S7nta/0 uppe (!trin") '/0
SQL-

sele!t uppe (=!o$pute =) f o$ dual. UAA'R """""""""""


COMAUT'R

!)

LO6'R

This will !onve t the st ing into lowe !ase5 S7nta/0 lowe (!trin") '/0
SQL-

sele!t lowe (=COMAUT'R=) f o$ dual.

42

LO6'R """"""""""" !o$pute d)

L'N8T:

This will give length of the st ing5 S7nta/0 length (!trin") '/0
SQL-

sele!t length(=!o$pute =) f o$ dual. L'N8T: """"""""""" W

e)

RA(D

This will allows 7ou to pad the ight side of a !olu$n with an7 set of !ha a!te s5 S7nta/0 pad (!trin", len"t) *, paddin"_c)ar+) '/0
SQL-

sele!t pad(=!o$pute =#2C#=E=)# pad(=!o$pute =#2C#=EY=) f o$ dual. RA(D(=COMAUT'R= RA(D(=COMAUT'R= """""""""""""""""""""" """""""""""""""""""""" !o$pute EEEEEEE !o$pute EYEYEYE

"" Default padding !ha a!te was %lan& spa!e5 f)

LA(D

43
This will allows 7ou to pad the left side of a !olu$n with an7 set of !ha a!te s5 S7nta/0 lpad (!trin", len"t) *, paddin"_c)ar+) '/0
SQL-

sele!t lpad(=!o$pute =#2C#=E=)# lpad(=!o$pute =#2C#=EY=) f o$ dual. LA(D(=COMAUT'R= LA(D(=COMAUT'R= """"""""""""""""""""" EEEEEEE!o$pute """"""""""""""""""""" EYEYEYE!o$pute

"" Default padding !ha a!te was %lan& spa!e5 g)

LTRIM

This will t i$ off unwanted !ha a!te s f o$ the left end of st ing5 S7nta/0 lt i$ (!trin" *,unwanted_c)ar!+) '/0
SQL-

sele!t lt i$(=!o$pute =#=!o=)# lt i$(=!o$pute =#=!o$=) f o$ dual. LTRIM( LTRIM """""""" """"""""" $pute pute

SQL-

sele!t lt i$(=!o$pute =#=pute =)# lt i$(=!o$pute =#=o$pute =) f o$ dual. LTRIM(=C LTRIM(=C """""""""" !o$pute """""""""" !o$pute

"" If 7ou haven;t spe!if7 an7 unwanted !ha a!te s it will displa7 enti e st ing5 h)

RTRIM

44
This will t i$ off unwanted !ha a!te s f o$ the ight end of st ing5 S7nta/0 t i$ (!trin" *, unwanted_c)ar!+) '/0
SQL-

sele!t t i$(=!o$pute =#=e =)# t i$(=!o$pute =#=te =) f o$ dual. RTRIM( RTRIM """""""" """"""""" !o$put !o$pu

SQL-

sele!t t i$(=!o$pute =#=!o$put;)# t i$(=!o$pute =#=!o$pute=) f o$ dual. RTRIM(=C RTRIM(=C """""""""" !o$pute """""""""" !o$pute

"" If 7ou haven;t spe!if7 an7 unwanted !ha a!te s it will displa7 enti e st ing5 i)

TRIM

This will t i$ off unwanted !ha a!te s f o$ the %oth sides of st ing5 S7nta/0 t i$ (unwanted_c)ar! f o$ !trin") '/0
SQL-

sele!t t i$( =i= f o$ =indiani=) f o$ dual. TRIM( """"" ndian sele!t t i$( leading=i= f o$ =indiani=) f o$ dual. TRIM(L """""" ndiani "" this will wo & as

SQL-

LTRIM

45

SQL-

sele!t t i$( t ailing=i= f o$ =indiani=) f o$ dual. TRIM(T """""" Indian

"" this will wo & as

RTRIM

J)

TR(NSL(T'

This will epla!e the set of !ha a!te s# !ha a!te %7 !ha a!te 5 S7nta/0 t anslate (!trin", old_c)ar!, new_c)ar!) '/0
SQL-

sele!t t anslate(=india=#=in=#=/7=) f o$ dual. TR(NS """""""" /7d/a

&)

R'AL(C'

This will epla!e the set of !ha a!te s# st ing %7 st ing5 S7nta/0 epla!e (!trin", old_c)ar! *, new_c)ar!+) '/0
SQL-

sele!t epla!e(=india=#=in=#=/7=)# epla!e(Kindia;#;in;) f o$ dual. R'AL(C' +7dia R'AL(C' dia

""""""""""" """""""""""

l)

SOUND'+

This will %e used to find wo ds that sound li&e othe wo ds# e/!lusivel7 used in whe e

46
!lause5 S7nta/0 sounde/ (!trin") '/0
SQL-

sele!t E f o$ e$p whe e sounde/(ena$e) H sounde/(=SMIT=). >O) """"" CL'RF M8R :IR'D(T' """"" """""""""""" VS31 2V"D'C"W3 S(L C33 D'ATNO 13

'MANO 'N(M' """""""" """""""" V4DS $) SMIT:

""""""""" """"""""""

CONC(T

This will %e used to !o$%ine two st ings onl75 S7nta/0 !on!at (!trin"1, !trin"2) '/0
SQL-

sele!t !on!at(=!o$pute =#= ope ato =) f o$ dual. CONC(T(=COMAUT'R= """"""""""""""""""""""""" !o$pute ope ato

If 7ou want to !o$%ine $o e than two st ings 7ou have to use !on!atenation ope ato (XX)5 sele!t =how= XX = a e= XX = 7ou= f o$ dual. =:O6=XX=(R' """"""""""""""" how a e 7ou n)

SQL-

(SCII

This will etu n the de!i$al ep esentation in the data%ase !ha a!te set of the fi st

47
!ha a!te of the st ing5 S7nta/0 as!ii (!trin") '/0
SQL-

sele!t as!ii(=a=)# as!ii(=apple=) f o$ dual. (SCII(=(=) (SCII(=(AAL'=) """""""""""" """""""""""""""""" SV SV

o)

C:R

This will etu n the !ha a!te having the %ina 7 eUuivalent to the st ing in eithe the data%ase !ha a!te set o the national !ha a!te set5 S7nta/0 !h (number) '/0
SQL-

sele!t !h (SV) f o$ dual. C:R """"" a

p)

SU)STR

This will %e used to e/t a!t su%st ings5 S7nta/0 su%st (!trin", !tart_c)r_count *, no_o'_c)ar!+) '/0
SQL-

sele!t su%st (=!o$pute =#1)# su%st (=!o$pute =#1#C)# su%st (=!o$pute =#4#V) f o$ dual.

48

SU)STR( SU)ST SU)STR """""""""" """"""" """""""" o$pute o$put $pute If no_o'_c)ar! pa a$ete is negative then it will displa7 nothing5 If %oth pa a$ete s e/!ept !trin" a e null o Te os then it will displa7 nothing5 If no_o'_c)ar! pa a$ete is g eate than the length of the st ing then it igno es and !al!ulates %ased on the o ginal st ing length5 If !tart_c)r_count is negative then it will e/t a!t the su%st ing f o$ ight end5 2 C "W U)
INSTR

1 O "V

4 M "D

@ A "C

C U "@

D T "4

V ' "1

W R "2

This will allows 7ou fo sea !hing th ough a st ing fo set of !ha a!te s5 S7nta/0 inst (!trin", !earc)_!tr *, !tart_c)r_count *, occurrence+ +) '/0
SQL-

sele!t inst (=info $ation=#=o=#@#2)# inst (=info $ation=#=o=#@#1) f o$ dual. INSTR(=INBORM(TION=#=O=#@#2) INSTR(=INBORM(TION=#=O=#@#1) """""""""""""""""""""""""""""""""""" """"""""""""""""""""""""""""""""""""" @ 23

If 7ou a e not spe!if7ing !tart_c)r_count and occurrence then it will sta t sea !h f o$ the %eginning and finds fi st o!!u en!e onl75 If %oth pa a$ete s !tart_c)r_count and occurrence a e null# it will displa7 nothing5

D'COD'

49
De!ode will a!t as value %7 value su%stitution5 Bo eve 7 value of field# it will !he!&s fo a $at!h in a se ies of if?then tests5 S7nta/0 de!ode (value, i'1, t)en1, i'2, t)en2, . el!e). '/0
SQL-

sele!t sal# de!ode(sal#C33#=Low=#C333#=:igh=#=Mediu$=) f o$ e$p. S(L """"" C33 1C33 1333 4C33 4333 C333 @333 C333 2W33 2133 1333 1V33 1133 4133 D'COD' """"""""" Low Mediu$ Mediu$ Mediu$ Mediu$ :igh Mediu$ :igh Mediu$ Mediu$ Mediu$ Mediu$ Mediu$ Mediu$

SQL-

sele!t de!ode(2#2#4)# de!ode(2#1#4#@#@#D) f o$ dual. D'COD'(2#2#4) D'COD'(2#1#4#@#@#D) """"""""""""""""" """""""""""""""""""""""" 4 D

If the nu$%e of pa a$ete s a e odd and diffe ent then de!ode will displa7 nothing5 If the nu$%e of pa a$ete s a e even and diffe ent then de!ode will displa7 last value5 If all the pa a$ete s a e null then de!ode will displa7 nothing5 If all the pa a$ete s a e Te os then de!ode will displa7 Te o5

50

s)

8R'(T'ST

This will give the g eatest st ing5 S7nta/0 g eatest (!trn"1, !trin"2, !trin"3 !trin"n) '/0
SQL-

sele!t g eatest(=a=# =%=# =!=)# g eatest(=satish=#=s inu=#=sa&eth=) f o$ dual.

8R'(T 8R'(T """"""" """"""" ! t) s inu

If all the pa a$ete s a e nulls then it will displa7 nothing5 If an7 of the pa a$ete s is null it will displa7 nothing5

L'(ST

This will give the least st ing5 S7nta/0 g eatest (!trn"1, !trin"2, !trin"3 !trin"n) '/0
SQL-

sele!t least(=a=# =%=# =!=)# least(=satish=#=s inu=#=sa&eth=) f o$ dual.

L'(ST L'(ST """"""" """"""" a u) sa&eth

If all the pa a$ete s a e nulls then it will displa7 nothing5 If an7 of the pa a$ete s is null it will displa7 nothing5

CO(L'SC'

51
This will gives the fi st non"null st ing5 S7nta/0 !oales!e (!trn"1, !trin"2, !trin"3 !trin"n) '/0
SQL-

sele!t !oales!e(=a=#=%=#=!=)# !oales!e(null#=a=#null#=%=) f o$ dual. CO(L'SC' CO(L'SC' """"""""""" a """"""""""" a

D(T' BUNCTIONS

S7sdate Cu entMdate Cu entMti$esta$p S7sti$esta$p Lo!alti$esta$p D%ti$eTone Sessionti$eTone ToM!ha ToMdate (ddM$onths MonthsM%etween Ne/tMda7 LastMda7 '/t a!t 8 eatest Least Round T un! NewMti$e Coales!e
DD"MON"**5

O a!le default date fo $at is

6e !an !hange the default fo $at to ou desi ed fo $at %7 using the following !o$$and5

52

SQL-

alte session set nlsMdateMfo $at H KDD"MONT:"****;. )ut this will e/pi e on!e the session was !losed5

a)

S*SD(T'

This will give the !u ent date and ti$e5 '/0


SQL-

sele!t s7sdate f o$ dual. S*SD(T' """"""""""" 1@"D'C"3D

%)

CURR'NTMD(T'

This will etu ns the !u ent date in the session;s ti$eTone5 '/0
SQL-

sele!t !u entMdate f o$ dual. CURR'NTMD(T' """""""""""""""""" 1@"D'C"3D

!)

CURR'NTMTIM'ST(MA

This will etu ns the !u ent ti$esta$p with the a!tive ti$e Tone info $ation5 '/0
SQL-

sele!t !u entMti$esta$p f o$ dual. CURR'NTMTIM'ST(MA """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" 1@"D'C"3D 345@15@254W44DS (M G3C043

53

d)

S*STIM'ST(MA

This will etu ns the s7ste$ date# in!luding f a!tional se!onds and ti$e Tone of the data%ase5 '/0
SQL-

sele!t s7sti$esta$p f o$ dual. S*STIM'ST(MA """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" 1@"D'C"3D 345@S5425W433SS (M G3C043

e)

LOC(LTIM'ST(MA

This will etu ns lo!al ti$esta$p in the a!tive ti$e Tone info $ation# with no ti$e Tone info $ation shown5 '/0
SQL-

sele!t lo!alti$esta$p f o$ dual. LOC(LTIM'ST(MA """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" 1@"D'C"3D 345@@52W5C31WV@ (M

f)

D)TIM'ZON'

This will etu ns the !u ent data%ase ti$e Tone in UTC fo $at5 (Coo dinated Unive sal Ti$e) '/0
SQL-

sele!t d%ti$eTone f o$ dual. D)TIM'ZON' """"""""""""""" "3V033

g)

S'SSIONTIM'ZON'

This will etu ns the value of the !u ent session;s ti$e Tone5

54

'/0
SQL-

sele!t sessionti$eTone f o$ dual. S'SSIONTIM'ZON' """""""""""""""""""""""""""""""""""" G3C043

h)

TOMC:(R

This will %e used to e/t a!t va ious date fo $ats5 The availa%le date fo $ats as follows5 S7nta/0 toM!ha (date# 'ormat)
D(T' BORM(TS

D DD DDD MM MON MONT: RM D* D(* * ** *** **** S**** I I* I** I***

"" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""

No of da7s in wee& No of da7s in $onth No of da7s in 7ea No of $onth Th ee lette a%% eviation of $onth Bull7 spelled out $onth Ro$an nu$e al $onth Th ee lette a%% eviated da7 Bull7 spelled out da7 Last one digit of the 7ea Last two digits of the 7ea Last th ee digits of the 7ea Bull fou digit 7ea Signed 7ea One digit 7ea f o$ ISO standa d Two digit 7ea f o$ ISO standa d Th ee digit 7ea f o$ ISO standa d Bou digit 7ea f o$ ISO standa d

55
*# *** *'(R CC Q 6 66 I6 :: MI SS BB (M (5M (D (5D BM T: SA SAT: T:SA

"" "" "" "" "" "" "" "" "" "" ""

*ea with !o$$a Bull7 spelled out 7ea Centu 7 No of Uua te s No of wee&s in $onth No of wee&s in 7ea No of wee&s in 7ea f o$ ISO standa d :ou s Minutes Se!onds B a!tional se!onds Displa7s (M o AM depending upon ti$e of da7 Displa7s (5M o A5M depending upon ti$e of da7 Displa7s (D o )C depending upon the date Displa7s (D o )C depending upon the date A efi/ to $onth o da7# supp esses padding of $onth o da7 Suffi/ to a nu$%e suffi/ to a nu$%e to %e spelled out Suffi/ !o$%ination of T: and SA to %e %oth spelled out sa$e as SAT:

o o o o

AM A5M )C )5C

"" "" "" "" "" "" "" "" ""

'/0
SQL-

sele!t toM!ha (s7sdate#=dd $onth 7777 hh0$i0ss a$ d7=) f o$ dual. TOMC:(R(S*SD(T'#=DD MONT: ****::0MI """""""""""""""""""""""""""""""""""""""""""""""""""" 1@ de!e$%e 133D 31034014 p$ sun

SQL-

sele!t toM!ha (s7sdate#=dd $onth 7ea =) f o$ dual.

TOMC:(R(S*SD(T'#=DDMONT:*'(R=) """"""""""""""""""""""""""""""""""""""""""""""""""""""" 1@ de!e$%e


SQL-

two thousand si/

sele!t toM!ha (s7sdate#=dd f$$onth 7ea =) f o$ dual.

56

TOMC:(R(S*SD(T'#=DD BMMONT: *'(R=) """"""""""""""""""""""""""""""""""""""""""""""""""""""" 1@ de!e$%e two thousand si/


SQL-

sele!t toM!ha (s7sdate#=ddth DDT:=) f o$ dual. TOMC:(R(S """""""""""" 1@th 1@T: sele!t toM!ha (s7sdate#=ddspth DDSAT:=) f o$ dual. TOMC:(R(S*SD(T'#=DDSAT:DDSAT: """""""""""""""""""""""""""""""""""""""""" twent7"fou th T6'NT*"BOURT:

SQL-

SQL-

sele!t toM!ha (s7sdate#=ddsp Ddsp DDSA =) f o$ dual. TOMC:(R(S*SD(T'#=DDSADDSADDSA=) """""""""""""""""""""""""""""""""""""""""""""""" twent7"fou Twent7"Bou T6'NT*"BOUR

i)

TOMD(T'

This will %e used to !onve t the st ing into data fo $at5 S7nta/0 toMdate (date) '/0
SQL-

sele!t toM!ha (toMdate(=1@?de!?133D=#=dd?$on?7777=)# =dd E $onth E da7=) f o$ dual. TOMC:(R(TOMD(T'(=1@?D'C?13 """""""""""""""""""""""""" 1@ E de!e$%e E Sunda7

57

"" If 7ou a e not using toM!ha o a!le will displa7 output in default date fo $at5 J)
(DDMMONT:S

This will add the spe!ified $onths to the given date5 S7nta/0 addM$onths (date, no_o'_mont)!) '/0
SQL-

sele!t addM$onths(toMdate(=22"Jan"2SS3=#=dd"$on"7777=)# C) f o$ dual. (DDMMONT:S """""""""""""""" 22">UN"S3 sele!t addM$onths(toMdate(=22"Jan"2SS3=#=dd"$on"7777=)# "C) f o$ dual. (DDMMONT: """"""""""""""" 22"(U8"WS

SQL-

&)

If no_o'_mont)! is Te o then it will displa7 the sa$e date5 If no_o'_mont)! is null then it will displa7 nothing5

MONT:SM)'T6''N

This will give diffe en!e of $onths %etween two dates5 S7nta/0 $onthsM%etween (date1, date2) '/0
SQL-

sele!t $onthsM%etween(toMdate(=22"aug"2SS3=#=dd"$on"7777=)# toMdate(=22" Jan"2SS3=#=dd"$on"7777=)) f o$ dual.

58

MONT:SM)'T6''N(TOMD(T'(=22"(U8"2SS3=#=DD"MON"****=)#TOMD(T'(=22">(N" 2SS3=#=DD"MON"****=)) """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" V


SQL-

sele!t $onthsM%etween(toMdate(=22"Jan"2SS3=#=dd"$on"7777=)# toMdate(=22" aug"2SS3=#=dd"$on"7777=)) f o$ dual.

MONT:SM)'T6''N(TOMD(T'(=22">(N"2SS3=#=DD"MON"****=)#TOMD(T'(=22"(U8" 2SS3=#=DD"MON"****=)) """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" "V l)

N'+TMD(*

This will p odu!e ne/t da7 of the given da7 f o$ the spe!ified date5 S7nta/0 ne/tMda7 (date, day) '/0
SQL-

sele!t ne/tMda7(toMdate(=1@"de!"133D=#=dd"$on"7777=)#=sun=) f o$ dual. N'+TMD(*( """"""""""""" 42"D'C"3D

"" If the da7 pa a$ete is null then it will displa7 nothing5 $)

L(STMD(*

This will p odu!e last da7 of the given date5 S7nta/0 lastMda7 (date) '/0

59
SQL-

sele!t lastMda7(toMdate(=1@"de!"133D=#=dd"$on"7777=)#=sun=) f o$ dual. L(STMD(*( """"""""""""" 42"D'C"3D

n)

'+TR(CT

This is used to e/t a!t a po tion of the date value5 S7nta/0 e/t a!t ((7ea X $onth X da7 X hou X $inute X se!ond)# date) '/0
SQL-

sele!t e/t a!t(7ea f o$ s7sdate) f o$ dual. '+TR(CT(*'(RBROMS*SD(T') """""""""""""""""""""""""""""""""""" 133D

"" *ou !an e/t a!t onl7 one value at a ti$e5 o)

8R'(T'ST

This will give the g eatest date5 S7nta/0 g eatest (date1, date2, date3 daten) '/0
SQL-

sele!t g eatest(toMdate(=22"Jan"S3=#=dd"$on"77=)#toMdate(=22"$a "S3=#=dd" $on"77=)#toMdate(=22"ap "S3=#=dd"$on"77=)) f o$ dual. 8R'(T'ST( """"""""""""" 22"(AR"S3

60

p)

L'(ST

This will give the least date5 S7nta/0 least (date1, date2, date3 daten) '/0
SQL-

sele!t least(toMdate(=22"Jan"S3=#=dd"$on"77=)#toMdate(=22"$a "S3=#=dd"$on" 77=)#toMdate(=22"ap "S3=#=dd"$on"77=)) f o$ dual. L'(ST( """"""""""""" 22">(N"S3

U)

ROUND

Round will ounds the date to whi!h it was eUual to o g eate than the given date5 S7nta/0 ound (date, (da7 X $onth X 7ea )) If the se!ond pa a$ete was year then ound will !he!&s the $onth of the given date in the following anges5
>(N >UL "" "" >UN D'C

If the $onth falls %etween >(N and >UN then it etu ns the fi st da7 of the !u ent 7ea 5 If the $onth falls %etween >UL and D'C then it etu ns the fi st da7 of the ne/t 7ea 5 If the se!ond pa a$ete was mont) then ound will !he!&s the da7 of the given date in the following anges5 2 2D "" "" 2C 42

If the da7 falls %etween 2 and 2C then it etu ns the fi st da7 of the !u ent $onth5

61
If the da7 falls %etween 2D and 42 then it etu ns the fi st da7 of the ne/t $onth5 If the se!ond pa a$ete was day then ound will !he!&s the wee& da7 of the given date in the following anges5
SUN T:U "" "" 6'D SUN

If the wee& da7 falls %etween SUN and 6'D then it etu ns the p evious sunda75 If the wee&da7 falls %etween T:U and SUN then it etu ns the ne/t sunda75 If the se!ond pa a$ete was null then it etu ns nothing5 If the 7ou a e not spe!if7ing the se!ond pa a$ete then ound will esets the ti$e to the %egining of the !u ent da7 in !ase of use spe!ified date5 If the 7ou a e not spe!if7ing the se!ond pa a$ete then ound will esets the ti$e to the %egining of the ne/t da7 in !ase of s7sdate5 '/0
SQL-

sele!t ound(toMdate(=1@"de!"3@=#=dd"$on"77=)#=7ea =)# ound(toMdate(=22"$a " 3D=#=dd"$on"77=)#=7ea =) f o$ dual. ROUND(TOM ROUND(TOM """""""""""" 32">(N"3C """"""""""""""" 32">(N"3D

SQL-

sele!t ound(toMdate(=22"Jan"3@=#=dd"$on"77=)#=$onth=)# ound(toMdate(=2W" Jan"3@=#=dd"$on"77=)#=$onth=) f o$ dual. ROUND(TOM ROUND(TOM """"""""""""" """"""""""""""" 32">(N"3@ 32"B')"3@

SQL-

sele!t ound(toMdate(=1D"de!"3D=#=dd"$on"77=)#=da7=)# ound(toMdate(=1S"de!" 3D=#=dd"$on"77=)#=da7=) f o$ dual. ROUND(TOM ROUND(TOM

62
"""""""""""""" """""""""""""" 1@"D'C"3D 42"D'C"3D

SQL-

sele!t toM!ha ( ound(toMdate(=1@"de!"3D=#=dd"$on"77=))# =dd $on 7777 hh0$i0ss a$=) f o$ dual. TOMC:(R(ROUND(TOMD(T'(= """"""""""""""""""""""""""""""""" 1@ de! 133D 21033033 a$

TRUNC

T un! will !hops off the date to whi!h it was eUual to o less than the given date5 S7nta/0 t un! (date, (da7 X $onth X 7ea )) If the se!ond pa a$ete was year then it alwa7s etu ns the fi st da7 of the !u ent 7ea 5 If the se!ond pa a$ete was mont) then it alwa7s etu ns the fi st da7 of the !u ent $onth5 If the se!ond pa a$ete was day then it alwa7s etu ns the p evious sunda75 If the se!ond pa a$ete was null then it etu ns nothing5 If the 7ou a e not spe!if7ing the se!ond pa a$ete then t un& will esets the ti$e to the %egining of the !u ent da75 '/0
SQL-

sele!t t un!(toMdate(=1@"de!"3@=#=dd"$on"77=)#=7ea =)# t un!(toMdate(=22"$a " 3D=#=dd"$on"77=)#=7ea =) f o$ dual. TRUNC(TOM TRUNC(TOM """"""""""""" """""""""""""" 32">(N"3@ 32">(N"3D

SQL-

sele!t t un!(toMdate(=22"Jan"3@=#=dd"$on"77=)#=$onth=)# t un!(toMdate(=2W"Jan" 3@=#=dd"$on"77=)#=$onth=) f o$ dual. TRUNC(TOM TRUNC(TOM

63
""""""""""""" """"""""""""" 32">(N"3@ 32">(N"3@

SQL-

sele!t t un!(toMdate(=1D"de!"3D=#=dd"$on"77=)#=da7=)# t un!(toMdate(=1S"de!" 3D=#=dd"$on"77=)#=da7=) f o$ dual. TRUNC(TOM TRUNC(TOM """"""""""""" """""""""""""" 1@"D'C"3D 1@"D'C"3D

SQL-

sele!t toM!ha (t un!(toMdate(=1@"de!"3D=#=dd"$on"77=))# =dd $on 7777 hh0$i0ss a$=) f o$ dual. TOMC:(R(TRUNC(TOMD(T'(= """"""""""""""""""""""""""""""""" 1@ de! 133D 21033033 a$

s)

N'6MTIM'

This will give the desi ed ti$eTone;s date and ti$e5 S7nta/0 newMti$e (date, current_time,one, de!ired_time,one) (vaila%le ti$eTones a e as follows5
TIM'ZON'S

(ST?(DT )ST?)DT CST?CDT 'ST?'DT 8MT :ST?:DT MST?MDT NST AST?ADT *ST?*DT

"" "" "" "" "" "" "" "" "" ""

(tlanti! standa d?da7 light ti$e )e ing standa d?da7 light ti$e Cent al standa d?da7 light ti$e 'aste n standa d?da7 light ti$e 8 eenwi!h $ean ti$e (las&a":awaii standa d?da7 light ti$e Mountain standa d?da7 light ti$e Newfoundland standa d ti$e Aa!ifi! standa d?da7 light ti$e *u&on standa d?da7 light ti$e

64

'/0
SQL-

sele!t toM!ha (newMti$e(s7sdate#=g$t=#=7st=)#=dd $on 7777 hh0$i0ss a$=) f o$ dual. TOMC:(R(N'6MTIM'(S*SD(T """"""""""""""""""""""""""""""""""" 1@ de! 133D 310C2013 p$ sele!t toM!ha (newMti$e(s7sdate#=g$t=#=est=)#=dd $on 7777 hh0$i0ss a$=) f o$ dual. TOMC:(R(N'6MTIM'(S*SD(T """"""""""""""""""""""" 1@ de! 133D 3D0C201D p$

SQL-

t)

CO(L'SC'

This will give the fi st non"null date5 S7nta/0 !oales!e (date1, date2, date3 daten) '/0
SQL-

sele!t !oales!e(=21"Jan"S3=#=24"Jan"SS=)# !oales!e(null#=21"Jan"S3=#=14"$a " SW=#null) f o$ dual. CO(L'SC'( CO(L'SC'( """"""""""""" """""""""""" 21"Jan"S3 21"Jan"S3

MISC'LL(N'OUS BUNCTIONS

Uid Use 9siTe Ran&

65
a)
UID

DenseM an&

This will etu ns the intege value !o esponding to the use !u entl7 logged in5 '/0
SQL-

sele!t uid f o$ dual. UID """""""""" 42S

%)

US'R

This will etu ns the login;s use na$e5 '/0


SQL-

sele!t use f o$ dual. US'R """""""""""""""" S(F'T:

!)

9SIZ'

This will etu ns the nu$%e of %7tes in the e/p ession5 '/0
SQL-

sele!t vsiTe(214)# vsiTe(=!o$pute =)# vsiTe(=21"Jan"S3=) f o$ dual. 9SIZ'(214) 9SIZ'(=COMAUT'R=) 9SIZ'(=21">(N"S3=) """"""""""""" """"""""""""""""""""""" """""""""""""""""""""" 4 W S

66

d)

R(NF

This will give the non"seUuential an&ing5 '/0


SQL-

sele!t ownu$#sal f o$ (sele!t sal f o$ e$p o de %7 sal des!). RO6NUM 2 1 4 @ C D V W S 23 22 21 24 2@ 2C S(L """""""""" """""""""" C333 4333 4333 1SVC 1WC3 1@C3 2D33 2C33 2433 21C3 21C3 2233 2333 SC3 W33

SQL-

sele!t an&(1SVC) within g oup(o de %7 sal des!) f o$ e$p. R(NF(1SVC)6IT:IN8ROUA(ORD'R)*S(LD'SC) """"""""""""""""""""""""""""""""""""""""""""""""""""""""" @

d)

D'NS'MR(NF

This will give the seUuential an&ing5

67

'/0
SQL-

sele!t denseM an&(1SVC) within g oup(o de %7 sal des!) f o$ e$p. D'NS'MR(NF(1SVC)6IT:IN8ROUA(ORD'R)*S(LD'SC) """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" 4

CON9'RSION BUNCTIONS

a)
)INMTOMNUM

)inMtoMnu$ Cha to owid Rowidto!ha ToMnu$%e ToM!ha ToMdate

This will !onve t the %ina 7 value to its nu$e i!al eUuivalent5 S7nta/0 %inMtoMnu$( binary_bit!) '/0
SQL-

sele!t %inMtoMnu$(2#2#3) f o$ dual. )INMTOMNUM(2#2#3) """""""""""""""""""""""" D

%)

If all the %its a e Te o then it p odu!es Te o5 If all the %its a e null then it p odu!es an e o 5

C:(RTORO6ID

This will !onve t a !ha a!te st ing to a!t li&e an inte nal o a!le ow identifie o

owid5

68

!)

RO6IDTOC:(R

This will !onve t an inte nal o a!le ow identifie o d)

owid to !ha a!te st ing5

TOMNUM)'R

This will !onve t a !ha o va !ha to nu$%e 5 e)

TOMC:(R

This will !onve t a nu$%e o date to !ha a!te st ing5 f)

TOMD(T'

This will !onve t a nu$%e # !ha o va !ha to a date5

8ROUA BUNCTIONS Su$ (vg Ma/ Min Count

8 oup fun!tions will %e applied on all the ows %ut p odu!es single output5 a)

SUM

This will give the su$ of the values of the spe!ified !olu$n5 S7nta/0 su$ (column) '/0
SQL-

sele!t su$(sal) f o$ e$p.

69

SUM(S(L) """""""""" 4WD33 %)


(98

This will give the ave age of the values of the spe!ified !olu$n5 S7nta/0 avg (column) '/0
SQL-

sele!t avg(sal) f o$ e$p. (98(S(L) """"""""""""""" 1VCV52@1WD

!)

M(+

This will give the $a/i$u$ of the values of the spe!ified !olu$n5 S7nta/0 $a/ (column) '/0
SQL-

sele!t $a/(sal) f o$ e$p. M(+(S(L) """""""""" C333

d)

MIN

This will give the $ini$u$ of the values of the spe!ified !olu$n5 S7nta/0 $in (column)

70

'/0
SQL-

sele!t $in(sal) f o$ e$p. MIN(S(L) """""""""" C33

e)

COUNT

This will give the !ount of the values of the spe!ified !olu$n5 S7nta/0 !ount (column) '/0
SQL-

sele!t !ount(sal)#!ount(E) f o$ e$p. COUNT(S(L) """""""""""""" 2@ COUNT(E) """""""""""" 2@

71

CONSTR(INTS
Const aints a e !atego iTed as follows5 Do$ain integ it7 !onst aints Not null Che!& 'ntit7 integ it7 !onst aints UniUue A i$a 7 &e7 Refe ential integ it7 !onst aints Bo eign &e7 Const aints a e alwa7s atta!hed to a !olu$n not a ta%le5 6e !an add !onst aints in th ee wa7s5 Colu$n level Ta%le level (lte level "" along with the !olu$n definition "" afte the ta%le definition "" using alte !o$$and

6hile adding !onst aints 7ou need not spe!if7 the na$e %ut the t7pe onl7# o a!le will inte nall7 na$e the !onst aint5 If 7ou want to give a na$e to the !onst aint# 7ou have to use the !onst aint !lause5

72

NOT NULL This is used to avoid null values5 6e !an add this !onst aint in !olu$n level onl75 '/0
SQL-

! eate ta%le student(no nu$%e (1) not null# na$e va !ha (23)# $a &s nu$%e (4)). ! eate ta%le student(no nu$%e (1) !onst aint nn not null# na$e va !ha (23)# $a &s nu$%e (4)).

SQL-

C:'CF This is used to inse t the values %ased on spe!ified !ondition5 6e !an add this !onst aint in all th ee levels5 '/0
COLUMN L'9'L

SQL-

! eate ta%le student(no nu$%e (1) # na$e va !ha (23)# $a &s nu$%e (4) !he!& ($a &s - 433)). ! eate ta%le student(no nu$%e (1) # na$e va !ha (23)# $a &s nu$%e (4) !onst aint !h !he!&($a &s - 433)).

SQL-

T()L' L'9'L

SQL-

! eate ta%le student(no nu$%e (1) # na$e va !ha (23)# $a &s nu$%e (4)# !he!& ($a &s - 433)). ! eate ta%le student(no nu$%e (1) # na$e va !ha (23)# $a &s nu$%e (4)# !onst aint !h !he!&($a &s - 433)).

SQL-

73

(LT'R L'9'L

SQLSQL-

alte ta%le student add !he!&($a &s-433). alte ta%le student add !onst aint !h !he!&($a &s-433).

UNIQU' This is used to avoid dupli!ates %ut it allow nulls5 6e !an add this !onst aint in all th ee levels5 '/0
COLUMN L'9'L

SQL-

! eate ta%le student(no nu$%e (1) uniUue# na$e va !ha (23)# $a &s nu$%e (4)). ! eate ta%le student(no nu$%e (1) !onst aint un uniUue# na$e va !ha (23)# $a &s nu$%e (4)).

SQL-

T()L' L'9'L

SQL-

! eate ta%le student(no nu$%e (1) # na$e va !ha (23)# $a &s nu$%e (4)# uniUue(no)). ! eate ta%le student(no nu$%e (1) # na$e va !ha (23)# $a &s nu$%e (4)# !onst aint un uniUue(no)).

SQL-

(LT'R L'9'L

SQLSQL-

alte ta%le student add uniUue(no). alte ta%le student add !onst aint un uniUue(no).

ARIM(R* F'* This is used to avoid dupli!ates and nulls5 This will wo & as !o$%ination of uniUue and not null5 A i$a 7 &e7 alwa7s atta!hed to the pa ent ta%le5

74
'/0
COLUMN L'9'L

6e !an add this !onst aint in all th ee levels5

SQL-

! eate ta%le student(no nu$%e (1) p i$a 7 &e7# na$e va !ha (23)# $a &s nu$%e (4)). ! eate ta%le student(no nu$%e (1) !onst aint p& p i$a 7 &e7# na$e va !ha (23)# $a &s nu$%e (4)).

SQL-

T()L' L'9'L

SQL-

! eate ta%le student(no nu$%e (1) # na$e va !ha (23)# $a &s nu$%e (4)# p i$a 7 &e7(no)). ! eate ta%le student(no nu$%e (1) # na$e va !ha (23)# $a &s nu$%e (4)# !onst aint p& p i$a 7 &e7(no)).

SQL-

(LT'R L'9'L

SQLSQL-

alte ta%le student add p i$a 7 &e7(no). alte ta%le student add !onst aint p& p i$a 7 &e7(no).

BOR'I8N F'* This is used to efe en!e the pa ent ta%le p i$a 7 &e7 !olu$n whi!h allows dupli!ates5 '/0
T()L' L'9'L

Bo eign &e7 alwa7s atta!hed to the !hild ta%le5 6e !an add this !onst aint in ta%le and alte levels onl75

SQL-

! eate ta%le e$p(e$pno nu$%e (1)# ena$e va !ha (23)# deptno nu$%e (1)# p i$a 7 &e7(e$pno)# fo eign &e7(deptno) efe en!es dept(deptno)). ! eate ta%le e$p(e$pno nu$%e (1)# ena$e va !ha (23)# deptno nu$%e (1)#

SQL-

75
!onst aint p& p i$a 7 &e7(e$pno)# !onst aint f& fo eign &e7(deptno) efe en!es dept(deptno)).
(LT'R L'9'L

SQLSQL-

alte ta%le e$p add fo eign &e7(deptno) efe en!es dept(deptno). alte ta%le e$p add !onst aint f& fo eign &e7(deptno) efe en!es dept(deptno).

On!e the p i$a 7 &e7 and fo eign &e7 elationship has %een ! eated then 7ou !an not e$ove an7 pa ent e!o d if the dependent !hilds e/ists5

USIN8 ON D'LT' C(SC(D' )7 using this !lause 7ou !an e$ove the pa ent e!o d even it !hilds e/ists5 )e!ause when eve dependent !onst aint5 '/0
T()L' L'9'L

7ou

e$ove pa ent

e!o d o a!le auto$ati!all7

e$oves all its

e!o ds f o$ !hild ta%le# if this !lause is p esent while ! eating fo eign &e7

SQL-

! eate ta%le e$p(e$pno nu$%e (1)# ena$e va !ha (23)# deptno nu$%e (1)# p i$a 7 &e7(e$pno)# fo eign &e7(deptno) efe en!es dept(deptno) on delete !as!ade). ! eate ta%le e$p(e$pno nu$%e (1)# ena$e va !ha (23)# deptno nu$%e (1)# !onst aint p& p i$a 7 &e7(e$pno)# !onst aint f& fo eign &e7(deptno) efe en!es dept(deptno) on delete !as!ade).

SQL-

(LT'R L'9'L

SQL-

alte ta%le e$p add fo eign &e7(deptno) efe en!es dept(deptno) on delete !as!ade. alte ta%le e$p add !onst aint f& fo eign &e7(deptno) efe en!es dept(deptno) on delete !as!ade.

SQL-

COMAOSIT' F'*S

76

( !o$posite &e7 !an %e defined on a !o$%ination of !olu$ns5 6e !an define !o$posite &e7s on entit7 integ it7 and efe ential integ it7 !onst aints5 Co$posite &e7 !an %e defined in ta%le and alte levels onl75 '/0
UNIQU' (T()L' L'9'L)

SQL-

! eate ta%le student(no nu$%e (1) # na$e va !ha (23)# $a &s nu$%e (4)# uniUue(no#na$e)). ! eate ta%le student(no nu$%e (1) # na$e va !ha (23)# $a &s nu$%e (4)# !onst aint un uniUue(no#na$e)).

SQL-

UNIQU' ((LT'R L'9'L)

SQLSQL-

alte ta%le student add uniUue(no#na$e). alte ta%le student add !onst aint un uniUue(no#na$e).

ARIM(R* F'* (T()L' L'9'L)

SQL-

! eate ta%le student(no nu$%e (1) # na$e va !ha (23)# $a &s nu$%e (4)# p i$a 7 &e7(no#na$e)). ! eate ta%le student(no nu$%e (1) # na$e va !ha (23)# $a &s nu$%e (4)# !onst aint p& p i$a 7 &e7(no#na$e)).

SQL-

ARIM(R* F'* ((LT'R L'9'L)

SQLSQL-

alte ta%le student add p i$a 7 &e7(no#an$e). alte ta%le student add !onst aint p& p i$a 7 &e7(no#na$e).

BOR'I8N F'* (T()L' L'9'L)

SQL-

! eate ta%le e$p(e$pno nu$%e (1)# ena$e va !ha (23)# deptno nu$%e (1)# dna$e va !ha (23)# p i$a 7 &e7(e$pno)# fo eign &e7(deptno#dna$e) efe en!es dept(deptno#dna$e)). ! eate ta%le e$p(e$pno nu$%e (1)# ena$e va !ha (23)# deptno nu$%e (1)# dna$e va !ha (23)# !onst aint p& p i$a 7 &e7(e$pno)# !onst aint f& fo eign &e7(deptno#dna$e) efe en!es dept(deptno#dna$e)).

SQL-

77

BOR'I8N F'* ((LT'R L'9'L)

SQLSQL-

alte ta%le e$p add fo eign &e7(deptno#dna$e) efe en!es dept(deptno#dna$e). alte ta%le e$p add !onst aint f& fo eign &e7(deptno#dna$e) efe en!es dept(deptno#dna$e).

D'B'RR()L' CONSTR(INTS 'a!h !onst aint has two additional att i%utes to suppo t defe ed !he!&ing of !onst aints5 Defe ed initiall7 i$$ediate Defe ed initiall7 defe ed

Defe ed initiall7 i$$ediate !he!&s fo !onst aint violation at the ti$e of inse t5 Defe ed initiall7 defe ed !he!&s fo !onst aint violation at the ti$e of !o$$it5 '/0
SQL-

! eate ta%le student(no nu$%e (1)# na$e va !ha (23)# $a &s nu$%e (4)# !onst aint un uniUue(no) defe ed initiall7 i$$ediate). ! eate ta%le student(no nu$%e (1)# na$e va !ha (23)# $a &s nu$%e (4)# !onst aint un uniUue(no) defe ed initiall7 defe ed). alte ta%le student add !onst aint un uniUue(no) defe a%le initiall7 defe ed. set !onst aints all i$$ediate.

SQL-

SQL-

SQL-

This will ena%le all the !onst aints violations at the ti$e of inse ting5 set !onst aints all defe ed.

SQL-

This will ena%le all the !onst aints violations at the ti$e of !o$$it5 OA'R(TIONS 6IT: CONSTR(INTS Aossi%le ope ations with !onst aints as follows5 'na%le Disa%le 'nfo !e D op

78

'N()L'

This will ena%le the !onst aint5 )efo e ena%le# the !onst aint will !he!& the e/isting data5 '/0
SQL-

alte ta%le student ena%le !onst aint un.

DIS()L'

This will disa%le the !onst aint5 '/0


SQL-

alte ta%le student ena%le !onst aint un.

'NBORC'

This will enfo !e the !onst aint athe than ena%le fo futu e inse ts o updates5 This will not !he!& fo e/isting data while enfo !ing data5 '/0
SQL-

alte ta%le student enfo !e !onst aint un.

DROA

This will e$ove the !onst aint5 '/0


SQL-

alte ta%le student d op !onst aint un.

On!e the ta%le is d opped# !onst aints auto$ati!all7 will d op5

79

C(S' (ND D'B(ULT

C(S' Case is si$ila to de!ode %ut easie to unde stand while going th ough !oding '/0
SQL-

Sele!t sal# Case sal 6hen C33 then Klow; 6hen C333 then Khigh; 'lse K$ediu$; 'nd !ase B o$ e$p. S(L """"" C33 1C33 1333 4C33 4333 C333 C(S' """""""" low $ediu$ $ediu$ $ediu$ $ediu$ high

80
@333 C333 2W33 2133 1333 1V33 1133 4133 $ediu$ high $ediu$ $ediu$ $ediu$ $ediu$ $ediu$ $ediu$

D'B(ULT -e'ault !an %e !onside ed as a su%stitute %ehavio of not null !onst aint when applied to new ows %eing ente ed into the ta%le5 6hen 7ou define a !olu$n with the de'ault &e7wo d followed %7 a value# 7ou a e a!tuall7 telling the data%ase that# on inse t if a ow was not assigned a value fo this !olu$n# use the default value that 7ou have spe!ified5 Default is applied onl7 du ing inse tion of new ows5 '/0
SQLSQLSQL-

! eate ta%le student(no nu$%e (1) default 22#na$e va !ha (1)). inse t into student values(2#=a=). inse t into student(na$e) values(=%=). sele!t E f o$ student. NO 2 22 N(M' a %

SQL-

"""""" """""""""

SQL-

inse t into student values(null# K!;). sele!t E f o$ student.

SQL-

81
NO 2 22 N(M' a % C "" Default !an not ove ide nulls5

"""""" """""""""

()STR(CT D(T( T*A'S


So$e ti$es 7ou $a7 want t7pe whi!h holds all t7pes of data in!luding nu$%e s# !ha s and spe!ial !ha a!te s so$ething li&e this5 *ou !an not a!hieve this using p e"defined t7pes5 *ou !an define !usto$ t7pes whi!h holds 7ou desi ed data5 '/0 Suppose in a ta%le we have add ess !olu$n whi!h holds hno and !it7 info $ation5 6e will define a !usto$ t7pe whi!h holds %oth nu$e i! as well as !ha data5
CR'(TIN8 (DT

SQL-

! eate t7pe add as o%Je!t(hno nu$%e (4)#!it7 va !ha (23)). ?

CR'(TIN8 T()L' )(S'D ON (DT

SQL-

! eate ta%le student(no nu$%e (1)#na$e va !ha (1)#add ess add ).

INS'RTIN8 D(T( INTO (DT T()L'S

SQLSQLSQL-

inse t into student values(2#=a=#add (222#=h7d=)). inse t into student values(1#=%=#add (111#=%ang=)). inse t into student values(4#=!=#add (444#=delhi=)).

82
S'L'CTIN8 D(T( BROM (DT T()L'S

SQL-

sele!t E f o$ student.

NO N(M' (DDR'SS(:NO# CIT*) """ """"""" """"""""""""""""""""""""" 2 1 4 a % ! (DDR(222# =h7d=) (DDR(111# =%ang=) (DDR(444# =delhi=)

SQL-

sele!t no#na$e#s5add ess5hno#s5add ess5!it7 f o$ student s.

NO N(M' (DDR'SS5:NO (DDR'SS5CIT* """" """"""" """"""""""""""""" """""""""""""""" 2 1 4 a % ! 222 111 444 h7d %ang delhi

UAD(T' 6IT: (DT T()L'S

SQLSQL-

update student s set s5add ess5!it7 H =%o$%a7= whe e s5add ess5hno H 444. sele!t no#na$e#s5add ess5hno#s5add ess5!it7 f o$ student s. NO N(M' (DDR'SS5:NO (DDR'SS5CIT* """" """"""" """"""""""""""""" """""""""""""""" 2 1 4 a % ! 222 111 444 h7d %ang %o$%a7

D'L'T' 6IT: (DT T()L'S

SQLSQL-

delete student s whe e s5add ess5hno H 222. sele!t no#na$e#s5add ess5hno#s5add ess5!it7 f o$ student s. NO N(M' (DDR'SS5:NO (DDR'SS5CIT* """" """"""" """"""""""""""""" """""""""""""""" 1 % 111 %ang

83
4
DROAAIN8 (DT

444

%o$%a7

SQL-

d op t7pe add .

O)>'CT 9I'6S (ND M'T:ODS


O)>'CT 9I'6S If 7ou want to i$ple$ent o%Je!ts with the e/isting ta%le# o%Je!t views !o$e into pi!tu e5 *ou define the o%Je!t and ! eate a view whi!h nothing %ut ob#ect view5 O%Je!t views a e used to elate the use defined o%Je!ts to the e/isting ta%le5 '/0 2) (ssu$e that the ta%le student has al ead7 %een ! eated with the following !olu$ns
SQL-

elates this o%Je!t to the e/isting ta%le

! eate ta%le student(no nu$%e (1)#na$e va !ha (23)#hno nu$%e (4)#!it7 va !ha (23)).

1) C eate the following t7pes


SQLSQL-

! eate t7pe add as o%Je!t(hno nu$%e (1)#!it7 va !ha (23)).? ! eate t7pe stud as o%Je!t(na$e va !ha (23)#add ess add ).? ! eate view studentMov(no#studMinfo) as sele!t no#stud(na$e#add (hno#!it7)) f o$ student.

4) Relate the o%Je!ts to the student ta%le %7 ! eating the o%Je!t view
SQL-

@) Now 7ou !an inse t data into student ta%le in two wa7s a) )7 egula inse t
SQL-

Inse t into student values(2#;sudha;#222#;h7d;). Inse t into studentMov values(2#stud(Ksudha;#add (222#;h7d;))).

%) )7 using o%Je!t view


SQL-

84

M'T:ODS *ou !an define $ethods whi!h a e nothing %ut fun!tions in t7pes and appl7 in the ta%les whi!h holds the t7pes. '/0 2) Defining $ethods in t7pes
SQL-

C eate t7pe stud as o%Je!t(na$e va !ha (23)#$a &s nu$%e (4)# Me$%e fun!tion $a& sMf($a &s in nu$%e ) etu n nu$%e # A ag$a est i!tM efe en!es($a &sMf#wnds# nds#wnps#fnps)).?

1) Defining t7pe %od7


SQL-

C eate t7pe %od7 stud as Me$%e fun!tion $a &sMf($a &s in nu$%e ) etu n nu$%e is )egin Retu n ($a &sG233). 'nd $a &sMf. 'nd.?

4) C eate a ta%le using stud t7pe


SQL-

C eate ta%le student(no nu$%e (1)#info stud). Inse t into student values(2#stud(Ksudha;#233)). Sele!t s5info5$a &sMf(s5info5$a &s) f o$ student s.

@) Inse t so$e data into student ta%le


SQL-

C) Using $ethod in sele!t


SQL-

"" :e e we a e using the p ag$a est i!tM efe en!es to avoid the w ites to the Data%ase5

85

9(RR(*S (ND N'ST'D T()L'S


9(RR(*S ( va 7ing a a7 allows 7ou to sto e epeating att i%utes of a e!o d in a single ow %ut with li$it5 '/0 2) 6e !an ! eate va a7s using o a!le t7pes as well as use defined t7pes5 a) 9a a7 using p e"defined t7pes
SQL-

C eate t7pe va as va a7(C) of va !ha (23).? C eate t7pe add as o%Je!t(hno nu$%e (4)#!it7 va !ha (23)).? C eate t7pe va as va a7(C) of add .?

%) 9a

a7s using use defined t7pes

SQLSQL-

1) Using va a7 in ta%le
SQL-

C eate ta%le student(no nu$%e (1)#na$e va !ha (23)#add ess va). Inse t into student values(2#;sudha;#va(add (222#;h7d;))). Inse t into student values(1#;Jagan;#va(add (222#;h7d;)#add (111#;%ang;))). Sele!t E f o$ student. Sele!t no#na$e# s5E f o$ student s2# ta%le(s25add ess) s.

4) Inse ting values into va a7 ta%le


SQLSQL-

@) Sele!ting data f o$ va a7 ta%le


SQL-

"" This will displa7 va a7 !olu$n data along with va a7 and adt.
SQL-

"" This will displa7 in gene al fo $at C) Instead of s5E 7ou !an spe!if7 the !olu$ns in va a7

86
SQL-

Sele!t no#na$e# s5hno#s5!it7 f o$ student s2#ta%le(s25add ess) s. a7s5

"" Update and delete not possi%le in va output e/!luding va a7 and t7pes5

"" :e e we used ta%le fun!tion whi!h will ta&e the va a7 !olu$n as input fo p odu!ing

N'ST'D T()L'S ( nested ta%le is# as its na$e i$plies# a ta%le within a ta%le5 In this !ase it is a ta%le that is ep esented as a !olu$n within anothe ta%le5 Nested ta%le has the sa$e effe!t of va a7s %ut has no li$it5 '/0 2) 6e !an ! eate nested ta%les using o a!le t7pes and use defined t7pes whi!h has no li$it5 a) Nested ta%les using p e"defined t7pes
SQL-

C eate t7pe nt as ta%le of va !ha (23).? C eate t7pe add as o%Je!t(hno nu$%e (4)#!it7 va !ha (23)).? C eate t7pe nt as ta%le of add .?

%) Nested ta%les using use defined t7pes


SQLSQL-

1) Using nested ta%le in ta%le


SQL-

C eate ta%le student(no nu$%e (1)#na$e va !ha (23)#add ess nt) nested ta%le add ess sto e as studentMte$p.

4) Inse ting values into ta%le whi!h has nested ta%le


SQLSQL-

Inse t into student values (2#;sudha;#nt(add (222#;h7d;))). Inse t into student values (1#;Jagan;#nt(add (222#;h7d;)#add (111#;%ang;))). Sele!t E f o$ student. Sele!t no#na$e# s5E f o$ student s2# ta%le(s25add ess) s.

@) Sele!ting data f o$ ta%le whi!h has nested ta%le


SQL-

"" This will displa7 nested ta%le !olu$n data along with nested ta%le and adt.
SQL-

"" This will displa7 in gene al fo $at C) Instead of s5E 7ou !an spe!if7 the !olu$ns in nested ta%le

87
SQL-

Sele!t no#na$e# s5hno#s5!it7 f o$ student s2#ta%le(s25add ess) s. Inse t into ta%le(sele!t add ess f o$ student whe e noH2) values(add (CCC#;!hennai;)).

D) Inse ting nested ta%le data to the e/isting ow


SQL-

V) Update in nested ta%les


SQL-

Update ta%le(sele!t add ess f o$ student whe e noH1) s set s5!it7H;%o$%a7; whe e s5hno H 111.

W) Delete in nested ta%le


SQL-

Delete ta%le(sele!t add ess f o$ student whe e noH4) s whe e s5hnoH444.

D(T( MOD'L (LLMCOLLMT*A'S (LLMT*A'S D)(MCOLLMT*A'S D)(MT*A'S US'RMCOLLMT*A'S US'RMT*A'S

88

BL(S:)(CF QU'R*
Used to et ieve the data whi!h has %een al ead7 !o$$itted with out going fo Blash%a!&s a e of two t7pes '/0 2) Using ti$e %ased flash%a!& a) %) !) d)
SQL-

e!ove 75

Ti$e %ase flash%a!& SCN %ased flash%a!& (SCN stands fo S7ste$ Change Nu$%e )

Sele!t Ef o$ student. Delete student. Co$$it. "" this will !o$$it the wo &5 Sele!t Ef o$ student.

"" This will displa7 all the ows


SQLSQLSQL-

"" :e e it will displa7 nothing e) Then e/e!ute the following p o!edu es


SQL-

'/e! d%$sMflash%a!&5ena%leMatMti$e(s7sdate"1?2@@3) Sele!t Ef o$ student.

f)

SQL-

"" :e e it will displa7 the lost data "" The lost data will !o$e %ut the !u ent s7ste$ ti$e was used g)
SQL-

'/e! d%$sMflash%a!&5disa%le

"" :e e we have to disa%le the flash%a!& to ena%le it again

89
1) Using SCN %ased flash%a!& a) De!la e a va ia%le to sto e SCN
SQL-

9a ia%le s nu$%e '/e! 0s 0H e/e! d%$sMflash%a!&5getMs7ste$M!hangeMnu$%e

%) 8et the SCN


SQL-

!) To see the SCN


SQL-

A int s '/e! d%$sMflash%a!&5ena%leMatMs7ste$M!hangeMnu$%e (0s) '/e! d%$sMflash%a!&5disa%le

d) Then e/e!ute the following p o!edu es


SQLSQL-

'+T'RN(L T()L'S
*ou !an use e/te nal ta%le featu e to a!!ess e/te nal files as if the7 a e ta%les inside the data%ase5 6hen 7ou ! eate an e/te nal ta%le# 7ou define its st u!tu e and lo!ation with in o a!le5 6hen 7ou Uue 7 the ta%le# o a!le eads the e/te nal ta%le and etu ns the esults Just as if the data had %een sto ed with in the data%ase5 (CC'SSIN8 '+T'RN(L T()L' D(T( To a!!ess e/te nal files f o$ within o a!le# 7ou $ust fi st use the ! eate di e!to 7 !o$$and to define a di e!to 7 o%Je!t pointing to the e/te nal file lo!ation Use s who will a!!ess the e/te nal files $ust have the di e!to 75 '/0
CR'(TIN8 DIR'CTOR* (ND OS L'9'L BIL'

ead and w ite p ivilege on the

SQLSQLSQLSQLSQLSQLSQL-

SUlplus s7ste$?$anage C eate di e!to 7 sa&ethMdi as K?9isd%?visd%?S5153?e/te nal;. 8 ant all on di e!to 7 sa&ethMdi to sa&eth. Conn sa&eth?sa&eth Spool dept5lst Sele!t deptno XX K#; XX dna$e XX K#; XX lo! f o$ dept. Spool off

90

CR'(TIN8 '+T'RN(L T()L'

SQL-

C eate ta%le deptMe/t (deptno nu$%e (1)# Dna$e va !ha (2@)# Lo! va !ha (24)) O ganiTation e/te nal ( t7pe o a!leMloade Default di e!to 7 sa&ethMdi (!!ess pa a$ete s ( e!o ds deli$ited %7 newline Bields te $inated %7 P#Q ( deptno nu$%e (1)# Dna$e va !ha (2@)# Lo! va !ha (24))) Lo!ation (K?9isd%?visd%?S5153?dept5lst;)).

S'L'CTIN8 D(T( BROM '+T'RN(L T()L'

SQL-

sele!t E f o$ deptMe/t.

This will ead f o$ dept5lst whi!h is a ope ating s7ste$ level file5
LIMIT(TIONS ON '+T'RN(L T()L'S

a) *ou !an not pe fo $ inse t# update# and delete ope ations a) Inde/ing not possi%le %) Const aints not possi%le
)'N'BITS OB '+T'RN(L T()L'S

a) Que ies of e/te nal ta%les !o$plete ve 7 Uui!&l7 even though a full ta%le s!an id eUui ed with ea!h a!!ess %) *ou !an Join e/te nal ta%les to ea!h othe o to standa d ta%les

91

R'B D'R'B 9(LU'


R'B The ef fun!tion allows efe en!ing of e/isting ow o%Je!ts5 'a!h of the ow o%Je!ts has an o%Je!t id value assigned to it5 The o%Je!t id assigned !an %e seen %7 using ef fun!tion5

D'R'B The de ef fun!tion pe fo $s opposite a!tion5 It ta&es a efe en!e value of o%Je!t id and etu ns the value of the ow o%Je!ts5

9(LU' 'ven though the p i$a 7 ta%le is o%Je!t ta%le# still it displa7s the ows in gene al fo $at5 '/0 2) ! eate vendotMadt t7pe
SQL-

To displa7 the enti e st u!tu e of the o%Je!t# this will %e used5

C eate t7pe vendo Madt as o%Je!t (vendo M!ode nu$%e (1)# vendo Mna$e va !ha (1)# vendo Madd ess va !ha (23)).?

1) ! eate o%Je!t ta%les vendo s and vendo s2


SQL-

C eate ta%le vendo s of vendo Madt.

92
SQL-

C eate ta%le vendo s2 of vendo Madt. inse t into vendo s values(2# Ka;# Kh7d;). inse t into vendo s values(1# K%;# K%ang;). inse t into vendo s2 values(4# K!;# Kdelhi;). inse t into vendo s2 values(@# Kd;# K!hennai;). C eate ta%le o de s (o de Mno nu$%e (1)# vendo Minfo ef vendo Madt). O C eate ta%le o de s (o de Mno nu$%e (1)# vendo Minfo ef vendo Madt with owid).

4) inse t the data into o%Je!t ta%les


SQLSQLSQLSQL-

@) ! eate anothe ta%le o de s whi!h holds the vendo Madt t7pe also5
SQL-

SQL-

C) inse t the data into o de s ta%le The vendo Minfo !olu$n in the following s7nta/es will sto e o%Je!t id of an7 ta%le whi!h is efe en!ed %7 vendo Madt o%Je!t ( %oth vendo s and vendo s2)5
SQL-

inse t into o de s values(22#(sele!t ef(v) f o$ vendo s v whe e vendo M!ode H 2)). inse t into o de s values(21#(sele!t ef(v) f o$ vendo s v whe e vendo M!ode inse t into o de s values(24#(sele!t ef(v2) f o$ vendo s2 v2 whe e vendo M!ode H 2)). inse t into o de s values(2@#(sele!t ef(v2) f o$ vendo s2 v2 whe e vendo M!ode H 2)).

SQL-

H 1)).
SQL-

SQL-

D) To see the o%Je!t ids of vendo ta%le


SQL-

Sele!t ef(9) f o$ vendo s v.

V) If 7ou see the vendo Minfo of o de s it will show onl7 the o%Je!t ids not the values# to see the values
SQL-

Sele!t de ef(o5vendo Minfo) f o$ o de s o.

W) 'ven though the vendo s ta%le is o%Je!t ta%le it will not show the adt along with data# to see the data along with the adt
SQL-Sele!t

E f o$ vendo s. value(v) f o$ vendo s v.

This will give the data without adt5


SQL-Sele!t

This will give the !olu$ns data along wih the t7pe5

93
R'B CONSTR(INTS Ref !an also a!ts as !onst aint5 'ven though vendo s2 also holding vendo Madt# the o de s ta%le will sto e the o%Je!t ids of vendo s onl7 %e!ause it is !onst ained to that ta%le onl75 The vendo Minfo !olu$n in the following s7nta/es will sto e o%Je!t ids of vendo s onl75

SQL-

C eate ta%le o de s (o de Mno nu$%e (1)# vendo Minfo ef vendo Madt s!ope is vendo s). O C eate ta%le o de s (o de Mno nu$%e (1)# vendo Minfo ef vendo Madt !onst aint f& efe en!es vendo s).

SQL-

94

O)>'CT 9I'6S 6IT: R'B'R'NC'S


To i$ple$ent the o%Je!ts and the ef !onst aints to the e/isting ta%les# what we !an do[ Si$pl7 d op the %oth ta%les and e! eate with o%Je!ts and ef !onst ains5 )ut 7ou !an a!hieve this with out d opping the ta%les and without losing the data %7 ! eating o%Je!t views with efe en!es5 '/0 a) C eate the following ta%les
SQL-

C eate ta%le student2(no nu$%e (1) p i$a 7 &e7#na$e va !ha (1)#$a &s nu$%e (4)). C eate ta%le student1(no nu$%e (1) p i$a 7 &e7#hno nu$%e (4)#!it7 va !ha (23)#id nu$%e (1)#fo eign Fe7(id) efe en!es student2(no)).

SQL-

%) Inse t the e!o ds into %oth ta%les


SQLSQLSQLSQLSQL-

inse t into student2(2#;a;#233). inse t into student2(1#;%;#133). inse t into student1(22#222#;h7d;#2). inse t into student1(21#111#;%ang;#1). inse t into student1(24#444#;%o$%a7;#2). epla!e t7pe stud as o%Je!t(no nu$%e (1)#na$e va !ha (1)#$a &s

!) C eate the t7pe


SQL-

! eate o

nu$%e (4)).? d) 8ene ating OIDs


SQL-

C eate o

epla!e view student2Mov of stud with o%Je!t identifie (o id) (no) as

95
Sele!t E f o$ Student2. e) 8ene ating efe en!es
SQL-

C eate o

epla!e view student1Mov as sele!t no#hno#!it7#

$a&eM ef(student2Mov#id) id f o$ Student1. d) Que 7 the following


SQLSQLSQLSQ-

sele!t Ef o$ student2Mov. sele!t ef(s) f o$ student2Mov s. sele!t values(s) f o$ student2Mov. sele!t de ef(s5id) f o$ student1Mov s.

sele!t Ef o$ student1Mov.

SQL-

A(RTITIONS
( single logi!al ta%le !an %e split into a nu$%e of ph7si!all7 sepa ate pie!es %ased on anges of &e7 values5 'a!h of the pa ts of the ta%le is !alled a pa tition5 ( non"pa titioned ta%le !an not %e pa titioned late 5 T*A'S Range pa titions List pa titions :ash pa titions Su% pa titions

(D9(NT(8'S Redu!ing downti$e fo s!heduled $aintenan!e# whi!h allows $aintenan!e pa titions a e

ope ations to %e !a ied out on sele!ted pa titions while othe availa%le to use s5

Redu!ing downti$e due to data failu e# failu e of a pa ti!ula pa tition will no wa7 affe!t othe pa titions5 Aa tition independen!e allows fo va ious pu poses5 !on!u ent use of the va ious pa titions fo

(D9(NT(8'S OB A(RTITIONS )* STORIN8 T:'M IN DIBB'R'NT T()L'SA(C'S

96
Redu!es the possi%ilit7 of data !o uption in $ultiple pa titions5 )a!& up and e!ove 7 of ea!h pa tition !an %e done independentl75

DIS(D9(NT(8'S Aa titioned ta%les !annot !ontain an7 !olu$ns with long o LO) t7pes o o%Je!t t7pes5 long aw datat7pes#

R(N8' A(RTITIONS a) C eating ange pa titioned ta%le


SQL-

C eate ta%le student(no nu$%e (1)#na$e va !ha (1)) pa tition %7 ange(no) (pa tition p2 values less than(23)# pa tition p1 values less than(13)# pa tition p4 values less than(43)#pa tition p@ values less than($a/value)).

EE if 7ou a e using $a/value fo the last pa tition# 7ou !an not add a pa tition5 %) Inse ting e!o ds into ange pa titioned ta%le
SQLSQLSQLSQL-

Inse t into student values(2#;a;). Inse t into student values(22#;%;). Inse t into student values(12#;!;). Inse t into student values(42#;d;). Sele!t Ef o$ student. Sele!t Ef o$ student pa tition(p2). (dd D op T un!ate Rena$e Split Move '/!hange

"" this will go to p2 "" this will go to p1 "" this will go to p4 "" this will go to p@

!) Ret ieving e!o ds f o$ ange pa titioned ta%le


SQLSQL-

d) Aossi%le ope ations with ange pa titions

e) (dding a pa tition

97
SQL-

(lte ta%le student add pa tition pC values less than(@3).

f) D opping a pa tition
SQL-

(lte ta%le student d op pa tition p@. (lte ta%le student ena$e pa tition p4 to pD. (lte ta%le student t un!ate pa tition pD.

g) Rena$ing a pa tition
SQL-

h) T un!ate a pa tition
SQL-

i) Splitting a pa tition
SQL-

(lte ta%le student split pa tition p1 at(2C) into (pa tition p12#pa tition p11).

J) '/!hanging a pa tition
SQL-

(lte ta%le student e/!hange pa tition p2 with ta%le student1. (lte ta%le student $ove pa tition p12 ta%lespa!e sa&ethMts.

&) Moving a pa tition


SQL-

LIST A(RTITIONS a) C eating list pa titioned ta%le


SQL-

C eate ta%le student(no nu$%e (1)#na$e va !ha (1)) pa tition %7 list(no) (pa tition p2 values(2#1#4#@#C)# pa tition p1 values(D#V#W#S#23)#pa tition p4 values(22#21#24#2@#2C)# pa tition p@ values(2D#2V#2W#2S#13)).

%) Inse ting e!o ds into list pa titioned ta%le


SQLSQLSQLSQL-

Inse t into student values(2#;a;). Inse t into student values(D#;%;). Inse t into student values(22#;!;). Inse t into student values(2D#;d;).

"" this will go to p2 "" this will go to p1 "" this will go to p4 "" this will go to p@

!) Ret ieving e!o ds f o$ list pa titioned ta%le


SQLSQL-

Sele!t Ef o$ student. Sele!t Ef o$ student pa tition(p2). (dd D op T un!ate Rena$e Move

d) Aossi%le ope ations with list pa titions

98
'/!hange e) (dding a pa tition
SQL-

(lte ta%le student add pa tition pC values(12#11#14#1@#1C). (lte ta%le student d op pa tition p@. (lte ta%le student ena$e pa tition p4 to pD. (lte ta%le student t un!ate pa tition pD.

f) D opping a pa tition
SQL-

g) Rena$ing a pa tition
SQL-

h) T un!ate a pa tition
SQL-

i) '/!hanging a pa tition
SQL-

(lte ta%le student e/!hange pa tition p2 with ta%le student1. (lte ta%le student $ove pa tition p1 ta%lespa!e sa&ethMts.

J) Moving a pa tition
SQL-

:(S: A(RTITIONS a) C eating hash pa titioned ta%le


SQL-

C eate ta%le student(no nu$%e (1)#na$e va !ha (1)) pa tition %7 hash(no) pa titions C.

:e e o a!le auto$ati!all7 gives pa tition na$es li&e S*SMA2 S*SMA1 S*SMA4 S*SMA@ S*SMAC %) Inse ting e!o ds into hash pa titioned ta%le it will inse t the e!o ds %ased on hash fun!tion !al!ulated %7 ta&ing the pa tition &e7
SQLSQLSQLSQL-

Inse t into student values(2#;a;). Inse t into student values(D#;%;). Inse t into student values(22#;!;). Inse t into student values(2D#;d;). Sele!t Ef o$ student. Sele!t Ef o$ student pa tition(s7sMp2).

!) Ret ieving e!o ds f o$ hash pa titioned ta%le


SQLSQL-

99
d) Aossi%le ope ations with hash pa titions (dd T un!ate Rena$e Move '/!hange e) (dding a pa tition
SQL-

(lte ta%le student add pa tition pD .

f) Rena$ing a pa tition
SQL-

(lte ta%le student ena$e pa tition pD to pV. (lte ta%le student t un!ate pa tition pV. (lte ta%le student e/!hange pa tition s7sMp2 with ta%le student1.

g) T un!ate a pa tition
SQL-

h) '/!hanging a pa tition
SQL-

i) Moving a pa tition
SQL-

(lte ta%le student $ove pa tition s7sMp1 ta%lespa!e sa&ethMts.

SU)"A(RTITIONS 6IT: R(N8' (ND :(S: Su%pa titions !lause is used %7 hash onl75 6e !an not ! eate su%pa titions with list and hash pa titions5 a) C eating su%pa titioned ta%le
SQL-

C eate ta%le student(no nu$%e (1)#na$e va !ha (1)#$a &s nu$%e (4)) Aa tition %7 ange(no) su%pa tition %7 hash(na$e) su%pa titions 4 (Aa tition p2 values less than(23)#pa tition p1 values less than(13)).

This will ! eate two pa titions p2 and p1 with th ee su%pa titions fo ea!h pa tition A2 \ S*SMSU)A2 S*SMSU)A1 S*SMSU)A4 A1 \ S*SMSU)A@ S*SMSU)AC S*SMSU)AD EE if 7ou a e using $a/value fo the last pa tition# 7ou !an not add a pa tition5 %) Inse ting e!o ds into su%pa titioned ta%le

100
SQLSQL-

Inse t into student values(2#;a;). Inse t into student values(22#;%;). Sele!t Ef o$ student. Sele!t Ef o$ student pa tition(p2).

"" this will go to p2 "" this will go to p1

!) Ret ieving e!o ds f o$ su%pa titioned ta%le


SQLSQLSQL-

Sele!t Ef o$ student su%pa tition(s7sMsu%p2). (dd D op T un!ate Rena$e Split

d) Aossi%le ope ations with su%pa titions

e) (dding a pa tition
SQL-

(lte ta%le student add pa tition p4 values less than(43). (lte ta%le student d op pa tition p4. (lte ta%le student ena$e pa tition p1 to p4. (lte ta%le student t un!ate pa tition p2. (lte ta%le student split pa tition p4 at(2C) into (pa tition p42#pa tition p41).

f) D opping a pa tition
SQL-

g) Rena$ing a pa tition
SQL-

h) T un!ate a pa tition
SQL-

i) Splitting a pa tition
SQL-

D(T( MOD'L (LLMINDMA(RTITIONS (LLMINDMSU)A(RTITIONS (LLMT()MA(RTITIONS (LLMT()MSU)A(RTITIONS D)(MINDMA(RTITIONS D)(MINDMSU)A(RTITIONS D)(MT()MA(RTITIONS D)(MT()MSU)A(RTITIONS US'RMINDMA(RTITIONS US'RMINDMSU)A(RTITIONS

101
US'RMT()MA(RTITIONS US'RMT()MSU)A(RTITIONS

8ROUA )* (ND :(9IN8


8ROUA )* Using g oup %7# we !an ! eate g oups of elated info $ation5 Colu$ns used in sele!t $ust %e used with g oup %7# othe wise it was not a g oup %7 e/p ession5 '/0
SQL-

sele!t deptno# su$(sal) f o$ e$p g oup %7 deptno. D'ATNO 23 13 43 SUM(S(L) WVC3 23WVC S@33

"""""""""" """"""""""

SQL-

sele!t deptno#Jo%#su$(sal) f o$ e$p g oup %7 deptno#Jo%. D'ATNO >O) """""""""" """"""""" 23 23 23 CL'RF M(N(8'R AR'SID'NT SUM(S(L) """""""""" 2433 1@C3 C333

102
13 13 13 43 43 43 :(9IN8 This will wo & as whe e !lause whi!h !an %e used onl7 with g oup %7 %e!ause of a%sen!e of whe e !lause in g oup %75 '/0
SQL-

(N(L*ST CL'RF M(N(8'R CL'RF M(N(8'R S(L'SM(N

D333 2S33 1SVC SC3 1WC3 CD33

sele!t deptno#Jo%#su$(sal) tsal f o$ e$p g oup %7 deptno#Jo% having su$(sal) 4333. D'ATNO 23 13 43 >O) TS(L """""""""" C333 D333 CD33

"""""""""" """"""""" AR'SID'NT (N(L*ST S(L'SM(N

SQL-

sele!t deptno#Jo%#su$(sal) tsal f o$ e$p g oup %7 deptno#Jo% having su$(sal) 4333 o de %7 Jo%. D'ATNO 13 23 43 >O) (N(L*ST AR'SID'NT S(L'SM(N TS(L """""""""" D333 C333 CD33

"""""""""" """""""""

ORD'R OB '+'CUTION 8 oup the ows togethe %ased on g oup %7 !lause5 Cal!ulate the g oup fun!tions fo ea!h g oup5 Choose and eli$inate the g oups %ased on the having !lause5

103
O de the g oups %ased on the spe!ified !olu$n5

ROLLUA 8ROUAIN8 CU)'

These a e the enhan!e$ents to the g oup %7 featu e5 USIN8 ROLLUA This will give the sala ies in ea!h depa t$ent in ea!h Jo% !atego 7 along wih the total sala 7 fo individual depa t$ents and the total sala 7 of all the depa t$ents5
SQL-

Sele!t deptno#Jo%#su$(sal) f o$ e$p g oup %7 ollup(deptno#Jo%). D'ATNO 23 23 23 23 13 13 13 13 43 43 CL'RF M(N(8'R (N(L*ST CL'RF M(N(8'R >O) CL'RF M(N(8'R AR'SID'NT SUM(S(L) """""""""" 2433 1@C3 C333 WVC3 D333 2S33 1SVC 23WVC SC3 1WC3

"""""""""" """""""""

104
43 43 S(L'SM(N CD33 S@33 1S31C

USIN8 8ROUAIN8 In the a%ove Uue 7 it will give the total sala 7 of the individual depa t$ents %ut with a %lan& in the Jo% !olu$n and gives the total sala 7 of all the depa t$ents with %lan&s in deptno and Jo% !olu$ns5 To epla!e these %lan&s with 7ou desi ed st ing g ouping will %e used sele!t de!ode(g ouping(deptno)#2#=(ll Depts=#deptno)#de!ode(g ouping(Jo%)#2#=(ll Jo%s=#Jo%)#su$(sal) f o$ e$p g oup %7 ollup(deptno#Jo%). D'COD'(8ROUAIN8(D'ATNO)#2#=(LLD'ATS=#D'A D'COD'(8R """"""""""""""""""""""""""""""""""" """""""""""""""""""""""""""""""""" 23 23 23 23 13 13 13 13 43 43 43 43 (ll Depts CL'RF M(N(8'R AR'SID'NT (ll Jo%s (N(L*ST CL'RF M(N(8'R (ll Jo%s CL'RF M(N(8'R S(L'SM(N (ll Jo%s (ll Jo%s SUM(S(L) """""""""""""" 2433 1@C3 C333 WVC3 D333 2S33 1SVC 23WVC SC3 1WC3 CD33 S@33 1S31C

SQL-

8 ouping will etu n 2 if the !olu$n whi!h is spe!ified in the g ouping fun!tion has %een used in ollup5 8 ouping will %e used in asso!iation with de!ode5

105

USIN8 CU)' This will give the sala ies in ea!h depa t$ent in ea!h Jo% !atego 7# the total sala 7 fo individual depa t$ents# the total sala 7 of all the depa t$ents and the sala ies in ea!h Jo% !atego 75 sele!t de!ode(g ouping(deptno)#2#;(ll Depts;#deptno)#de!ode(g ouping(Jo%)#2#;(ll >o%s;#Jo%)#su$(sal) f o$ e$p g oup %7 !u%e(deptno#Jo%).

SQL-

D'COD'(8ROUAIN8(D'ATNO)#2#=(LLD'ATS=#D'A D'COD'(8R 23 23 23 23 13 13 13 13 43 43 43 43 (ll Depts (ll Depts (ll Depts (ll Depts (ll Depts (ll Depts CL'RF M(N(8'R AR'SID'NT (ll >o%s (N(L*ST CL'RF M(N(8'R (ll >o%s CL'RF M(N(8'R S(L'SM(N (ll >o%s (N(L*ST CL'RF M(N(8'R AR'SID'NT S(L'SM(N (ll >o%s

SUM(S(L) 2433 1@C3 C333 WVC3 D333 2S33 1SVC 23WVC SC3 1WC3 CD33 S@33 D333 @2C3 W1VC C333 CD33 1S31C

""""""""""""""""""""""""""""""""""" """""""""""""""""""""""""""""""""""" """"""""""""

106

S'T OA'R(TORS
T*A'S Union Union all Inte se!t Minus

UNION

This will !o$%ine the e!o ds of $ultiple ta%les having the sa$e st u!tu e5 '/0
SQL-

sele!t E f o$ student2 union sele!t E f o$ student1.

UNION (LL This will !o$%ine the e!o ds of $ultiple ta%les having the sa$e st u!tu e %ut in!luding dupli!ates5 '/0

107
SQL-

sele!t E f o$ student2 union all sele!t E f o$ student1.

INT'RS'CT This will give the !o$$on e!o ds of $ultiple ta%les having the sa$e st u!tu e5 '/0
SQL-

sele!t E f o$ student2 inte se!t sele!t E f o$ student1.

MINUS This will give the e!o ds of a ta%le whose e!o ds a e not in othe ta%les having the sa$e st u!tu e5 '/0
SQL-

sele!t E f o$ student2 $inus sele!t E f o$ student1.

108

9I'6S
( view is a data%ase o%Je!t that is a logi!al ep esentation of a ta%le5 It is delive ed f o$ a ta%le %ut has no sto age of its own and often $a7 %e used in the sa$e $anne as a ta%le5 ( view ta&es the output of the Uue 7 and t eats it as a ta%le# the efo e a view !an %e thought of as a sto ed Uue 7 o a vi tual ta%le5 T*A'S Si$ple view Co$ple/ view

Si$ple view !an %e ! eated f o$ one ta%le whe e as !o$ple/ view !an %e ! eated f o$ $ultiple ta%les5 6:* 9I'6S[ A ovides additional level of se!u it7 %7 est i!ting a!!ess to a p edete $ined set of ows and?o !olu$ns of a ta%le5 :ide the data !o$ple/it75 Si$plif7 !o$$ands fo the use 5

109

9I'6S 6IT:OUT DML Read onl7 view 9iew with g oup %7 9iew with agg egate fun!tions 9iew with ownu$ Aa tition view 9iew with distin!t

'/0
SQLSQLSQLSQLSQL-

C eate view deptMv as sele!t Ef o$ dept with ead onl7. C eate view deptMv as sele!t deptno# su$(sal) tMsal f o$ e$p g oup %7 deptno. C eate view stud as sele!t ownu$ no# na$e# $a &s f o$ student. C eate view student as sele!t Ef o$ student2 union sele!t Ef o$ student1. C eate view stud as sele!t distin!t no#na$e f o$ student.

9I'6S 6IT: DML 9iew with not null !olu$n "" inse t with out not null !olu$n not possi%le "" update not null !olu$n to null is not possi%le "" delete possi%le 9iew with out not null !olu$n whi!h was in %ase ta%le "" inse t not possi%le "" update# delete possi%le 9iew with e/p ession "" inse t # update not possi%le "" delete possi%le 9iew with fun!tions (e/!ept agg egate) "" inse t# update not possi%le "" delete possi%le 9iew was ! eated %ut the unde l7ing ta%le was d opped then we will get the $essage li&e P view has e o s Q5 9iew was ! eated %ut the %ase ta%le has %een alte ed %ut still the view was with the initial definition# we have to epla!e the view to affe!t the !hanges5 Co$ple/ view (view with $o e than one ta%le) "" inse t not possi%le "" update# delete possi%le (not alwa7s)

110
CR'(TIN8 9I'6 6IT:OUT :(9IN8 T:' )(S' T()L' C eate fo !e view stud as sele!t EB o$ student.

SQL-

"" On!e the %ase ta%le was ! eated then the view is validated5 9I'6 6IT: C:'CF OATION CONSTR(INT C eate view stud as sele!t Ef o$ student whe e $a &s H C33 with !he!& option !onst aint C&. " Inse t possi%le with $a &s value as C33 " Update possi%le e/!luding $a &s !olu$n " Delete possi%le DROAAIN8 9I'6S d op view deptMv.

SQL-

SQL-

D(T( MOD'L (LLM9I'6 D)(M9I'6 US'RM9I'6S

111

S*NON*M (ND S'QU'NC'


S*NON*M ( s7non7$ is a data%ase o%Je!t# whi!h is used as an alias fo a ta%le# view o seUuen!e5
T*A'S

A ivate Au%li!

A ivate s7non7$ is availa%le to the pa ti!ula use who ! eates5 Au%li! s7non7$ is ! eated %7 D)( whi!h is availa%le to all the use s5
(D9(NT(8'S

:ide the na$e and owne of the o%Je!t5 A ovides lo!ation t anspa en!7 fo e$ote o%Je!ts of a dist i%uted data%ase5

CR'(T' (ND DROA

SQLSQLSQL-

! eate s7non7$ s2 fo e$p. ! eate pu%li! s7non7$ s1 fo e$p. d op s7non7$ s2.

112
S'QU'NC' ( seUuen!e is a data%ase o%Je!t# whi!h !an gene ate uniUue# seUuential intege values5 It !an %e used to auto$ati!all7 gene ate p i$a 7 &e7 o uniUue &e7 values5 ( seUuen!e !an %e eithe in an as!ending o des!ending o de 5 S7nta/0 C eate seUuen!e ,!e._name- Nin! e$ent %t7 nO Nsta t with nO N$a/value nO N$invalue nO N!7!le?no!7!leO N!a!he?no!a!heO. )7 defalult the seUuen!e sta ts with 2# in! e$ents %7 2 with $invalue of 2 and with no!7!le# no!a!he5 Ca!he option p e"alloo!ates a set of seUuen!e nu$%e s and etains the$ in $e$o 7 fo faste a!!ess5 '/0
SQLSQL-

! eate seUuen!e s. ! eate seUuen!e s in! e$ent %7 23 sta t with 233 $invalue C $a/value 133 !7!le !a!he 13.

USIN8 S'QU'NC'

SQLSQL-

! eate ta%le student(no nu$%e (1)#na$e va !ha (23)). inse t into student values(s5ne/tval# Ksa&eth;). Initiall7 !u val is not defined and ne/tval is sta ting value5 (fte that ne/tval and !u val a e alwa7s eUual5

CR'(TIN8 (LA:("NUM'RIC S'QU'NC'

SQLSQL-

! eate seUuen!e s sta t with 22214@. Inse t into student values (s5ne/tval XX t anslate (s5ne/tval#;214@CDVWS3;#;a%!defghiJ;)).

(LT'RIN8 S'QU'NC'

113
6e !an alte the seUuen!e to pe fo $ the following5 '/0
SQLSQLSQL-

Set o eli$inate $invalue o $a/value5 Change the in! e$ent value5 Change the nu$%e of !a!hed seUuen!e nu$%e s5

alte seUuen!e s $invalue C. alte seUuen!e s in! e$ent %7 1. alte seUuen!e s !a!he 23.

DROAAIN8 S'QU'NC'

SQL-

d op seUuen!e s.

>OINS
The pu pose of a Join is to !o$%ine the data a! oss ta%les5 ( Join is a!tuall7 pe fo $ed %7 the whe e !lause whi!h !o$%ines the spe!ified ows of ta%les5 If a Join involves in $o e than two ta%les then o a!le Joins fi st two ta%les %ased on the Joins !ondition and then !o$pa es the esult with the ne/t ta%le and so on5 T*A'S 'Uui Join Non"eUui Join Self Join Natu al Join C oss Join Oute Join Left oute Right oute Bull oute

Inne Join Using !lause On !lause

114
(ssu$e that we have the following ta%les5 sele!t E f o$ dept. LOC h7d %ang %o$%a7

SQL-

D'ATNO DN(M' 23 13 43 $&t fin h

"""""" """""""""" """"""""""

SQL-

sele!t E f o$ e$p. 'N(M' sa&eth sudha Jagan $adhu >O) anal7st !le & $anage enginee M8R D'ATNO @@@ 444 222 111 23 13 23 @3

'MANO 222 111 444 @@@ 'QUI >OIN

"""""""""" """""""""" """""""""" """""""""" """"""""""

( Join whi!h !ontains an KH; ope ato in the Joins !ondition5 '/0
SQL-

sele!t e$pno#ena$e#Jo%#dna$e#lo! f o$ e$p e#dept d whe e e5deptnoHd5deptno. 'MANO 222 444 111 'N(M' sa&eth Jagan sudha >O) anal7st $anage !le & DN(M' $&t $&t fin LOC h7d h7d %ang

"""""""""" """""""""" """""""""" """""""""" """"""""""

115
USIN8 CL(US' sele!t e$pno#ena$e#Jo% #dna$e#lo! f o$ e$p e Join dept d using(deptno). 'MANO 222 444 111 ON CL(US' sele!t e$pno#ena$e#Jo%#dna$e#lo! f o$ e$p e Join dept d on(e5deptnoHd5deptno). 'MANO 222 444 111 NON"'QUI >OIN ( Join whi!h !ontains an ope ato othe than KH; in the Joins !ondition5 '/0
SQL-

SQL-

'N(M' sa&eth Jagan sudha

>O) anal7st

DN(M' $&t $&t fin

LOC h7d h7d %ang

"""""""""" """""""""" """""""""" """""""""" """""""""" $anage !le &

SQL-

'N(M' sa&eth Jagan sudha

>O) anal7st

DN(M' $&t $&t fin

LOC h7d h7d %ang

"""""""""" """""""""" """""""""" """""""""" """""""""" $anage !le &

sele!t e$pno#ena$e#Jo%#dna$e#lo! f o$ e$p e#dept d whe e e5deptno d5deptno. 'MANO 111 @@@ @@@ @@@ 'N(M' sudha $adhu $adhu $adhu >O) !le & enginee enginee enginee DN(M' $&t $&t fin h LOC h7d h7d %ang %o$%a7

"""""""""" """""""""" """""""""" """""""""" """"""""""

S'LB >OIN

116

>oining the ta%le itself is !alled self Join5 '/0


SQL-

sele!t e25e$pno#e15ena$e#e25Jo%#e15deptno f o$ e$p e2#e$p e1 whe e e25e$pnoHe15$g . 'MANO 222 111 444 @@@ 'N(M' Jagan $adhu sudha sa&eth >O) anal7st !le & $anage enginee D'ATNO 23 @3 13 23

"""""""""" """""""""" """""""""" """"""""""

N(TUR(L >OIN Natu al Join !o$pa es all the !o$$on !olu$ns5 '/0
SQL-

sele!t e$pno#ena$e#Jo%#dna$e#lo! f o$ e$p natu al Join dept. 'MANO 222 444 111 'N(M' sa&eth Jagan sudha >O) anal7st $anage !le & DN(M' $&t $&t fin LOC h7d h7d %ang

"""""""""" """""""""" """""""""" """""""""" """"""""""

CROSS >OIN This will gives the ! oss p odu!t5 '/0


SQL-

sele!t e$pno#ena$e#Jo%#dna$e#lo! f o$ e$p ! oss Join dept. 'MANO 'N(M' >O) DN(M' LOC

"""""""""" """""""""" """""""""" """""""""" """"""""""

117
222 111 444 @@@ 222 111 444 @@@ 222 111 444 @@@ OUT'R >OIN Oute Join gives the non"$at!hing e!o ds along with $at!hing e!o ds5 sa&eth sudha Jagan $adhu sa&eth sudha Jagan $adhu sa&eth sudha Jagan $adhu anal7st !le & $anage enginee anal7st !le & $anage enginee anal7st !le & $anage enginee $&t $&t $&t $&t fin fin fin fin h h h h h7d h7d h7d h7d %ang %ang %ang %ang %o$%a7 %o$%a7 %o$%a7 %o$%a7

L'BT OUT'R >OIN

This will displa7 the all $at!hing e!o ds and the e!o ds whi!h a e in left hand side ta%le those that a e not in ight hand side ta%le5 '/0
SQL-

sele!t e$pno#ena$e#Jo%#dna$e#lo! f o$ e$p e left oute Join dept d on(e5deptnoHd5deptno). O sele!t e$pno#ena$e#Jo%#dna$e#lo! f o$ e$p e#dept d whe e e5deptnoHd5deptno(G). 'MANO 222 444 111 @@@ 'N(M' sa&eth Jagan sudha $adhu >O) anal7st $anage !le & enginee DN(M' $&t $&t fin LOC h7d h7d %ang

SQL-

"""""""""" """""""""" """""""""" """""""""" """"""""""

118

RI8:T OUT'R >OIN

This will displa7 the all $at!hing

e!o ds and the

e!o ds whi!h a e in

ight hand side

ta%le those that a e not in left hand side ta%le5 '/0


SQL-

sele!t e$pno#ena$e#Jo%#dna$e#lo! f o$ e$p e ight oute Join dept d on(e5deptnoHd5deptno). O sele!t e$pno#ena$e#Jo%#dna$e#lo! f o$ e$p e#dept d whe e e5deptno(G) H d5deptno.

SQL-

'MANO 222 444 111

'N(M' sa&eth Jagan sudha

>O) anal7st

DN(M' $&t $&t fin h

LOC h7d h7d %ang %o$%a7

"""""""""" """""""""" """""""""" """""""""" """""""""" $anage !le &

BULL OUT'R >OIN

This will displa7 the all $at!hing e!o ds and the non"$at!hing e!o ds f o$ %oth ta%les5 '/0
SQL-

sele!t e$pno#ena$e#Jo%#dna$e#lo! f o$ e$p e full oute Join dept d on(e5deptnoHd5deptno). 'MANO 444 222 111 @@@ 'N(M' Jagan sa&eth sudha $adhu >O) $anage anal7st !le & enginee h %o$%a7 DN(M' $&t $&t fin LOC h7d h7d %ang

"""""""""" """""""""" """""""""" """""""""" """"""""""

119
INN'R >OIN This will displa7 all the e!o ds that have $at!hed5 '/0
SQL-

sele!t e$pno#ena$e#Jo%#dna$e#lo! f o$ e$p inne Join dept using(deptno). 'MANO 222 444 111 'N(M' sa&eth Jagan sudha >O) anal7st $anage !le & DN(M' $&t $&t fin LOC h7d h7d %ang

"""""""""" """""""""" """""""""" """""""""" """"""""""

SU)QU'RI'S (ND '+ISTS


SU)QU'RI'S Nesting of Uue ies# one within the othe is te $ed as a su%Uue 75 ( state$ent !ontaining a su%Uue 7 is !alled a pa ent Uue 75 Su%Uue ies a e used to et ieve data f o$ ta%les that depend on the values in the ta%le itself5
T*A'S

Single ow su%Uue ies Multi ow su%Uue ies Multiple su%Uue ies Co elated su%Uue ies

SIN8L' RO6 SU)QU'RI'S

In single ow su%Uue 7# it will etu n one value5 '/0

120
SQL-

sele!t E f o$ e$p whe e sal - (sele!t sal f o$ e$p whe e e$pno H VCDD). 'MANO VVWW VW4S VS31 'N(M' SCOTT FIN8 BORD >O) (N(L*ST AR'SID'NT (N(L*ST VCDD M8R VCDD :IR'D(T' 2S"(AR"WV 34"D'C"W2 S(L COMM D'ATNO 13 23 13

"""""""""" """""""""" """"""""" """""""""" """""""""""" """"""" """""""""" """""""""" 4333 4333 2V"NO9"W2 C333

MULTI RO6 SU)QU'RI'S

In $ulti su%Uue 75 '/0


SQL-

ow su%Uue 7# it will

etu n $o e than one value5 In su!h !ases we should not in %etween the !o$pa ision ope ato and the

in!lude ope ato s li&e an7# all# in o

sele!t E f o$ e$p whe e sal - an7 (sele!t sal f o$ e$p whe e sal %etween 1C33 and @333). 'MANO VCDD VVWW VW4S VS31 'N(M' >ON'S SCOTT FIN8 BORD >O) M8R :IR'D(T' S(L COMM D'ATNO 13 13 23 13

"""""""""" """""""""" """"""""" """""""""" """"""""""" M(N(8'R (N(L*ST AR'SID'NT (N(L*ST VCDD 2S"(AR"WV

"""""""" """""""""" """""""""" 1SVC 4333 C333 4333

VW4S 31"(AR"W2 2V"NO9"W2 VCDD 34"D'C"W2

SQL-

sele!t E f o$ e$p whe e sal - all (sele!t sal f o$ e$p whe e sal %etween 1C33 and @333). 'MANO VW4S 'N(M' FIN8 >O) M8R :IR'D(T' S(L COMM D'ATNO 23

"""""""""" """""""""" """"""""" """""""""" """"""""""""" """""" """""""""" """""""""" AR'SID'NT 2V"NO9"W2 C333

MULTIAL' SU)QU'RI'S

The e is no li$it on the nu$%e

of su%Uue ies in!luded in a whe e !lause5 It allows

nesting of a Uue 7 within a su%Uue 75

121

'/0
SQL-

sele!t E f o$ e$p whe e sal H (sele!t $a/(sal) f o$ e$p whe e sal , (sele!t $a/(sal) f o$ e$p)). 'MANO VVWW VS31 'N(M' SCOTT BORD >O) M8R :IR'D(T' 2S"(AR"WV 34"D'C"W2 S(L 4333 4333 COMM D'ATNO 13 13

"""""""""" """""""""" """"""""" """""""""" """""""""""" """"""" """""""""" """""""""" (N(L*ST VCDD (N(L*ST VCDD

CORR'L(T'D SU)QU'RI'S

( su%Uue 7 is evaluated on!e fo '/0


SQL-

the enti e pa ent state$ent whe e as a !o elated

su%Uue 7 is evaluated on!e fo eve 7 ow p o!essed %7 the pa ent state$ent5 sele!t distin!t deptno f o$ e$p e whe e C ,H (sele!t !ount(ena$e) f o$ e$p whe e e5deptno H deptno). D'ATNO """""""""" 13 43 '+ISTS '/ists fun!tion is a test fo e/isten!e5 This is a logi!al test fo the etu n of ows f o$ a Uue 75 '/0 Suppose we want to displa7 the depa t$ent nu$%e s whi!h has $o e than @ e$plo7ees5 sele!t deptno#!ount(E) f o$ e$p g oup %7 deptno having !ount(E) - @. D'ATNO """"""""" COUNT(E) """"""""""

SQL-

122
13 43 C D

B o$ the a%ove Uue 7 !an 7ou want to displa7 the na$es of e$plo7ees[
SQL-

sele!t deptno#ena$e# !ount(E) f o$ e$p g oup %7 deptno#ena$e having !ount(E) - @.

no ows sele!ted The a%ove Uue 7 etu ns nothing %e!ause !o$%ination of deptno and ena$e neve etu n $o e than one !ount5 The solution is to use e/ists whi!h follows5
SQL-

sele!t deptno#ena$e f o$ e$p e2 whe e e/ists (sele!t E f o$ e$p e1 whe e e25deptnoHe15deptno g oup %7 e15deptno having !ount(e15ena$e) - @) o de %7 deptno#ena$e. D'ATNO 13 13 13 13 13 43 43 43 43 43 43 'N(M' (D(MS BORD >ON'S SCOTT SMIT: (LL'N )L(F' >(M'S M(RTIN TURN'R 6(RD

"""""""""" """"""""""

NOT '+ISTS sele!t deptno#ena$e f o$ e$p e2 whe e not e/ists (sele!t E f o$ e$p e1 whe e e25deptnoHe15deptno g oup %7 e15deptno having !ount(e15ena$e) - @) o de

SQL-

123
%7 deptno#ena$e. D'ATNO 'N(M' """"""""" """""""""" 23 23 23 CL(RF FIN8 MILL'R

6(LFUA TR''S (ND INLIN' 9I'6


6(LFUA TR''S Using hie a !hi!al Uue ies# 7ou !an et ieve data %ased on a natu al hie a !hi!al

elationship %etween ows in a ta%le5 :oweve # whe e a hie a !hi!al elationship e/ists %etween the ows of a ta%le# a p o!ess !alled t ee wal&ing ena%les the hie a !h7 to %e !onst u!ted5 '/0
SQL-

sele!t ena$e XX =HH-= XX p io ena$e# level f o$ e$p sta t with ena$e H =FIN8= !onne!t %7 p io e$pnoH$g . 'N(M'XX=HH-=XXARIOR'N(M """""""""""""""""""""""""""""""""""" FIN8HH>ON'SHH-FIN8 SCOTTHH->ON'S (D(MSHH-SCOTT BORDHH->ON'S SMIT:HH-BORD L'9'L """""""" 2 1 4 @ 4 @

124
)L(F'HH-FIN8 (LL'NHH-)L(F' 6(RDHH-)L(F' M(RTINHH-)L(F' TURN'RHH-)L(F' >(M'SHH-)L(F' CL(RFHH-FIN8 MILL'RHH-CL(RF In the a%ove Sta t with !lause spe!ifies the oot ow of the ta%le5 Level pseudo !olu$n gives the 2 fo oot # 1 fo !hild and so on5 Conne!t %7 p io !lause spe!ifies the !olu$ns whi!h has pa ent"!hild elationship5 INLIN' 9I'6 OR TOA"N (N(L*SIS In the sele!t state$ent instead of ta%le na$e# epla!ing the sele!t state$ent is &nown as inline view5 '/0
SQL-

1 4 4 4 4 4 1 4

Sele!t ena$e# sal# ownu$ an& f o$ (sele!t Ef o$ e$p o de %7 sal). 'N(M' SMIT: >(M'S (D(MS 6(RD M(RTIN MILL'R TURN'R (LL'N CL(RF )L(F' >ON'S S(L W33 SC3 2233 21C3 21C3 2433 2C33 2D33 1@C3 1WC3 1SVC R(NF 2 1 4 @ C D V W S 23 22

"""""""""" """""""""" """"""""""

125
SCOTT BORD FIN8 4333 4333 C333 21 24 2@

LOCFS
Lo!&s a e the $e!hanis$s used to p event dest u!tive inte a!tion %etween use s a!!essing sa$e esou !e si$ultaneousl75 Lo!&s p ovides high deg ee of data !on!u en!75 T*A'S Row level lo!&s Ta%le level lo!&s

RO6 L'9'L LOCFS In the ow level lo!& a ow is lo!&ed e/!lusivel7 so that othe !annot $odif7 the ow until the t ansa!tion holding the lo!& is !o$$itted o sele!t55fo update !lause5 '/0
SQL-

olled %a!&5 This !an %e done %7 using

sele!t E f o$ e$p whe e sal - 4333 fo update of !o$$5.

T()L' L'9'L LOCFS

126
( ta%le level lo!& will p ote!t ta%le data the e%7 gua anteeing data integ it7 when data is %eing a!!essed !on!u entl7 %7 $ultiple use s5 ( ta%le lo!& !an %e held in seve al $odes5 Sha e lo!& Sha e update lo!& '/!lusive lo!&


S:(R' LOCF

( sha e lo!& lo!&s the ta%le allowing othe use s to onl7 Uue 7 %ut not inse t# update o delete ows in a ta%le5 Multiple use s !an pla!e sha e lo!&s on the sa$e esou !e at the sa$e ti$e5 '/0
SQL-

lo!& ta%le e$p in sha e $ode.

S:(R' UAD(T' LOCF

It lo!&s

ows that a e to %e updated in a ta%le5 It pe $its othe

use s to !on!u entl7

Uue 7# inse t # update o even lo!& othe

ows in the sa$e ta%le5 It p events the othe

use s f o$ updating the ow that has %een lo!&ed5 '/0


SQL-

lo!& ta%le e$p in sha e update $ode.

'+CLUSI9' LOCF

'/!lusive lo!& is the $ost est i!tive of ta%les lo!&s5 6hen issued %7 an7 use # it allows the othe use to onl7 Uue 75 It is si$ila to sha e lo!& %ut onl7 one use !an pla!e e/!lusive lo!& on a ta%le at a ti$e5 '/0
SQL-

lo!& ta%le e$p in sha e e/!lusive $ode.

NO6(IT

127
If one use o lo!&ed the ta%le without nowait then anothe use t 7ing to lo!& the sa$e

ta%le then he has to wait until the use who has initiall7 lo!&ed the ta%le issues a !o$$it oll%a!& state$ent5 This dela7 !ould %e avoided %7 appending a nowait !lause in the lo!& ta%le !o$$and5 '/0
SQL-

lo!& ta%le e$p in e/!lusive $ode nowait5

D'(DLOCF ( deadlo!& o!!u s when tow use s have a lo!& ea!h on sepa ate o%Je!t# and the7 want to a!Uui e a lo!& on the ea!h othe ;s o%Je!t5 6hen this happens# the fi st use has to wait fo the se!ond use to elease the lo!&# %ut the se!ond use will not elease it until the lo!& on the fi st use ;s o%Je!t is f eed5 In su!h a !ase# o a!le dete!ts the deadlo!& auto$ati!all7 and solves the p o%le$ %7 a%o ting one of the two t ansa!tions5

IND'+'S

Inde/ is t7pi!all7 a listing of &e7wo ds a!!o$panied %7 the lo!ation of info $ation on a su%Je!t5 6e !an ! eate inde/es e/pli!itl7 to speed up SQL state$ent e/e!ution on a ta%le5 The inde/ points di e!tl7 to the lo!ation of the ows !ontaining the value5 6:* IND'+'S[ Inde/es a e $ost useful on la ge ta%les# on !olu$ns that a e li&el7 to appea in whe e !lauses as si$ple eUualit75 T*A'S UniUue inde/ Non"uniUue inde/ )t ee inde/ )it$ap inde/ Co$posite inde/

128
Reve se &e7 inde/ Bun!tion"%ased inde/ Des!ending inde/ Do$ain inde/ O%Je!t inde/ Cluste inde/ Te/t inde/ Inde/ o ganiTed ta%le Aa tition inde/ Lo!al inde/ Lo!al p efi/ed Lo!al non"p efi/ed UNIQU' IND'+ UniUue inde/es gua antee that no two uniUue !onst aint is ! eated5 '/0
SQL-

8lo%al inde/ 8lo%al p efi/ed 8lo%al non"p efi/ed

ows of a ta%le have dupli!ate values in the

!olu$ns that define the inde/5 UniUue inde/ is auto$ati!all7 ! eated when p i$a 7 &e7 o

! eate uniUue inde/ studMind on student(sno).

NON"UNIQU' IND'+ Non"UniUue inde/es do not i$pose the a%ove est i!tion on the !olu$n values5 '/0
SQL-

! eate inde/ studMind on student(sno).

)TR'' IND'+ o (SC'NDIN8 IND'+

129
The default t7pe of inde/ used in an o a!le data%ase is the %t ee inde/5 ( %t ee inde/ is designed to p ovide %oth apid a!!ess to individual ows and Uui!& a!!ess to g oups of ows within a ange5 The %t ee inde/ does this %7 pe fo $ing a su!!ession of value !o$pa isons5 'a!h !o$pa ison eli$inates $an7 of the ows5 '/0
SQL-

! eate inde/ studMind on student(sno).

)ITM(A IND'+ This !an %e used fo low !a dinalit7 !olu$ns0 that is !olu$ns in whi!h the nu$%e of

distin!t values is snall when !o$pa ed to the nu$%e of the ows in the ta%le5 '/0
SQL-

! eate %it$ap inde/ studMind on student(se/).

COMAOSIT' IND'+ ( !o$posite inde/ also !alled a !on!atenated inde/ is an inde/ ! eated on $ultiple !olu$ns of a ta%le5 Colu$ns in a !o$posite inde/ !an appea in an7 o de and need not %e adJa!ent !olu$ns of the ta%le5 '/0
SQL-

! eate %it$ap inde/ studMind on student(sno# sna$e).

R'9'RS' F'* IND'+ ( eve se &e7 inde/ when !o$pa ed to standa d inde/# eve ses ea!h %7te of the !olu$n %eing inde/ed while &eeping the !olu$n o de 5 6hen the !olu$n is inde/ed in eve se $ode then the !olu$n values will %e sto ed in an inde/ in diffe ent %lo!&s as the sta ting value diffe s5 Su!h an a ange$ent !an help avoid pe fo $an!e deg adations in inde/es whe e $odifi!ations to the inde/ a e !on!ent ated on a s$all set of %lo!&s5 '/0

130
SQL-

! eate inde/ studMind on student(sno# eve se).

6e !an e%uild a eve se &e7 inde/ into no $al inde/ using the no eve se &e7wo d5 '/0
SQL-

alte inde/ studMind e%uild no eve se.

BUNCTION )(S'D IND'+ This will use esult of the fun!tion as &e7 instead of using !olu$n as the value fo the &e75 '/0
SQL-

! eate inde/ studMind on student(uppe (sna$e)).

D'SC'NDIN8 IND'+ The o de used %7 )"t ee inde/es has %een as!ending o de 5 *ou !an !atego iTe data in )" t ee inde/ in des!ending o de as well5 This featu e !an %e useful in appli!ations whe e so ting ope ations a e eUui ed5 '/0
SQL-

! eate inde/ studMind on student(sno des!).

T'+T IND'+ Que 7ing te/t is diffe ent f o$ Uue 7ing data %e!ause wo ds have shades of $eaning# elationships to othe wo ds# and opposites5 *ou $a7 want to sea !h fo wo ds that a e nea ea!h othe # o wo ds that a e elated to the s5 These Uue ies would %e e/t e$el7
SQL

diffi!ult if all 7ou had availa%le was the standa d elational ope ato s5 )7 e/tending te/t5

to

in!lude te/t inde/es# o a!le te/t pe $its 7ou to as& ve 7 !o$ple/ Uuestions a%out the

131
To use o a!le te/t# 7ou need to ! eate a te(t inde( on the !olu$n in whi!h the te/t is sto ed5 Te/t inde/ is a !olle!tion of ta%les and inde/es that sto e info $ation a%out the te/t sto ed in the !olu$n5
T*A'S

The e a e seve al diffe ent t7pes of inde/es availa%le in o a!le Si5 The fi st# suppo ted in o a!le Wi as well as o a!le Si5 (s of o a!le Si# 7ou !an use the inde/ fo fu the enhan!e 7ou te/t inde/ $anage$ent and Uue 7 !apa%ilities5
CONT'+T CT+C(T CT+RUL'

CONT'+T CT+C(T

is

te/t

The

CT+C(T

inde/ t7pe suppo ts the t ansa!tional s7n!h oniTation of data %etween the
CONT'+T

%ase ta%le and its te/t inde/5 6ith

inde/es# 7ou need to $anuall7 tell o a!le to


CT+C(T

update the values in the te/t inde/ afte data !hanges in %ase ta%le5 not gene ate s!o e values du ing the te/t Uue ies5
:O6 TO CR'(T' T'+T IND'+[

inde/ t7pes do

*ou !an ! eate a te/t inde/ via a spe!ial ve sion of the ! eate inde/ !o$$an5 Bo !onte/t inde/# spe!if7 the !t/s7s5!onte/t inde/ t7pe and fo !t/!at inde/# spe!if7 the !t/s7s5!t/!at inde/ t7pe5 '/0 Suppose 7ou have a ta%le !alled )OOFS with the following !olu$ns Title# (utho # Info5 ! eate inde/ %oo&Minde/ on %oo&s(info) inde/t7pe is !t/s7s5!onte/t. ! eate inde/ %oo&Minde/ on %oo&s(info) inde/t7pe is !t/s7s5!t/!at.

SQLSQL-

T'+T QU'RI'S

On!e a te/t inde/ is ! eated on the info !olu$n of in! ease d7na$i!all75

)OOFS

ta%le# te/t"sea !hing !apa%ilities

132
CONT(INS < C(TS'(RC:

CONT(INS

fun!tion ta&es two pa a$ete s \ the !olu$n na$e and the sea !h st ing5

S7nta/0 Contains(inde(ed_column, !earc)_!tr). If 7ou ! eate a inde/# use the fun!tion in pla!e of
CONT(INS5 C(TS'(RC:

CT+C(T

C(TS'(RC:

ta&es th ee pa a$ete s \ the !olu$n na$e# the sea !h st ing and the inde/ set5 S7nta/0 Contains(inde(ed_column, !earc)_!tr, inde(_!et).
:O6 ( T'+T Q'UR* 6ORFS[

6hen a fun!tion su!h as

CONT(INS

C(TS'(RC:

is used in Uue 7# the te/t po tion of the of the Uue 7 is p o!essed Just li&e a

Uue 7 is p o!essed %7 o a!le te/t5 The

e$ainde

egula Uue 7 within the data%ase5 The esult of the te/t Uue 7 p o!essing and the egula Uue 7 p o!essing a e $e ged to etu n a single set of e!o ds to the use 5
S'(RC:IN8 BOR (N '+(CT M(TC: OB ( 6ORD

The following Uue ies will sea !h fo a wo d !alled Kp pe t7; whose s!o e is g eate than Te o5 sele!t E f o$ %oo&s whe e !ontains(info# Kp ope t7;) - 3. sele!t E f o$ %oo&s whe e !atsea !h(info# Kp ope t7;# null) - 3.

SQLSQL-

Suppose if 7ou want to &now the s!o e of the Kp ope t7; in ea!h %oo&# if s!o e values fo individual sea !hes ange f o$ 3 to 23 fo ea!h o!!u en!e of the st ing within the te/t then use the s!o e fun!tion5 sele!t title# s!o e(23) f o$ %oo&s whe e !ontains(info# Kp ope t7;# 23) - 3.

SQL-

S'(RC:IN8 BOR (N '+(CT M(TC: OB MULTIAL' 6ORDS

The following Uue ies will sea !h fo two wo ds5

133
SQLSQL-

sele!t E f o$ %oo&s whe e !ontains(info# Kp ope t7

(ND

ha vests;) - 3. ha vests;# null) - 3.

sele!t E f o$ %oo&s whe e !atsea !h(info# Kp ope t7

(ND

Instead of using

(ND

7ou !ould hae used an a$pe sand(<)5 )efo e using this $ethod# set

define off so the < !ha a!te will not %e seen as pa t of a va ia%le na$e5 set define off sele!t E f o$ %oo&s whe e !ontains(info# Kp ope t7 < ha vests;) - 3. sele!t E f o$ %oo&s whe e !atsea !h(info# Kp ope t7 ha vests;# null) - 3.

SQLSQLSQL-

The following Uue ies will sea !h fo $o e than two wo ds5


SQLSQL-

sele!t E f o$ %oo&s whe e !ontains(info# Kp ope t7

(ND

ha vests

(ND

wo &e s;) - 3.

sele!t E f o$ %oo&s whe e !atsea !h(info# Kp ope t7 ha vests wo &e s;# null) - 3.

The following Uue ies will sea !h fo eithe of the two wo ds5
SQL-

sele!t E f o$ %oo&s whe e !ontains(info# Kp ope t7 7ou !an use a ve ti!al line (X)5

OR

ha vests;) - 3.

Instead of

OR

SQLSQL-

sele!t E f o$ %oo&s whe e !ontains(info# Kp ope t7

ha vests;) - 3.
X

sele!t E f o$ %oo&s whe e !atsea !h(info# Kp ope t7


(CCUM(a!!u$ulate)

ha vests;# null) - 3.

In the following Uue ies the

ope ato adds togethe the s!o es of the

individual sea !hes and !o$pa es the a!!u$ulated s!o e to the th eshold value5 sele!t E f o$ %oo&s whe e !ontains(info# Kp ope t7 ha vests;) - 3. ha vests;# null) - 3.

SQLSQL-

(CCUM

sele!t E f o$ %oo&s whe e !atsea !h(info# Kp ope t7 7ou !an use a !o$$a(#)5

(CCUM

Instead of
SQLSQL-

OR

sele!t E f o$ %oo&s whe e !ontains(info# Kp ope t7 # ha vests;) - 3. sele!t E f o$ %oo&s whe e !atsea !h(info# Kp ope t7 # ha vests;# null) - 3.

134
In the following Uue ies the
MINUS

ope ato

su%t a!ts the s!o e of the se!ond te $;s

sea !h f o$ the s!o e of the fi st te $;s sea !h5 sele!t E f o$ %oo&s whe e !ontains(info# Kp ope t7 ha vests;) - 3.

SQLSQL-

MINUS NOT

sele!t E f o$ %oo&s whe e !atsea !h(info# Kp ope t7 7ou !an use \ and instead of

ha vests;# null) - 3.

Instead of

MINUS

NOT

7ou !an use ]5 ha vests;) - 3.


]

SQLSQL-

sele!t E f o$ %oo&s whe e !ontains(info# Kp ope t7

"

sele!t E f o$ %oo&s whe e !atsea !h(info# Kp ope t7

ha vests;# null) - 3.

S'(RC:IN8 BOR (N '+(CT M(TC: OB ( A:R(S'

The following Uue ies will sea !h fo the ph ase5 If the sea !h ph ase in!ludes a ese ved wo d within o a!le te/t# the 7ou $ust use !u l7 % a!es (^_) to en!lose te/t5 sele!t E f o$ %oo&s whe e !ontains(info# Kt ansa!tions ^and_ finan!es;) - 3. sele!t E f o$ %oo&s whe e !atsea !h(info# Kt ansa!tions ^and_ finan!es;# null) - 3.

SQLSQL-

*ou !an en!lose the enti e ph ase within !u l7 % a!es# in whi!h !ase an7 ese ved wo ds within the ph ase will %e t eated as pa t of the sea !h ! ite ia5 sele!t E f o$ %oo&s whe e !ontains(info# K^t ansa!tions and finan!es_;) - 3. sele!t E f o$ %oo&s whe e !atsea !h(info# K^t ansa!tions and finan!es_;# null) - 3.

SQLSQL-

S'(RC:IN8 BOR 6ORDS T:(T (R' N'(R '(C: OT:'R

The following Uue ies will sea !h fo the wo ds that a e in %etween the sea !h te $s5 sele!t E f o$ %oo&s whe e !ontains(info# Kwo &e s 7ou !an use .5 ha vests;) - 3.

SQL-

N'(R

Instead of

N'(R

SQL-

sele!t E f o$ %oo&s whe e !ontains(info# Kwo &e s . ha vests;) - 3.

135
In
CONT'+T

inde/ Uue ies# 7ou !an spe!if7 the $a/i$u$ nu$%e

of wo ds %etween the

sea !h te $s5
SQL-

sele!t E f o$ %oo&s whe e !ontains(info# KN'(R((wo &e s# ha vests)#23); - 3.

USIN8 6ILDC(RDS DURIN8 S'(RC:'S

*ou !an use wild!a ds to e/pand the list of valid sea !h te $s used du ing 7ou Uue 75 >ust as in egula te/t"st ing wild!a d p o!essing# two wild!a ds a e availa%le5 L M " " pe !ent sign. $ultiple"!ha a!te wild!a d unde s!o e. single"!ha a!te wild!a d

SQLSQL-

sele!t E f o$ %oo&s whe e !ontains(info# Kwo &e L;) - 3. sele!t E f o$ %oo&s whe e !ontains(info# Kwo &MMM;) - 3.

S'(RC:IN8 BOR 6ORDS T:(T S:(R' T:' S(M' ST'M

Rathe than using wild!a ds# 7ou !an use ste$"e/pansion !apa%ilities to e/pand the list of te/t st ings5 8iven the Kste$; of a wo d# o a!le will e/pand the list of wo ds to sea !h fo to in!lude all wo ds having the sa$e ste$5 Sa$ple e/pansions a e show he e5 Ala7 " pla7s pla7ing pla7ed pla7ful

SQL-

sele!t E f o$ %oo&s whe e !ontains(info# K`$anage;) - 3.

S'(RC:IN8 BOR BUZZ* M(TC:'S

( fuTT7 $at!h e/pands the spe!ified sea !h te $ to in!lude wo ds that a e spelled si$ila l7 %ut that do not ne!essa il7 have the sa$e wo d ste$5 BuTT7 $at!hes a e $ost helpful when the te/t !ontains $isspellings5 The $isspellings !an %e eithe sea !hed te/t o in the sea !h st ing spe!ified %7 the use du ing the Uue 75 The following Uue ies will not wo d Kha dest;5 sele!t E f o$ %oo&s whe e !ontains(info# Kha dest;) - 3. etu n an7thing %e!ause its sea !h does not !ontain the in the

SQL-

136
It does# howeve # !ontains the wo d Kha vest;5 ( fuTT7 $at!h will wo d used as the sea !h te $5 To use a fuTT7 $at!h# p e!ede the sea !h te $ with a Uuestion $a &# with no spa!e %etween the Uuestion $a & and the %eginning of the sea !h te $5 sele!t E f o$ %oo&s whe e !ontains(info# K[ha dest;) - 3. etu n the %oo&s

!ontaining the wo d Kha vest; even though Kha vest; has a diffe ent wo d ste$ thant the

SQL-

S'(RC:IN8 BOR 6ORDS T:(T SOUND LIF' OT:'R 6ORDS

SOUND'+# e/pands sea !h te $s %ased on how the wo d sounds5 The SOUND'+ e/pansion $ethod uses the sa$e te/t"$at!hing logi! availa%le via the SOUND'+ fun!tion in SQL5 To use the SOUND'+ option# 7ou $ust p e!ede the sea !h te $ with an e/!la$ation $a &(I)5 sele!t E f o$ %oo&s whe e !ontains(info# KIg ate;) - 3.

SQL-

IND'+ S*NC:RONIZ(TION

6hen using

CONT'+T

inde/es# 7ou need to $anage the te/t inde/ !ontents. the te/t

inde/es a e not updated when the %ase ta%le is updated5 6hen the ta%le was updated# its te/t inde/ is out of s7n! with the %ase ta%le5 To s7n! of the inde/# e/e!ute the p o!edu e of the e/e!
CT+MDDL S*NCMIND'+

pa!&age5

SQL-

CT+MDDL5S*NCMIND'+(K%oo&Minde/;).

IND'+ S'TS

:isto i!all7# p o%le$s with Uue ies of te/t inde/es have o!!u ed when othe ! ite ia a e used alongside te/t sea !hes as pa t of the whe e !lause5 To i$p ove the $i/ed Uue 7 !apa%ilit7# o a!le featu es inde/ sets5 The inde/es within the inde/ set $a7 %e st u!tu ed elational !olu$ns o on te/t !olu$ns5

137
To ! eate an inde/ set# use the
CT+MDDL

pa!&age to ! eate the inde/ set and add inde/es to

it5 6hen 7ou ! eate a te/t inde/# 7ou !an then spe!if7 the inde/ set it %elongs to5 e/e! CT+MDDL5CR'(T'MIND'+MS'T(K%oo&sMinde/Mset;).

SQL-

The add non"te/t inde/es5 e/e!


CT+MDDL5(DDMIND'+(K%oo&sMinde/Mset;#

SQL-

KtitleMinde/;).

Now ! eate a

CT+C(T

te/t inde/5 Spe!if7 !t/s7s5!t/!at as the inde/ t7pe# and list the inde/

set in the pa a$ete s !lause5 ! eate inde/ %oo&Minde/ on %oo&s(info) inde/t7pe is !t/s7s5!t/!at pa a$ete s(Kinde/ set %oo&sMinde/Mset;). IND'+"OR8(NIZ'D T()L' (n inde/"o ganiTed ta%le &eeps its data so ted a!!o ding to the p i$a 7 &e7 !olu$n values fo the ta%le5 Inde/"o ganiTed ta%les sto e thei data as if the enti e ta%le was sto ed in an inde/5 (n inde/"o ganiTed ta%le allows 7ou to sto e the enti e ta%le;s data in an inde/5 '/0
SQL-

SQL-

! eate ta%le student (sno nu$%e (1)#sna$e va !ha (23)#s$a &s nu$%e (4) !onst aint p& p i$a 7 &e7(sno) o ganiTation inde/.

A(RTITION IND'+ Si$ila to pa titioning ta%les# o a!le allows 7ou to pa tition inde/es too5 Li&e ta%le

pa titions# inde/ pa titions !ould %e in diffe ent ta%lespa!es5


LOC(L IND'+'S

Lo!al &e7wo d tells o a!le to ! eate a sepa te inde/ fo ea!h pa tition5 In the lo!al p efi/ed inde/ the pa tition &e7 is spe!ified on the left p efi/5 6hen the unde l7ing ta%le is pa titioned %aes on# sa7 two !olu$ns then the inde/ !an %e p efi/ed on the fi st !olu$n spe!ified5

138
'/0
SQL-

Lo!al p efi/ed inde/es !an %e uniUue o non uniUue5 Lo!al inde/es $a7 %e easie to $anage than glo%al inde/es5

! eate inde/ studMinde/ on student(sno) lo!al.

8LO)(L IND'+'S

( glo%al inde/ $a7 !ontain values f o$ $ultiple pa titions5 (n inde/ is glo%al p efi/ed if it is pa titioned on the left p efi/ of the inde/ !olu$ns5 The glo%al !lause allows 7ou to ! eate a non"pa titioned inde/5 8lo%al inde/es $a7 pe fo $ uniUueness !he!&s faste inde/es5 than lo!al (pa titioned)

'/0

*ou !annot ! eate glo%al inde/es fo hash pa titions o su%pa titions5

SQL-

! eate inde/ studMinde/ on student(sno) glo%al.

Si$ila to ta%le pa titions# it is possi%le to $ove the$ f o$ one devi!e to anothe 5 )ut unli&e ta%le pa titions# $ove$ent of inde/ pa titions eUui es individual e!onst u!tion of the inde/ o ea!h pa tition (onl7 in the !ase of glo%al inde/)5 '/0
SQL-

alte inde/ studMind e%uild pa tition p1

Inde/ pa titions !annot %e d opped $anuall75 The7 a e d opped i$pli!itl7 when the data the7 pa titioned ta%le5 efe to is d opped f o$ the

MONITORIN8 US' OB IND'+'S On!e 7ou tu ned on the $onito ing the use of inde/es# then we !an !he!& whethe the ta%le is hitting the inde/ o not5

139
To $onito the use of inde/ use the follwing s7nta/5 S7nta/0 alte inde/ inde(_name $onito ing usage. then !he!& fo the details in 9`O)>'CTMUS(8' view5 If 7ou want to stop $onito ing use the following5 S7nta/0 alte inde/ inde(_name no$onito ing usage. D(T( MOD'L (LLMIND'+'S D)(MIND'+'S US'RMIND'+'S (LLMIND"COLUMNS D)("INDMCOLUMNS US'RMINDMCOLUMNS (LLMA(RTMIND'+'S D)(MA(RTMIND'+'S US'RMA(RTMIND'+'S 9`O)>'CTMUS(8'

140

SQLEALUS COMMN(NDS
These !o$$ands does not eUui e state$ent te $inato and appli!a%le to the sessions # those will %e auto$ati!all7 !lea ed when session was !losed5 )R'(F This will %e used to % ea&up the data depending on the g ouping5 S7nta/0 ) ea& o % e Non ,!olu$n_name- on epo tO COMAUT' This will %e used to pe fo $ g oup fun!tions on the data5

141
S7nta/0 Co$pute o !o$p Ng oupMfun!tion of column_name on brea in"_column_name o epo tO TTITL' This will give the top title fo 7ou S7nta/0 Ttitle o ttit Nleft X !ente X ightO title_name s&ip n ot)er_c)aracter! Ttitle o ttit Non o offO )TITL' This will give the %otto$ title fo 7ou S7nta/0 )title o %tit Nleft X !ente X ightO title_name s&ip n ot)er_c)aracter! )title o %tit Non o offO '/0
SQLSQLSQLSQLSQLSQL-

epo t5 *ou !an on o off the ttitle5

epo t5 *ou !an on o off the %title5

% e on deptno s&ip 2 on epo t !o$p su$ of sal on deptno !o$p su$ of sal on epo t ttitle !ente ='MALO*'' D'T(ILS= s&ip2 !ente =""""""""""""""""= %title !ente =EE T:(NFQ EE= sele!t E f o$ e$p o de %7 deptno.

Output0 'MALO*'' D'T(ILS """"""""""""""""""""""" 'MANO VVW1 'N(M' CL(RF >O) M(N(8'R M8R VW4S :IR'D(T' 3S">UN"W2 S(L 1@C3 COMM D'ATNO 23

"""""""""" """""""""" """""""""

""""""" """""""""""""" """""""" """""""""" """"""""""

142
VW4S VS4@ FIN8 AR'SID'NT VVW1 2V"NO9"W2 14">(N"W1 C333 2433 """""""""" WVC3 V4DS VWVD VS31 VVWW VCDD SMIT: BORD CL'RF (N(L*ST VS31 VVWW VCDD VCDD VW4S 2V"D'C"W3 14"M(*"WV 34"D'C"W2 2S"(AR"WV 31"(AR"W2 W33 2233 4333 4333 1SVC """""""""" 23WVC V@SS VDSW VDC@ VS33 VW@@ VC12 (LL'N )L(F' >(M'S 6(RD S(L'SM(N M(N(8'R CL'RF S(L'SM(N VDSW VW4S VDSW VDSW VDSW VDSW 13"B')"W2 32"M(*"W2 1W"S'A"W2 34"D'C"W2 3W"S'A"W2 11"B')"W2 2D33 1WC3 21C3 SC3 2C33 21C3 """""""""" S@33 """""""""" su$ 1S31C 3 C33 EEEEEEEEEE su$ 2@33 433 EEEEEEEEEE su$ 43 EEEEEEEEEE su$ 13

MILL'R CL'RF

(D(MS CL'RF SCOTT (N(L*ST >ON'S M(N(8'R

M(RTIN S(L'SM(N TURN'R S(L'SM(N

IMAORT(NT QU'RI'S
2) To find the nth ow of a ta%le Sele!t Ef o$ e$p whe e owid H (sele!t $a/( owid) f o$ e$p whe e ownu$ ,H @). O
SQL-

SQL-

Sele!t Ef o$ e$p whe e ownu$ ,H @ $inus sele!t Ef o$ e$p whe e ownu$ ,H 4.

143
1) To find dupli!ate ows Sele!t Ef o$ e$p whe e owid in (sele!t $a/( owid) f o$ e$p g oup %7 e$pno# ena$e# $g # Jo%# hi edate# !o$$# deptno# sal). O
SQL-

SQL-

Sele!t e$pno#ena$e#sal#Jo%#hi edate#!o$$ # !ount(E) f o$ e$p g oup %7 e$pno#ena$e#sal#Jo%#hi edate#!o$$ having !ount(E) -H2.

4) To delete dupli!ate ows Delete e$p whe e owid in (sele!t $a/( owid) f o$ e$p g oup %7 e$pno#ena$e#$g #Jo%#hi edate#sal#!o$$#deptno). @) To find the !ount of dupli!ate ows Sele!t ena$e# !ount(E) f o$ e$p g oup %7 ena$e having !ount(E) -H 2.

SQL-

SQL-

C) :ow to displa7 alte native ows in a ta%le[ sele!t Ef o$ e$p whe e ( owid#3) in (sele!t owid#$od( ownu$#1) f o$ e$p).

SQL-

D) 8etting e$plo7ee details of ea!h depa t$ent who is d awing $a/i$u$ sal[ sele!t Ef o$ e$p whe e (deptno#sal) in ( sele!t deptno#$a/(sal) f o$ e$p g oup %7 deptno). V) :ow to get nu$%e of e$plo7ees in ea!h depa t$ent # in whi!h depa t$ent is having $o e than 1C33 e$plo7ees[ Sele!t deptno#!ount(E) f o$ e$p g oup %7 deptno having !ount(E) -1C33.

SQL-

SQL-

W) To eset the ti$e to the %eginning of the da7 Sele!t toM!ha (t un!(s7sdate)#;dd"$on"7777 hh0$i0ss a$;) f o$ dual.

SQL-

S) To find nth $a/i$u$ sal

144

SQL-

Sele!t Ef o$ e$p whe e sal in (sele!t $a/(sal) f o$ (sele!t Ef o$ e$p o de %7 sal) whe e ownu$ ,H C).

EE T:(NFQ EE

Вам также может понравиться