Download as pdf or txt
Download as pdf or txt
You are on page 1of 45

Chapter 9

SQL: Assertions, Views, and


Programming Techniques
Copyright 2004 Pearson Education, Inc.
Chapter 9-3
Copyright 2004 Rame E!masri and "ham#ant $a%athe
Elmasri!a"athe, #undamentals o$ %ata&ase S'stems, #ourth Edition
Chapter (utline
9.1 General Constraints as Assertions
9.2 Views in SQL
9.3 Database Programming
9.4 Embedded SQL
9. !"n#tions Calls$ SQL%CL&
9.' Stored Pro#ed"res$ SQL%PS(
9.) S"mmar*
Chapter 9-4
Copyright 2004 Rame E!masri and "ham#ant $a%athe
Elmasri!a"athe, #undamentals o$ %ata&ase S'stems, #ourth Edition
C+a,ter -b.e#ti/es

S,e#i0i#ation o0 more general #onstraints


/ia assertions

SQL 0a#ilities 0or de0ining /iews 1/irt"al


tables2

Vario"s te#+ni3"es 0or a##essing and


mani,"lating a database /ia ,rograms in
general4,"r,ose lang"ages 1e.g.$ 5a/a2
Chapter 9-&
Copyright 2004 Rame E!masri and "ham#ant $a%athe
Elmasri!a"athe, #undamentals o$ %ata&ase S'stems, #ourth Edition
Constraints as Assertions

General #onstraints6 #onstraints t+at do


not 0it in t+e basi# SQL #ategories
1,resented in #+a,ter 72

(e#+anism6 CREAT ASSERTION


8
#om,onents in#l"de6 a #onstraint name$
0ollowed b* CHECK$ 0ollowed b* a #ondition
Chapter 9-'
Copyright 2004 Rame E!masri and "ham#ant $a%athe
Elmasri!a"athe, #undamentals o$ %ata&ase S'stems, #ourth Edition
Assertions6 An E9am,le

:;+e salar* o0 an em,lo*ee m"st not be


greater t+an t+e salar* o0 t+e manager o0
t+e de,artment t+at t+e em,lo*ee wor<s
0or==
CREAT ASSERTION SALARY_CONSTRAINT
CHECK (NOT EXISTS (SELECT *
FROM EMPLOYEE E, EMPLOYEE M, DEPARTMENT D
WHERE E.SALARY > M.SALARY AND
E.DNO=D.NUMBER AND D.MGRSSN=M.SSN))
Chapter 9-(
Copyright 2004 Rame E!masri and "ham#ant $a%athe
Elmasri!a"athe, #undamentals o$ %ata&ase S'stems, #ourth Edition
>sing General Assertions

S,e#i0* a 3"er* t+at /iolates t+e


#ondition? in#l"de inside a NOT EXISTS
#la"se

Q"er* res"lt m"st be em,t*


8
i0 t+e 3"er* res"lt is not em,t*$ t+e assertion
+as been /iolated
Chapter 9-)
Copyright 2004 Rame E!masri and "ham#ant $a%athe
Elmasri!a"athe, #undamentals o$ %ata&ase S'stems, #ourth Edition
SQL ;riggers

-b.e#ti/e6 to monitor a database and


ta<e a#tion w+en a #ondition o##"rs

;riggers are e9,ressed in a s*nta9


similar to assertions and in#l"de t+e
0ollowing6
8
e/ent 1e.g.$ an ",date o,eration2
8
#ondition
8
a#tion 1to be ta<en w+en t+e #ondition is
satis0ied2
Chapter 9-9
Copyright 2004 Rame E!masri and "ham#ant $a%athe
Elmasri!a"athe, #undamentals o$ %ata&ase S'stems, #ourth Edition
SQL ;riggers6 An E9am,le

A trigger to #om,are an em,lo*ee=s salar* to +is%+er


s",er/isor d"ring insert or ",date o,erations6
CREATE TRIGGER INFORM_SUPERISOR
BEFORE INSERT OR UPDATE OF
SALARY, SUPERISOR_SSN ON EMPLOYEE
FOR EACH ROW
WHEN
(NEW.SALARY> (SELECT SALARY FROM EMPLOYEE
WHERE SSN=NEW.SUPERISOR_SSN))
INFORM_SUPERISOR (NEW.SUPERISOR_SSN,NEW.SSN!
Chapter 9-*0
Copyright 2004 Rame E!masri and "ham#ant $a%athe
Elmasri!a"athe, #undamentals o$ %ata&ase S'stems, #ourth Edition
Views in SQL

A /iew is a :/irt"al@ table t+at is deri/ed


0rom ot+er tables

Allows 0or limited ",date o,erations


1sin#e t+e table ma* not ,+*si#all* be
stored2

Allows 0"ll 3"er* o,erations

A #on/enien#e 0or e9,ressing #ertain


o,erations
Chapter 9-**
Copyright 2004 Rame E!masri and "ham#ant $a%athe
Elmasri!a"athe, #undamentals o$ %ata&ase S'stems, #ourth Edition
S,e#i0i#ation o0 Views

SQL #ommand6 CREATE IEW


8
a table 1/iew2 name
8
a ,ossible list o0 attrib"te names 10or
e9am,le$ w+en arit+meti# o,erations are
s,e#i0ied or w+en we want t+e names to be
di00erent 0rom t+e attrib"tes in t+e base
relations2
8
a 3"er* to s,e#i0* t+e table #ontents
Chapter 9-*2
Copyright 2004 Rame E!masri and "ham#ant $a%athe
Elmasri!a"athe, #undamentals o$ %ata&ase S'stems, #ourth Edition
SQL Views6 An E9am,le

S,e#i0* a di00erent A-BCSD-E table


CREATE TABLE WORKS_ON_NEW AS
SELECT FNAME, LNAME, PNAME, HOURS
FROM EMPLOYEE, PRO"ECT, WORKS_ON
WHERE SSN=ESSN AND PNO=PNUMBER
GROUP BY PNAME!
Chapter 9-*3
Copyright 2004 Rame E!masri and "ham#ant $a%athe
Elmasri!a"athe, #undamentals o$ %ata&ase S'stems, #ourth Edition
>sing a Virt"al ;able

Ae #an s,e#i0* SQL 3"eries on a newl*


#reate table 1/iew26
SELECT FNAME, LNAME FROM WORKS_ON_NEW
WHERE PNAME=#S$$%&'!

A+en no longer needed$ a /iew #an be


dro,,ed6
DROP WORKS_ON_NEW!
Chapter 9-*4
Copyright 2004 Rame E!masri and "ham#ant $a%athe
Elmasri!a"athe, #undamentals o$ %ata&ase S'stems, #ourth Edition
E00i#ient View &m,lementation

Q"er* modi0i#ation6 ,resent t+e /iew


3"er* in terms o0 a 3"er* on t+e
"nderl*ing base tables
8
disad/antage6 ine00i#ient 0or /iews de0ined
/ia #om,le9 3"eries 1es,e#iall* i0 additional
3"eries are to be a,,lied to t+e /iew wit+in
a s+ort time ,eriod2
Chapter 9-*&
Copyright 2004 Rame E!masri and "ham#ant $a%athe
Elmasri!a"athe, #undamentals o$ %ata&ase S'stems, #ourth Edition
E00i#ient View &m,lementation

View materialiFation6 in/ol/es ,+*si#all*


#reating and <ee,ing a tem,orar* table
8
ass"m,tion6 ot+er 3"eries on t+e /iew will
0ollow
8
#on#erns6 maintaining #orres,onden#e
between t+e base table and t+e /iew w+en
t+e base table is ",dated
8
strateg*6 in#remental ",date
Chapter 9-*'
Copyright 2004 Rame E!masri and "ham#ant $a%athe
Elmasri!a"athe, #undamentals o$ %ata&ase S'stems, #ourth Edition
View >,date

>,date on a single /iew wit+o"t


aggregate o,erations6 ",date ma* ma,
to an ",date on t+e "nderl*ing base
table

Views in/ol/ing .oins6 an ",date may


ma, to an ",date on t+e "nderl*ing base
relations
8
not alwa*s ,ossible
Chapter 9-*(
Copyright 2004 Rame E!masri and "ham#ant $a%athe
Elmasri!a"athe, #undamentals o$ %ata&ase S'stems, #ourth Edition
>n4",datable Views

Views de0ined "sing gro",s and


aggregate 0"n#tions are not ",dateable

Views de0ined on m"lti,le tables "sing


.oins are generall* not ",dateable

WITH CHECK OPTION6 m"st be added to


t+e de0inition o0 a /iew i0 t+e /iew is to be
",dated
8
to allow #+e#< 0or ",databilit* and to ,lan
0or an e9e#"tion strateg*
Chapter 9-*)
Copyright 2004 Rame E!masri and "ham#ant $a%athe
Elmasri!a"athe, #undamentals o$ %ata&ase S'stems, #ourth Edition
Database Programming

-b.e#ti/e6 to a##ess a database 0rom an


a,,li#ation ,rogram 1as o,,osed to
intera#ti/e inter0a#es2

A+*G An intera#ti/e inter0a#e is


#on/enient b"t not s"00i#ient? a ma.orit*
o0 database o,erations are made t+r"
a,,li#ation ,rograms 1nowada*s t+r"
web a,,li#ations2
Chapter 9-*9
Copyright 2004 Rame E!masri and "ham#ant $a%athe
Elmasri!a"athe, #undamentals o$ %ata&ase S'stems, #ourth Edition
Database Programming
A,,roa#+es

Embedded #ommands6 database


#ommands are embedded in a
general4,"r,ose ,rogramming lang"age

Librar* o0 database 0"n#tions6 a/ailable


to t+e +ost lang"age 0or database #alls?
<nown as an API

A brand new$ 0"ll40ledged lang"age


1minimiFes im,edan#e mismat#+2
Chapter 9-20
Copyright 2004 Rame E!masri and "ham#ant $a%athe
Elmasri!a"athe, #undamentals o$ %ata&ase S'stems, #ourth Edition
&m,edan#e (ismat#+

&n#om,atibilities between a +ost


,rogramming lang"age and t+e
database model$ e.g.$
8
t*,e mismat#+ and in#om,atibilities?
re3"ires a new binding 0or ea#+ lang"age
8
set /s. re#ord4at4a4time ,ro#essing

need s,e#ial iterators to loo, o/er 3"er* res"lts


and mani,"late indi/id"al /al"es
Chapter 9-2*
Copyright 2004 Rame E!masri and "ham#ant $a%athe
Elmasri!a"athe, #undamentals o$ %ata&ase S'stems, #ourth Edition
Ste,s in Database Programming
1. Client ,rogram o,ens a #onne#tion to
t+e database ser/er
2. Client ,rogram s"bmits 3"eries to
and%or ",dates t+e database
3. A+en database a##ess is no longer
needed$ #lient ,rogram terminates t+e
#onne#tion
Chapter 9-22
Copyright 2004 Rame E!masri and "ham#ant $a%athe
Elmasri!a"athe, #undamentals o$ %ata&ase S'stems, #ourth Edition
Embedded SQL

(ost SQL statements #an be embedded


in a general4,"r,ose host ,rogramming
lang"age s"#+ as C-H-L$ C$ 5a/a

An embedded SQL statement is


disting"is+ed 0rom t+e +ost lang"age
statements b* EXEC S(L and a
mat#+ing END)EXEC 1or semi#olon2
8
shared variables 1"sed in bot+ lang"ages2
"s"all* ,re0i9ed wit+ a #olon 162 in SQL
Chapter 9-23
Copyright 2004 Rame E!masri and "ham#ant $a%athe
Elmasri!a"athe, #undamentals o$ %ata&ase S'stems, #ourth Edition
E9am,le6 Variable De#laration
in Lang"age C

Variables inside DECLARE are s+ared and #an a,,ear


1w+ile ,re0i9ed b* a #olon2 in SQL statements

S(LCODE is "sed to #omm"ni#ate errors%e9#e,tions


between t+e database and t+e ,rogram
*%+ ,--.!
EXEC S(L BEGIN DECLARE SECTION!
/&012&0 3%&4$5678, 9%&4$5678, :!
12&0 ;;%56<8, =3&+$5668, :!
*%+ 3%-, 3%>4=$0, S(LCODE, :!
EXEC S(L END DECLARE SECTION!
Chapter 9-24
Copyright 2004 Rame E!masri and "ham#ant $a%athe
Elmasri!a"athe, #undamentals o$ %ata&ase S'stems, #ourth Edition
SQL Commands 0or
Conne#ting to a Database

Conne#tion 1m"lti,le #onne#tions are


,ossible b"t onl* one is a#ti/e2
CONNECT TO ;$0/$0)%&4$ AS 1-%%$1+*-%)%&4$
AUTHORI?ATION >;$0)&11->%+)*%9-!

C+ange 0rom an a#ti/e #onne#tion to


anot+er one
SET CONNECTION 1-%%$1+*-%)%&4$!

Dis#onne#tion
DISCONNECT 1-%%$1+*-%)%&4$!
Chapter 9-2&
Copyright 2004 Rame E!masri and "ham#ant $a%athe
Elmasri!a"athe, #undamentals o$ %ata&ase S'stems, #ourth Edition
Embedded SQL in C
Programming E9am,les
,--. = 6!
@2*,$ (,--.) A
.0-4.+ (BE%+$0 SSNC B, ;;%)!
EXEC S(L
;$,$1+ FNAME, LNAME, ADDRESS, SALARY
*%+- C9%&4$, C,%&4$, C&330$;;, C;&,&0D
90-4 EMPLOYEE @2$0$ SSN == C;;%!
*9 (S(LCODE == <) .0*%+9(9%&4$, :)!
$,;$ .0*%+9(BSSN 3-$; %-+ $E*;+C B, ;;%)!
.0-4.+(BM-0$ SSNF (6=D$;, <=%-)C B, ,--.)!
END)EXEC
G
Chapter 9-2'
Copyright 2004 Rame E!masri and "ham#ant $a%athe
Elmasri!a"athe, #undamentals o$ %ata&ase S'stems, #ourth Edition
Embedded SQL in C
Programming E9am,les

A cursor 1iterator2 is needed to ,ro#ess


m"lti,le t",les

FETCH #ommands mo/e t+e #"rsor to


t+e ne9t t",le

CLOSE CURSOR indi#ates t+at t+e


,ro#essing o0 3"er* res"lts +as been
#om,leted
Chapter 9-2(
Copyright 2004 Rame E!masri and "ham#ant $a%athe
Elmasri!a"athe, #undamentals o$ %ata&ase S'stems, #ourth Edition
D*nami# SQL

-b.e#ti/e6 e9e#"ting new 1not ,re/io"sl*


#om,iled2 SQL statements at r"n4time
8
a ,rogram a##e,ts SQL statements 0rom t+e
<e*board at r"n4time
8
a ,oint4and4#li#< o,eration translates to #ertain SQL
3"er*

D*nami# ",date is relati/el* sim,le? d*nami#


3"er* #an be #om,le9
8
be#a"se t+e t*,e and n"mber o0 retrie/ed attrib"tes
are "n<nown at #om,ile time
Chapter 9-2)
Copyright 2004 Rame E!masri and "ham#ant $a%athe
Elmasri!a"athe, #undamentals o$ %ata&ase S'stems, #ourth Edition
D*nami# SQL6 An E9am,le
EXEC S(L BEGIN DECLARE SECTION!
/&012&0 ;H,>.3&+$;+0*%I5JK78!
EXEC S(L END DECLARE SECTION!
:
.0-4.+ (BE%+$0 >.3&+$ 1-44&%3CB, ;H,>.3&+$;+0*%I)!
EXEC S(L PREPARE ;H,1-44&%3 FROM C;H,>.3&+$;+0*%I!
EXEC S(L EXECUTE ;H,1-44&%3!
Chapter 9-29
Copyright 2004 Rame E!masri and "ham#ant $a%athe
Elmasri!a"athe, #undamentals o$ %ata&ase S'stems, #ourth Edition
Embedded SQL in 5a/a

SQL56 a standard 0or embedding SQL in


5a/a

An SQL5 translator #on/erts SQL


statements into 5a/a 1to be e9e#"ted
t+r" t+e JDBC inter0a#e2

Certain #lasses$ e.g.$ L&/&.;H, +a/e to


be im,orted
Chapter 9-30
Copyright 2004 Rame E!masri and "ham#ant $a%athe
Elmasri!a"athe, #undamentals o$ %ata&ase S'stems, #ourth Edition
5a/a Database Conne#ti/it*

5DHC6 SQL #onne#tion 0"n#tion #alls 0or


5a/a ,rogramming

A 5a/a ,rogram wit+ 5DHC 0"n#tions


#an a##ess an* relational DH(S t+at
+as a 5DHC dri/er

5DHC allows a ,rogram to #onne#t to


se/eral databases 1<nown as data
sources2
Chapter 9-3*
Copyright 2004 Rame E!masri and "ham#ant $a%athe
Elmasri!a"athe, #undamentals o$ %ata&ase S'stems, #ourth Edition
Ste,s in 5DHC Database A##ess
1. &m,ort 5DHC librar* (L&/&.;H,.*)
2. Load 5DHC dri/er6
C,&;;.9-0%&4$(B-0&1,$.L3=1.30*/$0.O0&1,$D0*/$0M)
3. De0ine a,,ro,riate /ariables
4. Create a #onne#t ob.e#t 1/ia I$+C-%%$1+*-%2
. Create a statement ob.e#t 0rom t+e
S+&+$4$%+ #lass6
6. P0$.&0$3S+&+4$%+
J. C&,,&=,$S+&+$4$%+
Chapter 9-32
Copyright 2004 Rame E!masri and "ham#ant $a%athe
Elmasri!a"athe, #undamentals o$ %ata&ase S'stems, #ourth Edition
Ste,s in 5DHC Database A##ess
1#ontin"ed2
'. &denti0* statement ,arameters 1to be
designated b* 3"estion mar<s2
). Ho"nd ,arameters to ,rogram
/ariables
7. E9e#"te SQL statement 1re0eren#ed b*
an ob.e#t2 /ia 5DHC=s $E$1>+$(>$0D
9. Pro#ess 3"er* res"lts 1ret"rned in an
ob.e#t o0 t*,e R$;>,+S$+2
N
R$;>,+S$+ is a 24dimentional table
Chapter 9-33
Copyright 2004 Rame E!masri and "ham#ant $a%athe
Elmasri!a"athe, #undamentals o$ %ata&ase S'stems, #ourth Edition
Embedded SQL in 5a/a6
An E9am,le
;;% = 0$&3E%+0D(BE%+$0 & SSNC B)!
+0D A
O;H,A;$,$1+ FNAMEP LNAME, ADDRESS, SALARY
*%+- C9%&4$, C,%&4$, C&330$;;, C;&,&0D
90-4 EMPLOYEE @2$0$ SSN = C;;%G!
G
1&+12 (S(LEE1$.+*-% ;$) A
SD;+$4.->+..0*%+,%(BSSN 3-$; %-+ $E*;+C B,Q;;%)!
0$+>0%!
G
SD;+$4.->+..0*%+,%(9%&4$QB BQ,%&4$Q: )!
Chapter 9-34
Copyright 2004 Rame E!masri and "ham#ant $a%athe
Elmasri!a"athe, #undamentals o$ %ata&ase S'stems, #ourth Edition
("lti,le ;",les in SQL5

SQL5 s",,orts two t*,es o0 iterators6


8
named iterator6 asso#iated wit+ a 3"er*
res"lt
8
positional iterator6 lists onl* attrib"te t*,es in
a 3"er* res"lt

A FETCH o,eration retrie/es t+e ne9t t",le


in a 3"er* res"lt6
9$+12 *+$0&+-0)/&0*&=,$ *%+- .0-I0&4)/&0*&=,$
Chapter 9-3&
Copyright 2004 Rame E!masri and "ham#ant $a%athe
Elmasri!a"athe, #undamentals o$ %ata&ase S'stems, #ourth Edition
Database Programming wit+
!"n#tional Calls

Embedded SQL ,ro/ides stati#


database ,rogramming

AP&6 d*nami# database ,rogramming


wit+ a librar* o0 0"n#tions
8
ad/antage6 no ,re,ro#essor needed 1t+"s
more 0le9ible2
8
drawba#<6 SQL s*nta9 #+e#<s to be done at
r"n4time
Chapter 9-3'
Copyright 2004 Rame E!masri and "ham#ant $a%athe
Elmasri!a"athe, #undamentals o$ %ata&ase S'stems, #ourth Edition
SQL Call Le/el &nter0a#e

A ,art o0 t+e SQL standard

Pro/ides eas* a##ess to se/eral


databases wit+in t+e same ,rogram

Certain libraries 1e.g.$ ;H,1,*.2 0or C2


+a/e to be installed and a/ailable

SQL statements are d*nami#all* #reated


and ,assed as string ,arameters in t+e
#alls
Chapter 9-3(
Copyright 2004 Rame E!masri and "ham#ant $a%athe
Elmasri!a"athe, #undamentals o$ %ata&ase S'stems, #ourth Edition
Com,onents o0 SQL%CL&

Environment record6 <ee,s tra#< o0


database #onne#tions

Connection record6 <ee, tra#<s o0 in0o


needed 0or a ,arti#"lar #onne#tion

Statement record6 <ee,s tra#< o0 in0o


needed 0or one SQL statement

Description record6 <ee,s tra#< o0 t",les


Chapter 9-3)
Copyright 2004 Rame E!masri and "ham#ant $a%athe
Elmasri!a"athe, #undamentals o$ %ata&ase S'stems, #ourth Edition
Ste,s in C and SQL%CL&
Programming
1. Load SQL%CL& libraries
2. De#lare re#ord +andle /ariables 0or t+e
abo/e #om,onents 1#alled6 S(LHSTMT, S(LHDBC,
S(LHEN, S(LHDEC2
3. Set ", an en/ironment re#ord "sing
S(LA,,-1H&%3,$
4. Set ", a #onne#tion re#ord "sing
S(LA,,-1H&%3,$
. Set ", a statement re#ord "sing
S(LA,,-1H&%3,$
Chapter 9-39
Copyright 2004 Rame E!masri and "ham#ant $a%athe
Elmasri!a"athe, #undamentals o$ %ata&ase S'stems, #ourth Edition
Ste,s in C and SQL%CL&
Programming 1#ontin"ed2
'. Pre,are a statement "sing SQL%CL&
0"n#tion S(LP0$.&0$
). Ho"nd ,arameters to ,rogram /ariables
7. E9e#"te SQL statement /ia S(LEE$1>+$
9. Ho"nd #ol"mns in a 3"er* to a C
/ariable /ia S(LB*%3C-,
1I. >se S(LF$+12 to retrie/e #ol"mn /al"es
into C /ariables
Chapter 9-40
Copyright 2004 Rame E!masri and "ham#ant $a%athe
Elmasri!a"athe, #undamentals o$ %ata&ase S'stems, #ourth Edition
Database Stored Pro#ed"res

Persistent ,ro#ed"res%0"n#tions 1mod"les2 are


stored lo#all* and e9e#"ted b* t+e database
ser/er 1as o,,osed to e9e#"tion b* #lients2

Ad/antages6
8
i0 t+e ,ro#ed"re is needed b* man* a,,li#ations$ it
#an be in/o<ed b* an* o0 t+em 1t+"s red"#e
d",li#ations2
8
e9e#"tion b* t+e ser/er red"#es #omm"ni#ation
#osts
8
en+an#e t+e modeling ,ower o0 /iews
Chapter 9-4*
Copyright 2004 Rame E!masri and "ham#ant $a%athe
Elmasri!a"athe, #undamentals o$ %ata&ase S'stems, #ourth Edition
Stored Pro#ed"re Constr"#ts

A stored ,ro#ed"re
CREATE PROCEDURE .0-1$3>0$)%&4$ (.&0&4;)
,-1&,)3$1,&0&+*-%;
.0-1$3>0$)=-3D!

A stored 0"n#tion
CREATE FUNCTION 9>%)%&4$ (.&0&4;) RETRUNS 0$+>0%)+D.$
,-1&,)3$1,&0&+*-%;
9>%1+*-%)=-3D!

Calling a ,ro#ed"re or 0"n#tion


CALL .0-1$3>0$)%&4$R9>%)%&4$ (&0I>4$%+;)!
Chapter 9-42
Copyright 2004 Rame E!masri and "ham#ant $a%athe
Elmasri!a"athe, #undamentals o$ %ata&ase S'stems, #ourth Edition
SQL Persistent Stored (od"les

SQL%PS(6 ,art o0 t+e SQL standard 0or


writing ,ersistent stored mod"les

SQL J stored ,ro#ed"res%0"n#tions J


additional ,rogramming #onstr"#ts
8
e.g.$ bran#+ing and loo,ing statements
8
en+an#e t+e ,ower o0 SQL
Chapter 9-43
Copyright 2004 Rame E!masri and "ham#ant $a%athe
Elmasri!a"athe, #undamentals o$ %ata&ase S'stems, #ourth Edition
SQL%PS(6 An E9am,le
CREATE FUNCTION DEPT_SI?E (IN 3$.+%- INTEGER)
RETURNS ARCHAR5S8
DECLARE TOT_EMPS INTEGER!
SELECT COUNT (*) INTO TOT_EMPS
FROM SELECT EMPLOYEE WHERE DNO = 3$.+%-!
IF TOT_EMPS > 6<< THEN RETURN BHUGEM
ELSEIF TOT_EMPS > K< THEN RETURN BLARGEM
ELSEIF TOT_EMPS > T< THEN RETURN BMEDIUMM
ELSE RETURN BSMALLM
ENDIF!
Chapter 9-44
Copyright 2004 Rame E!masri and "ham#ant $a%athe
Elmasri!a"athe, #undamentals o$ %ata&ase S'stems, #ourth Edition
S"mmar*

Assertions ,ro/ide a means to s,e#i0*


additional #onstraints

;riggers are a s,e#ial <ind o0


assertions? t+e* de0ine a#tions to be
ta<en w+en #ertain #onditions o##"r

Views are a #on/enient means 0or


#reating tem,orar* 1/irt"al2 tables
Chapter 9-4&
Copyright 2004 Rame E!masri and "ham#ant $a%athe
Elmasri!a"athe, #undamentals o$ %ata&ase S'stems, #ourth Edition
S"mmar* 1#ontin"ed2

A database ma* be a##essed /ia an


intera#ti/e database

(ost o0ten$ +owe/er$ data in a database is


mani,"late /ia a,,li#ation ,rograms

Se/eral met+ods o0 database ,rogramming6


8
embedded SQL
8
d*nami# SQL
8
stored ,ro#ed"re and 0"n#tion

You might also like