28503543 SQL PLSQL Material Ramanjanayalu

download 28503543 SQL PLSQL Material Ramanjanayalu

of 324

Transcript of 28503543 SQL PLSQL Material Ramanjanayalu

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    1/323

    Oracle SQL Guide 1

    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%le56e 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). '/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

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    2/323

    Oracle SQL Guide 2

    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/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    3/323

    Oracle SQL Guide 3

    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/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    4/323

    Oracle SQL Guide 4

    2 Sudha 233

    1 Sa&eth 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&eth 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&eth

    2 >agan

    1 Naen

    4 Ra$esh

    @ Madhu

    9isu

    Rattu

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    5/323

    Oracle SQL Guide 5

    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# ,-

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    6/323

    Oracle SQL Guide 6

    '/0

    SQL- sele!t E fo$ student whee no H 1.

    NO NM' MRFS

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

    1 Sa&eth 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&eth 133

    2 >agan 433

    1 Naen @33

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    7/323

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    8/323

    Oracle SQL Guide 8

    Rattu

    %) USIN8 ND

    This will gives the output when all the !onditions %e!o$e tue5

    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&eth 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&eth 133

    2 >agan 433

    1 Naen @33

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    9/323

    Oracle SQL Guide 9

    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-.

    '/0

    SQL- sele!t E fo$ student whee $a&s %etween 133 and @33.

    NO NM' MRFS

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

    1 Sa&eth 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

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    10/323

    Oracle SQL Guide 10

    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).

    NO NM' MRFS

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

    2 Sudha 233

    1 Sa&eth 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

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    11/323

    Oracle SQL Guide 11

    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.

    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&eth 133

    2 >agan 433

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    12/323

    Oracle SQL Guide 12

    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.

    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&eth 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&eth 133

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    13/323

    Oracle SQL Guide 13

    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&eth 133

    2 >agan 433

    1 Naen @33

    4 Ra$esh

    @ Madhu Rattu

    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&eth 133

    Rattu

    9ii) This will give the ows whose na$e;s thid lette stat with Ke; fo$ ending5

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    14/323

    Oracle SQL Guide 14

    SQL- sele!t E fo$ student whee na$e li&e =e=.

    NO NM' MRFS

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

    1 Sa&eth 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

    E *ou have to spe!if7 the pattens in lieusing 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

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    15/323

    Oracle SQL Guide 15

    1 Sa&eth 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

    1 Sa&eth 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.

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    16/323

    Oracle SQL Guide 16

    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.

    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-.

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    17/323

    Oracle SQL Guide 17

    '/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

    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-.

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    18/323

    Oracle SQL Guide 18

    '/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

    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 ,oldtable_name- to ,new_table_name-.

    '/0

    SQL- ena$e student to stud.

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    19/323

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    20/323

    Oracle SQL Guide 20

    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.

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    21/323

    Oracle SQL Guide 21

    This will oll%a!& last two e!ods5

    SQL- sele!t E fo$ student.

    NO NM' MRFS

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

    2 a 233

    1 % 133

    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&eth5student.

    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

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    22/323

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    23/323

    Oracle SQL Guide 23

    Inset into ,table1- sele!t E fo$ ,table2-.

    '/0

    SQL- inset into student2 sele!t E fo$ student.

    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 %tablena$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.

    USIN8 M'R8'

    M'R8'

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    24/323

    Oracle SQL Guide 24

    *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).

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    25/323

    Oracle SQL Guide 25

    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.

    "" This insets 4 ows

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    26/323

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    27/323

    Oracle SQL Guide 27

    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.

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    28/323

    Oracle SQL Guide 28

    "" This insets 2 e!od %e!ause the else !lause satisfied on!e

    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

    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

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    29/323

    Oracle SQL Guide 29

    NUM'RIC BUNCTIONS

    %s

    Sign

    St

    Mod

    Nvl

    Aowe

    '/p

    Ln

    Log

    Ceil Bloo

    Round

    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

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    30/323

    Oracle SQL Guide 30

    %) 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

    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.

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    31/323

    Oracle SQL Guide 31

    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 4dow $a&s value is null

    NO NM' MRFS

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

    2 a 233

    1 % 133

    4 !

    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 @

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    32/323

    Oracle SQL Guide 32

    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

    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

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    33/323

    Oracle SQL Guide 33

    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

    '/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)

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    34/323

    Oracle SQL Guide 34

    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$

    dual.

    BLOOR() BLOOR(52) BLOOR(") BLOOR("52) BLOOR(3) BLOOR(NULL)

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

    " " 3

    l) ROUND

    This will ounds nu$%es to a given nu$%e of digits of pe!ision5

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    35/323

    Oracle SQL Guide 35

    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

    SQL- sele!t ound(214#"2)# ound(214#"1)# ound(214#"4)# ound("214#"2)# ound(

    "214#"1)# ound("214#"4) fo$ dual.

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    36/323

    Oracle SQL Guide 36

    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

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    37/323

    Oracle SQL Guide 37

    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)

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    38/323

    Oracle SQL Guide 38

    '/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)

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

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    39/323

    Oracle SQL Guide 39

    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)

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    40/323

    Oracle SQL Guide 40

    s!ii

    Ch

    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

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    41/323

    Oracle SQL Guide 41

    This will !onvet the sting into lowe!ase5

    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.

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    42/323

    Oracle SQL Guide 42

    RAD(=COMAUT'R= RAD(=COMAUT'R=

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

    !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.

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    43/323

    Oracle SQL Guide 43

    LTRIM(=C LTRIM(=C

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

    !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.

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    44/323

    Oracle SQL Guide 44

    TRIM(

    """""

    ndian

    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!+)

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    45/323

    Oracle SQL Guide 45

    '/0

    SQL- sele!t epla!e(=india=#=in=#=/7=)# epla!e(Kindia;#;in;) fo$ dual.

    R'ALC' R'ALC'

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

    +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

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    46/323

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    47/323

    Oracle SQL Guide 47

    """""

    a

    p) SUSTR

    This will %e used to e/ta!t su%stings5

    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_countis 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.

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    48/323

    Oracle SQL Guide 48

    INSTR(=INBORMTION=#=O=#@#2) INSTR(=INBORMTION=#=O=#@#1)

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

    @ 23

    If 7ou ae not spe!if7ing !tart_cr_countand occurrencethen it will stat

    sea!h fo$ the %eginning and finds fist o!!uen!e onl75

    If %oth paa$etes !tart_cr_countand occurrenceae 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$

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    49/323

    Oracle SQL Guide 49

    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#@#@#)""""""""""""""""" """""""""""""""""""""""" 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&eth=) 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

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    50/323

    Oracle SQL Guide 50

    S7nta/0 geatest (!trn"1, !trin"2, !trin"3 !trin"n)

    '/0

    SQL- sele!t least(=a=# =%=# =!=)# least(=satish=#=sinu=#=sa&eth=) fo$ dual.

    L'ST L'ST

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

    a sa&eth

    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

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    51/323

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    52/323

    Oracle SQL Guide 52

    '/0

    SQL- sele!t !uentdate fo$ dual.

    CURR'NTDT'

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

    1@"D'C"3

    !) CURR'NTTIM'STMA

    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

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    53/323

    Oracle SQL Guide 53

    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

    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)

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    54/323

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    55/323

    Oracle SQL Guide 55

    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

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

    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.

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    56/323

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    57/323

    Oracle SQL Guide 57

    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

    &) 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.

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    58/323

    Oracle SQL Guide 58

    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*("""""""""""""

    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

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    59/323

    Oracle SQL Guide 59

    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)

    '/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(

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    60/323

    Oracle SQL Guide 60

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

    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 yearthen 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

    If the se!ond paa$ete was montthen 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 daythen ound will !he!&s the wee& da7 of the given date

    in the following anges5

    SUN "" 6'D

    T: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

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    61/323

    Oracle SQL Guide 61

    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.

    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$

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    62/323

    Oracle SQL Guide 62

    ) 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 yearthen it alwa7s etuns the fist da7 of the !uent 7ea5

    If the se!ond paa$ete was montthen it alwa7s etuns the fist da7 of the !uent $onth5

    If the se!ond paa$ete was daythen 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.

    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

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    63/323

    Oracle SQL Guide 63

    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

    CST?CDT "" Cental standad?da7 light ti$e

    'ST?'DT "" 'asten standad?da7 light ti$e8MT "" 8eenwi!h $ean ti$e

    :ST?:DT "" las&a":awaii standad?da7 light ti$e

    MST?MDT "" Mountain standad?da7 light ti$e

    NST "" 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$

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    64/323

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    65/323

    Oracle SQL Guide 65

    '/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

    '/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

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    66/323

    Oracle SQL Guide 66

    '/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)

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

    @

    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.

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    67/323

    Oracle SQL Guide 67

    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)

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

    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

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    68/323

    Oracle SQL Guide 68

    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

    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)

    """"""""""

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    69/323

    Oracle SQL Guide 69

    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

    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.

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    70/323

    Oracle SQL Guide 70

    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@

    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

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    71/323

    Oracle SQL Guide 71

    6e !an add !onstaints in thee wa7s5

    Colu$n level "" along with the !olu$n definition

    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)).

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    72/323

    Oracle SQL Guide 72

    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)).

    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)).

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    73/323

    Oracle SQL Guide 73

    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).

    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).

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    74/323

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    75/323

    Oracle SQL Guide 75

    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

    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) ondelete !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)

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    76/323

    Oracle SQL Guide 76

    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)

    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

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    77/323

    Oracle SQL Guide 77

    '/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

    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

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    78/323

    Oracle SQL Guide 78

    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

    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.

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    79/323

    Oracle SQL Guide 79

    SL CS'

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

    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.

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    80/323

    Oracle SQL Guide 80

    NO NM'

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

    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

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

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    81/323

    Oracle SQL Guide 81

    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

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    82/323

    Oracle SQL Guide 82

    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

    view5

    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).?

    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.

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    83/323

    Oracle SQL Guide 83

    @) Now 7ou !an inset data into student ta%le in two wa7s

    a) 7 egula inset

    SQL- 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

    9RR*S ND N'ST'D TL'S

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    84/323

    Oracle SQL Guide 84

    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

    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

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    85/323

    Oracle SQL Guide 85

    '/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 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

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    86/323

    Oracle SQL Guide 86

    DCOLLT*A'S

    DT*A'S

    US'RCOLLT*A'S

    US'RT*A'S

    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

    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

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    87/323

    Oracle SQL Guide 87

    !) 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%ase5

    CC'SSIN8 '+T'RNL TL' DT

    To a!!ess e/tenal files fo$ within oa!le# 7ou $ust fist use the !eate die!to7 !o$$and to define a

    die!to7 o%Je!t pointing to the e/tenal file lo!ation

    Uses who will a!!ess the e/tenal files $ust have the ead and wite pivilege on the die!to75

    '/0

    CR'TIN8 DIR'CTOR* ND OS L'9'L BIL'

    SQL- Slplus s7ste$?$anage

    SQL- Ceate die!to7 sa&ethdi as K?9isd%?visd%?5153?e/tenal;.

    SQL- 8ant all on die!to7 sa&ethdi to sa&eth.

    SQL- Conn sa&eth?sa&eth

    SQL- Spool dept5lst

    SQL- Sele!t deptno XX K#; XX dna$e XX K#; XX lo! fo$ dept.

    SQL- Spool off

    CR'TIN8 '+T'RNL TL'

    SQL- Ceate ta%le depte/t

    (deptno nu$%e(1)#

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    88/323

    Oracle SQL Guide 88

    Dna$e va!ha(2@)#

    Lo! va!ha(24))

    Oganiation e/tenal ( t7pe oa!leloade

    Default die!to7 sa&ethdi

    !!ess paa$etes

    ( e!ods deli$ited %7 newline

    Bields te$inated %7 P#

    ( deptno nu$%e(1)#

    Dna$e va!ha(2@)#

    Lo! va!ha(24)))

    Lo!ation (K?9isd%?visd%?5153?dept5lst;)).

    S'L'CTIN8 DT BROM '+T'RNL TL'

    SQL- sele!t E fo$ depte/t.

    This will ead fo$ dept5lst whi!h is a opeating s7ste$ level file5

    LIMITTIONS ON '+T'RNL TL'S

    a) *ou !an not pefo$ inset# update# and delete opeations

    a) Inde/ing not possi%leb) Constaints not possi%le

    'N'BITS OB '+T'RNL TL'S

    a) Queies of e/tenal ta%les !o$plete ve7 ui!&l7 even though a full ta%le s!an id euied with

    ea!h a!!ess

    %) *ou !an Join e/tenal ta%les to ea!h othe o to standad ta%les

    R'B D'R'B 9LU'

    R'B

    The ef fun!tion allows efeen!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

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    89/323

    Oracle SQL Guide 89

    D'R'B

    The deef fun!tion pefo$s opposite a!tion5

    It ta&es a efeen!e value of o%Je!t id and etuns the value of the ow o%Je!ts5

    9LU'

    'ven though the pi$a7 ta%le is o%Je!t ta%le# still it displa7s the ows in geneal fo$at5

    To displa7 the entie stu!tue of the o%Je!t# this will %e used5

    '/0

    2) !eate vendotadt t7pe

    SQL- Ceate t7pe vendoadt as o%Je!t (vendo!ode nu$%e(1)# vendona$e

    va!ha(1)# vendoaddess va!ha(23)).?

    1) !eate o%Je!t ta%les vendos and vendos2

    SQL- Ceate ta%le vendos of vendoadt.

    SQL- Ceate ta%le vendos2 of vendoadt.

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

    SQL- inset into vendos values(2# Ka;# Kh7d;).

    SQL- inset into vendos values(1# K%;# K%ang;).

    SQL- inset into vendos2 values(4# K!;# Kdelhi;).

    SQL- inset into vendos2 values(@# Kd;# K!hennai;).

    @) !eate anothe ta%le odes whi!h holds the vendoadt t7pe also5

    SQL- Ceate ta%le odes (odeno nu$%e(1)# vendoinfo ef vendoadt).

    O

    SQL- Ceate ta%le odes (odeno nu$%e(1)# vendoinfo ef vendoadt with

    owid).

    ) inset the data into odes ta%le

    The vendoinfo !olu$n in the following s7nta/es will stoe o%Je!t id of an7 ta%le

    whi!h is efeen!ed %7 vendoadt o%Je!t ( %oth vendos and vendos2)5

    SQL- inset into odes values(22#(sele!t ef(v) fo$ vendos v whee vendo!ode

    H 2)).

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    90/323

    Oracle SQL Guide 90

    SQL- inset into odes values(21#(sele!t ef(v) fo$ vendos v whee vendo!ode

    H 1)).

    SQL- inset into odes values(24#(sele!t ef(v2) fo$ vendos2 v2 whee

    vendo!ode H 2)).

    SQL- inset into odes values(2@#(sele!t ef(v2) fo$ vendos2 v2 whee

    vendo!ode H 2)).

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

    SQL- Sele!t ef(9) fo$ vendos v.

    V) If 7ou see the vendoinfo of odes it will show onl7 the o%Je!t ids not the values#

    to see the values

    SQL- Sele!t deef(o5vendoinfo) fo$ odes o.

    W) 'ven though the vendos 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 fo$ vendos.

    This will give the data without adt5

    SQL-Sele!t value(v) fo$ vendos v.

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

    R'B CONSTRINTS

    Ref !an also a!ts as !onstaint5

    'ven though vendos2 also holding vendoadt# the odes ta%le will stoe the o%Je!t ids of

    vendos onl7 %e!ause it is !onstained to that ta%le onl75

    The vendoinfo !olu$n in the following s7nta/es will stoe o%Je!t ids of vendos onl75

    SQL- Ceate ta%le odes (odeno nu$%e(1)# vendoinfo ef vendoadt s!ope is

    vendos).

    OSQL- Ceate ta%le odes (odeno nu$%e(1)# vendoinfo ef vendoadt !onstaint f&

    efeen!es vendos).

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

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    91/323

    Oracle SQL Guide 91

    To i$ple$ent the o%Je!ts and the ef !onstaints to the e/isting ta%les# what we !an do[ Si$pl7 dop the

    %oth ta%les and e!eate with o%Je!ts and ef !onstains5

    ut 7ou !an a!hieve this with out dopping the ta%les and without losing the data %7 !eating o%Je!t

    views with efeen!es5

    '/0

    a) Ceate the following ta%les

    SQL- Ceate ta%le student2(no nu$%e(1) pi$a7 &e7#na$e va!ha(1)#$a&s

    nu$%e(4)).

    SQL- Ceate ta%le student1(no nu$%e(1) pi$a7 &e7#hno nu$%e(4)#!it7

    va!ha(23)#id nu$%e(1)#foeign Fe7(id) efeen!es student2(no)). %) Inset the e!ods into %oth ta%les

    SQL- inset into student2(2#;a;#233).

    SQL- inset into student2(1#;%;#133).

    SQL- inset into student1(22#222#;h7d;#2).

    SQL- inset into student1(21#111#;%ang;#1).

    SQL- inset into student1(24#444#;%o$%a7;#2).

    !) Ceate the t7pe

    SQL- !eate o epla!e t7pe stud as o%Je!t(no nu$%e(1)#na$e va!ha(1)#$a&snu$%e(4)).?

    d) 8eneating OIDs

    SQL- Ceate o epla!e view student2ov of stud with o%Je!t identifie(o id) (no) as

    Sele!t E fo$ Student2.

    e) 8eneating efeen!es

    SQL- Ceate o epla!e view student1ov as sele!t no#hno#!it7#

    $a&eef(student2ov#id) id fo$ Student1.

    d) Que7 the following

    SQL- sele!t Efo$ student2ov.

    SQL- sele!t ef(s) fo$ student2ov s.

    SQL- sele!t values(s) fo$ student2ov.

    SQ- sele!t Efo$ student1ov.

    SQL- sele!t deef(s5id) fo$ student1ov s.

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    92/323

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    93/323

    Oracle SQL Guide 93

    a) Ceating ange patitioned ta%le

    SQL- Ceate ta%le student(no nu$%e(1)#na$e va!ha(1)) patition %7 ange(no)

    (patition p2 values less than(23)# patition p1 values less than(13)# patition p4

    values less than(43)#patition p@ values less than($a/value)).

    EE if 7ou ae using $a/value fo the last patition# 7ou !an not add a patition5

    %) Inseting e!ods into ange patitioned ta%le

    SQL- Inset into student values(2#;a;). "" this will go to p2

    SQL- Inset into student values(22#;%;). "" this will go to p1

    SQL- Inset into student values(12#;!;). "" this will go to p4

    SQL- Inset into student values(42#;d;). "" this will go to p@

    !) Retieving e!ods fo$ ange patitioned ta%le SQL- Sele!t Efo$ student.

    SQL- Sele!t Efo$ student patition(p2).

    d) Aossi%le opeations with ange patitions

    dd

    Dop

    Tun!ate

    Rena$e

    Split

    Move

    '/!hange

    e) dding a patition

    SQL- lte ta%le student add patition p values less than(@3).

    f) Dopping a patition

    SQL- lte ta%le student dop patition p@.

    g) Rena$ing a patition

    SQL- lte ta%le student ena$e patition p4 to p.

    h) Tun!ate a patition

    SQL- lte ta%le student tun!ate patition p.

    i) Splitting a patition

    SQL- lte ta%le student split patition p1 at(2) into (patition p12#patition p11).

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    94/323

    Oracle SQL Guide 94

    J) '/!hanging a patition

    SQL- lte ta%le student e/!hange patition p2 with ta%le student1.

    &) Moving a patition

    SQL- lte ta%le student $ove patition p12 ta%lespa!e sa&ethts.

    LIST ARTITIONS

    a) Ceating list patitioned ta%le

    SQL- Ceate ta%le student(no nu$%e(1)#na$e va!ha(1)) patition %7 list(no)

    (patition p2 values(2#1#4#@#)# patition p1 values(#V#W##23)#patition p4

    values(22#21#24#2@#2)# patition p@ values(2#2V#2W#2#13)).

    %) Inseting e!ods into list patitioned ta%le

    SQL- Inset into student values(2#;a;). "" this will go to p2 SQL- Inset into student values(#;%;). "" this will go to p1

    SQL- Inset into student values(22#;!;). "" this will go to p4

    SQL- Inset into student values(2#;d;). "" this will go to p@

    !) Retieving e!ods fo$ list patitioned ta%le

    SQL- Sele!t Efo$ student.

    SQL- Sele!t Efo$ student patition(p2).

    d) Aossi%le opeations with list patitions

    dd

    Dop

    Tun!ate

    Rena$e

    Move

    '/!hange

    e) dding a patition

    SQL- lte ta%le student add patition p values(12#11#14#1@#1).

    f) Dopping a patition

    SQL- lte ta%le student dop patition p@.

    g) Rena$ing a patition

    SQL- lte ta%le student ena$e patition p4 to p.

    h) Tun!ate a patition

    SQL- lte ta%le student tun!ate patition p.

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    95/323

    Oracle SQL Guide 95

    i) '/!hanging a patition

    SQL- lte ta%le student e/!hange patition p2 with ta%le student1.

    J) Moving a patition

    SQL- lte ta%le student $ove patition p1 ta%lespa!e sa&ethts.

    :S: ARTITIONS

    a) Ceating hash patitioned ta%le

    SQL- Ceate ta%le student(no nu$%e(1)#na$e va!ha(1)) patition %7 hash(no)

    patitions .

    :ee oa!le auto$ati!all7 gives patition na$es li&e

    S*SA2S*SA1

    S*SA4

    S*SA@

    S*SA

    %) Inseting e!ods into hash patitioned ta%le

    it will inset the e!ods %ased on hash fun!tion !al!ulated %7 ta&ing the patition &e7

    SQL- Inset into student values(2#;a;).

    SQL- Inset into student values(#;%;). SQL- Inset into student values(22#;!;).

    SQL- Inset into student values(2#;d;).

    !) Retieving e!ods fo$ hash patitioned ta%le

    SQL- Sele!t Efo$ student.

    SQL- Sele!t Efo$ student patition(s7sp2).

    d) Aossi%le opeations with hash patitions

    dd

    Tun!ate

    Rena$e

    Move

    '/!hange

    e) dding a patition

    SQL- lte ta%le student add patition p .

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    96/323

    Oracle SQL Guide 96

    f) Rena$ing a patition

    SQL- lte ta%le student ena$e patition p to pV.

    g) Tun!ate a patition

    SQL- lte ta%le student tun!ate patition pV.

    h) '/!hanging a patition

    SQL- lte ta%le student e/!hange patition s7sp2 with ta%le student1.

    i) Moving a patition

    SQL- lte ta%le student $ove patition s7sp1 ta%lespa!e sa&ethts.

    SU"ARTITIONS 6IT: RN8' ND :S:

    Su%patitions !lause is used %7 hash onl75 6e !an not !eate su%patitions with list and hash patitions5

    a) Ceating su%patitioned ta%le

    SQL- Ceate ta%le student(no nu$%e(1)#na$e va!ha(1)#$a&s nu$%e(4))

    Aatition %7 ange(no) su%patition %7 hash(na$e) su%patitions 4

    (Aatition p2 values less than(23)#patition p1 values less than(13)).

    This will !eate two patitions p2 and p1 with thee su%patitions fo ea!h patition

    A2 \ S*SSUA2

    S*SSUA1

    S*SSUA4

    A1 \ S*SSUA@

    S*SSUA

    S*SSUA

    EE if 7ou ae using $a/value fo the last patition# 7ou !an not add a patition5

    %) Inseting e!ods into su%patitioned ta%le

    SQL- Inset into student values(2#;a;). "" this will go to p2

    SQL- Inset into student values(22#;%;). "" this will go to p1

    !) Retieving e!ods fo$ su%patitioned ta%le

    SQL- Sele!t Efo$ student.

    SQL- Sele!t Efo$ student patition(p2).

    SQL- Sele!t Efo$ student su%patition(s7ssu%p2).

    d) Aossi%le opeations with su%patitions

    dd

  • 8/13/2019 28503543 SQL PLSQL Material Ramanjanayalu

    97/323

    Oracle SQL Guide 97

    Dop

    Tun!ate

    Rena$e

    Split

    e) dding a patition

    SQL- lte ta%le student add patition p4 values less than(43).

    f) Dopping a patition

    SQL- lte ta%le student dop patition p4.

    g) Rena$ing a patition

    SQL- lte ta%le student ena$e patition p1 to p4.

    h) Tun!ate a patition

    SQL- lte ta%le student tun!ate patition p2.

    i) Splitting a patition

    SQL- lte ta%le student split patition p4