Anda di halaman 1dari 12

SQL DML and DDL SQL can be divided into two parts: The Data Manipulation Language (DML)

and the Data Definition Language (DDL). The quer and update co!!ands for! the DML part of SQL:

SELECT " e#tracts data fro! a database UPDATE " updates data in a database DELETE " deletes data fro! a database INSERT INTO " inserts new data into a database

The DDL part of SQL per!its database tables to be created or deleted. $t also defines inde#es (%e s)& specifies lin%s between tables& and i!poses constraints between tables. The !ost i!portant DDL state!ents in SQL are:

CREATE DATABASE " creates a new database ALTER DATABASE " !odifies a database CREATE TABLE " creates a new table ALTER TABLE " !odifies a table DROP TABLE " deletes a table CREATE INDEX " creates an inde# (search %e ) DROP INDEX " deletes an inde#

Oracle Built in Functions There are two t pes of functions in 'racle. ! Sin"le Ro# Functions$ Single row or Scalar functions return a value for ever row that is processed in a quer . %! &rou' Functions$ These functions group the rows of data based on the values returned b the quer . This is discussed in SQL ()'*+ ,unctions. The group functions are used to calculate aggregate values li%e total or average& which return -ust one total or one average value after processing a group of rows. here are four t pes of single row functions. The are: ! Nu(eric Functions$ These are functions that accept nu!eric input and return nu!eric values. %! C)aracter or Te*t Functions$ These are functions that accept character input and can return both character and nu!ber values. +! Date Functions$ These are functions that ta%e values that are of datat pe D.T/ as input and return values of datat pe D.T/& e#cept for the M'0T1S23/T4//0 function& which returns a nu!ber. ,! Con-ersion Functions$ These are functions that help us to convert a value in one for! to another for!. ,or /#a!ple: a null value into an actual value& or a value fro! one datat pe to another datat pe li%e 05L& T'261.)& T'20*M3/)& T'2D.T/ etc.

7ou can co!bine !ore than one function together in an e#pression. This is %nown as nesting of functions. .)at is a DUAL Ta/le in Oracle0 This is a single row and single colu!n du!! table provided b oracle. This is used to perfor! !athe!atical calculations without using a table. Select 8 fro! D*.L Out'ut$ D*MM7 """"""" 9 Select ::: 8 ;;; fro! Dual Out'ut$ ::: 8 ;;; """"""""" <;==:< ! Nu(eric Functions$ 0u!eric functions are used to perfor! operations on nu!bers. The accept nu!eric values as input and return nu!eric values as output. ,ew of the 0u!eric functions are: ,unction 0a!e .3S (#) 6/$L (#) )eturn 5alue .bsolute value of the nu!ber >x> $nteger value that is (reater than or equal to the nu!ber >x> $nteger value that is Less than or equal to the nu!ber > x>

,L'') (#) T)*06 (#& Truncates value of nu!ber >x> up to >y> deci!al places ) )'*0D (#& )ounded off value of the nu!ber >x> up to the nu!ber >y> ) deci!al places

The following e#a!ples e#plains the usage of the above nu!eric functions Function Na(e .3S (#) 6/$L (#) E*a('les .3S (?) .3S ("?) 6/$L (@.;A) 6/$L (@.B=) Return 1alue ? "? A A

6/$L ("?.<) "? ,L'') (@.;A) @ ,L'') (#) ,L'') (@.B=) @ ,L'') ("?.<) "@ )'*0D (?@C.BC<& ?) ?@C.B )'*0D T)*06 (#& ) ?@C (?@C.BC<& D) ?@D )'*0D (?@B.BC<& "?) T)*06 (?BD.@AB& ?BD.@A @) CB )'*0D (#& ) T)*06 ("CB& ?) C T)*06 (C.:) ?BD T)*06 (?B@& "?) These functions can be used on database colu!ns. ,or /#a!ple: Let>s consider the product table used in sql -oins. 4e can use )'*0D to round off the unit2price to the nearest integer& if an product has prices in fraction. S/L/6T )'*0D (unit2price) ,)'M productE %! C)aracter or Te*t Functions$ 6haracter or te#t functions are used to !anipulate te#t strings. The accept strings or characters as input and can return both character and nu!ber values as output. ,ew of the character or te#t functions are as given below: Function Na(e L'4/) (string2value) *++/) (string2value) $0$T6.+ (string2value) LT)$M (string2value& tri!2te#t) )T)$M (string2value& tri!2te#t) T)$M (tri!2te#t ,)'M string2value) S*3ST) (string2value& !& n) L/0(T1 Return 1alue .ll the letters in 'string_value' is converted to lowercase. .ll the letters in 'string_value' is converted to uppercase. .ll the letters in 'string_value' is converted to !i#ed case. .ll occurrences of 'trim_text' is re!oved fro! the left of 'string_value'. .ll occurrences of 'trim_text' is re!oved fro! the right of 'string_value' . .ll occurrences of 'trim_text' fro! the left and right of 'string_value' & 'trim_text' can also be onl one character long . )eturns 'n' nu!ber of characters fro! 'string_value' starting fro! the >m' position. 0u!ber of characters in 'string_value' in returned.

(string2value) L+.D (string2value& n& pad2value) )+.D (string2value& n& pad2value)

)eturns >string_value' left"padded with 'pad_value' . The length of the whole string will be of 'n' characters. )eturns >string_value' right"padded with 'pad_value' . The length of the whole string will be of 'n' characters.

,or /#a!ple& we can use the above *++/)() te#t function with the colu!n value as follows. S/L/6T *++/) (product2na!e) ,)'M productE The following e#a!ples e#plains the usage of the above character or te#t functions Function Na(e L'4/)(string2value) *++/)(string2value) $0$T6.+(string2value) LT)$M(string2value& tri!2te#t) )T)$M (string2value& tri!2te#t) T)$M (tri!2te#t ,)'M string2value) S*3ST) (string2value& !& n) L/0(T1 (string2value) L+.D (string2value& n& pad2value) )+.D (string2value& n& pad2value) +! Date Functions$ These are functions that ta%e values that are of datat pe D.T/ as input and return values of datat pes D.T/& e#cept for the M'0T1S23/T4//0 function& which returns a nu!ber as output. ,ew date functions are as given below. Function Na(e .DD2M'0T1S (date& n) M'0T1S23/T4//0 (#?& #@) Return 1alue )eturns a date value after adding 'n' !onths to the date 'x'. )eturns the nu!ber of !onths between dates #? and #@. E*a('les L'4/)(>(ood Morning>) *++/)(>(ood Morning>) $0$T6.+(>(''D M')0$0(>) LT)$M (>(ood Morning>& >(ood) )T)$M (>(ood Morning>& > Morning>) T)$M (>o> ,)'M >(ood Morning>) S*3ST) (>(ood Morning>& <& :) L/0(T1 (>(ood Morning>) L+.D (>(ood>& <& >8>) )+.D (>(ood>& <& >8>) Return 1alue good !orning (''D M')0$0( (ood Morning Morning (ood (d Mrning Morning ?@ 88(ood (ood88

)'*0D (#& date2for!at) T)*06 (#& date2for!at) 0/9T2D.7 (#& wee%2da ) L.ST2D.7 (#) S7SD.T/ 0/42T$M/ (#& Fone?& Fone@)

)eturns the date 'x' rounded off to the nearest centur & ear& !onth& date& hour& !inute& or second as specified b the 'date_format'. )eturns the date 'x' lesser than or equal to the nearest centur & ear& !onth& date& hour& !inute& or second as specified b the >date2for!at>. )eturns the ne#t date of the 'week_day' on or after the date 'x' occurs. $t is used to deter!ine the nu!ber of da s re!aining in a !onth fro! the date 'x' specified. )eturns the s ste!s current date and ti!e. )eturns the date and ti!e in Fone@ if date >#> represents the ti!e in Fone?.

The below table provides the e#a!ples for the above functions Function Na(e E*a('les .DD2M'0T1S ( ) M'0T1S23/T4// 0( ) 0/9T2D.7( ) L.ST2D.7( ) 0/42T$M/( ) Return 1alue ?<"Dec";?

.DD2M'0T1S (>?<"Sep";?>& A) M'0T1S23/T4//0 (>?<"Sep";?>& >?<" A Dec";?>) 0/9T2D.7 (>D?"Gun"D;>& >4ednesda >) DB"G*0"D; L.ST2D.7 (>D?"Gun"D;>) AD"Gun"D; 0/42T$M/ (>D?"Gun"D;>& >$ST>& >/ST>) A?"Ma "D;

,! Con-ersion Functions$ These are functions that help us to convert a value in one for! to another for!. ,or /#: a null value into an actual value& or a value fro! one datat pe to another datat pe li%e 05L& T'261.)& T'20*M3/)& T'2D.T/. ,ew of the conversion functions available in oracle are: Function Na(e T'261.) (# H& I) T'2D.T/ (# H& date2for!atI) 05L (#& ) D/6'D/ (a& b& c& d& e& default2value) Return 1alue 6onverts 0u!eric and Date values to a character string value. $t cannot be used for calculations since it is a string value. 6onverts a valid 0u!eric and 6haracter values to a Date value. Date is for!atted to the for!at specified b 'date_format'. $f 'x' is 0*LL& replace it with 'y'. 'x' and 'y' !ust be of the sa!e datat pe. 6hec%s the value of 'a'& if a = b& then returns 'c'. $f a = d& then returns 'e'. /lse& returns default_value.

The below table provides the e#a!ples for the above functions

Function Na(e T'261.) () T'2D.T/ () 05L ()

E*a('les T'261.) (ADDD& >J====>) T'261.) (S7SD.T/& >Da & Month 7777>) T'2D.T/ (>D?"Gun"D;>) 05L (null& ?)

Return 1alue JADDD Monda & Gune @DD; D?"Gun"D; ?

In2or(ation Sc)e(a $
$0,')M.T$'02S61/M. provides access to database !etadata. Metadata is data about the data& such as the na!e of a database or table& the data t pe of a colu!n& or access privileges. 'ther ter!s that so!eti!es are used for this infor!ation are data dictionary and system catalog. $0,')M.T$'02S61/M. is the infor!ation database& the place that stores infor!ation about all the other databases that the M SQL server !aintains. $nside $0,')M.T$'02S61/M. there are several read"onl tables. The are actuall views& not base tables& so there are no files associated with the!& and ou cannot set triggers on the!. .lso& there is no database director with that na!e. .lthough ou can select $0,')M.T$'02S61/M. as the default database with a *S/ state!ent& ou can onl read the contents of tables& not perfor! $0S/)T& *+D.T/& or D/L/T/ operations on the!. 1ere is an e#a!ple of a state!ent that retrieves infor!ation fro! $0,')M.T$'02S61/M.: ! sqlK SELECT ta/le3na(e4 ta/le3t5'e4 en"ine "K FROM in2or(ation3sc)e(a6ta/les "K .7ERE ta/le3sc)e(a 8 9d/:9 "K ORDER B; ta/le3na(e DESC< L""""""""""""L""""""""""""L""""""""L M table2na!e M table2t pe M engine M L""""""""""""L""""""""""""L""""""""L M vC< M 5$/4 M 0*LL M M vA M 5$/4 M 0*LL M M v@ M 5$/4 M 0*LL M Mv M 5$/4 M 0*LL M M tables M 3.S/ T.3L/ M M $S.M M M t: M 3.S/ T.3L/ M M $S.M M M tA M 3.S/ T.3L/ M M $S.M M M t@ M 3.S/ T.3L/ M M $S.M M Mt M 3.S/ T.3L/ M M $S.M M

M p% M 3.S/ T.3L/ M $nnoD3 M M loop M 3.S/ T.3L/ M M $S.M M M %urs M 3.S/ T.3L/ M M $S.M M M% M 3.S/ T.3L/ M M $S.M M M into M 3.S/ T.3L/ M M $S.M M M goto M 3.S/ T.3L/ M M $S.M M M f%@ M 3.S/ T.3L/ M $nnoD3 M M f% M 3.S/ T.3L/ M $nnoD3 M L""""""""""""L""""""""""""L""""""""L ?: rows in set (D.D? sec) /#planation: The state!ent requests a list of all the tables in database dbC& in reverse alphabetic order& showing -ust three pieces of infor!ation: the na!e of the table& its t pe& and its storage engine. The definition for character colu!ns (for e#a!ple& T.3L/S.T.3L/20.M/) is generall 5.)61.)(N) 61.).6T/) S/T utf; where N is at least <B. M SQL uses the default collation for this character set (utf;2general2ci) for all searches& sorts& co!parisons& and other string operations on such colu!ns. 5alues such as table na!es in $0,')M.T$'02S61/M. colu!ns are treated as strings& not identifiers& and are not co!pared using the identifier rules described in Section =.@.@& N$dentifier 6ase Sensitivit O. $f the result of a string operation on an $0,')M.T$'02S61/M. colu!n differs fro! e#pectations& a wor%around is to use an e#plicit 6'LL.T/ clause to force a suitable collation (Section ?D.?.:.@& N*sing 6'LL.T/ in SQL State!entsO). 7ou can also use the *++/)() or L'4/)() function. ,or e#a!ple& in a 41/)/ clause& ou !ight use: 41/)/ 41/)/ 41/)/ 41/)/ T.3L/20.M/ 6'LL.T/ utf;2bin P >6it > T.3L/20.M/ 6'LL.T/ utf;2general2ci P >cit > *++/)(T.3L/20.M/) P >6$T7> L'4/)(T.3L/20.M/) P >cit >

/ach M SQL user has the right to access these tables& but can see onl the rows in the tables that correspond to ob-ects for which the user has the proper access privileges. $n so!e cases (for e#a!ple& the )'*T$0/2D/,$0$T$'0 colu!n in the $0,')M.T$'02S61/M..)'*T$0/S table)& users who have insufficient privileges will see 0*LL. The S/L/6T ... ,)'M $0,')M.T$'02S61/M. state!ent is intended as a !ore consistent wa to provide access to the infor!ation provided b the various S1'4 state!ents that M SQL supports (S1'4 D.T.3.S/S& S1'4 T.3L/S& and so forth). *sing S/L/6T has these advantages& co!pared to S1'4:

$t confor!s to 6odd>s rules. That is& all access is done on tables. 0obod needs to learn a new state!ent s nta#. 3ecause the alread %now how S/L/6T wor%s& the onl need to learn the ob-ect na!es.

The i!ple!entor need not worr about adding %e words. There are !illions of possible output variations& instead of -ust one. This provides !ore fle#ibilit for applications that have var ing require!ents about what !etadata the need. Migration is easier because ever other D3MS does it this wa .

1owever& because S1'4 is popular and because it !ight be confusing were it to disappear& the advantages of conventional s nta# are not a sufficient reason to eli!inate S1'4. $n fact& along with the i!ple!entation of $0,')M.T$'02S61/M.& there are enhance!ents to S1'4 as well. These are described in Section ?=.?;& N/#tensions to S1'4 State!entsO. There is no difference between the privileges required for S1'4 state!ents and those required to select infor!ation fro! $0,')M.T$'02S61/M.. $n either case& ou have to have so!e privilege on an ob-ect in order to see infor!ation about it. The i!ple!entation for the $0,')M.T$'02S61/M. table structures in M SQL follows the .0S$Q$S' SQL:@DDA standard +art ?? Schemata. 'ur intent is appro#i!ate co!pliance with SQL:@DDA core feature ,D@? Basic information schema. *sers of SQL Server @DDD (which also follows the standard) !a notice a strong si!ilarit . 1owever& M SQL has o!itted !an colu!ns that are not relevant for our i!ple!entation& and added colu!ns that are M SQL"specific. 'ne such colu!n is the /0($0/ colu!n in the $0,')M.T$'02S61/M..T.3L/S table. .lthough other D3MSs use a variet of na!es& li%e s scat or s ste!& the standard na!e is $0,')M.T$'02S61/M.. The following sections describe each of the tables and colu!ns that are in $0,')M.T$'02S61/M.. ,or each colu!n& there are three pieces of infor!ation:

N$0,')M.T$'02S61/M. 0a!eO indicates the na!e for the colu!n in the $0,')M.T$'02S61/M. table. This corresponds to the standard SQL na!e unless the N)e!ar%sO field sa s NM SQL e#tension.O NS1'4 0a!eO indicates the equivalent field na!e in the closest S1'4 state!ent& if there is one. N)e!ar%sO provides additional infor!ation where applicable. $f this field is NULL& it !eans that the value of the colu!n is alwa s NULL. $f this field sa s NM SQL e#tension&O the colu!n is a M SQL e#tension to standard SQL.

Transact=SQL e*tensions
Transact"SQL enhances the power of SQL and !ini!iFes the occasions on which users !ust resort to a progra!!ing language to acco!plish a desired tas%. Transact"SQL>s capabilities go be ond the $S' standards and the !an co!!ercial versions of SQL. Most of the Transact"SQL enhance!ents (%nown as e#tensions) are su!!ariFed here. The Transact"SQL e#tensions for each co!!and are in the )eference Manual. T)e co('ute clause The Transact"SQL co!pute clause e#tension is used with the row aggregate functions su!& !a#& !in& avg& and count to calculate su!!ar values. Queries that include a co!pute clause displa results with both detail and su!!ar rows. These reports rese!ble those produced b al!ost an Database Manage!ent S ste! (D3MS) with a report generator. co!pute displa s su!!ar values as additional rows in the results& instead of as new colu!ns. The co!pute clause is covered in 6hapter A& R*sing .ggregates& (rouping& and Sorting.R Control=o2=2lo# lan"ua"e Transact"SQL provides control"of"flow language that can be used as part of an SQL state!ent or batch. These constructs are available: begin...end& brea%& continue& declare& goto label& if...else& print& raiserror& return& waitfor& and while. Local variables can be defined with declare and assigned values. . nu!ber of predefined global variables are supplied b the s ste!. Transact"SQL also supports case e#pressions& which include the %e words case& when& then& coalesce& and nullif. case e#pressions replace the if state!ents of standard SQL. case e#pressions are allowed an where a value e#pression is used. Stored 'rocedures 'ne of the !ost i!portant Transact"SQL e#tensions is the abilit to create stored procedures. . stored procedure is a collection of SQL state!ents and optional control"of"flow state!ents stored under a na!e. The creator of a stored procedure can also define para!eters to be supplied when the stored procedure is e#ecuted. The abilit to write our own stored procedures greatl enhances the power& efficienc & and fle#ibilit of the SQL database language. Since the e#ecution plan is saved after stored procedures are run& stored procedures can subsequentl run !uch faster than standalone state!ents.

.daptive Server"supplied stored procedures& called s ste! procedures& aid in .daptive Server s ste! ad!inistration. 6hapter ?B& R*sing Stored +rocedures&R discusses s ste! procedures and e#plains how to create stored procedures. S ste! procedures are discussed in detail in the )eference Manual. *sers can e#ecute stored procedures on re!ote servers. .ll Transact"SQL e#tensions support return values fro! stored procedures& user"defined return status fro! stored procedures& and the abilit to pass para!eters fro! a procedure to its caller. E*tended stored 'rocedures .n e#tended stored procedure (/S+) has the interface of a stored procedure& but instead of containing SQL state!ents and control"of"flow state!ents& it e#ecutes procedural language code that has been co!piled into a d na!ic lin% librar (DLL). The procedural language in which an /S+ function is written can be an language capable of calling 6 language functions and !anipulating 6 datat pes. /S+s allow .daptive Server to perfor! a tas% outside the )D3MS in response to an event occurring within the database. ,or e#a!ple& ou could use an /S+ to send an e"!ail notification or networ%"wide broadcast in response to an event occurring within the )elational Database Manage!ent S ste! ()D3MS). There are so!e .daptive Server"supplied /S+s& called s ste! e#tended stored procedures. 'ne of these& #p2c!dshell& allows ou to e#ecute an operating s ste! co!!and fro! within .daptive Server. 6hapter ?C& R*sing /#tended Stored +rocedures&R describes /S+s. The )eference Manual includes detailed infor!ation about s ste! /S+s. /S+s are i!ple!ented b an 'pen Server(TM) application called 9+ Server(TM)& which runs on the sa!e !achine as .daptive Server. )e!ote e#ecution of a stored procedure is called a re!ote procedure call ()+6). .daptive Server and 9+ Server co!!unicate through )+6s. 9+ Server is auto!aticall installed with .daptive Server. Tri""ers . trigger is a stored procedure that instructs the s ste! to ta%e one or !ore actions when a specific change is atte!pted. 3 preventing incorrect& unauthoriFed& or inconsistent changes to data& triggers help !aintain the integrit of a database.

10

Triggers can also protect referential integrit ""enforcing rules about the relationships a!ong data in different tables. Triggers go into effect when a user atte!pts to !odif data with an insert& delete& or update co!!and. Triggers can nest to a depth of ?< levels& and can call local or re!ote stored procedures or other triggers. De2aults and rules Transact"SQL provides %e words for !aintaining entit integrit (ensuring that a value is supplied for ever colu!n requiring one) and do!ain integrit (ensuring that each value in a colu!n belongs to the set of legal values for that colu!n). Defaults and rules define integrit constraints that co!e into pla during data entr and !odification. . default is a value lin%ed to a particular colu!n or datat pe& and inserted b the s ste! if no value is provided during data entr . )ules are user"defined integrit constraints lin%ed to a particular colu!n or datat pe& and enforced at data entr ti!e. )ules and defaults are discussed in 6hapter ?@& RDefining Defaults and )ules for Data.R Error )andlin" and set o'tions . nu!ber of error handling techniques are available to the Transact"SQL progra!!er& including the abilit to capture return status fro! stored procedures& define custo!iFed return values fro! stored procedures& pass para!eters fro! a procedure to its caller& and get reports fro! global variables such as SSerror. The raiserror and print state!ents& in co!bination with control"of"flow language& can direct error !essages to the user of a Transact"SQL application. Developers can localiFe print and raiserror to use different languages. set options custo!iFe the displa of results& show processing statistics& and provide other diagnostic aids for debugging our Transact"SQL progra!s. .ll set options e#cept showplan and char2convert ta%e effect i!!ediatel . The following paragraphs list the available set options. ,or !ore infor!ation& refer to the )eference Manual.

parseonl & noe#ec& prefetch& showplan& rowcount& nocount& and tablecount control the wa a quer is e#ecuted. The statistics options displa perfor!ance statistics after each quer . flush!essage deter!ines when .daptive Server returns !essages to the user. See the +erfor!ance and Tuning (uide for !ore infor!ation. arithabort deter!ines whether .daptive Server aborts queries with arith!etic overflow and nu!eric truncation errors. arithignore deter!ines whether .daptive Server prints a warning !essage if a quer results in an arith!etic overflow. ,or !ore infor!ation& see R.rith!etic errorsR.

11

offsets and procid are used in D3"Librar (TM) to interpret results fro! .daptive Server. datefirst& datefor!at& and language affect date functions& date order& and !essage displa . char2convert controls character"set conversion between .daptive Server and a client. te#tsiFe controls the siFe of te#t or i!age data returned with a select state!ent. See RTe#t functions used for te#t and i!age dataR. cursor rows and close on endtran affect the wa .daptive Server handles cursors. See R,etching data rows using cursorsR. identit 2insert allows or prohibits inserts that affect a table>s $D/0T$T7 colu!n. See R(aps due to insertions& deletions& identit grab siFe& and rollbac%sR. chained and transaction isolation level control how .daptive Server handles transactions. See RSelecting the transaction !ode and isolation levelR. self2recursion allows .daptive Server to handle triggers that cause the!selves to fire. See RTrigger self"recursionR. ansinull& ansi2per!issions& and fipsflagger control whether .daptive Server flags the use of nonstandard SQL. string2rtruncation controls whether .daptive Server raises an e#ception error when truncating a char or nchar string. See R6o!pliance to .0S$ standardsR. quoted2identifier controls whether .daptive Server treats character strings enclosed in double quotes as identifiers. See RDeli!ited identifiersR for !ore infor!ation. role controls the roles granted to ou. ,or infor!ation about roles& see the S ste! .d!inistration (uide.

12

Anda mungkin juga menyukai