SQL-99: Schema Definition, Basic Constraints, and Queries
SQL-99: Schema Definition, Basic Constraints, and Queries
Chapter 8
SQL-99: Schema
Definition, Basic
Constraints, and Queries
Slide 8-2
Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition
Copyright 2004 Rae! Elasri and Sha"ant #a$athe
Data Definition, Constraints,
and Schema Changes
Synta/0
E/aple0
DROP TABLE DEPENDENT;
Slide 8-C
Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition
Copyright 2004 Rae! Elasri and Sha"ant #a$athe
ALTER TABLE
'he ne6 attri,ute 6ill ha$e #%**s in all the tuples o+ the
relation right a+ter the coand is e/ecuted3 hence, the
#)' #%** constraint is not allowed +or such an attri,ute
E/aple0
ALTER TABLE EMPLOYEE ADD JOB
VARCHAR(1);
'he data,ase users ust still enter a $alue +or the ne6
attri,ute D)1 +or each E9P*)@EE tuple. 'his can ,e
done using the %P(&'E coand.
Slide 8-8
Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition
Copyright 2004 Rae! Elasri and Sha"ant #a$athe
Featres Added in S!L" and
S!L#$$
CREATE SCHEMA
REFERENTIAL INTEGRITY
OPTIONS
Slide 8-E
Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition
Copyright 2004 Rae! Elasri and Sha"ant #a$athe
CREATE SC%E&A
<ernati$ely
G
CREATE DOMAIN (2#%9 &S I#'E7ER
C;EC? -(2#%9J0 &#( (2#%9K24.
G
(#%91ER (2#%9
Slide 8-44
Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition
Copyright 2004 Rae! Elasri and Sha"ant #a$athe
Additiona( Data Types in
S!L" and S!L#$$
;as (&'E, 'I9E, and 'I9ES'&9P data types
DATE!
G
9ade up o+ year-onth-day in the +orat yyyy--dd
TIME!
G
9ade up o+ hour0inute0second in the +orat hh00ss
TIME(")!
G
9ade up o+ hour0inute0second plus i additional digits
speci+ying +ractions o+ a second
G
+orat is hh00ss0ii...i
TIMESTAMP!
G
;as ,oth (&'E and 'I9E coponents
Slide 8-42
Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition
Copyright 2004 Rae! Elasri and Sha"ant #a$athe
Additiona( Data Types in
S!L" and S!L#$$ )cont*+
INTERVAL!
G
Speci+ies a relati$e $alue rather than an a,solute $alue
G
Can ,e (&@L'I9E inter$als or @E&RL9)#';
inter$als
G
Can ,e positi$e or negati$e 6hen added to or
su,tracted +ro an a,solute $alue, the result is an
a,solute $alue
Slide 8-45
Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition
Copyright 2004 Rae! Elasri and Sha"ant #a$athe
Referentia( ,ntegrity )R,+
(e+ault action
G
Re:ect the update operation that 6ill cause the
$iolation
>uery 40 Retrie$e the nae and address o+ all eployees 6ho 6or"
+or the OResearchO departent.
Q1! SELECT FNAME, LNAME, ADDRESS
FROM EMPLOYEE, DEPARTMENT
8HERE DNAME9:R#.#(%,4: AND
DNUMBER9DNO
G
Siilar to a SE*EC'-PR)DEC'-D)I# seBuence o+ relational
alge,ra operations
G
-(#&9EPOResearchO. is a seletion ondition -corresponds to a
SE*EC' operation in relational alge,ra.
G
-(#%91ERP(#). is a #oin ondition -corresponds to a D)I#
operation in relational alge,ra.
Slide 8-22
Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition
Copyright 2004 Rae! Elasri and Sha"ant #a$athe
Simp(e S!L !eries )cont*+
>uery 20 8or e$ery pro:ect located in OSta++ordO, list the pro:ect nu,er, the
controlling departent nu,er, and the departent anagerOs last nae,
address, and ,irthdate.
Q! SELECT PNUMBER, DNUM, LNAME, BDATE, ADDRESS
FROM PROJECT, DEPARTMENT, EMPLOYEE
8HERE DNUM9DNUMBER AND MGRSSN9SSN
AND PLOCATION9:S'($$-%+:
G
In >2, there are two :oin conditions
G
'he :oin condition (#%9P(#%91ER relates a pro:ect to its controlling
departent
G
'he :oin condition 97RSS#PSS# relates the controlling departent to
the eployee 6ho anages that departent
Slide 8-25
Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition
Copyright 2004 Rae! Elasri and Sha"ant #a$athe
A(iases, 3 and D,ST,-CT,
Empty 4%ERE#c(ase
In S>*, 6e can use the sae nae +or t6o -or ore.
attri,utes as long as the attri,utes are in diffe"ent "elations
& Buery that re+ers to t6o or ore attri,utes 6ith the sae
nae ust %ualif$ the attri,ute nae 6ith the relation
nae ,y p"efi&ing the relation nae to the attri,ute nae
E/aple0
E9P*)@EE.*#&9E, (EP&R'9E#'.(#&9E
Slide 8-24
Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition
Copyright 2004 Rae! Elasri and Sha"ant #a$athe
AL,ASES
>uery 80 8or each eployee, retrie$e the eployeeOs nae, and the nae
o+ his or her iediate super$isor.
Q<! SELECT E=FNAME, E=LNAME, S=FNAME,
S=LNAME
FROM EMPLOYEE E S
8HERE E=SUPERSSN9S=SSN
G
In >8, the alternate relation naes E and S are called aliases or tuple
'a"iables +or the E9P*)@EE relation
G
Me can thin" o+ E and S as t6o diffe"ent opies o+ E9P*)@EE3 E
represents eployees in role o+ supe"'isees and S represents
eployees in role o+ supe"'iso"s
Slide 8-2=
Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition
Copyright 2004 Rae! Elasri and Sha"ant #a$athe
AL,ASES )cont*+
G
&liasing can also ,e used in any S>* Buery +or con$enience
Can also use the &S "ey6ord to speci+y aliases
Q<! SELECT E=FNAME, E=LNAME, S=FNAME,
S=LNAME
FROM EMPLOYEE AS E, EMPLOYEE AS S
8HERE E=SUPERSSN9S=SSN
Slide 8-2A
Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition
Copyright 2004 Rae! Elasri and Sha"ant #a$athe
5-SPEC,F,ED
4%ERE#c(ase
E/aple0
Q10! SELECT SSN, DNAME
FROM EMPLOYEE, DEPARTMENT
G
It is e/treely iportant not to o$erloo" speci+ying any selection and
:oin conditions in the M;ERE-clause3 other6ise, incorrect and $ery
large relations ay result
Slide 8-28
Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition
Copyright 2004 Rae! Elasri and Sha"ant #a$athe
Simp(e E6amp(es
create data,ase courses2students3
use courses2students3
Q 'a,le de+inition
create ta,le courses -
cid int-40. #)' #%** auto2increent,
cnae $archar-50. #)' #%**,
credit tinyint-2. #)' #%**,
croo $archar-40.,
priary "ey -cid.
.3
create ta,le students -
sid int-40. unsigned #)' #%**
auto2increent,
snae $archar-50. #)' #%** de+ault OO,
syear tinyint-2. unsigned,
priary "ey -sid.
.3
create ta,le selected -
cid int-40. unsigned,
sid int-40. unsigned
.3
Q (ata
insert into courses $alues -4, O(19SO, 5, O50CO.3
insert into courses $alues -2, O)SO, 5, O40AO.3
insert into courses $alues -5, O&lgorithO, 5, O50CO.3
insert into students $alues -4, OSaO, 4.3
insert into students $alues -2, ODoeO, 5.3
insert into students $alues -5, O9aryO, 5.3
insert into students $alues -4, ODohnO, 5.3
insert into selected $alues -4, 4.3
insert into selected $alues -4, 2.3
insert into selected $alues -4, 4.3
insert into selected $alues -2, 4.3
insert into selected $alues -2, 5.3
insert into selected $alues -2, 4.3
insert into selected $alues -2, 2.3
insert into selected $alues -5, 2.3
insert into selected $alues -5, 4.3
Slide 8-2E
Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition
Copyright 2004 Rae! Elasri and Sha"ant #a$athe
7oin Operations
select selected.cid, selected.sid, snae
+ro selected, students
@4#%# .#)#,'#+=."+ 9 .'?+#&'.=."+;
R------R------R-------R
S cid S sid S snae S
R------R------R-------R
S 4 S 2 S Doe S
S 4 S 4 S Dohn S
S 4 S 4 S Sa S
S 2 S 2 S Doe S
S 2 S 5 S 9ary S
S 2 S 4 S Dohn S
S 2 S 4 S Sa S
S 5 S 4 S Dohn S
S 5 S 2 S Doe S
R------R------R-------R
select T +ro
students3
R-----R-------R-------R
S sid S snae S syear S
R-----R-------R-------R
S 4 S Sa S 4 S
S 2 S Doe S 5 S
S 5 S 9ary S 5 S
S 4 S Dohn S 5 S
R-----R-------R-------R
select T +ro selected3
R------R------R
S cid S sid S
R------R------R
S 4 S 4 S
S 4 S 2 S
S 4 S 4 S
S 2 S 4 S
S 2 S 5 S
S 2 S 4 S
S 2 S 2 S
S 5 S 2 S
S 5 S 4 S
R------R------R
I+ oitted,
5A ro6s
6ill result.
Slide 8-50
Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition
Copyright 2004 Rae! Elasri and Sha"ant #a$athe
5SE OF 3
>uery 40 9a"e a list o+ all pro:ect nu,ers +or pro:ects that in$ol$e an
eployee 6hose last nae is OSithO as a 6or"er or as a anager o+
the departent that controls the pro:ect.
QC! (SELECT PNAME
FROM PROJECT, DEPARTMENT, EMPLOYEE
8HERE DNUM9DNUMBER AND MGRSSN9SSN
AND LNAME9:S1"'4:)
UNION (SELECT PNAME
FROM PROJECT, 8ORKSDON, EMPLOYEE
8HERE PNUMBER9PNO AND ESSN9SSN AND
LNAME9:S1"'4:)
Slide 8-54
Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition
Copyright 2004 Rae! Elasri and Sha"ant #a$athe
-EST,-. OF !5ER,ES
>uery 40 Retrie$e the nae and address o+ all eployees 6ho 6or"
+or the OResearchO departent.
Q1! SELECT FNAME, LNAME, ADDRESS
FROM EMPLOYEE
8HERE DNO IN (SELECT DNUMBER
FROM DEPARTMENT
8HERE DNAME9:R#.#(%,4: )
Slide 8-5=
Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition
Copyright 2004 Rae! Elasri and Sha"ant #a$athe
-EST,-. OF !5ER,ES
)cont*+
'he outer Buery select an E9P*)@EE tuple i+ its (#) $alue is in the
result o+ either nested Buery
In this e/aple, the nested Buery is not o""elated 6ith the outer Buery
Slide 8-5A
Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition
Copyright 2004 Rae! Elasri and Sha"ant #a$athe
CORRELATED -ESTED
!5ER,ES
'he result o+ a correlated nested Buery is diffe"ent fo" eah tuple (o"
ombination of tuples) of the "elation(s) the oute" %ue"$
>uery 420 Retrie$e the nae o+ each eployee 6ho has a dependent
6ith the sae +irst nae as the eployee.
Q1! SELECT E=FNAME, E=LNAME
FROM EMPLOYEE AS E
8HERE E=SSN IN (SELECT ESSN
FROM DEPENDENT
8HERE ESSN9E=SSN AND
E=FNAME9DEPENDENTDNAME)
Slide 8-5C
Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition
Copyright 2004 Rae! Elasri and Sha"ant #a$athe
CORRELATED -ESTED
!5ER,ES )cont*+
G In >42, the nested Buery has a di++erent result fo" eah tuple in the outer
Buery
G & Buery 6ritten 6ith nested SE*EC'... 8R)9... M;ERE... ,loc"s and
using the P or I# coparison operators can always ,e e/pressed as a
single ,loc" Buery. 8or e/aple, >42 ay ,e 6ritten as in >42&
Q1A! SELECT E=FNAME, E=LNAME
FROM EMPLOYEE E, DEPENDENT D
8HERE E=SSN9D=ESSN AND
E=FNAME9D=DEPENDENTDNAME
G
'he original S>* as speci+ied +or S@S'E9 R also had a CONTAINS
coparison operator, 6hich is used in con:unction 6ith nested correlated
Bueries
G 'his operator 6as dropped +ro the language, possi,ly ,ecause o+ the
di++iculty in ipleenting it e++iciently
Slide 8-58
Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition
Copyright 2004 Rae! Elasri and Sha"ant #a$athe
CORRELATED -ESTED
!5ER,ES )cont*+
G 9ost ipleentations o+ S>* do not ha$e this operator
G 'he C)#'&I#S operator copares t6o sets of 'alues , and returns
'R%E i+ one set contains all $alues in the other set
-reiniscent o+ the di'ision operation o+ alge,ra..
>uery 50 Retrie$e the nae o+ each eployee 6ho 6or"s on all the pro:ects
controlled ,y departent nu,er =.
Q/! SELECT FNAME, LNAME
FROM EMPLOYEE
8HERE ( (SELECT PNO
FROM 8ORKSDON
8HERE SSN9ESSN)
CONTAINS
(SELECT PNUMBER
FROM PROJECT
8HERE DNUM9B) )
Slide 8-5E
Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition
Copyright 2004 Rae! Elasri and Sha"ant #a$athe
CORRELATED -ESTED
!5ER,ES )cont*+
G
In >5, the second nested Buery, 6hich is not correlated
6ith the outer Buery, retrie$es the pro:ect nu,ers o+ all
pro:ects controlled ,y departent =
G
'he +irst nested Buery, 6hich is correlated, retrie$es the
pro:ect nu,ers on 6hich the eployee 6or"s, 6hich is
di++erent fo" eah emplo$ee tuple ,ecause o+ the
correlation
Slide 8-40
Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition
Copyright 2004 Rae! Elasri and Sha"ant #a$athe
T%E E8,STS F5-CT,O-
E/aples0
Q<! SELECT E=FNAME, E=LNAME, S=FNAME, S=LNAME
FROM EMPLOYEE E S
8HERE E=SUPERSSN9S=SSN
can ,e 6ritten as0 -#ote0 there is one ro6 in di++erence.
Q<! SELECT E=FNAME, E=LNAME, S=FNAME, S=LNAME
FROM (EMPLOYEE E LEFT OUTER JOIN EMPLOYEE S
ON E=SUPERSSN9S=SSN)
Q1! SELECT FNAME, LNAME, ADDRESS
FROM EMPLOYEE, DEPARTMENT
8HERE DNAME9:R#.#(%,4: AND DNUMBER9DNO
Slide 8-4C
Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition
Copyright 2004 Rae! Elasri and Sha"ant #a$athe
7oined Re(ations Featre
in S!L" )cont*+
¬her E/aple3
G
>2 could ,e 6ritten as +ollo6s3 this illustrates ultiple
:oins in the :oined ta,les
Q! SELECT PNUMBER, DNUM,
LNAME, BDATE,
ADDRESS
FROM ((PROJECT JOIN
DEPARTMENT ON
DNUM9DNUMBER) JOIN
EMPLOYEE ON
MGRSSN9SSN)
8HERE PLOCATION9:S'($$-%+;
Slide 8-4E
Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition
Copyright 2004 Rae! Elasri and Sha"ant #a$athe
A..RE.ATE F5-CT,O-S
>uery 4=0 8ind the a/iu salary, the iniu salary, and
the a$erage salary aong all eployees.
Q1B! SELECT MAE(SALARY),
MIN(SALARY), AVG(SALARY)
FROM EMPLOYEE
G
Soe S>* ipleentations ma$ not allow mo"e than one
funtion in the SE*EC'-clause
Slide 8-=0
Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition
Copyright 2004 Rae! Elasri and Sha"ant #a$athe
A..RE.ATE F5-CT,O-S
)cont*+
>uery 200 8or each departent, retrie$e the departent nu,er, the
nu,er o+ eployees in the departent, and their a$erage salary.
Q0! SELECT DNO, COUNT (A), AVG (SALARY)
FROM EMPLOYEE
GROUP BY DNO
G
In >20, the E9P*)@EE tuples are di$ided into groups--each
group ha$ing the sae $alue +or the grouping attri,ute (#)
G
'he C)%#' and &<7 +unctions are applied to each such group
o+ tuples separately
G
'he SE*EC'-clause includes only the grouping attri,ute and the
+unctions to ,e applied on each group o+ tuples
G
& :oin condition can ,e used in con:unction 6ith grouping
Slide 8-=4
Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition
Copyright 2004 Rae! Elasri and Sha"ant #a$athe
.RO5P,-. )cont*+
>uery 240 8or each pro:ect, retrie$e the pro:ect nu,er, pro:ect
nae, and the nu,er o+ eployees 6ho 6or" on that pro:ect.
Q1! SELECT PNUMBER, PNAME, COUNT (A)
FROM PROJECT, 8ORKSDON
8HERE PNUMBER9PNO
GROUP BY PNUMBER, PNAME
G
In this case, the grouping and +unctions are applied afte" the :oining o+
the t6o relations
>uery 2A0 Retrie$e all eployees 6ho 6ere ,orn during the
4E=0s. ;ere, O=O ust ,e the 8th character o+ the string
-according to our +orat +or date., so the 1(&'E $alue is
O2222222=2O, 6ith each underscore as a place holder +or a
single ar,itrary character.
QF! SELECT FNAME, LNAME
FROM EMPLOYEE
8HERE BDATE LIKE :DDDDDDDBD;
'he *I?E operator allo6s us to get around the +act that each
$alue is considered atoic and indi$isi,le3 hence, in S>*,
character string attri,ute $alues are not atoic
Slide 8-A0
Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition
Copyright 2004 Rae! Elasri and Sha"ant #a$athe
AR,T%&ET,C OPERAT,O-S
'he standard arithetic operators ORO, O-O. OTO, and OLO -+or addition,
su,traction, ultiplication, and di$ision, respecti$ely. can ,e
applied to nueric $alues in an S>* Buery result
>uery 2C0 Sho6 the e++ect o+ gi$ing all eployees 6ho 6or"
on the OProductHO pro:ect a 40V raise.
QI! SELECT FNAME, LNAME, 1=1ASALARY
FROM EMPLOYEE, 8ORKSDON, PROJECT
8HERE SSN9ESSN AND PNO9PNUMBER AND
PNAME9:P%-+?,'E;
Slide 8-A4
Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition
Copyright 2004 Rae! Elasri and Sha"ant #a$athe
ORDER B/
E/aple0
U1! INSERT INTO EMPLOYEE
VALUES (:R",4(%+:,:K:,:M(%"&":, :FB/9<FB/:, :/0ODECOB:,
:9< O(3 F-%#.',K('2,TE:, :M:, /I000,:9<IFBC/1:, C )
E/aples0
UCA! DELETE FROM EMPLOYEE
8HERE LNAME9:B%-@&;
UCB! DELETE FROM EMPLOYEE
8HERE SSN9:1/CBFI<9;
UCC! DELETE FROM EMPLOYEE
8HERE DNO IN
(SELECT DNUMBER
FROM DEPARTMENT
8HERE DNAME9:R#.#(%,4:)
UCD! DELETE FROM EMPLOYEE
Slide 8-C5
Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition
Copyright 2004 Rae! Elasri and Sha"ant #a$athe
5PDATE
'he re+erence to the S&*&R@ attri,ute on the le+t o+ P re+ers to the ne6
S&*&R@ $alue a+ter odi+ication
Slide 8-CA
Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition
Copyright 2004 Rae! Elasri and Sha"ant #a$athe
%ome 4or: