Top Banner

of 339

138251985 Best SQL Plsql Material

Jun 04, 2018

Download

Documents

Svr Ravi
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • 8/13/2019 138251985 Best SQL Plsql Material

    1/338

    INTRODUCTION

    SQL is divided into the following

    1 Data Definition Language (DDL)

    1 Data Manipulation Language (DML)

    1 Data Retieval Language (DRL)

    1 Tansa!tion Contol Language (TCL)

    1 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

    1 7 value $ethod1 7 addess $ethod

    a) USIN8 9LU' M'T:OD

    S7nta/0

    1

  • 8/13/2019 138251985 Best SQL Plsql Material

    2/338

  • 8/13/2019 138251985 Best SQL Plsql Material

    3/338

    !) INS'RTIN8 DT INTO SA'CIBI'D COLUMNS USIN8 9LU' M'T:OD

    S7nta/0

    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/13/2019 138251985 Best SQL Plsql Material

    4/338

    o

    Sele!t col1, col2, colnfo$ ,table_name-.

    '/0

    SQL-sele!t E 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# $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

  • 8/13/2019 138251985 Best SQL Plsql Material

    5/338

    1 Sa&eth

    2 >agan

    1 Naen

    4 Ra$esh

    @ Madhu 9isu

    Rattu

    CONDITIONL S'L'CTIONS ND

    OA'RTORS

    6e have two !lauses used in this

    1 6hee

    1 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

    1 ith$eti! opeatos

    1 Co$paison opeatos

    1 Logi!al opeatos

    1 ith$eti! opeatos "" highest pe!eden!e

    G# "# E# ?

    2 Co$paison opeatos

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

    2 %etween# not %etween

    3 in# not in

    5

  • 8/13/2019 138251985 Best SQL Plsql Material

    6/338

    2 null# not null

    2 li&e

    2 Logi!al opeatos

    3 nd

    4 O "" lowest pe!eden!e

    3 not

    a) USIN8H# -# ,# -H# ,H# H# ,-

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

  • 8/13/2019 138251985 Best SQL Plsql Material

    7/338

    NO NM' MRFS

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

    2 Sudha 233

    1 Sa&eth 133

    2 >agan 433

    1 Naen @33

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

    NO NM' MRFS

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

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

    7

  • 8/13/2019 138251985 Best SQL Plsql Material

    8/338

    2 >agan 433

    4 Ra$esh

    @ Madhu

    9isu

    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

  • 8/13/2019 138251985 Best SQL Plsql Material

    9/338

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

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

    9

  • 8/13/2019 138251985 Best SQL Plsql Material

    10/338

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

    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

  • 8/13/2019 138251985 Best SQL Plsql Material

    11/338

    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.

    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.

    11

  • 8/13/2019 138251985 Best SQL Plsql Material

    12/338

    NO NM' MRFS

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

    2 Sudha 233

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

    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

  • 8/13/2019 138251985 Best SQL Plsql Material

    13/338

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

    NO NM' MRFS

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

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

    13

  • 8/13/2019 138251985 Best SQL Plsql Material

    14/338

    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

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

  • 8/13/2019 138251985 Best SQL Plsql Material

    15/338

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

    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'

    15

  • 8/13/2019 138251985 Best SQL Plsql Material

    16/338

    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.

    USIN8 DDL

  • 8/13/2019 138251985 Best SQL Plsql Material

    17/338

    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

    S7nta/0

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

    17

  • 8/13/2019 138251985 Best SQL Plsql Material

    18/338

  • 8/13/2019 138251985 Best SQL Plsql Material

    19/338

    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.

    USIN8 TCL

    USIN8 COMMIT

    This will %e used to save the wo&5

    Co$$it is of two t7pes5

    1 I$pli!it

    1 '/pli!it

    a) IMALICIT

    This will %e issued %7 oa!le intenall7 in two situations5

    1 6hen an7 DDL opeation is pefo$ed5

    19

  • 8/13/2019 138251985 Best SQL Plsql Material

    20/338

    1 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

    1 Upto pevious !o$$it

    1 Upto pevious oll%a!&

    S7nta/0

    Roll o oll wo&.

    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.

  • 8/13/2019 138251985 Best SQL Plsql Material

    21/338

    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.

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

    SQL-sele!t E fo$ student.

    NO NM' MRFS

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

    2 a 233

    1 % 133

    21

  • 8/13/2019 138251985 Best SQL Plsql Material

    22/338

    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.

  • 8/13/2019 138251985 Best SQL Plsql Material

    23/338

    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

    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

    23

  • 8/13/2019 138251985 Best SQL Plsql Material

    24/338

    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.

    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.

  • 8/13/2019 138251985 Best SQL Plsql Material

    25/338

    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'

    *ou !an use $ege !o$$and to pefo$ inset and update in a single !o$$and5

    '/0

    25

  • 8/13/2019 138251985 Best SQL Plsql Material

    26/338

  • 8/13/2019 138251985 Best SQL Plsql Material

    27/338

    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

    d) MULTI INS'RT 6IT: DUALICT' RO6S

    SQL-inset all

    27

  • 8/13/2019 138251985 Best SQL Plsql Material

    28/338

    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

    Into student4 values(4#;!;#433)

    'lse

    Into student values(@#;d;#@33)

  • 8/13/2019 138251985 Best SQL Plsql Material

    29/338

    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

    29

  • 8/13/2019 138251985 Best SQL Plsql Material

    30/338

    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

  • 8/13/2019 138251985 Best SQL Plsql Material

    31/338

    1 Single ow fun!tions

    1 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

    1 Nu$ei! fun!tions

    1 Sting fun!tions

    1 Date fun!tions

    1 Mis!ellaneous fun!tions

    1 Convesion fun!tions

    NUM'RIC BUNCTIONS

    1 %s

    1 Sign

    1 St

    1Mod

    1 Nvl

    1 Aowe

    1 '/p

    1 Ln

    1 Log

    1 Ceil

    1 Bloo

    1 Round

    1 Tun&

    1 itand

    1 8eatest

    1 Least

    31

  • 8/13/2019 138251985 Best SQL Plsql Material

    32/338

    1 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

    S7nta/0 st (value) "" hee value $ust %e positive5

  • 8/13/2019 138251985 Best SQL Plsql Material

    33/338

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

    NO NM' MRFS

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

    2 a 233

    1 % 133

    4 !

    33

  • 8/13/2019 138251985 Best SQL Plsql Material

    34/338

    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#")

  • 8/13/2019 138251985 Best SQL Plsql Material

    35/338

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

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

    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

    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

    35

  • 8/13/2019 138251985 Best SQL Plsql Material

    36/338

    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)

    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.

  • 8/13/2019 138251985 Best SQL Plsql Material

    37/338

    C'IL () C'IL (52) C'IL (") C'IL ("52) C'IL (3) '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

    S7nta/0 ound (value, preci!ion)

    '/0

    SQL-sele!t ound (214514@)# ound (214514@# 1)# ound (214514@)# 1)

    fo$ dual.

    37

  • 8/13/2019 138251985 Best SQL Plsql Material

    38/338

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

    Round (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 138251985 Best SQL Plsql Material

    39/338

    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.

    39

  • 8/13/2019 138251985 Best SQL Plsql Material

    40/338

    TRUNC(214514@#"2) TRUNC(214514@#"1) TRUNC(214514@#"4)

    TRUNC(214514@#"@)

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

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

    213 233 3 3

    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$

  • 8/13/2019 138251985 Best SQL Plsql Material

    41/338

    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)

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

    41

  • 8/13/2019 138251985 Best SQL Plsql Material

    42/338

    8R'T'ST(2#1#4) 8R'T'ST("2#"1#"4)

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

    4 "2

    1 If all the values ae eos then it will displa7 eo5

    1 If all the paa$etes ae nulls then it will displa7 nothing5

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

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

    2 "4

    4 If all the values ae eos then it will displa7 eo5

    4 If all the paa$etes ae nulls then it will displa7 nothing5

    4 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#)

  • 8/13/2019 138251985 Best SQL Plsql Material

    43/338

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

    2 1

    STRIN8 BUNCTIONS

    18 Init!ap

    18 Uppe

    18 Lowe

    18 Length

    18 Rpad

    18 Lpad

    18 Lti$

    18 Rti$

    18 Ti$

    18 Tanslate

    18 Repla!e

    18 Sounde/

    18 Con!at ( K XX K Con!atenation opeato)

    18 s!ii

    18 Ch

    18 Su%st

    18 Inst

    18 De!ode

    18 8eatest

    18 Least

    18 Coales!e

    a)INITCA

    This will !apitalie the initial lette of the sting5

    S7nta/0 init!ap (!trin")

    43

  • 8/13/2019 138251985 Best SQL Plsql Material

    44/338

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

    S7nta/0 lowe (!trin")

    '/0

    SQL-sele!t lowe(=COMAUT'R=) fo$ dual.

    LO6'R

    """""""""""

    !o$pute

  • 8/13/2019 138251985 Best SQL Plsql Material

    45/338

    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=

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

    !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

    45

  • 8/13/2019 138251985 Best SQL Plsql Material

    46/338

    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

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

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

  • 8/13/2019 138251985 Best SQL Plsql Material

    47/338

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

    SQL-sele!t ti$( leading=i= fo$ =indiani=) fo$ dual."" this will

    wo& as LTRIM

    TRIM(L

    """"""

    47

  • 8/13/2019 138251985 Best SQL Plsql Material

    48/338

    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'

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

  • 8/13/2019 138251985 Best SQL Plsql Material

    49/338

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

    opeato(XX)5

    SQL-sele!t =how= XX = ae= XX = 7ou= fo$ dual.

    49

  • 8/13/2019 138251985 Best SQL Plsql Material

    50/338

    =:O6=XX=R'

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

    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

  • 8/13/2019 138251985 Best SQL Plsql Material

    51/338

    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

    1 If no_o'_car!paa$ete is negative then it will displa7 nothing5

    1 If %oth paa$etes e/!ept!trin"ae null o eos then it will displa7

    nothing5

    1 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

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

    51

  • 8/13/2019 138251985 Best SQL Plsql Material

    52/338

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

    1 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

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

  • 8/13/2019 138251985 Best SQL Plsql Material

    53/338

    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#@#@#)""""""""""""""""" """""""""""""""""""""""" 4

    1If the nu$%e of paa$etes ae odd and diffeent then de!ode will

    displa7

    nothing5

    2 If the nu$%e of paa$etes ae even and diffeent then de!ode will

    displa7 last

    value5

    3 If all the paa$etes ae null then de!ode will displa7 nothing5

    3 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

    53

  • 8/13/2019 138251985 Best SQL Plsql Material

    54/338

    7 If all the paa$etes ae nulls then it will displa7 nothing5

    7 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&eth=) fo$ dual.

    L'ST L'ST

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

    a sa&eth

    9 If all the paa$etes ae nulls then it will displa7 nothing5

    9 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

  • 8/13/2019 138251985 Best SQL Plsql Material

    55/338

    DT' BUNCTIONS

    39 S7sdate

    39 Cuentdate

    39 Cuentti$esta$p

    39 S7sti$esta$p

    39 Lo!alti$esta$p

    39 D%ti$eone

    39 Sessionti$eone

    39 To!ha

    39 Todate39 dd$onths

    39 Months%etween

    39 Ne/tda7

    39 Lastda7

    39 '/ta!t

    39 8eatest

    39 Least

    39 Round

    39 Tun!

    39 Newti$e

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

    55

  • 8/13/2019 138251985 Best SQL Plsql Material

    56/338

    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

    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

  • 8/13/2019 138251985 Best SQL Plsql Material

    57/338

    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 noti$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

    Ti$e)

    '/0

    SQL-sele!t d%ti$eone fo$ dual.

    DTIM'ZON'""""""""""""""" "3V033

    g) S'SSIONTIM'ZON'

    57

  • 8/13/2019 138251985 Best SQL Plsql Material

    58/338

    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

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

    S**** "" Signed 7ea

    I "" One digit 7ea fo$ ISO standad

  • 8/13/2019 138251985 Best SQL Plsql Material

    59/338

    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 7eaCC "" 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

    Mo AM "" Displa7s M o AM depending upon ti$e of da7

    5Mo A5M "" Displa7s 5M o A5M depending upon ti$e of da7

    Do C "" Displa7s D o C depending upon the date

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

    59

  • 8/13/2019 138251985 Best SQL Plsql Material

    60/338

    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'

    This will %e used to !onvet the sting into data fo$at5

    S7nta/0 todate (date)

  • 8/13/2019 138251985 Best SQL Plsql Material

    61/338

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

    1 If no_o'_mont!is eo then it will displa7 the sa$e date5

    61

  • 8/13/2019 138251985 Best SQL Plsql Material

    62/338

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

    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

  • 8/13/2019 138251985 Best SQL Plsql Material

    63/338

    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

    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

    63

  • 8/13/2019 138251985 Best SQL Plsql Material

    64/338

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

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

    22">N"3

    ) ROUND

  • 8/13/2019 138251985 Best SQL Plsql Material

    65/338

    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

    65

  • 8/13/2019 138251985 Best SQL Plsql Material

    66/338

    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

    1 If the se!ond paa$ete was null then it etuns nothing5

    1 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

    1 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

  • 8/13/2019 138251985 Best SQL Plsql Material

    67/338

  • 8/13/2019 138251985 Best SQL Plsql Material

    68/338

    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_time-one, de!ired_time-one)

    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$e

  • 8/13/2019 138251985 Best SQL Plsql Material

    69/338

    8MT "" 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$

    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.

    69

  • 8/13/2019 138251985 Best SQL Plsql Material

    70/338

    COL'SC'( COL'SC'(

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

    21"Jan"3 21"Jan"3

    MISC'LLN'OUS BUNCTIONS

    59 Uid

    59 Use

    59 9sie

    59 Ran&

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

  • 8/13/2019 138251985 Best SQL Plsql Material

    71/338

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

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

    71

  • 8/13/2019 138251985 Best SQL Plsql Material

    72/338

    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.

    D'NS'RNF(1V)6IT:IN8ROUA(ORD'R*SLD'SC)

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

    4

    CON9'RSION BUNCTIONS

    64 intonu$

    64 Chatoowid

    64 Rowidto!ha

    64 Tonu$%e

    64 To!ha

    64 Todate

    a) INTONUM

    This will !onvet the %ina7 value to its nu$ei!al euivalent5

    S7nta/0 %intonu$( binary_bit!)

    '/0

  • 8/13/2019 138251985 Best SQL Plsql Material

    73/338

    SQL-sele!t %intonu$(2#2#3) fo$ dual.

    INTONUM(2#2#3)

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

    1 If all the %its ae eo then it podu!es eo5

    1 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

    73

  • 8/13/2019 138251985 Best SQL Plsql Material

    74/338

    70 Su$

    70 vg

    70 Ma/

    70 Min

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

    """"""""""

    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

  • 8/13/2019 138251985 Best SQL Plsql Material

    75/338

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

    MIN(SL)

    """"""""""

    33

    e) COUNT

    This will give the !ount of the values of the spe!ified !olu$n5

    S7nta/0 !ount (column)

    '/0

    75

  • 8/13/2019 138251985 Best SQL Plsql Material

    76/338

    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 !onstaints1 Not null

    1 Che!&

    'ntit7 integit7 !onstaints

    1 Uniue

  • 8/13/2019 138251985 Best SQL Plsql Material

    77/338

    1 Ai$a7 &e7

    Refeential integit7 !onstaints

    1 Boeign &e7

    Constaints ae alwa7s atta!hed to a !olu$n not a ta%le5

    6e !an add !onstaints in thee wa7s5

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

    2 Ta%le level "" afte the ta%le definition

    2 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

    77

  • 8/13/2019 138251985 Best SQL Plsql Material

    78/338

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

    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

  • 8/13/2019 138251985 Best SQL Plsql Material

    79/338

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

    ARIMR* F'*

    1 This is used to avoid dupli!ates and nulls5 This will wo& as !o$%ination of

    uniue and not null5

    1 Ai$a7 &e7 alwa7s atta!hed to the paent ta%le5

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

    79

  • 8/13/2019 138251985 Best SQL Plsql Material

    80/338

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

    1 This is used to efeen!e the paent ta%le pi$a7 &e7 !olu$n whi!h allows

    dupli!ates5

    1 Boeign &e7 alwa7s atta!hed to the !hild ta%le5

    1 6e !an add this !onstaint in ta%le and alte levels onl75

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

  • 8/13/2019 138251985 Best SQL Plsql Material

    81/338

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

    !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

    81

  • 8/13/2019 138251985 Best SQL Plsql Material

    82/338

    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)

  • 8/13/2019 138251985 Best SQL Plsql Material

    83/338

    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

    1 Defeed initiall7 i$$ediate

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

    83

  • 8/13/2019 138251985 Best SQL Plsql Material

    84/338

    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

    3 'na%le

    3 Disa%le

    3 'nfo!e

    3 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

  • 8/13/2019 138251985 Best SQL Plsql Material

    85/338

    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

    85

  • 8/13/2019 138251985 Best SQL Plsql Material

    86/338

    SQL-Sele!t sal#

    Case sal

    6hen 33 then Klow;

    6hen 333 then Khigh;

    'lse K$ediu$; 'nd !ase

    Bo$ e$p.

    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

  • 8/13/2019 138251985 Best SQL Plsql Material

    87/338

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

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

    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

    87

  • 8/13/2019 138251985 Best SQL Plsql Material

    88/338

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

    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*

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

  • 8/13/2019 138251985 Best SQL Plsql Material

    89/338

    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

    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.

    89

  • 8/13/2019 138251985 Best SQL Plsql Material

    90/338

    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?

    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.

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

  • 8/13/2019 138251985 Best SQL Plsql Material

    91/338

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

    91

  • 8/13/2019 138251985 Best SQL Plsql Material

    92/338

    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

    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.

  • 8/13/2019 138251985 Best SQL Plsql Material

    93/338

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

    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.

    93

  • 8/13/2019 138251985 Best SQL Plsql Material

    94/338

    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

    1 LLCOLLT*A'S

    1 LLT*A'S

    1 DCOLLT*A'S

    1 DT*A'S

    1 US'RCOLLT*A'S

    1 US'RT*A'S

  • 8/13/2019 138251985 Best SQL Plsql Material

    95/338

    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

    1 Ti$e %ase flash%a!&

    1 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

    95

  • 8/13/2019 138251985 Best SQL Plsql Material

    96/338

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

  • 8/13/2019 138251985 Best SQL Plsql Material

    97/338

    CR'TIN8 '+T'RNL TL'

    SQL-Ceate ta%le depte/t

    (deptno nu$%e(1)#

    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

    b) Inde/ing not possi%le

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

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

    97

  • 8/13/2019 138251985 Best SQL Plsql Material

    98/338

    R'B D'R'B 9LU'

    R'B

    1 The ef fun!tion allows efeen!ing of e/isting ow o%Je!ts5

    1 'a!h of the ow o%Je!ts has an o%Je!t id value assigned to it51 The o%Je!t id assigned !an %e seen %7 using ef fun!tion5

    D'R'B

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

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

    o%Je!ts5

    9LU'

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

    geneal fo$at5

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

  • 8/13/2019 138251985 Best SQL Plsql Material

    99/338

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

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

    99

  • 8/13/2019 138251985 Best SQL Plsql Material

    100/338

    ) 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 with the t7pe5

    R'B CONSTRINTS

    1 Ref !an also a!ts as !onstaint5

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

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

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

    is

    vendos).

    O

    SQL-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 138251985 Best SQL Plsql Material

    101/338

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

    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&s

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

    101

  • 8/13/2019 138251985 Best SQL Plsql Material

    102/338

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

    ARTITIONS

    single logi!al ta%le !an %e split into a nu$%e of ph7si!all7 sepaate pie!es

    %ased on anges of &e7 values5 'a!h of the pats of the ta%le is !alled a patition5

    non"patitioned ta%le !an not %e patitioned late5

    T*A'S

    1 Range patitions

    1 List patitions

    1:ash patitions

    1 Su% patitions

    D9NT8'S

    1 Redu!ing downti$e fo s!heduled $aintenan!e# whi!h allows $aintenan!e

    opeations to %e !aied out on sele!ted patitions while othe patitions ae

    availa%le to uses5

    1 Redu!ing downti$e due to data failue# failue of a pati!ula patition willno wa7 affe!t othe patitions5

    1 Aatition independen!e allows fo !on!uent use of the vaious patitions

    fo vaious puposes5

    D9NT8'S OB ARTITIONS * STORIN8 T:'M IN DIBB'R'NT TL'SAC'S

    1 Redu!es the possi%ilit7 of data !ouption in $ultiple patitions5

    1 a!& up and e!ove7 of ea!h patition !an %e done independentl75

    DISD9NT8'S

    1 ?Aatitioned ta%les !annot !ontain an7 !olu$ns with long o long aw

    datat7pes# LO t7pes o o%Je!t t7pes5

  • 8/13/2019 138251985 Best SQL Plsql Material

    103/338

    RN8' ARTITIONS

    a) Ceatin