Está en la página 1de 25

What are the difference between DDL, DML and DCL commands? DDL is Data Definition Language statements.

Some examples: CREATE - to create objects in the database ALTER - alters the structure of the database DROP - delete objects from the database TRUNCATE - remove all records from a table includin! all s"aces allocated for the records are removed CO##ENT - add comments to the data dictionar$ %RANT - !ives user&s access "rivile!es to database RE'O(E - )ithdra) access "rivile!es !iven )ith the %RANT command DML is Data Manipulation Language statements. Some examples: *ELECT - retrieve data from the a database +N*ERT - insert data into a table UPDATE - u"dates e,istin! data )ithin a table DELETE - deletes all records from a table the s"ace for the records remain CALL - call a PL-*.L or /ava sub"ro!ram E0PLA+N PLAN - e,"lain access "ath to data LOC( TA1LE - control concurrenc$

DCL is Data Control Language statements. Some examples: CO##+T - save )or2 done *A'EPO+NT - identif$ a "oint in a transaction to )hich $ou can later roll bac2 ROLL1AC( - restore database to ori!inal since the last CO##+T *ET TRAN*ACT+ON - Chan!e transaction o"tions li2e )hat rollbac2 se!ment to use

3 1ac2 to to" of file ow does one escape special characters when building S!L "ueries? The L+(E 2e$)ord allo)s for strin! searches4 The &5& )ild card character is used to match e,actl$ one character &6& is used to match 7ero or more occurrences of an$ characters4 These characters can be esca"ed in *.L4 E,am"le8 *ELECT name 9RO# em" :;ERE id L+(E &6<56& E*CAPE &<&= Use t)o >uotes for ever$ one dis"la$ed4 E,am"le8 *ELECT &9ran2s&&s Oracle site& 9RO# DUAL=

*ELECT &A &&>uoted&& )ord4& 9RO# DUAL= *ELECT &A &&&&double >uoted&&&& )ord4& 9RO# DUAL= 3 1ac2 to to" of file ow does one eliminate duplicates rows from a table? Choose one of the follo)in! >ueries to identif$ or remove du"licate ro)s from a table leavin! onl$ uni>ue records in the table8 Method #: *.L? DELETE 9RO# table5name A :;ERE RO:+D ? @ A C *ELECT min@ro)idB 9RO# table5name 1 :;ERE A42e$5values D 142e$5valuesB=

Method $: *.L? create table table5nameA as select distinct E from table5nameF= *.L? dro" table5nameF= *.L? rename table5nameA to table5nameF= *.L? -- Remember to recreate all inde,es constraints tri!!ers etc on table444 Method %: &than's to Dennis (urnic') *.L? delete from m$5table tF *.L? )here e,ists @select &,& from m$5table tA *.L? *.L? *.L? )here tA42e$5valueF D tF42e$5valueF and tA42e$5valueA D tF42e$5valueA and tA4ro)id ? tF4ro)idB=

Note8 One can eliminate NGA unnecessar$ o"erations b$ creatin! an inde, on the joined fields in the inner loo" @no need to loo" throu!h the entire table on each "ass b$ a recordB4 This )ill s"eed-u" the deletion "rocess4 Note A8 +f $ou are com"arin! NOT-NULL columns use the N'L function4 Remember that NULL is not e>ual to NULL4 This should not be a "roblem as all 2e$ columns should be NOT NULL b$ definition4 3 1ac2 to to" of file

ow does one generate primar* 'e* +alues for a table? Create $our table )ith a NOT NULL column @sa$ *E.NOB4 This column can no) be "o"ulated )ith uni>ue values8 *.L? UPDATE table5name *ET se>no D RO:NU#= or use a se>uences !enerator8 *.L? CREATE *E.UENCE se>uence5name *TART :+T; F +NCRE#ENT 1H F= *.L? UPDATE table5name *ET se>no D se>uence5name4NE0T'AL= 9inall$ create a uni>ue inde, on this column4 3 1ac2 to to" of file ow does one get the time difference between two date columns? Loo2 at this e,am"le >uer$8 select floor@@@dateF-dateABEAIEJKEJKB-CJKKB LL & ;OUR* & LL floor@@@@dateF-dateABEAIEJKEJKB floor@@@dateF-dateABEAIEJKEJKB-CJKKBECJKKB-JKB LL & #+NUTE* & LL round@@@@dateF-dateABEAIEJKEJKB floor@@@dateF-dateABEAIEJKEJKB-CJKKBECJKK @floor@@@@dateF-dateABEAIEJKEJKB floor@@@dateF-dateABEAIEJKEJKB-CJKKBECJKKB-JKBEJKBBB LL & *EC* & time5difference from 444

+f $ou don&t )ant to !o throu!h the floor and ceilin! math tr$ this method @contributed b$ Eri2 :ileB8 select to5char@to5date@&KK8KK8KK& &;;AI8#+8**&B M @dateF - dateAB &;;AI8#+8**&B time5difference from 444

Note that this >uer$ onl$ uses the time "ortion of the date and i!nores the date itself4 +t )ill thus never return a value bi!!er than AC8NO8NO4 3 1ac2 to to" of file ow does one add a da*,hour,minute,second to a date +alue? The *H*DATE "seudo-column sho)s the current s$stem date and time4 Addin! F to *H*DATE )ill advance the date b$ F da$4 Use fractions to add hours minutes or seconds to the date4 Loo2 at these e,am"les8 *.L? select s$sdate s$sdateMF-AI s$sdate MF-FIIK s$sdate M F-PJIKK from dual=

*H*DATE *H*DATEMF-PJIKK

*H*DATEMF-AI

*H*DATEMF-FIIK

-------------------- -------------------- -------------------- -------------------KC-/ul-AKKA KP8CA8FA KC-/ul-AKKA KO8CA8FA KC-/ul-AKKA KP8CC8FA KC-/ulAKKA KP8CA8FC The follo)in! format is fre>uentl$ used )ith Oracle Re"lication8 select s$sdate NO: s$sdateMCK-@AIEJKEJKB NO:5PLU*5CK5*EC* from dual=

NO:

NO:5PLU*5CK5*EC*

-------------------- -------------------KC-/UL-AKKA FJ8IQ8AC KC-/UL-AKKA FJ8IQ8NC ere are a couple of examples: Description No) Tomoro)- ne,t da$ *even da$s from no) One hour from no) Three hours from no) An half hour from no) *H*DATE *H*DATE M F *H*DATE M Q *H*DATE M F-AI *H*DATE M C-AI *H*DATE M F-IP Date -xpression

FK minutes from no) *H*DATE M FK-FIIK

CK seconds from no) *H*DATE M CK-PJIKK Tomorro) at FA midni!ht Tomorro) at P A# Ne,t #onda$ at FA8KK noon 9irst da$ of ne,t month at FA midni!ht 9irst da$ of the current month TRUNC@*H*DATE M FB TRUNC@*H*DATE M FB M P-AI NE0T5DAH@TRUNC@*H*DATEB &#ONDAH&B M FA-AI TRUNC@LA*T5DAH@*H*DATE B M FB TRUNC@LA*T5DAH@ADD5#ONT;*@*H*DATE -FBBB M F

The ne,t #onda$ TRUNC@LEA*T@NE0T5DAH@s$sdate &&#ONDAH&& B NE0T5DAH@s$sd :ednesda$ or 9rida$ ate &&:EDNE*DAH&&B NE0T5DAH@s$sdate &&9R+DAH&& BBB M @O-AIB at O a4m 3 1ac2 to to" of file ow does one count different data +alues in a column? Use this sim"le >uer$ to count the number of data values in a column8 select m$5table5column count@EB from m$5table

!rou" b$ m$5table5column= A more so"histicated e,am"le444 select de"t sum@ decode@se, &#& F KBB #ALE sum@ decode@se, &9& F KBB 9E#ALE count@decode@se, &#& F &9& FBB TOTAL from m$5em"5table

!rou" b$ de"t= 3 1ac2 to to" of file ow does one count,sum ./0(-S of data +alues in a column? A value , )ill be bet)een values $ and 7 if %REATE*T@, $B D LEA*T@, 7B4 Loo2 at this e,am"le8 select fA sum@decode@!reatest@fF NOB least@fF FKKB F KBB RRan!e JK-FKKR sum@decode@!reatest@fF CKB least@fF NOB F KBB RRan!e CK-NOR

sum@decode@!reatest@fF KB least@fF AOB F KBB RRan!e KK-AOR from m$5table

!rou" b$ fA= 9or e>ual si7e ran!es it mi!ht be easier to calculate it )ith DECODE@TRUNC@value-ran!eB K rate5K F rate5F 444B4 E!4 select ename RNameR sal R*alar$R decode@ trunc@fA-FKKK KB K K4K F K4F A K4A C K4CFB RTa, rateR from m$5table=

3 1ac2 to to" of file Can one retrie+e onl* the 0th row from a table? Ru"a2 #ohan "rovided this solution to select the Nth ro) from a table8 *ELECT E 9RO# tF a :;ERE n D @*ELECT COUNT@ro)idB 9RO# tF b :;ERE a4ro)id ?D b4ro)idB= *hai2 (haleel "rovided this solution8 *ELECT E 9RO# @ *ELECT ENA#E RO:NU# RN 9RO# E#P :;ERE RO:NU# S FKF B :;ERE RN D FKK= Note8 +n this first >uer$ )e select one more than the re>uired ro) number then )e select the re>uired one4 +ts far better than usin! a #+NU* o"eration4 Ravi Pachalla "rovided these solutions8 *ELECT fF 9RO# tF :;ERE ro)id D @

*ELECT ro)id 9RO# tF :;ERE ro)num SD FK #+NU* *ELECT ro)id 9RO# tF :;ERE ro)num S FKB= *ELECT ro)num em"no 9RO# scott4em" a %ROUP 1H ro)num em"no ;A'+N% ro)num D I=

Alternativel$444 *ELECT E 9RO# em" :;ERE ro)numDF AND ro)id NOT +N @*ELECT ro)id 9RO# em" :;ERE ro)num S FKB= Please note there is no e,"licit ro) order in a relational database4 ;o)ever this >uer$ is >uite fun and ma$ even hel" in the odd situation4 3 1ac2 to to" of file Can one retrie+e onl* rows 1 to 2 from a table? *hai2 (haleel "rovided this solution to the "roblem8 *ELECT E 9RO# @ *ELECT ENA#E RO:NU# RN 9RO# E#P :;ERE RO:NU# S FKF B :;ERE RN bet)een OF and FKK = Note8 the FKF is just one !reater than the ma,imum ro) of the re>uired ro)s @means ,D OK $DFKK so the inner values is $MFB4 Ravi Pachalla "rovided this solution8 *ELECT ro)num fF 9RO# tF %ROUP 1H ro)num fF ;A'+N% ro)num 1ET:EEN A AND I=

Another solution is to use the #+NU* o"eration4 9or e,am"le to dis"la$ ro)s N to Q construct a >uer$ li2e this8 *ELECT E 9RO# table0

:;ERE ro)id in @ *ELECT ro)id 9RO# table0 :;ERE ro)num SD Q #+NU* *ELECT ro)id 9RO# table0 :;ERE ro)num S NB= Please note there is no e,"licit ro) order in a relational database4 ;o)ever this >uer$ is >uite fun and ma$ even hel" in the odd situation4 3 1ac2 to to" of file ow does one select -3-.2 0th row from a table? One can easil$ select all even odd or Nth ro)s from a table usin! *.L >ueries li2e this8 Method #: Usin! a sub>uer$ *ELECT E 9RO# em"

:;ERE @RO:+D KB +N @*ELECT RO:+D #OD@RO:NU# IB 9RO# em"B=

Method $: Use d$namic vie)s @available from OracleQ4AB8 *ELECT E 9RO# @ *ELECT ro)num rn em"no ename 9RO# em" B tem" :;ERE #OD@tem"4RO:NU# IB D K= Method %: Usin! %ROUP 1H and ;A'+N% - "rovided b$ Ravi Pachalla *ELECT ro)num fF 9RO# tF %ROUP 1H ro)num fF ;A'+N% #OD@ro)num nB D K OR ro)num D A-n Please note there is no e,"licit ro) order in a relational database4 ;o)ever these >ueries are >uite fun and ma$ even hel" in the odd situation4

3 1ac2 to to" of file ow does one select the 456 0 rows from a table? 9orm OraclePi one can have an inner->uer$ )ith an ORDER 1H clause4 Loo2 at this e,am"le8 *ELECT E 9RO# @*ELECT E 9RO# m$5table ORDER 1H col5name5F DE*CB

:;ERE RO:NU# S FK= Use this )or2around )ith "rior releases8 *ELECT E 9RO# m$5table a :;ERE FK ?D @*ELECT COUNT@D+*T+NCT ma,colB 9RO# m$5table b :;ERE b4ma,col ?D a4ma,colB ORDER 1H ma,col DE*C= 3 1ac2 to to" of file ow does one code a tree7structured "uer*? Tree-structured >ueries are definitel$ non-relational @enou!h to 2ill Codd and ma2e him roll in his !raveB4 Also this feature is not often found in other database offerin!s4 The *COTT-T+%ER database schema contains a table E#P )ith a self-referencin! relation @E#PNO and #%R columnsB4 This table is "erfect for tesin! and demonstratin! tree-structured >ueries as the #%R column contains the em"lo$ee number of the RcurrentR em"lo$ee&s boss4 The LE'EL "seudo-column is an indication of ho) dee" in the tree one is4 Oracle can handle >ueries )ith a de"th of u" to ANN levels4 Loo2 at this e,am"le8 select LE'EL E#PNO ENA#E #%R from E#P connect b$ "rior E#PNO D #%R start )ith #%R is NULL=

One can "roduce an indented re"ort b$ usin! the level number to substrin! or l"ad@B a series of s"aces and concatenate that to the strin!4 Loo2 at this e,am"le8 select l"ad@& & LE'EL E AB LL ENA#E 44444444 One uses the Rstart )ithR clause to s"ecif$ the start of the tree4 #ore than one record can match the startin! condition4 One disadvanta!e of havin! a Rconnect b$ "riorR clause is that $ou cannot "erform a join to other tables4 The Rconnect b$ "riorR clause is rarel$ im"lemented in the other database offerin!s4 Tr$in! to do this "ro!rammaticall$ is difficult as one has to do the to" level >uer$ first then for each of the records o"en a cursor to loo2 for child nodes4 One )a$ of )or2in! around this is to use PL-*.L o"en the drivin! cursor )ith the Rconnect b$ "riorR statement and the select matchin! records from other tables on a ro)-b$-ro) basis insertin! the results into a tem"orar$ table for later retrieval4 3 1ac2 to to" of file ow does one code a matrix report in S!L? Loo2 at this e,am"le >uer$ )ith sam"le out"ut8 *ELECT E 9RO# @*ELECT job sum@decode@de"tno FK salBB DEPTFK sum@decode@de"tno AK salBB DEPTAK sum@decode@de"tno CK salBB DEPTCK sum@decode@de"tno IK salBB DEPTIK 9RO# scott4em" %ROUP 1H jobB ORDER 1H F=

/O1

DEPTFK

DEPTAK

DEPTCK

DEPTIK

--------- ---------- ---------- ---------- ---------ANALH*T CLER( #ANA%ER PRE*+DENT *ALE*#AN FCKK AINK NKKK NJKK JKKK FOKK AOQN ONK APNK

3 1ac2 to to" of file ow does one implement 8974 -07-LS- in a select statement? The Oracle decode function acts li2e a "rocedural statement inside an *.L statement to return different values or columns based on the values of other columns in the select statement4 Some examples: select decode@se, &#& &#ale& &9& &9emale& &Un2no)n&B from em"lo$ees=

select a b decode@ abs@a-bB a-b &a ? b& K &a D b& &a S b&B from table0=

select decode@ %REATE*T@A 1B A &A is !reater OR E.UAL than 1& &1 is !reater than A&B444

select decode@ %REATE*T@A 1B A decode@A 1 &A NOT %REATER T;AN 1& &A %REATER T;AN 1&B &A NOT %REATER T;AN 1&B444 0ote: The decode function is not AN*+ *.L and is rarel$ im"lemented in other RD1#* offerin!s4 +t is one of the !ood thin!s about Oracle but use it s"arin!l$ if "ortabilit$ is re>uired4 9rom Oracle Pi one can also use CA*E statements in *.L4 Loo2 at this e,am"le8 *ELECT ename CA*E :;EN sal?FKKK T;EN &Over "aid& EL*E &Under "aid& END 9RO# em"=

3 1ac2 to to" of file ow can one dump, examine the exact content of a database column? *ELECT DU#P@colFB

9RO# tabF :;ERE condF D valF=

DU#P@COLFB ------------------------------------T$"DOJ LenDI8 JN JJ JQ CA 9or this e,am"le the t$"e is OJ indicatin! C;AR and the last b$te in the column is CA )hich is the A*C++ code for a s"ace4 This tells us that this column is blan2"added4 3 1ac2 to to" of file Can one drop a column from a table? 9rom OraclePi one can DROP a column from a table4 Loo2 at this sam"le scri"t demonstratin! the ALTER TABLE table_name DROP COLUMN column_name; command4 Other )or2arounds8 F4 *.L? u"date tF set column5to5dro" D NULL= *.L? rename tF to tF5base= *.L? create vie) tF as select Ss"ecific columns? from tF5base=

A4 *.L? create table tA as select Ss"ecific columns? from tF= *.L? dro" table tF= *.L? rename tA to tF= 3 1ac2 to to" of file Can one rename a column in a table? 9rom OracleOi one can RENA#E a column from a table4 Loo2 at this e,am"le8 ALTER TA1LE tablename RENA#E COLU#N oldcolumn TO ne)column= Other )or2arounds8 F4 -- Use a vie) )ith correct column names444

rename tF to tF5base= create vie) tF Scolumn list )ith ne) name? as select E from tF5base=

A4 -- Recreate the table )ith correct column names444 create table tA Scolumn list )ith ne) name? as select E from tF= dro" table tF= rename tA to tF=

C4 -- Add a column )ith a ne) name and dro" an old column444 alter table tF add @ ne)colame datat$"e B= u"date tF set ne)colnameDoldcolname= alter table tF dro" column oldcolname= 3 1ac2 to to" of file ow can 8 change m* 5racle password? +ssue the follo)in! *.L command8 ALTER U*ER Susername? +DENT+9+ED 1H Sne)5"ass)ord? 9rom OracleP $ou can just t$"e R"ass)ordR from *.LEPlus or if $ou need to chan!e another user&s "ass)ord t$"e R"ass)ord user5nameR4 3 1ac2 to to" of file ow does one find the next +alue of a se"uence? Perform an RALTER *E.UENCE 444 NOCAC;ER to unload the unused cached se>uence numbers from the Oracle librar$ cache4 This )a$ no cached numbers )ill be lost4 +f $ou then select from the U*ER5*E.UENCE* dictionar$ vie) $ou )ill see the correct hi!h )ater mar2 value that )ould be returned for the ne,t NE0T'ALL call4 After)ards "erform an RALTER *E.UENCE 444 CAC;ER to restore cachin!4 Hou can use the above techni>ue to "revent se>uence number loss before a SHUTDOWN ABORT or an$ other o"eration that )ould cause !a"s in se>uence values4 3 1ac2 to to" of file Wor'around for snapshots on tables with L50( columns

Hou can use the *.LEPlus COPH command instead of sna"shots if $ou need to co"$ LON% and LON% RA: variables from one location to another4 E!8 COPH TO *COTT-T+%ERTRE#OTE CREATE +#A%E5TA1LE U*+N% *ELECT +#A%E5NO +#A%E 9RO# +#A%E*= -

Note8 +f $ou run OracleP convert $our LON%s to LO1s as it can be re"licated4

Contents of this Page 3 3 3 3 3 Can + U"date 9rom Another Table U Can + Remove Du"licate Ro)s U :hat is RO:+D !ood for U :hat is RO:NU# !ood for U 'ie)s

.uestions on vie)s have moved to another "a!e4 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 Can + im"lement Tree *tructured .ueries U ;o) can + !et information on the ro) based on !rou" information U ;o) can + !et a name for a tem"orar$ table that )ill not clash U ;o) can + discover )hat tables columns etc are there U ;o) can + rename a column U +s there a formatter for *.L or PL-*.L U ;o) come records for the date + )ant are missin! U ;o) can + inter"ret a t)o di!it $ear after AKKK U :hat is a cluster U ;o) do + control )hich rollbac2 se!ment + use U ;o) do order a union U :ho are *COTT *H*TE# and *H* U ;o) do + avoid blo)in! rollbac2 se!ments U ;o) can + restore "ass)ords U Not 9ormatted Het

Can 8 :pdate 9rom /nother 4able ? Hes4 9or e,am"le if )e had a table DEPT5*U##ARH )e could u"date the number of em"lo$ees field as follo)s8 u"date DEPT5*U##ARH s set NU#5E#P* D @ select count@FB from E#P E

)here E4DEPTNO D *4DEPTNO B= 1ac2 to To" of 9ile

Can 8 remo+e duplicate rows ? Hes usin! the RO:+D field4 The RO:+D is !uaranteed uni>ue4 There are man$ variations on this theme but the lo!ic is to delete all but one record for each 2e$ value4 delete from E#P E )here not E4RO:+D D @ select min@94RO:+DB from E#P 9 )here 94E#P5+D D E4E#P5+D B=

1ac2 to To" of 9ile

Can 8 implement 4ree Structured !ueries ? HesV This is commonl$ as2ed b$ those mi!ratin! from non-RD1#* a""s4 This is definitel$ non-relational @enou!h to 2ill Codd and then ma2e him roll in his !raveB and is a feature + have not seen in the com"etition4 The definitive e,am"le is in the e,am"le *COTT-T+%ER database )hen loo2in! at the E#P table @E#PNO and #%R columnsB4 The #%R column contains the em"lo$ee number of the RcurrentR em"lo$ee&s boss4 Hou have available an e,tra "seudo-column LE'EL that sa$s ho) dee" in the tree $ou are4 Oracle can handle >ueries )ith a de"th u" to ANN4 select LE'EL E#PNO ENA#E #%R from E#P connect b$ "rior E#PNO D #%R start )ith #%R is NULL= Hou can !et an RindentedR re"ort b$ usin! the level number to substrin! or l"ad a series of s"aces and concatenate that to the strin!4

select l"ad@& & LE'EL E AB LL ENA#E 44444444 Hou use the start )ith clause to s"ecif$ the start of the tree@sB4 #ore than one record can match the startin! condition4 One disadvanta!e of a Rconnect b$ "riorR is that $ou cannot "erform a join to other tables4 *till + have not mana!ed to see an$thin! else li2e the Rconnect b$ "riorR in the other vendor offerin!s and + li2e trees4 Even tr$in! to doin! this "ro!rammaticall$ in embedded *.L is difficult as $ou have to do the to" level >uer$ for each of them o"en a cursor to loo2 for lo)er level ro)s for each of these4444444 soon $ou blo) the cursor limit for $our installation4 The )a$ around this is to use PL-*.L o"en the drivin! cursor )ith the Rconnect b$ "riorR statement and the select matchin! records from other tables on a ro)-b$-ro) basis insertin! the results into a tem"orar$ table for later retrieval4 Note that $ou can&t tric2 Oracle b$ usin! CONNECT 1H PR+OR on a vie) that does the join4 1ac2 to To" of 9ile

ow can 8 get information on the row based on group information ? +ma!ine )e have the E#P table and )ant details on the em"lo$ee )ho has the hi!hest salar$4 Hou need to use a sub>uer$4 select e4ENA#E e4E#PNO e4*AL from E#P e )here e4*AL in @ select ma, @eA4*ALB from E#P eA B= Hou could !et similar info on em"lo$ees )ith the hi!hest salar$ in their de"artments as follo)s select e4ENA#E e4DEPTNO e4*AL from E#P e )here e4*AL D @ select ma, @eA4*ALB

from E#P eA )here eA4DEPTNO D e4DEPTNO B= 1ac2 to To" of 9ile

ow can 8 get a name for a temporar* table that will not clash ? Use a se>uence and use the number to hel" $ou build the tem"orar$ table name4 Note that *.L-OA is develo"in! s"ecific constructs for usin! tem"orar$ tables4 1ac2 to To" of 9ile

ow can 8 disco+er what tables, columns, etc are there ? Oracle maintains a live set of vie)s that $ou can >uer$ to tell $ou )hat $ou have available4 +n 'J the first t)o to loo2 at are D+CT and D+CT5COLU#N* )hich act as a director$ of the other dictionar$ vie)s4 +t is a !ood idea to be familiar )ith these4 Not all of these vie)s are accessible b$ all users4 +f $ou are a D1A $ou should also create "rivate D1A s$non$ms b$ runnin! WORACLE5;O#E-rdbms-admin-dba5s$n4s>l in $our account4 1ac2 to To" of 9ile

ow can 8 rename a column ? There is no )a$ a column can be renamed usin! normal *.L4 +t can be done carefull$ b$ the D1A "la$in! around )ith internal *H* dictionar$ tables and bouncin! the database but this is not su""orted4 @+ have successfull$ done it in 'I thru 'QB4 Do bac2u" the database first unless $ou feel brave4 +&ve )ritten a >uic2 and dirt$ scri"t rncol4s>l to do this4 +f $ou can&t fi!ure out ho) to use it from the source $ou definitel$ should not run it4 Hou can use a similar dirt$ tric2 for chan!in! o)nershi" of tables if stora!e s"ace is limited4 1ac2 to To" of 9ile

8s there a formatter for S!L or 6L,S!L ? There are a number of RbeautifiersR for various "ro!ram lan!ua!es4 The cb and indent "ro!rams for the C lan!ua!e s"rin! to mind @althou!h the$ have sli!htl$ different conventionsB4 As far as + 2no) there is no PD formatter for *.L available4 %iven that there are PD !eneral *.L "arsers and that the *.L standards are drafted in somethin! close to 1N9 ma$be someone could base a reformatter based on the !rammar4 Note that $ou CANNOT use cb and indent )ith ProEC as both these "ro!rams )ill scre) u" the embedded *.L code4 + have recentl$ heard that (umaran *$stems @see 'endor listB have a 9orms PL-*.L and *.L formatter but + do not no) if the$ have unbundled it4 1ac2 to To" of 9ile

ow come records for the date 8 want are missing ? Hou are tr$in! to retrieve data based on somethin! li2e8 *ELECT fldF fldA 9RO# tbl :;ERE date5field D &FP-jun-JK& Hou E2no)E there are records for that da$ - but none of them are comin! bac2 to $ou4 :hat has ha""ened is that $our records are not set to midni!ht @)hich is the default value if time of da$ not s"ecifiedB4 Hou can either use to5char and to5date functions )hich can be a bad move re!ardin! *.L "erformance or $ou can sa$ :;ERE date5field ?D &FP-jun-JK& AND date5field S &FO-jun-JK& An alternative could be somethin! li2e :;ERE date5field bet)een &FP-jun-FOJK& AND to5date@&AC8NO8NO FP-jun-JK& &;;AI8444444HH&B = 1ac2 to To" of 9ile

ow can 8 interpret a two digit *ear after $;;; ? :hen convertin! to dates from characters )hen $ou onl$ have t)o characters for the $ear the "icture format RRRR )ill be inter"reted as the $ear based on a !uess that that date is bet)een FONK and AKIO4

1ac2 to To" of 9ile

What are these 3< tables? There are a number of tables-vie)s be!innin! )ith 'W that hold !or$ details for "erformance monitorin!4 These are not !uaranteed to be stable from minor release to minor release and are for D1As onl$4 There are usuall$ no real underl$in! tables @unli2e *H*4O1/WB and are dummied u" b$ the RD1#* 2ernel soft)are in much the same )a$ that UN+0 *$stem '4I dummies u" the files in the -"roc or -dev-"roc directories4 +f $ou have an$ code de"endin! on these @and the )idel$ used tools su""lied b$ Oracle but unsu""orted are in this cate!or$B then $ou need to verif$ that ever$thin! )or2s each time $ou u"!rade $our database4 And )hen a major revision chan!es all bets are off4 1ac2 to To" of 9ile

ow do 8 get a top ten ? This >uestion often !ets the res"onse :;ERE RO:NU# SD FK but this )ill not )or2 @e,ce"t accidentall$B because the RO:NU# "seudocolumn is !enerated before the ORDER or :;ERE clauses come into effect4 One ele!ant *.L-onl$ a""roach @althou!h it )ill be a bitch on a lar!e tableB )as su!!ested b$ Stowe@aol.com select a4ordered5column a4other5stuff from table5name a

)here FK ? @ select count@FB from table5name b

)here b4ordered5column S a4ordered5column B order b$ a4ordered5columnl= + do not believe that strai!ht *.L is the )a$ to !o for such "roblems )hen $ou have PL-*.L available4

#$ a""roach is to use PL-*.L instead @in *.LEPlusB8 variable tenthsal number declare n number= cursor cF is select *AL from E#P order 1H *AL desc= be!in o"en cF= for n in F44FK loo" fetch cF into 8tenthsal= end loo"= close cF= end8 select E from E#P )here *AL SD 8tenthsal order b$ *AL desc= Late ne)s8 inde, descendin! hint to *.L )or2s if $ou use a dumm$ restriction to force use of the inde,4 Needs 'Q etc4 1ac2 to To" of 9ile

ow do control which rollbac' segment 8 use ? +n *.L $ou ma$ need to control the rollbac2 se!ment used as the default rollbac2 se!ment ma$ be too small for the re>uired transaction or $ou ma$ )ant to ensure that $our transaction runs in a s"ecial rollbac2 se!ment unaffected b$ others4 The statement is as follo)s8 *ET TRAN*ACT+ON U*E ROLL1AC( *E%#ENT se!ment5name= On a related note if all $ou are doin! are *ELECT* it is )orth tellin! the database of this usin! the follo)in!8

*ET TRAN*ACT+ON READ ONLH= 1oth these statements must be the first statement of the transaction4 1ac2 to To" of 9ile

ow do 8 order a union ? @%overnments around the )orld have been tr$in! to fi!ure this one outB4 Use the column number4 *a$ )e are !ettin! a list of names and codes and )ant it ordered b$ the name usin! both E#P and DEPT tables8 select DEPTNO DNA#E from DEPT union select E#PNO ENA#E from E#P order b$ A= 1ac2 to To" of 9ile

Who are SC544, S2S4-M and S2S ? These three users are common in man$ databases4 *ee the !lossar$ entries under *COTT *COTT and *H*4 Another common user-"ass)ord is PL*.L-*UPER*ECRET used for PL-*.L demo stuff4 1ac2 to To" of 9ile

ow can 8 a+oid blowing rollbac' segments ? The sim"le ans)er is ma2e sure $ou have them bi! enou!h and 2ee" $our transactions small but that is bein! a smartarse4 #ore recent versions of Oracle have an o"tion for the session that $ou can set that commits ever$ so man$ D#L statements4 This is O( e,ce"t for )here $ou are doin! $our )or2 in a sin!le statement rather than usin! PL-*.L and a loo" construct4

+ma!ine $ou have a ;U%E table and need to u"date it "ossibl$ u"datin! the 2e$4 Hou cannot u"date it in one !o because $our rollbac2 se!ments are too small4 Hou cannot o"en a cursor and commit ever$ n records because usuall$ the cursor )ill close4 Hou cannot have a number of u"dates of a fe) records each because the 2e$s ma$ chan!e - causin! $ou to visit records more than once4 The solution + have used )as to have one "rocess select RO:+D from the a""ro"riate ro)s and "um" these @via standard +-OB to another "rocess that loo"ed around readin! RO:+Ds from standard in"ut u"datin! the a""ro"riate record and committin! ever$ FK records or so4 This )as ver$ eas$ to "ro!ram and also )as >uite fast in e,ecution4 The number of loc2s and si7e of rollbac2 se!ments re>uired )as minimal4 +f $ou are )ritin! in ProEC and use #ODEDORACLE there are )a$s around it too but not if $ou are usin! #ODEDAN*+4 1ac2 to To" of 9ile

ow can 8 restore passwords ? O( so this is reall$ a D1A >uestion but it is )orth "uttin! in here because it involves *.L re!ardless of interface4 9irst loo2 at the PA**:ORD column in D1A5U*ER*4 +t loo2s li2e !obbled$!oo2 because it is an encr$"ted "ass)ord4 ;o)ever $ou can use this if $ou have saved it some)here else4 *a$ $ou )ant to im"ersonate a user in a batch run overni!ht4 9irst stash the !obbled$!oo2 "ass)ord a)a$ some)here !rant connect to the user identified b$ some "ass)ord $ou 2no) and then run $our batches usin! the ne) 2no)n "ass)ord4 To restore the "ass)ord to )hat it )as use the follo)in! s$nta, @)hich + thin2 is undocumentedB4 !rant connect to *COTT identified b$ "ass)ords %O11LEDH%OO(= Note es"eciall$ the * on the end of PA**:ORD*4 .uestion8 :hat versions are loaded on m$ machine U *.L? select substr@"roduct F FNB "roduct substr@version F FNB version substr@status F FNB status from "roduct5com"onent5version PRODUCT 'ER*+ON *TATU* --------------- --------------- --------------NL*RTL C4I4F4K4K Production OraclePi Enter" P4F4Q4C4K Production

PL-*.L P4F4Q4C4K TN* for CA-bit P4F4Q4C4K also

Production Production

*.L? select E from vWversion )here banner li2e &Oracle6&= 1ANNER ---------------------------------------------------------------OraclePi Enter"rise Edition Release P4F4Q4C4K - Production .uestion8 :hat A""lications-'ersions-Patch Levels are loaded on m$ machine U this "uer* gi+es more details than the one below

select substr@a4APPL+CAT+ON5NA#E F JKB RA""lication NameR substr@i4PRODUCT5'ER*+ON F IB R'ersionR i4PATC;5LE'EL RPatch LevelR i4APPL+CAT+ON5+D RA""lication +DR i4LA*T5UPDATE5DATE RLast U"dateR from APP*49ND5PRODUCT5+N*TALLAT+ON* i APP*49ND5APPL+CAT+ON5ALL5'+E: a )here i4APPL+CAT+ON5+D D a4APPL+CAT+ON5+D -- not all a""lications u"date the ne,t field correctl$ -- and i4PATC;5LE'EL li2e &FFi6& -- these are the a""lications that concern me most -- and i4APPL+CAT+ON5+D in @&K& &FIK& &AJK& &FKF& &AKK& &AQN& &AKF& &AAA& &FPN&B order b$ a4APPL+CAT+ON5NA#E .uestion8 :hat "atches are loaded on m$ machine U

select "atch5name "atch5t$"e a""lied5"atch5id ra"id5installed5fla! maint5"ac25level from ad5a""lied5"atches )here "atch5name li2e &6& order b$ F ;o) do + obtain CC+D - Chart of Account data for %eneral Led!er

select substr@!l4code5combination5id F NB ccid substr@!l4se!mentF F NB Auth substr@!l4se!mentA F PB Account substr@!l4se!mentC F NB RC substr@!l4se!mentI F NB 9unc substr@!l4se!mentN F NB /ob

9RO# !l4!l5code5combinations !l order b$ code5combination5id .uestion8 ;o) man$ transactions e,ist b$ %L DATE @"eriodB U select !l5date count@!l5dateB from ra5cust5tr,5line5!l5dist5All )here account5class D &RE'& !rou" b$ !l5date .uestion8 :hat is m$ current %L *ET O9 1OO(* select *ET5O951OO(*5+D NA#E *;ORT5NA#E C;ART5O95ACCOUNT*5+D CURRENCH5CODE PER+OD5*ET5NA#E ACCOUNTED5PER+OD5THPE LATE*T5OPENED5PER+OD5NA#E substr@DE*CR+PT+ON F CKB descri"tion from !l5sets5of5boo2s .uestion8 :hat D1 version is on m$ machine U

*.L? 'AR+A1LE 'ER*+ON 'ARC;ARA@NKB *.L? 'AR+A1LE CO#PAT+1+L+TH 'ARC;ARA@NKB *.L? E0EC D1#*5UT+L+TH4D15'ER*+ON@8'ER*+ON 8CO#PAT+1+L+THB PL-*.L "rocedure successfull$ com"leted4 *.L? PR+NT 'ER*+ON 'ER*+ON ---------------------------------------P4K4I4K4K *.L? PR+NT CO#PAT+1+L+TH CO#PAT+1+L+TH ---------------------------------------P4K4K .uestion8 :hat machine or instance am + usin! UU *.L? *elect name from vWdatabase=

NA#E --------AR%P

also $ou can use this8 *.L? select s$s5conte,t@&U*EREN'& &D15NA#E&B A* instance from dual=

+N*TANCE --------------------------------------------------------------------AR%P This variation )ill !ive $ou the machine name $ou are runnin! on8 *.L? *elect s$s5conte,t@&U*EREN'& &TER#+NAL&B from dual= *H*5CONTE0T@&U*EREN'& &TER#+NAL&B -------------------------------------------------------------ARKJJO

.uestion8 ;o) can + retrieve a random number U *.L? select dbms5random4random from dual= RANDO# ---------IONFAOKPQ .uestion8 :hat "atches are loaded on our machine U *.L? select E from AD5APPL+ED5PATC;E* .uestion8 Am + setu" for #ulti-Or! U select multi5or!5fla! from fnd5"roduct5!rou"s= Useful DATE out"ut *.L This format is $$mmdd D $ear month da$ L hhAImi D AI hour cloc2 and minutes select to5char@s$sdate &$$mmddhhAImi&B from dual TO5C;AR@*H ---------KIKOFIFKKN select to5char@s$sdate &hhAI8mi8ss&B from dual TO5C;AR@ -------FK8FF8FI

También podría gustarte