Vous êtes sur la page 1sur 76

Copyright 2004 Pearson Education, Inc.

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

%sed to CRE&'E, (R)P, and &*'ER the


descriptions o+ the ta,les -relations. o+ a
data,ase

Synta/0

CRE&'E (&'&1&SE data,ase2nae 3

CRE&'E '&1*E ta,le2nae -


colun2nae4 data2type,
colun2nae2 data2type, ....... .
Slide 8-5
Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition
Copyright 2004 Rae! Elasri and Sha"ant #a$athe
CREATE TABLE

Speci+ies a ne6 ,ase relation ,y gi$ing it a nae,


and speci+ying each o+ its attri,utes and their data
types -I#'E7ER, 8*)&', (ECI9&*-i,:.,
C;&R-n., <&RC;&R-n..

& constraint #)' #%** ay ,e speci+ied on an


attri,ute
CREATE TABLE DEPARTMENT
( DNAME VARCHAR(10) NOT NULL,
DNUMBER INTEGER NOT NULL,
MGRSSN CHAR(9),
MGRSTARTDATE CHAR(9) );
Slide 8-4
Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition
Copyright 2004 Rae! Elasri and Sha"ant #a$athe
CREATE TABLE: Data Type
Slide 8-=
Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition
Copyright 2004 Rae! Elasri and Sha"ant #a$athe
CREATE TABLE

In S>*2, can use the CRE&'E '&1*E coand +or


speci+ying the priary "ey attri,utes, secondary "eys, and
re+erential integrity constraints -+oreign "eys..

?ey attri,utes can ,e speci+ied $ia the PRI9&R@ ?E@


and %#I>%E phrases
CREATE TABLE DEPT
( DNAME VARCHAR(10) NOT NULL,
DNUMBER INTEGER NOT NULL,
MGRSSN CHAR(9),
MGRSTARTDATE CHAR(9),
PRIMARY KEY (DNUMBER),
UNIQUE (DNAME),
FOREIGN KEY (MGRSSN) REFERENCES EMP );
Slide 8-A
Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition
Copyright 2004 Rae! Elasri and Sha"ant #a$athe
DROP TABLE

%sed to reo$e a relation -,ase ta,le. and


its definition

'he relation can no longer ,e used in


Bueries, updates, or any other coands
since its description no longer e/ists

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

%sed to add an attri,ute to one o+ the ,ase relations

'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

the stateent can include all the eleentsF


de+initions.
G
Schea eleents include ta,les, constraints, $ie6s,
doains, and other constructs -such as authori!ation
grants.

&lternati$ely, the schea can ,e assigned a


nae and authori!ation identi+ier. 'he eleents
can ,e de+ined later.

EH0 schea o6ned ,y the user 6ith DS9I';


G
CRE&'E SC;E9& copany &%';)RII&'I)#
DS9I';3
Slide 8-40
Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition
Copyright 2004 Rae! Elasri and Sha"ant #a$athe
Attri'tes Constraints

Speci+y constraint on attri,ute0


G
(#%91ER I#' #)' #%** CHECK
-(#%91ER J 0 &#( (#%91ER K 24.

&lternati$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,+

RI constraints can ,e $iolated 6hen tuples


are inserted or deleted, or 6hen a +oreign
"ey or priary "ey is odi+ied

(e+ault action
G
Re:ect the update operation that 6ill cause the
$iolation

R#$#%#&'"() '%"**#%#+ (,'"-&.


G
SE' #%**, SE' (E8&%*', C&SC&(E
G
Mhen )# (E*E'E or )# %P(&'E
Slide 8-44
Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition
Copyright 2004 Rae! Elasri and Sha"ant #a$athe
REFERE-T,AL ,-TE.R,T/
OPT,O-S

Me can speci+y RESTRICT, CASCADE, SET NULL or


SET DEFAULT on re+erential integrity constraints
-+oreign "eys.
CREATE TABLE DEPT
( DNAME VARCHAR(10) NOT NULL,
DNUMBER INTEGER NOT NULL,
MGRSSN CHAR(9),
MGRSTARTDATE CHAR(9),
PRIMARY KEY (DNUMBER),
UNIQUE (DNAME),
FOREIGN KEY (MGRSSN) REFERENCES EMP
)# (E*E'E SE' (E8&%*' )# %P(&'E
C&SC&(E );
Slide 8-4=
Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition
Copyright 2004 Rae! Elasri and Sha"ant #a$athe
REFERE-T,AL ,-TE.R,T/
OPT,O-S )contined+
CREATE TABLE EMP
( ENAME VARCHAR(/0) NOT NULL,
ESSN CHAR(9) 0%"1(%2 3#2,
BDATE DATE,
DNO INTEGER DEFAULT 1 ,4#,3 (+&-50 (&+ +&-69)
SUPERSSN CHAR(9),
7 PRIMARY KEY (ESSN),
FOREIGN KEY (DNO) REFERENCES DEPT
ON DELETE SET DEFAULT ON UPDATE CASCADE,
FOREIGN KEY (SUPERSSN) REFERENCES EMP
ON DELETE SET NULL ON UPDATE CASCADE );
I+ the tuples +or a super$ising eployee is deleted, the $alue o+
S%PERSS# is set to #%**.
I+ SS# is updated, the ne6 $alue is cascaded to S%PERSS#.
Slide 8-4A
Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition
Copyright 2004 Rae! Elasri and Sha"ant #a$athe
Retrie0a( !eries in S!L

S>* has one ,asic stateent +or retrie$ing in+oration


+ro a data,ase3 the SE*EC' stateent

'his is not the same as the SE*EC' operation o+ the


relational alge,ra

Iportant distinction ,et6een S>* and the +oral


relational odel3 S>* allo6s a ta,le -relation. to ha$e t6o
or ore tuples that are identical in all their attri,ute $alues

;ence, an S>* relation -ta,le. is a multi-set -soeties


called a ,ag. o+ tuples3 it is not a set o+ tuples

S>* relations can ,e constrained to ,e sets ,y speci+ying


PRI9&R@ ?E@ or %#I>%E attri,utes, or ,y using the
(IS'I#C' option in a Buery
Slide 8-4C
Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition
Copyright 2004 Rae! Elasri and Sha"ant #a$athe
Retrie0a( !eries in S!L
)cont*+

1asic +or o+ the S>* SE*EC' stateent is called a


mapping or a SELECT-FROM-WHERE blo!
SELECT Kattri,ute listJ
FROM Kta,le listJ
8HERE KconditionJ
G
Kattri,ute listJ is a list o+ attri,ute naes 6hose $alues are to ,e
retrie$ed ,y the Buery
G
Kta,le listJ is a list o+ the relation naes reBuired to process the
Buery
G
KconditionJ is a conditional -1oolean. e/pression that identi+ies
the tuples to ,e retrie$ed ,y the Buery
Slide 8-48
Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition
Copyright 2004 Rae! Elasri and Sha"ant #a$athe
Re(ationa( Data'ase Schema##Figre 1*1
http0LL6e,.ntnu.edu.t6LNsatsengL
Slide 8-4E
Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition
Copyright 2004 Rae! Elasri and Sha"ant #a$athe
Pop(ated
Data'ase##Fig*1*2
Slide 8-20
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

1asic S>* Bueries correspond to using the SE*EC', PR)DEC', and


D)I# operations o+ the relational alge,ra

&ll su,seBuent e/aples use the C)9P&#@ data,ase

E/aple o+ a siple Buery on one relation

>uery 00 Retrie$e the ,irthdate and address o+ the eployee 6hose


nae is ODohn 1. SithO.
Q0! SELECT BDATE, ADDRESS
FROM EMPLOYEE
8HEREFNAME9:J-4&: AND MINIT9:B;
AND LNAME9:S1"'4;
G Siilar to a SE*EC'-PR)DEC' pair o+ relational alge,ra operations3 the
SE*EC'-clause speci+ies the p"o#etion att"ibutes and the
M;ERE-clause speci+ies the seletion ondition
G ;o6e$er, the result o+ the Buery ma$ ontain duplicate tuples
Slide 8-24
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 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

Soe Bueries need to re+er to the sae relation t6ice

In this case, aliases are gi$en to the relation nae

>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

& missing WHERE-lause indicates no condition3 hence,


all tuples o+ the relations in the 8R)9-clause are selected

'his is eBui$alent to the condition M;ERE 'R%E

>uery E0 Retrie$e the SS# $alues +or all eployees.


Q9! SELECT SSN
FROM EMPLOYEE

I+ ore than one relation is speci+ied in the 8R)9-clause


and there is &- >-"& ,-&+"'"-&, '4#& '4# CARTESIAN
PRODUCT -$ '?0)#. ". .#)#,'#+
Slide 8-2C
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 )cont*+

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

'o retrie$e all the attri,ute $alues o+ the selected tuples, a T is


used, 6hich stands +or all the att"ibutes
E/aples0
Q1C! SELECT A
FROM EMPLOYEE
8HERE DNO9B
Q1D! SELECT A
FROM EMPLOYEE, DEPARTMENT
8HERE DNAME9:R#.#(%,4: AND
DNO9DNUMBER
Slide 8-54
Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition
Copyright 2004 Rae! Elasri and Sha"ant #a$athe
5SE OF D,ST,-CT

S>* does not treat a relation as a set3 dupliate tuples an


appea"

'o eliinate duplicate tuples in a Buery result, the


"ey6ord DISTINCT is used

8or e/aple, the result o+ >44 ay ha$e duplicate


S&*&R@ $alues 6hereas >44& does not ha$e any
duplicate $alues
Q11! SELECT SALARY
FROM EMPLOYEE
Q11A! SELECT DISTINCT SALARY
FROM EMPLOYEE
Slide 8-52
Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition
Copyright 2004 Rae! Elasri and Sha"ant #a$athe
SET OPERAT,O-S

S>* has directly incorporated soe set operations

'here is a union operation -UNION), and in some


'e"sions o+ S>* there are set di++erence
-MINUS) and intersection -INTERSECT)
operations

'he resulting relations o+ these set operations are


sets o+ tuples3 dupliate tuples a"e eliminated f"om
the "esult

'he set operations apply only to union ompatible


"elations 3 the t6o relations ust ha$e the sae
attri,utes and the attri,utes ust appear in the
sae order
Slide 8-55
Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition
Copyright 2004 Rae! Elasri and Sha"ant #a$athe
SET OPERAT,O-S )cont*+

>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

& coplete SE*EC' Buery, called a nested %ue"$ , can ,e speci+ied


6ithin the M;ERE-clause o+ another Buery, called the oute" %ue"$

9any o+ the pre$ious Bueries can ,e speci+ied in an alternati$e +or


using nesting

>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 nested Buery selects the nu,er o+ the OResearchO departent

'he outer Buery select an E9P*)@EE tuple i+ its (#) $alue is in the
result o+ either nested Buery

'he coparison operator IN copares a $alue $ 6ith a set -or ulti-set.


o+ $alues <, and e$aluates to TRUE i+ $ is one o+ the eleents in <

In general, 6e can ha$e se$eral le$els o+ nested Bueries

& re+erence to an un%ualified att"ibute re+ers to the relation declared in


the inne"most nested %ue"$

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

I+ a condition in the M;ERE-clause o+ a nested %ue"$ re+erences an


attri,ute o+ a relation declared in the oute" %ue"$ , the t6o Bueries are
said to ,e o""elated

'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-

EHIS'S is used to chec" 6hether the result


o+ a correlated nested Buery is epty
-contains no tuples. or not

Me can +orulate >uery 42 in an


alternati$e +or that uses EHIS'S as >421
,elo6
Slide 8-44
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- )cont*+

>uery 420 Retrie$e the nae o+ each eployee 6ho


has a dependent 6ith the sae +irst nae as the
eployee.
Q1B! SELECT FNAME, LNAME
FROM EMPLOYEE
8HERE EEISTS (SELECT A
FROM DEPENDENT
8HERE SSN9ESSN AND
FNAME9DEPENDENTDNAME)
Slide 8-42
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- )cont*+

>uery A0 Retrie$e the naes o+ eployees 6ho ha$e no


dependents.
QF! SELECT FNAME, LNAME
FROM EMPLOYEE
8HERE NOT EEISTS (SELECT A
FROM DEPENDENT
8HERE SSN9ESSN)
G
In >A, the correlated nested Buery retrie$es all (EPE#(E#' tuples
related to an E9P*)@EE tuple. I+ none e&ist , the E9P*)@EE tuple
is selected
G
EHIS'S is necessary +or the e/pressi$e po6er o+ S>*
Slide 8-45
Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition
Copyright 2004 Rae! Elasri and Sha"ant #a$athe
E8PL,C,T SETS

It is also possi,le to use an #G0)","' (#&?1#%('#+) .#' -$


H()?#. in the M;ERE-clause rather than a nested Buery

>uery 450 Retrie$e the social security nu,ers o+ all


eployees 6ho 6or" on pro:ect nu,er 4, 2, or 5.
Q1/! SELECT DISTINCT ESSN
FROM 8ORKSDON
8HERE PNO IN (1, , /)
Slide 8-44
Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition
Copyright 2004 Rae! Elasri and Sha"ant #a$athe
-5LLS ,- S!L !5ER,ES

S>* allo6s Bueries that chec" i+ a $alue is #%** -issing


or unde+ined or not applica,le.

S>* uses IS or IS NOT to copare #%**s ,ecause it


considers each #%** $alue distinct +ro other #%**
$alues, so eBuality coparison is not appropriate .

>uery 440 Retrie$e the naes o+ all eployees 6ho do not


ha$e super$isors.
Q1C! SELECT FNAME, LNAME
FROM EMPLOYEE
8HERE SUPERSSN IS NULL
#ote0 I+ a :oin condition is speci+ied, tuples 6ith #%**
$alues +or the :oin attri,utes are not included in the result
Slide 8-4=
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"

Can speci+y a U:oined relationU in the 8R)9-clause

*oo"s li"e any other relation ,ut is the result o+ a :oin

&llo6s the user to speci+y di++erent types o+ :oins -regular


UthetaU D)I#, #&'%R&* D)I#, *E8' )%'ER D)I#,
RI7;' )%'ER D)I#, CR)SS D)I#, etc.
Slide 8-4A
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*+

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*+

could ,e 6ritten as0


Q1! SELECT FNAME, LNAME, ADDRESS
FROM (EMPLOYEE JOIN DEPARTMENT
ON DNUMBER9DNO)
8HERE DNAME9:R#.#(%,4;
or as0
Q1! SELECT FNAME, LNAME, ADDRESS
FROM (EMPLOYEE NATURAL JOIN DEPARTMENT
AS DEPT(DNAME, DNO, MSSN, MSDATE)
8HERE DNAME9:R#.#(%,4;
Slide 8-48
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*+

&nother 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

Include COUNT, SUM, MAE, MIN, and AVG

>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 4A0 8ind the a/iu salary, the iniu salary,


and the a$erage salary aong eployees 6ho 6or" +or the
OResearchO departent.
Q1F! SELECT MAE(SALARY), MIN(SALARY),
AVG(SALARY)
FROM EMPLOYEE, DEPARTMENT
8HERE DNO9DNUMBER AND
DNAME9:R#.#(%,4:
Slide 8-=4
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*+

>ueries 4C and 480 Retrie$e the total nu,er o+ eployees


in the copany ->4C., and the nu,er o+ eployees in the
OResearchO departent ->48..
Q1I! SELECT COUNT (A)
FROM EMPLOYEE
Q1<! SELECT COUNT (A)
FROM EMPLOYEE,
DEPARTMENT
8HERE DNO9DNUMBER AND
DNAME9:R#.#(%,4;
Slide 8-=2
Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition
Copyright 2004 Rae! Elasri and Sha"ant #a$athe
.RO5P,-.

In any cases, 6e 6ant to apply the aggregate


+unctions to subg"oups of tuples in a "elation

Each su,group o+ tuples consists o+ the set o+


tuples that ha$e the same 'alue +or the g"ouping
att"ibute(s)

'he +unction is applied to each su,group


independently

S>* has a GROUP BY-clause +or speci+ying the


grouping attri,utes, 6hich must also appea" in the
SELECT-lause
Slide 8-=5
Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition
Copyright 2004 Rae! Elasri and Sha"ant #a$athe
.RO5P,-. )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

'his type o+ selection is $ery use+ul.


Slide 8-==
Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition
Copyright 2004 Rae! Elasri and Sha"ant #a$athe
T%E %A9,-.#CLA5SE

Soeties 6e 6ant to retrie$e the $alues


o+ these +unctions +or only those g"oups that
satisf$ e"tain onditions

'he ;&<I#7-clause is used +or speci+ying


a selection condition on groups -rather than
on indi$idual tuples.
Slide 8-=A
Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition
Copyright 2004 Rae! Elasri and Sha"ant #a$athe
T%E %A9,-.#CLA5SE )cont*+

>uery 220 8or each pro:ect on whih mo"e than two


emplo$ees wo"! , retrie$e the pro:ect nu,er, pro:ect
nae, and the nu,er o+ eployees 6ho 6or" on that
pro:ect.
Q! SELECT PNUMBER, PNAME, COUNT
(A)
FROM PROJECT, 8ORKSDON
8HERE PNUMBER9PNO
GROUP BY PNUMBER, PNAME
HAVING COUNT (A) 5

S- @4(' ". '4# +"$$#%#&,# J#'@##& 8HERE (&+ HAVINGK


G
M;ERE 0 ,e+ore 3 ;&<I#7 0 a+ter
Slide 8-=C
Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition
Copyright 2004 Rae! Elasri and Sha"ant #a$athe
S5BSTR,-. CO&PAR,SO-

'he LIKE coparison operator is used to


copare partial strings

'6o reser$ed characters are used0 OVO -or OTO


in soe ipleentations. replaces an
ar,itrary nu,er o+ characters, and O2O
replaces a single ar,itrary character
Slide 8-=8
Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition
Copyright 2004 Rae! Elasri and Sha"ant #a$athe
S5BSTR,-. CO&PAR,SO-
)cont*+

>uery 2=0 Retrie$e all eployees 6hose address is in


;ouston, 'e/as. ;ere, the $alue o+ the &((RESS
attri,ute ust contain the su,string O;ouston,'HO.
QB! SELECT FNAME, LNAME
FROM EMPLOYEE
8HERE ADDRESS LIKE
:LH-?.'-&,TEL;
Slide 8-=E
Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition
Copyright 2004 Rae! Elasri and Sha"ant #a$athe
S5BSTR,-. CO&PAR,SO-
)cont*+

>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/

'he ORDER BY clause is used to sort the tuples in a


Buery result ,ased on the $alues o+ soe attri,ute-s.

>uery 280 Retrie$e a list o+ eployees and the


pro:ects each 6or"s in, ordered ,y the eployeeOs
departent, and 6ithin each departent ordered
alpha,etically ,y eployee last nae.
Q<! SELECT DNAME, LNAME, FNAME, PNAME
FROM DEPARTMENT, EMPLOYEE,
8ORKSDON, PROJECT
8HERE DNUMBER9DNO AND SSN9ESSN
AND PNO9PNUMBER
ORDER BY DNAME, LNAME
Slide 8-A2
Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition
Copyright 2004 Rae! Elasri and Sha"ant #a$athe
ORDER B/ )cont*+

'he de+ault order is in ascending order o+ $alues

Me can speci+y the "ey6ord DESC i+ 6e 6ant a


descending order3 the "ey6ord ASC can ,e used to
e/plicitly speci+y ascending order, e$en though it is
the de+ault
Slide 8-A5
Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition
Copyright 2004 Rae! Elasri and Sha"ant #a$athe
Smmary of S!L !eries

& Buery in S>* can consist o+ up to si/ clauses, ,ut only


the +irst t6o, SE*EC' and 8R)9, are andatory. 'he
clauses are speci+ied in the +ollo6ing order0
SELECT 6(''%"J?'# )".'5
FROM 6'(J)# )".'5
M8HERE 6,-&+"'"-&5N
MGROUP BY 6*%-?0"&* (''%"J?'#(.)5N
MHAVING 6*%-?0 ,-&+"'"-&5N
MORDER BY 6(''%"J?'# )".'5N
Slide 8-A4
Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition
Copyright 2004 Rae! Elasri and Sha"ant #a$athe
Smmary of S!L !eries
)cont*+

'he SE*EC'-clause lists the attri,utes or +unctions to ,e


retrie$ed

'he 8R)9-clause speci+ies all relations -or aliases. needed in


the Buery ,ut not those needed in nested Bueries

'he M;ERE-clause speci+ies the conditions +or selection and


:oin o+ tuples +ro the relations speci+ied in the 8R)9-clause

7R)%P 1@ speci+ies grouping attri,utes

;&<I#7 speci+ies a condition +or selection o+ groups

)R(ER 1@ speci+ies an order +or displaying the result o+ a


Buery

& Buery is e$aluated ,y +irst applying the M;ERE-clause, then


7R)%P 1@ and ;&<I#7, and +inally the SE*EC'-clause
Slide 8-A=
Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition
Copyright 2004 Rae! Elasri and Sha"ant #a$athe
Specifying 5pdates in S!L

'here are three S>* coands to odi+y


the data,ase3 I#SER', (E*E'E, and
%P(&'E
Slide 8-AA
Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition
Copyright 2004 Rae! Elasri and Sha"ant #a$athe
,-SERT

In its siplest +or, it is used to add one or


ore tuples to a relation

&ttri,ute $alues should ,e listed in the


sae order as the attri,utes 6ere speci+ied
in the CRE&'E '&1*E coand
Slide 8-AC
Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition
Copyright 2004 Rae! Elasri and Sha"ant #a$athe
,-SERT )cont*+

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 )

&n alternate +or o+ I#SER' speci+ies e/plicitly the attri,ute naes


that correspond to the $alues in the ne6 tuple

&ttri,utes 6ith #%** $alues can ,e le+t out

E/aple0 Insert a tuple +or a ne6 E9P*)@EE +or 6ho 6e only


"no6 the 8#&9E, *#&9E, and SS# attri,utes.
U1A! INSERT INTO EMPLOYEE (FNAME, LNAME, SSN)
VALUES (:R",4(%+:, :M(%"&":, :FB/9<FB/:)
Slide 8-A8
Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition
Copyright 2004 Rae! Elasri and Sha"ant #a$athe
,-SERT )cont*+

Iportant #ote0 )nly the constraints speci+ied in


the ((* coands are autoatically en+orced ,y
the (19S 6hen updates are applied to the
data,ase

&nother $ariation o+ I#SER' allo6s insertion o+


multiple tuples resulting +ro a Buery into a
relation
Slide 8-AE
Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition
Copyright 2004 Rae! Elasri and Sha"ant #a$athe
,-SERT )cont*+
G E/aple0 Suppose 6e 6ant to create a teporary ta,le that has the nae,
nu,er o+ eployees, and total salaries +or each departent. & ta,le
(EP'S2I#8) is created ,y %5&, and is loaded 6ith the suary
in+oration retrie$ed +ro the data,ase ,y the Buery in %51.
U/A! CREATE TABLE DEPTSDINFO
(DEPTDNAME VARCHAR(10),
NODOFDEMPS INTEGER,
TOTALDSAL INTEGER);
U/B! INSERT INTO DEPTSDINFO (DEPTDNAME,
NODOFDEMPS, TOTALDSAL)
SELECT DNAME, COUNT (A), SUM (SALARY)
FROM DEPARTMENT, EMPLOYEE
8HERE DNUMBER9DNO
GROUP BY DNAME ;
Slide 8-C0
Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition
Copyright 2004 Rae! Elasri and Sha"ant #a$athe
,-SERT )cont*+

#ote0 'he (EP'S2I#8) ta,le ay not ,e up-to-date i+ 6e


change the tuples in either the (EP&R'9E#' or the
E9P*)@EE relations afte" issuing %51. Me ha$e to
create a $ie6 -see later. to "eep such a ta,le up to date.
Slide 8-C4
Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition
Copyright 2004 Rae! Elasri and Sha"ant #a$athe
DELETE

Reo$es tuples +ro a relation

Includes a M;ERE-clause to select the tuples to ,e deleted

'uples are deleted +ro only one table at a tie -unless


C&SC&(E is speci+ied on a re+erential integrity
constraint.

& issing M;ERE-clause speci+ies that all tuples in the


relation are to ,e deleted3 the ta,le then ,ecoes an epty
ta,le

'he nu,er o+ tuples deleted depends on the nu,er o+


tuples in the relation that satis+y the M;ERE-clause

Re+erential integrity should ,e en+orced


Slide 8-C2
Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition
Copyright 2004 Rae! Elasri and Sha"ant #a$athe
DELETE )cont*+

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

%sed to odi+y attri,ute $alues o+ one or ore


selected tuples

& M;ERE-clause selects the tuples to ,e


odi+ied

&n additional SE'-clause speci+ies the attri,utes


to ,e odi+ied and their ne6 $alues

Each coand odi+ies tuples in the same


"elation

Re+erential integrity should ,e en+orced


Slide 8-C4
Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition
Copyright 2004 Rae! Elasri and Sha"ant #a$athe
5PDATE )cont*+

E/aple0 Change the location and controlling departent


nu,er o+ pro:ect nu,er 40 to O1ellaireO and =,
respecti$ely.
UB! UPDATE PROJECT
SET PLOCATION 9 :B#))("%#:, DNUM 9 B
8HERE PNUMBER910
Slide 8-C=
Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition
Copyright 2004 Rae! Elasri and Sha"ant #a$athe
5PDATE )cont*+

E/aple0 7i$e all eployees in the OResearchO departent a 40V raise


in salary.
UF! UPDATE EMPLOYEE
SET SALARY 9 SALARY A1=1
8HERE DNO IN (SELECT DNUMBER
FROM DEPARTMENT
8HERE DNAME9:R#.#(%,4:)

In this reBuest, the odi+ied S&*&R@ $alue depends on the original


S&*&R@ $alue in each tuple

'he re+erence to the S&*&R@ attri,ute on the right o+ P re+ers to the


old S&*&R@ $alue ,e+ore odi+ication

'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:

8.C-hand in 9yS>*-e/ecuta,le coands.

8.40 -hand in 9yS>*-e/ecuta,le coands.

8.44 -hand in 9yS>*-e/ecuta,le coands.

8.44 -hand in 9yS>*-e/ecuta,le coands.

Vous aimerez peut-être aussi