Download - Unit 5 (Data Manipulation and Control - I 1)
-
8/12/2019 Unit 5 (Data Manipulation and Control - I 1)
1/31
DATA MANIPULATION AND CONTROL - I
1
-
8/12/2019 Unit 5 (Data Manipulation and Control - I 1)
2/31
DATA MANIPULATION AND CONTROL - I
Objectives
At the end of this unit, you will be able to
Lea!n to "!eate, alte! and d!o# database ob$e"ts
Lea!n to inse!t , u#date and delete data
Lea!n to %!ant and !e&o'e #!i&ile%es
Topics
Data Definition (tate)ents
Data Mai#ulation (tate)ents
Data Cont!ol (tate)ents
T!ansa"tion Con!ol (tate)ents
*
+
-
8/12/2019 Unit 5 (Data Manipulation and Control - I 1)
3/31
DATA MANIPULATION AND CONTROL - I
Datatypes in Oracle
a"h "olu)n &alue of a table in O!a"le has a dataty#e* The dataty#e asso"iates afied set of #!o#e!ties with the &alue and defines the do)ain of &alues that ea"h "olu)n
"an "ontain* These #!o#e!ties "ause O!a"le to t!eat &alues of one dataty#e diffe!ently
f!o) &alues of anothe!*The followin% dataty#es a!e a&ailable in O!a"le*
Datatype Description
CHAR(size) (to!es fied len%th "ha!a"te! data* Mai)u) si.e is +///*
Default si.e is 1 byte*
VARCHAR2(size) (to!es &a!iable len%th "ha!a"te! st!in%s ha&in% )ai)u)
si.e of 0/// bytes* ou )ust s#e"ify si.e fo! a2ARC3AR+*
NUM!R(p"s) (to!es nu)be! ha&in% #!e"ision # and s"ale s, # "an !an%e
f!o) 1 to 45 and s dete!)ines the nu)be! of di%its to the!i%ht of the de"i)al #oint*
DAT! (to!es data and ti)e &alues between /1-/1-061+ 7C and
/1-1+-061+ AD
#ON$ (to!es la!%e,&a!iable len%th st!in%s,u# to + %i%abytes*
#ON$ RA% (to!es lon% bina!y st!in%s, u# to + %i%abytes*
RA%(size) (to!es s)all bina!y st!in%s,less than +/// bytes*
RO%&D (to!es heade"i)al RO8ID &alues that "o!!es#ond to
O!a"le table RO8ID(*9blo"'*!ow*file:
Data De'inition tateents
Data definition state)ents CRAT,ALTR and DROP a!e used
to "!eate new ob$e"ts, alte! the st!u"tu!es of eistin% ob$e"ts, o!"o)#letely !e)o&e ob$e"ts f!o) the database*
Constraints
Const!aints a!e #a!t of a table definition that li)it the &alues you"an ente! into its "olu)ns* Till now the only !est!i"tion on &alues
that you "ould ente! has been on the dataty#es and si.es of the
&alues ente!ed whi"h ha&e to be "o)#atible with those of the
"olu)ns into whi"h the &alues a!e bein% #la"ed*
8hen you "!eate a table you "an #la"e "onst!aints on the &alues
that "an be ente!ed into its fields; by doin% this (
-
8/12/2019 Unit 5 (Data Manipulation and Control - I 1)
4/31
DATA MANIPULATION AND CONTROL - I
Constraint Type Description
NOT NU## This "onst!aint is of "olu)n &a!iety only* The NOT NULL
Constraint 'eywo!d #la"ed i))ediately afte! the dataty#e of a "olu)nensu!es that any atte)#ts to #ut null &alues into that field by inse!t
o! u#date state)ent will be !e$e"ted*
UN&*U! As a "olu)n "onst!aint- The UNI
-
8/12/2019 Unit 5 (Data Manipulation and Control - I 1)
5/31
DATA MANIPULATION AND CONTROL - I
ffe"t of @o!ei%n @o! fields defined as @ORIN ?
?ey Const!aint
Any &alues you #ut into these fields with an IN(RT o!
UPDAT "o))and, the &alue )ust al!eady be #!esent in
thei! #a!ent 'eys*
ou )ay #ut NULLBs in these fields unless that field hasNOT NULL "onst!aint*
ou )ay delete any !ows with fo!ei%n 'eys without
affe"tin% the #a!ent 'eys*
As fa! as "han%es to the #a!ent 'ey &alues a!e "on"e!ned, the!e a!e
essentially th!ee #ossibilities
ou "an !est!i"t o! fo!bid the "han%e = whi"h )eans that
the "han%e on the #a!ent 'ey is !est!i"ted*
ou "an )a'e "han%e in the #a!ent 'ey and ha&e that sa)e
"han%e )ade in the fo!ei%n 'ey auto)ati"ally = whi"h
)eans the "han%e "as"ades*
ou "an )a'e "han%e in the #a!ent 'ey and set the fo!ei%n
'ey to NULL auto)ati"ally 9assu)in% NUL( a!e allowedin the fo!ei%n 'ey: = whi"h )eans the "han%e nulls the
fo!ei%n 'ey*
8ith these "ate%o!ies a&ailable, you )ay not want to t!eat all of
the DML "o))ands in the sa)e way*
IN(RT is i!!ele&ant as it #uts new #a!ent 'ey &alue in the table
so none of its &alues "an be "u!!ently be !efe!en"ed*
UPDAT( and DLT( "an be "as"aded o! !est!i"ted as desi!ed*
-
8/12/2019 Unit 5 (Data Manipulation and Control - I 1)
6/31
DATA MANIPULATION AND CONTROL - I
Tables
Tables a!e #!i)a!y sto!a%e units in O!a"le*
To sto!e data in a table, the table will ha&e to be "!eated, with the !e>ui!edst!u"tu!e, alon% with the &a!ious "onst!aints*
Create Table
tateent CR!AT! TA#!
+/rpose (to!a%e (t!u"tu!e to hold data*
ysnta0
%1ere
Tablena)e is na)e of the table
Colu)nna)e is na)e of the "olu)nDataty#e is the dataty#e of the "olu)n
Colu)n"onst!aint is data "onst!aint fo! the "olu)nTable"onst!aint is data "onst!aint fo! the tableA( >ue!y "olu)nBs ty#e and si.es a!e "o#ied f!o) the !esult
of A( >ue!y , afte! the table is "!ated the !esults of
the >ue!y a!e inse!ted as its !ows*
!0aple C!eate a new table usin% A( "lause*
*# Coan (
-
8/12/2019 Unit 5 (Data Manipulation and Control - I 1)
7/31
DATA MANIPULATION AND CONTROL - I
!0aple To "onfi!) the "ontents of table N8
*# Coan (
-
8/12/2019 Unit 5 (Data Manipulation and Control - I 1)
8/31
DATA MANIPULATION AND CONTROL - I
!0aple C!eate a new table
*# Coan (
-
8/12/2019 Unit 5 (Data Manipulation and Control - I 1)
9/31
DATA MANIPULATION AND CONTROL - I
%1ere
Tablena)e is na)e of the table*
Colna)e is na)ee of the "olu)n*
Dataty#e is the dataty#e of the "olu)n
Const!aintna)e is the na)e of the "onst!aint
nable"lause is the na)e of the "onst!aint o! all t!i%%e!s
diasble"lause on the table*
!0aple Add "olu)n to table te)#*
*# Coan (
-
8/12/2019 Unit 5 (Data Manipulation and Control - I 1)
10/31
DATA MANIPULATION AND CONTROL - I
!0aple To "onfi!) the st!u"tu!e of table te)#*
*# Coan (
-
8/12/2019 Unit 5 (Data Manipulation and Control - I 1)
11/31
DATA MANIPULATION AND CONTROL - I
Res/lt Table alte!ed*
!0aple To "onfi!) the st!u"tu!e of table salestab*
*# Coan (
-
8/12/2019 Unit 5 (Data Manipulation and Control - I 1)
12/31
DATA MANIPULATION AND CONTROL - I
!0aple To &e!ify the status of all "onst!aints of salestab table*
*# Coan (
-
8/12/2019 Unit 5 (Data Manipulation and Control - I 1)
13/31
DATA MANIPULATION AND CONTROL - I
Data Manip/lation tateents
Data Mani#ulation state)ents a!e used to alte! the database tables in so)e way*
The UPDAT, IN(RT and DLT state)ents alte! eisin% !ows in a database table,#la"e new !e"o!ds into a database table o! !e)o&e one o! )o!e !e"o!ds f!o) the database
table*
INSERT
tateent &N!RT
+/rpose Add new !ow9s: to the database table*
ynta0 IN(RT INTO tableG9"olu)nG,"olu)nH**:H2ALU( 9&aluesG,&alueH**: J >ue!yK
%1ere
table is na)e of the table
"olu)n is na)e of the table&alue is the "o!!es#ondin% &alue fo! the "olu)n
2ALU( adds one !ow*
>ue!y adds as )any !ows as sele"ted by the >ue!y*
!0aple To des"!ibe the st!u"tu!e of e)# table*
*# Coan (
-
8/12/2019 Unit 5 (Data Manipulation and Control - I 1)
14/31
DATA MANIPULATION AND CONTROL - I
Inserting null values implicitly
!0aple Inse!t a new e)#lo#yee, but only assi%n the e)#loyee nu)be!,
na)e, $ob, de#a!t)ent nu)be! and )ana%e! nu)be!*
*# Coan (
-
8/12/2019 Unit 5 (Data Manipulation and Control - I 1)
15/31
DATA MANIPULATION AND CONTROL - I
!0aple Dis#lay all data f!o) table de#t1
*# Coan (ue!y o#tion
*# Coan (
-
8/12/2019 Unit 5 (Data Manipulation and Control - I 1)
16/31
DATA MANIPULATION AND CONTROL - I
!0aple Inse!t &alues into "olu)ns usin% #a!a)ete!s*
*# Coan (
-
8/12/2019 Unit 5 (Data Manipulation and Control - I 1)
17/31
DATA MANIPULATION AND CONTROL - I
*# Coan (
-
8/12/2019 Unit 5 (Data Manipulation and Control - I 1)
18/31
DATA MANIPULATION AND CONTROL - I
!0aple To !eali.e the effe"t of the !ollba"', "o))it and sa&e#oint"o))ands*
*# Coan (
-
8/12/2019 Unit 5 (Data Manipulation and Control - I 1)
19/31
DATA MANIPULATION AND CONTROL - I
%1ere
Table is na)e of the table to be u#dated
Colu)n e#! sets fields in the s#e"ified "olu)n to the&alue of e#!*
9"olu)n,**: sets fields in the s#e"ified "olu)ns to the &alue!etu!ned
9sub>ue!y: by the sub>ue!y*
Condition identifies the !ow9s: to be u#dated*
!0aple Chan%e the "o))ission of e)#loyee 1+40 to and $ob to
(AL(MAN*
*# Coan (
-
8/12/2019 Unit 5 (Data Manipulation and Control - I 1)
20/31
-
8/12/2019 Unit 5 (Data Manipulation and Control - I 1)
21/31
DATA MANIPULATION AND CONTROL - I
!0aple Confi!) the "han%es )ade by the abo&e state)ent*
*# Coan (
-
8/12/2019 Unit 5 (Data Manipulation and Control - I 1)
22/31
DATA MANIPULATION AND CONTROL - I
!0aple Confi!) the "han%es to the unde!lyin% table )ade by the abo&estate)ent*
*# Coan (
-
8/12/2019 Unit 5 (Data Manipulation and Control - I 1)
23/31
DATA MANIPULATION AND CONTROL - I
ynta0
%1ere
Ob$e"t#!i& is an ob$e"t #!i&ile%e to be %!anted
ALL all ob$e"t #!i&ile%es
Colu)n s#e"ifies a "olu)n f!o) a table o! &iew on whi"h#!i&ile%es a!e %!anted
ON identifies the ob$e"t on whi"h #!i&ile%es a!e %!anted
TO identifies use!s to whi"h the ob$e"t #!i&ile%e is
%!antedPU7LIC %!ants ob$e"t #!i&ile%es to all use!s
8IT3 RANT OPTION allows %!antee to %!ant the ob$e"t #!i&ile%es to
othe! use!s
!0aple !ant (LCT #!i&ile%es to s"ott on e)# table
*# Coan (
-
8/12/2019 Unit 5 (Data Manipulation and Control - I 1)
24/31
DATA MANIPULATION AND CONTROL - I
!0aple C!eate a synony) fo! e)# table
*# Coan (
-
8/12/2019 Unit 5 (Data Manipulation and Control - I 1)
25/31
DATA MANIPULATION AND CONTROL - I
+/rpose Re&o'es database o! syste) o! ob$e"t a""ess #!i&ile%es f!o) use!s*
ynta0
%1ere
CACAD! a!e !e>ui!ed to !e)o&e any @? !efe!en"es
CONTRA&NT )ade to the ob$e"t by )eans of the
R@RNC( #!i&ile%e*
!0aple Conne"t to use!1 s"he)a
*# Coan (
-
8/12/2019 Unit 5 (Data Manipulation and Control - I 1)
26/31
DATA MANIPULATION AND CONTROL - I
!!o! in disablin% !oles in #!odu"t use! #!ofile
!0aple Dis#lay the "ontents of de#t table #!esent in use!1 s"he)a
*# Coan (
-
8/12/2019 Unit 5 (Data Manipulation and Control - I 1)
27/31
DATA MANIPULATION AND CONTROL - I
1are6!0cl/sive 8hen a use!, U(R1 is a""essin% RCORD1 f!o) the table and
#oc5 U(R+ t!ies to a""ess the anothe! !e"o!d f!o) the sa)e table, thenboth the t!ansa"tions a">ui!e a sha!ed lo"' on the table* If U(R4
t!ies to a""ess RCORD1 and U(R1Bs t!ansa"tion a">ui!es an
e"lusi&e lo"' on the table* This is also !efe!!ed to as !ow-le&ello"'in%, whi"h is a uni>ue featu!e of O!a"le* The abo&e ea)#le
shows how O!a"le handles two diffe!ent u#dates that "ontend fo!
the sa)e !e"o!d of a table*
M/lti7versionin3 O!a"le uses the )e"hanis) of Multi-2e!sionin% to handle >ue!ies,
whi"h "ontend with u#dates to !etu!n a""u!ate !esults* @i!st a
t!ansa"tion does not a">ui!e !ow lo"'s of any ty#e fo! >ue!ies* This)eans that two t!ansa"tions "an issue ea"tly the sa)e >ue!y at
the sa)e ti)e without any "ontention fo! the sa)e set of !ows* In
this "ase fo! e&e!y >ue!y O!a"le !etu!ns a ti)e#oint-based &e!sion
of the data o! a !ead-"onsistent sna#shot that the >ue!y !e>uests*O!a"le ensu!es that e&e!y !ow in a >ue!yBs !esult set is "onsistent at
the ti)e you issue the >ue!y* 3e!e O!a"le uses !ollba"' se%)entfo! its )ulti-&e!sionin% )e"hanis)*
a"h ti)e a use!Bs t!ansa"tion be%ins, O!a"le assi%ns thet!ansa"tion to a !ollba"' se%)ent* @o! the du!ation of a t!ansa"tion,
the asso"iated use! #!o"ess w!ites !ollba"' info!)ation to the
assi%ned !ollba"' se%)ent* 8hen you "o))it a t!ansa"tion, O!a"le
!eleases the !ollba"' info!)ation but does not i))ediately dest!oyit* The info!)ation !e)ains in the !ollba"' se%)ent to "!eate !ead-
"onsistent sna#shot of #e!tinent data fo! >ue!ies that sta!ted befo!e
the t!ansa"tion "o))ited* To %ua!antee that the !ollba"' data isa&ailable fo! as lon% as #ossible fo! su"h &iews O!a"le w!ites the
etents of !ollba"' se%)ent se>uentially*
"oc!
tateent #OC-
+/rpose Pe!)its sha!ed a""ess to the table by )ulti#le use!s while
#!ese!&in% the tableBs Inte%!ity
ynta0
%1ere
Table is the na)e of the table*
+6
LOC? TA7L G, tableHQ
IN (3AR J (3AR UPDAT J CLU(I2 K MOD GNO8AITH
-
8/12/2019 Unit 5 (Data Manipulation and Control - I 1)
28/31
DATA MANIPULATION AND CONTROL - I
NO8AIT )a'es LOC? TA7L !etu!n "ont!ol to you so that
you )ay ente! othe! "o))ands while you wait fo!
the table to be"o)e a&ailable*
CLU(I2 #!e&ents othe! use!s f!o) lo"'in% o! u#datin% any
!ow of the table
(3AR Allows othe! use!s >ue!y the table but not )odify it
(3AR UPDAT allows you to u#date !ows of the table while othe!
use!s ha&e !ows lo"'ed
!0aple To lo"' the e)#loyee table in e"lusi&e )ode, but to !etu!n the
"ont!ol if any othe! use! has lo"'ed it*
*# Coan (
-
8/12/2019 Unit 5 (Data Manipulation and Control - I 1)
29/31
DATA MANIPULATION AND CONTROL - I
DML state)ents, the t!ansa"tion is fi!st "o))itted, and the
DDL state)ent is ee"uted and "o))itted as a new, sin%le
state)ent t!ansa"tion*
A use! dis"onne"ts f!o) ORACL* 9The "u!!ent t!ansa"tion is
"o))itted:
The!e is an abno!)al te!)ination of a use! #!o"ess* 9The"u!!ent t!ansa"tion is !olled ba"':
Afte! one t!ansa"tion ends, the net ee"utable (
-
8/12/2019 Unit 5 (Data Manipulation and Control - I 1)
30/31
DATA MANIPULATION AND CONTROL - I
The s#e"ified sa&e#oint is #!ese!&ed, but all sa&e#oints that we!e
established afte! the s#e"ified one a!e lost*
All tables and !ow lo"'s a">ui!ed sin"e that sa&e#oint that a!e
!eleased,but all data lo"'s a">ui!ed #!e&ious to the sa&e#oint a!e!etained*
The t!ansa"tion is still a"ti&e; it "an be "ontinued*
tateent Rollbac5
+/rpose Rolls ba"' "han%es )ade to the tables in the database sin"e the )ost
!e"ent "o))it*
ynta0 ROLL7AC?
Savepoint
Inte!)ediate )a!'e!s o! sa&e#oints "an be de"la!ed within a t!ansa"tion*(a&e#oints a!e often used to di&ide a lon% t!ansa"tion into s)alle! #a!ts*
Usin% sa&e#oints, you "an a!bit!a!ily )a!' you! wo!' at any #oint within a
lon% t!ansa"tion* This allows you the o#tion of late! !ollin% ba"' wo!'#e!fo!)ed befo!e the "u!!ent #oint in the t!ansa"tion but afte! a de"la!ed
sa&e#oint within the t!ansa"tion*
(a&e#oints a!e also useful in a##li"ation #!o%!a)s in a si)ila! way* If a#!o"edu!e "ontains se&e!al fun"tions, a sa&e#oint "an be "!eated befo!e
ea"h fun"tion be%ins* Then, if a fun"tion fails, it is easy to !etu!n the data
to its o!i%inal state befo!e the fun"tion be%an and then !e-ee"ute thefun"tion with !e&ised #a!a)ete!s o! #e!fo!) a !e"o&e!y a"tion*
Afte! a !ollba"' to a sa&e#oint, the data lo"'s obtained by !olled ba"'state)ents a!e !eleased* Othe! t!ansa"tions that we!e waitin% fo! the
#!e&iously lo"'ed !esou!"es "an #!o"eed* Othe! t!ansa"tions that want to
u#date #!e&iously lo"'ed !ows "an do so*
tateent avepoint
+/rpose Ma!' a sa&e#oint within the "u!!ent t!ansa"tion*
ynta0 (A2POINT
4/
-
8/12/2019 Unit 5 (Data Manipulation and Control - I 1)
31/31
DATA MANIPULATION AND CONTROL - I
UMMAR, 97
Data Definition Lan%ua%e 9DDL: is the subset of s>l "o))ands used to )ani#ulateO!a"le database st!u"tu!es, in"ludin% tables*
Tables a!e "!eated usin% CRAT TA7L "o))and*
Const!aints li)it the &alues ente!ed into its "olu)ns* The &a!ious "onst!aints a!e NOT
NULL, UNIue!ies "an also be used in the UPDAT "o))and*
DLT @ROM "o))and is used to delete !ows f!o) a table*
To )a'e the wo!' #e!)anent, COMMIT is used and to dis"a!d the t!ansa"tions, the
ROLL7AC? "o))and is used*
One "an dis"a!d all the "han%es u#to a #oint, defined with the (A2POINT
state)ent*