Sei sulla pagina 1di 40

What SQL Is and Isnt

The first thing to understand about SQL is that SQL isnt a procedural language, as are FORTRAN, BASIC, C, COBOL, Pasca , and !a"a# To so "e a $rob e% in one of those $rocedura anguages, &ou 'rite a $rocedure that $erfor%s one s$ecific o$eration after another unti the tas( is co%$ ete# The $rocedure %a& be a inear se)uence or %a& oo$ bac( on itse f, but in either case, the $rogra%%er s$ecifies the order of e*ecution# SQL, on the other hand, is nonprocedural. To so "e a $rob e% using SQL, si%$ & te SQL what &ou 'ant +as if &ou 'ere ta (ing to A addins genie, instead of te ing the s&ste% how to get &ou 'hat &ou 'ant# The database %anage%ent s&ste% +-B.S, decides the best 'a& to get &ou 'hat &ou re)uest# To i ustrate 'hat I %ean b& /te the s&ste% 'hat &ou 'ant,0 su$$ose that &ou ha"e an 1.PLO211 tab e and &ou 'ant to retrie"e fro% that tab e the ro's that corres$ond to a &our senior $eo$ e# 2ou 'ant to define a senior $erson as an&one o der than age 34 or an&one earning %ore than 564,444 $er &ear# 2ou can %a(e the desired retrie"a b& using the fo o'ing )uer&7 S1L1CT 8 FRO. 1.PLO211 9:1R1 Age;34 OR Sa ar&;64444< This state%ent retrie"es a ro's fro% the 1.PLO211 tab e 'here either the "a ue in the Age co u%n is greater than 34 or the "a ue in the Sa ar& co u%n is greater than 64,444# In SQL, &ou dont need to s$ecif& ho' the infor%ation is retrie"ed# The database engine e*a%ines the database and decides for itse f ho' to fu fi &our re)uest# 2ou need on & to s$ecif& 'hat data &ou 'ant to retrie"e# A query is a )uestion &ou as( the database# If an& of the data in the database satisfies the conditions of &our )uer&, SQL retrie"es that data#

SQL Commands
The SQL co%%and anguage consists of a i%ited nu%ber of co%%ands that s$ecifica & re ate to data hand ing# So%e of these co%%ands $erfor% data definition functions< so%e $erfor% data= %ani$u ation functions< and others $erfor% data=contro functions#

Types of SQL Statements


A though SQL is considered a sub anguage because of its non$rocedura nature, it is nonethe ess a co%$ ete anguage in that it a o's &ou to create and %aintain database ob>ects, secure those ob>ects, and %ani$u ate the data 'ithin the ob>ects# One co%%on %ethod used to categori?e SQL state%ents is to di"ide the% according to the functions the& $erfor%# Based on this %ethod, SQL can be se$arated into three t&$es of state%ents7

@ Data Definition Language (DDL) --L state%ents are used to create, %odifies, or de ete
database ob>ects such as tab es, "ie's, sche%as, do%ains, triggers, and stored $rocedures# The SQL (e&'ords %ost often associated 'ith --L state%ents are CREATE, ALTER, and DRO # For e*a%$ e, &ou 'ou d use the CR1AT1 TABL1 state%ent to create a tab e, the ALT1R TABL1

state%ent to %odif& the tab es $ro$erties, and the -ROP TABL1 state%ent to de ete the tab e definition fro% the database# Another --L co%%ands are TR!"CATE and RE"A#E$

@ Data Que%y Language (DQL) -QL state%ents is used to retrie"e data fro% database# The
$ri%ar& 'or d (e&'ord associated 'ith -QL state%ent is SELECT#

@ Data #anipu&ation Language (D#L) -.L state%ents are used to retrie"e, adds, %odif&,
or de ete data stored in &our database ob>ects# The $ri%ar& (e&'ords associated 'ith -.L state%ents are I"SERT, ! DATE, and DELETE, a of 'hich re$resent the t&$es of state%ents &ou $robab & be using the %ost# For e*a%$ e, &ou can use a S1L1CT state%ent to retrie"e data fro% a tab e and an INS1RT state%ent to add data to a tab e#

@ Data Cont%o& Language (DCL) -CL state%ents a o' &ou to contro 'ho or 'hat
+a database user can be a $erson or an a$$ ication $rogra%, has access to s$ecific ob>ects in &our database# 9ith -CL, &ou can grant or restrict access b& using the 'RA"T or RE(O)E state%ents, the t'o $ri%ar& -CL co%%ands# The -CL state%ents a so a o' &ou to contro the t&$e of access each user has to database ob>ects# For e*a%$ e, &ou can deter%ine 'hich users can "ie' a s$ecific set of data and 'hich users can %ani$u ate that data#

@ T%ansa*tion Cont%o& Language (TCL) The %ain TCL co%%ands are CO##IT,
ROLL+AC) and SA(E OI"T$

Types of E,e*ution
In addition to defining ho' the anguage can be used, the SQL7 A446 standard $ro"ides detai s on ho' SQL state%ents can be e*ecuted# These %ethods of e*ecution, (no'n as binding styles, not on & affect the nature of the e*ecution, but a so deter%ine 'hich state%ents, at a %ini%u%, %ust be su$$orted b& a $articu ar binding st& e# The standard defines four %ethods of e*ecution7 @ Di%e*t in-o*ation. B& using this %ethod, &ou can co%%unicate direct & fro% a front=end a$$ ication, such as iSQL8P us in Orac e or .anage%ent Studio in .icrosoft SQL Ser"er, to the database# +The front=end a$$ ication and the database can be on the sa%e co%$uter, but often are not#, 2ou si%$ & enter &our )uer& into the a$$ ication 'indo' and e*ecute &our SQL state%ent# The resu ts of &our )uer& are returned to &ou as i%%ediate & as $rocessor $o'er and database constraints $er%it# This is a )uic( 'a& to chec( data, "erif& connections, and "ie' database ob>ects# :o'e"er, the SQL standards guide ines about direct in"ocation are fair & %ini%a , so the %ethods used and SQL state%ents su$$orted can "ar& 'ide & fro% $roduct to $roduct# @ Em/edded SQL. In this %ethod, SQL state%ents are encoded +e%bedded, direct & in the host $rogra%%ing anguage# For e*a%$ e, &ou can e%bed SQL state%ents 'ithin C a$$ ication code# Before the code is co%$i ed, a $re$rocessor ana &?es the SQL state%ents and s$ its the% out fro% the C code# The SQL code is con"erted to a for% the R-B.S can understand, and the re%aining C code is co%$i ed as it 'ou d be nor%a &# @ #odu&e /inding. In this %ethod a o's &ou to create b oc(s of SQL state%ents +%odu es, that are se$arate fro% the host $rogra%%ing anguage# Once the %odu e is created, it is co%bined into an a$$ ication 'ith a in(er# A %odu e contains, a%ong other things, $rocedures, and it is the $rocedures that contain the actua SQL state%ents#

@ Ca&&0&e-e& inte%fa*e (CLI). A CLI a o's &ou to in"o(e SQL state%ents through an interface b& $assing SQL state%ents as argu%ent "a ues to subroutines# The state%ents are not $reco%$i ed as the& are in e%bedded SQL and %odu e binding# Instead, the& are e*ecuted direct & b& the R-B.S# 1%bedded SQL is current & the %ethod %ost co%%on & used in business a$$ ications#

Data Types
-e$ending on their histories, different SQL i%$ e%entations su$$ort a "ariet& of data t&$es# The SQLA44B s$ecification recogni?es on & fi"e $redefined genera t&$es7 C# Nu%erics A# Strings B# Boo eans 3# -ateti%es D# Inter"a s

1$ "ume%i*
1$1$ E,a*t nume%i*
As &ou can $robab & guess fro% the na%e, the exact numeric data t&$es enab e &ou to e*$ress the "a ue of a nu%ber e*act &# Fi"e data t&$es fa into this categor&7 1$1$1$ I"TE'ER data type -ata of the INT1E1R t&$e has no fractiona $art, and its $recision de$ends on the s$ecific SQL i%$ e%entation# The database de"e o$er cant s$ecif& the $recision# The precision of a nu%ber is the %a*i%u% nu%ber of digits the nu%ber can ha"e# 1$1$2$ S#ALLI"T data type The S.ALLINT t&$e is a so for integers, but the $recision of a S.ALLINT in a s$ecific i%$ e%entation cant be an& arger than the $recision of an INT1E1R on the sa%e i%$ e%entation# I%$ e%entations on IB. S&ste%FBG4 co%$uters co%%on & re$resent S.ALLINT and INT1E1R 'ith C6=bit and BA=bit binar& nu%bers res$ecti"e &# In %an& i%$ e%entations, S.ALLINT and INT1E1R are the sa%e# If &oure defining a database tab e co u%n to ho d integer data and &ou (no' that the range of "a ues in the co u%n 'ont e*ceed the $recision of S.ALLINT data on &our i%$ e%entation, assign the co u%n the S.ALLINT t&$e rather than the INT1E1R t&$e# This assign%ent %a& enab e &our -B.S to conser"e storage s$ace# 1$1$3$ +I'I"T data type The BIEINT data t&$e is ne' 'ith SQL7A44B# It is a so an integer t&$e, and it is defined as a t&$e 'hose $recision is at east as great as that of the INT1E1R t&$e and cou d be greater# The e*act $recision of a BIEINT data t&$e is i%$ e%entation de$endent# 1$1$4$ "!#ERIC data type NH.1RIC data can ha"e a fractiona co%$onent in addition to its integer co%$onent# 2ou can s$ecif& both the $recision and the sca e of NH.1RIC data# +Precision, re%e%ber, is the %a*i%u% nu%ber of digits $ossib e#, The scale of a nu%ber is the nu%ber of digits in its fractiona $art# The sca e of a nu%ber cant be negati"e or arger than that nu%bers $recision# If &ou s$ecif& the NH.1RIC data t&$e, &our SQL i%$ e%entation gi"es &ou e*act & the $recision and sca e that &ou re)uest# 2ou %a& s$ecif& NH.1RIC and get a defau t $recision and sca e, or NH.1RIC +p, and get &our s$ecified $recision and the defau t sca e, or NH.1RIC + p,s, and get

both &our s$ecified $recision and &our s$ecified sca e# The $ara%eters p and s are $ aceho ders that 'ou d be re$ aced b& actua "a ues in a data dec aration. Sa&, for e*a%$ e, that the NH.1RIC data t&$es defau t $recision for &our SQL i%$ e%entation is CA and the defau t sca e is 6# If &ou s$ecif& a database co u%n as ha"ing a NH.1RIC data t&$e, the co u%n can ho d nu%bers u$ to III,III#IIIIII# If, on the other hand, &ou s$ecif& a data t&$e of NH.1RIC +C4, for a co u%n, that co u%n can ho d on & nu%bers 'ith a %a*i%u% "a ue of I,III#IIIIII# The $ara%eter +C4, s$ecifies the %a*i%u% nu%ber of digits $ossib e in the nu%ber# If &ou s$ecif& a data t&$e of NH.1RIC +C4,A, for a co u%n, that co u%n can ho d nu%bers 'ith a %a*i%u% "a ue of II,III,III#II# In this case, &ou %a& sti ha"e ten tota digits, but on & t'o of the digits can fa to the right of the deci%a $oint# NH.1RIC data is for "a ues such as DID#GA# This "a ue has a $recision of D +the tota nu%ber of digits, and a sca e of A +the nu%ber of digits to the right of the deci%a $oint,# A data t&$e of NH.1RIC +D,A, is a$$ro$riate for such nu%bers# 1$1$5$ DECI#AL data type The -1CI.AL data t&$e is si%i ar to NH.1RIC# This data t&$e can ha"e a fractiona co%$onent, and &ou can s$ecif& its $recision and sca e# The difference is that the $recision &our i%$ e%entation su$$ ies %a& be greater than 'hat &ou s$ecif&, and if so, the i%$ e%entation uses the greater $recision# If &ou do not s$ecif& $recision or sca e, the i%$ e%entation uses defau t "a ues, as it does 'ith the NH.1RIC t&$e# An ite% that &ou s$ecif& as NH.1RIC +D,A, can ne"er contain a nu%ber 'ith an abso ute "a ue greater than III#II# An ite% that &ou s$ecif& as -1CI.AL +D,A, can a 'a&s ho d "a ues u$ to III#II, but if the i%$ e%entation $er%its arger "a ues, the -B.S doesnt re>ect "a ues arger than III#II# Hse the NH.1RIC or -1CI.AL t&$e if &our data has fractiona $ositions, and use the INT1E1R, S.ALLINT, or BIEINT t&$e if &our data a 'a&s consists of 'ho e nu%bers# Hse the NH.1RIC t&$e if &ou 'ant to %a*i%i?e $ortabi it&, because a "a ue that &ou define as NH.1RIC +D,A,, for e*a%$ e, ho ds the sa%e range of "a ues on a s&ste%s#

1$2$ App%o,imate nume%i*


So%e )uantities ha"e such a arge range of $ossib e "a ues +%an& orders of %agnitude, that a co%$uter 'ith a gi"en register si?e cant re$resent a the "a ues e*act &# +1*a%$ es of register sizes are BA bits, 63 bits, and CAJ bits#, Hsua & in such cases, e*actness isnt necessar&, and a c ose a$$ro*i%ation is acce$tab e# SQLA44B defines three a$$ro*i%ate nu%eric data t&$es to hand e this (ind of data# 1$2$1$ REAL data type The R1AL data t&$e gi"es &ou a sing e=$recision f oating=$oint nu%ber, the $recision of 'hich de$ends on the i%$ e%entation# In genera , the hard'are &oure using deter%ines $recision# A 63=bit %achine, for e*a%$ e, gi"es &ou %ore $recision than does a BA=bit %achine# A floating-point number is a nu%ber that contains a deci%a $oint# The deci%a $oint /f oats0 or a$$ears in different ocations in the nu%ber, de$ending on the nu%bers "a ue# B#C, B#C3, and B#C3CDI are e*a%$ es of f oating= $oint nu%bers#

1$2$2$ DO!+LE RECISIO" data type The -OHBL1 PR1CISION data t&$e gi"es &ou a doub e=$recision f oating $oint nu%ber, the $recision of 'hich again de$ends on the i%$ e%entation# Sur$rising &, the %eaning of the 'ord -OHBL1 a so de$ends on the i%$ e%entation# -oub e=$recision arith%etic is $ri%ari & e%$ o&ed b& scientific users# -ifferent scientific disci$ ines ha"e different needs in the area of $recision# So%e SQL i%$ e%entations cater to one categor& of users, and other i%$ e%entations cater to other categories of users# In so%e s&ste%s, the -OHBL1 PR1CISION t&$e has e*act & t'ice the ca$acit& of the R1AL data t&$e for both %antissa and e*$onent# +In case &ou"e forgotten 'hat &ou earned in high schoo , &ou can re$resent an& nu%ber as a mantissa %u ti$ ied b& ten raised to the $o'er gi"en b& an e*$onent# 2ou can 'rite 6,6A6, for e*a%$ e, as 6#6A61B# The nu%ber 6#6A6 is the %antissa, 'hich &ou %u ti$ & b& ten raised to the third $o'er< in that case, 3 is the e*$onent#, 2ou gain no benefit b& re$resenting nu%bers that are fair & c ose to one +such as 6,6A6 or e"en 6,6A6,444, 'ith an a$$ro*i%ate nu%eric data t&$e# 1*act nu%eric t&$es 'or( >ust as 'e , and after a , the&re e*act# For nu%bers that are either "er& near ?ero or %uch arger than one, ho'e"er, such as 6#6A61=B3 +a "er& s%a nu%ber,, &ou %ust use an a$$ro*i%ate nu%eric t&$e# The e*act nu%eric t&$es cant ho d such nu%bers# On other s&ste%s, the -OHBL1 PR1CISION t&$e gi"es &ou so%e'hat %ore than t'ice the %antissa ca$acit& and so%e'hat ess than t'ice the e*$onent ca$acit& as the R1AL t&$e# On &et another t&$e of s&ste%, the -OHBL1 PR1CISION t&$e gi"es doub e the %antissa ca$acit& but the sa%e e*$onent ca$acit& as the R1AL t&$e# In this case, accurac& doub es, but range does not# The SQLA44B s$ecification does not tr& to arbitrate or estab ish b& fiat 'hat -OHBL1 PR1CISION %eans# The s$ecification re)uires on & that the $recision of a -OHBL1 PR1CISION nu%ber be greater than the $recision of a R1AL nu%ber# This constraint, though rather 'ea(, is $erha$s the best $ossib e in ight of the great differences &ou encounter in hard'are# 1$2$3$ 6LOAT data type The FLOAT data t&$e is %ost usefu if &ou thin( that &our database %a& so%eda& %igrate to a hard'are $ atfor% 'ith different register si?es than the one on 'hich &ou origina & design it# B& using the FLOAT data t&$e, &ou can s$ecif& a $recision K for e*a%$ e, FLOAT +D,# If &our hard'are su$$orts the s$ecified $recision 'ith its sing e=$recision circuitr&, sing e=$recision arith%etic is 'hat &our s&ste% uses# If the s$ecified $recision re)uires doub e=$recision arith%etic, the s&ste% uses doub e=$recision arith%etic# Hsing FLOAT rather than R1AL or -OHBL1 PR1CISION %a(es $orting &our databases to other hard'are easier, because the FLOAT data t&$e enab es &ou to s$ecif& $recision# The $recision of R1AL and -OHBL1 PR1CISION nu%bers is hard'are=de$endent# If &ou arent sure 'hether to use the e*act nu%eric data t&$es +NH.1RICF -1CI.AL, or the a$$ro*i%ate nu%eric data t&$es +FLOATFR1AL,, use the e*act nu%eric t&$es# The e*act data t&$es are ess de%anding of s&ste% resources and, of course, gi"e e*act rather than a$$ro*i%ate resu ts# If the range of $ossib e "a ues of &our data is arge enough to re)uire the use of the a$$ro*i%ate data t&$es, &ou can $robab & deter%ine this fact in ad"ance#

2$ Cha%a*te% st%ings
-atabases store %an& t&$es of data, inc uding gra$hic i%ages, sounds, and ani%ations# I e*$ect odors to co%e ne*t# Can &ou i%agine a three=di%ensiona C644 * CA44 A3=bit co or i%age of a arge s ice of

$e$$eroni $i??a on &our screen, 'hi e an odor sa%$ e ta(en at -iFi i$$is Pi??a Erotto re$ a&s through &our su$er=%u ti%edia cardL Such a setu$ %a& get frustrating K at east unti &ou can afford to add taste=t&$e data to &our s&ste% as 'e # A as, &ou can e*$ect to 'ait a ong ti%e before odor and taste beco%e standard SQL data t&$es# These da&s, the data t&$es that &ou use %ost co%%on & K after the nu%eric t&$es, of course K are the character=string t&$es# 2ou ha"e three %ain t&$es of character data7 fi,ed *ha%a*te% data +C:ARACT1R or C:AR,, -a%ying *ha%a*te% data +C:ARACT1R MAR2INE or MARC:AR,, and *ha%a*te% &a%ge o/7e*t data +C:ARACT1R LARE1 OB!1CT or CLOB,# 2ou a so ha"e three "ariants of these t&$es of character data7 NATIONAL C:ARACT1R, NATIONAL C:ARACT1R MAR2INE, and NATIONAL C:ARACT1R LARE1 OB!1CT# 2$1$ C8ARACTER data type If &ou define the data t&$e of a co u%n as C:ARACT1R or C:AR, &ou can s$ecif& the nu%ber of characters the co u%n ho ds b& using the s&nta* C:ARACT1R + x,, 'here x is the nu%ber of characters# If &ou s$ecif& a co u%ns data t&$e as C:ARACT1R +C6,, for e*a%$ e, the %a*i%u% ength of an& data &ou can enter in the co u%n is C6 characters# If &ou dont s$ecif& an argu%ent +that is, &ou dont $ro"ide a "a ue in $ ace of the x,, SQL assu%es a fie d ength of one character# If &ou enter data into a C:ARACT1R fie d of a s$ecified ength and &ou enter fe'er characters than the s$ecified nu%ber, SQL fi s the re%aining character s$aces 'ith b an(s# 2$2$ C8ARACTER (AR9I"' data type The C:ARACT1R MAR2INE data t&$e is usefu if entries in a co u%n can "ar& in ength, but &ou dont 'ant SQL to $ad the fie d 'ith b an(s# This data t&$e enab es &ou to store e*act & the nu%ber of characters that the user enters# No defau t "a ue e*ists for this data t&$e# To s$ecif& this data t&$e, use the for% C:ARACT1R MAR2INE +x, or MARC:AR +x,, 'here x is the %a*i%u% nu%ber of characters $er%itted# 2$3$ C8ARACTER LAR'E O+:ECT data type The C:ARACT1R LARE1 OB!1CT +CLOB, data t&$e 'as introduced 'ith SQL7CIII# As its na%e i%$ ies, it is used 'ith huge character strings that are too arge for the C:ARACT1R t&$e# CLOBs beha"e %uch i(e ordinar& character strings, but there are a nu%ber of restrictions on 'hat &ou can do 'ith the%# A CLOB %a& not be used in a PRI.AR2 N12, FOR1IEN N12, or HNIQH1 $redicate# Further%ore, it %a& not be used in a co%$arison other than one for either e)ua it& or ine)ua it&# Because of their arge si?e, a$$ ications genera & do not transfer CLOBs to or fro% a database# Instead, a s$ecia c ient=side t&$e ca ed a CLO locator is used to %ani$u ate the CLOB data# It is a $ara%eter 'hose "a ue identifies a character arge ob>ect# "ATIO"AL C8ARACTER; "ATIO"AL C8ARACTER (AR9I"' and "ATIO0"AL C8ARACTER LAR'E O+:ECT data types -ifferent anguages ha"e so%e characters that differ fro% an& characters in another anguage# For e*a%$ e, Eer%an has so%e s$ecia characters not $resent in the 1ng ish anguage character set# So%e anguages, such as Russian, ha"e a "er& different character set fro% the 1ng ish one# If &ou s$ecif&, for e*a%$ e, the 1ng ish character set as the defau t for &our s&ste%, &ou can use a ternate character

sets because the NATIONAL C:ARACT1R, NATIONAL C:ARACT1R MAR2INE, and NATIONAL C:ARACT1R LARE1 OB!1CT data t&$es function the sa%e as the C:ARACT1R, C:ARACT1R MAR2INE, and C:ARACT1R LARE1 OB!1CT data t&$es, e*ce$t that the character set &oure s$ecif&ing is different fro% the defau t character set# 2ou can s$ecif& the character set as &ou define a tab e co u%n# If &ou 'ant, each co u%n can use a different character set#

3$ +oo&eans
The BOOL1AN data t&$e co%$rises the distinct truth "a ues true and false, as 'e as un!nown. If either a Boo ean true or false "a ue is co%$ared to a NHLL or un!nown truth "a ue, the resu t 'i ha"e the un!nown "a ue#

4$ Datetimes
The SQLA44B standard defines fi"e data t&$es that dea 'ith dates and ti%es# These data t&$es are ca ed datetime data types, or si%$ & datetimes. Considerab e o"er a$ e*ists a%ong these data t&$es, so so%e i%$ e%entations &ou encounter %a& not su$$ort a fi"e# I%$ e%entations that do not fu & su$$ort a fi"e data t&$es for dates and ti%es %a& e*$erience $rob e%s 'ith databases that &ou tr& to %igrate fro% another i%$ e%entation# If &ou ha"e troub e 'ith a %igration, chec( ho' both the source and the destination i%$ e%entations re$resent dates and ti%es#

4$1$ DATE data type The -AT1 t&$e stores &ear, %onth, and da& "a ues of a date, in that order# The &ear "a ue is four digits ong, and the %onth and da& "a ues are both t'o digits ong# A -AT1 "a ue can re$resent an& date fro% the &ear 444C to the &ear IIII# The ength of a -AT1 is ten $ositions, as in CIDG=4J=C3# Because SQL e*$ icit & re$resents a four digits of a &ear in the -AT1 t&$e, SQL data 'as ne"er sub>ect to the %uch=h&$ed "ear #$$$ +2AN, $rob e%# 4$2$ TI#E WIT8O!T TI#E <O"E data type The TI.1 9IT:OHT TI.1 OON1 data t&$e stores hour, %inute, and second "a ues of ti%e# The hours and %inutes occu$& t'o digits# The seconds "a ue %a& be on & t'o digits but %a& a so e*$and to inc ude an o$tiona fractiona $art# This data t&$e, therefore, re$resents a ti%e of BA %inutes and DJ#3B6 seconds $ast I a#%#, for e*a%$ e, as 4I7BA7DJ#3B6# The $recision of the fractiona $art is i%$ e%entation=de$endent but is at east si* digits ong# A TI.1 9IT:OHT TI.1 OON1 "a ue ta(es u$ eight $ositions +inc uding co ons, 'hen the "a ue has no fractiona $art, or nine $ositions +inc uding the deci%a $oint, $ us the nu%ber of fractiona digits 'hen the "a ue does inc ude a fractiona $art# 2ou s$ecif& TI.1 9IT:OHT TI.1 OON1 t&$e data either as TI.1, 'hich gi"es &ou the defau t of no fractiona digits, or as TI.1 9IT:OHT TI.1 OON1 +p,, 'here p is the nu%ber of digit $ositions to the right of the deci%a # The e*a%$ e in the $receding $aragra$h re$resents a data t&$e of TI.1 9IT:OHT TI.1 OON1 +B,#

4$3$ TI#ESTA# WIT8O!T TI#E <O"E data type TI.1STA.P 9IT:OHT TI.1 OON1 data inc udes both date and ti%e infor%ation# The engths and the restrictions on the "a ues of the co%$onents of TI.1STA.P 9IT:OHT TI.1 OON1 data are the sa%e as the& are for -AT1 and TI.1 9IT:OHT TI.1 OON1 data, e*ce$t for one difference7 The defau t ength of the fractiona $art of the ti%e co%$onent of a TI.1STA.P 9IT:OHT TI.1 OON1 is si* digits rather than ?ero# If the "a ue has no fractiona digits, the ength of a TI.1STA.P 9IT:OHT TI.1 OON1 is CI $ositions K ten date $ositions, one s$ace as a se$arator, and eight ti%e $ositions, in that order# If fractiona digits are $resent +si* digits is the defau t,, the ength is A4 $ositions $ us the nu%ber of fractiona digits# The t'entieth $osition is for the deci%a $oint# 2ou s$ecif& a fie d as TI.1STA.P 9IT:OHT TI.1 OON1 t&$e b& using either TI.1STA.P 9IT:OHT TI.1 OON1 or TI.1STA.P 9IT:OHT TI.1 OON1 + p,, 'here p is the nu%ber of fractiona digit $ositions# The "a ue of p cant be negati"e, and the i%$ e%entation deter%ines its %a*i%u% "a ue# 4$4$ TI#E WIT8 TI#E <O"E data type The TI.1 9IT: TI.1 OON1 data t&$e is the sa%e as the TI.1 9IT:OHT TI.1 OON1 data t&$e e*ce$t this t&$e adds infor%ation about the offset fro% uni%ersal time +HTC, a so (no'n as Ereen'ich .ean Ti%e or E.T,# The "a ue of the offset %a& range an&'here fro% PCA7DI to QCB744# This additiona infor%ation ta(es u$ si* %ore digit $ositions fo o'ing the ti%e K a h&$hen as a se$arator, a $ us or %inus sign, and then the offset in hours +t'o digits, and %inutes +t'o digits, 'ith a co on in bet'een the hours and %inutes# A TI.1 9IT: TI.1 OON1 "a ue 'ith no fractiona $art +the defau t, is C3 $ositions ong# If &ou s$ecif& a fractiona $art, the fie d ength is CD $ositions $ us the nu%ber of fractiona digits# 4$5$ TI#ESTA# WIT8 TI#E <O"E data type The TI.1STA.P 9IT: TI.1 OON1 data t&$e functions the sa%e as the TI.1STA.P 9IT:OHT TI.1 OON1 data t&$e e*ce$t that this data t&$e a so adds infor%ation about the offset fro% uni"ersa ti%e# The additiona infor%ation ta(es u$ si* %ore digit $ositions fo o'ing the ti%esta%$ +see the $receding section for the for% of the ti%e ?one infor%ation,# Inc uding ti%e ?one data sets u$ AD $ositions for a fie d 'ith no fractiona $art and A6 $ositions $ us the nu%ber of fractiona digits for fie ds that do inc ude a fractiona $art +si* digits is the defau t nu%ber of fractiona digits,#

5$ Inte%-a&s
The inter%al data t&$es re ate c ose & to the dateti%e data t&$es# An inter"a is the difference bet'een t'o dateti%e "a ues# In %an& a$$ ications that dea 'ith dates, ti%es, or both, &ou so%eti%es need to deter%ine the inter"a bet'een t'o dates or t'o ti%es# SQL recogni?es t'o distinct t&$es of inter"a s7 the year-month inter"a and the day-time inter"a # A &ear=%onth inter"a is the nu%ber of &ears and %onths bet'een t'o dates# A da&=ti%e inter"a is the nu%ber of da&s, hours, %inutes, and seconds bet'een t'o instants 'ithin a %onth# 2ou cant %i* ca cu ations in"o "ing a &ear=%onth inter"a 'ith ca cu ations in"o "ing a da&=ti%e inter"a , because %onths co%e in "ar&ing engths +AJ, AI, B4, or BC da&s ong,#

=$ ROW types
The RO9 data t&$e 'as introduced 'ith SQLCIII# Its not that eas& to understand, and as a beginning to inter%ediate SQL $rogra%%er, &ou %a& ne"er use it# After a , $eo$ e got b& 'ithout it >ust fine bet'een CIJ6 and CIII# One notab e thing about the RO9 data t&$e is that it "io ates the ru es of nor%a i?ation that 1#F# Codd dec ared in the ear & da&s of re ationa database theor&# I ta ( %ore about those ru es in Cha$ter D# One of the defining characteristics of first nor%a for% is that a fie d in a tab e ro' %a& not be %u ti"a ued# A fie d %a& contain one and on & one "a ue# :o'e"er, the RO9 data t&$e a o's &ou to dec are an entire ro' of data to be contained 'ithin a sing e fie d in a sing e ro' of a tab e K in other 'ords, a ro' nested 'ithin a ro'# Consider the fo o'ing SQL state%ent, 'hich defines a RO9 t&$e for a $ersons address infor%ation7 CR1AT1 RO9 T2P1 addrRt&$ + Street C:ARACT1R MAR2INE +AD, Cit& C:ARACT1R MAR2INE +A4, State C:ARACT1R +A, Posta Code C:ARACT1R MAR2INE +I, ,< After its defined, the ne' RO9 t&$e can be used in a tab e definition7 CR1AT1 TABL1 CHSTO.1R + CustI- INT1E1R PRI.AR2 N12, LastNa%e C:ARACT1R MAR2INE +AD,, FirstNa%e C:ARACT1R MAR2INE +A4,, Address addrRt&$ Phone C:ARACT1R MAR2INE +CD, , < The ad"antage here is that if &ou are %aintaining address infor%ation for %u ti$ e entities K such as custo%ers, "endors, e%$ o&ees, and stoc(ho ders K &ou on & ha"e to define the detai s of the address s$ecification once, in the RO9 t&$e definition#

>$ Co&&e*tion types


After SQL bro(e out of the re ationa straight>ac(et 'ith SQLCIII, t&$es that "io ate first nor%a for% beca%e $ossib e# It beca%e $ossib e for a fie d to contain a 'ho e co ection of ob>ects rather than >ust one# The ARRA2 t&$e 'as introduced in SQL7CIII, and the .HLTIS1T t&$e 'as introduced in SQL7A44B# T'o co ections %a& be co%$ared to each other on & if the& are both the sa%e t&$e, either ARRA2 or .HLTIS1T, and if their e e%ent t&$es are co%$arab e# Because arra&s ha"e a defined e e%ent order, corres$onding e e%ents fro% the arra&s can be co%$ared# .u tisets do not ha"e a defined e e%ent order, but can be co%$ared if an enu%eration e*ists for each %u tiset being co%$ared and the enu%erations can be $aired# >$1$ ARRA9 type The ARRA2 data t&$e "io ates first nor%a for% +CNF, but in a different 'a& than the 'a& the RO9 t&$e "io ates CNF# The ARRA2 t&$e, a co ection t&$e, is not a distinct t&$e in the sa%e sense that

C:ARACT1R or NH.1RIC is distinct data t&$es# An ARRA2 t&$e %ere & a o's one of the other t&$es to ha"e %u ti$ e "a ues 'ithin a sing e fie d of a tab e# Re$eating grou$s are a no=no according to c assica re ationa database theor&, but this is one of se"era e*a%$ es of cases 'here SQLCIII bro(e the ru es# 9hen -r# Codd first enunciated the ru es of nor%a i?ation, he traded off functiona f e*ibi it& for data integrit&# SQLCIII too( bac( so%e of that functiona f e*ibi it&, at the cost of so%e added structura co%$ e*it&# The increased structura co%$ e*it& cou d trans ate into co%$ro%ised data integrit& if &ou are not fu & a'are of a the effects of actions &ou $erfor% on &our database# Arra&s are ordered in that each e e%ent in an arra& is associated 'ith e*act & one ordina $osition in the arra&# >$2$ #u&tiset type A %u tiset is an unordered co ection# S$ecific e e%ents of the %u tiset %a& not be referenced, because the& are not assigned a s$ecific ordina $osition in the %u tiset#

?$ RE6 types
R1F t&$es are not $art of core SQL# This %eans that a -B.S %a& c ai% co%$ iance 'ith SQLA44B 'ithout i%$ e%enting R1F t&$es at a # The R1F t&$e is not a distinct data t&$e in the sense that C:ARACT1R and NH.1RIC are# Instead, it is a $ointer to a data ite%, ro' t&$e, or abstract data t&$e that resides in a ro' of a tab e +a site,# -ereferencing the $ointer can retrie"e the "a ue stored at the target site# If &oure confused, dont 'orr&, because &oure not a one# Hsing the R1F t&$es re)uires a 'or(ing (no' edge of ob>ect=oriented $rogra%%ing +OOP, $rinci$ es# This boo( refrains fro% 'ading too dee$ & into the %ur(& 'aters of OOP# In fact K because the R1F t&$es are not a $art of core SQL K &ou %a& be better off if &ou dont use the%# If &ou 'ant %a*i%u% $ortabi it& across -B.S $ atfor%s, stic( to core SQL#

@$ !se%0defined types
&ser-defined types '&()s* re$resent another e*a%$ e of features that arri"ed in SQLCIII that co%e fro% the ob>ect=oriented $rogra%%ing 'or d# As an SQL $rogra%%er, &ou are no onger restricted to the data t&$es defined in the SQLA44B s$ecification# 2ou can define &our o'n data t&$es, using the $rinci$ es of abstract data t&$es +A-Ts, found in such ob>ect=oriented $rogra%%ing anguages as CQ Q# One of the %ost i%$ortant benefits of H-Ts is the fact that the& can be used to e i%inate the /i%$edance %is%atch0 bet'een SQL and the host anguage that is /'ra$$ed around0 the SQL# A ong=standing $rob e% 'ith SQL has been the fact the SQLs $redefined data t&$es do not %atch the data t&$es of the host anguages 'ithin 'hich SQL state%ents are e%bedded# No', 'ith H-Ts, a database $rogra%%er can create data t&$es 'ithin SQL that %atch the data t&$es of the host anguage# A H-T has attributes and %ethods, 'hich are enca$su ated 'ithin the H-T# The outside

'or d can see the attribute definitions and the resu ts of the %ethods, but the s$ecific i%$ e%entations of the %ethods are hidden fro% "ie'# Access to the attributes and %ethods of a H-T can be further restricted b& s$ecif&ing that the& are $ub ic, $ri"ate, or $rotected# Pub ic attributes or %ethods are a"ai ab e to a users of a H-T# Pri"ate attributes or %ethods are a"ai ab e on & to the H-T itse f# Protected attributes or %ethods are a"ai ab e on & to the H-T itse f or its subt&$es# 2ou see fro% this that a H-T in SQL beha"es %uch i(e a c ass in an ob>ect=oriented $rogra%%ing anguage# T'o for%s of user=defined t&$es e*ist7 distinct t&$es and structured t&$es# @$1$ Distin*t types -istinct t&$es are the si%$ er of the t'o for%s of user=defined t&$es# A distinct t&$es defining feature is that it is e*$ressed as a sing e data t&$e# It is constructed fro% one of the $redefined data t&$es, ca ed the source type. .u ti$ e distinct t&$es that are a based on a sing e source t&$e are distinct fro% each other and are thus not direct & co%$arab e# For e*a%$ e, &ou can use distinct t&$es to distinguish bet'een different currencies# Consider the fo o'ing t&$e definition7 C+,-), (./).0C) )"1, &/dollar -/ (,C.2-L '3,#* 4 This creates a ne' data t&$e for H#S# do ars, based on the $redefined -1CI.AL data t&$e# 2ou can create another distinct t&$e in a si%i ar %anner7 C+,-), (./).0C) )"1, ,uro -/ (,C.2-L '3,#* 4 2ou can no' create tab es that use these ne' t&$es7 CR1AT1 TABL1 HSIn"oice + In"IINT1E1R PRI.AR2 N12, CustIINT1E1R, 1%$IINT1E1R, Tota Sa e HSdo ar, Ta* HSdo ar, Shi$$ing HSdo ar, ErandTota HSdo ar,< CR1AT1 TABL1 1uroIn"oice + In"IINT1E1R PRI.AR2 N12, CustIINT1E1R, 1%$IINT1E1R, Tota Sa e 1uro, Ta* 1uro, Shi$$ing 1uro, ErandTota 1uro,< The HSdo ar t&$e and the 1uro t&$e are both based on the -1CI.AL t&$e, but instances of one cannot be direct & co%$ared 'ith instances of the other or 'ith instances of the -1CI.AL t&$e# In

SQL as in the rea 'or d, it is $ossib e to con"ert H#S# do ars into 1uros, but this re)uires a s$ecia o$eration +CAST,# After the con"ersion has been %ade, co%$arisons beco%e $ossib e# @$2$ St%u*tu%ed types The second for% of user=defined t&$e, the structured t&$e, is e*$ressed as a ist of attribute definitions and %ethods instead of being based on a sing e $redefined source t&$e# Constructors 9hen &ou create a structured H-T, the -B.S auto%atica & creates a constructor function for it, gi"ing it the sa%e na%e as the H-T# The constructors >ob is to initia i?e the attributes of the H-T to their defau t "a ues# .utators and obser"ers 9hen &ou create a structured H-T, the -B.S auto%atica & creates a %utator function and an obser"er function# A mutator, 'hen in"o(ed, changes the "a ue of an attribute of a structured t&$e# An obser%er function is the o$$osite of a %utator function# Its >ob is to retrie"e the "a ue of an attribute of a structured t&$e# 2ou can inc ude obser"er functions in S1L1CT state%ents to retrie"e "a ues fro% a database# Subt&$es and su$ert&$es A hierarchica re ationshi$ can e*ist bet'een t'o structured t&$es# For e*a%$ e, a t&$e na%ed .usicC-udt has a subt&$e na%ed Roc(C-udt and another subt&$e na%ed C assica C-udt# .usicC-udt is the su$ert&$e of those t'o subt&$es# Roc(C-udt is a proper subtype of .usicC-udt if there is no subt&$e of .usicC-udt that is a su$ert&$e of Roc(C-udt# If Roc(C-udt has a subt&$e na%ed :ea"&.eta C-udt, :ea"&.eta C-udt is a so a subt&$e of .usicC-udt, but it is not a $ro$er subt&$e of .usicC-udt# A structured t&$e that has no su$ert&$e is ca ed a maximal supertype, and a structured t&$e that has no subt&$es is ca ed a leaf subtype#

!nde%stand the SQL En-i%onment


The /5L en%ironment is, )uite si%$ &, the su% of a the $arts that %a(e u$ that en"iron%ent# 1ach distinct $art, or co%$onent, 'or(s in con>unction 'ith other co%$onents to su$$ort SQL o$erations such as creating and %odif&ing ob>ects, storing and )uer&ing data, or %odif&ing and de eting that data# Ta(en together, these co%$onents for% a %ode on 'hich an R-B.S can be based# This does not i%$ &, ho'e"er, that R-B.S "endors adhere strict & to this %ode < 'hich co%$onents the& i%$ e%ent and ho' the& i%$ e%ent the% are eft, for the %ost $art, to the discretion of those "endors# 1"en so, I 'ant to $ro"ide &ou 'ith an o"er"ie' of the 'a& in 'hich the SQL en"iron%ent is defined, in ter%s of its distinct co%$onents, as the& are described in the SQL7A446 standard# The SQL en"iron%ent is %ade u$ of si* t&$es of co%$onents, as sho'n in Figure A=C# The SQL c ient and SQL ser"ers are $art of the SQL i%$ e%entation and are therefore subt&$es of that co%$onent# Notice that there is on & one SQL agent and one SQL i%$ e%entation, but there are %u ti$ e co%$onents for other t&$es, such as cata ogs and sites# According to SQLA446, there %ust be e*act & one SQL agent and SQL i%$ e%entation and ?ero or %ore SQL c ient %odu es, authori?ation identifiers, and cata ogs# The standard does not s$ecif& ho' %an& sites are su$$orted, but i%$ ies %u ti$ e sites#

The components of the SQL environment

The components of a catalog

S*hemas
1ach cata og contains one or %ore sche%as# A schema is a set of re ated ob>ects that are co ected under a co%%on na%es$ace# The sche%a acts as a container for those ob>ects, 'hich in turn store the SQL data or $erfor% other data=re ated functions# 1ach sche%a, the ob>ects contained in the sche%a, and the SQL data 'ithin those ob>ects are o'ned b& the authori?ation identifier associated 'ith that sche%a# Hn i(e cata ogs, sche%as are 'ide & i%$ e%ented in R-B.S $roducts# :o'e"er, as 'ith cata ogs, SQL ea"es %ost of the i%$ e%entation detai s u$ to the "endor, a though the standard does $ro"ide anguage for the creation and de etion of sche%as# For creating a sche%a, the CR1AT1 SC:1.A state%ent is used, and for de eting a sche%a, the -ROP SC:1.A state%ent is used# Creating and de eting sche%as are discussed in %ore detai in the /Create a Sche%a0 section# The treat%ent of sche%as in an R-B.S can "ar& 'ide & fro% the standard, and therefore, its i%$ortant that &ou read the $roduct docu%entation carefu & if &ou 'ant to create a sche%a in &our SQL en"iron%ent# For e*a%$ e, the Orac e database architecture co%bines the conce$t of a sche%a and the o'ning authori?ation identifierK'hen &ou create a user in Orac e, &ou are a so i%$ icit & creating a sche%a for that user# 9hi e Orac e CC g does contain a CR1AT1 SC:1.A state%ent for co%$atibi it& 'ith the SQL standard, it %ere & a o's &ou to e*ecute a batch of s$ecific SQL state%ents to create tab es and "ie's and grant $ri"i eges 'ithin a sche%a that a read& e*ists +that is, one that 'as a read& i%$ icit & created using the CR1AT1 HS1R state%ent,# Info%mation S*hema 1ach cata og contains a s$ecia sche%a na%ed INFOR.ATIONRSC:1.A# This sche%a contains definitions for a nu%ber of sche%a ob>ects, %ost & "ie's# A %iew is a "irtua tab e that a o's &ou to "ie' data co ected fro% actua tab es# B& using these "ie's, &ou can dis$ a& the definitions of ob>ects in that cata og as though it 'ere SQL data# 2ou cannot change an& of the data Kif &ou did &ou 'ou d be changing the ob>ect definitions the%se "esKbut &ou can dis$ a& infor%ation si%$ & b& )uer&ing the a$$ro$riate "ie'# As 'ith %ost SQL features, the i%$ e%entation of the infor%ation sche%a and 'hat functions are su$$orted "aries fro% $roduct to $roduct, a though these i%$ e%entations are usua & fair & straightfor'ard# For e*a%$ e, SQL Ser"er A44J inc udes a "ie' in the infor%ation sche%a na%ed INFOR.ATIONRSC:1.A#COLH.NS# If &ou )uer& this "ie', the resu ts 'i inc ude a ist that contains infor%ation about e"er& co u%n accessib e to the current user 'ithin the current database# The resu ts inc ude such infor%ation as the co u%n na%e, the data t&$e assigned to that co u%n, and the o'ner +authori?ation identifier, 'ho o'ns that co u%n# S*hema O/7e*ts At the botto% tier of the cata og hierarch& sit the sche%a ob>ects# The schema ob6ects are a set of re ated co%$onents that are contained 'ithin a sche%a# This is the e"e 'here the SQL data is stored and, conse)uent &, the e"e that concerns SQL $rogra%%ers the %ost# B& using SQL, &ou be ab e to define SQL ob>ects, %odif& those definitions, and store and %ani$u ate SQL data 'ithin the ob>ects# In fact, %ost of 'hat &ou be doing in this boo( fro% here on in has a direct i%$act on or is direct & connected 'ith the sche%a ob>ects# The SQLA446 standard defines CA t&$es of sche%a ob>ects# These ob>ects, described in Tab e A=A, $ro"ide the foundation for the SQL en"iron%ent and the structure for the 'a& in 'hich data is stored 'ithin that en"iron%ent# I be discussing %ost of these ob>ects in greater detai ater in the boo(< as a resu t, I"e inc uded references, 'here a$$ro$riate, to his a$$ icab e cha$ters#

CREATI"' A"D ALTERI"' TA+LES

In an SQL en"iron%ent, tab es are the basic unit of data %anage%ent# .ost SQL $rogra%%ing &ou do is re ated either direct & or indirect & to those tab es# As a resu t, before &ou can insert data into &our database or %odif& that data, the a$$ro$riate tab es %ust ha"e been created or &ou %ust create the%# The SQL7A446 standard $ro"ides three state%ents that a o' &ou to define, change, and de ete tab e definitions in an SQL en"iron%ent# 2ou can use the CR1AT1 TABL1 state%ent to add a tab e, the ALT1R TABL1 state%ent to %odif& that definition, or the -ROP TABL1 state%ent to de ete the tab e and its data fro% &our database# Of these three state%ents, the CR1AT1 TABL1 state%ent has the %ost co%$ e* s&nta*# Not on & is this because of the "arious t&$es of tab es su$$orted b& SQL, but a so because a tab e definition can inc ude %an& e e%ents#

:o'e"er, des$ite these co%$ e*ities, tab e creation is a fair & straightfor'ard $rocess, once &ou understand the basic s&nta*# Data/ase O/7e*ts An Orac e database can contain %u ti$ e data structures# 1ach structure shou d be out ined in the database design so that it can be created during the bui d stage of database de"e o$%ent# S Tab e7 Stores data S Mie'7 Subset of data fro% one or %ore tab es S Se)uence7 Nu%eric "a ue generator S Inde*7 I%$ro"es the $erfor%ance of so%e )ueries S S&non&%7 Ei"es a ternati"e na%es to ob>ects O%a*&e@i Ta/&e St%u*tu%es S Tab es can be created at an& ti%e, e"en 'hi e users are using the database# S 2ou do not need to s$ecif& the si?e of an& tab e# The si?e is u ti%ate & defined b& the a%ount of s$ace a ocated to the database as a 'ho e# It is i%$ortant, ho'e"er, to esti%ate ho' %uch s$ace a tab e 'i use o"er ti%e# S Tab e structure can be %odified on ine# "aming Ru&es Tab e na%es and co u%n na%es7 S .ust begin 'ith a etter S .ust be C to B4 characters ong S .ust contain on & APO, aP?, 4PI, R, 5, and T S .ust not du$ icate the na%e of another ob>ect o'ned b& the sa%e user S .ust not be an Orac e Ser"er reser"ed 'ord Na%es are case insensiti"e# For e*a%$ e, 1.PLO211S is treated as the sa%e na%e as e.P o&ees or e.$LO211S#

1$ CREATE TA+LE
To create a tab e, a user %ust ha"e the CR1AT1 TABL1 $ri"i ege and a storage area in 'hich to create ob>ects# /yntax7 CREATE TABLE [schema.]table (column datatype [DEFAULT expr][, ...]); where schema is the sa%e as the o'ners na%e< table is the na%e of the tab e< -1FAHLT expr s$ecifies a defau t "a ue if a "a ue is o%itted in the INS1RT state%ent< column is the na%e of the co u%n< datatype is the co u%ns data t&$e and ength< A C%eate a ta/&e /ased on anothe% ta/&e /y using a su/Bue%y

A second %ethod for creating a tab e is to a$$ & the AS subquery c ause, 'hich both creates the tab e and inserts ro's returned fro% the sub)uer&# /yntax7 CREATE TABLE table [(column, column...)] AS subquery; where table is the na%e of the tab e< column is the na%e of the co u%n, defau t "a ue, and integrit& constraint< subquery is the S1L1CT state%ent that defines the set of ro's to be inserted into the ne' tab e< 'uide&ines S The tab e is created 'ith the s$ecified co u%n na%es, and the ro's retrie"ed b& the S1L1CT state%ent are inserted into the tab e# S The co u%n definition can contain on & the co u%n na%e and defau t "a ue# S If co u%n s$ecifications are gi"en, the nu%ber of co u%ns %ust e)ua the nu%ber of co u%ns in the sub)uer& S1L1CT ist# S If no co u%n s$ecifications are gi"en, the co u%n na%es of the tab e are the sa%e as the co u%n na%es in the sub)uer&# S The integrit& ru es are not $assed onto the ne' tab e, on & the co u%n data t&$e definitions#

2$ ALTER TA+LE
After &ou create a tab e, &ou %a& need to change the tab e structure because &ou o%itted a co u%n or &our co u%n definition needs to be changed or &ou need to re%o"e co u%ns# 2ou can do this b& using the ALT1R TABL1 state%ent# /yntax7 - ALTER TABLE table ADD (column datatype [DEFAULT expr] [, column datatype]...); - ALTER TABLE table MODIFY (column datatype [DEFAULT expr] [, column datatype]...); - ALTER TABLE table DROP (column); - ALTER TABLE table RENAME COLUMN old_name TO new_name; where: table is the na%e of the tab e< A--U.O-IF2U-ROP is the t&$e of %odification< column is the na%e of the ne' co u%n< datatype is the data t&$e and ength of the ne' co u%n< -1FAHLT expr s$ecifies the defau t "a ue for a ne' co u%n< 'uide&ines fo% Adding a Co&umn

S 2ou can add or %odif& co u%ns# S 2ou cannot s$ecif& 'here the co u%n is to a$$ear# The ne' co u%n beco%es the ast co u%n# 'uide&ines fo% #odifying a Co&umn S 2ou can increase the 'idth or $recision of a nu%eric co u%n# S 2ou can increase the 'idth of nu%eric or character co u%ns# S 2ou can decrease the 'idth of a co u%n on & if the co u%n contains on & nu "a ues or if the tab e has no ro's# S 2ou can change the data t&$e on & if the co u%n contains nu "a ues# S 2ou can con"ert a C:AR co u%n to the MARC:ARA data t&$e or con"ert a MARC:ARA co u%n to the C:AR data t&$e on & if the co u%n contains nu "a ues or if &ou do not change the si?e# S A change to the defau t "a ue of a co u%n affects on & subse)uent insertions to the tab e# 'uide&ines fo% D%opping a Co&umn S The co u%n %a& or %a& not contain data# S Hsing the ALT1R TABL1 state%ent, on & one co u%n can be dro$$ed at a ti%e# S The tab e %ust ha"e at east one co u%n re%aining in it after it is a tered# S Once a co u%n is dro$$ed, it cannot be reco"ered# The SET !"!SED Option The S1T HNHS1- o$tion %ar(s one or %ore co u%ns as unused so that the& can be dro$$ed 'hen the de%and on s&ste% resources is o'er# S$ecif&ing this c ause does not actua & re%o"e the target co u%ns fro% each ro' in the tab e +that is, it does not restore the dis( s$ace used b& these co u%ns,# Therefore, the res$onse ti%e is faster than if &ou e*ecuted the -ROP c ause# Hnused co u%ns are treated as if the& 'ere dro$$ed, e"en though their co u%n data re%ains in the tab es ro's# After a co u%n has been %ar(ed as unused, &ou ha"e no access to that co u%n# A S1L1CT 8 )uer& 'i not retrie"e data fro% unused co u%ns# In addition, the na%es and t&$es of co u%ns %ar(ed unused 'i not be dis$ a&ed during a -1SCRIB1, and &ou can add to the tab e a ne' co u%n 'ith the sa%e na%e as an unused co u%n# S1T HNHS1- infor%ation is stored in the HS1RRHNHS1-RCOLRTABS dictionar& "ie'# The DRO !"!SED COL!#"S Option -ROP HNHS1- COLH.NS re%o"es fro% the tab e a co u%ns current & %ar(ed as unused# 2ou can use this state%ent 'hen &ou 'ant to rec ai% the e*tra dis( s$ace fro% unused co u%ns in the tab e# If the tab e contains no unused co u%ns, the state%ent returns 'ith no errors# /yntax7 ALTER TABLE table SET UNUSED (column); ALTER TABLE table DROP UNUSED COLUMNS;

3$ DRO TA+LE
The -ROP TABL1 state%ent re%o"es the definition of an Orac e tab e# 9hen &ou dro$ a tab e, the database oses a the data in the tab e and a the inde*es associated 'ith it# /yntax7 DROP TABLE table; "ote. The -ROP TABL1 state%ent, once e*ecuted, is irre"ersib e# The Orac e Ser"er does not )uestion the action 'hen &ou issue the -ROP TABL1 state%ent# If &ou o'n that tab e or ha"e a

high= e"e $ri"i ege, then the tab e is i%%ediate & re%o"ed# As 'ith a --L state%ents, -ROP TABL1 is co%%itted auto%atica &# 'uide&ines S A data is de eted fro% the tab e# S An& "ie's and s&non&%s re%ain but are in"a id# S An& $ending transactions are co%%itted# S On & the creator of the tab e or a user 'ith the -ROP AN2 TABL1 $ri"i ege can re%o"e a tab e#

4$ RE"A#E
Additiona DDL state%ents inc ude the RE"A#E state%ent, 'hich is used to rena%e a tab e, "ie', se)uence, or a s&non&%# 2ou %ust be the o'ner of the ob>ect that &ou rena%e# /yntax7 RENAME table_old_name TO table_new_name;

5$ TR!"CATE
Another DDL state%ent is the TRHNCAT1 TABL1 state%ent, 'hich is used to re%o"e a ro's fro% a tab e and to re ease the storage s$ace used b& that tab e# 9hen using the TRHNCAT1 TABL1 state%ent, &ou cannot ro bac( ro' re%o"a # /yntax7 TRUNCATE TABLE table_name; 2ou %ust be the o'ner of the tab e or ha"e -1L1T1 TABL1 s&ste% $ri"i eges to truncate a tab e# The -1L1T1 state%ent can a so re%o"e a ro's fro% a tab e, but it does not re ease storage s$ace# The TRHNCAT1 co%%and is faster# Re%o"ing ro's 'ith the TRHNCAT1 state%ent is faster than re%o"ing the% 'ith the -1L1T1 state%ent for the fo o'ing reasons7 S The TRHNCAT1 state%ent is a data definition anguage +--L, state%ent and generates no ro bac( infor%ation# S Truncating a tab e does not fire the de ete triggers of the tab e# S If the tab e is the $arent of a referentia integrit& constraint, &ou cannot truncate the tab e# -isab e the constraint before issuing the TRHNCAT1 state%ent#

=$ CO##E"T
2ou can add a co%%ent of u$ to A,444 b&tes about a co u%n, tab e, "ie', or sna$shot b& using the CO..1NT state%ent# /yntax7 CO##E"T O" TA+LE table C COL!#" table.column IS Dte,tDE where table is the na%e of the tab e< column is the na%e of the co u%n in a tab e< text is the te*t of the co%%ent<

2ou can dro$ a co%%ent fro% the database b& setting it to e%$t& string +VV,7 CO##E"T O" TA+LE table IS D DE

INCLUDING CONSTRAINTS
What Are Constraints? Constraints enforce rules at the table level. Constraints revent the !eletion of a table if there are !e The follo"in# constraint t$ es are vali!%
NOT NULL UNIQUE PRIMARY KEY FOREIGN KEY CHECK

en!encies.

The Orac e Ser"er uses constraints to $re"ent in"a id data entr& into tab es# 2ou can use constraints to do the fo o'ing7 S 1nforce ru es on the data in a tab e 'hene"er a ro' is inserted, u$dated, or de eted fro% that tab e# The constraint %ust be satisfied for the o$eration to succeed# S Pre"ent the de etion of a tab e if there are de$endencies fro% other tab es S Pro"ide ru es for Orac e too s, such as Orac e -e"e o$er
Data Integ%ity Const%aints Const%aint Des*%iption NOT NULL S$ecifies that the co u%n cannot contain a nu "a ue UNIQUE S$ecifies a co u%n or co%bination of co u%ns 'hose "a ues %ust be uni)ue for a ro's in the tab e PRIMARY KEY Hni)ue & identifies each ro' of the tab e FOREIGN KEY 1stab ishes and enforces a foreign (e& re ationshi$ bet'een the co u%n and a co u%n of the referenced tab e CHECK S$ecifies a condition that %ust be true

Const%aint 'uide&ines A constraints are stored in the data dictionar&# Constraints are eas& to reference if &ou gi"e the% a %eaningfu na%e# Constraint na%es %ust fo o' the standard ob>ect=na%ing ru es# If &ou do not na%e &our constraint, the Orac e ser"er generates a na%e 'ith the for%at S2SRC n, 'here n is an integer so that the constraint na%e is uni)ue# Constraints can be defined at the ti%e of tab e creation or after the tab e has been created# 2ou can "ie' the constraints defined for a s$ecific tab e b& oo(ing at the HS1RRCONSTRAINTS data dictionar& tab e#

1$ DE6I"I"' CO"STRAI"TS
Syntax: CREATE TABLE [schema.]table (column datatype [DEFAULT expr] [column_constraint], ... [table_constraint][,...]); Constraints are usua & created at the sa%e ti%e as the tab e# Constraints can be added to a tab e after its creation and a so te%$orari & disab ed# Constraints can be defined at one of t'o e"e s7 co u%n e"e and tab e e"e # Co&umn Le-e& References a sing e co u%n and is defined 'ithin a s$ecification for the o'ning co u%n< can define an& t&$e of integrit& constraint

column [CONSTRAINT constraint_name] constraint_type, Ta/&e Le-e& References one or %ore co u%ns and is defined se$arate & fro% the definitions of the co u%ns in the tab e< can define an& constraints e*ce$t NOT NHLL

column, [CONSTRAINT constraint_name] constraint_type (column, ...),

The NOT NULL constraint ensures that the co u!n contains no nu "a ues# Co u!ns $ithout the NOT NULL constraint can contain nu "a ues %& 'e(au t# The NOT NULL constraint can %e s)eci(ie' on & at the co u!n e"e * not at the ta% e e"e #

A HNIQH1 (e& integrit& constraint re)uires that e"er& "a ue in a co u%n or set of co u%ns +(e&, be uni)ue7 that is, no t'o ro's of a tab e can ha"e du$ icate "a ues in a s$ecified co u%n or set of co u%ns# The co u%n +or set of co u%ns, inc uded in the definition of the HNIQH1 (e& constraint is ca ed the unique !ey# If the HNIQH1 constraint co%$rises %ore than one co u%n, that grou$ of co u%ns is ca ed a composite unique !ey# HNIQH1 constraints a o' the in$ut of nu s un ess &ou a so define NOT NHLL constraints for the sa%e co u%ns# In fact, an& nu%ber of ro's can inc ude nu s for co u%ns 'ithout NOT NHLL constraints because nu s are not considered e)ua to an&thing# A nu in a co u%n +or in a co u%ns of a co%$osite HNIQH1 (e&, a 'a&s satisfies a HNIQH1 constraint# "ote. Because of the search %echanis% for HNIQH1 constraints on %ore than one co u%n, &ou cannot ha"e identica "a ues in the non=nu co u%ns of a $artia & nu co%$osite HNIQH1 (e& constraint# HNIQH1 constraints can be defined at the co u%n or tab e e"e # A co%$osite uni)ue (e& is created b& using the tab e e"e definition# "ote. The Orac e Ser"er enforces the HNIQH1 constraint b& i%$ icit & creating a uni)ue inde* on the uni)ue (e& co u%n or co u%ns#

A PRI.AR2 N12 constraint creates a $ri%ar& (e& for the tab e# On & one $ri%ar& (e& can be created for a each tab e# The PRI.AR2 N12 constraint is a co u%n or set of co u%ns that uni)ue & identifies each ro' in a tab e# This constraint enforces uni)ueness of the co u%n or co u%n co%bination and ensures that no co u%n that is $art of the $ri%ar& (e& can contain a nu "a ue# PRI.AR2 N12 constraints can be defined at the co u%n e"e or tab e e"e # A co%$osite PRI.AR2 N12 is created b& using the tab e= e"e definition# A tab e can ha"e on & one PRI.AR2 N12 constraint but can ha"e se"era HNIQH1 constraints# "ote. A HNIQH1 inde* is created auto%atica & for a PRI.AR2 N12 co u%n# CREATE TABLE !"#$%&!'%(( !"#$%&!'%)* NUMBER(+), !"#$%&!'%)'#&! ,ARCHAR-(./) CONSTRAINT !"%)'#&!)'' NOT NULL, &#'#0!$)* NUMBER(1), 234#%*3')* NUMBER(+), CONSTRAINT !"%)* )"5 PRIMARY KEY( !"#$%&!'%)* ));

The FOR1IEN N12, or referentia integrit& constraint, designates a co u%n or co%bination of co u%ns as a foreign (e& and estab ishes a re ationshi$ bet'een a $ri%ar& (e& or a uni)ue (e& in the sa%e tab e or a different tab e# In the e*a%$ e on the s ide, -1PART.1NTRI- has been defined as the foreign (e& in the 1.PLO211S tab e +de$endent or chi d tab e,< it references the -1PART.1NTRI- co u%n of the -1PART.1NTS tab e +the referenced or $arent tab e,# A foreign (e& "a ue %ust %atch an e*isting "a ue in the $arent tab e or be NHLL# Foreign (e&s are based on data "a ues and are $ure & ogica , not $h&sica , $ointers# FOR1IEN N12 constraints can be defined at the co u%n or tab e constraint e"e # A co%$osite foreign (e& %ust be created b& using the tab e= e"e definition# CREATE TABLE !&"236!!(( !&"236!!)* NUMBER(1), 2#(%)'#&! ,ARCHAR-(-7) NOT NULL, !&#*2 ,ARCHAR-(-7), (#2#$6 NUMBER(8,-), 43&&*((*3')"4% NUMBER(-,-), 9*$!) #%! DATE NOT NULL, ... !"#$%&!'%)* NUMBER(+), CONSTRAINT !&") !"%):5 FOREIGN KEY ( !"#$%&!'%)* ) REFERENCES !"#$%&!'%(( !"#$%&!'%)* ), CONSTRAINT !&")!&#*2);5 UNIQUE(!&#*2));

The foreign (e& can a so be defined at the co u%n e"e , $ro"ided the constraint is based on a sing e co u%n# The s&nta* differs in that the (e&'ords FOR1IEN N12 do not a$$ear# For e*a%$ e7
CREATE TABLE !&"236!!( (... !"#$%&!'%)* NUMBER(+) CONSTRAINT !&") !"%* ):5 REFERENCES !"#$%&!'%(( !"#$%&!'%)* ), ...)

The foreign (e& is defined in the chi d tab e, and the tab e containing the referenced co u%n is the $arent tab e# The foreign (e& is defined using a co%bination of the fo o'ing (e&'ords7 S FOR1IEN N12 is used to define the co u%n in the chi d tab e at the tab e constraint e"e # S R1F1R1NC1S identif& the tab e and co u%n in the $arent tab e# S ON -1L1T1 CASCA-1 indicates that 'hen the ro' in the $arent tab e is de eted, the de$endent ro's in the chi d tab e 'i a so be de eted# S ON -1L1T1 S1T NHLL con"erts foreign (e& "a ues to nu 'hen the $arent "a ue is re%o"ed# The defau t beha"ior is ca ed the restrict ru e, 'hich disa o's the u$date or de etion of referenced data# 9ithout the ON -1L1T1 CASCA-1 or the ON -1L1T1 S1T NHLL o$tions, the ro' in the $arent tab e cannot be de eted if it is referenced in the chi d tab e#

The CHECK Constraint


The C:1CN constraint defines a condition that each ro' %ust satisf&# The condition can use the sa%e constructs as )uer& conditions, 'ith the fo o'ing e*ce$tions7 S References to the CHRRMAL, N1WTMAL, L1M1L, and RO9NH. $seudoco u%ns S Ca s to S2S-AT1, HI-, HS1R, and HS1R1NM functions S Queries that refer to other "a ues in other ro's A sing e co u%n can ha"e %u ti$ e C:1CN constraints 'hich reference the co u%n in its definition# There is no i%it to the nu%ber of C:1CN constraints 'hich &ou can define on a co u%n# C:1CN constraints can be defined at the co u%n e"e or tab e e"e $ 1*a%$ e7
CREATE TABLE !&"236!!( (... (#2#$6 NUMBER(8,-) CONSTRAINT !&")(#2#$6)&*' CHECK ((#2#$6 < /), ...

2$ ADDI"' A CO"STRAI"T
2ou can add a constraint for e*isting tab es b& using the ALTER TA+LE state%ent 'ith the ADD c ause#

/yntax7 ALTER TABLE table ADD [CONSTRAINT constraint] type (column); where: table is the na%e of the tab e< constraint is the na%e of the constraint< type is the constraint t&$e< column is the na%e of the co u%n affected b& the constraint< The constraint na%e s&nta* is o$tiona , a though reco%%ended# If &ou do not na%e &our constraints, the s&ste% 'i generate constraint na%es# 'uide&ines S 2ou can add, dro$, enab e, or disab e a constraint, but &ou cannot %odif& its structure# S 2ou can add a NOT NHLL constraint to an e*isting co u%n b& using the .O-IF2 c ause of the ALT1R TABL1 state%ent# "ote. 2ou can define a NOT NHLL co u%n on & if the tab e is e%$t& or if the co u%n has a "a ue for e"er& ro'# 1*a%$ e7 ALTER TABLE !&"236!!( ADD CONSTRAINT !&")&#'#0!$):5 FOREIGN KEY(&#'#0!$)* ) REFERENCES !&"236!!((!&"236!!)* ); The constraint ensures that a %anager e*ists as a "a id e%$ o&ee in the 1.PLO211S tab e#

3$ DRO

I"' CO"STRAI"TS

To dro$ a constraint, &ou can identif& the constraint na%e fro% the HS1RRCONSTRAINTS and HS1RRCONSRCOLH.NS data dictionar& "ie's# Then use the ALT1R TABL1 state%ent 'ith the -ROP c ause# The CASCA-1 o$tion of the -ROP c ause causes an& de$endent constraints a so to be dro$$ed# /yntax7
ALTER TABLE table DROP PRIMARY KEY = UNIQUE (column) = CONSTRAINT constraint [CASCADE];

1*a%$ e7 ALTER TABLE !"#$%&!'%( DROP PRIMARY KEY CASCADE; This 'i Re%o"e the PRI.AR2 N12 constraint on the -1PART.1NTS tab e and dro$ the associated FOR1IEN N12 constraint on the 1.PLO211S#-1PART.1NTRI- co u%n#

4$ DISA+LI"' CO"STRAI"TS

2ou can disab e a constraint 'ithout dro$$ing it or re=creating it b& using the ALT1R TABL1 state%ent 'ith the -ISABL1 c ause# /yntax7
ALTER TABLE table DISABLE CONSTRAINT constraint [CASCADE];

'uide&ines S 2ou can use the -ISABL1 c ause in both the CR1AT1 TABL1 state%ent and the ALT1R TABL1 state%ent# S The CASCA-1 c ause disab es de$endent integrit& constraints# S -isab ing a uni)ue or $ri%ar& (e& constraint re%o"es the uni)ue inde*#

5$ E"A+LI"' CO"STRAI"TS
2ou can enab e a constraint 'ithout dro$$ing it or re=creating it b& using the ALT1R TABL1 state%ent 'ith the 1NABL1 c ause# /yntax7
ALTER TABLE table ENABLE CONSTRAINT constraint;

'uide&ines S If &ou enab e a constraint, that constraint a$$ ies to a the data in the tab e# A the data in the tab e %ust fit the constraint# S If &ou enab e a HNIQH1 (e& or PRI.AR2 N12 constraint, a HNIQH1 or PRI.AR2 N12 inde* is created auto%atica &# S 2ou can use the 1NABL1 c ause in both the CR1AT1 TABL1 state%ent and the ALT1R TABL1 state%ent# S 1nab ing a $ri%ar& (e& constraint that 'as disab ed 'ith the CASCA-1 o$tion does not enab e an& foreign (e&s that are de$endent u$on the $ri%ar& (e&#

=$ CASCADE CO"STRAI"TS
S The CASCA-1 CONSTRAINTS c ause is used a ong 'ith the -ROP COLH.N c ause# S The CASCA-1 CONSTRAINTS c ause dro$s a referentia integrit& constraints that refer to the $ri%ar& and uni)ue (e&s defined on the dro$$ed co u%ns# S The CASCA-1 CONSTRAINTS c ause a so dro$s a %u tico u%n constraints defined on the dro$$ed co u%ns#

1*a%$ e7
CREATE TABLE %!(%> ( "5 NUMBER PRIMARY KEY, :5 NUMBER, 432> NUMBER, 432- NUMBER,

CONSTRAINT :5)43'(%$#*'% FOREIGN KEY (:5) REFERENCES %!(%>, CONSTRAINT 45> CHECK ("5 < / #' 432> < /), CONSTRAINT 45- CHECK (432- < /));

Sub%itting the fo o'ing state%ent dro$s co u%n PN, the p%ima%y Fey constraint, the fFG*onst%aint foreign (e& constraint, and the chec( constraint, 45>7
ALTER TABLE %!(%> DROP ("5) CASCADE CONSTRAINTS;

CREATI"' A"D #ODI69I"' (IEWS

What Is a (ieHI 2ou can $resent ogica subsets or co%binations of data b& creating "ie's of tab es# A "ie' is a ogica tab e based on a tab e or another "ie'# A "ie' contains no data of its o'n but is i(e a 'indo' through 'hich data fro% tab es can be "ie'ed or changed# The tab es on 'hich a "ie' is based are ca ed base tab es# The "ie' is stored as a S1L1CT state%ent in the data dictionar&# Why !se (ieHsI S Mie's restrict access to the data because the "ie' can dis$ a& se ecti"e co u%ns fro% the tab e# S Mie's can be used to %a(e si%$ e )ueries to retrie"e the resu ts of co%$ icated )ueries# For e*a%$ e, "ie's can be used to )uer& infor%ation fro% %u ti$ e tab es 'ithout the user (no'ing ho' to 'rite a >oin state%ent# S Mie's $ro"ide data inde$endence for ad hoc users and a$$ ication $rogra%s# One "ie' can be used to retrie"e data fro% se"era tab es# S Mie's $ro"ide grou$s of users access to data according to their $articu ar criteria# There are t'o c assifications for "ie's7 si%$ e and co%$ e*# The basic difference is re ated to the -.L +INS1RT, HP-AT1, and -1L1T1, o$erations# S A simp&e "ie' is one that7

P -eri"es data fro% on & one tab e P Contains no functions or grou$s of data P Can $erfor% -.L o$erations through the "ie' S A *omp&e, "ie' is one that7 P -eri"es data fro% %an& tab es P Contains functions or grou$s of data P -oes not a 'a&s a o' -.L o$erations through the "ie' /yntax7 CREATE [OR REPLACE] [FORCE=NOFORCE] ,IE? !iew [(alias[, alias]...)] AS subquery [?ITH CHECK OPTION [CONSTRAINT constraint]] [?ITH READ ONLY [CONSTRAINT constraint]]; where: OR REPLACE FORCE NOFORCE !iew alias re=creates the "ie' if it a read& e*ists< creates the "ie' regard ess of 'hether or not the base tab es e*ist< creates the "ie' on & if the base tab es e*ist +This is the defau t,< is the na%e of the "ie'< s$ecifies na%es for the e*$ressions se ected b& the "ie's )uer& +The nu%ber of a iases %ust %atch the nu%ber of e*$ressions se ected b& the "ie'#,< subquery is a co%$ ete S1L1CT state%ent +2ou can use a iases for the co u%ns in the S1L1CT ist#,< ?ITH CHECK OPTION s$ecifies that on & ro's accessib e to the "ie' can be inserted or u$dated< constraint is the na%e assigned to the C:1CN OPTION constraint< ?ITH READ ONLY ensures that no -.L o$erations can be $erfor%ed on this "ie'< Euide ines for creating a "ie'7 S The sub)uer& that defines a "ie' can contain co%$ e* S1L1CT s&nta*, inc uding >oins, grou$s, and sub)ueries# S The sub)uer& that defines the "ie' cannot contain an OR-1R B2 c ause# The OR-1R B2 c ause is s$ecified 'hen &ou retrie"e data fro% the "ie'# S If &ou do not s$ecif& a constraint na%e for a "ie' created 'ith the 9IT: C:1CN OPTION, the s&ste% assigns a defau t na%e in the for%at S2SRCn# S 2ou can use the OR R1PLAC1 o$tion to change the definition of the "ie' 'ithout dro$$ing and recreating it or regranting ob>ect $ri"i eges $re"ious & granted on it# S 2ou can contro the co u%n na%es b& inc uding co u%n a iases 'ithin the sub)uer&# 2ou can retrie"e data fro% a "ie' as &ou 'ou d fro% an& tab e# 2ou can dis$ a& either the contents of the entire "ie' or >ust s$ecific ro's and co u%ns# 9hen &ou access data using a "ie', the Orac e Ser"er $erfor%s the fo o'ing o$erations7 C# It retrie"es the "ie' definition fro% the data dictionar& tab e HS1RRMI19S# A# It chec(s access $ri"i eges for the "ie' base tab e# B# It con"erts the "ie' )uer& into an e)ui"a ent o$eration on the under &ing base tab e or tab es# In

other 'ords, data is retrie"ed fro%, or an u$date is %ade to, the base tab es#

#odifying a (ieH
9ith the OR R1PLAC1 o$tion, a "ie' can be created e"en if one e*ists 'ith this na%e a read&, thus re$ acing the o d "ersion of the "ie' for its o'ner# This %eans that the "ie' can be a tered 'ithout dro$$ing, re=creating, and regranting ob>ect $ri"i eges# "ote. 9hen assigning co u%n a iases in the CR1AT1 MI19 c ause, re%e%ber that the a iases are isted in the sa%e order as the co u%ns in the sub)uer&#

C%eating an *omp&e, -ieH


1*a%$ e7 Create a co%$ e* "ie' that contains grou$ functions to dis$ a& "a ues fro% t'o tab es# CREATE ,IE? !"%)(;&)@; ('#&!, &*'(#2, &#A(#2, #@0(#2) AS SELECT . !"#$%&!'%)'#&!, MIN(!.(#2#$6), MAB(!.(#2#$6), A,G(!.(#2#$6) FROM !&"236!!( !, !"#$%&!'%( ?HERE !. !"#$%&!'%)* C . !"#$%&!'%)* GROUP BY . !"#$%&!'%)'#&!;

e%fo%ming D#L Ope%ations on a (ieH


2ou can $erfor% -.L o$erations on data through a "ie' if those o$erations fo o' certain ru es# 2ou cannot %emo-e a ro' if the "ie' contains the fo o'ing7 P Erou$ functions< P A EROHP B2 c ause< P The -ISTINCT (e&'ord< P The $seudoco u%n RO9NH. (e&'ord< 2ou cannot modify data in a "ie' if it contains7 P Erou$ functions< P A EROHP B2 c ause< P The -ISTINCT (e&'ord< P The $seudoco u%n RO9NH. (e&'ord< P Co u%ns defined b& e*$ressions +e+, (#2#$6 J12,< 2ou cannot add data th%ough a -ieH if the "ie' inc udes7 P Erou$ functions< P A EROHP B2 c ause< P The -ISTINCT (e&'ord< P The $seudoco u%n RO9NH. (e&'ord< P Co u%ns defined b& e*$ressions< P NOT NHLL co u%ns in the base tab es that are not se ected b& the "ie'<

!sing the WIT8 C8EC) O TIO" C&ause


It is $ossib e to $erfor% referentia integrit& chec(s through "ie's# 2ou can a so enforce constraints at the database e"e # The "ie' can be used to $rotect data integrit&, but the use is "er& i%ited# The 9IT: C:1CN OPTION c ause s$ecifies that INS1RTs and HP-AT1s $erfor%ed through the "ie' cannot create ro's 'hich the "ie' cannot se ect, and therefore it a o's integrit& constraints and data "a idation chec(s to be enforced on data being inserted or u$dated# If there is an atte%$t to $erfor% -.L o$erations on ro's that the "ie' has not se ected, an error is dis$ a&ed, 'ith the constraint na%e if that has been s$ecified# 1*a%$ e7 CREATE OR REPLACE ,IE? !&"@;-/ AS SELECT D FROM !&"236!!( ?HERE !"#$%&!'%)* C -/ ?ITH CHECK OPTION CONSTRAINT !&"@;-/)45;
UPDATE !&"@;-/ SET !"#$%&!'%)* ?HERE !&"236!!)* UPDATE !&"@;-/ C >/ C -/>;

ERROR #% 2*'! >E ORA-/>+/-E @*!F ?ITH CHECK OPTION F9!$!-42#;(! @*32#%*3'

"ote. No ro's are u$dated because if the de$art%ent nu%ber 'ere to change to C4, the "ie' 'ou d no onger be ab e to see that e%$ o&ee# Therefore, 'ith the 9IT: C:1CN OPTION c ause, the "ie' can see on & e%$ o&ees in de$art%ent A4 and does not a o' the de$art%ent nu%ber for those e%$ o&ees to be changed through the "ie'#

Denying D#L Ope%ations


2ou can ensure that no -.L o$erations occur on &our "ie' b& creating it 'ith the WIT8 READ O"L9 o$tion# An& atte%$t to $erfor% a -.L on an& ro' in the "ie' resu ts in an Orac e ser"er error# 1*a%$ e7 CREATE OR REPLACE ,IE? (!&"236!!)';&G!$, AS SELECT !&"236!!)* , FROM !&"236!!( ?HERE !"#$%&!'%)* ?ITH READ ONLY; !&"@;>/ !&"236!!)'#&!, H3G)%*%2!) 2#(%)'#&!, H3G)* C >/

An& atte%$ts to re%o"e a ro' fro% a "ie' 'ith a read=on & constraint resu ts in an error# DELETE FROM !&"@;>/ ?HERE !&"236!!)';&G!$ C -//; DELETE FROM !&"@;>/ 8
ERROR at &ine 1. ORA0K1>52. *annot de&ete f%om -ieH Hithout e,a*t&y one Fey 0 p%ese%-ed ta/&e

Remo-ing a (ieH
2ou use the DRO (IEW state%ent to re%o"e a "ie'# The state%ent re%o"es the "ie' definition fro% the database# -ro$$ing "ie's has no effect on the tab es on 'hich the "ie' 'as based# Mie's or other a$$ ications based on de eted "ie's beco%e in"a id# On & the creator or a user 'ith the -ROP AN2 MI19 $ri"i ege can re%o"e a "ie'#

In&ine (ieHs
An in ine "ie' is created b& $ acing a sub)uer& in the FRO. c ause and gi"ing that sub)uer& an a ias# The sub)uer& defines a data source that can be referenced in the %ain )uer&# 1*a%$ e7 SELECT #.432;&'>, #.432;&'-, #.432;&'., G.#2*#( FROM %#G2!> #, (SELECT 432;&'., &#A(432;&'+) #2*#( FROM %#G2!GROUP BY 432;&'.) G ?HERE #.432;&'.C G.432;&'. AND #.432;&'- I G.#2*#(;

CONCLUSIONS:
What Is a (ieHI A "ie' is based on a tab e or another "ie' and acts as a 'indo' through 'hich data on tab es can be "ie'ed or changed# A "ie' does not contain data# The definition of the "ie' is stored in the data dictionar&# 2ou can see the definition of the "ie' in the HS1RRMI19S data dictionar& tab e# Ad-antages of (ieHs S Restrict database access S Si%$ if& )ueries S Pro"ide data inde$endence S Pro"ide %u ti$ e "ie's of the sa%e data S Can be re%o"ed 'ithout affecting the under &ing data (ieH Options S Can be a si%$ e "ie', based on one tab e S Can be a co%$ e* "ie' based on %ore than one tab e or can contain grou$s of functions S Can re$ ace other "ie's 'ith the sa%e na%e S Can contain a chec( constraint S Can be read=on &

SEQ!E"CES
What Is a SeBuen*eI A se)uence is a user created database ob>ect that can be shared b& %u ti$ e users to generate uni)ue integers# A t&$ica usage for se)uences is to create a $ri%ar& (e& "a ue, 'hich %ust be uni)ue for each ro'# The se)uence is generated and incre%ented +or decre%ented, b& an interna Orac e routine# This can be a ti%esa"ing ob>ect because it can reduce the a%ount of a$$ ication code needed to 'rite a se)uence=generating routine# Se)uence nu%bers are stored and generated inde$endent & of tab es# Therefore, the sa%e se)uence can be used for %u ti$ e tab es#

C%eating a SeBuen*e
/yntax7 CREATE SEQUENCE sequence [INCREMENT BY n] [START ?ITH n] [JMAB,ALUE n = NOMAB,ALUEK] [JMIN,ALUE n = NOMIN,ALUEK] [JCYCLE = NOCYCLEK] [JCACHE n = NOCACHEK]; where7 sequence is the na%e of the se)uence generator INCREMENT BY n s$ecifies the inter"a bet'een se)uence nu%bers 'here n is an integer START ?ITH n MAB,ALUE n NOMAB,ALUE MIN,ALUE n NOMIN,ALUE CYCLE = NOCYCLE CACHE n = NOCACHE
+If this c ause is o%itted, the se)uence incre%ents b& C, s$ecifies the first se)uence nu%ber to be generated +If this c ause is o%itted, the se)uence starts 'ith C, s$ecifies the %a*i%u% "a ue the se)uence can generate s$ecifies a %a*i%u% "a ue of C4XAG for an ascending se)uence and PC for a descending se)uence +This is the defau t o$tion#, s$ecifies the %ini%u% se)uence "a ue s$ecifies a %ini%u% "a ue of C for an ascending se)uence and P +C4XA6, for a descending se)uence +This is the defau t o$tion#, s$ecifies 'hether the se)uence continues to generate "a ues after reaching its %a*i%u% or %ini%u% "a ue +NOCYCLE is the defau t o$tion#, s$ecifies ho' %an& "a ues the Orac e Ser"er $rea ocates and (ee$s in %e%or& +B& defau t, the Orac e Ser"er caches A4 "a ues#,

"ote. The se)uence is not tied to a tab e# Eenera &, &ou shou d na%e the se)uence after its intended use< ho'e"er the se)uence can be used an&'here, regard ess of its na%e#

!sing a SeBuen*e
After &ou create &our se)uence, it generates se)uentia nu%bers for use in &our tab es# Reference the se)uence "a ues b& using the "ELT(AL and C!RR(AL $seudoco u%ns#

"ELT(AL and C!RR(AL seudo*o&umns


The N1WTMAL $seudoco u%n is used to e*tract successi"e se)uence nu%bers fro% a s$ecified se)uence# 2ou %ust )ua if& N1WTMAL 'ith the se)uence na%e# 9hen &ou reference sequence#N1WTMAL, a ne' se)uence nu%ber is generated and the current se)uence nu%ber is $ aced in CHRRMAL# The CHRRMAL $seudoco u%n is used to refer to a se)uence nu%ber that the current user has >ust generated# N1WTMAL %ust be used to generate a se)uence nu%ber in the current users session before CHRRMAL can be referenced# 2ou %ust )ua if& CHRRMAL 'ith the se)uence na%e# 9hen sequence#CHRRMAL is referenced, the ast "a ue returned to that users $rocess is dis$ a&ed#

Ru&es fo% !sing "ELT(AL and C!RR(AL


9ou *an use N1WTMAL and CHRRMAL in the fo o'ing conte*ts7 S The S1L1CT ist of a S1L1CT state%ent that is not $art of a sub)uer&< S The S1L1CT ist of a sub)uer& in an INS1RT state%ent< S The MALH1S c ause of an INS1RT state%ent< S The S1T c ause of an HP-AT1 state%ent< 9ou *annot use N1WTMAL and CHRRMAL in the fo o'ing conte*ts7 S The S1L1CT ist of a "ie'< S A S1L1CT state%ent 'ith the -ISTINCT (e&'ord< S A S1L1CT state%ent 'ith EROHP B2, :AMINE, or OR-1R B2 c auses< S A sub)uer& in a S1L1CT, -1L1T1, or HP-AT1 state%ent< S The -1FAHLT e*$ression in a CR1AT1 TABL1 or ALT1R TABL1 state%ent<

#odifying a SeBuen*e
2ou can Change the incre%ent "a ue, %a*i%u% "a ue, %ini%u% "a ue, c&c e o$tion, or cache o$tion# If &ou reach the .AWMALH1 i%it for &our se)uence, no additiona "a ues fro% the se)uence are a ocated and &ou 'i recei"e an error indicating that the se)uence e*ceeds the .AWMALH1# To continue to use the se)uence, &ou can %odif& it b& using the ALT1R S1QH1NC1 state%ent# /yntax7 ALTER SEQUENCE (!L;!'4! [INCREMENT BY '] [JMAB,ALUE ' = NOMAB,ALUEK] [JMIN,ALUE ' = NOMIN,ALUEK] [JCYCLE = NOCYCLEK] [JCACHE ' = NOCACHEK]; 'uide&ines fo% #odifying SeBuen*es S 2ou %ust be the o'ner or ha"e the ALT1R $ri"i ege for the se)uence in order to %odif& it# S On & future se)uence nu%bers are affected b& the ALT1R S1QH1NC1 state%ent# S The START 9IT: o$tion cannot be changed using ALT1R S1QH1NC1# The se)uence %ust be dro$$ed and re=created in order to restart the se)uence at a different nu%ber# S So%e "a idation is $erfor%ed# For e*a%$ e, a ne' .AWMALH1 that is ess than the current se)uence nu%ber cannot be i%$osed#

Remo-ing a SeBuen*e
To re%o"e a se)uence fro% the data dictionar&, use the dro$ se)uence state%ent# 2ou %ust be the o'ner of the se)uence or ha"e the dro$ an& se)uence $ri"i ege to re%o"e it# /yntax7 DROP SEQUENCE (!L;!'4!;

Potrebbero piacerti anche