SQL Plsql Document
Transcript of SQL Plsql Document
-
8/20/2019 SQL Plsql Document
1/319
INTRODUCTION
SQL is divided into the following
Data Definition Language (DDL)
Data Manipulation Language (DML)
Data Retieval Language (DRL)
Tansa!tion Contol Language (TCL)
Data Contol Language (DCL)
DDL "" !eate# alte# dop# tun!ate# ena$e
DML "" inset# update# delete
DRL "" sele!t
TCL "" !o$$it# oll%a! savepoint
DCL "" gant# evo&e
CR'T' TL' S*NT+
Ceate 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 inset the e!ods into ta%le5
6e have two $ethods to inset5
7 value $ethod
7 addess $ethod
a) USIN8 9LU' M'T:OD
S7nta/0
inset into ,table_name) values (value1, value2, value3 …. Valuen).
© Copy rights are reserved.
1
-
8/20/2019 SQL Plsql Document
2/319
'/0
SQL- inset into student values (2# ;sudha;# 233).
SQL- inset into student values (1# ;sað# 133).
To inset a new e!od again 7ou have to t7pe entie inset !o$$and# if thee ae lot of
e!ods this will %e diffi!ult5
This will %e avoided %7 using addess $ethod5
%) USIN8 DDR'SS M'T:OD
S7nta/0
inset into ,table_name) values (&col1, &col2, &col3 …. &coln).
This will po$pt 7ou fo the values %ut fo eve7 inset 7ou have to use fowad slash5
'/0
SQL- inset into student values (agan
'nte value fo $a&s0 433
old 20 inset into student values(
-
8/20/2019 SQL Plsql Document
3/319
inset into ,table_name)(col1, col2, col3 … Coln) values (value1, value2, value3
….
Valuen).
'/0
SQL- inset into student (no# na$e) values (4# ;Ra$esh;). SQL- inset into student (no# na$e) values (@# ;Madhu;).
d) INS'RTIN8 DT INTO SA'CIBI'D COLUMNS USIN8 DDR'SS M'T:OD
S7nta/0
inset into ,table_name)(col1, col2, col3 … coln) values (&col1, &col2 ….&coln);
This will po$pt 7ou fo the values %ut fo eve7 inset 7ou have to use fowad slash5
'/0
SQL- inset into student (no# na$e) values (
-
8/20/2019 SQL Plsql Document
4/319
NO NM' MRFS
""" """""" """"""""
2 Sudha 233
1 Sað 133 2 >agan 433
1 Naen @33
4 Ra$esh
@ Madhu
9isu
Rattu
SQL- sele!t no# na$e# $a&s fo$ student.
NO NM' MRFS
""" """""" """"""""
2 Sudha 233
1 Sað 133
2 >agan 433
1 Naen @33
4 Ra$esh
@ Madhu
9isu
Rattu
SQL- sele!t no# na$e fo$ student.
NO NM'
""" """""""
2 Sudha 1 Sað
2 >agan
1 Naen
4 Ra$esh
@ Madhu
© Copy rights are reserved.
4
-
8/20/2019 SQL Plsql Document
5/319
9isu
Rattu
CONDITIONL S'L'CTIONS ND OA'RTORS
6e have two !lauses used in this
6hee
Ode %7
USIN8 6:'R'
S7nta/0
sele!t E fo$ ,table_name- whee ,condition-.
the following ae the diffeent t7pes of opeatos used in whee !lause5
ith$eti! opeatos
Co$paison opeatos
Logi!al opeatos
ith$eti! opeatos "" highest pe!eden!e
G# "# E# ?
Co$paison opeatos
H# H# -# ,# -H# ,H# ,-
%etween# not %etween
in# not in
null# not null
li&e
Logi!al opeatos
nd
O "" lowest pe!eden!e
not
a) USIN8 H# -# ,# -H# ,H# H# ,-
© Copy rights are reserved.
5
-
8/20/2019 SQL Plsql Document
6/319
'/0
SQL- sele!t E fo$ student whee no H 1.
NO NM' MRFS """ """"""" """""""""
1 Sað 133
1 Naen @33
SQL- sele!t E fo$ student whee no , 1.
NO NM' MRFS
""" """"""" """"""""""
2 Sudha 233
2 >agan 433
SQL- sele!t E fo$ student whee no - 1.
NO NM' MRFS
""" """"""" """"""""""
4 Ra$esh
@ Madhu
9isu
Rattu
SQL- sele!t E fo$ student whee no ,H 1.
NO NM' MRFS
""" """"""" """"""""""
2 Sudha 233 1 Sað 133
2 >agan 433
1 Naen @33
SQL- sele!t E fo$ student whee no -H 1.
© Copy rights are reserved.
6
-
8/20/2019 SQL Plsql Document
7/319
NO NM' MRFS
""" """"""" """""""""
1 Sað 133
1 Naen @33 4 Ra$esh
@ Madhu
9isu
Rattu
SQL- sele!t E fo$ student whee no H 1.
NO NM' MRFS
""" """"""" """"""""""
2 Sudha 233
2 >agan 433
4 Ra$esh
@ Madhu
9isu
Rattu
SQL- sele!t E fo$ student whee no ,- 1.
NO NM' MRFS
""" """"""" """"""""""
2 Sudha 233
2 >agan 433
4 Ra$esh
@ Madhu
9isu Rattu
%) USIN8 ND
This will gives the output when all the !onditions %e!o$e tue5
© Copy rights are reserved.
7
-
8/20/2019 SQL Plsql Document
8/319
S7nta/0
sele!t E fo$ ,table_name- whee ,condition1- and ,condition2- and 55
,conditionn-.
'/0 SQL- sele!t E fo$ student whee no H 1 and $a&s -H 133.
NO NM' MRFS
""" """"""" """"""""
1 Sað 133
1 Naen @33
!) USIN8 OR
This will gives the output when eithe of the !onditions %e!o$e tue5
S7nta/0
sele!t E fo$ ,table_name- whee ,condition1- and ,condition2- o 55
,conditionn-.
'/0
SQL- sele!t E fo$ student whee no H 1 o $a&s -H 133.
NO NM' MRFS
""" """"""" """""""""
1 Sað 133
2 >agan 433
1 Naen @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 fo$ ,table_name- whee ,col - %etween ,lower bound - and ,upper
bound -.
© Copy rights are reserved.
8
-
8/20/2019 SQL Plsql Document
9/319
'/0
SQL- sele!t E fo$ student whee $a&s %etween 133 and @33.
NO NM' MRFS """ """"""" """""""""
1 Sað 133
2 >agan 433
1 Naen @33
e) USIN8 NOT 'T6''N
This will gives the output %ased on the !olu$n whi!h values ae not in its lowe %ound#
uppe%ound5
S7nta/0
sele!t E fo$ ,table_name- whee ,col - not %etween ,lower bound - and ,upper
bound -.
'/0
SQL- sele!t E fo$ student whee $a&s not %etween 133 and @33.
NO NM' MRFS
""" """"""" """""""""
2 Sudha 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 fo$ ,table_name- whee ,col - in ( value1, value2, value3 … valuen).
'/0
SQL- sele!t E fo$ student whee no in (2# 1# 4).
© Copy rights are reserved.
9
-
8/20/2019 SQL Plsql Document
10/319
NO NM' MRFS
""" """"""" """""""""
2 Sudha 233
1 Sað 133
2 >agan 433 1 Naen @33
4 Ra$esh
g) USIN8 NOT IN
This will gives the output %ased on the !olu$n whi!h values ae not in the list of
values spe!ified5
S7nta/0
sele!t E fo$ ,table_name- whee ,col - not in ( value1, value2, value3 … valuen).
'/0
SQL- sele!t E fo$ student whee no not in (2# 1# 4).
NO NM' MRFS
""" """"""" """""""""
@ Madhu
9isu
Rattu
h) USIN8 NULL
This will gives the output %ased on the null values in the spe!ified !olu$n5
S7nta/0 sele!t E fo$ ,table_name- whee ,col - is null.
'/0
SQL- sele!t E fo$ student whee $a&s is null.
© Copy rights are reserved.
10
-
8/20/2019 SQL Plsql Document
11/319
NO NM' MRFS
""" """"""" """""""""
4 Ra$esh
@ Madhu
9isu Rattu
i) 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 fo$ ,table_name- whee ,col - is not null.
'/0
SQL- sele!t E fo$ student whee $a&s is not null.
NO NM' MRFS
""" """"""" """""""""
2 Sudha 233
1 Sað 133
2 >agan 433
1 Naen @33
J) USIN8 LIF'
This will %e used to sea!h though the ows of data%ase !olu$n %ased on the patten
7ou spe!if75
S7nta/0
sele!t E fo$ ,table_name- whee ,col - li&e , pattern-.
'/0
i) This will give the ows whose $a&s ae 2335
SQL- sele!t E fo$ student whee $a&s li&e 233.
© Copy rights are reserved.
11
-
8/20/2019 SQL Plsql Document
12/319
NO NM' MRFS
""" """"""" """""""""
2 Sudha 233
ii) This will give the ows whose na$e stat with KS;5
SQL- sele!t E fo$ student whee na$e li&e =S=.
NO NM' MRFS
""" """"""" """""""""
2 Sudha 233
1 Sað 133
iii) This will give the ows whose na$e ends with Kh;5
SQL- sele!t E fo$ student whee na$e li&e =h=.
NO NM' MRFS
""" """"""" """""""""
1 Sað 133
4 Ra$esh
i9) This will give the ows whose na$e;s se!ond lette stat with Ka;5
SQL- sele!t E fo$ student whee na$e li&e =a=.
NO NM' MRFS
""" """"""" """"""""
1 Sað 133
2 >agan 433 1 Naen @33
4 Ra$esh
@ Madhu
Rattu
© Copy rights are reserved.
12
-
8/20/2019 SQL Plsql Document
13/319
9) This will give the ows whose na$e;s thid lette stat with Kd;5
SQL- sele!t E fo$ student whee na$e li&e =d=.
NO NM' MRFS """ """"""" """""""""
2 Sudha 233
@ Madhu
9i) This will give the ows whose na$e;s se!ond lette stat with Kt; fo$ ending5
SQL- sele!t E fo$ student whee na$e li&e =t=.
NO NM' MRFS
""" """"""" """""""""
1 Sað 133
Rattu
9ii) This will give the ows whose na$e;s thid lette stat with Ke; fo$ ending5
SQL- sele!t E fo$ student whee na$e li&e =e=.
NO NM' MRFS
""" """"""" """""""""
1 Sað 133
4 Ra$esh
9iii) This will give the ows whose na$e !otains 1 a;s5
SQL- sele!t E fo$ student whee na$e li&e =a a =.
NO NM' MRFS
""" """"""" """"""""""
2 >agan 433
© Copy rights are reserved.
13
-
8/20/2019 SQL Plsql Document
14/319
E *ou have to spe!if7 the pattens in lie using undes!oe ( )5
USIN8 ORD'R *
This will %e used to odeing the !olu$ns data (as!ending o des!ending)5
S7nta/0
Sele!t E fo$ ,table_name- ode %7 ,col - des!.
7 default oa!le will use as!ending ode5
If 7ou want output in des!ending ode 7ou have to use de!c &e7wod afte the !olu$n5
'/0
SQL- sele!t E fo$ student ode %7 no.
NO NM' MRFS
""" """"""" """""""""
2 Sudha 233
2 >agan 433
1 Sað 133
1 Naen @33
4 Ra$esh
@ Madhu
9isu
Rattu
SQL- sele!t E fo$ student ode %7 no des!.
NO NM' MRFS """ """"""" """""""""
Rattu
9isu
@ Madhu
4 Ra$esh
© Copy rights are reserved.
14
-
8/20/2019 SQL Plsql Document
15/319
1 Sað 133
1 Naen @33
2 Sudha 233
2 >agan 433
USIN8 DML
USIN8 UADT'
This !an %e used to $odif7 the ta%le data5
S7nta/0
Update ,table_name- set ,col1- H value2# ,col2- H value1 whee ,condition-.
'/0
SQL- update student set $a&s H 33.
If 7ou ae not spe!if7ing an7 !ondition this will update entie ta%le5
SQL- update student set $a&s H 33 whee no H 1.
SQL- update student set $a&s H 33# na$e H =9enu= whee no H 2.
USIN8 D'L'T'
This !an %e used to delete the ta%le data te$poail75
S7nta/0
Delete ,table_name- whee ,condition-.
'/0
SQL- delete student.
If 7ou ae not spe!if7ing an7 !ondition this will delete entie ta%le5
SQL- delete student whee no H 1.
© Copy rights are reserved.
15
-
8/20/2019 SQL Plsql Document
16/319
USIN8 DDL
USIN8 LT'R
This !an %e used to add o e$ove !olu$ns and to $odif7 the pe!ision of the datat7pe5
a) 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- dop ,col datatype-.
'/0
SQL- alte ta%le student dop !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().
E To de!ease pe!ision the !olu$n should %e e$pt75
d) MFIN8 COLUMN UNUS'D
© Copy rights are reserved.
16
-
8/20/2019 SQL Plsql Document
17/319
S7nta/0
alte ta%le ,table_name- set unused !olu$n ,col -.
'/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$o75
d) DROAAIN8 UNUS'D COLUMNS
S7nta/0
alte ta%le ,table_name- dop unused !olu$ns.
'/0
SQL- alte ta%le student dop unused !olu$ns.
E *ou !an not dop individual unused !olu$ns of a ta%le5
e) R'NMIN8 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 TRUNCT'
This !an %e used to delete the entie ta%le data pe$anentl75
S7nta/0
tun!ate ta%le ,table_name-.
'/0
SQL- tun!ate ta%le student.
USIN8 DROA
© Copy rights are reserved.
17
-
8/20/2019 SQL Plsql Document
18/319
This will %e used to dop the data%ase o%Je!t.
S7nta/0
Dop ta%le ,table_name-.
'/0
SQL- dop ta%le student.
USIN8 R'NM'
This will %e used to ena$e the data%ase o%Je!t.
S7nta/0
ena$e ,old table_name- to ,new_table_name-.
'/0
SQL- ena$e student to stud.
© Copy rights are reserved.
18
-
8/20/2019 SQL Plsql Document
19/319
USIN8 TCL
USIN8 COMMIT
This will %e used to save the wo&5
Co$$it is of two t7pes5
I$pli!it
'/pli!it
a) IMALICIT
This will %e issued %7 oa!le intenall7 in two situations5
6hen an7 DDL opeation is pefo$ed5
6hen 7ou ae e/iting fo$ SQL E ALUS5
%) '+ALICIT
This will %e issued %7 the use5
S7nta/0
Co$$it o !o$$it wo&.
E 6hen eve 7ou !o$$itted then the tansa!tion was !o$pleted5
USIN8 ROLLCF
This will undo the opeation5
This will %e applied in two $ethods5 Upto pevious !o$$it
Upto pevious oll%a!&
S7nta/0
Roll o oll wo&.
© Copy rights are reserved.
19
-
8/20/2019 SQL Plsql Document
20/319
O
Roll%a!& o oll%a!& wo&.
E 6hile po!ess is going on# if suddenl7 powe goes then oa!le will oll%a!& the
tansa!tion5
USIN8 S9'AOINT
*ou !an use savepoints to oll%a!& potions of 7ou !uent set of tansa!tions5
S7nta/0
Savepoint , !avepoint_name-.
'/0
SQL- savepoint s2.
SQL- inset into student values(2# Ka;# 233).
SQL- savepoint s1.
SQL- inset into student values(1# K%;# 133).
SQL- savepoint s4.
SQL- inset into student values(4# K!;# 433).
SQL- savepoint s@.
SQL- inset into student values(@# Kd;# @33).
efoe oll%a!&
SQL- sele!t E fo$ student.
NO NM' MRFS
""" """"""" """"""""""
2 a 233
1 % 133
4 ! 433 @ d @33
SQL- oll%a!& to savepoint s4.
O
SQL- oll%a!& to s4.
© Copy rights are reserved.
20
-
8/20/2019 SQL Plsql Document
21/319
This will oll%a!& last two e!ods5
SQL- sele!t E fo$ student.
NO NM' MRFS """ """"""" """"""""""
2 a 233
1 % 133
© Copy rights are reserved.
21
-
8/20/2019 SQL Plsql Document
22/319
USIN8 DCL
DCL !o$$ands ae used to ganting and evo&ing the pe$issions5
USIN8 8RNT
This is used to gant the pivileges to othe uses5
S7nta/0
8ant , privile"e!- on ,ob#ect_name- to ,u!er_name- with gant option.
'/0
SQL- gant sele!t on student to sudha. "" 7ou !an give individual pivilege
SQL- gant sele!t# inset on student to sudha. "" 7ou !an give set of pivileges
SQL- gant all on student to sudha. "" 7ou !an give all pivileges
The sudha use has to use dot $ethod to a!!ess the o%Je!t5
SQL- sele!t E fo$ sað5student.
The sudha use !an not gant pe$ission on student ta%le to othe uses5 To get this
t7pe of option use the following5
SQL- gant all on student to sudha with gant option.
Now sudha use also gant pe$issions on student ta%le5
USIN8 R'9OF'
This is used to evo&e the pivileges fo$ the uses to whi!h 7ou ganted the pivileges5
S7nta/0 Revo&e , privile"e!- on ,ob#ect_name- fo$ ,u!er_name-.
'/0
SQL- evo&e sele!t on student fo$ sudha. "" 7ou !an evo&e individual pivilege
SQL- evo&e sele!t# inset on student fo$ sudha. "" 7ou !an evo&e set of pivileges
© Copy rights are reserved.
22
-
8/20/2019 SQL Plsql Document
23/319
SQL- evo&e all on student fo$ sudha. "" 7ou !an evo&e all pivileges
USIN8 LIS'S
CR'T' 6IT: S'L'CT
6e !an !eate a ta%le using e/isting ta%le along with data5
S7nta/0
Ceate ta%le ,new_table_name- col1, col2, col3 ... coln as sele!t E fo$
,old_table_name-.
'/0
SQL- !eate ta%le student2 as sele!t E fo$ student.
Ceating ta%le with 7ou own !olu$n na$es5
SQL- !eate ta%le student1(sno# sna$e# s$a&s) as sele!t E fo$ student.
Ceating ta%le with spe!ified !olu$ns5
SQL- !eate ta%le student4 as sele!t no#na$e fo$ student.
Ceating ta%le with out ta%le data5
SQL- !eate ta%le student1(sno# sna$e# s$a&s) as sele!t E fo$ student whee 2 H 1.
In the a%ove whee !lause give an7 !ondition whi!h does not satisf75
INS'RT 6IT: S'L'CT
Using this we !an inset e/isting ta%le data to a anothe ta%le in a single tip5 ut the
ta%le stu!tue should %e sa$e5
S7nta/0
Inset into ,table1- sele!t E fo$ ,table2-.
'/0
SQL- inset into student2 sele!t E fo$ student.
© Copy rights are reserved.
23
-
8/20/2019 SQL Plsql Document
24/319
Inseting data into spe!ified !olu$ns
SQL- inset into student2(no# na$e) sele!t no# na$e fo$ student.
COLUMN LIS'S
S7nta/0
$elect %or"inal_col %alia!_name 'rom %table na$e-.
'/0
SQL- sele!t no sno fo$ student.
o
SQL- sele!t no Psno fo$ student.
TL' LIS'S
If 7ou ae 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- ,alia!_name-5,coln- fo$
,table_name- ,alia!_name-.
'/0
SQL- sele!t s5no# s5na$e fo$ student s.
© Copy rights are reserved.
24
-
8/20/2019 SQL Plsql Document
25/319
USIN8 M'R8'
M'R8'
*ou !an use $ege !o$$and to pefo$ inset and update in a single !o$$and5
'/0
SQL- Mege into student2 s2
Using (sele!t EBo$ student1) s1
On(s25noHs15no)
6hen $at!hed then
Update set $a&s H s15$a&s
6hen not $at!hed then
Inset (s25no#s25na$e#s25$a&s)
9alues(s15no#s15na$e#s15$a&s).
In the a%ove the two ta%les ae with the sa$e stu!tue %ut we !an $ege diffeent
stu!tued 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
SQL- Mege into student2 s2
Using (sele!t EBo$ student1) s1
On(s25noHs15no)
6hen $at!hed then
Update set $a&s H s15hno 6hen not $at!hed then
Inset (s25no#s25na$e#s25$a&s)
9alues(s15no#s15na$e#s15hno).
© Copy rights are reserved.
25
-
8/20/2019 SQL Plsql Document
26/319
MULTIL' INS'RTS
6e have ta%le !alled D'AT with the following !olu$ns and data
D'ATNO DNM' LOC
"""""""" """""""" """"
23 a!!ounting new 7o&
13 esea!h dallas
43 sales Chi!ago
@3 opeations %oston
a) CR'T' STUD'NT TL'
SQL- Ceate 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- Inset all
Into student values(2#;a;#233)
Into student values(1#;%;#133)
Into student values(4#;!;#433)
Sele!t Efo$ dept whee deptnoH23.
"" This insets 4 ows
!) MULTI INS'RT 6IT: SA'CIBI'D BI'LDS
SQL- inset all
Into student (no#na$e) values(@#;d;)
Into student(na$e#$a&s) values(;e;#@33)
Into student values(4#;!;#433)
Sele!t Efo$ dept whee deptnoH23.
© Copy rights are reserved.
26
-
8/20/2019 SQL Plsql Document
27/319
"" This insets 4 ows
d) MULTI INS'RT 6IT: DUALICT' RO6S
SQL- inset all
Into student values(2#;a;#233)
Into student values(1#;%;#133)
Into student values(4#;!;#433)
Sele!t Efo$ dept whee deptno - 23.
"" This insets ows %e!ause in the sele!t state$ent etieves 4 e!ods (4 insets fo
ea!h ow etieved)
e) MULTI INS'RT 6IT: CONDITIONS S'D
SQL- Inset all
6hen deptno - 23 then
Into student2 values(2#;a;#233)
6hen dna$e H KSL'S; then
Into student1 values(1#;%;#133)
6hen lo! H KN'6 *ORF; then Into student4 values(4#;!;#433)
Sele!t Efo$ dept whee deptno-23.
"" This insets @ ows %e!ause the fist !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- Inset 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
© Copy rights are reserved.
27
-
8/20/2019 SQL Plsql Document
28/319
Into student4 values(4#;!;#433)
'lse
Into student values(@#;d;#@33)
Sele!t Efo$ dept whee deptno-23.
"" This insets 4 e!ods %e!ause the else satisfied 4 ti$es
g) MULTI INS'RT 6IT: CONDITIONS S'D ND BIRST
SQL- Inset fist
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 Efo$ dept whee deptnoH13.
"" This insets 2 e!od %e!ause the fist !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- Inset fist
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 Efo$ dept whee deptnoH13.
"" This insets 2 e!od %e!ause the else !lause satisfied on!e
© Copy rights are reserved.
28
-
8/20/2019 SQL Plsql Document
29/319
i) MULTI INS'RT 6IT: MULTIL' TL'S
SQL- Inset all
Into student2 values(2#;a;#233)
Into student1 values(1#;%;#133)
Into student4 values(4#;!;#433)
Sele!t Efo$ dept whee deptnoH23.
"" This insets 4 ows
EE *ou !an use $ulti ta%les with spe!ified fields# with dupli!ate ows# with !onditions#
with fist and else !lauses5
© Copy rights are reserved.
29
-
8/20/2019 SQL Plsql Document
30/319
BUNCTIONS
Bun!tions !an %e !ategoied as follows5
Single ow fun!tions
8oup fun!tions
SIN8L' RO6 BUNCTIONS
Single ow fun!tions !an %e !ategoied into five5 These will %e applied fo ea!h ow and
podu!es individual output fo ea!h ow5
Nu$ei! fun!tions
Sting fun!tions
Date fun!tions
Mis!ellaneous fun!tions
Convesion fun!tions
NUM'RIC BUNCTIONS
%s
Sign
St
Mod
Nvl
Aowe
'/p
Ln
Log
Ceil
Bloo
Round
© Copy rights are reserved.
30
-
8/20/2019 SQL Plsql Document
31/319
Tun&
itand
8eatest
Least
Coales!e
a) S
%solute value is the $easue of the $agnitude of value5
%solute value is alwa7s a positive nu$%e5
S7nta/0 a%s (value)
'/0 SQL- sele!t a%s()# a%s(")# a%s(3)# a%s(null) fo$ dual.
S() S(") S(3) S(NULL)
"""""""""" """""""""" """""""""" """""""""""""
3
%) SI8N
Sign gives the sign of a value5
S7nta/0 sign (value)
'/0
SQL- sele!t sign()# sign(")# sign(3)# sign(null) fo$ dual.
SI8N() SI8N(") SI8N(3) SI8N(NULL)
"""""""""" """""""""" """""""""" """"""""""""""
2 "2 3
!) SQRT
This will give the suae oot of the given value5
© Copy rights are reserved.
31
-
8/20/2019 SQL Plsql Document
32/319
S7nta/0 st (value) "" hee value $ust %e positive5
'/0
SQL- sele!t st(@)# st(3)# st(null)# st(2) fo$ dual. SQRT(@) SQRT(3) SQRT(NULL) SQRT(2)
"""""""""" """""""""" """"""""""""""" """"""""""
1 3 2
d) MOD
This will give the e$ainde5
S7nta/0 $od (value, divi!or )
'/0
SQL- sele!t $od(V#@)# $od(2#)# $od(null#null)# $od(3#3)# $od("V#@) fo$ dual.
MOD(V#@) MOD(2#) MOD(NULL#NULL) MOD(3#3) MOD("V#@)
"""""""""""" """""""""" """"""""""""""""""""" """"""""""" """""""""""""
4 2 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 fo$ student. "" hee fo 4d ow $a&s value is null
NO NM' MRFS
""" """"""" """""""""
2 a 233
1 % 133
4 !
© Copy rights are reserved.
32
-
8/20/2019 SQL Plsql Document
33/319
SQL- sele!t no# na$e# nvl($a&s#433) fo$ student.
NO NM' N9L(MRFS#433)
""" """"""" """"""""""""""""""""" 2 a 233
1 % 133
4 ! 433
SQL- sele!t nvl(2#1)# nvl(1#4)# nvl(@#4)# nvl(#@) fo$ dual.
N9L(2#1) N9L(1#4) N9L(@#4) N9L(#@)
"""""""""" """""""""" """""""""" """"""""""
2 1 @
SQL- sele!t nvl(3#3)# nvl(2#2)# nvl(null#null)# nvl(@#@) fo$ dual.
N9L(3#3) N9L(2#2) N9L(null#null) N9L(@#@)
"""""""""" """""""""" """"""""""""""""" """"""""""
3 2 @
f) AO6'R
Aowe is the a%ilit7 to aise a value to a given e/ponent5
S7nta/0 powe (value, eponent )
'/0
SQL- sele!t powe(1#)# powe(3#3)# powe(2#2)# powe(null#null)# powe(1#")
fo$ dual.
AO6'R(1#) AO6'R(3#3) AO6'R(2#2) AO6'R(NULL#NULL) AO6'R(1#")
"""""""""""""" """""""""""""" """"" """"""""" """"""""""""""""""""""" """""""""""""""
41 2 2 53421
© Copy rights are reserved.
33
-
8/20/2019 SQL Plsql Document
34/319
g) '+A
This will aise e value to the give powe5
S7nta/0 e/p (value) '/0
SQL- sele!t e/p(2)# e/p(1)# e/p(3)# e/p(null)# e/p("1) fo$ dual.
'+A(2) '+A(1) '+A(3) '+A(NULL) '+A("1)
"""""""" """"""""" """""""" """"""""""""" """"""""""
15V2W1W2W4 V54W32 2 524441W4
h) LN
This is %ased on natual o %ase e logaith$5
S7nta/0 ln (value) "" hee value $ust %e geate than eo whi!h is positive onl75
'/0
SQL- sele!t ln(2)# ln(1)# ln(null) fo$ dual.
LN(2) LN(1) LN(NULL)
""""""" """"""" """"""""""""
3 542@V2W2
Ln and '/p ae e!ipo!al to ea!h othe5
'+A (4) H 1353W4
LN (1353W4) H 4
i) LO8
This is %ased on 23 %ased logaith$5
S7nta/0 log (23# value)"" hee value $ust %e geate than eo whi!h is positive onl75
© Copy rights are reserved.
34
-
8/20/2019 SQL Plsql Document
35/319
'/0
SQL- sele!t log(23#233)# log(23#1)# log(23#2)# log(23#null) fo$ dual.
LO8(23#233) LO8(23#1) LO8(23#2) LO8(23#NULL)
""""""""""""""" """"""""""" """""""""""" """"""""""""""""" 1 543231 3
LN (value) H LO8 ('+A(2)# value)
SQL- sele!t ln(4)# log(e/p(2)#4) fo$ dual.
LN(4) LO8('+A(2)#4)
""""""" """""""""""""""""
253W211 253W211
J) C'IL
This will podu!e a whole nu$%e that is geate than o eual to the spe!ified value5
S7nta/0 !eil (value)
'/0
SQL- sele!t !eil()# !eil(52)# !eil(")# !eil( "52)# !eil(3)# !eil(null) fo$ dual.
C'IL() C'IL(52) C'IL(") C'IL("52) C'IL(3) C'IL(NULL)
""""""""" """"""""""" """""""""" """""""""""" """""""" """"""""""""""
" " 3
&) BLOOR
This will podu!e a whole nu$%e that is less than o eual to the spe!ified value5
S7nta/0 floo (value)
'/0
SQL- sele!t floo()# floo(52)# floo(")# floo( "52)# floo(3)# floo(null) fo$
© Copy rights are reserved.
35
-
8/20/2019 SQL Plsql Document
36/319
dual.
BLOOR() BLOOR(52) BLOOR(") BLOOR("52) BLOOR(3) BLOOR(NULL)
""""""""""" """"""""""""" """""""""""" """""""""""""" """"""""""" """"""""""""""""
" " 3l) ROUND
This will ounds nu$%es to a given nu$%e of digits of pe!ision5
S7nta/0 ound (value, preci!ion)
'/0
SQL- sele!t ound(214514@)# ound(214514@#1)# ound(214514@#1) fo$ dual.
ROUND(214514@) ROUND(214514@#3) ROUND(214514@#1) ROUND(214514@#1)
""""""""""""""""""""" """""""""""""""""""""""" """"""""""""""""""""""" """""""""""""""""""""""
214 214 214514 21451@
SQL- sele!t ound(214514@#"2)# ound(214514@#"1)# ound(214514@#"4)#
ound(214514@#"@) fo$ dual.
ROUND(214514@#"2) ROUND(214514@#"1) ROUND(214514@#"4) ROUND(214514@#"@)"""""""""""""""""""""""" """"""""""""""""""""""""" """""""""""""""""""""""" """"""""""""""""""""""""
213 233 3 3
SQL- sele!t ound(214#3)# ound(214#2)# ound(214#1) fo$ 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) fo$ dual.
ROUND("214#3) ROUND("214#2) ROUND("214#1)
"""""""""""""""""" """"""""""""""""" """""""""""""""""""
"214 "214 "214
© Copy rights are reserved.
36
-
8/20/2019 SQL Plsql Document
37/319
SQL- sele!t ound(214#"2)# ound(214#"1)# ound(214#"4)# ound("214#"2)# ound(
"214#"1)# ound("214#"4) fo$ dual.
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)
fo$ dual.
ROUND(NULL#NULL) ROUND(3#3) ROUND(2#2) ROUND("2#"2) ROUND("1#"1)
""""""""""""""""""""""" """""""""""""" """""""""""""" """""""""""""""" """"""""""""""""
3 2 3 3
$) TRUNC
This will tun!ates o !hops off digits of pe!ision fo$ a nu$%e5
S7nta/0 tun! (value, preci!ion)
'/0
SQL- sele!t tun!(214514@)# tun!(214514@#1)# tun!(214514@#1) fo$ dual.
TRUNC(214514@) TRUNC(214514@#1) TRUNC(214514@#1)
""""""""""""""""""""" """"""""""""""""""""""" """""""""""""""""""""""
214 214514 214514
SQL- sele!t tun!(214514@#"2)# tun!(214514@#"1)# tun!(214514@#"4)#
tun!(214514@#"@) fo$ dual.
TRUNC(214514@#"2) TRUNC(214514@#"1) TRUNC(214514@#"4) TRUNC(214514@#"@)
"""""""""""""""""""""""" """""""""""""""""""""""" """"""""""""""""""""""" """"""""""""""""""""""""
213 233 3 3
© Copy rights are reserved.
37
-
8/20/2019 SQL Plsql Document
38/319
SQL- sele!t tun!(214#3)# tun!(214#2)# tun!(214#1) fo$ dual.
TRUNC(214#3) TRUNC(214#2) TRUNC(214#1)"""""""""""""""" """""""""""""""" """""""""""""""""
214 214 214
SQL- sele!t tun!("214#3)# tun!("214#2)# tun!("214#1) fo$ dual.
TRUNC("214#3) TRUNC("214#2) TRUNC("214#1)
""""""""""""""""" """"""""""""""""" """""""""""""""""
"214 "214 "214
SQL- sele!t tun!(214#"2)# tun!(214#"1)# tun!(214#"4)# tun!("214#"2)# tun!(
"214#1)# tun!("214#"4) fo$ 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 tun!(null#null)# tun!(3#3)# tun!(2#2)# tun!("2#"2)# tun!("1#"1) fo$
dual.
TRUNC(NULL#NULL) TRUNC(3#3) TRUNC(2#2) TRUNC("2#"2) TRUNC("1#"1)
""""""""""""""""""""""" """"""""""""" """"""""""""" """"""""""""""" """"""""""""""""
3 2 3 3
n) ITND
This will pefo$ %itwise and opeation5
S7nta/0 %itand (value1, value2)
© Copy rights are reserved.
38
-
8/20/2019 SQL Plsql Document
39/319
'/0
SQL- sele!t %itand(1#4)# %itand(3#3)# %itand(2#2)# %itand(null#null)# %itand("1#"4)
fo$ dual.
ITND(1#4) ITND(3#3) ITND(2#2) ITND(NULL#NULL) ITND("1#"4)"""""""""""""" """"""""""""""" """""""""""""" """""""""""""""""""""""" """""""""""""""""
1 3 2 "@
o) 8R'T'ST
This will give the geatest nu$%e5
S7nta/0 geatest (value1, value2, value3 … valuen)
'/0
SQL- sele!t geatest(2# 1# 4)# geatest("2# "1# "4) fo$ dual.
8R'T'ST(2#1#4) 8R'T'ST("2#"1#"4)
"""""""""""""""""""" """""""""""""""""""""""
4 "2
If all the values ae eos then it will displa7 eo5
If all the paa$etes ae nulls then it will displa7 nothing5
If an7 of the paa$etes is null it will displa7 nothing5
p) L'ST
This will give the least nu$%e5
S7nta/0 least (value1, value2, value3 … valuen)
'/0
SQL- sele!t least(2# 1# 4)# least("2# "1# "4) fo$ dual.
L'ST(2#1#4) L'ST("2#"1#"4)
© Copy rights are reserved.
39
-
8/20/2019 SQL Plsql Document
40/319
"""""""""""""""""""" """""""""""""""""""""""
2 "4
If all the values ae eos then it will displa7 eo5
If all the paa$etes ae nulls then it will displa7 nothing5
If an7 of the paa$etes is null it will displa7 nothing5
) COL'SC'
This will etun fist 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#) fo$ dual.
COL'SC'(2#1#4) COL'SC'(NULL#1#NULL#)
""""""""""""""""""" """""""""""""""""""""""""""""""
2 1
STRIN8 BUNCTIONS
Init!ap
Uppe
Lowe
Length
Rpad
Lpad
Lti$
Rti$
Ti$
Tanslate
Repla!e
Sounde/
Con!at ( K XX K Con!atenation opeato)
s!ii
Ch
© Copy rights are reserved.
40
-
8/20/2019 SQL Plsql Document
41/319
Su%st
Inst
De!ode
8eatest
Least
Coales!e
a) INITCA
This will !apitalie the initial lette of the sting5
S7nta/0 init!ap ( !trin")
'/0
SQL- sele!t init!ap(=!o$pute=) fo$ dual.
INITCA
"""""""""""
Co$pute
%) UAA'R
This will !onvet the sting into uppe!ase5
S7nta/0 uppe ( !trin")
'/0
SQL- sele!t uppe(=!o$pute=) fo$ dual.
UAA'R
"""""""""""
COMAUT'R
!) LO6'R
This will !onvet the sting into lowe!ase5
© Copy rights are reserved.
41
-
8/20/2019 SQL Plsql Document
42/319
S7nta/0 lowe ( !trin")
'/0
SQL- sele!t lowe(=COMAUT'R =) fo$ dual.
LO6'R
"""""""""""
!o$pute
d) L'N8T:
This will give length of the sting5
S7nta/0 length ( !trin")
'/0
SQL- sele!t length(=!o$pute=) fo$ dual.
L'N8T:
"""""""""""
W
e) RAD
This will allows 7ou to pad the ight side of a !olu$n with an7 set of !haa!tes5
S7nta/0 pad ( !trin", len"t *, paddin"_car+)
'/0
SQL- sele!t pad(=!o$pute=#2#=E=)# pad(=!o$pute=#2#=EY=) fo$ dual.
RAD(=COMAUT'R= RAD(=COMAUT'R=
"""""""""""""""""""""" """"""""""""""""""""""
© Copy rights are reserved.
42
-
8/20/2019 SQL Plsql Document
43/319
!o$puteEEEEEEE !o$puteEYEYEYE
"" Default padding !haa!te was %lan& spa!e5
f) LAD
This will allows 7ou to pad the left side of a !olu$n with an7 set of !haa!tes5
S7nta/0 lpad ( !trin", len"t *, paddin"_car+)
'/0
SQL- sele!t lpad(=!o$pute=#2#=E=)# lpad(=!o$pute=#2#=EY=) fo$ dual.
LAD(=COMAUT'R= LAD(=COMAUT'R=
""""""""""""""""""""" """""""""""""""""""""
EEEEEEE!o$pute EYEYEYE!o$pute
"" Default padding !haa!te was %lan& spa!e5
g) LTRIM
This will ti$ off unwanted !haa!tes fo$ the left end of sting5
S7nta/0 lti$ ( !trin" *,unwanted_car!+)
'/0
SQL- sele!t lti$(=!o$pute=#=!o=)# lti$(=!o$pute=#=!o$=) fo$ dual.
LTRIM( LTRIM
"""""""" """""""""
$pute pute
SQL- sele!t lti$(=!o$pute=#=pute=)# lti$(=!o$pute=#=o$pute=) fo$ dual.
LTRIM(=C LTRIM(=C
"""""""""" """"""""""
© Copy rights are reserved.
43
-
8/20/2019 SQL Plsql Document
44/319
!o$pute !o$pute
"" If 7ou haven;t spe!if7 an7 unwanted !haa!tes it will displa7 entie sting5
h) RTRIM
This will ti$ off unwanted !haa!tes fo$ the ight end of sting5
S7nta/0 ti$ ( !trin" *, unwanted_car!+)
'/0
SQL- sele!t ti$(=!o$pute=#=e=)# ti$(=!o$pute=#=te=) fo$ dual.
RTRIM( RTRIM
"""""""" """""""""
!o$put !o$pu
SQL- sele!t ti$(=!o$pute=#=!o$put;)# ti$(=!o$pute=#=!o$pute=) fo$ dual.
RTRIM(=C RTRIM(=C
"""""""""" """"""""""
!o$pute !o$pute
"" If 7ou haven;t spe!if7 an7 unwanted !haa!tes it will displa7 entie sting5
i) TRIM
This will ti$ off unwanted !haa!tes fo$ the %oth sides of sting5
S7nta/0 ti$ (unwanted_car! fo$ !trin")
'/0
SQL- sele!t ti$( =i= fo$ =indiani=) fo$ dual.
TRIM(
"""""
ndian
© Copy rights are reserved.
44
-
8/20/2019 SQL Plsql Document
45/319
SQL- sele!t ti$( leading=i= fo$ =indiani=) fo$ dual. "" this will wo& as LTRIM
TRIM(L
""""""
ndiani
SQL- sele!t ti$( tailing=i= fo$ =indiani=) fo$ dual. "" this will wo& as RTRIM
TRIM(T
""""""
Indian
J) TRNSLT'
This will epla!e the set of !haa!tes# !haa!te %7 !haa!te5
S7nta/0 tanslate ( !trin", old_car!, new_car!)
'/0
SQL- sele!t tanslate(=india=#=in=#=/7=) fo$ dual.
TRNS
""""""""
/7d/a
&) R'ALC'
This will epla!e the set of !haa!tes# sting %7 sting5
S7nta/0 epla!e ( !trin", old_car! *, new_car!+)
'/0
SQL- sele!t epla!e(=india=#=in=#=/7=)# epla!e(Kindia;#;in;) fo$ dual.
R'ALC' R'ALC'
© Copy rights are reserved.
45
-
8/20/2019 SQL Plsql Document
46/319
""""""""""" """""""""""
+7dia dia
l) SOUND'+
This will %e used to find wods that sound li&e othe wods# e/!lusivel7 used in whee
!lause5
S7nta/0 sounde/ ( !trin")
'/0
SQL- sele!t E fo$ e$p whee sounde/(ena$e) H sounde/(=SMIT=).
'MANO 'NM' >O M8R :IR'DT' SL D'ATNO
"""""""" """""""" """"" """"" """""""""""" """"""""" """"""""""
V4 SMIT: CL'RF V31 2V"D'C"W3 33 13
$) CONCT
This will %e used to !o$%ine two stings onl75
S7nta/0 !on!at ( !trin"1, !trin"2)
'/0
SQL- sele!t !on!at(=!o$pute=#= opeato=) fo$ dual.
CONCT(=COMAUT'R=
"""""""""""""""""""""""""
!o$pute opeato
If 7ou want to !o$%ine $oe than two stings 7ou have to use !on!atenationopeato(XX)5
SQL- sele!t =how= XX = ae= XX = 7ou= fo$ dual.
=:O6=XX=R'
© Copy rights are reserved.
46
-
8/20/2019 SQL Plsql Document
47/319
"""""""""""""""
how ae 7ou
n) SCII
This will etun the de!i$al epesentation in the data%ase !haa!te set of the fist
!haa!te of the sting5
S7nta/0 as!ii ( !trin")
'/0
SQL- sele!t as!ii(=a=)# as!ii(=apple=) fo$ dual.
SCII(==) SCII(=AAL'=)
"""""""""""" """"""""""""""""""
V V
o) C:R
This will etun the !haa!te having the %ina7 euivalent to the sting in eithe the
data%ase !haa!te set o the national !haa!te set5
S7nta/0 !h (number )
'/0
SQL- sele!t !h(V) fo$ dual.
C:R
"""""
a
p) SUSTR
This will %e used to e/ta!t su%stings5
© Copy rights are reserved.
47
-
8/20/2019 SQL Plsql Document
48/319
S7nta/0 su%st ( !trin", !tart_cr_count *, no_o'_car!+)
'/0
SQL- sele!t su%st(=!o$pute=#1)# su%st(=!o$pute=#1#)# su%st(=!o$pute=#4#V)
fo$ dual.
SUSTR( SUST SUSTR """""""""" """"""" """"""""o$pute o$put $pute
If no_o'_car! paa$ete is negative then it will displa7 nothing5
If %oth paa$etes e/!ept !trin" ae null o eos then it will displa7 nothing5
If no_o'_car! paa$ete is geate than the length of the sting then it
ignoes and !al!ulates %ased on the oginal sting length5
If !tart_cr_count is negative then it will e/ta!t the su%sting fo$ ight
end5
2 1 4 @ V W
C O M A U T ' R
"W "V " " "@ "4 "1 "2
) INSTR
This will allows 7ou fo sea!hing though a sting fo set of !haa!tes5
S7nta/0 inst ( !trin", !earc_!tr *, !tart_cr_count *, occurrence+ +)
'/0
SQL- sele!t inst(=info$ation=#=o=#@#2)# inst(=info$ation=#=o=#@#1) fo$ dual.
INSTR(=INBORMTION=#=O=#@#2) INSTR(=INBORMTION=#=O=#@#1)
"""""""""""""""""""""""""""""""""""" """""""""""""""""""""""""""""""""""""
@ 23
© Copy rights are reserved.
48
-
8/20/2019 SQL Plsql Document
49/319
If 7ou ae not spe!if7ing !tart_cr_count and occurrence then it will stat
sea!h fo$ the %eginning and finds fist o!!uen!e onl75
If %oth paa$etes !tart_cr_count and occurrence ae null# it will displa7
nothing5
) D'COD'
De!ode will a!t as value %7 value su%stitution5
Bo eve7 value of field# it will !he!&s fo a $at!h in a seies of if?then tests5
S7nta/0 de!ode (value, i'1, ten1, i'2, ten2, ……. el!e).
'/0
SQL- sele!t sal# de!ode(sal#33#=Low=#333#=:igh=#=Mediu$=) fo$ e$p.
SL D'COD'
""""" """""""""
33 Low
133 Mediu$
1333 Mediu$
433 Mediu$
4333 Mediu$
333 :igh
@333 Mediu$
333 :igh
2W33 Mediu$
2133 Mediu$
1333 Mediu$
1V33 Mediu$
1133 Mediu$ 4133 Mediu$
SQL- sele!t de!ode(2#2#4)# de!ode(2#1#4#@#@#) fo$ dual.
D'COD'(2#2#4) D'COD'(2#1#4#@#@#)""""""""""""""""" """"""""""""""""""""""""
© Copy rights are reserved.
49
-
8/20/2019 SQL Plsql Document
50/319
4
If the nu$%e of paa$etes ae odd and diffeent then de!ode will displa7
nothing5
If the nu$%e of paa$etes ae even and diffeent then de!ode will displa7 last
value5
If all the paa$etes ae null then de!ode will displa7 nothing5
If all the paa$etes ae eos then de!ode will displa7 eo5
s) 8R'T'ST
This will give the geatest sting5
S7nta/0 geatest ( !trn"1, !trin"2, !trin"3 … !trin"n)
'/0
SQL- sele!t geatest(=a=# =%=# =!=)# geatest(=satish=#=sinu=#=sað=) fo$ dual.
8R'T 8R'T
""""""" """""""
! sinu
If all the paa$etes ae nulls then it will displa7 nothing5
If an7 of the paa$etes is null it will displa7 nothing5
t) L'ST
This will give the least sting5
S7nta/0 geatest ( !trn"1, !trin"2, !trin"3 … !trin"n)
'/0
SQL- sele!t least(=a=# =%=# =!=)# least(=satish=#=sinu=#=sað=) fo$ dual.
L'ST L'ST
© Copy rights are reserved.
50
-
8/20/2019 SQL Plsql Document
51/319
""""""" """""""
a sað
If all the paa$etes ae nulls then it will displa7 nothing5
If an7 of the paa$etes is null it will displa7 nothing5
u) COL'SC'
This will gives the fist non"null sting5
S7nta/0 !oales!e ( !trn"1, !trin"2, !trin"3 … !trin"n)
'/0
SQL- sele!t !oales!e(=a=#=%=#=!=)# !oales!e(null#=a=#null#=%=) fo$ dual.
COL'SC' COL'SC'
""""""""""" """""""""""
a a
DT' BUNCTIONS
S7sdate
Cuentdate
Cuentti$esta$p
S7sti$esta$p
Lo!alti$esta$p
D%ti$eone
Sessionti$eone
To!ha
Todate
dd$onths
Months%etween
Ne/tda7
Lastda7
'/ta!t
© Copy rights are reserved.
51
-
8/20/2019 SQL Plsql Document
52/319
8eatest
Least
Round
Tun!
Newti$e
Coales!e
Oa!le default date fo$at is DD"MON"**5
6e !an !hange the default fo$at to ou desied fo$at %7 using the following !o$$and5
SQL- alte session set nlsdatefo$at H KDD"MONT:"****;.
ut this will e/pie on!e the session was !losed5
a) S*SDT'
This will give the !uent date and ti$e5
'/0
SQL- sele!t s7sdate fo$ dual.
S*SDT'
"""""""""""
1@"D'C"3
%) CURR'NTDT'
This will etuns the !uent date in the session;s ti$eone5
'/0
SQL- sele!t !uentdate fo$ dual.
CURR'NTDT'
""""""""""""""""""
1@"D'C"3
!) CURR'NTTIM'STMA
© Copy rights are reserved.
52
-
8/20/2019 SQL Plsql Document
53/319
This will etuns the !uent ti$esta$p with the a!tive ti$e one info$ation5
'/0
SQL- sele!t !uentti$esta$p fo$ dual.
CURR'NTTIM'STMA
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
1@"D'C"3 345@15@254W44 M G3043
d) S*STIM'STMA
This will etuns the s7ste$ date# in!luding fa!tional se!onds and ti$e one of the
data%ase5
'/0
SQL- sele!t s7sti$esta$p fo$ dual.
S*STIM'STMA
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
1@"D'C"3 345@5425W433 M G3043
e) LOCLTIM'STMA
This will etuns lo!al ti$esta$p in the a!tive ti$e one info$ation# with no ti$e
one info$ation shown5
'/0
SQL- sele!t lo!alti$esta$p fo$ dual.
LOCLTIM'STMA"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""1@"D'C"3 345@@52W531WV@ M
f) DTIM'ZON'
This will etuns the !uent data%ase ti$e one in UTC fo$at5 (Coodinated Univesal
© Copy rights are reserved.
53
-
8/20/2019 SQL Plsql Document
54/319
Ti$e)
'/0
SQL- sele!t d%ti$eone fo$ dual.
DTIM'ZON'""""""""""""""" "3V033
g) S'SSIONTIM'ZON'
This will etuns the value of the !uent session;s ti$e one5
'/0
SQL- sele!t sessionti$eone fo$ dual.
S'SSIONTIM'ZON'
""""""""""""""""""""""""""""""""""""
G3043
h) TOC:R
This will %e used to e/ta!t vaious date fo$ats5
The availa%le date fo$ats as follows5
S7nta/0 to!ha (date# 'ormat )
DT' BORMTS
D "" No of da7s in wee&
DD "" No of da7s in $onth
DDD "" No of da7s in 7ea
MM "" No of $onth
MON "" Thee lette a%%eviation of $onth
MONT: "" Bull7 spelled out $onth
RM "" Ro$an nu$eal $onth
D* "" Thee lette a%%eviated da7
© Copy rights are reserved.
54
-
8/20/2019 SQL Plsql Document
55/319
D* "" Bull7 spelled out da7
* "" Last one digit of the 7ea
** "" Last two digits of the 7ea
*** "" Last thee digits of the 7ea
**** "" Bull fou digit 7eaS**** "" Signed 7ea
I "" One digit 7ea fo$ ISO standad
I* "" Two digit 7ea fo$ ISO standad
I** "" Thee digit 7ea fo$ ISO standad
I*** "" Bou digit 7ea fo$ ISO standad
*# *** "" *ea with !o$$a
*'R "" Bull7 spelled out 7ea
CC "" Centu7
Q "" No of uates
6 "" No of wee&s in $onth
66 "" No of wee&s in 7ea
I6 "" No of wee&s in 7ea fo$ ISO standad
:: "" :ous
MI "" Minutes
SS "" Se!onds
BB "" Ba!tional se!onds
M o AM "" Displa7s M o AM depending upon ti$e of da7
5M o A5M "" Displa7s 5M o A5M depending upon ti$e of da7
D o C "" Displa7s D o C depending upon the date
5D o 5C "" Displa7s D o C depending upon the date
BM "" Aefi/ to $onth o da7# suppesses padding of $onth o da7
T: "" Suffi/ to a nu$%e
SA "" suffi/ to a nu$%e to %e spelled out
SAT: "" Suffi/ !o$%ination of T: and SA to %e %oth spelled out
T:SA "" sa$e as SAT:
'/0
SQL- sele!t to!ha(s7sdate#=dd $onth 7777 hh0$i0ss a$ d7=) fo$ dual.
TOC:R(S*SDT'#=DD MONT: ****::0MI
© Copy rights are reserved.
55
-
8/20/2019 SQL Plsql Document
56/319
""""""""""""""""""""""""""""""""""""""""""""""""""""
1@ de!e$%e 133 31034014 p$ sun
SQL- sele!t to!ha(s7sdate#=dd $onth 7ea=) fo$ dual.
TOC:R(S*SDT'#=DDMONT:*'R=)
"""""""""""""""""""""""""""""""""""""""""""""""""""""""
1@ de!e$%e two thousand si/
SQL- sele!t to!ha(s7sdate#=dd f$$onth 7ea=) fo$ dual.
TOC:R(S*SDT'#=DD BMMONT: *'R=)
"""""""""""""""""""""""""""""""""""""""""""""""""""""""
1@ de!e$%e two thousand si/
SQL- sele!t to!ha(s7sdate#=ddth DDT:=) fo$ dual.
TOC:R(S
""""""""""""
1@th 1@T:
SQL- sele!t to!ha(s7sdate#=ddspth DDSAT:=) fo$ dual.
TOC:R(S*SDT'#=DDSAT:DDSAT:
""""""""""""""""""""""""""""""""""""""""""
twent7"fouth T6'NT*"BOURT:
SQL- sele!t to!ha(s7sdate#=ddsp Ddsp DDSA =) fo$ dual.
TOC:R(S*SDT'#=DDSADDSADDSA=)
""""""""""""""""""""""""""""""""""""""""""""""""
twent7"fou Twent7"Bou T6'NT*"BOUR
i) TODT'
© Copy rights are reserved.
56
-
8/20/2019 SQL Plsql Document
57/319
This will %e used to !onvet the sting into data fo$at5
S7nta/0 todate (date)
'/0 SQL- sele!t to!ha(todate(=1@?de!?133=#=dd?$on?7777=)# =dd E $onth E da7=)
fo$ dual.
TOC:R(TODT'(=1@?D'C?13
""""""""""""""""""""""""""
1@ E de!e$%e E Sunda7
"" If 7ou ae not using to!ha oa!le will displa7 output in default date fo$at5
J) DDMONT:S
This will add the spe!ified $onths to the given date5
S7nta/0 add$onths (date, no_o'_mont!)
'/0
SQL- sele!t add$onths(todate(=22"Jan"23=#=dd"$on"7777=)# ) fo$ dual.
DDMONT:S
""""""""""""""""
22">UN"3
SQL- sele!t add$onths(todate(=22"Jan"23=#=dd"$on"7777=)# ") fo$ dual.
DDMONT:
"""""""""""""""
22"U8"W
If no_o'_mont! is eo then it will displa7 the sa$e date5
If no_o'_mont! is null then it will displa7 nothing5
© Copy rights are reserved.
57
-
8/20/2019 SQL Plsql Document
58/319
&) MONT:S'T6''N
This will give diffeen!e of $onths %etween two dates5
S7nta/0 $onths%etween (date1, date2)
'/0
SQL- sele!t $onths%etween(todate(=22"aug"23=#=dd"$on"7777=)# todate(=22"
Jan"23=#=dd"$on"7777=)) fo$ dual.
MONT:S'T6''N(TODT'(=22"U8"23=#=DD"MON"****=)#TODT'(=22">N"
23=#=DD"MON"****=))
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
V
SQL- sele!t $onths%etween(todate(=22"Jan"23=#=dd"$on"7777=)# todate(=22"
aug"23=#=dd"$on"7777=)) fo$ dual.
MONT:S'T6''N(TODT'(=22">N"23=#=DD"MON"****=)#TODT'(=22"U8"
23=#=DD"MON"****=))
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"V
l) N'+TD*
This will podu!e ne/t da7 of the given da7 fo$ the spe!ified date5
S7nta/0 ne/tda7 (date, day )
'/0
SQL- sele!t ne/tda7(todate(=1@"de!"133=#=dd"$on"7777=)#=sun=) fo$ dual.
N'+TD*(
"""""""""""""
© Copy rights are reserved.
58
-
8/20/2019 SQL Plsql Document
59/319
42"D'C"3
"" If the da7 paa$ete is null then it will displa7 nothing5
$) LSTD*
This will podu!e last da7 of the given date5
S7nta/0 lastda7 (date)
'/0
SQL- sele!t lastda7(todate(=1@"de!"133=#=dd"$on"7777=)#=sun=) fo$ dual.
LSTD*(
"""""""""""""
42"D'C"3
n) '+TRCT
This is used to e/ta!t a potion of the date value5
S7nta/0 e/ta!t ((7ea X $onth X da7 X hou X $inute X se!ond)# date)
'/0
SQL- sele!t e/ta!t(7ea fo$ s7sdate) fo$ dual.
'+TRCT(*'RBROMS*SDT')
""""""""""""""""""""""""""""""""""""
133
"" *ou !an e/ta!t onl7 one value at a ti$e5
o) 8R'T'ST
This will give the geatest date5
S7nta/0 geatest (date1, date2, date3 … daten)
© Copy rights are reserved.
59
-
8/20/2019 SQL Plsql Document
60/319
'/0
SQL- sele!t geatest(todate(=22"Jan"3=#=dd"$on"77=)#todate(=22"$a"3=#=dd"
$on"77=)#todate(=22"ap"3=#=dd"$on"77=)) fo$ dual.
8R'T'ST(
"""""""""""""
22"AR"3
p) L'ST
This will give the least date5
S7nta/0 least (date1, date2, date3 … daten)
'/0
SQL- sele!t least(todate(=22"Jan"3=#=dd"$on"77=)#todate(=22"$a"3=#=dd"$on"
77=)#todate(=22"ap"3=#=dd"$on"77=)) fo$ dual.
L'ST(
"""""""""""""
22">N"3
) ROUND
Round will ounds the date to whi!h it was eual to o geate than the given date5
S7nta/0 ound (date, ( da7 X $onth X 7ea ))
If the se!ond paa$ete was year then ound will !he!&s the $onth of the given date in
the following anges5
>N "" >UN
>UL "" D'C
If the $onth falls %etween >N and >UN then it etuns the fist da7 of the !uent 7ea5
If the $onth falls %etween >UL and D'C then it etuns the fist da7 of the ne/t 7ea5
© Copy rights are reserved.
60
-
8/20/2019 SQL Plsql Document
61/319
If the se!ond paa$ete was mont then ound will !he!&s the da7 of the given date in
the following anges5
2 "" 2
2 "" 42
If the da7 falls %etween 2 and 2 then it etuns the fist da7 of the !uent $onth5
If the da7 falls %etween 2 and 42 then it etuns the fist da7 of the ne/t $onth5
If the se!ond paa$ete was day then ound will !he!&s the wee& da7 of the given date
in the following anges5
SUN "" 6'DT:U "" SUN
If the wee& da7 falls %etween SUN and 6'D then it etuns the pevious sunda75
If the wee&da7 falls %etween T:U and SUN then it etuns the ne/t sunda75
If the se!ond paa$ete was null then it etuns nothing5
If the 7ou ae not spe!if7ing the se!ond paa$ete then ound will esets the ti$e
to the %egining of the !uent da7 in !ase of use spe!ified date5
If the 7ou ae not spe!if7ing the se!ond paa$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(todate(=1@"de!"3@=#=dd"$on"77=)#=7ea=)# ound(todate(=22"$a"
3=#=dd"$on"77=)#=7ea=) fo$ dual.
ROUND(TO ROUND(TO
"""""""""""" """""""""""""""32">N"3 32">N"3
SQL- sele!t ound(todate(=22"Jan"3@=#=dd"$on"77=)#=$onth=)# ound(todate(=2W"
Jan"3@=#=dd"$on"77=)#=$onth=) fo$ dual.
© Copy rights are reserved.
61
-
8/20/2019 SQL Plsql Document
62/319
ROUND(TO ROUND(TO
""""""""""""" """""""""""""""
32">N"3@ 32"B'"3@
SQL- sele!t ound(todate(=1"de!"3=#=dd"$on"77=)#=da7=)# ound(todate(=1"de!" 3=#=dd"$on"77=)#=da7=) fo$ dual.
ROUND(TO ROUND(TO
"""""""""""""" """"""""""""""
1@"D'C"3 42"D'C"3
SQL- sele!t to!ha(ound(todate(=1@"de!"3=#=dd"$on"77=))# =dd $on 7777
hh0$i0ss a$=) fo$ dual.
TOC:R(ROUND(TODT'(=
"""""""""""""""""""""""""""""""""
1@ de! 133 21033033 a$
) TRUNC
Tun! will !hops off the date to whi!h it was eual to o less than the given date5
S7nta/0 tun! (date, ( da7 X $onth X 7ea ))
If the se!ond paa$ete was year then it alwa7s etuns the fist da7 of the !uent
7ea5
If the se!ond paa$ete was mont then it alwa7s etuns the fist da7 of the
!uent $onth5
If the se!ond paa$ete was day then it alwa7s etuns the pevious sunda75
If the se!ond paa$ete was null then it etuns nothing5
If the 7ou ae not spe!if7ing the se!ond paa$ete then tun& will esets the ti$e
to the %egining of the !uent da75
'/0
SQL- sele!t tun!(todate(=1@"de!"3@=#=dd"$on"77=)#=7ea=)# tun!(todate(=22"$a"
3=#=dd"$on"77=)#=7ea=) fo$ dual.
© Copy rights are reserved.
62
-
8/20/2019 SQL Plsql Document
63/319
TRUNC(TO TRUNC(TO
""""""""""""" """"""""""""""
32">N"3@ 32">N"3
SQL- sele!t tun!(todate(=22"Jan"3@=#=dd"$on"77=)#=$onth=)# tun!(todate(=2W"Jan" 3@=#=dd"$on"77=)#=$onth=) fo$ dual.
TRUNC(TO TRUNC(TO
""""""""""""" """""""""""""
32">N"3@ 32">N"3@
SQL- sele!t tun!(todate(=1"de!"3=#=dd"$on"77=)#=da7=)# tun!(todate(=1"de!"
3=#=dd"$on"77=)#=da7=) fo$ dual.
TRUNC(TO TRUNC(TO
""""""""""""" """"""""""""""
1@"D'C"3 1@"D'C"3
SQL- sele!t to!ha(tun!(todate(=1@"de!"3=#=dd"$on"77=))# =dd $on 7777 hh0$i0ss
a$=) fo$ dual.
TOC:R(TRUNC(TODT'(=
"""""""""""""""""""""""""""""""""1@ de! 133 21033033 a$
s) N'6TIM'
This will give the desied ti$eone;s date and ti$e5
S7nta/0 newti$e (date, current_timeone, de!ired_timeone)
vaila%le ti$eones ae as follows5
TIM'ZON'S
ST?DT "" tlanti! standad?da7 light ti$e
ST?DT "" eing standad?da7 light ti$e
© Copy rights are reserved.
63
-
8/20/2019 SQL Plsql Document
64/319
CST?CDT "" Cental standad?da7 light ti$e
'ST?'DT "" 'asten standad?da7 light ti$e
8MT "" 8eenwi!h $ean ti$e
:ST?:DT "" las&a":awaii standad?da7 light ti$e
MST?MDT "" Mountain standad?da7 light ti$eNST "" Newfoundland standad ti$e
AST?ADT "" Aa!ifi! standad?da7 light ti$e
*ST?*DT "" *u&on standad?da7 light ti$e
'/0
SQL- sele!t to!ha(newti$e(s7sdate#=g$t=#=7st=)#=dd $on 7777 hh0$i0ss a$=) fo$
dual.
TOC:R(N'6TIM'(S*SDT
"""""""""""""""""""""""""""""""""""
1@ de! 133 3102013 p$
SQL- sele!t to!ha(newti$e(s7sdate#=g$t=#=est=)#=dd $on 7777 hh0$i0ss a$=) fo$
dual.
TOC:R(N'6TIM'(S*SDT
"""""""""""""""""""""""1@ de! 133 30201 p$
t) COL'SC'
This will give the fist non"null date5
S7nta/0 !oales!e (date1, date2, date3 … daten)
'/0
SQL- sele!t !oales!e(=21"Jan"3=#=24"Jan"=)# !oales!e(null#=21"Jan"3=#=14"$a"
W=#null) fo$ dual.
COL'SC'( COL'SC'(
""""""""""""" """"""""""""
© Copy rights are reserved.
64
-
8/20/2019 SQL Plsql Document
65/319
21"Jan"3 21"Jan"3
MISC'LLN'OUS BUNCTIONS
Uid
Use
9sie
Ran&
Densean&
a) UID
This will etuns the intege value !oesponding to the use !uentl7 logged in5
'/0
SQL- sele!t uid fo$ dual.
UID
""""""""""
42
%) US'R
This will etuns the login;s use na$e5
'/0
SQL- sele!t use fo$ dual.
US'R
""""""""""""""""
SF'T:
!) 9SIZ'
This will etuns the nu$%e of %7tes in the e/pession5
© Copy rights are reserved.
65
-
8/20/2019 SQL Plsql Document
66/319
'/0
SQL- sele!t vsie(214)# vsie(=!o$pute=)# vsie(=21"Jan"3=) fo$ dual.
9SIZ'(214) 9SIZ'(=COMAUT'R=) 9SIZ'(=21">N"3=)
""""""""""""" """"""""""""""""""""""" """""""""""""""""""""" 4 W
d) RNF
This will give the non"seuential an&ing5
'/0
SQL- sele!t ownu$#sal fo$ (sele!t sal fo$ e$p ode %7 sal des!).
RO6NUM SL
"""""""""" """"""""""
2 333
1 4333
4 4333
@ 1V
1W3
1@3
V 233
W 233
2433
23 213
22 213
21 2233
24 2333
2@ 3
2 W33
SQL- sele!t an&(1V) within goup(ode %7 sal des!) fo$ e$p.
RNF(1V)6IT:IN8ROUA(ORD'R*SLD'SC)
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
© Copy rights are reserved.
66
-
8/20/2019 SQL Plsql Document
67/319
@
d) D'NS'RNF
This will give the seuential an&ing5
'/0
SQL- sele!t densean&(1V) within goup(ode %7 sal des!) fo$ e$p.
D'NS'RNF(1V)6IT:IN8ROUA(ORD'R*SLD'SC)
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
4
CON9'RSION BUNCTIONS
intonu$
Chatoowid
Rowidto!ha
Tonu$%e
To!ha
Todate
a) INTONUM
This will !onvet the %ina7 value to its nu$ei!al euivalent5
S7nta/0 %intonu$( binary_bit!)
'/0
SQL- sele!t %intonu$(2#2#3) fo$ dual.
INTONUM(2#2#3)
""""""""""""""""""""""""
© Copy rights are reserved.
67
-
8/20/2019 SQL Plsql Document
68/319
If all the %its ae eo then it podu!es eo5
If all the %its ae null then it podu!es an eo5
%) C:RTORO6ID
This will !onvet a !haa!te sting to a!t li&e an intenal oa!le ow identifie o owid5
!) RO6IDTOC:R
This will !onvet an intenal oa!le ow identifie o owid to !haa!te sting5
d) TONUM'R
This will !onvet a !ha o va!ha to nu$%e5
e) TOC:R
This will !onvet a nu$%e o date to !haa!te sting5
f) TODT'
This will !onvet a nu$%e# !ha o va!ha to a date5
8ROUA BUNCTIONS
Su$
vg
Ma/
Min
Count
8oup fun!tions will %e applied on all the ows %ut podu!es single output5
a) SUM
© Copy rights are reserved.
68
-
8/20/2019 SQL Plsql Document
69/319
This will give the su$ of the values of the spe!ified !olu$n5
S7nta/0 su$ (column)
'/0 SQL- sele!t su$(sal) fo$ e$p.
SUM(SL)
""""""""""
4W33
%) 98
This will give the aveage of the values of the spe!ified !olu$n5
S7nta/0 avg (column)
'/0
SQL- sele!t avg(sal) fo$ e$p.
98(SL)
""""""""""""""" 1VV52@1W
!) 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) fo$ e$p.
M+(SL)
""""""""""
333
© Copy rights are reserved.
69
-
8/20/2019 SQL Plsql Document
70/319
d) MIN
This will give the $ini$u$ of the values of the spe!ified !olu$n5
S7nta/0 $in (column)
'/0
SQL- sele!t $in(sal) fo$ e$p.
MIN(SL)
""""""""""
33
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) fo$ e$p.
COUNT(SL) COUNT(E)
"""""""""""""" """"""""""""
2@ 2@
© Copy rights are reserved.
70
-
8/20/2019 SQL Plsql Document
71/319
CONSTRINTS
Constaints ae !ategoied as follows5
Do$ain integit7 !onstaints
Not null
Che!&
'ntit7 integit7 !onstaints
Uniue
Ai$a7 &e7
Refeential integit7 !onstaints
Boeign &e7
Constaints ae alwa7s atta!hed to a !olu$n not a ta%le5
6e !an add !onstaints in thee wa7s5
Colu$n level "" along with the !olu$n definition
© Copy rights are reserved.
71
-
8/20/2019 SQL Plsql Document
72/319
Ta%le level "" afte the ta%le definition
lte level "" using alte !o$$and
6hile adding !onstaints 7ou need not spe!if7 the na$e %ut the t7pe onl7# oa!le will
intenall7 na$e the !onstaint5
If 7ou want to give a na$e to the !onstaint# 7ou have to use the !onstaint !lause5
NOT NULL
This is used to avoid null values5
6e !an add this !onstaint 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)).
SQL- !eate ta%le student(no nu$%e(1) !onstaint nn not null# na$e va!ha(23)#
$a&s nu$%e(4)).
C:'CF
This is used to inset the values %ased on spe!ified !ondition5
6e !an add this !onstaint in all thee 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)).
SQL- !eate ta%le student(no nu$%e(1) # na$e va!ha(23)# $a&s nu$%e(4)
!onstaint !h !he!&($a&s - 433)).
TL' 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)).
© Copy rights are reserved.
72
-
8/20/2019 SQL Plsql Document
73/319
SQL- !eate ta%le student(no nu$%e(1) # na$e va!ha(23)# $a&s nu$%e(4)#
!onstaint !h !he!&($a&s - 433)).
LT'R L'9'L
SQL- alte ta%le student add !he!&($a&s-433).
SQL- alte ta%le student add !onstaint !h !he!&($a&s-433).
UNIQU'
This is used to avoid dupli!ates %ut it allow nulls5
6e !an add this !onstaint in all thee levels5
'/0
COLUMN L'9'L
SQL- !eate ta%le student(no nu$%e(1) uniue# na$e va!ha(23)# $a&s
nu$%e(4)).
SQL- !eate ta%le student(no nu$%e(1) !onstaint un uniue# na$e va!ha(23)#
$a&s nu$%e(4)).
TL' L'9'L
SQL- !eate ta%le student(no nu$%e(1) # na$e va!ha(23)# $a&s nu$%e(4)#
uniue(no)).
SQL- !eate ta%le student(no nu$%e(1) # na$e va!ha(23)# $a&s nu$%e(4)#
!onstaint un uniue(no)).
LT'R L'9'L
SQL- alte ta%le student add uniue(no).
SQL- alte ta%le student add !onstaint un uniue(no).
© Copy rights are reserved.
73
-
8/20/2019 SQL Plsql Document
74/319
ARIMR* F'*
This is used to avoid dupli!ates and nulls5 This will wo& as !o$%ination of uniue
and not null5
Ai$a7 &e7 alwa7s atta!hed to the paent ta%le5
6e !an add this !onstaint in all thee levels5
'/0
COLUMN L'9'L
SQL- !eate ta%le student(no nu$%e(1) pi$a7 &e7# na$e va!ha(23)# $a&s
nu$%e(4)).
SQL- !eate ta%le student(no nu$%e(1) !onstaint p& pi$a7 &e7# na$e va!ha(23)#
$a&s nu$%e(4)).
TL' L'9'L
SQL- !eate ta%le student(no nu$%e(1) # na$e va!ha(23)# $a&s nu$%e(4)#
pi$a7 &e7(no)).
SQL- !eate ta%le student(no nu$%e(1) # na$e va!ha(23)# $a&s nu$%e(4)#
!onstaint p& pi$a7 &e7(no)).
LT'R L'9'L
SQL- alte ta%le student add pi$a7 &e7(no).
SQL- alte ta%le student add !onstaint p& pi$a7 &e7(no).
BOR'I8N F'*
This is used to efeen!e the paent ta%le pi$a7 &e7 !olu$n whi!h allows
dupli!ates5
Boeign &e7 alwa7s atta!hed to the !hild ta%le5
6e !an add this !onstaint in ta%le and alte levels onl75
© Copy rights are reserved.
74
-
8/20/2019 SQL Plsql Document
75/319
'/0
TL' L'9'L
SQL- !eate ta%le e$p(e$pno nu$%e(1)# ena$e va!ha(23)# deptno nu$%e(1)#
pi$a7 &e7(e$pno)# foeign &e7(deptno) efeen!es dept(deptno)).
SQL- !eate ta%le e$p(e$pno nu$%e(1)# ena$e va!ha(23)# deptno nu$%e(1)#
!onstaint p& pi$a7 &e7(e$pno)# !onstaint f& foeign &e7(deptno) efeen!es
dept(deptno)).
LT'R L'9'L
SQL- alte ta%le e$p add foeign &e7(deptno) efeen!es dept(deptno).
SQL- alte ta%le e$p add !onstaint f& foeign &e7(deptno) efeen!es dept(deptno).
On!e the pi$a7 &e7 and foeign &e7 elationship has %een !eated then 7ou !an not
e$ove an7 paent e!od if the dependent !hilds e/ists5
USIN8 ON D'LT' CSCD'
7 using this !lause 7ou !an e$ove the paent e!od even it !hilds e/ists5
e!ause when eve 7ou e$ove paent e!od oa!le auto$ati!all7 e$oves all its
dependent e!ods fo$ !hild ta%le# if this !lause is pesent while !eating foeign &e7
!onstaint5
'/0
TL' L'9'L
SQL- !eate ta%le e$p(e$pno nu$%e(1)# ena$e va!ha(23)# deptno nu$%e(1)#
pi$a7 &e7(e$pno)# foeign &e7(deptno) efeen!es dept(deptno) on delete
!as!ade).
SQL- !eate ta%le e$p(e$pno nu$%e(1)# ena$e va!ha(23)# deptno nu$%e(1)#
!onstaint p& pi$a7 &e7(e$pno)# !onstaint f& foeign &e7(deptno) efeen!es
dept(deptno) on delete !as!ade).
LT'R L'9'L
© Copy rights are reserved.
75
-
8/20/2019 SQL Plsql Document
76/319
SQL- alte ta%le e$p add foeign &e7(deptno) efeen!es dept(deptno) on delete
!as!ade.
SQL- alte ta%le e$p add !onstaint f& foeign &e7(deptno) efeen!es dept(deptno) on
delete !as!ade.
COMAOSIT' F'*S
!o$posite &e7 !an %e defined on a !o$%ination of !olu$ns5
6e !an define !o$posite &e7s on entit7 integit7 and efeential integit7 !onstaints5
Co$posite &e7 !an %e defined in ta%le and alte levels onl75
'/0
UNIQU' (TL' L'9'L)
SQL- !eate ta%le student(no nu$%e(1) # na$e va!ha(23)# $a&s nu$%e(4)#
uniue(no#na$e)).
SQL- !eate ta%le student(no nu$%e(1) # na$e va!ha(23)# $a&s nu$%e(4)#
!onstaint un uniue(no#na$e)).
UNIQU' (LT'R L'9'L)
SQL- alte ta%le student add uniue(no#na$e).
SQL- alte ta%le student add !onstaint un uniue(no#na$e).
ARIMR* F'* (TL' L'9'L)
SQL- !eate ta%le student(no nu$%e(1) # na$e va!ha(23)# $a&s nu$%e(4)#
pi$a7 &e7(no#na$e)).
SQL- !eate ta%le student(no nu$%e(1) # na$e va!ha(23)# $a&s nu$%e(4)#
!onstaint p& pi$a7 &e7(no#na$e)).
ARIMR* F'* (LT'R L'9'L)
SQL- alte ta%le student add pi$a7 &e7(no#an$e).
SQL- alte ta%le student add !onstaint p& pi$a7 &e7(no#na$e).
BOR'I8N F'* (TL' L'9'L)
© Copy rights are reserved.
76
-
8/20/2019 SQL Plsql Document
77/319
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)# pi$a7 &e7(e$pno)# foeign &e7(deptno#dna$e) efeen!es
dept(deptno#dna$e)).
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)# !onstaint p& pi$a7 &e7(e$pno)# !onstaint f& foeign&e7(deptno#dna$e) efeen!es dept(deptno#dna$e)).
BOR'I8N F'* (LT'R L'9'L)
SQL- alte ta%le e$p add foeign &e7(deptno#dna$e) efeen!es dept(deptno#dna$e).
SQL- alte ta%le e$p add !onstaint f& foeign &e7(deptno#dna$e) efeen!es
dept(deptno#dna$e).
D'B'RRL' CONSTRINTS
'a!h !onstaint has two additional atti%utes to suppot defeed !he!&ing of !onstaints5
Defeed initiall7 i$$ediate
Defeed initiall7 defeed
Defeed initiall7 i$$ediate !he!&s fo !onstaint violation at the ti$e of inset5
Defeed initiall7 defeed !he!&s fo !onstaint 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)#
!onstaint un uniue(no) defeed initiall7 i$$ediate).
SQL- !eate ta%le student(no nu$%e(1)# na$e va!ha(23)# $a&s nu$%e(4)#
!onstaint un uniue(no) defeed initiall7 defeed).
SQL- alte ta%le student add !onstaint un uniue(no) defea%le initiall7 defeed.
SQL- set !onstaints all i$$ediate.
This will ena%le all the !onstaints violations at the ti$e of inseting5
SQL- set !onstaints all defeed.
This will ena%le all the !onstaints violations at the ti$e of !o$$it5
OA'RTIONS 6IT: CONSTRINTS
© Copy rights are reserved.
77
-
8/20/2019 SQL Plsql Document
78/319
Aossi%le opeations with !onstaints as follows5
'na%le
Disa%le
'nfo!e
Dop
'NL'
This will ena%le the !onstaint5 efoe ena%le# the !onstaint will !he!& the e/isting data5
'/0
SQL- alte ta%le student ena%le !onstaint un.
DISL'
This will disa%le the !onstaint5
'/0
SQL- alte ta%le student ena%le !onstaint un.
'NBORC'
This will enfo!e the !onstaint athe than ena%le fo futue insets o updates5
This will not !he!& fo e/isting data while enfo!ing data5
'/0
SQL- alte ta%le student enfo!e !onstaint un.
DROA
This will e$ove the !onstaint5
'/0
SQL- alte ta%le student dop !onstaint un.
On!e the ta%le is dopped# !onstaints auto$ati!all7 will dop5
© Copy rights are reserved.
78
-
8/20/2019 SQL Plsql Document
79/319
CS' ND D'BULT
CS'
Case is si$ila to de!ode %ut easie to undestand while going though !oding
'/0
SQL- Sele!t sal#
Case sal
6hen 33 then Klow;
6hen 333 then Khigh;
'lse K$ediu$;
'nd !ase
Bo$ e$p.
SL CS'
© Copy rights are reserved.
79
-
8/20/2019 SQL Plsql Document
80/319
""""" """"""""
33 low
133 $ediu$
1333 $ediu$
433 $ediu$ 4333 $ediu$
333 high
@333 $ediu$
333 high
2W33 $ediu$
2133 $ediu$
1333 $ediu$
1V33 $ediu$
1133 $ediu$
4133 $ediu$
D'BULT
-e'ault !an %e !onsideed as a su%stitute %ehavio of not null !onstaint when applied to
new ows %eing enteed into the ta%le5
6hen 7ou define a !olu$n with the de'ault &e7wod followed %7 a value# 7ou ae a!tuall7
telling the data%ase that# on inset 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 duing insetion of new ows5
'/0
SQL- !eate ta%le student(no nu$%e(1) default 22#na$e va!ha(1)).
SQL- inset into student values(2#=a=). SQL- inset into student(na$e) values(=%=).
SQL- sele!t E fo$ student.
NO NM'
© Copy rights are reserved.
80
-
8/20/2019 SQL Plsql Document
81/319
"""""" """""""""
2 a
22 %
SQL- inset into student values(null# K!;).
SQL- sele!t E fo$ student.
NO NM'
"""""" """""""""
2 a
22 %
C
"" Default !an not oveide nulls5
STRCT DT T*A'S
So$e ti$es 7ou $a7 want t7pe whi!h holds all t7pes of data in!luding nu$%es# !has
and spe!ial !haa!tes so$ething li&e this5 *ou !an not a!hieve this using pe"defined
t7pes5
*ou !an define !usto$ t7pes whi!h holds 7ou desied data5
'/0
Suppose in a ta%le we have addess !olu$n whi!h holds hno and !it7 info$ation5
6e will define a !usto$ t7pe whi!h holds %oth nu$ei! 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 TL' S'D ON DT
© Copy rights are reserved.
81
-
8/20/2019 SQL Plsql Document
82/319
SQL- !eate ta%le student(no nu$%e(1)#na$e va!ha(1)#addess add).
INS'RTIN8 DT INTO DT TL'S
SQL- inset into student values(2#=a=#add(222#=h7d=)).
SQL- inset into student values(1#=%=#add(111#=%ang=)).
SQL- inset into student values(4#=!=#add(444#=delhi=)).
S'L'CTIN8 DT BROM DT TL'S
SQL- sele!t E fo$ student.
NO NM' DDR'SS(:NO# CIT*)
""" """"""" """"""""""""""""""""""""" 2 a DDR(222# =h7d=)
1 % DDR(111# =%ang=)
4 ! DDR(444# =delhi=)
SQL- sele!t no#na$e#s5addess5hno#s5addess5!it7 fo$ student s.
NO NM' DDR'SS5:NO DDR'SS5CIT*
"""" """"""" """"""""""""""""" """"""""""""""""
2 a 222 h7d
1 % 111 %ang
4 ! 444 delhi
UADT' 6IT: DT TL'S
SQL- update student s set s5addess5!it7 H =%o$%a7= whee s5addess5hno H 444.
SQL- sele!t no#na$e#s5addess5hno#s5addess5!it7 fo$ student s.
NO NM' DDR'SS5:NO DDR'SS5CIT*
"""" """"""" """"""""""""""""" """"""""""""""""
2 a 222 h7d
1 % 111 %ang
4 ! 444 %o$%a7
© Copy rights are reserved.
82
-
8/20/2019 SQL Plsql Document
83/319
D'L'T' 6IT: DT TL'S
SQL- delete student s whee s5addess5hno H 222.
SQL- sele!t no#na$e#s5addess5hno#s5addess5!it7 fo$ student s.
NO NM' DDR'SS5:NO DDR'SS5CIT*
"""" """"""" """"""""""""""""" """"""""""""""""
1 % 111 %ang
4 ! 444 %o$%a7
DROAAIN8 DT
SQL- dop 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!tue5
*ou define the o%Je!t and !eate a view whi!h elates this o%Je!t to the e/isting ta%le
nothing %ut ob#ect view 5
O%Je!t views ae 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 alead7 %een !eated with the following !olu$ns
SQL- !eate ta%le student(no nu$%e(1)#na$e va!ha(23)#hno nu$%e(4)#!it7
va!ha(23)).
1) Ceate the following t7pes
SQL- !eate t7pe add as o%Je!t(hno nu$%e(1)#!it7 va!ha(23)).?
SQL- !eate t7pe stud as o%Je!t(na$e va!ha(23)#addess add).?
© Copy rights are reserved.
83
-
8/20/2019 SQL Plsql Document
84/319
4) Relate the o%Je!ts to the student ta%le %7 !eating the o%Je!t view
SQL- !eate view studentov(no#studinfo) as sele!t no#stud(na$e#add(hno#!it7))
fo$ student.
@) Now 7ou !an inset data into student ta%le in two wa7s
a) 7 egula insetSQL- Inset into student values(2#;sudha;#222#;h7d;).
%) 7 using o%Je!t view
SQL- Inset into studentov values(2#stud(Ksudha;#add(222#;h7d;))).
M'T:ODS
*ou !an define $ethods whi!h ae nothing %ut fun!tions in t7pes and appl7 in the ta%les
whi!h holds the t7pes.
'/0
2) Defining $ethods in t7pes
SQL- Ceate t7pe stud as o%Je!t(na$e va!ha(23)#$a&s nu$%e(4)#
Me$%e fun!tion $a&sf($a&s in nu$%e) etun nu$%e#
Aag$a esti!tefeen!es($a&sf#wnds#nds#wnps#fnps)).?
1) Defining t7pe %od7
SQL- Ceate t7pe %od7 stud as
Me$%e fun!tion $a&sf($a&s in nu$%e) etun nu$%e is egin
Retun ($a&sG233).
'nd $a&sf.
'nd.?
4) Ceate a ta%le using stud t7pe
SQL- Ceate ta%le student(no nu$%e(1)#info stud).
@) Inset so$e data into student ta%le
SQL- Inset into student values(2#stud(Ksudha;#233)).
) Using $ethod in sele!t
SQL- Sele!t s5info5$a&sf(s5info5$a&s) fo$ student s.
"" :ee we ae using the pag$a esti!tefeen!es to avoid the wites to the
Data%ase5
© Copy rights are reserved.
84
-
8/20/2019 SQL Plsql Document
85/319
9RR*S ND N'ST'D TL'S
9RR*S
va7ing aa7 allows 7ou to stoe epeating atti%utes of a e!od in a single ow %ut
with li$it5
'/0
2) 6e !an !eate vaa7s using oa!le t7pes as well as use defined t7pes5
a) 9aa7 using pe"defined t7pes
SQL- Ceate t7pe va as vaa7() of va!ha(23).?
%) 9aa7s using use defined t7pes SQL- Ceate t7pe add as o%Je!t(hno nu$%e(4)#!it7 va!ha(23)).?
SQL- Ceate t7pe va as vaa7() of add.?
1) Using vaa7 in ta%le
SQL- Ceate ta%le student(no nu$%e(1)#na$e va!ha(23)#addess va).
4) Inseting values into vaa7 ta%le
© Copy rights are reserved.
85
-
8/20/2019 SQL Plsql Document
86/319
SQL- Inset into student values(2#;sudha;#va(add(222#;h7d;))).
SQL- Inset into student values(1#;Jagan;#va(add(222#;h7d;)#add(111#;%ang;))).
@) Sele!ting data fo$ vaa7 ta%le
SQL- Sele!t E fo$ student.
"" This will displa7 vaa7 !olu$n data along with vaa7 and adt. SQL- Sele!t no#na$e# s5E fo$ student s2# ta%le(s25addess) s.
"" This will displa7 in geneal fo$at
) Instead of s5E 7ou !an spe!if7 the !olu$ns in vaa7
SQL- Sele!t no#na$e# s5hno#s5!it7 fo$ student s2#ta%le(s25addess) s.
"" Update and delete not possi%le in vaa7s5
"" :ee we used ta%le fun!tion whi!h will ta&e the vaa7 !olu$n as input fo podu!ing
output e/!luding vaa7 and t7pes5
N'ST'D TL'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 epesented as a !olu$n within anothe ta%le5
Nested ta%le has the sa$e effe!t of vaa7s %ut has no li$it5
'/0
2) 6e !an !eate nested ta%les using oa!le t7pes and use defined t7pes whi!h has no
li$it5
a) Nested ta%les using pe"defined t7pes
SQL- Ceate t7pe nt as ta%le of va!ha(23).?
%) Nested ta%les using use defined t7pes
SQL- Ceate t7pe add as o%Je!t(hno nu$%e(4)#!it7 va!ha(23)).?
SQL- Ceate t7pe nt as ta%le of add.?
1) Using nested ta%le in ta%le
SQL- Ceate ta%le student(no nu$%e(1)#na$e va!ha(23)#addess nt) nested ta%le
addess stoe as studentte$p.
4) Inseting values into ta%le whi!h has nested ta%le
© Copy rights are reserved.
86
-
8/20/2019 SQL Plsql Document
87/319
SQL- Inset into student values (2#;sudha;#nt(add(222#;h7d;))).
SQL- Inset into student values (1#;Jagan;#nt(add(222#;h7d;)#add(111#;%ang;))).
@) Sele!ting data fo$ ta%le whi!h has nested ta%le
SQL- Sele!t E fo$ student.
"" This will displa7 nested ta%le !olu$n data along with nested ta%le and adt. SQL- Sele!t no#na$e# s5E fo$ student s2# ta%le(s25addess) s.
"" This will displa7 in geneal fo$at
) Instead of s5E 7ou !an spe!if7 the !olu$ns in nested ta%le
SQL- Sele!t no#na$e# s5hno#s5!it7 fo$ student s2#ta%le(s25addess) s.
) Inseting nested ta%le data to the e/isting ow
SQL- Inset into ta%le(sele!t addess fo$ student whee noH2)
values(add(#;!hennai;)).
V) Update in nested ta%les
SQL- Update ta%le(sele!t addess fo$ student whee noH1) s set s5!it7H;%o$%a7;
whee s5hno H 111.
W) Delete in nested ta%le
SQL- Delete ta%le(sele!t addess fo$ student whee noH4) s whee s5hnoH444.
DT MOD'L
LLCOLLT*A'S
LLT*A'S
DCOLLT*A'S
DT*A'S
US'RCOLLT*A'S
US'RT*A'S
© Copy rights are reserved.
87
-
8/20/2019 SQL Plsql Document
88/319
BLS:CF QU'R*
Used to etieve the data whi!h has %een alead7 !o$$itted with out going fo e!ove75
Blash%a!&s ae of two t7pes
Ti$e %ase flash%a!&
SCN %ased flash%a!& (SCN stands fo S7ste$ Change Nu$%e)
'/0
2) Using ti$e %ased flash%a!&
a) SQL- Sele!t Efo$ student.
"" This will displa7 all the ows
%) SQL- Delete student.
!) SQL- Co$$it. "" this will !o$$it the wo&5
d) SQL- Sele!t Efo$ student.
"" :ee it will displa7 nothing
© Copy rights are reserved.
88
-
8/20/2019 SQL Plsql Document
89/319
e) Then e/e!ute the following po!edues
SQL- '/e! d%$sflash%a!&5ena%leatti$e(s7sdate"1?2@@3)
f) SQL- Sele!t Efo$ student.
"" :ee it will displa7 the lost data
"" The lost data will !o$e %ut the !uent s7ste$ ti$e was used g) SQL- '/e! d%$sflash%a!&5disa%le
"" :ee we have to disa%le the flash%a!& to ena%le it again
1) Using SCN %ased flash%a!&
a) De!lae a vaia%le to stoe SCN
SQL- 9aia%le s nu$%e
%) 8et the SCN
SQL- '/e! 0s 0H e/e! d%$sflash%a!&5gets7ste$!hangenu$%e
!) To see the SCN
SQL- Aint s
d) Then e/e!ute the following po!edues
SQL- '/e! d%$sflash%a!&5ena%leats7ste$!hangenu$%e(0s)
SQL- '/e! d%$sflash%a!&5disa%le
'+T'RNL TL'S
*ou !an use e/tenal ta%le featue to a!!ess e/tenal files as if the7 ae ta%les inside the
data%ase5
6hen 7ou !eate an e/tenal ta%le# 7ou define its stu!tue and lo!ation with in oa!le5
6hen 7ou ue7 the ta%le# oa!le eads the e/tenal ta%le and etuns the esults Just as
if the data had %een stoed with in the data%ase