PLSQL Full Notes1
PLSQL Full Notes1
Character Datatypes
Data Type Syntax char(size) nchar(size) Oracle 9i Maximum size of 2 bytes. Maximum size of 2 bytes. Oracle 10g Maximum size of 2 bytes. Maximum size of 2 bytes. Maximum size of ' bytes. Maximum size of ' bytes. Maximum size of 2)*. Maximum size of 2 bytes. Maximum size of 2)*. Oracle 11g Maximum size of 2 bytes. Maximum size of 2 bytes. Maximum size of ' bytes. Maximum size of ' bytes. Maximum size of 2)*. Maximum size of 2 bytes. Maximum size of 2)*. Explanation (i applica!le" !here size is the number of characters to store. "ixed#length strings. $pace padded. !here size is the number of characters to store. "ixed#length %&$ string $pace padded. !here size is the number of characters to store. (ariable#length %&$ string. !here size is the number of characters to store. (ariable#length string. (ariable#length strings. (bac+ward compatible) (ariable#length binary strings (ariable#length binary strings. (bac+ward compatible)
nvarchar2(size) Maximum size of ' bytes. varchar2(size) long raw long raw Maximum size of ' bytes. Maximum size of 2)*. Maximum size of 2 bytes. Maximum size of 2)*.
#$%eric Datatypes
Data Type Syntax Oracle 9i Oracle 10g Oracle 11g Explanation (i applica!le"
number(p,s)
-recision can range from . to /0. $cale can range from #0' to .21.
-recision can range from . to /0. $cale can range from #0' to .21.
-recision can range from !here p is the precision and s is the scale. . to /0. $cale can range from #0' "or example, number(1,2) is a number that has 2 digits before the decimal to .21. and 2 digits after the decimal.
numeric(p,s)
-recision can range from -recision can range from -recision can range from !here p is the precision and s is the scale. . to /0. . to /0. . to /0. "or example, numeric(1,2) is a number that has 2 digits before the decimal and 2 digits after the decimal.
float dec(p,s) -recision can range from -recision can range from -recision can range from !here p is the precision and s is the scale. . to /0. . to /0. . to /0. "or example, dec(/,.) is a number that has 2 digits before the decimal and . digit after the decimal. decimal(p,s) -recision can range from -recision can range from -recision can range from !here p is the precision and s is the scale. . to /0. . to /0. . to /0. "or example, decimal(/,.) is a number that has 2 digits before the decimal and . digit after the decimal. integer int smallint real double precision
Date/Ti%e Datatypes
Data Type Syntax date timestamp (fractional seconds precision) Oracle 9i Oracle 10g Oracle 11g Explanation (i applica!le"
3 date between 4an ., '1.2 *5 3 date between 4an ., '1.2 *5 3 date between 4an ., '1.2 *5 and 6ec /., 7777 36. and 6ec /., 7777 36. and 6ec /., 7777 36. fractional seconds precision must be a number between and 7. (default is 8) fractional seconds precision must be a number between and 7. (default is 8) fractional seconds precision must be a number between and 7. (default is 8) 9ncludes year, month, day, hour, minute, and seconds. "or example: timestamp(8)
9ncludes year, month, day, hour, minute, and seconds; with a time zone displacement value. "or example: timestamp(2) with time zone
9ncludes year, month, day, hour, minute, and seconds; with a time zone expressed as the session time zone. "or example: timestamp(') with local time zone
year precision is the number of year precision is the number of year precision is the number of Time period stored in years and months. digits in the year. (default is 2) digits in the year. (default is 2) digits in the year. (default is 2) "or example: interval year(') to month day precision must be a day precision must be a day precision must be a Time period stored in days, hours, minutes,
and 7.
and 7.
and 7.
Maximum file size of Maximum file size of 2/2#. bytes. ')*. $tore up to ')* of binary data. $tore up to ')* of character data. $tore up to ')* of character text data. $tore up to (' gigabytes #.) < (the value of the 5=>%? parameter of &O* storage). $tore up to (' gigabytes #.) < (the value of the 5=>%? parameter of &O* storage) of character data. $tore up to (' gigabytes #.) < (the value of the 5=>%? parameter of &O* storage) of character text data.
blob clob
$tore up to (' gigabytes #.) < (the value of the 5=>%? parameter of &O* storage). $tore up to (' gigabytes #.) < (the value of the 5=>%? parameter of &O* storage) of character data. $tore up to (' gigabytes #.) < (the value of the 5=>%? parameter of &O* storage) of character text data.
nclob
(o)i* Datatypes
Data Type Syntax rowid Oracle 9i The format of the rowid is: *******.AAAA.""""" !here ******* is the bloc+ in the database file; AAAA is the row in the bloc+; """"" is the database file. urowid(size) Oracle 10g The format of the rowid is: *******.AAAA.""""" !here ******* is the bloc+ in the database file; AAAA is the row in the bloc+; """"" is the database file. Oracle 11g The format of the rowid is: *******.AAAA.""""" !here ******* is the bloc+ in the database file; AAAA is the row in the bloc+; """"" is the database file. >niversal rowid. !here size is optional.
5opyright C 2 /#2 7 Tech on the %et. 3ll rights reserved.
Explanation (i applica!le" "ixed#length binary data. Bvery record in the database has a physical address or ro)i*.
LDescription ,archar5(60"7
6eclaring a variable with an initial value (not a constant): LType ,archar5(10" :2 =Exa%ple=7
Fou can also use E9$ %OT %>&&E in an $D& statement. "or example:
select ? ro% s$ppliers )here s$pplier-na%e >S #OT #1LL7 This will return all records from the suppliers table where the supplier_name does not contain a null value.
Fou can also use E9$ %>&&E in an $D& statement. "or example: select ? ro% s$ppliers )here s$pplier-na%e >S #1LL7 This will return all records from the suppliers table where the supplier_name contains a null value.
Case State%ent
BOTO State%ent Loop State%ent 4O( Loop C1(SO( 4O( Loop Chile Loop (epeat 1ntil Loop Exit State%ent
Syntax D:: >4@TAE#@ELS>4 >4 con*ition TAE# E<<<state%ents<<<F ELS>4 con*ition TAE# E<<<state%ents<<<F ELSE E<<<state%ents<<<F E#D >47
=ere is an example of a function that uses the 9"#T=B%#B&$B statement: C(E/TE O( (EPL/CE 4$nction >nco%eLe,el ( na%e-in ># ,archar5 " (ET1(# ,archar5 >S %onthly-,al$e n$%!er(;"7 >Le,el ,archar5(50"7 c$rsor c1 is select %onthly-inco%e ro% e%ployees )here na%e 2 na%e-in7 'EB>#
open c17 etch c1 into %onthly-,al$e7 close c17 >4 %onthly-,al$e G2 6000 TAE# >Le,el :2 =Lo) >nco%e=7 ELS>4 %onthly-,al$e H 6000 an* %onthly-,al$e G2 I000 TAE# >Le,el :2 =/,g >nco%e=7 ELS>4 %onthly-,al$e H I000 an* %onthly-,al$e G2 1J000 TAE# >Le,el :2 =Ko*erate >nco%e=7 ELSE >Le,el :2 =Aigh >nco%e=7 E#D >47 (ET1(# >Le,el7 E#D7 9n this example, weGve created a function called 9ncome&evel. 9t has one parameter called name_in and it returns a varchar2. The function will return the income level based on the employeeGs name.
<<< CAE# con*ition-n TAE# res$lt-n ELSE res$lt E#D expression is optional. 9t is the value that you are comparing to the list of conditions. (ie: conditionH., conditionH2, ... conditionHn) condition_1 to condition_n must all be the same datatype. 5onditions are evaluated in the order listed. Once a condition is found to be true, the case statement will return the result and not evaluate the conditions any further. result_1 to result_n must all be the same datatype. This is the value returned once a condition is found to be true.
#ote: 9f no condition is found to be true, then the case statement will return the value in the B&$B clause. 9f the B&$B clause is omitted and no condition is found to be true, then the case statement will return %>&&. Fou can have up to 222 comparisons in a case statement. Bach !=B% ... T=B% clause is considered 2 comparisons.
/pplies To:
4or exa%ple: Fou could use the case statement in an $D& statement as follows: (includes the expression clause)
select ta!le-na%e9 C/SE o)ner CAE# =SLS= TAE# =The o)ner is SLS= CAE# =SLSTEK= TAE# =The o)ner is SLSTEK= ELSE =The o)ner is another ,al$e= E#D ro% all-ta!les7 Or you could write the $D& statement using the case statement li+e this: (omits the expression clause) select ta!le-na%e9 C/SE CAE# o)ner2=SLS= TAE# =The o)ner is SLS= CAE# o)ner2=SLSTEK= TAE# =The o)ner is SLSTEK= ELSE =The o)ner is another ,al$e= E#D ro% all-ta!les7 The above two case statements are eIuivalent to the following 9"#T=B%#B&$B statement: >4 o)ner 2 =SLS= TAE# res$lt :2 =The o)ner is SLS=7 ELS>4 o)ner 2 =SLSTEK= TAE# res$lt :2 =The o)ner is SLSTEK==7 ELSE res$lt :2 =The o)ner is another ,al$e=7 E#D >47
The case statement will compare each owner value, one by one.
One thing to note is that the B&$B clause within the case statement is optional. Fou could have omitted it. &etGs ta+e a loo+ at the $D& statement above with the B&$B clause omitted. Four $D& statement would loo+ as follows: select ta!le-na%e9 C/SE o)ner CAE# =SLS= TAE# =The o)ner is SLS= CAE# =SLSTEK= TAE# =The o)ner is SLSTEK= E#D ro% all-ta!les7 !ith the B&$B clause omitted, if no condition was found to be true, the case statement would return %>&&.
4or Exa%ple: =ere is an example that demonstrates how to use the case statement to compare different conditions: select C/SE CAE# a G ! TAE# =hello= CAE# * G e TAE# =goo*!ye= E#D ro% s$ppliers7
Q$estion: 5an you create a case statement that evaluates two different fieldsJ 9 want to return a value based on the combinations in two different fields. /ns)er: Fes, below is an example of a case statement that evaluates two different fields.
select s$pplier-i*9 C/SE CAE# s$pplier-na%e 2 =>'K= an* s$pplier-type 2 =Aar*)are= TAE# =#orth o ice= CAE# s$pplier-na%e 2 =>'K= an* s$pplier-type 2 =So t)are= TAE# =So$th o ice= E#D ro% s$ppliers7 $o if supplierHname field is IBM and the supplierHtype field is Hardware, then the case statement will return North office. 9f the supplierHname field is IBM and the supplierHtype is Software, the case statement will return South office.
Then later in the code, you would place your label and code associated with that label. La!el-na%e: EstatementsF
Fou would use a &OO- statement when you are not sure how many times you want the loop body to execute and you want the loop body to execute at least once. The &OO- statement is terminated when it encounters either an BK9T statement or when it encounters an BK9T !=B% statement that evaluated to TA>B. &etGs ta+e a loo+ at an example: LOOP %onthly-,al$e :2 *aily-,al$e ? :17 EO>T CAE# %onthly-,al$e H 60007 E#D LOOP7 9n this example, the &OO- would terminate when the monthlyHvalue exceeded ' .
&etGs ta+e a loo+ at an example. 4O( Lcntr ># 1<<50 LOOP LCalc :2 Lcntr ? :17 E#D LOOP7 This example will loop 2 times. The counter will start at . and end at 2 .
The "OA &oop can also loop in reverse. "or example: 4O( Lcntr ># (E+E(SE 1<<1J LOOP LCalc :2 Lcntr ? :17 E#D LOOP7 This example will loop .2 times. The counter will start at .2 and end at .. (loops bac+wards)
'EB># total-,al :2 07 4O( e%ployee-rec in c1 LOOP total-,al :2 total-,al P e%ployee-rec<%onthly-inco%e7 E#D LOOP7 (ET1(# total-,al7 E#D7 9n this example, weGve created a cursor called c.. The C !S"! #"! Loop will terminate after all records have been fetched from the cursor c..
9n this example, the !=9&B &oop would terminate once the monthlyHvalue exceeded '
LOOP %onthly-,al$e :2 *aily-,al$e ? :17 EO>T CAE# %onthly-,al$e H 60007 E#D LOOP7 9n this example, the &OO- would terminate when the monthlyHvalue exceeded ' .
K/O+/L1E 999999999999999999999999999 $o you can simplify your 5AB3TB $BD>B%5B command as follows: C(E/TE SEQ1E#CE s$pplier-seM K>#+/L1E 1 ST/(T C>TA 1 >#C(EKE#T 'L 1 C/CAE 507 %ow that youGve created a seIuence ob@ect to simulate an autonumber field, weGll cover how to retrieve a value from this seIuence ob@ect. To retrieve the next value in the seIuence order, you need to use nextval. "or example: s$pplier-seM<next,al This would retrieve the next value from supplier_se$. The nextval statement needs to be used in an $D& statement. "or example: >#SE(T >#TO s$ppliers (s$pplier-i*9 s$pplier-na%e" +/L1ES (s$pplier-seM<next,al9 =Qra t 4oo*s="7 This insert statement would insert a new record into the suppliers table. The supplier_id field would be assigned the next number from the supplier_se$ seIuence. The supplier_name field would be set to ?raft "oods.
One common Iuestion about seIuences is: Q$estion: !hile creating a seIuence, what does cache and nocache options meanJ "or example, you could create a seIuence with a cache of 2 as follows:
C(E/TE SEQ1E#CE s$pplier-seM K>#+/L1E 1 ST/(T C>TA 1 >#C(EKE#T 'L 1 C/CAE 507
Or you could create the same seIuence with the nocache option: C(E/TE SEQ1E#CE s$pplier-seM K>#+/L1E 1 ST/(T C>TA 1 >#C(EKE#T 'L 1 #OC/CAE7
/ns)er: !ith respect to a seIuence, the cache option specifies how many seIuence values will be stored in memory for faster access. The downside of creating a seIuence with a cache is that if a system failure occurs, all cached seIuence values that have not be used, will be ElostE. This results in a EgapE in the assigned seIuence values. !hen the system comes bac+ up, Oracle will cache new numbers from where it left off in the seIuence, ignoring the so called ElostE seIuence values. %ote: To recover the lost seIuence values, you can always execute an 3&TBA $BD>B%5B command to reset the counter to the correct value. Nocache means that none of the seIuence values are stored in memory. This option may sacrifice some performance, however, you should not encounter a gap in the assigned seIuence values.
Q$estion: =ow do we set the &3$T(3&>B value in an Oracle $eIuenceJ /ns)er: Fou can change the &3$T(3&>B for an Oracle seIuence, by executing an 3&TBA $BD>B%5B command.
"or example, if the last value used by the Oracle seIuence was . commands. alter seM$ence seM-na%e incre%ent !y 1567 select seM-na%e<next,al ro% *$al7 alter seM$ence seM-na%e incre%ent !y 17 %ow, the next value to be served by the seIuence will be 222.
and you would li+e to reset the seIuence to serve 222 as the next value. Fou would execute the following
Oracle/PLSQL: Co%%it
The syntax for the 5OMM9T statement is: COKK>T .CO(Q0 .COKKE#T text07 The 5ommit statement commits all changes for the current session. Once a commit is issued, other users will be able to see your changes.
Oracle/PLSQL: (oll!acN
The syntax for the AO&&*35? statement is: (OLL'/CQ .CO(Q0 .TO .S/+EPO>#T0 sa,epoint-na%e07 The Aollbac+ statement undoes all changes for the current session up to the savepoint specified. 9f no savepoint is specified, then all changes are undone.
LOCQ T/'LE ta!les ># locN-%o*e KODE .#OC/>T07 Ta!les is a comma#delimited list of tables. LocN-%o*e is one of: !"% SH&!' !"% '(CL SI)' SH&!' *+&,' SH&!' SH&!' !"% '(CL SI)' '(CL SI)'#oCait specifies that the database should not wait for a loc+ to be released.
5ursor Bxamples: Proce*$re that o$tp$ts a *yna%ic PLSQL c$rsor C$rsor )ithin a c$rsor C$rsor )ith ,aria!le in an T># CL/1SET
C1(SO( c1 >S SELECT co$rse-n$%!er ro% co$rses-t!l )here co$rse-na%e 2 na%e-in7 The result set of this cursor is all courseHnumbers whose courseHname matches the variable called nameHin.
*elow is a function that uses this cursor. C(E/TE O( (EPL/CE 4$nction 4in*Co$rse ( na%e-in ># ,archar5 " (ET1(# n$%!er >S cn$%!er n$%!er7 C1(SO( c1 >S SELECT co$rse-n$%!er ro% co$rses-t!l )here co$rse-na%e 2 na%e-in7 'EB># open c17 etch c1 into cn$%!er7 i c1Snot o$n* then cn$%!er :2 99997 en* i 7 close c17 (ET1(# cn$%!er7
E#D7
"or example, you could define a cursor called c2 as below. C1(SO( c5 (s$!&ect-i*-in ># ,archar5" >S SELECT co$rse-n$%!er ro% co$rses-t!l )here s$!&ect-i* 2 s$!&ect-i*-in7 The result set of this cursor is all courseHnumbers whose sub@ectHid matches the sub@ectHid passed to the cursor via the parameter.
"or example, you could define a cursor called c/ as below. C1(SO( c: (ET1(# co$rses-t!lS(OCTLPE >S SELECT ? ro% co$rses-t!l )here s$!&ect 2 =Kathe%atics=7 The result set of this cursor is all columns from the courseHtbl where the sub@ect is Mathematics.
"or example, you could open a cursor called c. with the following command: OPE# c17
*elow is a function that demonstrates how to use the O-B% statement: C(E/TE O( (EPL/CE 4$nction 4in*Co$rse ( na%e-in ># ,archar5 " (ET1(# n$%!er >S cn$%!er n$%!er7
C1(SO( c1 >S SELECT co$rse-n$%!er ro% co$rses-t!l )here co$rse-na%e 2 na%e-in7 'EB># open c17 etch c1 into cn$%!er7 i c1Snot o$n* then cn$%!er :2 99997 en* i 7 close c17 (ET1(# cn$%!er7 E#D7
C1(SO( c1 >S SELECT co$rse-n$%!er ro% co$rses-t!l )here co$rse-na%e 2 na%e-in7 The command that would be used to fetch the data from this cursor is: 4ETCA c1 into cn$%!er7 This would fetch the first courseHnumber into the variable called cnumber;
*elow is a function that demonstrates how to use the "BT5= statement. C(E/TE O( (EPL/CE 4$nction 4in*Co$rse ( na%e-in ># ,archar5 " (ET1(# n$%!er >S cn$%!er n$%!er7 C1(SO( c1 >S SELECT co$rse-n$%!er ro% co$rses-t!l )here co$rse-na%e 2 na%e-in7 'EB># open c17 etch c1 into cn$%!er7 i c1Snot o$n* then cn$%!er :2 99997 en* i 7
"or example, you could close a cursor called c. with the following command: CLOSE c17
*elow is a function that demonstrates how to use the 5&O$B statement: C(E/TE O( (EPL/CE 4$nction 4in*Co$rse ( na%e-in ># ,archar5 " (ET1(# n$%!er >S cn$%!er n$%!er7 C1(SO( c1 >S
SELECT co$rse-n$%!er ro% co$rses-t!l )here co$rse-na%e 2 na%e-in7 'EB># open c17 etch c1 into cn$%!er7 i c1Snot o$n* then cn$%!er :2 99997 en* i 7 close c17 (ET1(# cn$%!er7 E#D7
# Aeturns "3&$B if no row was returned. M%OT"O>%6 # Aeturns 9%(3&96H5>A$OA if cursor is declared, but not open; or if cursor has been closed. # Aeturn %>&& if cursor is open, but fetch has not been executed. # Aeturns "3&$B if a successful fetch has been executed. # Aeturns TA>B if no row was returned. MAO!5O>%T # Aeturns 9%(3&96H5>A$OA if cursor is declared, but not open; or if cursor has been closed. # Aeturns the number of rows fetched. # The AO!5O>%T attribute doesnGt give the real row count until you have iterated through the entire cursor. 9n other words, you shouldnGt rely on this attribute to tell you how many rows are in a cursor after it is opened.
*elow is an example of how you might use the M%OT"O>%6 attribute. C(E/TE O( (EPL/CE 4$nction 4in*Co$rse ( na%e-in ># ,archar5 " (ET1(# n$%!er >S cn$%!er n$%!er7 C1(SO( c1 >S SELECT co$rse-n$%!er ro% co$rses-t!l )here co$rse-na%e 2 na%e-in7
'EB># open c17 etch c1 into cn$%!er7 i c1Snot o$n* then cn$%!er :2 99997 en* i 7 close c17 (ET1(# cn$%!er7 E#D7
"or example, you could use the Select 4or 1p*ate statement as follows: C1(SO( c1 >S
9f you plan on updating or deleting records that have been referenced by a Select 4or 1p*ate statement, you can use the Chere C$rrent O statement.
The !here 5urrent Of statement allows you to update or delete the record that was last fetched by the cursor.
C(E/TE O( (EPL/CE 4$nction 4in*Co$rse ( na%e-in ># ,archar5 " (ET1(# n$%!er >S cn$%!er n$%!er7 C1(SO( c1 >S SELECT co$rse-n$%!er ro% co$rses-t!l )here co$rse-na%e 2 na%e-in 4O( 1PD/TE o instr$ctor7 'EB># open c17 etch c1 into cn$%!er7 i c1Snot o$n* then cn$%!er :2 99997 else 1PD/TE co$rses-t!l SET instr$ctor 2 =SK>TA= CAE(E C1((E#T O4 c17 COKK>T7 en* i 7 close c17 (ET1(# cn$%!er7 E#D7
E#D7
/ns)er: To solve your problem, you will need to output a dynamic -&$D& cursor in Oracle. &etGs ta+e a loo+ at how we can do this. !eGve divided this process into / steps.
create ta!le )ine ( col1 ,archar5(60"9 col5 ,archar5(60"9 col: ,archar5(60" "7 !eGve made this table definition very simple, for demonstration purposes.
create or replace P/CQ/BE )inepNg >S /? De ine the (E4 C1(SO( type< ?/ TLPE )ine-type >S (E4 C1(SO( (ET1(# )ineS(OCTLPE7 E#D )inepNg7 This cursor will accept all fields from the EwineE table.
create or replace proce*$re in*-)ine5 (col1-in in ,archar59 col5-in in ,archar59 col:-in in ,archar59 c1 o$t )inepNg<)ine-type" as 'EB># /? all col$%ns )ere entere* ?/ >4 (length(col1-in" H 0" an* (length(col5-in" H 0" an* (length(col:-in" H 0" TAE# OPE# c1 4O( select ? ro% )ine )here )ine<col1 2 col1-in an* )ine<col5 2 col5-in an* )ine<col: 2 col:-in7 /? col1 an* col5 )ere entere* ?/ ELS>4 (length(col1-in" H 0" an* (length(col5-in" H 0" an* (length(col:-in" 2 0" TAE# OPE# c1 4O( select ? ro% )ine )here )ine<col1 2 col1-in an* )ine<col5 2 col5-in7 /? col1 an* col: )ere entere* ?/ ELS>4 (length(col1-in" H 0" an* (length(col5-in" 2 0" an* (length(col:-in" H 0" TAE# OPE# c1 4O( select ? ro% )ine )here )ine<col1 2 col1-in an* )ine<col: 2 col:-in7
/? col5 an* col: )here entere* ?/ ELS>4 (length(col1-in" 2 0" an* (length(col5-in" H 0" an* (length(col:-in" H 0" TAE# OPE# c1 4O( select ? ro% )ine )here )ine<col5 2 col5-in an* )ine<col: 2 col:-in7 /? col1 )as entere* ?/ ELS>4 (length(col1-in" H 0" an* (length(col5-in" 2 0" an* (length(col:-in" 2 0" TAE# OPE# c1 4O( select ? ro% )ine )here )ine<col1 2 col1-in7 /? col5 )as entere* ?/ ELS>4 (length(col1-in" 2 0" an* (length(col5-in" H 0" an* (length(col:-in" 2 0" TAE# OPE# c1 4O( select ? ro% )ine )here )ine<col5 2 col5-in7 /? col: )as entere* ?/ ELS>4 (length(col1-in" 2 0" an* (length(col5-in" 2 0" an* (length(col:-in" H 0" TAE# OPE# c1 4O( select ? ro% )ine )here )ine<col: 2 col:-in7 E#D >47 E#D in*-)ine57
Q$estion: 9n -$D&, 9 want to declare a cursor within cursor. The second cursor should use a value from the first cursor in the Ewhere clauseE. =ow can 9 do thisJ
/ns)er: *elow is an example of how to declare a cursor within a cursor. 9n this example, we have a cursor called .et_ta/les that retrieves the owner and tableHname values. These values are then used in a second cursor called .et_columns. create or replace proce*$re K1LT>PLE-C1(SO(S-P(OC is ,-o)ner ,archar5(60"7 ,-ta!le-na%e ,archar5(60"7 ,-col$%n-na%e ,archar5(100"7 /? 4irst c$rsor ?/ c$rsor get-ta!les is select *istinct t!l<o)ner9 t!l<ta!le-na%e ro% all-ta!les t!l )here t!l<o)ner 2 =SLSTEK=7 /? Secon* c$rsor ?/ c$rsor get-col$%ns is select *istinct col<col$%n-na%e ro% all-ta!-col$%ns col )here col<o)ner 2 ,-o)ner an* col<ta!le-na%e 2 ,-ta!le-na%e7 !egin @@ Open irst c$rsor open get-ta!les7 loop etch get-ta!les into ,-o)ner9 ,-ta!le-na%e7
open get-col$%ns7 loop etch get-col$%ns into ,-col$%n-na%e7 en* loop7 close get-col$%ns7 en* loop7 close get-ta!les7 EOCEPT>O# CAE# OTAE(S TAE# raise-application-error(@500019=/n error )as enco$ntere* @ =33SQLCODE33= @E((O(@ =33SQLE((K"7 en* K1LT>PLE-C1(SO(S-P(OC7
The tric+ to declaring a cursor within a cursor is that you need to continue to open and close the second cursor each time a new record is retrieved from the first cursor. That way, the second cursor will use the new variable values from the first cursor.
5reate or Aeplace "unction funcHname (in%umbers in (archar2) Aeturn -ac+age%ame.refHcursor 3s outHcursor -ac+age%ame.AefHcursor; *egin Open outHcursor "or $elect < from TableHname where columnHname in (in%umbers); Aeturn outHcursor; Bnd; 9 seem to be getting an error when 9 try the code above. =ow can 9 use a variable in an 9% 5&3>$BJ
/ns)er: >nfortunately, there is no easy way to use a variable in an 9% 5&3>$B if the variable contains a list of items. !e can, however, suggest two alternative options:
Option D1
9nstead of creating a string variable that contains a list of numbers, you could try storing each value in a separate variable. "or example: Create or (eplace 4$nction $nc-na%e (et$rn PacNage#a%e<re -c$rsor /s o$t-c$rsor PacNage#a%e<(e -c$rsor7 ,1 ,archar(5"7 ,5 ,archar(5"7 ,: ,archar(5"7 'egin
,1 :2 =1=7 ,5 :2 =5=7 ,: :2 =:=7 Open o$t-c$rsor 4or Select ? ro% Ta!le-na%e )here col$%n-na%e in (,19 ,59 ,:"7 (et$rn o$t-c$rsor7 En*7
Option D5
Fou could try storing your values in a table. Then use a sub#select to retrieve the values. "or example: Create or (eplace 4$nction $nc-na%e (et$rn PacNage#a%e<re -c$rsor /s o$t-c$rsor PacNage#a%e<(e -c$rsor7 'egin Open o$t-c$rsor 4or Select ? ro% Ta!le-na%e )here col$%n-na%e in (select ,al$es ro% list-ta!le"7 (et$rn o$t-c$rsor7 En*7 9n this example, weGve stored our list in a table called list_ta/le.
Con,ert Deco%pose D$%p >nitcap >nstr Length #$%ToDS>nter,al #$%ToLK>nter,al (a)ToAex To-Char To-Clo! Bro$p->D Lag Lea* L##+L #/#+L Co,ar-pop Co,ar-sa%p Co$nt C$%e-Dist Dense-(anN Exp Extract 4loor Breatest Least
Lo)er Lpa* Ltri% (eplace (pa* (tri% To-Date To-DS>nter,al To-Lo! To-K$lti-'yte To-#Clo! #1LL>4 #+L #+L5 Sys-Context 1i* Kax Ke*ian Kin Ko* Po)er (anN (e%ain*er (o$n* (n$%!ers" (o$n* (*ates" Sign
So$n*ex S$!str Translate Tri% 1pper +SiVe To-#$%!er To-Single-'yte To-Ti%esta%p To-Ti%esta%p-TV To-LK>nter,al 1ser 1serEn,
SMrt St*De, S$% Tan Tanh Tr$nc (n$%!ers" Tr$nc (*ates" +ar-pop +ar-sa%p +ariance
Cos Cosh 6ate "unctions: /**-Konths C$rrent-Date C$rrent-Ti%esta%p D!Ti%eRone 4ro%-TV Brror "unctions: SQLCODE SQLE((K
Sin Sinh (o$n* SessionTi%eRone Sys*ate SysTi%esta%p To-Char To-Date Tr$nc TV-O set
Miscellaneous "unctions: (etrie,e $ser >D ro% the c$rrent Oracle session (etrie,e the session >D or the $ser logge* in
3&&H6*H&9%?$ 3&&HBAAOA$ 3&&H9%6BKB$ 3&&H9%6H5O&>M%$ 3&&H&O*$ 3&&HO*4B5T$ 3&&HO*4B5THT3*&B$ 3&&H$BD>B%5B$ 3&&H$%3-$=OT$ 3&&H$O>A5B 3&&H$F%O%FM$ 3&&HT3*&B$ 3&&HT3*H5O&>M%$ 3&&HT3*H5O&H$T3T9$T95$ 3&&HT3*H5OMMB%T$ 3&&HTA9))BA$ 3&&HTA9))BAH5O&$ 3&&HTF-B$ 3&&H>-63T3*&BH5O&>M%$ 3&&H>$BA$ 3&&H(9B!$
6atabase lin+s accessible to the user 5urrent errors on stored ob@ects that user is allowed to create 6escriptions of indexes on tables accessible to the user 5O&>M%s comprising 9%6BKes on accessible T3*&B$ 6escription of &O*s contained in tables accessible to the user Ob@ects accessible to the user 6escription of all ob@ect tables accessible to the user 6escription of $BD>B%5Bs accessible to the user $napshots the user can access 5urrent source on stored ob@ects that user is allowed to create 3ll synonyms accessible to the user 6escription of relational tables accessible to the user 5olumns of userGs tables, views and clusters 5olumns of userGs tables, views and clusters 5omments on tables and views accessible to the user Triggers accessible to the current user 5olumn usage in userGs triggers or in triggers on userGs tables 6escription of types accessible to the user 6escription of all updatable columns 9nformation about all users of the database 6escription of views accessible to the user
63T3*3$BH5OM-3T9*&BH&B(B& 6*3H6*H&9%?$ 6*3HBAAOA$ 6*3HO*4B5T$ 6*3HAO&B$ 6*3HAO&BH-A9($ 6*3H$O>A5B 6*3HT3*&B$-35B$ 6*3HT3*H-A9($ 6*3HTA9))BA$ 6*3HT$HD>OT3$ 6*3H>$BA$ 6*3H(9B!$ 695T9O%3AF 695TH5O&>M%$ )&O*3&H%3MB %&$H63T3*3$BH-3A3MBTBA$ %&$H9%$T3%5BH-3A3MBTBA$ %&$H$B$$9O%H-3A3MBTBA$
6atabase compatible parameter set via init.ora 3ll database lin+s in the database 5urrent errors on all stored ob@ects in the database 3ll ob@ects in the database 3ll Aoles which exist in the database Aoles granted to users and roles $ource of all stored ob@ects in the database 6escription of all tablespaces 3ll grants on ob@ects in the database 3ll triggers in the database Tablespace Iuotas for all users 9nformation about all users of the database 6escription of all views in the database 6escription of data dictionary tables and views 6escription of columns in data dictionary tables and views global database name -ermanent %&$ parameters of the database %&$ parameters of the instance %&$ parameters of the user session
-AO6>5TH5OM-O%B%TH(BA$9O% version and status information for component products AO&BHT3*H-A9($ Table privileges granted to roles
-rivileges which the user currently has set Aoles which the user currently has enabled. 6escription table for privilege type codes. Maps privilege type numbers to type names )rants on ob@ects for which the user is the grantor, grantee, owner, or an enabled role or ->*&95 is the grantee 6escription table for privilege (auditing option) type codes. Maps privilege (auditing option) type numbers to type names
C(E/TE T/'LE ta!le-na%e (col$%n1 *atatype n$ll/not n$ll9 col$%n5 *atatype n$ll/not n$ll9 <<< CO#ST(/>#T constraint-na%e P(>K/(L QEL (col$%n19 col$%n59 < col$%n-n" "7
4or exa%ple: C(E/TE T/'LE s$pplier ( s$pplier-i* n$%eric(10" not n$ll9 s$pplier-na%e ,archar5(J0" not n$ll9 contact-na%e ,archar5(J0"9 CO#ST(/>#T s$pplier-pN P(>K/(L QEL (s$pplier-i*" "7 9n this example, weGve created a primary +ey on the supplier table called supplierHp+. 9t consists of only one field # the supplierHid field.
!e could also create a primary +ey with more than one field as in the example below: C(E/TE T/'LE s$pplier ( s$pplier-i* n$%eric(10" not n$ll9 s$pplier-na%e ,archar5(J0" not n$ll9 contact-na%e ,archar5(J0"9 CO#ST(/>#T s$pplier-pN P(>K/(L QEL (s$pplier-i*9 s$pplier-na%e" "7
/LTE( T/'LE ta!le-na%e a** CO#ST(/>#T constraint-na%e P(>K/(L QEL (col$%n19 col$%n59 <<< col$%n-n"7
4or exa%ple: /LTE( T/'LE s$pplier a** CO#ST(/>#T s$pplier-pN P(>K/(L QEL (s$pplier-i*"7 9n this example, weGve created a primary +ey on the existing supplier table called supplierHp+. 9t consists of the field called supplierHid.
!e could also create a primary +ey with more than one field as in the example below: /LTE( T/'LE s$pplier a** CO#ST(/>#T s$pplier-pN P(>K/(L QEL (s$pplier-i*9 s$pplier-na%e"7
9n this example, weGre dropping a primary +ey on the supplier table called supplierHp+.
4or exa%ple: /LTE( T/'LE s$pplier *isa!le CO#ST(/>#T s$pplier-pN7 9n this example, weGre disabling a primary +ey on the supplier table called supplierHp+.
4or exa%ple:
/LTE( T/'LE s$pplier ena!le CO#ST(/>#T s$pplier-pN7 9n this example, weGre enabling a primary +ey on the supplier table called supplierHp+.
The referenced table is called the parent ta!le while the table with the foreign +ey is called the chil* ta!le. The foreign +ey in the child table will generally reference a pri%ary Ney in the parent table. 3 foreign +ey can be defined in either a 5AB3TB T3*&B statement or an 3&TBA T3*&B statement.
4or exa%ple: C(E/TE T/'LE s$pplier ( s$pplier-i* n$%eric(10" not n$ll9 s$pplier-na%e ,archar5(J0" not n$ll9 contact-na%e ,archar5(J0"9 CO#ST(/>#T s$pplier-pN P(>K/(L QEL (s$pplier-i*" "7 C(E/TE T/'LE pro*$cts ( pro*$ct-i* n$%eric(10" s$pplier-i* n$%eric(10" CO#ST(/>#T N-s$pplier
4O(E>B# QEL (s$pplier-i*" (E4E(E#CES s$pplier(s$pplier-i*" "7 9n this example, weGve created a primary +ey on the supplier table called supplier_p0. 9t consists of only one field # the supplierHid field. Then weGve created a foreign +ey called f0_supplier on the products table that references the supplier table based on the supplierHid field.
!e could also create a foreign +ey with more than one field as in the example below: C(E/TE T/'LE s$pplier ( s$pplier-i* n$%eric(10" not n$ll9 s$pplier-na%e ,archar5(J0" not n$ll9 contact-na%e ,archar5(J0"9 CO#ST(/>#T s$pplier-pN P(>K/(L QEL (s$pplier-i*9 s$pplier-na%e" "7 C(E/TE T/'LE pro*$cts ( pro*$ct-i* n$%eric(10" not n$ll9 s$pplier-i* n$%eric(10" not n$ll9 s$pplier-na%e ,archar5(J0" not n$ll9 CO#ST(/>#T N-s$pplier-co%p 4O(E>B# QEL (s$pplier-i*9 s$pplier-na%e" (E4E(E#CES s$pplier(s$pplier-i*9 s$pplier-na%e" "7 9n this example, our foreign +ey called f0_forei.n_comp references the supplier table based on two fields # the supplierHid and supplierHname fields.
/LTE( T/'LE ta!le-na%e a** CO#ST(/>#T constraint-na%e 4O(E>B# QEL (col$%n19 col$%n59 <<< col$%n-n" (E4E(E#CES parent-ta!le (col$%n19 col$%n59 <<< col$%n-n"7
4or exa%ple: /LTE( T/'LE pro*$cts a** CO#ST(/>#T N-s$pplier 4O(E>B# QEL (s$pplier-i*" (E4E(E#CES s$pplier(s$pplier-i*"7 9n this example, weGve created a foreign +ey called f0_supplier that references the supplier table based on the supplierHid field.
!e could also create a foreign +ey with more than one field as in the example below: /LTE( T/'LE pro*$cts a** CO#ST(/>#T N-s$pplier 4O(E>B# QEL (s$pplier-i*9 s$pplier-na%e" (E4E(E#CES s$pplier(s$pplier-i*9 s$pplier-na%e"7
3 foreign +ey with a cascade delete means that if a record in the parent table is deleted, then the corresponding records in the child table with automatically be deleted. This is called a cascade delete. 3 foreign +ey with a cascade delete can be defined in either a 5AB3TB T3*&B statement or an 3&TBA T3*&B statement.
4or exa%ple: C(E/TE T/'LE s$pplier ( s$pplier-i* n$%eric(10" not n$ll9 s$pplier-na%e ,archar5(J0" not n$ll9 contact-na%e ,archar5(J0"9 CO#ST(/>#T s$pplier-pN P(>K/(L QEL (s$pplier-i*" "7 C(E/TE T/'LE pro*$cts ( pro*$ct-i* n$%eric(10" s$pplier-i* n$%eric(10"
CO#ST(/>#T N-s$pplier 4O(E>B# QEL (s$pplier-i*" (E4E(E#CES s$pplier(s$pplier-i*" O# DELETE C/SC/DE "7 9n this example, weGve created a primary +ey on the supplier table called supplier_p0. 9t consists of only one field # the supplierHid field. Then weGve created a foreign +ey called f0_supplier on the products table that references the supplier table based on the supplierHid field. *ecause of the cascade delete, when a record in the supplier table is deleted, all records in the products table will also be deleted that have the same supplierHid value.
!e could also create a foreign +ey (with a cascade delete) with more than one field as in the example below: C(E/TE T/'LE s$pplier ( s$pplier-i* n$%eric(10" not n$ll9 s$pplier-na%e ,archar5(J0" not n$ll9 contact-na%e ,archar5(J0"9 CO#ST(/>#T s$pplier-pN P(>K/(L QEL (s$pplier-i*9 s$pplier-na%e" "7 C(E/TE T/'LE pro*$cts ( pro*$ct-i* n$%eric(10" not n$ll9 s$pplier-i* n$%eric(10" not n$ll9 s$pplier-na%e ,archar5(J0" not n$ll9 CO#ST(/>#T N-s$pplier-co%p 4O(E>B# QEL (s$pplier-i*9 s$pplier-na%e" (E4E(E#CES s$pplier(s$pplier-i*9 s$pplier-na%e" O# DELETE C/SC/DE "7 9n this example, our foreign +ey called f0_forei.n_comp references the supplier table based on two fields # the supplierHid and supplierHname fields.
The cascade delete on the foreign +ey called f0_forei.n_comp causes all corresponding records in the products table to be cascade deleted when a record in the supplier table is deleted, based on supplierHid and supplierHname.
4or exa%ple: /LTE( T/'LE pro*$cts a** CO#ST(/>#T N-s$pplier 4O(E>B# QEL (s$pplier-i*" (E4E(E#CES s$pplier(s$pplier-i*" O# DELETE C/SC/DE7 9n this example, weGve created a foreign +ey (with a cascade delete) called f0_supplier that references the supplier table based on the supplierHid field.
!e could also create a foreign +ey (with a cascade delete) with more than one field as in the example below: /LTE( T/'LE pro*$cts a** CO#ST(/>#T N-s$pplier 4O(E>B# QEL (s$pplier-i*9 s$pplier-na%e" (E4E(E#CES s$pplier(s$pplier-i*9 s$pplier-na%e" O# DELETE C/SC/DE7
4or exa%ple:
C(E/TE T/'LE s$pplier ( s$pplier-i* n$%eric(10" not n$ll9 s$pplier-na%e ,archar5(J0" not n$ll9 contact-na%e ,archar5(J0"9 CO#ST(/>#T s$pplier-pN P(>K/(L QEL (s$pplier-i*" "7 C(E/TE T/'LE pro*$cts ( pro*$ct-i* n$%eric(10" not n$ll9 s$pplier-i* n$%eric(10"9 CO#ST(/>#T N-s$pplier 4O(E>B# QEL (s$pplier-i*" (E4E(E#CES s$pplier(s$pplier-i*" O# DELETE SET #1LL "7 9n this example, weGve created a primary +ey on the supplier table called supplier_p0. 9t consists of only one field # the supplierHid field. Then weGve created a foreign +ey called f0_supplier on the products table that references the supplier table based on the supplierHid field. *ecause of the set null on delete, when a record in the supplier table is deleted, all corresponding records in the products table will have the supplierHid values set to null.
!e could also create a foreign +ey Eset null on deleteE with more than one field as in the example below: C(E/TE T/'LE s$pplier ( s$pplier-i* n$%eric(10" not n$ll9 s$pplier-na%e ,archar5(J0" not n$ll9 contact-na%e ,archar5(J0"9 CO#ST(/>#T s$pplier-pN P(>K/(L QEL (s$pplier-i*9 s$pplier-na%e" "7 C(E/TE T/'LE pro*$cts ( pro*$ct-i* n$%eric(10" s$pplier-i* n$%eric(10"9
not n$ll9
s$pplier-na%e ,archar5(J0"9 CO#ST(/>#T N-s$pplier-co%p 4O(E>B# QEL (s$pplier-i*9 s$pplier-na%e" (E4E(E#CES s$pplier(s$pplier-i*9 s$pplier-na%e" O# DELETE SET #1LL "7 9n this example, our foreign +ey called f0_forei.n_comp references the supplier table based on two fields # the supplierHid and supplierHname fields. The delete on the foreign +ey called f0_forei.n_comp causes all corresponding records in the products table to have the supplierHid and supplierHname fields set to null when a record in the supplier table is deleted, based on supplierHid and supplierHname.
4or exa%ple: /LTE( T/'LE pro*$cts a** CO#ST(/>#T N-s$pplier 4O(E>B# QEL (s$pplier-i*" (E4E(E#CES s$pplier(s$pplier-i*" O# DELETE SET #1LL7 9n this example, weGve created a foreign +ey Ewith a set null on deleteE called f0_supplier that references the supplier table based on the supplierHid field.
!e could also create a foreign +ey Ewith a set null on deleteE with more than one field as in the example below: /LTE( T/'LE pro*$cts a** CO#ST(/>#T N-s$pplier 4O(E>B# QEL (s$pplier-i*9 s$pplier-na%e" (E4E(E#CES s$pplier(s$pplier-i*9 s$pplier-na%e" O# DELETE SET #1LL7
4or exa%ple: 9f you had created a foreign +ey as follows: C(E/TE T/'LE s$pplier ( s$pplier-i* n$%eric(10" not n$ll9 s$pplier-na%e ,archar5(J0" not n$ll9 contact-na%e ,archar5(J0"9 CO#ST(/>#T s$pplier-pN P(>K/(L QEL (s$pplier-i*" "7 C(E/TE T/'LE pro*$cts ( pro*$ct-i* n$%eric(10" s$pplier-i* n$%eric(10"
CO#ST(/>#T N-s$pplier 4O(E>B# QEL (s$pplier-i*" (E4E(E#CES s$pplier(s$pplier-i*" "7 9n this example, weGve created a primary +ey on the supplier table called supplier_p0. 9t consists of only one field # the supplierHid field. Then weGve created a foreign +ey called f0_supplier on the products table that references the supplier table based on the supplierHid field.
9f we then wanted to drop the foreign +ey called f+Hsupplier, we could execute the following command: /LTE( T/'LE pro*$cts *rop CO#ST(/>#T N-s$pplier7
4or exa%ple: 9f you had created a foreign +ey as follows: C(E/TE T/'LE s$pplier ( s$pplier-i* n$%eric(10" not n$ll9 s$pplier-na%e ,archar5(J0" not n$ll9 contact-na%e ,archar5(J0"9 CO#ST(/>#T s$pplier-pN P(>K/(L QEL (s$pplier-i*"
"7 C(E/TE T/'LE pro*$cts ( pro*$ct-i* n$%eric(10" not n$ll9 s$pplier-i* n$%eric(10" not n$ll9 CO#ST(/>#T N-s$pplier 4O(E>B# QEL (s$pplier-i*" (E4E(E#CES s$pplier(s$pplier-i*" "7 9n this example, weGve created a primary +ey on the supplier table called supplier_p0. 9t consists of only one field # the supplierHid field. Then weGve created a foreign +ey called f0_supplier on the products table that references the supplier table based on the supplierHid field.
9f we then wanted to disable the foreign +ey called f+Hsupplier, we could execute the following command: /LTE( T/'LE pro*$cts *isa!le CO#ST(/>#T N-s$pplier7
C(E/TE T/'LE s$pplier ( s$pplier-i* n$%eric(10" not n$ll9 s$pplier-na%e ,archar5(J0" not n$ll9 contact-na%e ,archar5(J0"9 CO#ST(/>#T s$pplier-pN P(>K/(L QEL (s$pplier-i*" "7 C(E/TE T/'LE pro*$cts ( pro*$ct-i* n$%eric(10" not n$ll9 s$pplier-i* n$%eric(10" not n$ll9 CO#ST(/>#T N-s$pplier 4O(E>B# QEL (s$pplier-i*" (E4E(E#CES s$pplier(s$pplier-i*" "7 9n this example, weGve created a primary +ey on the supplier table called supplier_p0. 9t consists of only one field # the supplierHid field. Then weGve created a foreign +ey called f0_supplier on the products table that references the supplier table based on the supplierHid field.
9f the foreign +ey had been *isa!le* and we wanted to enable it, we could execute the following command: /LTE( T/'LE pro*$cts ena!le CO#ST(/>#T N-s$pplier7
9n Oracle, a uniIue constraint can not contain more than /2 columns. 3 uniIue constraint can be defined in either a 5AB3TB T3*&B statement or an 3&TBA T3*&B statement.
Oracle does not permit you to create both a primary +ey and uniIue constraint with the same columns.
col$%n5 *atatype n$ll/not n$ll9 <<< CO#ST(/>#T constraint-na%e 1#>Q1E (col$%n19 col$%n59 < col$%n-n" "7
4or exa%ple: C(E/TE T/'LE s$pplier ( s$pplier-i* n$%eric(10" not n$ll9 s$pplier-na%e ,archar5(J0" not n$ll9 contact-na%e ,archar5(J0"9 CO#ST(/>#T s$pplier-$niM$e 1#>Q1E (s$pplier-i*" "7 9n this example, weGve created a uniIue constraint on the supplier table called supplierHuniIue. 9t consists of only one field # the supplierHid field.
!e could also create a uniIue constraint with more than one field as in the example below: C(E/TE T/'LE s$pplier ( s$pplier-i* n$%eric(10" not n$ll9 s$pplier-na%e ,archar5(J0" not n$ll9 contact-na%e ,archar5(J0"9 CO#ST(/>#T s$pplier-$niM$e 1#>Q1E (s$pplier-i*9 s$pplier-na%e" "7
/LTE( T/'LE ta!le-na%e a** CO#ST(/>#T constraint-na%e 1#>Q1E (col$%n19 col$%n59 <<< col$%n-n"7
4or exa%ple: /LTE( T/'LE s$pplier a** CO#ST(/>#T s$pplier-$niM$e 1#>Q1E (s$pplier-i*"7 9n this example, weGve created a uniIue constraint on the existing supplier table called supplierHuniIue. 9t consists of the field called supplierHid.
!e could also create a uniIue constraint with more than one field as in the example below: /LTE( T/'LE s$pplier a** CO#ST(/>#T s$pplier-$niM$e 1#>Q1E (s$pplier-i*9 s$pplier-na%e"7
9n this example, weGre dropping a uniIue constraint on the supplier table called supplierHuniIue.
4or exa%ple: /LTE( T/'LE s$pplier *isa!le CO#ST(/>#T s$pplier-$niM$e7 9n this example, weGre disabling a uniIue constraint on the supplier table called supplierHuniIue.
4or exa%ple:
/LTE( T/'LE s$pplier ena!le CO#ST(/>#T s$pplier-$niM$e7 9n this example, weGre enabling a uniIue constraint on the supplier table called supplierHuniIue.
3 chec+ constraint can %OT be defined on a +>EC. The chec+ constraint defined on a table must refer to only columns in that table. 9t can not refer to columns in other tables. 3 chec+ constraint can %OT include a S1'Q1E(L.
3 chec+ constraint can be defined in either a 5AB3TB T3*&B statement or an 3&TBA T3*&B statement.
The 69$3*&B +eyword is optional. 9f you create a chec+ constraint using the 69$3*&B +eyword, the constraint will be created, but the condition will not be enforced.
4or exa%ple: C(E/TE T/'LE s$ppliers ( s$pplier-i* n$%eric(6"9 s$pplier-na%e ,archar5(J0"9 CO#ST(/>#T checN-s$pplier-i* CAECQ (s$pplier-i* 'ETCEE# 100 an* 9999" "7 9n this first example, weGve created a chec+ constraint on the suppliers table called chec+HsupplierHid. This constraint ensures that the supplierHid field contains values between . and 7777.
C(E/TE T/'LE s$ppliers ( s$pplier-i* n$%eric(6"9 s$pplier-na%e ,archar5(J0"9 CO#ST(/>#T checN-s$pplier-na%e CAECQ (s$pplier-na%e 2 $pper(s$pplier-na%e"" "7 9n this second example, weGve created a chec+ constraint called chec+HsupplierHname. This constraint ensures that the supplierHname column always contains uppercase characters.
/LTE( T/'LE ta!le-na%e a** CO#ST(/>#T constraint-na%e CAECQ (col$%n-na%e con*ition" .D>S/'LE07 The 69$3*&B +eyword is optional. 9f you create a chec+ constraint using the 69$3*&B +eyword, the constraint will be created, but the condition will not be enforced.
4or exa%ple: /LTE( T/'LE s$ppliers a** CO#ST(/>#T checN-s$pplier-na%e CAECQ (s$pplier-na%e ># (=>'K=9 =Kicroso t=9 =#+>D>/=""7 9n this example, weGve created a chec+ constraint on the existing suppliers table called chec+HsupplierHname. 9t ensures that the supplierHname field only contains the following values: 9*M, Microsoft, or %(9693.
4or exa%ple: /LTE( T/'LE s$ppliers *rop CO#ST(/>#T checN-s$pplier-i*7 9n this example, weGre dropping a chec+ constraint on the suppliers table called chec+HsupplierHid.
4or exa%ple: /LTE( T/'LE s$ppliers ena!le CO#ST(/>#T checN-s$pplier-i*7 9n this example, weGre enabling a chec+ constraint on the suppliers table called chec+HsupplierHid.
4or exa%ple: /LTE( T/'LE s$ppliers *isa!le CO#ST(/>#T checN-s$pplier-i*7 9n this example, weGre disabling a chec+ constraint on the suppliers table called chec+HsupplierHid.
Oracle/PLSQL: >n*exes
Chat is an >n*exW
3n in*ex is a performance#tuning method of allowing faster retrieval of records. 3n index creates an entry for each value that appears in the indexed columns. *y default, Oracle creates *#tree indexes.
Create an >n*ex
The syntax for creating a index is: C(E/TE .1#>Q1E0 >#DEO in*ex-na%e O# ta!le-na%e (col$%n19 col$%n59 < col$%n-n" . COKP1TE ST/T>ST>CS 07 >%9D>B indicates that the combination of values in the indexed columns must be uniIue. 5OM->TB $T3T9$T95$ tells Oracle to collect statistics during the creation of the index. The statistics are then used by the optimizer to choose a Eplan of executionE when $D& statements are executed.
4or exa%ple: C(E/TE >#DEO s$pplier-i*x O# s$pplier (s$pplier-na%e"7 9n this example, weGve created an index on the supplier table called supplierHidx. 9t consists of only one field # the supplierHname field.
!e could also create an index with more than one field as in the example below: C(E/TE >#DEO s$pplier-i*x O# s$pplier (s$pplier-na%e9 city"7
!e could also choose to collect statistics upon creation of the index as follows: C(E/TE >#DEO s$pplier-i*x O# s$pplier (s$pplier-na%e9 city" COKP1TE ST/T>ST>CS7
4or exa%ple: C(E/TE >#DEO s$pplier-i*x O# s$pplier (1PPE((s$pplier-na%e""7 9n this example, weGve created an index based on the uppercase evaluation of the supplier_name field.
=owever, to be sure that the Oracle optimizer uses this index when executing your $D& statements, be sure that >--BA(supplierHname) does not evaluate to a %>&& value. To ensure this, add 1PPE((s$pplier-na%e" >S #OT #1LL to your !=BAB clause as follows: SELECT s$pplier-i*9 s$pplier-na%e9 1PPE((s$pplier-na%e" 4(OK s$pplier CAE(E 1PPE((s$pplier-na%e" >S #OT #1LL O(DE( 'L 1PPE((s$pplier-na%e"7
(ena%e an >n*ex
The syntax for renaming an index is: /LTE( >#DEO in*ex-na%e (E#/KE TO ne)-in*ex-na%e7
4or exa%ple: /LTE( >#DEO s$pplier-i*x (E#/KE TO s$pplier-in*ex-na%e7 9n this example, weGre renaming the index called supplier_idx to supplier_index_name.
4or exa%ple: /LTE( >#DEO s$pplier-i*x (E'1>LD COKP1TE ST/T>ST>CS7 9n this example, weGre collecting statistics for the index called supplierHidx.
Drop an >n*ex
The syntax for dropping an index is: D(OP >#DEO in*ex-na%e7
4or exa%ple: D(OP >#DEO s$pplier-i*x7 9n this example, weGre dropping an index called supplierHidx.
9n Oracle, you can create your own functions. The syntax for a function is: C(E/TE .O( (EPL/CE0 41#CT>O# $nction-na%e . (para%eter .9para%eter0" 0 (ET1(# ret$rn-*atatype >S 3 /S .*eclaration-section0 'EB># exec$ta!le-section .EOCEPT>O# exception-section0 E#D . $nction-na%e07
!hen you create a procedure or function, you may define parameters. There are three types of parameters that can be declared: .. 2. /. ># # The parameter can be referenced by the procedure or function. The value of the parameter can not be overwritten by the procedure or function. O1T # The parameter can not be referenced by the procedure or function, but the value of the parameter can be overwritten by the procedure or function. ># O1T # The parameter can be referenced by the procedure or function and the value of the parameter can be overwritten by the procedure or function.
The following is a simple example of a function: C(E/TE O( (EPL/CE 4$nction 4in*Co$rse ( na%e-in ># ,archar5 " (ET1(# n$%!er >S cn$%!er n$%!er7 c$rsor c1 is select co$rse-n$%!er
ro% co$rses-t!l )here co$rse-na%e 2 na%e-in7 'EB># open c17 etch c1 into cn$%!er7 i c1Snot o$n* then cn$%!er :2 99997 en* i 7 close c17 (ET1(# cn$%!er7 EOCEPT>O# CAE# OTAE(S TAE# raise-application-error(@500019=/n error )as enco$ntere* @ =33SQLCODE33= @E((O(@ =33SQLE((K"7 E#D7 This function is called "ind5ourse. 9t has one parameter called name_in and it returns a number. The function will return the course number if it finds a match based on course name. Otherwise, it returns a 77777.
Fou could then reference your new function in an $D& statement as follows: select co$rse-na%e9 4in*Co$rse(co$rse-na%e" as co$rse-i* ro% co$rses )here s$!&ect 2 =Kathe%atics=7
The syntax for a procedure is: C(E/TE .O( (EPL/CE0 P(OCED1(E proce*$re-na%e . (para%eter .9para%eter0" 0 >S .*eclaration-section0 'EB># exec$ta!le-section .EOCEPT>O# exception-section0 E#D .proce*$re-na%e07
!hen you create a procedure or function, you may define parameters. There are three types of parameters that can be declared: .. 2. /. ># # The parameter can be referenced by the procedure or function. The value of the parameter can not be overwritten by the procedure or function. O1T # The parameter can not be referenced by the procedure or function, but the value of the parameter can be overwritten by the procedure or function. ># O1T # The parameter can be referenced by the procedure or function and the value of the parameter can be overwritten by the procedure or function.
The following is a simple example of a procedure: C(E/TE O( (EPL/CE Proce*$re 1p*ateCo$rse ( na%e-in ># ,archar5 " >S cn$%!er n$%!er7 c$rsor c1 is select co$rse-n$%!er ro% co$rses-t!l )here co$rse-na%e 2 na%e-in7 'EB>#
open c17 etch c1 into cn$%!er7 i c1Snot o$n* then cn$%!er :2 99997 en* i 7 insert into st$*ent-co$rses ( co$rse-na%e9 co$rse-n$%!er" ,al$es ( na%e-in9 cn$%!er "7 co%%it7 close c17 EOCEPT>O# CAE# OTAE(S TAE# raise-application-error(@500019=/n error )as enco$ntere* @ =33SQLCODE33= @E((O(@ =33SQLE((K"7 E#D7 This procedure is called >pdate5ourse. 9t has one parameter called name_in. The procedure will loo+up the courseHnumber based on course name. 9f it does not find a match, it defaults the course number to 77777. 9t then inserts a new record into the studentHcourses table.
'E4O(E 1PD/TE Trigger /4TE( 1PD/TE Trigger 6elete Triggers: 'E4O(E DELETE Trigger /4TE( DELETE Trigger 6rop Triggers: Drop a Trigger 6isableOBnable Triggers: Disa!le a Trigger Disa!le all Triggers on a ta!le Ena!le a Trigger Ena!le all Triggers on a ta!le
. 4O( E/CA (OC 0 DECL/(E @@ ,aria!le *eclarations 'EB># @@ trigger co*e EOCEPT>O# CAE# <<< @@ exception han*ling E#D7 tri..er_name is the name of the trigger to create. (estrictions:
Fou can not create a *B"OAB trigger on a view. Fou can update the :%B! values. Fou can not update the :O&6 values.
4or exa%ple: 9f you had a table created as follows: C(E/TE T/'LE or*ers ( or*er-i* M$antity cost-per-ite% total-cost create-*ate create*-!y "7
C(E/TE O( (EPL/CE T(>BBE( or*ers-!e ore-insert 'E4O(E >#SE(T O# or*ers 4O( E/CA (OC DECL/(E ,-$serna%e ,archar5(10"7 'EB># @@ 4in* $serna%e o person per or%ing >#SE(T into ta!le SELECT $ser >#TO ,-$serna%e 4(OK *$al7 @@ 1p*ate create-*ate iel* to c$rrent syste% *ate :ne)<create-*ate :2 sys*ate7 @@ 1p*ate create*-!y iel* to the $serna%e o the person per or%ing the >#SE(T :ne)<create*-!y :2 ,-$serna%e7 E#D7
'EB># @@ trigger co*e EOCEPT>O# CAE# <<< @@ exception han*ling E#D7 tri..er_name is the name of the trigger to create. (estrictions:
Fou can not create an 3"TBA trigger on a view. Fou can not update the :%B! values. Fou can not update the :O&6 values.
4or exa%ple: 9f you had a table created as follows: C(E/TE T/'LE or*ers ( or*er-i* n$%!er(J"9 M$antity n$%!er(6"9 cost-per-ite% n$%!er(;95"9 total-cost n$%!er(895" "7
!e could then create an 3"TBA 9%$BAT trigger as follows: C(E/TE O( (EPL/CE T(>BBE( or*ers-a ter-insert /4TE( >#SE(T O# or*ers 4O( E/CA (OC
DECL/(E ,-$serna%e ,archar5(10"7 'EB># @@ 4in* $serna%e o person per or%ing the >#SE(T into the ta!le SELECT $ser >#TO ,-$serna%e 4(OK *$al7 @@ >nsert recor* into a$*it ta!le >#SE(T >#TO or*ers-a$*it ( or*er-i*9 M$antity9 cost-per-ite%9 total-cost9 $serna%e " +/L1ES ( :ne)<or*er-i*9 :ne)<M$antity9 :ne)<cost-per-ite%9 :ne)<total-cost9 ,-$serna%e "7 E#D7
@@ ,aria!le *eclarations 'EB># @@ trigger co*e EOCEPT>O# CAE# <<< @@ exception han*ling E#D7 tri..er_name is the name of the trigger to create. (estrictions:
Fou can not create a *B"OAB trigger on a view. Fou can update the :%B! values. Fou can not update the :O&6 values.
4or exa%ple: 9f you had a table created as follows: C(E/TE T/'LE or*ers ( or*er-i* M$antity cost-per-ite% total-cost $p*ate*-*ate $p*ate*-!y "7
C(E/TE O( (EPL/CE T(>BBE( or*ers-!e ore-$p*ate 'E4O(E 1PD/TE O# or*ers 4O( E/CA (OC DECL/(E ,-$serna%e ,archar5(10"7 'EB># @@ 4in* $serna%e o person per or%ing 1PD/TE on the ta!le SELECT $ser >#TO ,-$serna%e 4(OK *$al7 @@ 1p*ate $p*ate*-*ate iel* to c$rrent syste% *ate :ne)<$p*ate*-*ate :2 sys*ate7 @@ 1p*ate $p*ate*-!y iel* to the $serna%e o the person per or%ing the 1PD/TE :ne)<$p*ate*-!y :2 ,-$serna%e7 E#D7
'EB># @@ trigger co*e EOCEPT>O# CAE# <<< @@ exception han*ling E#D7 tri..er_name is the name of the trigger to create. (estrictions:
Fou can not create an 3"TBA trigger on a view. Fou can not update the :%B! values. Fou can not update the :O&6 values.
4or exa%ple: 9f you had a table created as follows: C(E/TE T/'LE or*ers ( or*er-i* M$antity cost-per-ite% total-cost "7
!e could then create an 3"TBA >-63TB trigger as follows: C(E/TE O( (EPL/CE T(>BBE( or*ers-a ter-$p*ate /4TE( 1PD/TE O# or*ers 4O( E/CA (OC
DECL/(E ,-$serna%e ,archar5(10"7 'EB># @@ 4in* $serna%e o person per or%ing 1PD/TE into ta!le SELECT $ser >#TO ,-$serna%e 4(OK *$al7 @@ >nsert recor* into a$*it ta!le >#SE(T >#TO or*ers-a$*it ( or*er-i*9 M$antity-!e ore9 M$antity-a ter9 $serna%e " +/L1ES ( :ne)<or*er-i*9 :ol*<M$antity9 :ne)<M$antity9 ,-$serna%e "7 E#D7
'EB># @@ trigger co*e EOCEPT>O# CAE# <<< @@ exception han*ling E#D7 tri..er_name is the name of the trigger to create. (estrictions:
Fou can not create a *B"OAB trigger on a view. Fou can update the :%B! values. Fou can not update the :O&6 values.
4or exa%ple: 9f you had a table created as follows: C(E/TE T/'LE or*ers ( or*er-i* M$antity cost-per-ite% total-cost "7
!e could then create a *B"OAB 6B&BTB trigger as follows: C(E/TE O( (EPL/CE T(>BBE( or*ers-!e ore-*elete 'E4O(E DELETE O# or*ers 4O( E/CA (OC
DECL/(E ,-$serna%e ,archar5(10"7 'EB># @@ 4in* $serna%e o person per or%ing the DELETE on the ta!le SELECT $ser >#TO ,-$serna%e 4(OK *$al7 @@ >nsert recor* into a$*it ta!le >#SE(T >#TO or*ers-a$*it ( or*er-i*9 M$antity9 cost-per-ite%9 total-cost9 *elete-*ate9 *elete*-!y " +/L1ES ( :ol*<or*er-i*9 :ol*<M$antity9 :ol*<cost-per-ite%9 :ol*<total-cost9 sys*ate9 ,-$serna%e "7 E#D7
O# table_name . 4O( E/CA (OC 0 DECL/(E @@ ,aria!le *eclarations 'EB># @@ trigger co*e EOCEPT>O# CAE# <<< @@ exception han*ling E#D7 tri..er_name is the name of the trigger to create. (estrictions:
Fou can not create an 3"TBA trigger on a view. Fou can not update the :%B! values. Fou can not update the :O&6 values.
4or exa%ple: 9f you had a table created as follows: C(E/TE T/'LE or*ers ( or*er-i* M$antity cost-per-ite% total-cost "7
C(E/TE O( (EPL/CE T(>BBE( or*ers-a ter-*elete /4TE( DELETE O# or*ers 4O( E/CA (OC DECL/(E ,-$serna%e ,archar5(10"7 'EB># @@ 4in* $serna%e o person per or%ing the DELETE on the ta!le SELECT $ser >#TO ,-$serna%e 4(OK *$al7 @@ >nsert recor* into a$*it ta!le >#SE(T >#TO or*ers-a$*it ( or*er-i*9 M$antity9 cost-per-ite%9 total-cost9 *elete-*ate9 *elete*-!y" +/L1ES ( :ol*<or*er-i*9 :ol*<M$antity9 :ol*<cost-per-ite%9 :ol*<total-cost9 sys*ate9 ,-$serna%e "7 E#D7
4or exa%ple: 9f you had a trigger called ordersHbeforeHinsert, you could drop it with the following command: D(OP T(>BBE( or*ers-!e ore-insert7
4or exa%ple: 9f you had a trigger called ordersHbeforeHinsert, you could disable it with the following command: /LTE( T(>BBE( or*ers-!e ore-insert D>S/'LE7
4or exa%ple: 9f you had a table called orders and you wanted to disable all triggers on this table, you could execute the following command: /LTE( T/'LE or*ers D>S/'LE /LL T(>BBE(S7
4or exa%ple: 9f you had a trigger called ordersHbeforeHinsert, you could enable it with the following command: /LTE( T(>BBE( or*ers-!e ore-insert E#/'LE7
4or exa%ple: 9f you had a table called orders and you wanted to enable all triggers on this table, you could execute the following command:
Explanation Fou tried to execute an 9%$BAT or >-63TB statement that has created a duplicate value in a field restricted by a uniIue index. Fou were waiting for a resource and you timed out. The remote portion of a transaction has rolled bac+. Fou tried to reference a cursor that does not yet exist. This may have happened because youGve executed a "BT5= cursor or 5&O$B cursor before O-B%ing the cursor. Fou tried to execute a call to Oracle before logging in. Fou tried to log into Oracle with an invalid usernameOpassword combination. Fou tried one of the following: .. Fou executed a $B&B5T 9%TO statement and no rows were returned. 2. Fou referenced an uninitialized row in a table. /. Fou read past the end of file with the >T&H"9&B pac+age.
T9MBO>THO%HAB$O>A5B
OA3#
2. 8. .
TOOHM3%FHAO!$
OA3# .'22
statement and more than one row was returned. PBAOH69(96B 9%(3&96H%>M*BA OA3# .'18 OA3# .122 Fou tried to divide a number by zero. Fou tried to execute an $D& statement that tried to convert a string to a number, but it was unsuccessful. Fou ran out of memory or memory was corrupted. This is a generic E5ontact Oracle supportE message because an internal problem was encountered. Fou tried to perform an operation and there was a error on a conversion, truncation, or invalid constraining of numeric or character data. Fou tried to open a cursor that is already open.
$TOA3)BHBAAOA -AO)A3MHBAAOA
OA3# 82 OA3# 82 .
(3&>BHBAAOA
OA3# 82 2
5>A$OAH3&AB36FHO-B%
OA3# 82..
The syntax for the %amed $ystem Bxception in a procedure is: C(E/TE .O( (EPL/CE0 P(OCED1(E proce*$re-na%e . (para%eter .9para%eter0" 0 >S .*eclaration-section0 'EB># exec$ta!le-section EOCEPT>O# CAE# exception-na%e1 TAE# .state%ents0
CAE# exception-na%e5 TAE# .state%ents0 CAE# exception-na%e-n TAE# .state%ents0 CAE# OTAE(S TAE# .state%ents0 E#D .proce*$re-na%e07
The syntax for the %amed $ystem Bxception in a function is: C(E/TE .O( (EPL/CE0 41#CT>O# $nction-na%e . (para%eter .9para%eter0" 0 (ET1(# ret$rn-*atatype >S 3 /S .*eclaration-section0 'EB># exec$ta!le-section EOCEPT>O# CAE# exception-na%e1 TAE# .state%ents0 CAE# exception-na%e5 TAE# .state%ents0 CAE# exception-na%e-n TAE# .state%ents0 CAE# OTAE(S TAE# .state%ents0
E#D . $nction-na%e07
=ere is an example of a procedure that uses a %amed $ystem Bxception: C(E/TE O( (EPL/CE P(OCED1(E a**-ne)-s$pplier (s$pplier-i*-in ># #1K'E(9 s$pplier-na%e-in ># +/(CA/(5" >S 'EB># >#SE(T >#TO s$ppliers (s$pplier-i*9 s$pplier-na%e " +/L1ES ( s$pplier-i*-in9 s$pplier-na%e-in "7 EOCEPT>O# CAE# D1P-+/L-O#->#DEO TAE# raise-application-error (@500019=Lo$ ha,e trie* to insert a *$plicate s$pplier-i*<="7 CAE# OTAE(S TAE# raise-application-error (@500059=/n error has occ$rre* inserting a s$pplier<="7 E#D7 9n this example, we are trapping the %amed $ystem Bxception called D1P-+/L-O#->#DEO. !e are also using the CAE# OTAE(S cla$se to trap all remaining exceptions.
C(E/TE .O( (EPL/CE0 P(OCED1(E proce*$re-na%e . (para%eter .9para%eter0" 0 >S .*eclaration-section0 exception-na%e EOCEPT>O#7 'EB># exec$ta!le-section (/>SE exception-na%e 7 EOCEPT>O# CAE# exception-na%e TAE# .state%ents0 CAE# OTAE(S TAE# .state%ents0 E#D .proce*$re-na%e07
The syntax for the %amed -rogrammer#6efined Bxception in a function is: C(E/TE .O( (EPL/CE0 41#CT>O# $nction-na%e . (para%eter .9para%eter0" 0 (ET1(# ret$rn-*atatype >S 3 /S .*eclaration-section0 exception-na%e EOCEPT>O#7 'EB># exec$ta!le-section
(/>SE exception-na%e 7 EOCEPT>O# CAE# exception-na%e TAE# .state%ents0 CAE# OTAE(S TAE# .state%ents0 E#D . $nction-na%e07
=ere is an example of a procedure that uses a %amed -rogrammer#6efined Bxception: C(E/TE O( (EPL/CE P(OCED1(E a**-ne)-or*er (or*er-i*-in ># #1K'E(9 sales-in ># #1K'E(" >S no-sales EOCEPT>O#7 'EB># >4 sales-in 2 0 TAE# (/>SE no-sales7 ELSE >#SE(T >#TO or*ers (or*er-i*9 total-sales " +/L1ES ( or*er-i*-in9 sales-in "7 E#D >47 EOCEPT>O# CAE# no-sales TAE# raise-application-error (@500019=Lo$ %$st ha,e sales in or*er to s$!%it the or*er<="7 CAE# OTAE(S TAE# raise-application-error (@500059=/n error has occ$rre* inserting an or*er<="7
E#D7 9n this example, we have declared a %amed -rogrammer#6efined Bxception called no-sales in our declaration statement with the following code: no-sales EOCEPT>O#7
!eGve then raised the exception in the executable section of the code: >4 sales-in 2 0 TAE# (/>SE no-sales7 %ow if the sales_in variable contains a zero, our code will @ump directly to the %amed -rogrammer#6efined Bxception called no-sales.
"inally, we tell our procedure what to do when the no-sales exception is encountered by including code in the !=B% clause: CAE# no-sales TAE# raise-application-error (@500019=Lo$ %$st ha,e sales in or*er to s$!%it the or*er<="7
!e are also using the CAE# OTAE(S cla$se to trap all remaining exceptions: CAE# OTAE(S TAE# raise-application-error (@500059=/n error has occ$rre* inserting an or*er<="7
The !=B% OT=BA$ clause is used to trap all remaining exceptions that have not been handled by your #a%e* Syste% Exceptions and #a%e* Progra%%er@De ine* Exceptions. The syntax for the !=B% OT=BA$ clause in a procedure is: C(E/TE .O( (EPL/CE0 P(OCED1(E proce*$re-na%e . (para%eter .9para%eter0" 0 >S .*eclaration-section0 'EB># exec$ta!le-section EOCEPT>O# CAE# exception-na%e1 TAE# .state%ents0 CAE# exception-na%e5 TAE# .state%ents0 CAE# exception-na%e-n TAE# .state%ents0 CAE# OTAE(S TAE# .state%ents0 E#D .proce*$re-na%e07
The syntax for the !=B% OT=BA$ clause in a function is: C(E/TE .O( (EPL/CE0 41#CT>O# $nction-na%e . (para%eter .9para%eter0" 0 (ET1(# ret$rn-*atatype >S 3 /S .*eclaration-section0
'EB># exec$ta!le-section EOCEPT>O# CAE# exception-na%e1 TAE# .state%ents0 CAE# exception-na%e5 TAE# .state%ents0 CAE# exception-na%e-n TAE# .state%ents0 CAE# OTAE(S TAE# .state%ents0 E#D . $nction-na%e07
=ere is an example of a procedure that uses a !=B% OT=BA$ clause: C(E/TE O( (EPL/CE P(OCED1(E a**-ne)-or*er (or*er-i*-in ># #1K'E(9 sales-in ># #1K'E(" >S no-sales EOCEPT>O#7 'EB># >4 sales-in 2 0 TAE# (/>SE no-sales7 ELSE >#SE(T >#TO or*ers (or*er-i*9 total-sales " +/L1ES ( or*er-i*-in9 sales-in "7 E#D >47
EOCEPT>O# CAE# D1P-+/L-O#->#DEO TAE# raise-application-error (@500019=Lo$ ha,e trie* to insert a *$plicate or*er-i*<="7 CAE# no-sales TAE# raise-application-error (@500019=Lo$ %$st ha,e sales in or*er to s$!%it the or*er<="7 CAE# OTAE(S TAE# raise-application-error (@500059=/n error has occ$rre* inserting an or*er<="7 E#D7 9n this example, if an exception is encountered that is not a D1P-+/L-O#->#DEO or a no-sales, it will be trapped by the !=B% OT=BA$ clause.
Q$estion: 9s there any way to get the OA3 error number (andOor description) for the errors that will fall into OT=BA$J $omething li+e: !=B% OT=BA$ T=B% GBrror number G Q Brr.%umberQ G has happen.G /ns)er: Fes, you can use SQLCODE $nction to retrieve the error number and SQLE((K $nction to retrieve the error message. "or example, you could raise the error as follows: EOCEPT>O# CAE# OTAE(S TAE# raise-application-error(@500019=/n error )as enco$ntere* @ =33SQLCODE33= @E((O(@ =33SQLE((K"7 E#D7
Or you could log the error to a table as follows: EOCEPT>O# CAE# OTAE(S TAE# err-co*e :2 SQLCODE7 err-%sg :2 s$!str(SQLE((K9 19 500"7 >#SE(T >#TO a$*it-ta!le (error-n$%!er9 error-%essage" +/L1ES (err-co*e9 err-%sg"7 E#D7
Fou could use the $D&5O6B function to raise an error as follows: EOCEPT>O# CAE# OTAE(S TAE# raise-application-error(@500019=/n error )as enco$ntere* @ =33SQLCODE33= @E((O(@ =33SQLE((K"7 E#D7
Or you could log the error to a table as follows: EOCEPT>O# CAE# OTAE(S TAE# err-co*e :2 SQLCODE7 err-%sg :2 s$!str(SQLE((K9 19 500"7 >#SE(T >#TO a$*it-ta!le (error-n$%!er9 error-%essage" +/L1ES (err-co*e9 err-%sg"7 E#D7
EOCEPT>O# CAE# exception-na%e1 TAE# .state%ents0 CAE# exception-na%e5 TAE# .state%ents0 CAE# exception-na%e-n TAE# .state%ents0 CAE# OTAE(S TAE# .state%ents0 E#D .proce*$re-na%e07
Fou could use the $D&BAAM function to raise an error as follows: EOCEPT>O# CAE# OTAE(S TAE# raise-application-error(@500019=/n error )as enco$ntere* @ =33SQLCODE33= @E((O(@ =33SQLE((K"7 E#D7
Or you could log the error to a table as follows: EOCEPT>O# CAE# OTAE(S TAE# err-co*e :2 SQLCODE7 err-%sg :2 s$!str(SQLE((K9 19 500"7 >#SE(T >#TO a$*it-ta!le (error-n$%!er9 error-%essage" +/L1ES (err-co*e9 err-%sg"7 E#D7
777 O(/@0091I O(/@00918 O(/@00919 O(/@00950 O(/@0095: O(/@00956 O(/@0095J O(/@0095; O(/@0095I O(/@00958 O(/@009:1 O(/@009:5 O(/@009:: O(/@009:6 O(/@009:J O(/@009:; O(/@009:I O(/@009:8 O(/@009:9 O(/@00965 O(/@0096; O(/@0096I O(/@00968 O(/@009JJ O(/@009JI O(/@009;0 O(/@009;5 O(/@009I1 O(/@009I5 O(/@009IJ O(/@009I9 O(/@00980 O(/@00986 O(/@0098J O(/@0099J
O(/@00900 O(/@00905 O(/@0090: O(/@00906 O(/@0090J O(/@0090; O(/@0090I O(/@00908 O(/@00909 O(/@00910 O(/@00911 O(/@0091:
# ./77
.'
# .'77 O(/@01655 O(/@0165: O(/@01656 O(/@0165J O(/@0165; O(/@0165I O(/@01658 O(/@01659 O(/@016:0 O(/@016:5 O(/@016:: O(/@016:6 O(/@016:J O(/@016:; O(/@016:I O(/@016:8 O(/@016:9 O(/@01660 O(/@01661 O(/@01665 O(/@0166; O(/@01668 O(/@01669 O(/@016J1 O(/@016J5 O(/@016J: O(/@016J6 O(/@016;1 O(/@016;J O(/@016;8 O(/@016I1 O(/@016I;
O(/@01600 O(/@01601 O(/@01605 O(/@0160: O(/@01606 O(/@0160J O(/@0160; O(/@0160I O(/@01608 O(/@0161; O(/@0161I O(/@01618
.2
# .777 O(/@01I19 O(/@01I55 O(/@01I5: O(/@01I56 O(/@01I5I O(/@01I58 O(/@01I6I O(/@01IJ; O(/@01IIJ O(/@01I8J O(/@01I89 O(/@01I90 O(/@01810 O(/@01818 O(/@01850 O(/@01851 O(/@018:0 O(/@018:9 O(/@0186; O(/@0186I O(/@018J8 O(/@018;1 O(/@0191; O(/@01918 O(/@019J0
O(/@01I11
O(/@01II9
O(/@0186:
# 8'77 O(/@055;8 O(/@055I0 O(/@05590 O(/@05591 O(/@05595 O(/@0559: O(/@05598 O(/@056:I O(/@05666 O(/@05669 O(/@0:11: O(/@06000 O(/@060:1 O(/@06088 O(/@06091 O(/@06095 O(/@06098
82
# 7777 O(/@0;J08 O(/@0;J10 O(/@0;J11 O(/@0;J15 O(/@0;J16 O(/@0;J1J O(/@0;JJ0 O(/@0;J;6 O(/@0;JI5 O(/@0;JIJ O(/@08005 O(/@0810:
# .2777 O(/@151J6
Description 3bility to Iuery the table with a select statement. 3bility to add new rows to the table with the insert statement. 3bility to update rows in the table with the update statement. 3bility to delete rows from the table with the delete statement. 3bility to create a constraint that refers to the table. 3bility to change the table definition with the alter table statement. 3bility to create an index on the table with the create index statement.
The syntax for granting privileges on a table is: grant pri,ileges on o!&ect to $ser7 "or example, if you wanted to grant select, insert, update, and delete privileges on a table called suppliers to a user name smith@, you would execute the following statement: grant select9 insert9 $p*ate9 *elete on s$ppliers to s%ith&7 Fou can also use the all +eyword to indicate that you wish all permissions to be granted. "or example: grant all on s$ppliers to s%ith&7 9f you wanted to grant select access on your table to all users, you could grant the privileges to the public +eyword. "or example: grant select on s$ppliers to p$!lic7
The syntax for granting execute privileges on a functionOprocedure is: grant exec$te on o!&ect to $ser7 "or example, if you had a function called "indH(alue and you wanted to grant execute access to the user named smith@, you would execute the following statement: grant exec$te on 4in*-+al$e to s%ith&7 9f you wanted to grant all users the ability to execute this function, you would execute the following: grant exec$te on 4in*-+al$e to p$!lic7
Oracle/PLSQL: (oles
3 role is a set or group of privileges that can be granted to users or another role. This is a great way for database administrators to save time and effort.
%reating a Role
To create a role, you must have 5AB3TB AO&B system privileges. The syntax for creating a role is: C(E/TE (OLE role-na%e . #OT >DE#T>4>ED 3 >DE#T>4>ED E'L pass)or* 3 1S>#B .sche%a<0 pacNage 3 EOTE(#/LLL 3 BLO'/LLL F 7 %ote: 9f both the #OT >DE#T>4>ED and >DE#T>4>ED phrases are omitted in the 5AB3TB AO&B statement, the role will be created as a %OT 96B%T9"9B6 role. The role-na%e phrase is the name of the new role that you are creating. This is how you will refer to the grouping of privileges. The #OT >DE#T>4>ED phrase means that the role is immediately enabled. %o password is reIuired to enable the role. The >DE#T>4>ED phrase means that a user must be authorized by a specified method before the role is enabled. The 'L pass)or* phrase means that a user must supply a password to enable the role. The 1S>#B pacNage phrase means that you are creating an application role # a role that is enabled only by applications using an authorized pac+age. The EOTE(#/LLL phrase means that a user must be authorized by an external service to enable the role. 3n external service can be an operating system or third#party service. The BLO'/LLL phrase means that a user must be authorized by the enterprise directory service to enable the role.
4or exa%ple:
C(E/TE (OLE test-role7 This first example creates a role called testHrole. C(E/TE (OLE test-role >DE#T>4>ED 'L test15:7 This second example creates the same role called testHrole, but now it is password protected with the password of test.2/.
The syntax for granting privileges on a table is: grant pri,ileges on o!&ect to role-na%e
"or example, if you wanted to grant select, insert, update, and delete privileges on a table called suppliers to a role named testHrole, you would execute the following statement: grant select9 insert9 $p*ate9 *elete on s$ppliers to test-role7 Fou can also use the all +eyword to indicate that you wish all permissions to be granted. "or example: grant all on s$ppliers to test-role7
Pri,ilege Bxecute
Description 3bility to compile the functionOprocedure. 3bility to execute the functionOprocedure directly.
The syntax for granting execute privileges on a functionOprocedure is: grant exec$te on o!&ect to role-na%e7 "or example, if you had a function called "indH(alue and you wanted to grant execute access to the role named testHrole, you would execute the following statement: grant exec$te on 4in*-+al$e to test-role7
The syntax to grant a role to a user is: B(/#T role-na%e TO $ser-na%e7 4or exa%ple: B(/#T test-role to s%ith&7 This example would grant the role called testHrole to the user named smith@.
4or exa%ple: SET (OLE test-role >DE#T>4>ED 'L test15:7 This example would enable the role called testHrole with a password of test.2/.
4or exa%ple:
/LTE( 1SE( s%ith& DE4/1LT (OLE test-role7 This example would set the role called testHrole as a 6B"3>&T role for the user named smith@. /LTE( 1SE( s%ith& DE4/1LT (OLE /LL7 This example would set all roles assigned to smith@ as 6B"3>&T. /LTE( 1SE( s%ith& DE4/1LT (OLE /LL EOCEPT test-role7 This example would set all roles assigned to smith@ as 6B"3>&T, except for the role called testHrole.
&ropping a Role
9t is also possible to drop a role. The syntax for dropping a role is: D(OP (OLE role-na%e7
4or exa%ple: D(OP (OLE test-role7 This drop statement would drop the role called test_role that we defined earlier.
/ns)er: To change a userGs password in Oracle, you need to execute the alter user command. The syntax for changing a password is: alter $ser user_name i*enti ie* !y new_password7 user_name is the user whose password you wish to change. new_password is the new password to assign.
4or exa%ple: 9f you wanted to reset the password for a user named s%ith&, and you wanted to set the new password to a$t$%n, you would run the following command: alter $ser s%ith& i*enti ie* !y a$t$%n7
Oracle/PLSQL: Synony%s
3 synony% is an alternative name for ob@ects such as tables, views, seIuences, stored procedures, and other database ob@ects.
4or exa%ple: create p$!lic synony% s$ppliers or app<s$ppliers7 This first example demonstrates how to create a synonym called suppliers. %ow, users of other schemas can reference the table called suppliers without having to prefix the table name with the schema named app. "or example: select ? ro% s$ppliers7
9f this synonym already existed and you wanted to redefine it, you could always use the or replace phrase as follows: create or replace p$!lic synony% s$ppliers or app<s$ppliers7
&ropping a synonym
9t is also possible to drop a synonym. The syntax for dropping a synonym is: *rop .p$!lic0 synony% .sche%a <0 synony%-na%e . orce07 The p$!lic phrase allows you to drop a public synonym. 9f you have specified p$!lic, then you donGt specify a sche%a. The orce phrase will force Oracle to drop the synonym even if it has dependencies. 9t is probably not a good idea to use the orce phrase as it can cause invalidation of Oracle ob@ects.
4or exa%ple: *rop p$!lic synony% s$ppliers7 This drop statement would drop the synonym called suppliers that we defined earlier.
Deter%ine the length o a LO#B iel* (etrie,e the ,al$e o a LO#B iel* $tring 6ata Types: Dealing )ith apostrophes/single M$otes in strings
Test a string or a n$%eric ,al$e Test a string or an alpha!etic ,al$e Test a string or an alphan$%eric ,al$e
Sort a ,archar5 iel* as a n$%eric iel* Extract arith%etic operators ro% a string Parse a string ,al$e an* then ret$rn a s$!string Di erence !et)een an e%pty string an* a n$ll ,al$e
Extract the *irectory path ro% a $ll ile path Extract the ilena%e (incl$*ing s$ ix" ro% a $ll ile path Extract the ilena%e s$ ix ro% a $ll ile path
63TB 6ata Types: >nsert a *ate/ti%e ,al$e into an Oracle ta!le (etrie,e the total elapse* ti%e in %in$tes !et)een t)o *ates 5ursors: C$rsor )ithin a c$rsor Proce*$re that o$tp$ts a *yna%ic PLSQL c$rsor C$rsor )ith ,aria!le in an T># CL/1SET Brrors: O(/@06098 Error /,oi* T*ata not o$n*T error in PLSQL co*e Aetrieve Top, Middle, or *ottom % records of a Iuery: (etrie,e Top # recor*s ro% a M$ery (etrie,e 'otto% # recor*s ro% a M$ery (etrie,e Ki**le # recor*s ro% a M$ery (etrie,e secon* highest ,al$e ro% a ta!le (etrie,e thir* highest ,al$e ro% a ta!le (etrie,e secon* lo)est ,al$e ro% a ta!le
(etrie,e thir* lo)est ,al$e ro% a ta!le $D&-lus: Exec$te an SQL script ile in SQLPl$s Pro%pt $ser or a para%eter ,al$e in SQLPl$s Miscellaneous: 4or%at a n$%!er in Oracle Calc$late the a,erage !et)een t)o *ates (et$rn error %essage )hen ret$rn *atatype is set to #1K'E( (oll!acN !eha,ior an* DDL=s Proce*$re to ret$rn a %onthly perio* Exec$te a $nction that is *e ine* in a pacNage /** *ays to a *ate (sNipping Sat$r*ays an* S$n*ays" (etrie,e Oracle ,ersion in or%ation (etrie,e the na%e o the Oracle instance c$rrently connecte* to (etrie,e pri%ary Ney in or%ation (etrie,e the ,al$e that occ$rs %ost in a col$%n B(O1P 'L Cla$se an* sorting