SQL PLSQL Interview Question Answer

download SQL PLSQL Interview Question Answer

of 37

Transcript of SQL PLSQL Interview Question Answer

  • 8/10/2019 SQL PLSQL Interview Question Answer

    1/37

    SQL, PL/SQL

    1) What is SQL?1) SQL stands for "Structured Query Language". It is a slandered for relationdatabase syste. It is !th generation non#rocedural language $here $e ha%eto &ust s#ecify $hat to get and its syste's res#onsibility ho$ to get it.

    () o$ any ty#es of SQL Stateents are there in *racle?() +here are basically ty#es of SQL stateents. +hey are1. -ata -enition Stateents --L) --L stateents dene, aintain, anddro# ob&ects $hen they are no longer needed. --L stateents also includestateents that #erit a user to grant other users the #ri%ileges, or rights, toaccess the database and s#ecic ob&ects $ithin the database.(. -ata 0ani#ulation Stateents -0L) -0L stateents ani#ulate thedatabase's data. or e2a#le, 3uerying, inserting, u#dating, and deleting ro$sof a table are all -0L o#erations4 loc5ing a table or %ie$ and e2aining thee2ecution #lan of an SQL stateent are also -0L o#erations.

    6. +ransaction 7ontrol Stateents +ransaction control stateents anage thechanges ade by -0L stateents. +hey allo$ the user or a##licationde%elo#er to grou# changes into logical transactions. 82a#les include7*00I+, 9*LL:;7+.!. Session 7ontrol Stateents Session control stateents allo$ a user tocontrol the #ro#erties of his current session, including enabling and disablingroles and changing language settings. +he t$o session control stateents are;L+89 S8SSI*> and S8+ 9*L8.. Syste 7ontrol Stateents Syste control coands change the#ro#erties of the *racle Ser%er instance. +he only syste control coand is;L+89 S@S+804 it allo$s you to change such settings as the iniu nuber

    of shared ser%ers, to 5ill a session, and to #erfor other tas5s.. 8bedded SQL Stateents 8bedded SQL stateents incor#orate --L,-0L, and transaction control stateents in a #rocedural language #rograsuch as those used $ith the *racle Preco#ilers). 82a#les include *P8>,7L*S8, 8+7, and 8A87B+8.

    6) :uiltin -ata ty#es in *97;L8 Ci6)a) =;97;9( =ariable length character string. +he a2iu siDe is !EEEbytes. Will not ha%e trailing blan5s.b) >=;97;9( =ariable length character string. Fenerally used $ith national

    character sets. +he a2iu siDe is !EEE bytes. Will not ha%e trailing blan5s.c) >B0:89 =ariable length nueric data. 0a2iu length is (1 bytes.d) L*>F =ariable length character data. 0a2iu length is ( gigabyte.Pro%ide for bac5$ard ca#ability.e) -;+; i2ed length date and tie. Bses G bytes.f) +I08S+;0P 7ontain year, onth, day, second, and fractional seconds.g) I>+89=8L@8;9/-;@ Store #eriod of tie in years and onths.i) 9;W =ariable length ra$ binary data. 0a2iu length is (EEE bytes.Pro%ide for bac5$ard ca#ability.

  • 8/10/2019 SQL PLSQL Interview Question Answer

    2/37

    &) L*>F 9;W =ariable length ra$ binary data. 0a2iu length is 6 gigabytes.Pro%ide for bac5$ard ca#ability.h) 9*WI- e2adecial string containing the address of the ro$.i) B9*WI- e2adecial string containing the logical address of the ro$.&) 7;9 i2ed length character string. +he a2iu siDe is (EEE bytes. Willha%e trailing blan5s.5) >7;9 =ariable length character string. Fenerally used $ith national

    character sets. +he a2iu siDe is (EEE bytes. Will ha%e trailing blan5s.l) 7L*: Single byte character data u#to ! gigabyte.) >7L*: Single byte or 2ed length ultibyte >ational 7haracter set u# !gigabyte.n) :L*: Bnstructured binary data u# to ! gigabyte.o) :IL8 :inary data stored in an e2ternal le.

    !) What are the data ty#es su##orted by oracle I>+89>;L)?!) =archar(, >uber, 7har, 0LSL;:8L.

    ) 7har %s =ar7har ?

    ) +he diHeree diHerence bet$een 7;9 and =;9 7;9 is that 7;9 ta5es u#a 2ed aount of s#ace on the dis5 $hile =;97;9 ta5es u# only the s#acere3uired for the actual string or the nuber of bytes s#ecied by r if you usethe =;97;9,r) synta2).or e2a#le-8I>8 charJstr 7;91E)-8I>8 %arJstr1 =;97;91E)-8I>8 %arJstr( =;97;91E,)

    L8+ charJstr K ";"L8+ %arJstr1 K ";"

    L8+ %arJstr( K ";"*n dis5, the follo$ing s#ace $ill be used I use the hy#hen to ean a s#ace)charJstr ;%arJstr1 ;%arJstr( ; bytes total because of its declaration)

    So, %archars a5e uch better use of you dis5 s#ace$ise. +here is, of course,a #rice to #ay.1. When you add %archars to a table, the ro$s are no longer of a 2ed length.If your select does a se3uential scan of the table, it $ill no$ ta5e longerbecause it $ill need to calculate the starting location of each ro$.

    or e2a#le, before the engine can e2aine record M(, it ust gure out thelength of record M1. 8tc.(. 9ecord u#dates becoe ore diNcult.or e2a#le, iagine a record $ith a %archar of 1E bytes. If you u#date therecord so that the %archar is no$ (E bytes, the record $ill ost li5ely ha%e tobe o%ed to another s#ot on the dis5 $here it can gain the 1E e2tra bytes itneeds.

    So, the #ros and cons $or5 out li5e this

  • 8/10/2019 SQL PLSQL Interview Question Answer

    3/37

    Pro -is5 s#ace is conser%ed $hen the lengths of data ites %ary $idely or$hen only a fe$ ites are longer than a%erage. i.e. +he r %alue of the=;97;9,r) synta2)Pro Queries on the ore co#act tables can be faster.7on B#dates of a table can be slo$er in soe circustances.7on Se3uential scans can be slo$er as if they $eren't bad enough alreadyO)

    ;s far as soe of the other coents go.1) I thin5 that char strings are stored $ith their s#ace #adding on the dis5. Itdoesn't atter, though, since they are allocated a 2ed nuber of bytes either$ay.() =;97;9 is not e3ui%alent to +8A+ in any$ayO :lobs are handledco#letely diHerently than =;97;9O6) =archer ay or ay not be eNcient de#ending u#on your circustancesand $hat you ean byeNcient dis5 s#ace %s. #erforance).

    0y #ersonal e2#erience is that, for the ost #art, char is better than =;97;9.

    o$e%er, there denitely are ties $hen a =;97;9 is $hat you $ant.

    ) What is :+ree, 9e%erse

  • 8/10/2019 SQL PLSQL Interview Question Answer

    4/37

    8ach entry or 'bit' in the bita# inde2 corres#onds to a single ro$ of the table.+he %alue of each bit de#ends u#on the %alues of the corres#onding ro$ in thetable. or instance, the bita# ProductJno K 'PEEEE1' contains 1 as its rstbit. +his is because the #roduct no PEEEE1 is in the rst ro$ of the table.;d%antages1. Wor5 $ell $ith the tables containing illion of ro$s.(. :it 0a# inde2 i#ro%e res#onse tie draatically $hene%er ulti#le &oin

    conditions reference the colun ha%ing the :it 0a# inde2 created on it. :it0a# inde2 also $or5s good for &oin conditions ha%ing any ;>-, *9 clause).6. :it 0a# $or5s $ell $hen the colun referenced in the :it 0a# inde2 is notu#dated or deleted fre3uently.!. :it 0a# inde2es $or5 $ell $hen the coluns contained in the :it 0a# inde2has a lo$ cardinality is usually a sall nuber of distinct %alues). Lo$cardinality is usually $hene%er a colun has less then 1( distinct %alues. ;ne2a#le $ould be a colun containing @8S or >*.-is;d%antages1. -oes not $or5 $ell $ith the tables that ha%e the colun in the :it 0a# Inde2u#dated fre3uently.

    (. +he bit 0a# inde2 re3uires rebuilding fre3uently.d) unction based Inde2 1. ; function based inde2 #reco#utes the %alue of the e2#ression or functionand stores it in the inde2.(. +he e2#ression or function can't be aggregate function such as SB0, ;=F,7*B>+, 0;A, 0I>, F9*BPI>F, S+--8=, L;S+, I9S+, 9;>< etc.6. unction :ased inde2es still used the : tree inde2ing ethod.!. Bse function based inde2 $hene%er a function is used in the $here clause or&oin condition of a 3uery. ;n e2a#le $ould be $hene%er searching for a#ersons last nae using BPP89 function.. If a #erson's last nae is stored in i2ed case and the search condition is

    #ro%ide in the u##er case, a full table scan is #erfored if a function basedinde2 is not created using the BPP89 function.. If function is a PL/SQL function or #ac5age and the function or #ac5age isdisabled *racle $ill not use the unction :ased inde2.e) Inde2 *rganiDed +able I*+s)1. Inde2 *rganiDed +able I*+s) stores the inde2ed coluns of the table in thetable itself. ;n inde2 *rganiDed table I*+s) $ill in ost instance contain aninde2 on the #riary 5ey of the table that does not change fre3uently.(. Inde2 organiDed tables store the data in the order of #riary 5ey of thetable.6. Inde2 organiDed tables can #ro%ide %ery fast access to table data $hen the

    3uery e2ecute a e2act atch or range search on the inde2ed #riary 5ey ofthe table.!. +he decreased tie is #riarly because the *racle only has to a5e a singetri# to the table since the inde2 is #art of the table inde2. >o going to inde2and then to the table as in other inde2es.. Storage re3uireent for the inde2 are reduced since the inde2 colun isnot du#licated in the se#arate inde2.. +he 5ey$ord *9F;>I;+I*> I>-8A indicates to *racle that the table $ill beon I*+.

  • 8/10/2019 SQL PLSQL Interview Question Answer

    5/37

    G. Inde2ed organiDed tables can be becoe large since they contain all ro$s ofthe table. +he andatory *=89L*W clause #ro%ides the necessary s#ace todi%ide a ro$ into #arts.. If you use I*+, a5e sure your a##lication $ill not u#date #riary 5eyfre3uently.

    G) *b&ect +y#es ;bstract -ataty#e, >ested +ables , =arying ;rray, Large

    *b&ects, 9eferences, *b&ect =ie$s.G)1. ;bstract -ataty#e ;bstract dataty#e is a dataty#e that consists of one orore subty#es. abstract data ty#es can be nested and can contain referencesto other abstract data ty#es.

    (. >ested +ables ; nested table is a table $ithin a table. ; nested table is acollection of ro$s, re#resented as a colun $ithin the ain table. or eachrecord $ithin ain table, the nested table ay contain ulti#le ro$s. In asence, It's a $ay of storing a onetoany relationshi# $ithin one table.7onsider a table that contained inforation about de#artents, in $hich each

    de#artent ha%e any #ro&ects in #rogress at any ont tie, In a strictlyrelational odel, t$o se#arate tables $ould be created.>ested tables allo$ storing the inforation about #ro&ects $ithin -8P;9+08>+table. +he #ro&ect table records can be accessed directly %ia the -8P;9+08>+table, $ithout the need to #erfor &oin.+he ability to select data $ithout tra%ersing &oins a5es data access easier.8%en if ethods for accessing nested data are not dened. -e#artent andPro&ect data ha%e clearly been associated.

    6. =arying ;rray ; %arying array is a set of ob&ects, each $ith the sae dataty#es. +he siDe of the array is liited $hen it is created.

    When the table is created $ith a %arying array, the array is a nested table $itha liited set of ro$s) .=arying arrays also 5no$n as =;99;@S, allo$s storing re#eated attributes intables.

    or e2a#le, su##ose there is a P9*R87+ table, and #ro&ects ha%e $or5ersassigned to the.

    ; #ro&ect can ha%e any $or5ers, and a $or5er can $or5 on ulti#le #ro&ects.In a strictly relational i#leentation, a P9*R87+ table, a W*9

  • 8/10/2019 SQL PLSQL Interview Question Answer

    6/37

    +he %arying array can then be #o#ulated, so that for each #ro&ect the naes ofall of the #ro&ect's $or5ers can be selected $ithout 3uerying the W*9L*: and :IL8.a. 7L*: Single byte character data u#to ! gigabyte.

    b. >7L*: Single byte or 2ed length ultibyte >ational 7haracter set u# !gigabyte.c. :L*: Bnstructured binary data u# to ! gigabyte.d. :IL8 :inary data stored in an e2ternal le. It e2ist at o#erating systele%el. +he data baseonly aintains a #ointer to the le. +he siDe of the e2ternal le is liited onlyby the *S. Since the data is stored outside the database, *9;7L8 does notaintain concurrency or integrity of the data.0ulti#le L*:s #er table can be used. or e2a#le, there can be table $ith a7L*: colun and t$o :L*: coluns.

    . 9eferences >ested tables and %arying arrays are ebedded ob&ects. +heyare #hysically ebedded $ithin another ob&ect. ;nother ty#e of ob&ect, calledreferenced ob&ects are #hysically se#arate fro the ob&ect that refer to the.9eferences also 5no$ as 98s) are essentially #ointers to ro$ ob&ects. ; ro$ob&ect is diHerent fro a colun ob&ect. ;n e2a#le $ould be a %arying array,it is an ob&ect that is treated as a colun in a table. ; ro$ ob&ect, on the otherhand, al$ays re#resent a ro$.

    . *b&ect =ie$s *b&ect %ie$ allo$ adding ** conce#ts on to# of e2istingrelational tables. or e2a#le, an abstract dataty#e can be created based one2isting table denition. +hus, ob&ect %ie$s gi%e the benets of relational table

    storage and ** structures. *b&ect %ie$s allo$ the de%elo#ent of ** features$ithin a relational database, a 5ind og bridge bet$een the relational and **$orlds.

    ) What is ro$id? $hen it changed ? 7an you delete data using ro$id?) e2adecial string containing the address of the ro$. If you 8>;:L8 9*W0*=808>+ on a #artitioned table, then the ro$id of a ro$ can change if the#artitioning or sub#artitioning 5ey is u#dated such that the ro$ ust beo%ed into a diHerent #artition or sub#artition. o$e%er, this is not thedefault.o$e%er, you cannot assue that the ro$ids of deleted ro$s $ill not be reused

    by subse3uent inserts to the sae table. In fact, once the delete transactionhas been cleaned out, you can alost guarantee that they $ill be. So you $illneed to nd another $ay of doing your deletes #ossibly using the #riary5ey).+he 9*WI- ne%er changes for a ro$ unless you change it using 8AP/I0P utilitie

    C) When $ill you get in%alid ro$id error?C) *racle i's 9*WI- is a base !encoded #hysical address and has signicantdiHerence fro *racleG 9*WI-. If you database has not been igrated to i,you ay get in%alid ro$id error.

  • 8/10/2019 SQL PLSQL Interview Question Answer

    7/37

    1E) o$ to delete du#licate record fro table using ro$id?1E)a) delete fro tableJnae t1 $here t1.colunJnae in selectcolunJnaefro tableJnae t( $here t1.ro$id T t(.ro$id and t1.colunJnae Kt(.colunJnae)

    /undene tableJnaeundene colunJnae+he scri#t $ill #ro#t for a table nae and a colun nae fro $hichdu#licate %alues are to be reo%ed. It $ill then reo%e all ro$s $ith 9*WI-%alues higher than the lo$est 9*WI- for this #articular colun.b) Let us ta5e a table containing 6 coluns, then $e can use the follo$ingcoand to delete the du#licate ro$s fro the table.delete fro table $here ro$id in S8L87+ ro$id 9*0 grou# byro$id,col1,col(,col6inus S8L87+ inro$id) 9*0 grou# by col1,col(,col6)4

    c) delete fro table $here ro$id not in S8L87+ inro$id) 9*0 grou# bycol1,col(,col6 )4d) delete fro e#loyee $here e#id not in Select ine#id) froe#loyee)e) select colunJnae fro tableJnae grou# by colunJnae ha%ingcountU) T 1

    11) o$ to nd Second 0a2) and 0in) %alue of a 7olun..?11)a) or Second 0a2 Select colun; fro table; order by colun; desc liit1,14

    b) or Second 0in Select colun; fro table; order by colun; asc liit 1,14

    a) Select a2salary) fro table $here salary not inselect a2salary) frotable)4b) Select insalary) fro table $here salary not inselect insalary) frotable)4

    nth 0a2) and 0in) %alue of a 7olunSelect distinct col fro table a $here 6 K select countdistinct col) fro tableb $here b.col TK a.col)

    1() What is the diHerence bet$een -8L8+8 and +9B>7;+8 table ?1() -elete;t the si#lest le%el, delete scans the table and reo%es any ro$s that atchthe gi%en criteria in the o#tional) $here clause. It generates rollbac5inforation so that the deletions can be undone should it be necessary. Inde2entries for the deleted ro$s are reo%ed fro the inde2es. @ou ust coit toa5e the deletions #eranent.When deleting ro$s fro a table, e2tents are not deallocated, so if there $ereE e2tents in the table before the deletion, there $ill still be E after thedeletion. In addition the igh Water 0ar5 is not o%ed do$n, so it reains

  • 8/10/2019 SQL PLSQL Interview Question Answer

    8/37

    $here it $as before the deletion began. +his eans that any subse3uent fulltable scans ay still) ta5e a long tie to co#lete because a full table scanal$ays scans u# to the W0. So, by e2a#le, if a select countU) fro%eryJlargeJtable4 too5 1 inutes to co#lete before all the ro$s $eredeleted, you $ill nd that it still ta5es about 1 ins after the deletion because *racle is still scanning e%ery single bloc5 u# to the W0 e%enthough soe or all) of the bloc5s ay ha%e no data in the.

    +runcate+runcate, on the other hand, si#ly o%es the high $ater ar5 on the tableright do$n to the beginning. It does this %ery 3uic5ly, and does not need to becoitted. *nce you truncate a table, there is no going bac5. Inde2es are alsotruncated. +here is no facility to be able to s#ecify $hich ro$s to 'delete' asyou can $ith the $here clause on the delete coand.When a table is truncated, all its e2tents are deallocated lea%ing only thee2tents s#ecied $hen the table $as originally created. So if the table $asoriginally created $ith ine2tents 6, there $ill be 6 e2tents reaining $henthe tables is truncated.If you s#ecify the reuse storage clause, then the e2tents are not deallocated.

    +his sa%es tie in the recursi%e SQL de#artent if you intend to reload thetable $ith data fro an e2#ort for e2a#le, and can reduce the tie it ta5es todo the i#ort as there is no need to dynaically allocate any ne$ e2tents

    16) When ha%ing clause is used?16) +he ha%ing clause is used to restrict the result set returned by the F9*BP:@ clause. When you use F9*BP :@ $ith the ;=I>F clause, the F9*BP :@clause di%ides the ro$s into sets of grou#ed ro$s and aggregates their %alues,and then the ;=I>F clause eliinates undesired aggregated grou#s lterthe ro$s that a grou# by stateent returns).SQLT select U fro students4

    >;08 -;+8J*J: F S+B-8>+JI- 8A;0S FP; Sith E!RBL ( ( .Freen E!RBL 6 ( 6.6+aylor E1R;>GG ! 1 E.:ane E1R;> 0 ! !SQLT select gender, countU) fro students grou# by gender4@ou should see the follo$ing result.F 7*B>+U) 6

    0 1Select gender fro students grou# by gender ha%ing a%gg#a) T 6.C4@ou should see the follo$ing result.SQLT select gender fro students grou# by gender ha%ing a%gg#a) T 6.C4F01!) Is it andatory to ha%e a grou# by clause if , ha%ing e2ists ?1!) @es.

  • 8/10/2019 SQL PLSQL Interview Question Answer

    9/37

    1) What are the set o#erator?1) Set o#erators cobine the results of t$o 3ueries into one result.I>+89S87+ returns all distinct ro$s selected by both 3ueries. 0I>BS returns alldistinct ro$s selected by the rst 3uery but not by the second. B>I*> returnsall distinct ro$s selected by either 3uery. B>I*> ;LL returns all ro$s selectedby either 3uery, including all du#licates.1.If both 3ueries select %alues of dataty#e 7;9, the returned %alues ha%e

    dataty#e 7;9.(.If either or both of the 3ueries select %alues of dataty#e =;97;9(, thereturned %alues ha%e dataty#e =;97;9(.

    1) What are 9o$ *#erators?1) 9o$ o#erators return or reference #articular ro$s. ;LL retains du#licatero$s in the result of a 3uery or in an aggregate e2#ression. -IS+I>7+eliinates du#licate ro$s fro the result of a 3uery or fro an aggregatee2#ression. P9I*9 refers to the #arent ro$ of the current ro$ returned by atreestructured 3uery. @ou ust use this o#erator in the 7*>>87+ :@ clause ofsuch a 3uery to dene the #arentchild relationshi#.

    1G) What are co#arison o#erators?1G) +y#ically, you use co#arison o#erators in the W898 clause of a dataani#ulation stateent to for #redicates, $hich co#are one e2#ression toanother and al$ays yields +9B8, ;LS8, or >BLL. @ou can use all theco#arison o#erators listed belo$ to for #redicates. 0oreo%er, you cancobine #redicates using the logical o#erators ;>-, *9, and >*+.

    ;LL7o#ares a %alue to each %alue in a list or returned by a sub3uery and yields+9B8 if all of the indi%idual co#arisons yield +9B8.

    ;>@, S*087o#ares a %alue to each %alue in a list or returned by a sub3uery and yields+9B8 if any of the indi%idual co#arisons yields +9B8.:8+W88>+ests $hether a %alue lies in a s#ecied range.8AIS+S9eturns +9B8 if a sub3uery returns at least one ro$.I>+ests for set ebershi#.IS >BLL+ests for nulls.

    LI

  • 8/10/2019 SQL PLSQL Interview Question Answer

    10/37

    Pre#aring the SQL stateent for e2ecution When a SQL stateent enters the*racle library cache, the follo$ing ste#s ust occur before the stateent isready to e2ecute1. Synta2 chec5 +he SQL stateent is chec5ed for #ro#er s#elling and $ordorder.(. Seantic #arse *racle %eries all of the tables and colun naes againstthe data dictionary.

    6. Stored *utline chec5 *racle chec5s the data dictionary to see if a storedoutline e2ists for the SQL stateent.!. Fenerate e2ecution #lan *racle uses costbased o#tiiDer algoriths andstatistics in the data dictionary to deterine the o#tial e2ecution #lan.. 7reate binary code*racle generates a binary e2ecutable based on thee2ecution #lan.

    *nce a SQL stateent is #re#ared for e2ecution, subse3uent e2ecutions $illha##en %ery fast, because *racle recogniDes identical SQL stateents and reuses e2ecutables for those stateents.o$e%er, for systes that generate ad hoc SQL or SQL $ith ebedded literal

    %alues, SQL e2ecution #lan generation tie is signicant, and #re%iouse2ecution #lans often can't be used. or those 3ueries that &oin any tables,*racle can s#end a signicant aount of tie deterining the #ro#er order to&oin the tables together.

    8%aluating table &oin order+he ost e2#ensi%e ste# in the SQL #re#aration #rocess is the generation ofthe e2ecution #lan, #articularly $hen dealing $ith a 3uery $ith ulti#le &oins.When *racle e%aluates table &oin orders, it ust consider e%ery #ossiblecobination of tables. or e2a#le, a si2$ay table &oin has G(E #erutationsof , or U U ! U 6 U ( U 1 K G(E) #ossible $ays that the tables can be &oined

    together. +his #erutation issue becoes e%en ore #ronounced $hen youha%e ore than 1E &oined tables in a 3uery or a 1$ay table &oin, there areo%er one trillion 1,6EG,G!,6,EEE to be e2act) #ossible 3uery #erutationsthat ust be e%aluated.

    1C) What are the -iHerent *#tiisation +echni3ues1C) +he =arious *#tiisation techni3ues area) 82ecute Plan $e can see the #lan of the 3uery and change it accordinglybased on the inde2esb) *#tiiDerJhintsetJiteJ#ro#erty'-e#t:loc5',*P+I0I89JI>+,'I9S+J9*WS')4

    Select /UV irstJ9o$s U/ -e#tno,-nae,Loc,9o$id fro de#t$here -e#tno T ()c) *#tiiDeJS3l:y setting the *#tiiDeJS3l K >o, *racle ors assigns a single cursor for allSQL stateents.+his slo$ do$ns the #rocessing because for e%ertie the SQLust be #arsed $hen%er they are e2ecuted.f!run odule K yJrstfor userid K scott/tiger o#tiiDeJs3l K >od) *#tiiDeJ+#:y setting the *#tiiDeJ+#K >o, *racle ors assigns se#erate cursor onlyfor each 3uery S8L87+ stateent. ;ll other SQL stateents reuse the cursor.

  • 8/10/2019 SQL PLSQL Interview Question Answer

    11/37

    f!run odule K yJrstfor userid K scott/tiger o#tiiDeJ+# K >o

    (E) Where does oracle store inforation about 3uery?(E) *racle actually 'stores' t$o rele%ant things in buHer.irstly, it stores the actual code you're e2ecuting along $ith the e2ecution #lan.It stores this in the Library 7ache $hich is a co#onent of the Shared Pool$hich is a co#onent of the Syste Flobal ;rea SF;). When *racle #arses

    the stateent and deterines the e2ecution #lan $hich ta5es tie and in thecase of co#le2 3ueries can ta5e a substantial aount of tie), this is storeda$ay so that $hen the sae code is subse3uently e2ecuted, *racle doesn'tha%e to redo all the $or5 of #arsing and redeterining the e2ecution #lan. It'salready there note all users ha%e access to this area ofeory).

    +herefore, the rst tie you e2ecut the 3uesry it #robably needs to be hard)#arsed and so initially needs to go through this o%erhead. Subse3uent tiesyou e2ecute the 3uery, *racle #robably nds the SQL in the Library cache andis ore eNcient as a result as the hard #arse is not #erfored.

    +o 'get rid' of this, you can issue the ;L+89 S@S+80 LBS S;98-JP**L4Secondly, *racle stores the 'bloc5s' that contain the ro$s of interest in a :uHer7ache that is also a co#onent of the SF;. +he reason it does this is t$o fold.irstly, *racle can only ani#ulate and e2tract inforation out of the bloc5sonce they're loaded into eory. o$e%er they are stored in ashared eory structure because ha%ing gone to all the trouble of retrie%ingthe data fro dis5, there ight be a #ossibility that the bloc5s ight need tobe accessed again. >ote this #ossibility is greater for the session that has readin the bloc5s) but de#ending on the data eg. bloc5 on a freelist)and the data structure eg. #o#ular inde2) it is 3uite li5ely that another session

    ay $ant access to the sae bloc5. +he :uHer 7ache is organiDed in a nuberof lin5ed lists eg. Least 9ecently Bsed L9B list) to encourage #o#ular bloc5s toreain cached in eory.

    +herefore, the rst tie you run your 3uery, the bloc5s need to be read frodis5 and loaded into the :uHer 7ache. o$e%er $hen you subse3uently ree2ecute the 3uery, the bloc5s are #robably still cached in eory, so youa%oid the e2#ensi%e) dis5 reads and the 3uery runs faster as a result.

    +here is no ush :uHer 7ache coand as it ne%er a5es sense to do tis).

    (1) Si#le &oin and outer &oin?(1) *uter Roins+he outer &oin e2tends the result of a si#le &oin. ;n outer &oin returns all ro$sthat satisfy the &oin condition and those ro$s fro one table for $hich no ro$sfro the other satisfy the &oin condition. Such ro$s are not returned by asi#le &oin. +o $rite a 3uery that #erfors an outer &oin of tables ; and : andreturns all ro$s fro ;, a##ly the outer &oin o#erator V) to all coluns of : inthe &oin condition. or all ro$s in ; that ha%e no atching ro$s in :, *raclereturns >BLL for any select list e2#ressions containing coluns of :.

  • 8/10/2019 SQL PLSQL Interview Question Answer

    12/37

    82a#le I+his 3uery uses an outer &oin to e2tend the results of 82a#le AI=S8L87+ enae, &ob, de#t.de#tno, dnae 9*0 e#, de#tW898 e#.de#tno V) K de#t.de#tno4

    8>;08R*:

    -8P+>->;087L;9;F891E;77*B>+I>F FP98SI-8>+1E;77*B>+I>F0ILL89

    7L;9+I>F;-;0S7L;989S;L8S0;>(E98S8;97W;9-0;>;F896ES;L8SR*>8S0;>;F896E

    S;L8S

    !E*P89;+I*>In this outer &oin, *racle returns a ro$ containing the *P89;+I*>S de#artente%en though no e#loyees $or5 in this de#artent. *racle returns >BLL in the8>;08 and R*: coluns for this ro$. +he &oin 3uery in 82a#le A selects onlyde#artents that ha%e e#loyees.

  • 8/10/2019 SQL PLSQL Interview Question Answer

    13/37

    (() What is ++I+L8, :+I+L8, 98P8;-89, P89**+89 ?(() you can set a title to dis#lay at the to# of each #age of a re#ort. @ou canalso set a title to dis#lay at the botto of each #age. +he ++I+L8 coanddenes the to# title4 the :+I+L8 coand denes the botto title.@ou can also set a header and footer for each re#ort. +he 98P8;-89coand denes the re#ort header4 the 98P**+89 coand denes there#ort footer.

    (6) o$ do u i#leent the If stateent in the Select Stateent(6) We can i#leent the if stateent in the select stateent by using the-ecode stateent.e.g select -87*-8 80PJ7;+,'1','irst','(','Second',>ull)4ere the >ull is the else stateent $here null is done .

    If you $ant to co#are, for instance, tbl.Payent $ith (EE,use this codeSelect decodesigntbl.Payent (EE),E,'t$o hundred',1,'Freater than (EE','lessthan (EE')4

    +his is a nice shortcut rather than $riting the #rocedure.+he %alue of signtbl.Payent (EE) is E if tbl.Payent K (EE.+he %alue of signtbl.Payent (EE) is 1 if tbl.Payent T (EE.other$ise, or+he %alue of signtbl.Payent (EE) is 1 if tbl.Payent (EE

    (!) What is S89=89*B+PB+ setting? 7an I #rint inside a PL/SQL #rogra?(!) *ne can use the -:0SJ*B+PB+ #ac5age to $rite inforation to an out#utbuHer. +his buHer can be dis#layed on the screen fro SQLUPlus if you issuethe S8+ S89=89*B+PB+ *>4 coand. or e2a#leset ser%erout#ut on

    begin dbsJout#ut.#utJline'Loo5 0a, I can #rint fro PL/SQLOOO')4end4/-:0SJ*B+PB+ is useful for debugging PL/SQL #rogras. o$e%er, if you #rinttoo uch, the out#ut buHer $ill o%ero$. In that case, set the buHer siDe to alarger %alue, eg. set ser%erout#ut on siDe (EEEEEIf you forget to set ser%erout#ut on ty#e S8+ S89=89*B+PB+ *> once youreeber, and then 8A87 >BLL4. If you ha%en't cleared the -:0SJ*B+PB+buHer $ith the disable or enable #rocedure, SQLUPlus $ill dis#lay the entirecontents of the buHer $hen it e2ecutes this duy PL/SQL bloc5.

    () What builtin functions/o#erators are a%ailable for ani#ulating strings?() +he ost useful ones are L8>F+, SB:S+9, I>S+9, and XX1. L8>F+str) returns the length of str in characters.(. SB:S+9str, , n) returns a #ortion of str, beginning at character , ncharacters long. If n is oitted, all characters to the end of str $ill be returned.6. I>S+9str1,str(,n,) searches str1 beginning $ith its nth character for theth occurrence of str( and returns the #osition of the character in str1 that isthe rst character of this occurrence.!. str1 XX str( returns the concatenation of str1 and str(

  • 8/10/2019 SQL PLSQL Interview Question Answer

    14/37

    () Is it #ossible to $rite a PL/SQL #rocedure that ta5es a table nae as in#utand does soething $ith that table?() or #ure PL/SQL, the ans$er is no, because *racle has to 5no$ the scheaof the table in order to co#ile the PL/SQL #rocedure. o$e%er, *racle#ro%ides a #ac5age called -:0SJSQL, $hich allo$s PL/SQL to e2ecute SQL -0Las $ell as --L dynaically at run tie.or e2a#le, $hen called, the follo$ing stored #rocedure dro#s a s#ecied

    database table798;+8 P9*78-B98 dro#Jtable tableJnae I> =;97;9() ;S cid I>+8F894:8FI> o#en ne$ cursor and return cursor I- cid K -:0SJSQL.*P8>J7B9S*94 #arse and iediately e2ecute dynaic SQL stateent built by concatenating table nae to -9*P +;:L8 coand -:0SJSQL.P;9S8cid, '-9*P +;:L8 ' XX tableJnae, dbsJs3l.%G)4 close cursor -:0SJSQL.7L*S8J7B9S*9cid)4

    8A78P+I*> if an e2ce#tion is raised, close cursor before e2iting W8> *+89S +8> -:0SJSQL.7L*S8J7B9S*9cid)4 reraise the e2ce#tion 9;IS848>- dro#Jtable4.9B>4

    (G) What is the correct synta2 for ordering 3uery results by ro$ty#e ob&ects?

    (G) ;s a concrete e2a#le, su##ose $e ha%e dened an ob&ect ty#ePerson+y#e $ith an *9-89 080:89 B>7+I*>, and $e ha%e created a tablePerson of Person+y#e ob&ects. Su##ose you $ant to list all Person+y#e ob&ectsin Person in order. @ou'd #robably e2#ect the follo$ing to $or5

    S8L87+ U 9*0 Person # *9-89 :@ #4:ut it doesn't. Soeho$, *racle cannot gure out that you are orderingPerson+y#e ob&ects. ere is a hac5 that $or5s

    S8L87+ U 9*0 Person # *9-89 :@ -89898#))4

    () o$ you s#ecify colun se#arators?() +he solution is to concatenate the ro$s together $ith $hate%er se#arator

    you choose to dis#lay. or e2a#le S8L87+ colJa XX 'X' XX colJb 9*0 table4+his $ill sho$ the result li5erstX1EEsecondX(EEthridX6EE

    (C) 7an B disable database trigger? o$?(C) @es. With res#ect to table;L+89 +;:L8 +;:L8 Y-IS;:L8 allJtriggerZ

  • 8/10/2019 SQL PLSQL Interview Question Answer

    15/37

    6E) What are attributes of cursor?6E) [*B>- , [>*+*B>- , [IS*P8>, [9*W7*B>+

    61) What are #seudo coluns? >ae the?61) ; #seudo colun beha%es li5e a table colun, but is not actually stored inthe table. @ou can select fro #seudo coluns, but you cannot insert, u#date,or delete their %alues. +hese are 7B99=;L, >8A+=;L, L8=8L, 9*WI-,

    9*W>B0.

    6() 9o$nu $ith *9-89 :@ clause?6() *ne of the ost often uses of the #seudo colun ro$nu is to #ro%ideserial nubers to the records in a 3uery. +his feature is $idely used in re#ortsto re#resent systeatic dis#lay of inforation.or instanceListing ;Select ro$nu, enae, e#no fro e#1E4+able ;9*W>B0 8>;08 80P>*

    1 F G6C ( :L;8S G 0;9+I> G!o$e%er, $hen $e order this stateent the ro$nu gets disturbed as sho$nbelo$ Listing : and +able :).Listing :select ro$nu, enae, e#no fro e#1E order by enae4+able :

    9*W>B0 8>;08 80P>* ( :L;8S G 1 F G6C 0;9+I> G!;s $e can see fro abo%e the e#loyee naes did get ordered but thero$nu also got the $rong order. +he desired result $as :L;B0 8>;08 80P>* 1 :L;8S G

  • 8/10/2019 SQL PLSQL Interview Question Answer

    16/37

    ! F G6C 0;9+I> G!

    +he tric5 is to do an outer &oin $ith the colun that you $ant to order and this#rocess does not disturb the ro$nu order. In addition to that if the colun isof nuber dataty#e then one should a5e sure to use +*J7;9 dataty#econ%ersion function.

    6() 7an you use select in 9*0 clause of SQL select ?6() @es.

    66) Inline =ie$?66) ;n inline %ie$ is &ust a sub3uery in #lace of a table in an I>S89+, BP-;+8,-8L8+8, or S8L87+. If you could ha%e issued a "create %ie$ as sub3ueryT"and then inserted/u#dated/deleted/selected fro it, you can s5i# the create%ie$ and &ust inline it in the -0L.

    7onsider

    SQLT select U fro select e#no, enae fro e# $here de#tno K 1E )4

    80P>* 8>;08 GG( 7L;9 $hereclause %iolation

    SQLT insert into select e#no, enae, de#tno fro e#( $here de#tno K (E $ith chec5 o#tion ) %alues 1, '2', (E )4

    1 ro$ created.

    6!) o$ any ty#es of 82ce#tions are there?6!) +here are ( ty#es of e2ce#tions. +hey area) Syste 82ce#tions

  • 8/10/2019 SQL PLSQL Interview Question Answer

    17/37

    e.g. When noJdataJfound, When tooJanyJro$sb) Bser -ened 82ce#tionse.g. 0yJe2ce#tion e2ce#tionWhen 0yJe2ce#tion then

    6) o$ any coluns can table ha%e?6) +he nuber of coluns in a table can range fro 1 to (! 6( bit).

    6) What is the a2. length for %ariable nae and %alue to be stored in it6) ; %ariable nae ust begin $ith character and can be follo$ed by aa2iu of (C character.

    6G) What are the uid and user function?6G) BI- returns an integer that uni3uely identies the session user the user$ho logged on).

    S8L87+ BI- 9*0 -B;L4 BI-

    1C

    BS89 returns the nae of the session user the user $ho logged on) $ith thedataty#e =;97;9(. *racle co#ares %alues of this function $ith blan5#added co#arison seantics.In a distributed SQL stateent, the BI- and BS89 functions identify the user onyour local database. @ou cannot use these functions in the condition of a787< constraint.

    S8L87+ BS89, BI- 9*0 -B;L4

    BS89 BI- S7*++ 1C

    6) What does the set esca#e coand does? or o$ does one disableinteracti%e #ro#ting in SQLUPlus?6)8S7Y;P8Z \]XcX*X*>^-enes the character you enter as the esca#e character. * undenes theesca#e character. *> enables the esca#e character. *> changes the %alue of c

    bac5 to the default "]".

    @ou can use the esca#e character before the substitution character setthrough S8+ -8I>8) to indicate that SQLUPlus should treat the substitutioncharacter as an ordinary character rather than as a re3uest for %ariablesubstitutionIf your data contains s#ecial #unctuation characters, you ay encounterdiNculties in SQLPLBS of *9;7L8. +here are the solutions

  • 8/10/2019 SQL PLSQL Interview Question Answer

    18/37

    +o obtain a single 3uote character '), use t$o single 3uotes, i.e., '', to get onesingle 3uote character. or e2a#le, you $ould enter 'Sith''s oe' in SQLfor the string "Sith's oe".+he '' character is used by SQLPLBS as substitution character for data in#ut.+o use an '' character as #art of your string %alue, you need to turn on theesca#e character rst use the follo$ing coandSQLTset esca#e ]

    If you $ant enter a string containing ', you $ould use ']' to esca#e the ''character. or e2a#le, to enter the string, "Rohnson Son 7o.", as 'Rohnson ]Son 7o.'6C) o$ to insert an a#ersand into an table?6C) create table 2 a %archar(1E))4+able created.

    SQLT insert into 2 %alues 'a'XXchr6)XX'b')41 ro$ created.SQLT set esca#e onSQLT insert into 2 %alues '2]y')4

    1 ro$ created.SQLT set esca#e oHSQLT set dene _SQLT insert into 2 %alues'n')41 ro$ created.

    SQLT select U fro 24ab2yn

    6C) With set autocoit on coand, a PL/SQL bloc5 ha%ing ulti#le insert,u#date or delete stateents is e2ecuted. >o$ if the scri#t aborts in bet$een,$ill all the changes be coitted or none at all ?6C) S8+ ;B+*7*00I+ *> treats a PL/SQL bloc5 as a single transaction. Inother $ords, no atter ho$ any I>S89+, BP-;+8 and -8L8+8 stateentsthat are e2ecuted in the PL/SQL bloc5, the coit $ill ha##en only at the endof the co#lete bloc5. +herefore, if the scri#t aborts in the iddle of the bloc5,then nothing $ill be coitted. :ut as long as the bloc5 co#letessuccessfully, all stateents e2ecuted $ithin the bloc5 $ill be coitted.

    !E) o$ to e2ecute o#erating syste coands fro SQL #lus? *r What is

    the diHerence bet$een O and *S+?!E) host ls alrt:oth "O" and "*S+" $ill e2ecute o#erating syste coands as child#rocesses of SQLUPlus. +he diHerence is that "*S+" $ill #erfor %ariablesubstitution and sybols), $hereas "O" $ill not. >ote use "`" under0=S, =0S, and Windo$s en%ironents, not "O")

    !1) What is the diHerence bet$een and ?!1) "" is used to create a te#orary substitution %ariable and $ill #ro#tyou for a %alue e%ery tie it is referenced.

  • 8/10/2019 SQL PLSQL Interview Question Answer

    19/37

    "" is used to create a #eranent substitution %ariable as $ith the -8I>8coand and the *L-J=;LB8 or >8WJ=;LB8 clauses of a 7*LB0> stateent.*nce you ha%e entered a %alue it $ill use that %alue e%ery tie the %ariable isreferenced.8g SQLT S8L87+ U 9*0 +;: W898 +>;08 LI-4If you change the colun or table structure, the constructs dened on their

    dataty#es or structure autoatically change accordingly.o$e%er, $hile one ty#e of de#endency is eliinated using [+@P8 or[9*W+@P8, another is created. If you dene a construct using ob&ect[+@P8 orob&ect[9*W+@P8, the construct de#ends on ob&ect. If ob&ect is altered, theconstructs that de#end on ob&ect are in%alidated.

    !!) What are Procedure, functions and Pac5ages?!!) Procedures and functions consist of set of PL/SQL stateents that aregrou#ed together as a unit to sol%e a s#ecic #roble or #erfor set of relatedtas5s. #rocedure do not return %alues $hile function return one %alue.Pac5ages Pac5ages #ro%ide a ethod of enca#sulating and storing related

    #rocedures, functions, %ariables and other Pac5age 7ontents.

    !) What are -atabase +riggers and Stored Procedures?!) -atabase +riggers -atabase +riggers -+) are #rocedures that areautoatically e2ecuted as a result of insert in, u#date to, or delete fro table.-+ ha%e the %alues old and ne$ to denote the old %alue in the table before it isdeleted and the ne$ indicated the ne$ %alue that $ill be used. -+ are usefulfor i#leenting co#le2 business rules $hich cannot be enforced using theintegrity rules. We can ha%e the trigger as :efore trigger or ;fter +rigger and atStateent or 9o$ le%el.

  • 8/10/2019 SQL PLSQL Interview Question Answer

    20/37

    e.g. o#erations insert, u#date, and delete 6 before, after 6U( a total of cobinations;t stateent le%el once for the trigger) or ro$ le%el for e%ery e2ecution) U( ; total of 1(.+hus a total of 1( cobinations are there and the restriction of usage of 1(triggers has been lifted fro *racle G.6 *n$ards.

    What is a stored #rocedure? Preco#iled SQL stateent residing in thedatabase you can #ass in #araeters and you can recei%e result setss) out.

    Why use stored #rocedures? Preco#ile once, use any ties stored#rocedure is a reusable ob&ectreduce net$or5 traNc Se#arate business rules and logic fro #rresentationlayer +hey're faster

    Stored Procedures *racle Stored Procedures *SP) are Procedures that arestored in 7o#iled for in the database. +he ad%antage of using the stored#rocedures is that any users can use the sae #rocedure in co#iled and

    ready to use forat. It also reduce the net$or5 traNc.

    *SP should contain as uch of the business rules logic for an a##lication as#ossible. In frontend $hich handles only the user interface4 *SP in the bac5end enca#sulate all the business logic of the a##. +he frontend basicallygathers user in#ut and #asses it to *SP as #araeters4 the *SP contain all thelogic data %alidation, etc. +here are no SQL stateents any$here in the a##.

    +here are se%eral ad%antages to this a##roach. irst, $hen the business ruleschange, *SP can be changed as necessary indi%idually. +here is no "library"that needs $holesale re#laceent, as $ith =: odules or .dll's. ;t least $ith

    *racle, #rocedures can be recreated $ithout shutting do$n the ser%er or thefrontend a##. +his architecture #erits a clear distinction in functionalitybet$een the frontend and bac5end. @ou'll nd that centraliDing the businessrule logic on the ser%er bac5end side a5es both de%elo#ent andaintenance uch easier in the long run.

    !) What are the i#licit e2#licit cursor attributes?!) :oth I#licit and 82#licit cursor ha%e ! attributesI#licit 7ursor1. [IS*P8> +he *racle engine autoatically o#ens and closed the SQL cursorafter e2ecuting its associated select, insert, u#date or delete SQL stateent

    has been #rocessed in case of i#licit cursor. +hus the SQL[IS*P8> attributeof an i#licit cursor cannot be referenced outside of its SQL stateent. ;s aresult, SQL[IS*P8> al$ays e%aluates to ;LS8.(. [*B>- 8%aluates to +9B8, if an insert, u#date or delete aHected one orore ro$s, or a singlero$ select returned one or ore ro$s. *ther$ise ite%aluates to ;LS8. +he synta2 for accessing this attribute is SQL[*B>-.6. [>*+*B>- It is logical o##osite of [*B>-. It e%aluates to +9B8, if aninsert , u#date or delete aHected no ro$s, or other$ise it e%aluates to ;LS8.+he synta2 for accessing this attribute is SQL[>*+*B>-.

  • 8/10/2019 SQL PLSQL Interview Question Answer

    21/37

    !. [9*W7*B>+ 9eturns the nubers of ro$s aHected by an insert , u#date ordelete, or select into stateent. +he synta2 for accessing this attribute is SQL[9*W7*B>+.

    82#licit 7ursor1. [IS*P8> 9eturn +9B8 if cursor is o#en, ;LS8 other$ise.(. [*B>- 9eturn +9B8 if record $as fetched successfully, ;LS8 other$ise.

    6. [>*+*B>- 9eturn +9B8 if record $as not fetched successfully, ;LS8other$ise.!. [9*W7*B>+ 9eturns nuber of records #rocessed fro the cursor.

    !G) What is the diHerence bet$een i#licit and e2#licit cursors?!G) ;n e2#licit cursor is declared o#ened and fetched fro in the #rograbloc5 $here as an i#licit cursor is autoatically generated for SQLstateents that #rocess a single ro$ only.

    !) or $hich SQL stateents is the i#licit cursor o#ened?!) SQL stateents that #rocesses a single ro$.

    !C) o$ cursor attributes can be referenced in case of i#licit cursor?!C) :y using SQL[*B>-, SQL[>*+*B>-, SQL[9*W7*PB>+, SQL[IS*P8>.

    E) Which is the for of the cursor, $here these o#erations are adeautoatically?E) In case if I#licit 7ursor.

    1) When is it a##ro#riate to use e2#licit cursors, gi%e an e2a#le?1) When ore than one ro$ of a table is to be #rocessed by an SQL

    stateent.

    () 7an you #ass a #araeter to a cursor? What is the ode of the #araeterthat can be #assed?() @es, It can be of ty#e %alue/ %ariable/ e2#ression.+he sco#e of cursor#araeters is local to that cursor, $hich eans that they can be referencedonly $ithin the 3uery declared in the cursor declaration. 8ach #araeter in thedeclaration ust ha%e a corres#onding %alue in the o#en stateent.

    6) What are the ad%antages of using PL/SQL?6) PL/SQL is a co#letely #ortable, high#erforance transaction #rocessing

    language that oHers the follo$ing ad%antages1. Su##ort for SQL.(. igher #roducti%ity.6. :etter #erforance!. Portability.. integration $ith *racle

    !) +he #rocedural and SQL stateents in a PL/SQL bloc5 are #rocessed bythe SQL stateent e2ecutor and the Procedural stateent e2ecutor. 82#lain

  • 8/10/2019 SQL PLSQL Interview Question Answer

    22/37

    ho$ this $or5s in a client ser%er en%ironent and $here the co#onentsreside.!) ;##lication de%elo#ent tools that lac5 a local PL/SQL engine ust rely on*racle to #rocess PL/SQL bloc5s and sub#rogras.;nonyous PL/SQL bloc5s can be ebedded in an *racle Preco#iler or *7I#rogra. ;t run tie, the #rogra, lac5ing a local PL/SQL engine, sends thesebloc5s to the *racle Ser%er, $here they are co#iled and e2ecuted.

    When it contains the PL/SQL engine, an a##lication de%elo#ent tool can#rocess PL/SQL bloc5s. +he tool #asses the bloc5s to its local PL/SQL engine.+he engine e2ecutes all #rocedural stateents at the a##lication site andsends only SQL stateents to *racle. +hus, ost of the $or5 is done at thea##lication site, not at the ser%er site) What are ! of the #laces or %ehicles) for the use of PL/SQL?) What is the #ur#ose of each?1. ;s anonyous bloc5s $hich are co#iled and run $hen loaded. 9e3uired todo once oH tas5s.(. ;s triggers for integrity #ur#oses. 9e3uired for integrity #ur#oses.6. ;s #rocedures and functions collecti%ely 5no$n as sub#rogras). +o

    #eranently store bloc5s of a##lication code in the database for reuse at torefer to fro other bloc5s of codeodulariDation).!. ;s #ac5ages essentially a naed declarati%e section for storing relatedob&ects including #rocedures, functions and %ariables. See abo%e sub#rogras.+riggers, #rocedures, functions and #ac5ages are stored in the database andcan be called directly fro other PL/SQL bloc5s)8bedded in a host language such as 7. +o cobine PL/SQL $ith a ore#o$erful language such as 7 for s#ecic tas5s.

    ) What are utating tables?) When a table is in state of transition it is said to be utating. e.g. If a ro$

    has been deleted then the table is said to be utating and no o#erations canbe done on the table e2ce#t select.

    ; utating table is a table that is in the #rocess of being odied by anB-P;+8, -8L8+8 or I>S89+ stateent. ; table ay also be considered to beutating if it needs to be u#dated due to a -8L8+8 7;S7;-8 referentialintegrity constraint. +he reason you ay get a utating table error i.e. *9;E!EC1) is that if you re a trigger $hich atte#ts to loo5 at or odify the saetable, the trigger is eHecti%ely #re%ented fro seeing a consistent %ie$ of thedata.

    or e2a#le, let's say that you atte#t to gi%e e%ery e#loyee $ho a5es`1E/hour a 1E[ salary increase as follo$s BP-;+8 e#loyee S8+ salary Ksalary U 1.1E W898 salary K 1E 4+his $ould ean that the changed salary for all these e#loyees $ould be`11. @our BP-;+8 stateent caused an ;+89 9*W trigger to be red thatcontained the follo$ing stateent S8L87+ salary 9*0 e#loyee W898 . . .@ou $ould get the utating table error because the trigger tried to 3uery theoriginal e#loyee table but the original table $as in the #rocess of change.Which %ersion of the table should *racle retrie%e $hen the S8L87+ is issued?+hat's the #robleO *racle si#ly $ill not allo$ this stateent to occur,

  • 8/10/2019 SQL PLSQL Interview Question Answer

    23/37

    because it doesn't ha%e any $ay of 5no$ing ho$ to #ro%ide the correct set ofdata i.e. a consistent %ie$ of the data). *racle $ould generate the *9;E!EC1error and rollbac5 both the trigger body and the triggering stateent i.e. theBP-;+8).:ottoline you cannot touch the table you are u#dating, deleting fro orinserting into in any trigger or userdened function code that it ay e2ecute

    G) What restrictions a##ly to the use of PL/SQL in triggers?G) irst dene utating table and a constraining table.; utating table is one that is one that is currently being odied by a -0Lstateent. or a trigger this is the table on $hich the trigger is dened. ;lso ifa table is being u#dated as a result of a -8L8+8 7;S7;-8 referential integrityconstraint it is also utating.; constraining table is a table that ight need to be read fro for a referentialintegrity constraint.

    or all ro$ le%el triggers and also a stateent triggers that ha%e been red asa result of a -8L8+8 7;S7;-8 o#eration.

    SQL stateents in a trigger body ay notread fro or odify any utating table of the triggering stateent. +hisincludes the triggering table itself. read fro or odify the #riary, uni3ue, or foreign 5ey coluns of aconstraining table of the triggering table. +hey ay, ho$e%er, odify the othercoluns if desired.) What are the Loc5s in *racle?)Shared Loc5s1. Shared loc5s are #laced on resource $hene%er a 98;- o#erationS8L87+) is

    #erfored.(. 0ulti#le shared loc5s can be siultaneously set on a resource.82clusi%e Loc5ing1. 82clusi%e loc5s are #laced on resources $hene%er Write o#erations I>S89+,BP-;+8 and -8L8+8) are #erfored.(. *nly one e2clusi%e loc5 can be #laced on a resource at a tie i.e. the rstuser $ho ac3uire an e2clusi%e loc5 $ill continue to ha%e the sole o$nershi# ofthe resource, and no other user can ac3uire an e2clusi%e loc5 on that resource.C) What is the use of 'or B#date'?C)82#licit Loc5ing +he techni3ue of loc5 ta5en on a table or its resources by a

    user is called 82#licit Loc5ing.Who can 82#licitly Loc5?Bser can loc5 tables they o$n or any tables on $hich they ha%e been grantedtable #ri%ilegessuch as select , insert, u#date, delete).*racle #ro%ide facilities by $hich the default loc5ing strategy can beo%erridden. +ables) or ro$s) can be e2#licitly loc5ed by using either theSelect...for u#date stateent, or a loc5 table stateent.

    Select...or B#date Stateent

  • 8/10/2019 SQL PLSQL Interview Question Answer

    24/37

    It is used for ac3uiring e2clusi%e ro$ le%el loc5s in antici#ation of #erforingu#dates on records. It is often follo$ed by one or ore u#date stateent $itha $here clause.

    82a#le1. +$o client achine 7lient ; and 7lient : are generating salesJorderssiultaneously.

    (. 7lient ; res the follo$ing select stateent.

    7lient ;T S8L87+ U 9*0 SalesJ*rder Where orderJno K 'EEEE1' for BP-;+84When the abo%e select stateent is red, the *racle engine loc5s the recordEEEE1, this loc5 is released $hen a coit or rollbac5 is red by 7lient ;.

    >o$ 7lient : res a Select stateent, $hich #oints to record EEEE1, $hich hasalready been loc5ed by client ;.

    7lient :T S8L87+ U 9*0 SalesJ*rder Where orderJno K 'EEEE1' for BP-;+84+he *racle engine $ill ensure that 7lient :'s SQl stateent $aits for the loc5 to

    be released on SalesJ*rder by a 7*00I+ or 9*LL:;7< stateent red byclient a fore%er.

    In order to a%oid unnecessary $aiting tie, a >*W;I+ o#tion can be used toinfor the *racle engine to terinate the SQL stateent if the record hasalready been loc5ed.If this ha##ens the *racle engine terinates the running -0L and coes u#$ith a essage indicating that the resource is busy.If 7lient : res the follo$ing select stateent no$ $ith a >*W;I+ clause.

    7lient :T S8L87+ U 9*0 SalesJ*rder Where orderJno K 'EEEE1' for BP-;+8

    >*W;I+4

    *racle engine $ill dis#lay the follo$ing essageSQLT EEE! resource busy and ac3uire $ith no$ait s#ecied.

    +he Select...for u#date cannot be used $ith the follo$ing1. -istinct and the grou# by clause.(. Set o#erators and grou# functions.

    E) What is e2ce#tion handler?E) ;n 82ce#tion andler is nothing but a code bloc5 in eory that $ill

    atte#t to resol%e the current e2ce#tion condition. +he *racle engine canrecogniDe e%ery e2ce#tion condition that occurs in eory. +o handle %erycoon and re#etiti%e e2ce#tion conditions the *racle engine uses >aed82ce#tion andlers. +he *racle engine has about fteen to t$enty naede2ce#tion handlers. In addition to this *racle engine uses ore than t$entythousand nubered e2ce#tion handlers. +hese e2ce#tion handlers areidentied not by naes but by four integers #receded by a hy#heni.e. 1!1!) .+hese e2ce#tion handlers naes are actually a set of negati%e signed integers.8ach 82ce#tion andler, irres#ecti%e of ho$ it is identied, i.e. by >ae or

  • 8/10/2019 SQL PLSQL Interview Question Answer

    25/37

    >uber) has code attached that $ill atte#t to# resol%e an e2ce#tioncondition. +his is ho$ *racle's "-efault 82ce#tionandling" strategy $or5s.

    *racle's default e2ce#tion handling code can be o%erridden. When this is done*racle's default e2ce#tion handling code is not e2ecuted but the code bloc5that ta5es care of the e2ce#tion condition, in the e2ce#tion section, of thePL/SQL bloc5 e2ecuted. +his is an e2a#le of a #rograer gi%ing e2#licit

    e2ce#tion handling instructions to an *racle e2ce#tion handler.

    +his eans that *racle engin's 82ce#tion andler ust establish $hether toe2ecute its o$n e2ce#tion handling code or $hether it has to e2ecute userdened e2ce#tion handling code.

    ;s soon as the *racle engine in%o5es an e2ce#tion handler the e2ce#tionhandler goes bac5 to the PL/SQL bloc5 fro $hich the e2ce#tion $as raised.+he e2ce#tion handler scans the PL/SQL bloc5 for the e2istence of an 82ce#tionsection $ithin the PL/SQL bloc5, If an e2ce#tion section $ithin the PL/SQL bloc5e2ists the e2ce#tion handler scan the rst $ord, after the action $ord When,

    $ithin the e2ce#tion section.If the rst $ord after the action $ord "When", is the e2ce#tion handler's naethen the e2ce#tion handler e2ecutes the code contained in the "+hen" sectionof the construct as follo$s

    82ce#tion When \82ce#tion >aeT^ +hen\Bser dened action to be carried outT^

    +he rst $ord that follo$s the action $ord When ust be String. ence thistechni3ue $ill $or5 $ell for the fteen to t$enty naed e2ce#tion handlers. Inaddition to these *racle has t$enty thousand nubered e2ce#tion handlers,

    $hich are raised autoatically can a##ro#riately $hen the oracle enginerecogniDes e2ce#tion condition. Bser dened e2ce#tion handling code ust be#eritted e%en for thesenubered) e2ce#tion handlers.

    1) Pre-ened *racle error handlers?1)1. -BPJ=;LJ*>JI>-8A 9aised $hen an insert or u#date atte#ts to createt$o ro$s $ith du#licate %alues in colus/s constrained by a uni3ue inde2.(. L*FI>J-8>I8- 9aised $hen an in%alid usernae/#ass$ord $as used to logonto *racle.6. >*J-;+;J*B>- 9aised $hen a select stateent returns Dero ro$s.

    !. >*+JL*FF8-J*> 9aised $hen PL/SQL issues an *racle call $ithout beinglogged onto *racle,. P9*F9;0J899*9 9aised $hen *racle has internal #roble.. +I08*B+J*>J98S*B978 9aised $hen *racle has been $aiting to access aresource beyond the userdened tieout liit.G. +**J0;>@J9*WS 9aised $hen a select stateent returns ore than onero$.. =;LB8J899*9 9aised $hen the data ty#e or data siDe is in%alid.C. *+89S Stands for all other e2ce#tions not e2#licitly naed.

  • 8/10/2019 SQL PLSQL Interview Question Answer

    26/37

    () BS89>aed 82ce#tion andlers?() +o handle unnaed internal e2ce#tions, you ust use the *+89S handleror the#raga 8A78P+I*>JI>I+. ; #raga is a co#iler directi%e, $hich can bethought of as a #arenthetical rear5 to the co#iler. Pragas also called#seudoinstructions) are #rocessed at co#ile tie, not at run tie. In PL/SQL,the #raga 8A78P+I*>JI>I+ tells the co#iler to associate an e2ce#tion nae

    $ith an *racle error nuber. +hat allo$s you to refer to anyinternal e2ce#tion by nae and to $rite a s#ecic handler for it. @ou code the#raga 8A78P+I*>JI>I+ in the declarati%e #art of a PL/SQL bloc5, sub#rogra,or #ac5age using the synta2

    P9;F0; 8A78P+I*>JI>I+e2ce#tionJnae, *racleJerrorJnuber)4

    $here e2ce#tionJnae is the nae of a #re%iously declared e2ce#tion. +he#raga ust a##ear soe$here after the e2ce#tion declaration in the saedeclarati%e section, as sho$n in the follo$ing e2a#le

    -87L;98 deadloc5Jdetected 8A78P+I*>4P9;F0; 8A78P+I*>JI>I+deadloc5Jdetected, E)4 Bser-ened 82ce#tions:8FI>...8A78P+I*>W8> deadloc5Jdetected +8> handle the error8>-4

    or

    +he techni3ue that is used to bind a nubered e2ce#tion handler to a naeusing Praga 8ce#tionJinit), +his binding of a nubered e2ce#tion handler, toa nae i.e. a String), is done in the -eclare section of a PL/SQL bloc5.

    ;ll ob&ects declared in the -eclare section of a PL/SQL bloc5 not created untilactually re3uired $ithin the PL/SQL bloc5. o$e%er, the binding of a nuberede2ce#tion handler to a nae ust be done e2actly $hen declared not $henthe e2ce#tion handler is in%o5ed due to an e2ce#tion condition.

    +he Praga action $ord is a call to a #reco#iler, $hich iediately bindsthe nubered e2ce#tion handler to a nae $hen encountered.

    +he function 82ce#tionJinit) ta5es t$o #araeters the rst is the user denede2ce#tion nae the second is the *racle engine's e2ce#tion nuber. +heselines $ill be included in the -eclare section of the PL/SQL :loc5.

    Synta2-87L;98 e2ce#tionJnaeT 8A78P+I*>4

  • 8/10/2019 SQL PLSQL Interview Question Answer

    27/37

    P9;F0; 8A78P+I*>JI>I+e2ce#tionJnaeT,errorJcodeJnoT)4:8FI>

    Bsing this techni3ue it is #ossible to bind a##ro#riated nuber e2ce#tionhandlers to naes and use these naes in the 82ce#tion section of a PL/SQLbloc5. When this is done the default e2ce#tion handling code of the e2ce#tionhandler is o%erride and the userdened 2ece#tion handling code is e2ecuted.

    Synta2-87L;98 e2ce#tionJnaeT 8A78P+I*>4 P9;F0; 8A78P+I*>JI>I+e2ce#tionJnaeT,errorJcodeJnoT)4:8FI> .... ....8A78P+I*> W8> e2ce#tionJnaeT +8> action T

    8>-4

    Bsing raiseJa##licationJerror Pac5age -:0SJS+;>-;9-, $hich is su##lied$ith *racle, #ro%ides language facilities that hel# your a##lication interact $ith*racle.or e2a#le, the #rocedure raiseJa##licationJerror lets you issue userdenederror essages fro stored sub#rogras. +hat $ay, you can re#ort errors toyour a##lication and a%oidreturning unhandled e2ce#tions.+o call raiseJa##licationJerror, use the synta2

    raiseJa##licationJerrorerrorJnuber, essageY, \+9B8 X ;LS8^Z)4

    $here errorJnuber is a negati%e integer in the range (EEEE .. (ECCC andessage is a character string u# to (E! bytes long. If the o#tional third#araeteris +9B8, the error is #laced on the stac5 of #re%ious errors. If the #araeter is;LS8 the default), the error re#laces all #re%ious errors. Pac5age-:0SJS+;>-;9- is an e2tension of #ac5age S+;>-;9-, so you need not3ualify references to its contents.

    6) What is the bloc5 structure of PL/SQL bloc5?6)

    !) :ind %ariable?!) :ind %ariable, also referred to as in#ut %ariables and out#ut %araibles, areused to #roote the reuse of SQL stateents. or e2a#le, gi%en the faouse# table in the scott schea, you ay codeSQLT select U fro e# $here e#no K 1(6!4SQLT select U fro e# $here e#no K G4Bnfortunately, as far as *racle is concerned, the abo%e t$o 3ueries are t$oco#letely diHerent 3ueries and each $ill need to be #arsed, o#tiiDed, and

  • 8/10/2019 SQL PLSQL Interview Question Answer

    28/37

    %alidated for access can you select U fro the e# table?). If on the otherhand you coded

    SQLT %ariable e#no nuberSQLT e2ec e#no K 1(6!4SQLT select U fro e# $here e#no K e#no4SQLT e2ec e#no K G4

    SQLT select U fro e# $here e#no K e#no4

    @ou $ould get the sae results, but the second e2ecution of the selectstateent $ould 'reuse' the already co#iled, #arsed, o#tiiDed 3uery #laneg it $ould run faster the second tie around). Since SQL 3uery #lans areshared not only across sessions eg you could log out and log bac5 in bet$eene2ecutions of the select stateent and still see the s#eed u#) but across users,coonly e2ecuted SQL $ill get #arsed/co#iled/o#tiiDed once #er databaseinstance, not #er user/#er use.:ind %ariables can be used any$here a database colun can be used.;ny$here you can #ut a colun, you can #ut a bind %ariable. @ou cannot use

    bind %ariables in #lace of database ob&ects ho$e%er, so for e2a#leSQLT select U fro soeJtable4

    Will al$ays fail. +his SQL 3uery cannot be #arsed and co#iled and o#tiiDedsince the %alue of soeJtable $ill be changing. In order to acco#lish theabo%e you need to use dynaic s3l see dbsJs3l if #l/s3l is the accesslanguage, #roUc docs for docuentation on dynaic s3l ethods 1!other$ise).*9@ou can use bind %ariables any$here. PL/SQL actually con%erts all local%ariables to bind %ariables in your static SQL autoatically. With SQL, your host

    a##lication has to su##ly actual %alues for bind %ariables if you use the.>*+ using bind %ariables $here a##ro#riate and it's alost e%ery$here) is aa&or #erforance and scalability inhibitor. Without the, all your SQL isuni3ue to *racle, and each stateent is hard#arsed and is 5e#t in the librarycache as ne$ uni3ue SQL, $hich 3uic5ly lls u# the SF; $ith uni3ue nonshareable stateents and introduces library cache latch contention aongother bad things. +here are cases $hen you $ould $ant not to use bind%ariables to allo$ 7:* to see actual %alues so that it can coe u# $itho#tial #lan for your #articular data distribution), but these are actually rare.+o

  • 8/10/2019 SQL PLSQL Interview Question Answer

    29/37

    though it is #articularly useful $ith dynaic SQL. @ou ay $ant to loo5 at it,but I $ould discourage setting it at instance le%el and only set it to 8A;7+ insession $hen it is really needed. Ideally, you should e2#licitly use bind%ariables $here they are a##ro#riate and not rely on soe database setting todo it for you autoatically.*9; :ind ; bind %ariable is li5e a #araeter to a 3uery. or e2a#le the

    3uerySQLT select U fro e# $here enae K 'F'4 a5e no use of bind%ariables. If $e $ant to change the search criteria fro 'F' to ':L;

  • 8/10/2019 SQL PLSQL Interview Question Answer

    30/37

    6. ; Pac5age's #ublic %ariables and cursor #ersist for the duration of thesession. +herefore all cursor and #rocedures that e2ecute in this en%ironentcan share the.!. Pac5age enables the o%erloading of #rocedure and function $hen re3uired.. Pac5age i#ro%es #erforance by loading ulti#le ob&ects into eory atonce. +herefore, subse3uent calls to related sub#rogras in the #ac5agere3uire no I/*.

    . Pac5age #roote code reuse through the use of libraries that contain stored#rocedures and functions, reducing redundant coding.

    ) PL/SQL table?) PL/SQL tables are a lot ore useful than you ight thin5. +rue, a PL/SQLtable is LIL8SS you use hashing or a binary search.

    With a PL]SQL table, the inde2 is a binary inde2. +he inde2es do >*+ ha%e tobe in se3uential order. I can declare a PL/SQL table as such

    +@P8 errorJtableJty# IS +;:L8 of >B0:89G) I>-8A :@ :I>;9@JI>+8F894

    yJtable errorJtableJty#4

    I could then store nubers as follo$s

    yJtable1) K 1EE4yJtable1EE) K (E4yJtable1E) K 14

    ;ll that is in eory are these three "records". +hat's it. >o$, if I $anted tostore the this $ay in an array in 7 or Pascal, I $ould ha%e to create an arrayof at least 1E eleents. *f course, soe of you ay be thin5ing, "W@ $ouldI $ant to store eleents out of order?" Well, a PL/SQL table $or5s great if youha%e data you ha%e to constantly %erify against. Selecting against a PL/SQL

    table is a L*+ faster than ha%ing to constantly go against a table in thedatabase. Loading the table into a PL/SQL table and then using the PL/SQLtable to %erify your data is uch faster. +IS is $here a PL/SQL table coes inhandy.

    Let's say you ha%e a table that has an I- to indentify a business. @ou $ant thebusiness nae. +he I-'s are >*+ neccessarily in a se3uential order. :ut, they;98 nubers. ere's the table

    7*0P;>@J+;:L8

  • 8/10/2019 SQL PLSQL Interview Question Answer

    31/37

    co#anyJidJnbr >B0:89)4 co#anyJnaeJt2t =;97;9((EE)4

    We create a PL/SQL table as follo$s

    -87L;98

    +@P8 co#anyJnaeJtableJty# IS +;:L8 of =;97;9((EE) I>-8A :@ :I>;9@JI>+8F894

    yJtable co#anyJnaeJtableJty#4

    7B9S*9 yJcursor IS S8L87+ co#anyJidJnbr, co#anyJnaeJt2t 9*0 co#anyJtable4

    yJcursorJrec yJcursor[9*W+@P84

    :8FI> *P8> yJcursor4 8+7 yJcursor I>+* yJcursorJrec4

    WIL8 yJcursor[*B>- L**P yJtableyJcursorJrec.co#anyJidJnbr) K yJcursorJrec.co#anyJnaeJt2t4

    8+7 yJcursor

    I>+* yJcursorJrec4 8>- L**P4 7L*S8 yJcursor4 8>-4

    Put this code into a #rocedure before you're ain code runs a5e the PL/SQLtables global, of course). +hen, if $e e%er need to %erify the co#any naeand all $e ha%e is the id, $e can then do the follo$ing

    -87L;98 naeJt2t =;97;9((EE)4

    idJnbr >B0:89)4 :8FI> naeJt2t K yJtableidJnbr)4

    ...

    8A78P+I*> W8> noJdataJfound +8> /U +his $ill ha##en if the record does >*+ e2ists U/

  • 8/10/2019 SQL PLSQL Interview Question Answer

    32/37

    /U in the PL/SQL table. U/

    8>-4

    +his is 0B7 faster than going against the table in the database directly. +ISis the beauty of PL/SQL tables. ;t rst, I too felt they $ere useless. >o$,I'%e gone bac5 to any #rogras I'%e $ritten and re$rote the to use PL/SQL

    tables $ith this 5ind of #rograing. I ha%e noticed a ([V increase in thes#eed in the %erication routines of y #rogras, $hich also decreases theaount of tie y #rogras run es#ecially y o%ernight #rocesses that arehuge).

    With PL/SQL (.6, you $ill be able to ha%e PL/SQL tables $ith ore than onecolun $hich eans you can create a PL/SQL table that is a direct co#y of arecord of a table in the database. Bntil then, if you use PL/SQL %ersion before(.6, then you $on't be able to ha%e ore than one colun #er PL/SQL table.:ut,you &ust ha%e to create a PL/SQL table for each colun that has a diHerent

    ty#e and inde2 each table by the sae inde2. It's that si#le.

    ) o$ the nulls are handled in PL/SQL?) ; >BLL does not e3ual >BLL. >ull is al$ays undened. Since it has nodenition it cannot be e3ual to another undened %alue. +his is ho$ >BLL aretreated in *9;7L8. if both coluns that $ere being co#ared $ere null thenthey $ere not e3ual. *ne $ay to handle this is to $ra# n%lyourJcolunJher,E)in your cursor select or in the co#arison this $ill ensure that >BLL $ill behandled identically.I n%la,E) K n%lb,E) then do soething4

    8>- I4

    Watch outO +he abo%e e2a#le $ill fail $hen aKE and b is null, and When a isnull and bKE.+he only $ay you can really be sure is $ith soething li5e this

    I a K b or a is null and b is null)

    *racle's $ay of al$ays returning ;LS8 to any co#arison $here a colun%alue is null indicates thatthey don't $or5 uch in the UrealU.

    *5, here is the #artial #ac5age, the rest you can ll in $ith yourrules for co#arision is a null null for e2a#le?. Why is a null anything else? )

    create or re#lace #ac5age nullasfunction "K" a in %archar(, b in %archar( ) return nuber4#raga restrictJreferences "K", W>-S, 9>-S, W>PS, 9>PS )4

  • 8/10/2019 SQL PLSQL Interview Question Answer

    33/37

    function "" a in %archar(, b in %archar( ) return nuber4#raga restrictJreferences "", W>-S, 9>-S, W>PS, 9>PS )4

    end nulls4/create or re#lace #ac5age body nulls

    asfunction "K" a in %archar(, b in %archar( ) return nuberisbegin if a K b *9 a is >BLL and b is >BLL ) )then

    return 14 else return E4 end if4end "K"4

    function "" a in %archar(, b in %archar( ) return nuberisbegin if a is >BLL and b is >*+ >BLL )then

    return 14 elsif a b ) then return 14 else

    return E4 end if4end ""4

    end nulls4/select nulls."K" null, null )fro dual/>BLLS."K">BLL,>BLL)

    1select nulls."" null, 1 )fro dual/>BLLS."">BLL,1) 1

  • 8/10/2019 SQL PLSQL Interview Question Answer

    34/37

    select U fro dual$here nulls."K" null, null ) K 1/-A

    select U fro dual$here nulls."" null, duy ) K 1/-AG) 82ce#tion?G) 82ce#tions are identiers in PL/SQL that are raised during the e2ecution ofa bloc5 to terinate its action. ; bloc5 is al$ays terinated $hen PL/SQLraises an e2ce#tion but you can dene your o$n error handler to ca#ture

    e2ce#tions and #erfor soe nal actions before 3uitting the bloc5. If PL/SQLhandles the e2ce#tion $ithin the bloc5 then the e2ce#tion $ill not #ro#agateout to an enclosing bloc5 or en%ironent.+here are t$o classes of e2ce#tions, these are Predened *racle #redened errors $hich are associated $ith s#ecic errorcodes.Bserdened -eclared by the user and raised $hen s#ecically re3uested$ithin a bloc5. @ou ay associate a userdened e2ce#tion $ith an error code ifyou $ish.If an error occurs $ithin a bloc5 PL/SQL #asses control to the 8A78P+I*>section of the bloc5. If no 8A78P+I*> section e2ists $ithin the bloc5 or the

    8A78P+I*> section doesn't handle the error that's occurred then the bloc5 isterinated $ith an unhandled e2ce#tion. 82ce#tions #ro#agate u# throughnested bloc5s until an e2ce#tion handler is found that can handle the error. Ifno e2ce#tion handler is found in any bloc5 the error is #assed out to the hosten%ironent. 82ce#tions occur $hen either an *racle error occurs thisautoatically raises an e2ce#tion) or you e2#licitly raise an error using the9;IS8 stateent.+he t$o ost coon errors originating fro a S8L87+ stateent occur $henit returns no ro$s or ore than one ro$ reeber that this is not allo$ed).+he e2a#le belo$ deals $ith these t$o conditions.-87L;98

    +80PJ7*S+ >B0:891E,()4+80PJIS:> >B0:891E)4:8FI> S8L87+ IS:>, 7*S+ 9*0 R-11.:**< I>+* +80PJIS:>, +80PJ7*S+ W898IS:> T (14 I +80PJ7*S+ T E +8> BP-;+8 R-11.:**< S8+ 7*S+ K +80PJ7*S+U1.1G) W898 IS:> T (14 8LS8

    BP-;+8 R-11.:**< S8+ 7*S+ K (1.6( W898 IS:> T (14 8>- I4

  • 8/10/2019 SQL PLSQL Interview Question Answer

    35/37

    7*00I+48A78P+I*> W8> >*J-;+;J*B>- +8> I>S89+ I>+* R-11.899*9S 7*-8, 08SS;F8) =;LB8SCC, '>*+ *B>-')4 W8> +**J0;>@J9*WS +8> I>S89+ I>+* R-11.899*9S 7*-8, 08SS;F8) =;LB8SCC, '+** 0;>@')48>-4

    +he bloc5 abo%e $ill generate an error because there are ore than one record$ith an IS:> greater than (1. +he e2ce#tion raised fro this $ill be #assed tothe 8A78P+I*> section $here each handled action $ill be chec5ed. +hestateents $ithin the +**J0;>@J9*WS action $ill then be e2ecuted beforethe bloc5 is terinated.If soe other error occurred this 8A78P+I*> section $ould not handle itbecause is isn't dened as a chec5able action. +o co%er all #ossible errors youcan s#ecify a catch all action naed *+89S.-87L;98+80PJ7*S+ >B0:891E,()4+80PJIS:> >B0:891E)4

    :8FI> S8L87+ IS:>, 7*S+ 9*0 R-11.:**< I>+* +80PJIS:>, +80PJ7*S+ W898IS:> T (14 I +80PJ7*S+ T E +8> BP-;+8 R-11.:**< S8+ 7*S+ K +80PJ7*S+U1.1G) W898 IS:> T (14 8LS8

    BP-;+8 R-11.:**< S8+ 7*S+ K (1.6( W898 IS:> T (14 8>- I4

    7*00I+48A78P+I*> W8> >*J-;+;J*B>- +8>

    I>S89+ I>+* R-11.899*9S 7*-8, 08SS;F8) =;LB8SCC, '>*+ *B>-')4 W8> +**J0;>@J9*WS +8> I>S89+ I>+* R-11.899*9S 7*-8, 08SS;F8) =;LB8SCC, '+** 0;>@')4 W8> *+89S +8> I>S89+ I>+* R-11.899*9S 7*-8, 08SS;F8) =;LB8SCC, 'S*08 899*9*77B998-')48>-4+his bloc5 $ill tra# all errors. If the e2ce#tion isn't no ro$s returned or tooany ro$s returned then the *+89S action $ill #erfor the error handling.PL/SQL #ro%ides t$o s#ecial functions for use $ithin an 8A78P+I*> section,they are SQL7*-8 and SQL8990. SQL7*-8 is the *racle error code of the

    e2ce#tion, SQL8990 is the *racle error essage of the e2ce#tion. @ou can usethese functions to detect $hat error has occurred %ery useful in an *+89Saction). SQL7*-8 and SQL8990 should be assigned to %ariables before youatte#t to use the.-87L;98+80PJ7*S+ >B0:891E,()4+80PJIS:> >B0:891E)4899J0SF =;97;9(1EE)4899J7-8 >B0:894:8FI>

  • 8/10/2019 SQL PLSQL Interview Question Answer

    36/37

    S8L87+ IS:>, 7*S+ 9*0 R-11.:**< I>+* +80PJIS:>, +80PJ7*S+ W898IS:> T (14 I +80PJ7*S+ T E +8> BP-;+8 R-11.:**< S8+ 7*S+ K +80PJ7*S+U1.1G) W898 IS:> T (14 8LS8

    BP-;+8 R-11.:**< S8+ 7*S+ K (1.6( W898 IS:> T (14 8>- I4

    7*00I+48A78P+I*> W8> >*J-;+;J*B>- +8> I>S89+ I>+* R-11.899*9S 7*-8, 08SS;F8) =;LB8SCC, '>*+ *B>-')4 W8> +**J0;>@J9*WS +8> I>S89+ I>+* R-11.899*9S 7*-8, 08SS;F8) =;LB8SCC, '+** 0;>@')4 W8> *+89S +8> 899J0SF K SB:S+9SQL8990,1,1EE)4 899J7-8 K SQL7*-84 I>S89+ I>+* R-11.899*9S 7*-8, 08SS;F8) =;LB8S899J7-8,899J0SF)4

    8>-4If you e2#licitly need to raise an error you can do this by using the 9;IS8stateent. or e2a#le, if you $anted to raise an error if a S8L87+ stateentfound a ro$ $hich it shouldn't ha%e found) you $ould only be able to do thisby raising your o$n error.-87L;98+80PJ7*S+ >B0:891E,()4+80PJIS:> >B0:891E)4899J0SF =;97;9(1EE)4899J7-8 >B0:894:8FI>

    S8L87+ IS:>, 7*S+ 9*0 R-11.:**< I>+* +80PJIS:>, +80PJ7*S+ W898IS:> E4 9;IS8 +ISJISJW9*>F8A78P+I*> W8> +ISJISJW9*>F +8> I>S89+ I>+* R-11.899*9S 7*-8, 08SS;F8) =;LB8SCC, '**PS')48>-4+he e2a#le abo%e shouldn't nd any ro$s $ith an IS:> less than E. If it did$e raise our o$n e2ce#tion to force PL/SQL to #ass control to our o$n action$ithin the 8A78P+I*> section.

    ) o$ any triggers?) +here are total 1! database triggers

    insert beforedelete beforeu#date beforeinsert afterdelete afteru#date after

  • 8/10/2019 SQL PLSQL Interview Question Answer

    37/37

    abo%e all for each ro$abo%e all for each stateent

    these are 1(Istead of +rigger I>S+8;-* trigger #ro%ide a trans#arent $ay of odifying%ie$s thatcannot be odied directly through -0L stateent I>S89+, BP-;+8,

    -8L8+8). +hese trigger are called Isteadoftriggers because, unli5e other ty#esof triggers, oracle res the trigger instead of e2ecuting the triggeringstateent. +he trigger #erfor u#date, insert or delete o#eration directly onthe underlying tables. Bsers $rite noral I>S89+, BP-;+8 and -8L8+8stateent against the %ie$ nad the I>S+8;-* trigger $or5s in%isibly in thebac5ground to a5e the reght action ta5e #lace.

    Instead of trigger are only a##licable only for ro$ le%el triggers.

    Syste Le%el triggers ;fter Startu#, :efore Shutdo$n, ;fter Logging, beforelogoH,

    Ser%er8rror, create, alter, delete

    +otal is 1! database triggers.

    C) 7o#y a structure of a table $ithout records?C) create tableJnae as select U fro sourceJtable $here 1K(4

    GE) If t$o structure are sae and one table in ha%ing data ho$ to insert thedata into e#tytable?GE) insert into tablenae select U fro tablenae.

    G1) o$ to get salary $hose salary is greate then a%g sal?G1) select salary fro e#loyee $here salary T select a%gsalary) froe#loyee)4

    G() What $ill you get if you run "select 1 fro &aEJcustoer4'G() It $ill return nuber or ro$s $ith 1 li5e if there are to records it $ill return11