0% found this document useful (0 votes)
324 views246 pages

Notesh

This document contains lecture notes for a database management course. It outlines topics to be covered in each chapter, including the relational model, SQL, logical and physical database design, security, and performance issues. Homework assignments involve exercises at the end of chapters 1 and 2, including practicing SQL queries to select, insert, delete and update data. The course will cover both the traditional relational model and the emerging object-relational model, and how SQL is evolving with these changes.

Uploaded by

malliknl
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
324 views246 pages

Notesh

This document contains lecture notes for a database management course. It outlines topics to be covered in each chapter, including the relational model, SQL, logical and physical database design, security, and performance issues. Homework assignments involve exercises at the end of chapters 1 and 2, including practicing SQL queries to select, insert, delete and update data. The course will cover both the traditional relational model and the emerging object-relational model, and how SQL is evolving with these changes.

Uploaded by

malliknl
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 246

Lecture Notes for

Database Management , using


Database Principles, Programmi ng and Performance
Notes by Patrick E. O'Neil
Capters ! and "
Class !.
#and out : Syllabus. Go over Syllabus, prerequisi tes. Grader, DBA.
#ome$ork : Read Chapter 1 (quickl y, a!d Chapter " throu#h Sectio! ".$.
%o&e'ork i! t'o 'eeks : All u!dotted e(ercises at e!d o) Chapter ".
".1(b, d, "."b, ".*, ".$(b,d,), ".+(b, d, etc., ".,a, ".-b, "..b, ".1/,
".1+a,c, the )ollo'i !# hard o!es to try: ".1", ".1$. 0123 S1453D
6R1B437 80 2392 CA0 %346 :1;.
<a!t you to e!ter solutio! o!li!e, pri!t it out, sub&i t hardcopy.
%pply for a course account . ;si!# 1racle Database.
G32 B;DD: 6%103 =, 8> 78SS C4ASS ? CA2C% ;6.
0o' @uickl y throu#h ideas o) Chapter 1, 'hat 'e 'ill lear!. Aust try to #et
the idea )or !o' ? helps later 'he! 'ill cover a#ai!.
D3>. A database &a!a#e&e!t syste&, or DB7S, is a pro#ra& product )or
keepi!# co&puteriBed records (o! disk
<hat 'e 'ill be lear!i !# i! this course is u!derl yi !# co!cepts you !eed to
&ake a database 'ork: !ot ho' to build o!e but ho' to set o!e up.
Co&piler or 1perati !# Syste& is also a pro#ra&C !ot a tri vial thi!#.
2o be#i! 'i th 'e 'ill be deali!# 'ith the Relatio!al 7odel, so all our data
'ill look like tables. 3.#., see database, >i#ures ".1 D ".", p#s. "E, "-.
<e 'ill be usi!# these tables throu#hout the course. Dra' tables D t'o
ro's o! board.
0o&e!clature: <e have: tables (or relati o!s , colu&!s (de)i!e attri butes o)
the data, ro's (or tuples to represe!t i!di vi dual stude!ts or courses
(e!ti ti es or e!roll &e!ts (relati o!shi ps.
Go over colu&! !a&es a!d &ea!i !#. 0ote aid u!ique, a!a&e &i#ht dupli F
cate, &o!th values i! 1RD3RS. 2his is !ot realistic, e.#., a!a&es are too
short, 'ould 'a!t &ore colu&!s, ti &e &ore accurate.
F1F
Ca! a!s'er questio!s about the data 'i th queries . S@4 3(a&ple: 4ist a#e!t
8D, a#e!t !a&e, a!d perce!t co&&issio! o) all a#e!ts i! 0e' :ork.
select aid, aname, percent
from agents where city = 'New York';
0o' list the a#e!t 8D a!d a#e!t !a&e o) all a#e!ts 'ho placed a! order i!
Aa!uary. #o$ $ould you do tis& 0eed to co!!ect 2<1 2AB43S. 8! S@4:
select a.aid, a.aname from agents a, orders o
where a.aid = o.aid and o.month = 'jan';
Capter ". R;43S o) Relatio!al 7odel. 2ables have )ollo'ed rules so all
co&&ercial prod ucts are the sa&e, like type'ri ter keyboards.
>or years these rules 'ere as i! Sect. ".*. 3.#., !o colu&! o) a table should
have a co&ple( colu&! value (a record, o!ly a si&ple type (a! i!te#er.
Si&ilarl y, ca!G t have &ul ti pl e values )or a colu&! o) a ro'.
But there is co&i!# to be a !e' &odel, k!o'! as the 1bHectF Relatio!al
&odel, that does allo' co&ple( colu&! values (a!d collecti o! types as 'ell:
Ca! have a Set, 7ultiset, or 4ist o) values i! a si!#le ro' colu&!.
So&ethi !# to bear i! &i!d: <he! &athe&ati cs as a )ield 'as the a#e that
database is !o', it had Hust co&e up 'i th 6la!e Geo&etry, a!d 'as still
doi!# ari th&eti c i! Ro&a! 0u&erals.
2here are #oi!# to be a lot o) cha!#es i! database syste&s be)ore every F
thi!# settles do'!.
Relatio!al Al#ebra. A !o!F &achi!e query la!#ua#e that #ives a #ood idea
o) po'er o) query la!#ua#es: &ake !e' tables out o) old tables by si&ple
operati o!s. 3.#., 4ist c!o, c!a&e )or courses &eeti !# 7<".
Rel. Al#. (AG302S 'here city I G0e' :ork Jaid, a!a&e, perce!tK
Gives us si&plest possible approach u!dersta!di !# 'hat S@4 is ai&i!# at.
Relatio!al Al#ebra i! Chapter *, S@4 i! Chapter *.
Capter '. S@4 queries, already see!. 2ur!s out to have &ore po'er tha!
Rel. Al#. Also covers ho' to 8!sert !e' ro' i! table, Delete old ro's )ro&
table, ;pdate colu&! values o) e(isti !# ro's i! table.
S@4 is also i! the &idst o) cha!#e, cha!#i !# )ro& Relatio!al &odel (S@4F.",
1racle RE to 1bHectF Relatio!al &odel (S@4F*, DB" 5", 1racle R-.
F"F
2here are still data applicati o!s that do!G t )it the Relatio!al 7odel: store
"// 'ord abstracts o) "7 papers, )i!d abstracts o) all papers 'i th ab stract
'ords co!tai !i !# as &a!y as possible o) phrases such as the )ol lo'i!#:
Che&ical, Biosphere, 3!viro!&e!tal , 4ead poiso!i!#, . . .
Capter (. 1bHectF Relatio!al S@4 (8!)or&i (, 1racle. 0o' 'e CA0 per )or&
the query Hust !a&ed, by creati !# a table 'ith o!e colu&! (abstractL'ord
per&i t ti !# 7;428643 values.
<e 'ill cover chapter , be)ore chapter +.
Capter ). So&e o) 'hat DBAs do. Desi#! database. 4ots o) co&ple(
co&&a!ds to co!struct a database. Be#i! 'i th 4o#ical Desi#!.
4o#ical desi#!: break do'! all the data i!to tables so tables have #ood
properti es. 3.#., ho' ha!dle e&ployee 'i th u!k!o'! !u&ber o) depe! F
de!tsM Ca! #et 392R3734: co&plicated.
3.#., relati vel y si&ple school applicati o!: Depart &e!ts co!tai ! teachers a!d
are respo!sible )or o))eri !# subHects, &ade up o) &ul ti pl e classes.
2he subHects have prerequisi tes. 2he classes are o))ered i! #ive! periods i!
speci)ic roo&s. Stude!ts have schedules (each ter&, drop a!d add classes,
#et #rades, have library records, tui ti o! pay&e!ts, heal th i!sur a!ce, etc.
%o' break all these upM
1!e basic idea is that e!ti ti es (real 'orld obHects: stude!ts, roo&s, eve!
classFperiods each deserve thei r o'! table. 2hey have attri butes (stude!t
!a&e a!d id, roo& !u&ber a!d )loor, period le!#th a!d ho' #rouped:
*N'eek, "N'eek.
2here are relatio!shi ps bet'ee! e!ti ti es: above, enrollment is a relaF
tio!ship bet'ee! stude!t a!d course. But i! a &ore co&ple( sche&e, !eed
to relate teachers 'ith periods, roo&s, a!d subHects to )or& a class o) )eri!#,
the! the class o))eri !# a!d stude!t are related i! a stude!t sched ule a!d a
disti!ct class schedule.
Capter *. C pro#ra&s 'i th e&bedded S@4 state&e!ts: 3&bedded S@4.
8dea is to prese!t a 7e!u to !aive users so they !ever have to u!dersta!d
S@4. (S@4 o) Chapter * is called adF hoc, or i!teracti ve S@4.
Reaso!: co&ple( Hob to 'ri te S@4, ba!k tellers a!d airli!e reservati o! clerks
do!G t 'a!t to have to do co&ple( stu)) like this. DA0G3R1;S to let people
i! a hurry try to update data usi!# S@4.
F*F
Bri!#s up proble&s o) avoidi!# proble&s o) co!curre!cy a!d &aki !# data
durable: ;pdate tra!sacti o!s !eeded.
2er& O;ser >rie!dl yO is too va#ueC several di))ere!t types o) DB7S users.
F3!d ;sers:
F0aive ;sers (;se 7e!u i!ter)ace
FCasual ;sers (Co&pose S@4
FApplicatio! 6ro#ra&&ers (<rite 7e!u applicati o!s
FDatabase Ad&i!istrators (DBAs
Capter +. Co&&a!ds )or creati !# databases, creati !# a!d loadi!# ta bles,
(per)or&a!ce related issues, such as i!de(es, co&e later.
8!te#ri t y. Set up rules )or ho' data ca! cha!#e. Rules keep errors )ro&
occurri !# because o) i!e(perie!ced applicati o! 'ri ters. Rules ca! be
treated as data, so hi#h level desi#!ers k!o' 'hat is happe!i !#, !ot buried
i! &illio!s o) li!es o) applicati o!s.
Creati!# 5ie's. 5ie's are virtual tables, prete!d tables created out o) real
base tables. DBA creates these so speci)ic applicati o! pro#ra&&ers have
a! easier ti &e ()e'er )ields to lear!, easy to cha!#e DB 'i thout breaki!#
old applicati o!s.
Securi ty. <hat users ca! look atNcha!#e salary values )or e&ployees. 2a(
accou!ta!ts ca! look at, ca!G t cha!#e. 6ro#ra&&ers have very lo' data
securi ty.
Capter , a!d )ollo'i !#. Skip )or !o'. 6hysical desi#!: ho' to place
tables o! disk so &i!i &iBe access ti &eC hard proble&, because tradeF o))s.
8!de( creati o!. Desi#! )or shared data.
All per)or&a!ce related stu)) is put o)) to the seco!d hal) o) the book. But
!ote idea o) data shari!# duri!# update, so&ethi !# 'e ru! i!to i! pro F
#ra&&i !# 3&bedded S@4. 6roble& 'here o!e perso! looks at data 'hile
a!other perso! is cha!#i !# it. Su& up t'o accou!t bala!ces (ro's o) di) F
)ere!t tables, &i#ht #et su& that is )alse i) other applicati o! has Hust
subtracted )ro& o!e to tra!s)er &o!ey. 0eed so&ethi !# clever to ha!dle
this, call it a O2ra!sactio!O.
0e(t ti &e start o! Chapter " i! detail.
F$F
Class ".
Assi#!&e!t 1: G32 B;DD: 6%103 =, 8> 78SS C4ASS ? CA2C% ;6.
Read throu#h e!d o) Chapter ".
%o&e'ork i! t'o 'eeks: All u!dotted e(ercises at e!d o) Chapter ".
1P, 0o' start Chapter ". Relatio!al co!cepts D Relatio!al Al#ebra.
-ection ".!. 4ook at p#. "-. C;S2173RS, AG302S, 6R1D;C2S, 1RD3RS.
CA6 data base, collectio! o) co&puteriBed records &ai!tai !ed )or co&&o!
purpose.
F 6ossible to have t'o databases o! sa&e &achi!e used by sa&e people
(stude!t records a!d ;!iversi ty library co!te!ts
6ut o! board )irst t'o li!es o) C;S2173RS. Go over &ea!i !#s o) colu&!F
!a&es, p "E.
F 0ote that cid is u!ique )or custo&ers, c!a&e is 012.
F See ho' calculate dollars value i! orders: qty o) orders ro' ti &es price
i! products ro' )or that pid a!d take disc!t )or custo&er ro' )or cid. But 'e
do!G t 'a!t to do that every ti &e 'e ask )or the dollars value so 'e carry the
value i! the orders table.
F 0ote too that table is u!realisticall y s&all: &ore colu&!s (!a&e, straddr,
co!tact, ti &esta&p )or orders, &ore ro's, &ore tables (keep track o)
billi!#, salaries, ta(es
-ection ".". 2er&i !olo#y.
2able (relatio! (1ld: )ile o) records.
Colu&! !a&es (attri butes (1ld: )ield !a&es o) records
Ro's (tuples (1ld: records o) a )ile.
2able headi!# (Sche&a (1ld: set o) attri butes.
Set o) colu&!s is !or&al l y relati vel y co!sta!t, part o) &e!tal &odel used )or
queries, ro's are cha!#eabl e a!d !ot kept track o) by user (queried.
F 0ote ho' 'e say Co!te!ts at a #ive! &o&e!t o! p#. "-, CA6. 2his tur!s
out to be i&porta!t 'he! asked to &ake up a query to a!s'er a questio!,
query &ust still a!s'er the questio! eve! i) all the data cha!#es.
2his rule is so&eti &es called 6ro#ra&F Data 8!depe!de!ce ? &e!ti o!ed i!
Chapter 1C used to be )ile o) records, but this is better: level o) abstrac tio!
(3. #. i!de(i !# tra!spare!t
F Stude!ts o!ce i&ple&e!ted DB i! So)t'are 3!#i!eeri !# course, called
back later because 'as!G t 'orki !# 'ell i! Hob 'here they ported it. Did!G t
have 6ro#ra&F Data 8!depe!de!ceQ
%eadi!# (Sche&a o) table. %ead(C;S2173RS I Rci d, c!a&e, city, disc!t S .
2ypicall y !a&e a table 2, S, R 'i th subscripted attri butes.
F+F
%ead( 2 I A
1
A
"
A
*
A
$
0ote !otati o!: set o) attri butes is Hust a 48S2.
.E/0 1MPO/2%N2 CONCEP2. 2he !u&ber o) ro's cha!#es )reque!tl y
a!d ro's are !ot re&e&bered by usersC the colu&!s usuall y D10G2 cha!#e
i! !u&ber, &a!y !a&es are re&e&bered, a!d ;S3D 21 61S3 @;3R83S.
Column type (Column Domai n A table is D3C4AR3D i! S@4. Colu&!s
have certai ! 2:63S as i! S@4: )loat$, i!te#er, char(1*.
8dea o) Do&ai! i! Relatio!al Al#ebra, is like a! e!u&er ated type.
Do&ai! o) City: all the city !a&es i! the ;.S.
Do&ai! o) D8sc!t: all )loat =s bet'ee! /.// a!d "/.//.
2his co!cept is !ot i&ple&e!ted i! co&&ercial syste&s today. 1!ly have
types such as char(1* a!d )loat$. But assu&e it i! Relatio!al Al#ebra.
Say C8D I Do&ai!(cid, C0A73 I Do&ai !(c!a&e, C82: a!d D8SC02 are the
do&ai !s )or C;S2173RS table colu&!s, the! co!sider:
C8D ( C0A73 ( C82: ( D8SC02 (Cartesia! 6roduct
co!sisti !# o) all tuples: (', (, y, B, ' i! C8D, ( i! C0A73, . . .
A44 61SS8B43 tuples: (c..., Beo'ul), Saskatoo!, /./1
A &athe&ati cal relatio! bet'ee! these )our do&ai !s is a subset o) the
Cartesia! product. 3.#., i) have $ attri butes, A
1
, A
"
, A
*
, a!d A
$
, a!d 2 is a
relatio! such that %ead(2 I A
1
A
"
A
*
A
$
, the!:

2 Do&ai !(A
1
( Do&ai!(A
"
( Do&ai!(A
*
( Do&ai !(A
$

2 is said to relate a data ite& i! Do&ai !(A


1
'i th o!e i! Do&ai!(A
"
, . . .
-ection ".'. Relatio!al Rules. 8dea is to &ake all products have sa&e
character istics. But a bit too &athe&ati cal, overlooki !# i&porta!t i&F
ple&e!tati o! aspects, so so&e rules are broke! by &ost products.
Rule 1. >irst 0or&al >or& rule. Ca!G t have &ul tiF valued )ields. (Repeati !#
)ields. See p#. *E. All pure relati onal products obey this rule.
F But !e' obHectF relati o!al products break this rule
F 2hus ca!G t have e&ployees table 'ith colu&! Odepe!de!tsO 'hich co! tai!s
&ul ti pl e depe!de!tG s !a&es (>i#ure ".*
F Could create o!e table 'i th duplicates o! di))ere!t ro's (e.#., e&ploy eesF
depe!de!ts table Hoi! o) e&ployees a!d depe!de!ts, but this is bad )or
other reaso!s. (>i#ure ".$
F,F
F 3!ds up &ea!i !# 'e have to create t'o tables a!d join the& i! later
queries. (>i#ure ".+
? i! 1R &odel, >i#ure ".* is 1P, but 'o!G t ha!dle this )or a'hile so as !ot to
co!)use youC assu&e relatio!al FF !o &ul tiF valued )ields.
Rule ". Access ro's by co!te!t o!ly. Ca!G t say: the thi rd ro' do'! )ro&
the top. 0o order to the ro's. (Also: 0o order to the colu&!s.
F Disallo's Opoi!tersO to ro's, e.#. Ro' 8Ds (R8Ds or Ore)sO.
F 7ost relatio!al products break this rule by allo'i!# users to #et at ro's by
R8DsC !e' obHectF relati o!al products have re)s as part o) sy!ta(.
Rule *. ;!ique ro's. 2'o tuples ca!!ot be ide!tical i! all colu&! values at
o!ce. A relati o! is a! u!ordered S32 o) tuples (" ro's ca!G t be sa&e i! all
attri butes. But &a!y products allo' this )or e))icie!cy o) load.
F 2here are eve! so&e tables 'here it is a #ood thi!# (te&perature read i!#s
i! table, &i#ht repeat.
3ut in te current Capter, Capter ", $e $ill assume tat all tese
rules old perfectl y .
-ection ".(. Peys a!d Superkeys .
8dea is that by i!te!ti o! o) DBA, so&e set o) colu&!s i! a table disti! #uishes
ro's. 3.#., cid, ord!o.
8! co&&ercial product, DBA declares 'hich set o) colu&!s has this propert y
a!d it beco&es i&possible )or t'o colu&!s to have the sa&e val ues i! all
these colu&!s.
8t is ;S3>;4 to have such a key )or a table, so other tables ca! re)er to a
ro', e.#.: e&ployee !u&ber, ba!k accou!t 8D.
8! CA6 database, keys are colu&!s: cid, aid, pid, a!d ord!o.
Co!sider a !e' table, morders , su&s up qty a!d dollars o) orders table )or
each tuple o) &o!th, cid, aid, a!d pid. Pey ord!o o) orders table &ust #o
a'ay because several ro's 'i th di))ere!t ord!o values are added to#ether F
Pey )or &orders is: &o!th, cid, aid, pid.
A superkey is a set o) colu&!s that has the u!ique!ess propert y, a!d a key
is a &i!i &al superkey: !o subset o) colu&!s also has u!ique!ess propert y.
A superkey )or C;S2173RS is: cid, c!a&eC A key is: cid (alo!e
Ri#orous de)i!i ti o!s o! p#. *E, De) ".$.1. (2his is a rephrasi!#.
FEF
4et A
1
, A
"
, . . . , A
!
be the attri butes o) a table 2 (subset o) %ead(2. 4et S
be a subset o) these attri butes, A
i1
, A
i"
, . . . , A
ik
. 2he! S is a S;63RP3: )or
2 i) the set S disti!#uishes ro's o) 2 by designer intention , i.e. i) u a!d v
are t'o ro's the! )or so&e A
i&
i! S, uJA
i&
K M vJA
i&
K (!e' !otati o!.
Co!sider the set o) all colu&!s i! 2. 8s this a superkeyM (:es, by rela tio!al
rule *. Desi#!er i!te!ti o! &ust accept this rule.
A set o) attri butes P is a P3: (a CA0D8DA23 P3: i) it is a &i!i &al superkey,
i.e., !o proper subset o) P 'ill also act as a superkey.
F A si!#leto! colu&! superkey (cid is al'ays a key. Go to de)i!i ti o!.
@uestio!. 4ook at CA6, p#. "-. 1s pname a key for P/OD4C2-&
8) it is a superkey, it is a key, because !o subset e(cept !ull set. (0o, be F
cause !ot i!te!ti o! o) desi#!er. A superkey &ust re&ai ! o!e u!der all
possible le#al i!serts o) !e' ro's.
F2here &ay be &ore tha! o!e ca!didate key. Co!sider table o) a#e!ts 'i th
colu&! )or SS0 added. Desi#!er i!te!ti o! that SS0 is u!iqueM
:es, this is a )act, a!d desi#!er &ust accept this i! i!te!ti o!s. (6roduct
'ould!G t allo' duplicate SS0 )or t'o e&ployees. 7ay also have )ull !a&e
a!d address, a!d !eed to #et &ail i&plies that set o) colu&!s is u!ique.
3(a&ple ".$.1. Co!sider the table
2
A B C D
a1 b1 c1 d1
a1 b" c" d1
a" b" c1 d1
a" b1 c" d1
Assu&e i!te!t o) desi#!er is that this table 'ill re&ai! 'ith the sa&e
co!te!ts. 2he! ca! deter&i !e keys )ro& co!te!t alo!e (this is a 53R: ;0F
;S;A4 situa tio!.
(1 0o si!#le colu&! ca! be a key. (" 0o set o) colu&!s ca! be a key i) it
co!tai !s D. (* 6airs: AB, AC, BC. 3ach o) these pairs disti!#uishes all
ro's. 2here)ore all are keys. ($ All other sets (&ust have * or &ore el F
e&e!ts co!tai ! o!e o) these or else D as a proper subset. (Sho' subset
lattice. 2here)ore !o &ore keys.
F-F
5arious keys speci)ied by i!te!t o) DBA are called Ca!didate Peys. A 6ri F
&ary Pey is de)i!ed to be the ca!didate key chose! by the desi#!er to
ide!ti )i es ro's o) 2 used i! re)ere!ces by other tables.
As 1RD3RS re) ere!ces C;S2173RS by cid. 6robabl y keep that 'ay o) re) F
ere!ci!# eve! i) c!a&e city 'as a!other ca!didate key.
F.F
Class '.
Assi#!&e!t Read to e!d o) Chapter ". %o&e'ork: All u!dotted proble&s
throu#h e!d o) Chapter ", due at Class ,.
2heore& ".$.". 3very table 2 has at least o!e key.
8dea o) proo) based o! )act that a key is a &i!i &al set o) colu&!s i! 2 that
disti!#uishes all ro's: i) u a!d v are di))ere!t ro's a!d P is a key, the!
desi#!er i!te!ti o!: uJPK (u restricted to P is !ot ide!tical to vJPK.
Si!ce the set o) all colu&!s has that propert y (is a superkey )or 2 ? !ote
al'ays relati ve to table 2, see&s that there 7;S2 be a 78087A4 set o)
colu&!s that does. (0123 6AR28C;4AR4:: deali!# 'i th S32S o) colu&!s.
But &a!y 'i th !o &athe&ati cal trai !i !# ca!G t sho' this. 4ike you 'a!ted to
'ri te a pro#ra& to )i!d such a set, a!d ar#ue 'hy the pro#ra& ter&i !ates.
6roo) . 0A73 8237S. Give! a table 2 'i th %ead(2 I A
1
. . . A
!
.
S32 ;6 21 4116: 4et attri bute set S
1
be this e!ti re set. (8! 'hat )ollo's, 'e
assu&e 'e k!o' the i!te!ti o!s o) the table desi#!er to decide 'hich sets o)
colu&!s 'ill al'ays disti!#uish all ro's, but ca!G t a!s'er the questio!s u!til
the set o) colu&!s is !a&ed.
4116: 0o' S
1
is a superkey )or 2C either S
1
is a Pey )or 2 or it has a proper
subset S
"
, S
"
S
1
, such that S
"
is also a superkey )or 2. 0o', either S
"
is a
Pey or it has a proper subset S
*
, S
*
S
"
, such that S
*
is also a superkey )or
2. Ca! this #o o! )oreverM (1P i) you say ca!G t #o o! )orever.
7athe&ati ci a!: 'hy !otM
But each successive ele&e!t i! the seque!ce S
1
, S
"
, S
*
, . . . has a s&aller
!u&ber o) colu&!s (it is a 6R163R subset, a!d 'e ca! !ever #o to / (/
colu&!s do!G t have u!ique values or belo'. 2hus this &ust be a )i!i te seF
que!ce 'ith a s&allest set at the e!d S
!
. 2hat &ust be the key. @3D. :ou
should see 'hy it 'as!G t e!ou#h to stop 'ithout #ivi!# the loop a!d e( F
plai!i!# 'hy it ter&i !ates: proo) e(plai !s as &uch as possible.
De). 6ri&ary Pey. A 6ri&ary Pey o) a table 2 is the ca!didate key chose! by
the DBA to u!iquel y ide!ti )y ro's i! 2 (usuall y used i! re)ere!ces by other
tables i! O>orei#! PeyO. 2hus o! p#. "-, aid is the 6ri&ary key )or a#e!ts
a!d used as a )orei#! key i! orders.
0ote that this De) i&plies that there 78G%2 be a situati o! 'here a table
does!G t have to have a pri &ary key, as 'he! there is o!ly o!e table i! the
database. 7ost books do!G t allo' this idea, but co&&ercial databases do.
F1/F
Null 5alues 8!sert a !e' a#e!t (a1", Beo'ul ), u!k!o'!, u!k!o'!
A#e!t has!G t bee! assi#!ed a perce!t co&&issio! or city yet (still i!
trai !i !#, but 'a!t to have a record o) hi&.
A !ull value is placed i! a )ield o) a table 'he! a speci)ic value is either
u!k!o'! or i!appropri ate . %ere it is u!k!o'!. A sli#htl y di))ere!t &ea! i!#
'ould be i) 'arehouse &a!a#er also had a perce!t (co&&issio! )ield, but
si!ce 'arehouse &a!a#ers do!G t #et co&&issio!s, 'ould #et !ull value.
A !ull value ca! be used )or either a !u&eric or character type. B;2 82 %AS
A D8>>3R302 5A4;3 >R17 A0: R3A4 >834D. 8! particular, it is !ot Bero (/
or the !ull stri!# (GG . 8t is ha!dled speciall y by co&&erci al databases.
8) 'e 'ere to ask )or the a#e!t 'i th the &i!i &al perce!t or the short est city
!a&e, 'e 'ould!G t 'a!t to #et a! a#e!t 'i th !o perce!t or city yet
assi#!ed.
Si&ilarl y, i) 'e 'ere to ask )or the avera#e perce!t, 'ould!G t 'a!t to
avera#e i! Bero )or a !ull.
8! )act, i) 'e 'ere to ask )or all a#e!ts 'i th perce!t T ,, a!d the! all a#e!ts
'i th perce!t UI ,, 'e 'ould!G t #et the !e' a#e!t Beo'ul ) i! either a!s'er.
Aust !ot a &ea!i !#)ul questio!.
".* /elational %lgebra . 1peratio!s o! tables to #et other tables. Codd.
8!teresti !# thi!# is ca! #et a!s'er to &a!y use)ul @s ()irst order predi cate
lo#ic. But abstract la!#ua#e: 'e ca!G t use a &achi !e to #et a!s'er.
2'o types o) operati o!s: Set 2heoretic (depe!d o! )act that table is a set o)
ro's, a!d 0ati ve operati o!s (depe!d o! structure o) table. Give! t'o
tables R a!d S, %ead(R I A
1
. . . A
!
, (S o)te! the sa&e, de)i!e - opera F
tio!s. See p#. $1F $". 6ut o! board.
0ote: sy&bols are !ot portabl e bet'ee! versio!s i! 7icroso)t <ord, so
these !otes use the keyboard )or&s.
-E2 2#EO/E21C OPE/%21ON-
6E03O%/D
N%ME -0M3OL 7O/M E8%MPLE
F11F
;0810 ;0810 R ;0810 S
8023RS3C2810 8023RS3C2 R 8023RS3C2 S
D8>>3R30C3 F or 780;S R F S, or R 780;S S
6R1D;C2 ( or2873S R ( S, or R 2873S S
-PEC1%L OPE/%21ON-
6E03O%/D
N%ME -0M3OL 7O/M E8%MPLE
6R1A3C2 R [ ] R J K R JA
i
1
. . .A
i
k
K
S343C2 R 'here C R 'here C R 'here A
1
I +
A180
A180 R S, or R A180 S
D858S810 D858D3B: R D858D3B: S
F1"F
8dea o) Peyboard )or&: !a&e to use )or a! operati o! o! a type'ri ter that
does!G t have special operati o! sy&bol.
Set 2heoretic 1peratio!s
Def. ".). ! . 2'o tables are said to be co&pati bl e i)) they have the sa&e
sche&a. (i)) &ea!s Oi) a!d o!ly i)O, or O&ea!s e(actl y the sa&e thi!#O.
R S
A B C A B C
a
1
b
1
c
1
a
1
b
1
c
1
a
1
b
"
c
*
a
1
b
1
c
"
a
"
b
1
c
"
a
1
b
"
c
*
a
*
b
"
c
*
8llustrate R ;0810 S. ;!io! o) t'o tables co!sideri !# table as set o) ro's. +
ro's i! resul t. Clearl y &ust be co&pati bl e tables, because ro's o) di) )ere!t
sche&as 'ould!G t )it i! sa&e table.
8llustrate R 8023RS3C2 S. t'o ro's.
8llustrate R F S. o!l y o!e ro'.
illustrate S F R. t'o ro's.
Be)ore 'e #o o! to product (Cartesia! product, co!sider idea o) assi#! &e!t
or alias.
Def ".). ' 4et R be a table 'i th %ead(R I A
1
. . . , A
!
, a!d assu&e 'e 'ish
to create a table S 'i th %ead(S I B
1
, . . ., B
!
, B
i
attri butes such that
Do&(B
i
I Do&(A
i
)or all i, 1 I i I !, 'hich has the SA73 C102302 as A.
<e ca! de)i!e the table S by 'ri ti !# the assi#!&e!t
S(B
1
,. . .,B
!
:I R(A
1
,. . .,A
!
.
2he co!te!t o) the !e' table S is e(actl y the sa&e as the co!te!t o) the old
table R, that is, a ro' u is i! S i) a!d o!l y i) a ro' t e(ists i! R such that uJB
i
K
I tJA
i
K )or i, 1 I i I !. 2he sy&bol :I used i! this assi#!&e!t is called the
assi#!&e!t operator .
8) do!G t !eed to re!a&e colu&!s, Hust 'a!t a di))ere!t table !a&es 'ith
sa&e attri butes, 'e call this a table alias, a!d 'ri te:
S :I R.
F1*F
;se alias operator to save i!ter&edi ate resul ts o) operati o!s. Ca! 'ri te:
2 :I (R 8023RS3C2 S F (R S,
or could 'ri te i!stead:
21 :I (R 8023RS3C2 S
2" :I (R S
2 :I 21 F 2" (Dra' picture o) this resul t
0123 2%A2 'e o)te! do !ot absolutel y 033D to #et i!ter&edi ate resul ts.
Ca! usuall y use sube(pressio! i! place o) !a&ed alias. 1!e e(a&pl e belo'
'here absolutel y !eed it.
1P, !o' 6R1D;C2 or Cartesia! product. 3(a&ple o) R S ()ro& above,
di))ere!t tha! book: (calculate it. 0123 2ables 033D 012 B3 C176A28B43
)or a product to be calculated. (<rite )ollo'i !# De) )or ter&i !ol o#y.
Defini tion ".). ( Product. 2he product (or Cartesian product o) the
tables R a!d S is the table 2 'hose headi!# is %ead(2 I R.A
1
. . .R.A
!

S.B
1
. . .S.B
!
. >or every pair o) ro's u, v i! R a!d S, respecti vel y, there is a
ro' t i! 2 such that t(R.A
i
I u(A
i
)or 1 I i I ! a!d t(S.B
H
I v(B
H
)or 1 I H
I &. 0o ro's e(ist i! 2 that do !ot arise i! this 'ay. 2he product 2 o) R
a!d S is de!oted by R 2873S S.
Colu&!s o) product table are quali)ied !a&es. ;sed to disti !#uish ide!ti F
cally !a&ed attri butes. See 3(a&pl e ".,.$ )or e(a&pl e. Di))ere!ce be F
t'ee! Cartesia! product o) sets a!d Relatio!al product.
Special proble& i) try to take product o) table 'i th itsel). R 2873S R 'ould
have ide!ticall y !a&ed colu&!s. 2here)ore say S :I R a!d take R 2873S S.
"..E. 0ative Relatio!al 1perati o!s
6roHectio!. 4i&i t colu&!s a!d cast out duplicate ro's. (3(a&pl e, !ot De).
6R1A3C2810. Give! table R 'here %ead(R I A
1
, . . . , A
!
, proHect R o! subF
set o) colu&!s 2 :I RJA
i
1
, . . ., A
i
k
K, 'here list i! brackets is a subset o) the
co&plete set o) attri butes. Cross out all colu&!s a!d col u&! values i! R !ot
i! set, the! eli&i !ate duplicate ro's. (See De) ".E.1 i! te(t.
3(a&ple ".E.1. 4ist all custo&er !a&es )ro& the C;S2173RS table o) >i#ure
".". A!s'er to @uery: C;S2173RSJc!a&eK. Result is table 'ith c!a&e
F1$F
headi!#, 28p2op, Basics, Allied, AC73. (DRA< 82 (0ote cast out duplicate
ro's.
4ist city a!d perce!t co&&issio!s )or all a#e!ts. A!y duplicates #o a'ayM
(0e' :ork, ,C but !ote duplicates i! o!e o) t'o colu&!s is 1P.
S343C2810. R 'here Co!di ti o!. 2he Co!di ti o! is a lo#ical co!di tio! that
ca! be deter&i !ed )ro& the values o) a si!#le ro' o) C. 5ery si&ple ki!d o)
co!di ti o!s. De)i!i ti o! ".E.".
A
i
A
H
or A
i
a, 'here A
i
a!d A
H
are attri butes o) R, a is a co!sta!t.
is a!y o): U, T, I, UI, TI, UT
8) C, CG, also #et C a!d CG, C or CG, a!d )i!all y: !ot C.
3(a&ple: Give the cid a!d c!a&e o) all custo&ers livi!# i! Dallas 'i th
discou!t #reater tha! -.
(C;S2173RS 'here city I GDallasG a!d disc!t T - Jcid, c!a&eK
F1+F
Class (.
Assi#!&e!t
%o&e'ork (%ardcopy, Due i! o!e 'eek.: all !o!F dotted proble&s i! Ch ".
Display o! board R 2873SS (Cartesia! 6roduct (re&e&ber quali)ied !a&es
)or R D S belo'.
R S
A B B C
a
1
b
1
b1 c
1
a
1
b
"
b" c"
a
"
b
1
b* c
*
But !o' sho' 'hat it is )or R A180 S.
9O1N. Start by taki !# R 2873S S.
Cross out ro's 'here equi vale!tl y !a&ed attri butes 'i th quali)iers R or S i!
R 2873S S do not have equal values (do it above, re'ri te s&aller table.
Do a'ay 'i th o!e colu&! 'ith duplicate !a&eC drop quali)icati o! !a&es
(do!G t !eed, si!ce !o' all colu&!s have di))ere!t !a&es
Dra' li!es bet'ee! &atchi !# ro's i! R D S. 0ote b* i! table S has !o
&atchi !# ro' i! table R, does !ot appear i! Hoi!. <rite out R A180 S.
3(a&ple: 4ook at: 1RD3RS A180 C;S2173RS (p#. "-,. >or each ro' i!
orders, e(actl y o!e i! custo&ers 'ith sa&e cid value (o!l y co&&o! !a&e.
Dra' o!e ro'. e(te!ds orders ro' 'ith &ore i!)or&ati o! about custo&er.
7ost Relops de)i!ed !o'. 2hi!#s ca! #et ki!d o) co&ple( i! )or&i !# rela F
tio!al al#ebra e(pressio!s to a!s'er queriesC take thi!#s o!e step at a ti &e.
3(a&ple: Give the aid values o) a#e!ts 'ho take orders i! dollar volu&e
#reater tha! +// )or custo&ers livi!# i! Pyoto.
8! )ollo'i !#, assu&e have: C :I C;S2173RS, A :I AG302S, 6 :I
6R1D;C2S,
1 :I 1RD3RS.
0ote 'ill !eed i!)o )ro& 1RD3RS a!d )ro& C;S2173RS, si!ce do!G t k!o'
'here custo&ers live i! 1RD3RS, do!G t k!o' a!ythi !# about orders i! C;SF
2173RS. Aoi! 1RD3RS a!d C;S2173RS, a!d have !ecessary i!)o.
1RD3RS A180 C;S2173RS or 1 A180 C
F1,F
But !o' !eed to select, the! proHect.
((1 A180 C 'here city I GPyotoG a!d dollars T +//.// JaidK
<ould the )ollo'i !# be 1PM
1 A180 C 'here city I GPyotoG a!d dollars T +//.// JaidK
See precede!ce o) operati o!s table, p#. +*. Stro!#est bi!di!# is proHec tio!.
JaidK 'ould try to bi!d to C (!o such colu&!. <hat about
(1 A180 C 'here city I GPyotoG a!d dollars T +//.// JaidK
6roHectio! )irst, the! 'here clause does!G t 'ork. 8!stead:
((1 A180 C 'here city I GPyotoG a!d dollars T +//.// JaidK
(Slo'. 0ote could have do!e this di))ere!tl y
((C 'here city I GPyotoG A180 (1 'here dollars T +//.//Jai dK
3(a&ple. Give all (c!a&e, a!a&e pairs 'here the custo&er places a! or F
der throu#h the a#e!t. 0eed to i!vol ve three tables. Does this 'orkM
(C A180 1 A180 A Jc!a&e, a!a&eK
4ook at p#. "-. 0o, but 'hy !otM (Slo': look at de)i!i ti o! o) Aoi!. 1P that
have!G t de)i!ed double Hoi!: tur!s out that (R A180 S A180 2 I R A180 (S A180
2, so ca! de)i!e R A180 S A180 2 as either o!e o) these.
6roble& is: build up attri bute !a&es #oi!# le)t to ri#ht. Results i! all
(c!a&e, a!a&e pairs 'here the custo&er places a! order throu#h the
a#e!t a!d the custo&er a!d a#e!t are i! the sa&e city . 2o leave that out,
!eed:
((CJcid, c!a&eK A180 1 A180 A Jc!a&e, a!a&eK
Cuts out city i! )irst Hoi!. <hy !eed t'o attri butesM
3(a&ple: #ive (cid, cidG pairs o) custo&ers 'ho live i! the sa&e city.
4ook at p#. "-. <hat do 'e 'a!tM (c//1, c//$ a!d (c//", c//*. <ill the
)ollo'i !# do thisM (Assu&e P :I C;S2173RS
(C A180 P JC.cid, P.cidK
F1EF
0o. 0o quali)ied !a&es i! Hoi! resul t, !o duplicated attri bute !a&es. 8! )act
C A180 P (sa&e attri bute !a&es looks like 'hatM (4ook to de) o) Aoi!. 1P,
do!G t al'ays use Aoi!, try:
((C A180 P 'here C.city I P.cityJC.cid, P.cidK
4ook at p#. "-. Dra' &ul ti pl icati o! table, + ro's (C cid values, + colu&!s
(P cid values, put 9 i! 'he! equal city. 6roble&, 'ill #et (c//", c//". <ill
also #et both (c//1, c//$ a!d (c//$, c//1. 2o solve this, 'ri te:
((C ( P 'here C.city I P.city a!d C.cid U P.cidJC.cid, P.cidK
4ike choosi!# lo'er tria!#l e belo' the dia#o!al.
0o', Relatio!al Divisio!. De)i!e it i! ter&s o) Cartesia! product. 2he idea
is, as 'i th i!te#ers, #ive! t'o i!te#ers 9 a!d :, de)i!e V I 9N:, so that VW:
I 9. Ca!G t al'ays quite do this, eve! 'i th i!te#ers, so i!stead de)i!e V to
be the lar#est 'hole !u&ber s&aller tha! 9N:: e.#., i) 9 I 1E a!d : I +,
the! *.$ I 1EN+ a!d V I *. Sa&e 'i th table divisio!.
Defini tion ".+. *. Di5ision. Co!sider t'o tables R a!d S, 'here the
sche&a o) S is a subset o) the sche&a o) R. 2ables R a!d S:
%ead(R I A
1
. . .A
!
B
1
. . .B
&
, a!d %ead(S I B
1
. . .B
&
.
2he table 2 is the resul t o) the division R X S ('hich is read as OR D858D3B:
SO i) %ead(2 I A
1
. . .A
!
a!d 2 co!sists o) those ro's t such that )or every
ro' s i! S, the ro' resul ti !# )ro& co!cate!ati !# t a!d s ca! be )ou!d i!
table R, a!d there is !o lar#er set o) ro's )or 'hich this is true.
0ote there are !o colu&!s i! co&&o! bet'ee! S (%ead(S I B
1
. . .B
&
a!d
2 I R X S (%ead(2 I A
1
. . .A
!
, but si!ce %ead(R I A
1
. . .A
!
B
1
. . .B
&
, 'e
see that S a!d 2 have the proper sche&as so that 'e &i#ht be able to say:
2 ( S I R.
(2his is the divisio! operati o! 'e 'a!t to de)i!e, the i!verse o) prod uct. 8)
R I 2 A180 S, the! 'e 'ill be able to say that 2 I R X S.
7i#ht start 'ith R a!d S so R M 2 A180 S, )or a!y possible choice o) 2.
Still, 'he! 2 I R X S, the table 2 co!tai!s the lar#est possible set o) ro's
such that 2 S is co!tai !ed i! R. (3(ercise *. Make %nalogy to 1nteger
Di5ision .
E:ample ".+. ;. (8! book Start 'ith the table R #ive! by:
R
F1-F
A B C
a1 b1 c1
a" b1 c1
a1 b" c1
a1 b" c"
a" b1 c"
a1 b" c*
a1 b" c$
a1 b1 c+
<e list a !u&ber o) possible tables S a!d the resul ti !# table 2 :I R X S.
S 2
C A B
c1 a1 b1
a" b1
a1 b"
0ote that all o) the ro's i! S A180 2 are i! R, a!d there could!G t be a!y
lar#er a set o) ro's i! 2 )or 'hich that is true because there are o!ly three
ro's i! R 'i th a c1 value i! colu&! C.
S 2
C A B
c1 a1 b"
c" a" b1
All o) the ro's i! S A180 2 are i! R, a!d there could!G t be a!y lar#er a set o)
ro's i! 2 'ith this true: look at ro's i! R 'ith C values c".
S 2
C A B
c1 a1 b"
c"
c*
c$
S ( 2 i! R, a!d by looki!# at the ro's i! table R 'ith C value c* a!d c$, 'e
see 'hy 2 has the &a(i &al co!te!t 'i th this propert y.
S 2
B C A
b1 c1 a1
F1.F
a"
3(a&ple o) dividi!# R 'i th three colu&!s by a table S 'i th t'o colu&!s,
resul ti !# i! a table 2 'i th a si!#le colu&!. 2 is &a(i &al .
S 2
B C A
b1 c1 a1
b" c1
<hy is 2 &a(i &alM
0e(t 'e #ive! a! e(a&pl e to see 'hy 'e use divisio!: 'hat ki!d o) ques F
tio! does it a!s'er about the data.
Recall C:I C;S2173RS, A :I AG302S, 6 :I 6R1D;C2S, 1 :I 1RD3RS
3(a&ple ". Get !a&es o) custo&ers 'ho order all products. <he! see OallO
thi!k Odi visio!O. 4etGs #et cids o) such custo&ers.
1Jcid, pidK X 6JpidK
Clear 'hy &ust proHect 6 o! pid: colu&!s o) divisor &ust be subset o)
colu&!s o) divide!d.
<hy proHect 1 o! cid, pidM 1ther'ise 'ill try to )i!d other colu&! values i!
1RD3RS (e.#. qty the sa&e )or all pid, but Hust 'a!t cid the sa&e )or all pid.
2o #et !a&es o) custo&ers, Hoi! 'ith C.
((1Jcid, pidK X 6JpidK A180 CJcidK
3(a&ple *. Get cids o) custo&ers 'ho order all products priced at Y/.+/.
Build !e' table to take place o) 6 above.
((1Jcid, pidK X (6 'here price I /.+/Jpi dK A180 CJcidK
3(a&ple $. Get cids o) custo&ers 'ho order all products that a!ybody or F
ders. (7aybe )e' !e' products !oo!e orders yet.
1Jcid, pidK X 1JpidK
1P. So&e o) the - operati o!s o) relatio!al al#ebra: ;0810, 8023RS3C2810,
F, 2873S, 6R1A3C2, S343C2, A180, D858S810 are u!!ecessary. Aust carry the&
)or ease o) use.
F"/F
Class *.
%a!d i! h' 1 o! class ,.
Assi#!&e!t : readi!# throu#h Sectio! *.+. Assi#!&e!t to do e(ercise *.".1
(start up database, (try )or !e(t ti &e, the! ;0D1223D e(ercises at e!d o)
Chapter * throu#h *.E (so&e i! *.E are 53R: %ARD, 1P i) ca!G t do ? 6h.D.
qual level questio!.
1P. So&e o) the - operati o!s o) relatio!al al#ebra: ;0810, 8023RS3C2810,
F, 2873S, 6R1A3C2, S343C2, A180, D858S810 are u!!ecessary. Aust carry the&
)or ease o) use.
2h. ".-.1, do!G t !eed both 8023RS3C2 a!d 780;S.
A B I A F (A F B. Sho' this 'ith a picture.
2h. ".-.". Ca! de)i!e A180 o) R a!d S i! ter&s o) 2873S, 6R1A3C2 a!d ASF
S8G07302.
A180. Start by taki!# R 9 S.
Cross out ro's 'here equi vale!tl y !a&ed attri butes i! R 9 S (such as cid i!
custo&ers a!d cid i! orders do!G t have sa&e value.
Do a'ay 'i th colu&!s 'i th duplicate valuesC drop quali)icati o! !a&es
(do!G t !eed, si!ce !o' all colu&!s have di))ere!t !a&es
Say %ead(R I AB a!d %ead(S I BC.
2ake 21 :I R 2873S S (attri butes are R.A, R.B, S.B, S.C.
2he! take 2" :I (21 'here R.B I S.B JR.A, R.B, S.CK. (equate a!d proHect
>i!all y let 2*(A, B, C :I 2"(R.A, R.B, S.C (re!a&e the attri butes, #et rid o)
S.B.
2* is equival e!t to R S.
D858S810. (#ard< 8) %ead(R I A
1
. . . A
!
B
1
. . . B
&
a!d %ead(S I B
1
. . .
B
&
:
R X S I RJA
1
. . . A
!
K F ((RJA
1
. . . A
!
K 9 S F R JA
1
. . . A
!
K
3(a&ple: See 3(a&pl e ".E.. o! p#. +-, the seco!d S there. <ork out.
RJA
1
. . . A
!
K is RJA BK, 'i th three ro's, (a1, b1, (a", b1, a!d (a1, b".
Calculate 9 S (, ro's, a!d subtract R. 1!ly o!e 'e keep is (a1, b1, c" a!d
proHect o! JA BK #et (a1, b1, subtract )ro& RJA BK, resul t is t'o ro's.
F"1F
>irst ter& o! le)t (RJA
1
. . . A
!
K is certai !l y &a(i &u& possible a!s'er. 8t
'ill be the a!s'er (2 i) RJA
1
. . . A
!
K 9 S I R: 2he! 2 9 S I R, de)i!i ti o!.
But i) itGs too lar#e, the! RJA
1
. . . A
!
K 9 S co!tai !s &ore tha! R.
Co!sider reduci!# RJA
1
. . . A
!
K by subtracti !# out the part that #ives ro's
!ot i! R, i.e., subtract out: (RJA
1
. . . A
!
K 9 S F R JA
1
. . . A
!
K
Clai& !o' 'e have:
(RJA
1
. . . A
!
K F ((RJA
1
. . . A
!
K 9 S F R JA
1
. . . A
!
K 9 S R, a!d this is &a(.
0o', 'e see& to have a! a')ul lot o) capabili ty 'i th these operati o!s, to
create =ueries that a!s'er requests )or data. Describe a!y colu&!s you
'a!t as a resul t (do 'ith proHect, a!y 'ay i! 'hich di))ere!t tables are
i!terco!!ected (product the! select is &ore #e!eral tha! Hoi!.
CA0 D1 2%8S 80 G303RA4, as lo!# as co!!ecti o!s ca! be described 'i th the
co!di ti o!s 'e have speci)ied. (U, UI, I, UT, TI, T
Could thi!k o) co!di ti o!s !ot so described (city co!tai !s a character pat ter!
Ge'G C 0e' :ork or 0e'ark, but could add !e' co!di ti o!s: tri vi al.
3ve! >1R A44 type queries ca! be do!e 'i thout divisio! reall y, !ot easy
i!tui ti o!.
3%> O7 2/1C6- 7OLLO?- @E8%M AsBC P%0 C%/E74L %22EN21ONDDD
3(a&ple 1. Get aids o) a#e!ts 'ho do !ot suppl y product p/".
AJaidK F (1 'here pid I Gp/"G Jai dK
3(a&ple ". Get aids o) a#e!ts 'ho suppl y o!l y product p/".
1JaidK F (1 'here pid UT Gp/"G Jai dK
2he questio! see&s to i&pl y the these a#e!ts D1 suppl y product p/", so !o
#ood to say:
AJaidK F (1 'here pid UT Gp/"G Jai dK
(3!#lish is a bit a&bi#uous, a!d 'e &ay 'a!t to clari)y. <ill this rela tio!al
al#ebra e(pressio! have a!y a#e!ts 'ho do !ot place a! order )or product
p/" eitherM Do 'e !eed to 'ri te:
F""F
(1 'here pid I Gp/"G Jai dK F (1 'here pid UT Gp/"G Jai dK
(0o, )or has to be a! order a!d it ca!G t be )or a!y product other tha! p/".
3(a&ple *. Get aids o) a#e!ts 'ho take orders o! at least that set o)
products ordered by c//$. (%o'M (Rephrase: take orders o! A44 products
ordered by c//$.
1Jaid, pidK X (1 'here cid I Gc//$G Jpi dK
3(a&ple $. Get cids o) custo&ers 'ho order p/1 a!d p/E. (%o'M ( <ro!# to
'ri te: (1 'here pid I Gp/1G a!d pid I Gp/EG Jci dK
(1 'here pid I Gp/1G Jci dK 8023RS3C2 (1 'here pid I Gp/EG Jci dK
3(a&ple +. Get cids o) custo&ers 'ho order p/1 or p/E. (Ca! use or.
(1 'here pid I Gp/1G or pid I Gp/EG Jci dK
3(a&ple ,. 4ist all cities i!habi ted by custo&ers 'ho order product p/" or
a#e!ts 'ho place a! order )or p/". (Ca!G t use or i! selectio! co!di tio!
<hyM
((1 'here pid I Gp/"G A180 CJcityK ((1 'here pid I Gp/"G A180 AJci tyK
3(a&ple E. Get aids o) a#e!ts 'ho place a! order )or at least o!e cus to&er
that uses product p/1. NO2E: &i#ht be that a! a#e!t retri eved does 012
place a! order )or p/1. Co!sider dia#ra& that )ollo's.
NFF A#e!ts
N FF Custo&ers FFF 'ho
p/1 FFF 'ho order FFF place orders
Z FF p/1 FFF )or those
ZFF custo&ers
<e are retrievi !# cloud o! ri#ht. 2his is o!e o) the &ost &issed proble&s
o! e(a&s. Do it i!sideF out. Custo&ers 'ho order product p/1.
(1 'here pid I Gp/1G Jci dK
A#e!ts 'ho place orders )or those custo&ers.
((1 'here pid I Gp/1G Jci dK 1 JaidK (0ote 1P: Aoi! !ot @uali)ied.
F"*F
3(a&ple -. Retrieve product ids )or all products that are !ot ordered by a!y
custo&ers livi!# i! a city be#i!!i !# 'ith the letter ODO. 1P, start 'i th the
se!se o) Oproducts !ot orderedO.
6JpidK F (1 (C 'here . . .Jpi dK
2his 'orks i) 'e co&e up 'i th a co!di ti o! )or . . . that &ea!s livi!# i! city
be#i!!i !# 'i th the letter ODO. Ri#htM A!y ideasM 1!ly have co!di ti o!s: U,
UI, I, UT, TI, T. <ith alpha stri!#s, &ea!s earlier i! alphabetical order.
C 'here C.city TI GDG a!d C.city U G3G
8! the solved 3(ercise, i! ".+ (i, see ho' it is di))icul t to )i!d custo&ers 'ho
have the lar#est discou!t. (Good 3(a& questio!.
3(a&ple .. Retrieve cids o) custo&ers 'i th the lar#est discou!ts.
C: :I C
21(cyid, cid :I ((C: C 'here C:.disc!t TI C.disc!tJC:.cid,C.cidK
0o' have pairs (cyid, cid 'here the o!es 'ith lar#er or equal discou!t are
o! the le)t. 2he cids 'ith &a(i &u& discou!t 'ill be paired 'i th all other cid
values o! the ri#ht.
2" :I 21 X C:JcidK
F"$F
Class ). %a!d i! h' 1.
Assi#!&e!t : readi!# throu#h Sectio! *.+. ;0D1223D e(ercises at e!d o)
Chapter * throu#h *.E. Due Class .. 2ried 3( *.".1 (start up databaseM
%o' did it #oM
7%-2C 1dea of outer Eoin . Give! t'o tables R a!d S, %ead(R I AB,
%ead(S I BC,
R S
A B B C
a
1
b
1
b1 c
1
a
"
b
"
b" c"
a
*
b
+
b* c
*
b$ c$
<hat is the Hoi!, R A180 SM
R A180 S
A B C
a
1
b1 c
1
a
"
b" c"
But 'eG re losi!# a lot o) i!)or&ati o!. 1uter Hoi! P336S all the i!)or&ati o! o)
the t'o tables, )illi!# i! !ulls 'here !ecessary.
R 1;23R A180 S
A B C
a
1
b1 c
1
a
"
b" c"
a* b+ !ull
!ull b* c*
!ull b$ c$
:ou use this sort o) thi!# 'he! you do!G t 'a!t to lose i!)or&ati o!. 3(a& ple
#ive! i! the te(t is that eve! thou#h a! a#e!t i! AG302S table has &ade !o
sales i! SA43S table, 'e D1 'a!t to see AG302 !a&e i! report.
7ost co&&ercial product versio!s o) S@4 (8!cludi !# 1racle already have
outer Hoi!
2heta Hoi!. Recall i! equiHoi!, o) R a!d S, %ead(R I AB, %ead(S I BC:
8.e., (R ( S 'here R.B I S.B
<hat i) 'a!ted so&e other co!di ti o! (allo' co&&o! colu&!s,
F"+F
(R ( S 'here R.B S.B, 'here is U, UI, UT, T, TI, a!ythi !# but I.
0o bi# deal. 8! S@4, use product, !o special !a&e )or this.
Capter ' 8dea o) S@4. Co&puteriBed query la!#ua#e.
8B7 developed S@4, a!d it beca&e sta!dard. 7ost queries (i!cludi !#
updates, i!serts, deletes are very sta!dard across relatio!al products.
<e 'ill o!l y use 1RAC43 i! class to be#i! 'i th, but 'ill also talk about
others. 2ry to keep to basic sta!dard )or !o' (called Basic S@4 i! the te(t.
<ill sho' so&e &ore adva!ced )eatures o) S@4F.. a bit later.
8! Appe!di ( A.", to e!ter 1racle, tells you to lo#i! to ;089, #ive co&&a!d:
[sqlplus
A!y proble&s, co!tact DBA )or course.
:ou 'ill !eed to start creati !# tables. >ollo' directi o!s i! assi#!&e!t a!d i!
Appe!di ( A.".
Create table. Result is e&pt y table.
create table custo&ers (cid char($ !ot !ull, c!a&e varchar(1*,
city varchar("/, disc!t real, pri &ary key(cidC
0a&es a!d #ives types to each o) colu&!s: 3(plai! char($, varchar("/.
6ri&ary key clause &ea!s ca!G t duplicate i! table, )orei#! keys 'ill re)er to
this colu&! by de)aul t.
0ote O!ot !ull O is a! i!te#ri t y co!strai !t: &ust speci)y value o! i!sert, ca!G t
leave !ull. 7ore i!te#ri t y co!strai !ts i! Chapter E, o! DBA stu)).
0e(t, load )ro& 1S )ile i!to table. (Called sqlldr i! 1RAC43 0oFo!e 'a!ts
to type i! o!e li!e at a ti &e: everythi !# is &oved arou!d o! tape or disk
(say )ro& o!e DB7S to a!other. 0ote e(pected )or&at:
c//1,2ip2op,Duluth,1/.// UF F co&&a separated, !o spaces
c//",Basics,Dallas,1".//
F",F
:ou have to create a .ctl table )or each table, custs.ctl, . . . 2he! use Osql ldrO
co&&a!d (or sqlload i! 1racle versio! E )ro& the ;089 pro&pt (!ot )ro&
'i thi ! S@4W64;S.
A)ter thi!k have table loaded, use the sqlplus co&&a!d a!d 'ithi ! that
e!vi ro!&e!t type:
select W )ro& custo&ersCZ#
Should pri!t out 'hole table. (W &ea!s all colu&!s.
Chapter *.*. Si&ple Select State&e!ts (Basicall y sa&e as e(a&pl es i! the
te(t, althou#h starts so&e'hat di))ere!t.
3(a&ple *.*.1 . >i!d (A;S2 !a&es o) a#e!ts based i! 0e' :ork.
rel. al#.: (AG302S 'here city I G0e' :orkGJa!a&eK
S@4: select a!a&e from a#e!ts where city I G0e' :orkGC
Pey'ords: S343C2, >R17 (clause, <%3R3 (clause, e!d 'ith se&iF colo! (C.
A)ter S343C2 have select list , resul ti !# variables to report: project on.
2he >R17 clause !a&es tables to take Cartesia! product.
2he <%3R3 clause co!!ects a!d li&i ts ro's i! Cartesia! product. 0o Hoi!.
(S@4F." !o' allo's Hoi! operati o!: Cover later.
0ote duplicate a!a&e values %/E possible. S@4 state&e!t above ?1LL reF
port duplicate !a&es (Relatio!al rule broke! u!less 'ri te:
select distinct a!a&e )ro& a#e!ts 'here city I G0e' :orkGC
8) 'a!t to e&phasiBe that duplicates ca! arise, #ive !a&e to de)aul t:
select all a!a&e )ro& a#e!ts 'here city I G0e' :orkGC
Recall 'e did: select W )ro& custo&ersC at e!d o) table load.
Select W &ea!s all colu&!s (!o proHectio!. 2here is !o 'here clause so
'hole table is chose!. 4ike rel al# query: C;S2173RS
3(a&ple *.*.* . Select product ids o) products )or 'hich orders are placed.
select disti!ct pid )ro& ordersC
F"EF
7i#ht there be dupli cates i! thisM %o' avoid duplicatesM D8S280C2.
3(a&ple *.*.$ . Retrieve all (c!a&e, p!a&e pairs 'here the custo&er
places a! order )or the product. 8) do!G t use A180 operator i! S@4, ca!G t
&ake &istake o) Hoi!i!# o! city colu&! as 'ell. 8! R34A4G, !eed:
(CJcid, c!a&eK A180 1 A180 6Jc!a&e, p!a&eK
Recall that C is a! alias )or C;S2173RS, 1 )or 1RD3RS a!d 6 )or 6R1D;C2S.
Rather:
((C 9 1 9 6
'here C.cid I 1.cid a!d 1.pid I 6.pidJc!a&e, p!a&eK
8! S@4:
select disti!ct c!a&e, p!a&e )ro& custo&ers c, orders o, products p
'here c.cid I o.cid a!d o.pid I p.pidC
0ote that all table alias !a&es &ust be speci)ied i! si!#le state&e!t 'here
they are used, i! >R17 clause )ollo'i !# table !a&e 'i thout co&&a
i!terve!i !#.
8! 1RAC43 a!d 80>1R789, look up Oalias, tableO. 8! DB" ;S, say Ocorrelati o!
!a&eO, a!d OaliasO is so&ethi !# else. (Also i! 80>1R789, Ocorrelati o! !a&eO
is so&ethi !# elseQ
S@4 is !o!F procedural to the #reatest e(te!t possible, so a state&e!t ca!G t
depe!d o! a! earlier state&e!t. 0ote too, could leave out alias i! this
e(a&pl e ? Hust saves typi !# here.
0eed quali)ier 'he! colu&! !a&es are a&bi #uous )ro& o!e table to a! F
otherC !ot other'ise (above c!a&e, p!a&e, but o.cid, c.cid. But it !ever
hurts to quali)y colu&! !a&es.
Co!ceptual process i! Select. (1 2ake product o) tables i! )ro& clause, ("
appl y selectio! i! 'here clause, (* proHect o! attri butes i! selectF list a!d
report.
But this i! !ot !ecessaril y (usuall y !ot the actual order o) eve!ts. @uery
opti &iBati o! is applied. 8) custo&ers has 1/// ro's, orders 1//,///, a!d
products 1///, the! Cartesia! product has 1// billio! ro's. But eve!tual
a!s'er 'ill have at &ost 1//,/// ro's.
F"-F
Ca! &ake this &uch &ore e))i cie!t: i!de( custo&ers by cid a!d products by
pidC !o' #o throu#h orders o!e ro' at a ti &e a!d put out c!a&e, p!a&e
i!to te&porary )ile. >i!all y, sort c!a&e, p!a&e pairs a!d cast out dupli F
cates.
2his is 'hat the query opti &iBer &i#ht do i! its query pla!. Basicall y, a
query pla! is like a pro#ra& that retri eves the data you 'a!t. :ou do!G t
have to 'orry about it. 1!ly type S@4, query opti &iBer &akes the pro#ra&.
Ca! per)or& calculati o!s i! select.
3(a&ple *.*.+ . retri eve a table based o! orders, 'i th colu&!s ord!o, cid,
aid, pid, a!d pro)i t o! order.
select ord!o, (.cid, (.aid, (.pid, /.$/W((.qt yWp.pri ce
F /./1W(c.disc!t \ a.perce!tW((.qt yWp.pri ce as pro)i t
)ro& orders (, custo&ers c, a#e!ts a, products p
'here c.cid I (.cid a!d a.aid I (.aid a!d p.pid I (.pidC
(6ro)i t is qua!ti t y W price, 'here subtract o)) ,/[ o) cost, disc!t )or
custo&er, a!d perce!t co&&issio! )or a#e!t. 3(pressio!s allo': \, F, W, N
WW, a!d )u!ctio!s: upper( or ceil( (1racle, s&alles i!te#er #reater tha!
)loat FF )u!cti o!s 'i th !u&eric ar#u&e!ts are !ot part o) sta!dard.
<ithout the Oas pro)i tO clause, 1racle 'ill head the colu&! 'i th the e( F
pressio!. A)ter clause put i!, headi!# 'ill be Opro)i tO 8! 1RAC43, this is
called a colu&! alias, co&parabl e to the 'ay table aliases are created.
8! 80>1R789, it is called a ODisplay 4abelO.
2able alias is also called correlati o! !a&e (i! DB" a!d ra!#e variabl e.
So&eti &es !ot Hust shortha!d, but !ecessary, as 'he! Hoi! table 'i th it sel)
(!eed di))ere!t quali)yi !# !a&es.
3(a&ple *.*., . 4ist all pairs o) custo&er cids based i! the sa&e city.
<e have see! this ki!d o) thi!# be)ore i! Rel Al#. 0eed to re port disti!ct
pairs o) cid o!ly o!ce.
select Jdisti !ctMK c1.cid, c".cid )ro& custo&ers c1, custo&ers c"
'here c1.ci ty I c".ci ty a!d c1.cid U c".cidC
0ote, could be upper case C1 a!d C" colu&! aliasesC lo'er case is our
#e!eral rule, but !ot crucialC Hust !eed to be co!siste!t (case cou!ts.
F".F
Do 'e !eed to say disti!ct above, classM (0o. Because (c1, c" is a set o)
u!ique pairs. <aste to use disti!ct 'he! !ot !ecessary.
Alias idea is to &ake t'o copies o) all co!te!t o) custo&ers table as c1 a!d
c"C the! take Cartesia! product, per)or& selectio!, a!d )i!all y report out
proHect o!to c1, c".
But 'hat syste& reall y does is (dra' custo&ers table, sho' c1 a!d c" as
variables ra!#i!# over ro's, per)or& !ested loop ? RA0G3 5AR8AB43S:
>1R c1 >R17 R1<S 1 21 4AS2 1> custo&ers
>1R c" >R17 R1<S 1 21 4AS2 1> custo&ers
8> (c1.ci ty I c".city a!d c1.cid U c".cid
6R802 1;2 S343C2F48S2 5A4;3S: c1.cid, c".cid
30D >1R c"
30D >1R c1
1!ce a#ai! c1 a!d c" do !ot represe!t copied tables, but variables taki !#
o! ro' values i! the sa&e tableQ 2rick here is to 012 7A23R8A48V3 2%3
6R1D;C2. A!d !o' see 'hy aliases are so&eti &es called ra!#e variables:
'e ca! picture the& as variabl es that ra!#e over the ro's o) a table.
F*/F
Class +. Due Class .: 3(ercises throu#h e!d o) Sectio! *.+.
3(a&ple *.*.E . >i!d pids o) products ordered by at least t'o custo&ers.
012 3AS:Q %o' to doM 2hi!k o) idea o) t'o ra!#e variabl es alive at sa&e
ti &e i! o!e table. 6icture i! orders tables. Say pid the sa&e a!d cid di) F
)ere!t )or t'o.
select Jdisti !ctMK (1.pid )ro& orders (1, orders ("
'here (1.pi d I (".pi d a!d (1.cid UT (".cidC
0eed disti!ctM :es. 6icture Cartesia! product, pid p/1 o!ce 'i th three
di))ere!t pid values, p/1, p/", p/*. <hat happe!sM 2here are three pairs o)
ro's that (1, (" ca! )all o!: (p/1, p/", (p/1, p/*, a!d (p/", p/*, A0D the
R353RS3Q
Ca! at least reduce to three ti &es by cha!#i!# UT above to U. Cha!#es
!ested loop, assu&i!# ! ro's i! orders )ro&:
>1R (1 >R17 R1<S 1 21 ! 1> orders FF 68C2;R3 RA0G3 5AR8AB43S
>1R (" >R17 R1<S 1 21 ! 1> orders
to:
>1R (1 >R17 R1< 1 21 ! 1> orders
>1R (" >R17 R1< (1 21 ! 1> orders
1bviousl y saves e))ort, si!ce less loopi!#.
3(a&ple *.*.- . Get cids o) custo&ers orderi !# a product )or 'hich a! or der
is placed by a#e!t a/,. Re&i!isce!t o) a! e(a&pl e 'e had i! rel al#. 0ote
that a cid retrieved does!G t !ecessaril y order a!y product throu#h a/,.
NFF Custo&ers CR3A23 68C2;R3 1> RA0G3
N FF 6roducts FFF 'ho 5AR8AB43S A0D %1< 21
a/, FFF ordered by FFF place orders S1453 2%8S 2%80P80G
80
Z FF a#e!t a/, FFF )or those 23R7S 1> @B3C 2%A2
ZFF products 2RA0S4A23S D8R3C24:
21 S@4 S2A237302
2<1 co!!ectio!s throu#h orders relatio!, so !eed t'o aliases. >irst prod F
ucts ordered by a/, ()irst cloud above:
select Jdisti !ctMK (.pid )ro& orders ( 'here (.aid I Ga/,G C
<ould !eed disti!ct i) 'a!ted u!ique (.pid values. 0e(t, custo&ers 'ho
order those products:
F*1F
select disti!ct y.cid )ro& orders (, orders y 03S23D 4116 1>
'here (.aid I Ga/,G a!d y.pid I (.pidC (S41< RA0G3 5AR8AB43S
(.pid reports back the )irst cloud above, a!d set y.pid I (.pid to #et seco!d
cloud y.cid.
But a! eve! 71R3 strai#ht)or'ard 'ay to do that is to use a subquery
(Sectio! *.$:
select disti!ct y.cid )ro& orders y 'here y.pid i!
(select (.pid )ro& orders ( 'here (.aid I Ga/,G C
2he OsubqueryO retri eves the )irst cloud, a!d the! the outer select re trieves
all y.cid 'here y.pid is Oi!O the )irst cloud.
2he co!di ti o! Oi!O is a! operator e(pecti !# a variabl e o! the le)t a!d a OsetO
o) values o! the ri#ht ()ro& a subqueryC it is true 'he! the vari able o! the
le)t is equal to a!y o) the ele&e!ts retri eved by the subquery.
1) course the outer Select retri eves all ro's )or 'hich the co!di tio! is true.
>or the )irst ti &e, 'e are usi!# so&e recursio! based o! the idea that a
select retrieves a table. 0o' 'e ca! operate o! that table as a set o) ro's
(or i! the case Hust covered, a set o) values.
NO2E: >or a!y type o) query 'e have lear!ed so )ar, i) 'e ca! do it 'i th
Hoi! 'e ca! do 'i th subquery a!d viceF versa. Subqueri es are supposed to
#ive a sli#htl y &ore !atural approach to so&e queries. B;2 8 'ill i!sist that
you be able to tra!slate Subqueries to Hoi!s i! &ost cases.
3(a&ple *.$.1 . Retrieve cids o) custo&ers 'ho place orders 'i th a#e!ts i!
4os A!#eles or Dallas. >irst )i!d all a#e!ts i! 4A or Dallas, a!d &ake that a
subquery:
select cid )ro& orders 'here aid i! (select aid )ro& a#e!ts
'here city I G4os A!#elesG or city I GDallasG C
0ote do!G t !eed to quali)y aid i! subquery use or outside subquery: i! each
case there is o!l y o!e table that is !atural.
Co!ceptual l y, i!!er&ost subquery operates )irst, retur!s a set o) values,
the! outer select ru!s throu#h custo&er ro's a!d tests 'hether cid is i!
this set. 8! )act that &i#ht 012 be the 'ay it is do!e by the syste& (a)ter
query opti &iBati o!. 3quivale!t to:
F*"F
select Jdisti !ctMK (.cid )ro& orders (, a#e!ts a 'here
(.aid I a.aid a!d (a.ci ty I G4os A!#elesG or a.city I GDallasG C
<hy ( sM Because o) order o) operati o!s: p a!d q or r is (p a!d q or r,
but that is!G t 'hat 'e 'a!tC 'a!t p a!d (q or r.
1ther'ise, i) r 'ere true )or a!y p, q, 'hole state&e!t trueC the! i) a#e!ts
has so&e a#e!t i! Dallas, 'ill #et all cids reported ('o!G t !eed (.aid I
a.aid.
Skip a )e' si&ple e(a&pl es. 8! particular, ca! co!struct your o'! set usi!#
a co!di tio! such as: . . . 'here . . . city i! (GDuluthG , GDallasG . 1) course 'e
could also do this usi!# or,
All these varia!t 'ays o) doi!# thi!#s do!G t !ecessaril y help ? Hust &ake
user u!co&)ortabl e about havi !# a co&plete #rasp o) the S@4 sy!ta(.
1P, !o' idea o) correlated subquery. Recall i! *.$.1 did!G t !eed quali)ied
!a&es because ro's had !atural ho&e. But ca! re)er to 1;23R ra!#e vari F
able )ro& 8003R subquery.
3(a&ple *.$.$ . (5aria!t >i!d !a&es o) custo&ers 'i th disc!t TI 1/ 'ho
order product p/+. 2o illustrate a poi!t, usi!# subquery:
select c!a&e )ro& custo&ers 'here disc!t TI 1/ a!d
Gp/+G i! (select pid )ro& orders 'here cid I custo&ers.ci dC
:es, u!usual to say 'here Uco!st T i! subquery (looks like a typo, but per F
)ectl y valid a!d as lo!# as co!di ti o! is true 'ill retrieve c!a&e.
0ote that i! subquery, the cid 'i thout a quali)ier has ho&e quali)ier o) or F
ders, 'hile custo&ers.ci d harks back to outer select. As i) 'e 'rote:
>1R c >R17 R1< 1 21 4AS2 1> custo&ers
discard c i) c.disc!t U 1/
create 4 as e&pty list
>1R ( >R17 R1< 1 21 4AS2 1> orders
Add (.pid to list 4 i) (.cid I c.cid NW value set outside loop WN
30D >1R (
discard c i) Gp/+G !ot i! list 4
other'ise place c.c!a&e i! A0S<3R list
30D >1R c
pri!t out A0S<3R list
0ote ca!G t precalculate the i!!er loopC !eed the outer loop to be set (2his is
called a correlated subquery . 1) course, 'e could cha!#e query to:
F**F
select c!a&e )ro& custo&ers 'here disc!t TI 1/ a!d
cid i! (select cid )ro& orders 'here pid I Gp/+G C
Ca! evaluate this 'i th precalculati o! o) subquery.
create 4 as e&pty list
>1R ( >R17 R1<S 1 21 4AS2 1> orders
Add (.cid to list 4 i) (.pid I Gp/+G
30D >1R (
>1R c >R17 R1<S 1 21 4AS2 1> custo&ers
i) (c.disc!t U 1/ or i) c.cid !ot i! list 4
discard c a!d #o to !e(t i! loop
else place c.c!a&e i! A0S<3R list
30D >1R c
pri!t out A0S<3R list
1P, ca! re)er to outer variabl e )ro& i!!er loop. But ca!G t do the R353RS3QQ
3(a&ple *.$.+. 2o #et !a&es o) custo&ers 'ho order product p/E )ro&
a#e!t a/*, ca!G t use query:
select c!a&e )ro& custo&ers 'here orders.aid I Ga/*G a!d
Gp/EG i! (select pid )ro& orders 'here cid I custo&ers.ci dC
<<<1LLE>%L<<<
2he co!di ti o! orders.aid I Ga/*G ca!G t be used outside subquery (does!G t
e(ist yet FF scopi!# rule
(WW !e' WW 3(a&ple. >i!d cids o) custo&ers 'ho do !ot place a!y order
throu#h a#e!t Ga/*G . Ca! use !ot i!, true e(actl y 'he! i! is )alse:
select cid )ro& custo&ers 'here cid !ot i! (select cid )ro&
orders 'here orders.aid I Ga/*G C NW e(a&pl e o) &i!us operator WN
3(a&ple *.$.,. 8! older products, 80 predicate o!l y 'orks )or si!#le values.
Ca!G t have co!di ti o! O. . . 'here (cid, aid i! (select cid, aid )ro& ordersO . . .
2his is part o) the )ull S@4F." sta!dard (0ote !eed (,, a!d 1racle per&i ts it.
Auanti fi ed Comparison Predicates . 4ist:
Fsome GHsome GFsome Hsome Gsome HFsome
Fany GHany GFany Hany Gany HFany
Fall GHal l GFal l Hall Gall HFal l
F*$F
8) co&pariso! operator sta!ds )or operator i! the set R U, UI. I, UT, T,
TIS ,
v so&e (subquery is 2R;3 i)) v s )or some s i! the subquery
v a!y (subquery is 2R;3 i)) v s )or some s i! the subquery
v all (subquery is 2R;3 i)) v s is true )or all s i! the subquery.
so&e a!d a!y &ea! sa&e thi!#: so&e is easier to re&e&ber.
3(a&ple *.$.E . >i!d aid values o) a#e!ts 'i th a &i!i &u& perce!t co&F
&issio!. 2his 'as %ARD i! relati o!al al#ebra. 0o' easy.
select aid )ro& a#e!ts 'here perce!t UI (select perce!t )ro& a#e!tsC
F*+F
Class ,.
3(a& 1 o! Class 1* Respo!sible )or &ost o) Chapter *.
1P, last ti &e talki!# about @ua!ti )ied Co&pariso! 6redicates So&e, A!y, or
All. (:ou are respo!si ble )or this !o&e!clature, here a!d as i! book.
3(a&ple *.$.-. >i!d all cids o) custo&ers 'ho have the sa&e discou!t as
a!y custo&er i! Dallas or Bosto!.
select cid )ro& custo&ers 'here disc!t Iso&e (select
disc!t )ro& custo&ers 'here city I GDallasG or city I GBosto!G C
1P, Iso&e sa&e as Ia!y or i!. Could do a'ay 'ith i!. But Uso&e, etc.
!e'.
0e' 3(a&pl e. >i!d cids o) custo&ers 'ho do !ot place a!y order throu#h
a#e!t Ga/*G . ;sed !ot i!, above:
select cid )ro& custo&ers 'here cid !ot i! (select cid )ro&
orders 'here orders.aid I Ga/*G C
Si!ce Ia!y is sa&e as i!, is it true that UTa!y is sa&e as !ot i!M
select cid )ro& custo&ers 'here cid UTa!y (select cid )ro&
orders 'here orders.aid I Ga/*G C
01Q Does!G t 'orkQ UTa!y is true i) cid I Gc//1G a!d subquery is RG c//1G ,
Gc//*G S . 2here 8S 103 value it is !ot equal to. Really &ea! UTal l . 2he re F
verse o) Ia!y is UTal l . 3(ercise o! this.
By the 'ay, 'hat do you thi!k happe!s i) you 'ri te:
select cid )ro& custo&ers 'here cid UT (select cid )ro&
orders 'here orders.aid I Ga/*G C
Aust UT, !ot UTa!y or UTal lM Does it eve! &ake se!se i) si!#le value o!
le)t, set o! ri#htM A!s'er is, per)ectl y le#al i) set o! ri#ht co!tai!s e( actl y
o!e ele&e!t. 1ther'ise, ru!ti &e error.
By the 'ay, 'hat happe!s i) cid TI subquery, a!d subquery is a! e&pty
setM 8s the resul t 2R;3 or >A4S3M 2he a!s'er is, resul t is ;0P01<0:
al&ost like )alse, but !ot quite. 7ore later.
3(a&ple *.$.. . Get cid values o) custo&ers 'i th disc!t s&aller tha! disc!t
o) a!y custo&er 'ho lives i! Duluth. (8s the )ollo'i !# ri#htM
F*,F
select cid )ro& custo&ers 'here disc!t Ua!y
(select disc!t )ro& custo&ers 'here city I GDuluthG C
01Q <R10G 3>>3C2Q 1n Englis here, Os&aller tha! a!yO &ea!s Os&aller
tha! allO. <hy i! S@4F." replace a!y 'ith so&e (less a&bi#uous.
2e E:ists predicate .
e(ists(subquery is true i)) subquery is a !o!F e&pty set.
!ot e(ists(subquery is true i)) subquery is a! e&pty set.
;se !ot e(ists &ost o)te! to ha!dle >1R A44 co!di ti o!s.
3(a&ple *.$.1/ . >i!d c!a&es o) custo&ers 'ho place a! order throu#h
a/+.
select c!a&e )ro& custo&ers c 'here e(ists (select W )ro& orders (
'here (.cid I c.cid a!d (.aid I Ga/+G C
1P, that 'orks, but also:
select c.c!a&e )ro& custo&ers c, orders (
'here (.cid I c.cid a!d (.aid I Ga/+G C
8!teresti !#. Do!G t say a!ythi !# e(plici t about (, do!G t say, Oselect c.c!a&e
i) there e(ists a! order ( co!!ecti !# c to Ga/+G ,O but thatG s 'hat 'e &ea!.
6icture this i! ter&s o) ra!#e variables o! tables: ( &ust e(ist co!!ect i!#
c.cid to Ga/+G .
8) 'e do!G t retrieve a colu&! )ro& a table ra!#e variable, that ra!#e vari F
able is called u!bou!d (predicate lo#ic se!se FF a! u!bou!d variable, a!d
there is a! i&plici t Oe(istsO presu&pti o! that &akes the resul t true
Sa&e as sayi!# ra!#e variabl es ra!#e i!depe!de!tl y a!d retrieve all ro's o)
product 'here co!di ti o! is true.
2his basicall y &ea!s that to use the )or& O3(istsO is u! !ecessary. Do!G t use
3(ists i) do!G t !eed it.
!ot e(ists, o! the other ha!d, is a di))ere!t &atter.
3(a&ple *.$.1". Retrieve all custo&er !a&es 'here the custo&er does !ot
place a! order throu#h Ga/+G . Recall that this does!G t 'ork:
F*EF
select c.c!a&e )ro& custo&ers c, orders (
'here !ot (c.cid I (.cid a!d (.aid I Ga/+G C <<< ?/ON> E77EC2 <<<
Because custo&ers c 'here c.cid I Gc//1G a!d orders ( 'here (.cid I Gc//1G
a!d (.aid I Ga/1G , so co!di tio! is )alse, !ot is true, a!d Gc//1G is retri eved.
But c//1 does order throu#h a/+.
2#E/E 1- NO ?%0 2O DO 2#1- ?12#O42 % -43A4E/0D <a!t:
select c.c!a&e )ro& custo&ers c 'here !ot e(ists
(select W )ro& orders ( 'here c.cid I (.cid a!d (.aid I Ga/+G C
Recall 'e did so&ethi !# like this above also, 'i th !ot i!:
select c!a&e )ro& custo&ers 'here cid !ot i!
(select cid )ro& orders 'here aid I Ga/+G C
8s !ot e(ists &ore po'er)ul tha! !ot i!M
3(a&ple. >i!d all cid, aid pairs 'here the custo&er does !ot place a! or der
throu#h the a#e!t:
select cid, aid )ro& custo&ers c, a#e!ts a 'here !ot e(ists
(select W )ro& orders ( 'here (.cid I c.cid a!d (.aid I a.aidC
0ote !ot i! ;S3D 21 assu&e a si&ple value, so could!G t 'ri te:
select cid, aid )ro& custo&ers c, a#e!ts a 'here (cid, aid !ot i!
(select cid, aid )ro& orders (C
As a resul t, !ot e(ists 'as able to do thi!#s !ot i! 'as!G t. But this is !o
lo!#er true (S@4F.", 1racle 5E.1, . . .
-ection '.* . %o' to do >1R A44 (5ery %ard a!d ;!io! (easy
3(a&ple *.+." . >i!d cids o) custo&ers 'ho place orders 'ith A44 a#e!ts
based i! 0e' :ork. 8! rel al#:
1RD3RSJcid, aidK D858D3B: (AG302S 'here city I G0e' :orkGJai dK
%arder i! S@4. >or&ulate as )ollo's.
<a!t to retrieve cid such that >1R A44 a#e!ts based i! 0e' :ork, there
398S2S a! order co!!ecti !# cid to a#e!t.
F*-F
But 'e do!G t have a >1RA44 predicate. 8) 'e did, 'eG d like to 'ri te i! S@4:
select cid )ro& custo&ers c 'here
>1RA44(select aid )ro& a#e!ts a 'here city I G0e' :orkG,
398S2S(select W )ro& orders ( 'here (.aid I a.aid a!d
c.cid I (.cidC
<here >1RA44(subquery, co!di tio! is true i)) )or all ele&e!ts i! the sub F
query, the co!di tio! is true. (Bei!# e(plici t about 398S2S here )or a rea so!.
1P, !o >1RA44 predicate, so i!stead have to use a trick. >1RA44 ((, p is
equival e!t to 012 398S2S ((, 012 p (there is !o cou!tere(a&pl e 'hich
'ould cause >1RA44 to )ail.
8! our case, 'e have >1RA44((, 398S2S (y, p is equi vale!t to 012 398S2S
((, 012 398S2S (y, p. So state&e!t above beco&es:
select cid )ro& custo&ers c 'here
!ot e(ists (select aid )ro& a#e!ts a 'here city I G0e' :orkG a!d
!ot e(ists (select W )ro& orders ( 'here (.aid I a.aid a!d
c.cid I (.cidC
4ook at )or& above a#ai! )or >1RA44. 8! #e!eral, queries 'hich obey a
>1RA44 co!di tio!, have this )or&:
select . . . 'here !ot e(ists( select . . . 'here !ot e(ists( select . . . C
%ardest query you 'ill be asked to pose. 4ast hard trick !eed to )i!ish
ho&e'ork.
0e' 3(a&ple . Get aids o) a#e!ts 'ho place orders )or all custo&ers 'ho
have discou!t #reater tha! -.
Assu&e 'e are thi!ki !# o) retrievi !# aid )or a! a#e!t that 'eG re !ot sure )its
the bill. %o' 'ould 'e speci)y a cou!terF e(a&pl e usi!# the e(ists co!ceptM
2here e(ists so&ethi !# that 'ould rule out aid. (ClassM
(A!s'er: 2here e(ists a custo&er 'ith disc!t T - )or 'hich aid does !ot
place a! order.
Good: that 'ould rule out aid. Assu&e 'e are i!side a subquery, 'ith a.aid
speci)yi !# the outer aid to rule out: thi!k o) it as a co!sta!t like Ga/"G . %o'
'ould you select a custo&er cid 'ith disc!t T - )or 'hich a/" does !ot
place a! orderM
select cid )ro& custo&ers c 'here c.disc!t T - a!d (ClassM
F*.F
!ot e(ists(select W )ro& orders ( 'here (.cid I c.cid
a!d (.aid I Ga/"G C
Ri#htM 1P, !o' 'e 'a!t to &ake that (.aid I a.aid, 'here a.aid is i! a!
outer select, a!d say that 01 C1;023RF39A7643 C8D 398S2SQ
select aid )ro& a#e!ts a 'here 01 C1;023RF39A7643 398S2SQ
select aid )ro& a#e!ts a 'here !ot e(ists ( subquery above, 'i th
Ga/"G replaced by a.aidC
select aid )ro& a#e!ts a 'here !ot e(ists
(select cid )ro& custo&ers c 'here c.disc!t T - a!d
!ot e(ists(select W )ro& orders ( 'here (.cid I c.cid
a!d (.aid I a.aidC
<he! have to do >1RA44: (Go throu#h stepso! p#. 11/.
3(a&ple *.+.+. >i!d cids )or custo&ers 'ho order all products ordered by
custo&er c//,.
1P, 'e are retri evi !# M.cid. (Do!G t k!o' )ro& 'hat table, custo&ers or or F
ders %o' disquali)y itM (1 Give Cou!ter e(a&pl e i! 3!#lishM (ClassM
2here is a product ordered by custo&er c//, that our ca!didate
custo&er M.cid does !ot order.
(" Select all cou!ter e(a&pl es. Start you o)):
select pid )ro& orders ( 'here (.cid I Gc//,G a!d
FF products ordered by Gc//,G a!d (ClassM
!ot e(ists (select W )ro& orders y 'here (.pidI y.pid a!d y.cidI M.cidC
(* 1P, !o' say !o cou!terF e(a&pl e e(ists: !ot e(ists (subquery above
($ 4et the ro' 'e 'a!t to retrieve be ra!#e variable i! outer select 'i th !o
cou!terF e(a&pl es. (;suall y choose ra!#e variabl e i! s&allest table.
select c.cid )ro& custo&ers c 'here !ot e(ists
(select pid )ro& orders ( 'here (.cid I Gc//,G a!d !ot e(ists
(select W )ro& orders y 'here (.pid I y.pid a!d y.cid I c.cidC
1P, subtracti o! a!d u!io! i! Sectio! *.+ are very easy. 0e(t ti &e 8 'ill #o
o! to *.,.
F$/F
Class ;.
0e(t h', all u!dotted e(ercises throu#h e!d o) Chapter *. Due class 1*.
3(a& 1, Class 1$. Respo!sible )or A44 o) Chapter " a!d * (e(cept !o detail
o! Chapters *.1/ a!d *.11 ? Respo!sible )or all ho&e'ork a!d co!cepts
covered i! class.
-ection '.)
Start 'i th ;!io!, Di))ere!ce, 8!tersect: ho' to do i! S@4. <e i!te!d to add
!e' operators to per)or& these operati o!s i! this Sectio!. Already cov ered
;!io! i! Sectio! *.+.
2hese operators are 012 u!i)or&l y available i! the di))ere!t prod ucts.
0e' 3(a&ple . 4ist all cities that co!tai ! the products costi!# less tha!
Y1.// or that co!tai ! a#e!ts 'ho have &ade !o sales.
8t is easy to #et cities associated 'i th products or cities associated 'ith
a#e!ts. But you should see that !o si&ple query o) the )or&:
retrieve (.ci ty )ro& table1 (, table" y, . . .
8s #oi!# to 'ork, si!ce (.city co&es )ro& o!e table o!l y. 7i#ht co!cei v abl y
do this by retrievi !#:
retrieve (.ci ty, y.ci ty )ro& table1 (, table" y, . . .
But the! 'ill be retri evi !# pairs, a!d that &esses us up. <hat 'ill 'e
retrieve i) there are 01 cities o) the seco!d ki!dM (ClassM 0othi!#.
2his is easy usi!# ;0810 o) relati o!al al#ebra, a!d it tur!s out that sta! dard
S@4 has a u!io! )or& 2%A2 A44 530D1RS %A53 8764373023D. A!s'er
select city )ro& products 'here price U 1.//
u!io!
select city )ro& a#e!ts 'here aid !ot i!
(select aid )ro& ordersC
0o' co!sider the Subquery form. 2'o )or&s o!l y, Subquery a!d Select
state&e!t. Subquery is a select sql sy!ta( that 'ill #o i! (subquery.
4ater, a Select state&e!t adds a clause (1RD3R B: a!d ca!!ot appear i!
(subquery. But u!til the!, Subquery a!d Select state&e!t are ide!tical.
F$1F
A Subquery )or& does!G t %A53 to appear as a subquery ? Hust a prescrip F
tio! )or a state&e!t )or&at. 8t ca! be a! outer Select state&e!t.
So here is the )or&at )or ;0810, 8023RS3C2, a!d 39C362 (relati o!al al#e bra
D8>>3R30C3 or 780;S
subquery R;0810 JA44K ] 8023RS3C2 JA44K ] 39C362 JA44K S
7igure '.!I Adva!ced S@4 Subquery >or&
0ote that )e' products have i&ple&e!ted all o) these possibili ti esC ;0810
JA44K is part o) 3!try S@4F." a!d i&ple&e!ted by all serious products.
8dea o) A44 i! these three cases. ;0810 A44 ca! cause several copies o) a
ro' to e(ist a)ter bei!# applied to &ul ti pl e subqueri es. 2akes !u&ber i!to
accou!t.
So ca! have * ro's to i!tersect 'i th ". Result : " (8) use A44QQ 1!e
other'ise. Ca! have " ro's to subtract )ro& *. Result : 1
(8) do!G t use A44, 'ould #et resul t /.
<e have already e!cou!tered the co!cept o) subtracti o! usi!# !ot e(ists.
3(a&ple *.$.1$. >i!d cids o) custo&ers 'ho do !ot place a!y order throu#h
a#e!t a/*.
select cid )ro& custo&ers c 'here !ot e(ists
(select W )ro& orders 'here cid I c.cid a!d aid I Ga/*G C
2his i!cludes custo&ers 'ho have placed !o orders. 8) 'e had asked )or
custo&ers 'ho placed so&e order but !o!e throu#h a/*, 'ould cha!#e cus F
to&ers c above to orders (.
0o' the 'ay 'e 'ould 'ri te this 'i th our !e' operators is:
select cid )ro& custo&ers e(cept
select cid )ro& orders 'here cid I c.cid a!d aid I Ga/*G C
Recall, idea o) too &a!y equi vale!t )or&s. 2his &akes it 'orse. A44 is a !e'
capabili ty, but is it ever 'a!tedM
%o' 'ould 'e i&ple&e!t Oi!tersectOM Get list o) cities that co!tai ! products
A0D a#e!ts.
select city )ro& products 'here city i! (select city )ro& a#e!tsC
F$"F
<ith use o) 8023RS3C2, this beco&es:
select city )ro& products i!tersect select city )ro& a#e!tsC
2he C1RR3S610D80G clause is used to select a set o) equival e!tl y !a&ed
colu&!s o! 'hich to per)or& i!tersecti o! (laBy about selectLlist.
1P, !o' e(pa!ded de)i!i ti o! o) >R17 Clause. S@4F-. De)i!i ti o! 'as:
)ro& table!a&e JJASK corrL!a&eK R, table!a&e JJASK corrL!a&eK... S
8&ple&e!tati o! o) these operators.
1RAC43 Release - provi des ;0810, ;0810 A44, 8023RS3C2 a!d 780;S
(varia!t o) 39C362, but !ot 8023R3S3C2 A44 or 780;S A44 a!d provi des !o
C1RR3S610D80G Clause.
DB" ;!iversal Database, 5ersio! +, i&ple&e!ts R;0810 ] 8023RS3C2 ]
39C362S JA44K, but !o C1RR3S610D80G Clause.
80>1R789 basicall y i&ple&e!ts o!ly ;0810 JA44K a!d reco&&e!ds
'orkarou!ds )or the other operators.
2o de)i!e S@4F." >R17 Clause #e!eral )or&, start 'ith de) o) tablere):
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
tablere):: I table!a&e JJASK corrL!a&eK J(col!a&e R, col!a&e ... SK K
] (subquery JJASK corrL!a&eK J(col!a&e R, col!a&e ... SK
] tablere)1 J 8003R ] R43>2 ] R8G%2 ] >;44S 1;23R K
A180 tablere)"
J10 searchLco!di ti o! ] ;S80G (col!a&e R, col!a&e . . . SK
>R17 clause :: I >R17 tablere) R, tablere) . . .S
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
>i#ure *.11 S@4F.. Recursive tablere) De)i!i ti o!
0ote that tablere) is recursi ve: tablere) ca! be a!y ki!d o) Hoi! o) t'o
tablere)s.
4etGs start 'ith the )act that o!e ca! Select >R17 a Subquery.
3(a&ple *.,.*. Retrieve all custo&er !a&es 'here the custo&er places at
least t'o orders )or the sa&e product. %ereGs a !e' 'ay:
select c!a&e )ro&
(select o.cid as spcid )ro& orders o, orders (
F$*F
'here o.cid I (.cid a!d o.pid I (.pid a!d o.ord!o UT (.ord!o as y,
custo&ers c 'here y.spcid I c.cidC
0ote alias y )or Subquery, alias spcid )or colu&! o.cid.
>oreshado'i !# i! this query )or& o) so&ethi !# called 5ie's.
0e(t, talk about the various Aoi! operators o! li!es * D $ (a!d +
Ca! say: tablere)1 CR1SS A180 tablere)"
select c!a&e )ro& custo&ers c cross Hoi! orders o 'here . . .
Sa&e e))ect as
select c!a&e )ro& custo&ers c, orders o 'here . . .
1P, !o' i) 'e o!l y use the A180 key'ord i! the #e!eral )or& above, 'e ca!
use o!e o) the )or&s o! li!e +, either the 10 searchLco!di ti o! or ;S80G.
<e call it a co!di ti o! Hoi! 'he! t'o di))ere!t !a&ed colu&!s are to be
co&pared i! a! 10 clause. 3.#. i) had cities table 'i th colu&!s !a&ed
city!a&e, lati tude a!d lo!#i tude, could 'ri te:
select city, lati tude, lo!#i tude )ro& custo&ers c Hoi! cities (
o! (c.city I (.ci ty!a&eC

1P, !e(t is 0A2;RA4 A180.
select c!a&e )ro& custo&ers !atural Hoi! ordersC
Aust like i! relatio!al al#ebra (!atural , equiHoi!. Ca! already do this:
select c!a&e )ro& custo&ers c, orders o 'here c.cid I o.cidC
0A2;RA4 A180 uses all co&&o!l y !a&ed colu&!s to )orce a! equali ty &atch
o! the t'o ar#u&e!t tables.
E:ample '.). ( Retrieve all custo&ers 'ho purchased at least o!e product
costi!# less tha! Y/.+/. 8! relatio!al al#ebra, 2%8S is 'ro!#:
((1RD3RS A180 6R1D;C2S 'here price U /.+/ A180 C;S2173RS Jc!a&eK
F$$F
(<hy, classM Because cities o) 6R1D;C2S a!d C;S2173RS 'ill &atch,
beco&es: Retrieve custo&ers 'ho purchased at least o!e product costi!#
less tha! Y/.+/ that is stored i! the sa&e city as the custo&er.
Correct this 'i th )ollo'i !# )or&:
((1RD3RS A180 (6R1D;C2S 'here price U/.+/ JpidK A180 C;S2173RS
Jc!a&eK
8! S@4, usi!# 8003R A180, the de)aul t A180 operator:
select c!a&e )ro& ((orders Hoi!
(select pid )ro& products 'here price U /.,/ p usi!# (pid op
Hoi! custo&ers usi!# (cidC
or hereGs a co!di tio! Hoi!:
select c!a&e
)ro& ((orders Hoi!
(select pid )ro& products 'here price U /.,/ p o! o.pid I p.pid op
Hoi! custo&ers c o! op.cid I c.cidC
A ;S80G clause li&i ts the colu&!s equated i! a A180 to a set o) co&&o!l y
!a&ed o!es (i.e., sa&e colu&! !a&e i! both tables i!vol ved i! the Hoi!.
Si!ce city is !ot !a&ed i! the usi!# clause or 10 clause, 'e do!G t ru! i!to
the troubl e 'e had 'i th the city colu&! i! relati o!al al#ebra.
1P, !e(t is O42E/ 9O1N.
Covered this i! Chapter ". 8dea is that 21 >;44 1;23R A180 2" (&ust say
>;44, or 43>2 or R8G%2, ca!G t Hust say O1;23R A180O, i) 21 has ro's that
do!G t &atch i! the Aoi! they are i!cluded i! the a!s'er 'ith !ulls )or the
colu&!s )ro& 2", a!d viceF versa.
<e also have 43>2 1;23R A180 a!d R8G%2 1;23R A180. 43>2 &ea!s that 'ill
o!l y preserver ro's )ro& the le)tF ha!d table i! resul t, R8G%2 a!alo#ousl y.
8003R A180 &ea!s opposi te o) 1;23R, 'hat 'e have bee! usi!# 'i thout
thi!ki !# all this ti &e: 8003R is the de)aul t o) 1;23R a!d ;0810 A180.
;0810 A180 is so&ethi !# !e', but u!clear ho' use)ul. Pi! o) a #e! er al F
iBatio! o) 1;23R A180. Say have t'o tables S a!d 2
S 2
F$+F
C A A B
c1 a1 a1 b1
c* a* a" b"
c$ a$ a* b*
2he >;44 1;23R A180 o) S a!d 2 'ould #ive the )ollo'i !#:
select W )ro& S >;44 1;23R A180 2
S.C S.A 2.B
c1 a1 b1
c* a* b*
c$ a$ !ull
!ull a" b"
But this is ;0810 A180. Do!G t eve! try to &atch.
select W )ro& S >;44 1;23R A180 2
S.C S.A 2.A 2.B
c1 a1 !ull !ull
c* a* !ull !ull
c$ a$ !ull !ull
!ull !ull a1 b1
!ull !ull a" b"
!ull !ull a* b*
Aoi! >or&s i! operati o!al Database Syste&s
1RAC43 Release - provi des o!l y 4e)t a!d Ri#ht 1uter Aoi!, speci)ied i!
<here Clause. Ge!eral )or&:
S343C2 . . . >R17 21, 2" <%3R3 R21.c1 J(\K I 2".c" ] 21.c1 I 2".c"
J(\K S C
0ote o!l y o! o!e side. 6lus i! pare!theses )ollo'i !# the table &ea!s it 'ill
ACC362 !ulls i! the other table FF 'ill 6R3S3R53 ro's i! other table.
F$,F
Class !I.
Collect h' throu#h *.E. 0e(t h', all u!dotted e(ercises throu#h e!d o)
Chapter *
3(a& 1, Class 1$, <ed 1ct "". Respo!sible )or A44 o) Chapter " a!d *
(e(cept !o detail o! Chapters *.1/ a!d *.11 ? Respo!sible )or all ho&e'ork
a!d co!cepts covered i! class.
-ection '.) . Set >u!ctio!s i! S@4. cou!t, &a(, &i!, su&, av#. 1perates
o! sets o) values, retur!s a si!#le value (all e(cept cou!t, 'hich ca! op erate
o! sets o) ro's.
select su&(dollars )ro& orders 'here aid I Ga/1G C
Retur!s total dollar sales o) a#e!t a/1 (!ote 'here clause #ets applied
)irstQQ Q. 4ooks like:
C141
1$//.//
Do!G t #et co!)used, !ot like:
select sqrt(qt y )ro& ordersC
. . . 'hich &ust retur! o!e value )or each ro' i! orders, operates o! o!e ro'
at a ti &e. A Set )u!cti o! ca! operate o! a S32 o) ro's.
0ote relatio!al al# could!G t )i!d the total dollar sales o) a! a#e!t.
4ots o) di))ere!t ter&i !ol o#y. 9N1630 a!d 80GR3S re)er to set )u!cti o!s,
A0S8 says a##re#ate )u!cti o!s (a##re#ate: to bri!# to#ether a !u&ber o)
obHects i!to a si!#le &easure, 1RAC43 has #roup )u!ctio!s, DB" says col F
u&! )u!cti o!s . <e say set functions . %ere are sta!dard o!es.
Name %rgument type /esult type Description
Cou!t a!y (ca! be W !u&eri c cou!t o) occurre!ces
su& !u&eri c !u&eri c su& o) ar#u&e!ts
av# !u&eri c !u&eri c avera#e o) ar#u&e!ts
&a( char or !u&eri c sa&e as ar# &a(i &u& value
&i! char or !u&eri c sa&e as ar# &i!i &u& value
F$EF
7igure '.!" 2e set functions in -AL @pg. !"(B
1RAC43 has a )e' &ore, stddev a!d varia!ce:
select varia!ce(observati o!s )ro& e(peri &e!tC
select stddev(observati o!s )ro& e(peri &e!tC
0ote &a( a!d &i! retur! char &a( a!d &i!.
select &i!(ci ty )ro& productsC
0ote av#(( has sa&e value as su&((Ncou!t((C probabl y &ore e))icie!t.
0ote that these three 78G%2 012 #ive sa&e values:
select cou!t(W )ro& custo&ersC
select cou!t(ci d )ro& custo&ersC NW !ull values !ot cou!ted WN
NW but cid !ot !ull i! custo&ers WN
select cou!t(ci t y )ro& custo&ersC NW o!l y i) !o !ull city values WN
Surprisi!#M Does!G t i!sist o! di))ere!t city values. But:
select cou!t(disti !ct city )ro& custo&ersC
is ho' 'e #et cou!t o) disti!ct city !a&es. Could also do:
select av#(disti !ct dollars )ro& ordersC
But it 'ould be 53R: ;0;S;A4 i) you reall y 'a!ted this.
8t is not legal to use a! a##re#ate )u!ctio! directl y i! a 'here clause.
3.#., try to list all custo&ers 'ith &a(i &u& discou!t.
select cid, c!a&e )ro& custo&ers c 'here c.disc!t I &a((c.disc!tC
J<<< NO2 LE>%L <<<J
6roble& is that ra!#e variable c is o!l y supposed to ra!#e o!ce over cusF
to&ers, !ot o!ce outside a!d o!ce i!side &a(( .
2here is o!l y o!e ra!#e variable i! this e(pressio! a!d 'e !eed t'o, a!d
!eed to evaluate &a(( )irst. %o' solve thisM (ClassM
select cid, c!a&e )ro& custo&ers 'here disc!t I
F$-F
(select &a((disc!t )ro& custo&ersC
0o' custo&ers i!side subquery ra!#es separatel y )ro& out side. <hy ca!
'e use I above, rather tha! Ia!y or Ial lM Because o!l y o!e value
retur!edQ
/EMEM3E/ 2#1-C M4-2 4-E -43A4E/0 17 -E2 74NC21ON 1N
?#E/ED
3(a&ple *.E.,. >i!d products ordered by at least t'o custo&ers. 6revi F
ously:
select disti!ct c1.pid )ro& orders c1, orders c"
'here c1.pid I c".pid a!d c1.cid U c".cidC
0e' 'ay (&ore e))icie!t, #e!eraliBes to &ore tha! " 'i thout i!creasi!#
!u&ber o) alias Hoi!s:
select pid )ro& products p 'here " UI (select cou!t(disti !ct cid
)ro& orders 'here pid I p.pidC
%a!dli!# 0ull 5alues.
Recall that a !ull value appears as a colu&! value i! a ro' 'he! the value
is either u!k!o'! (disc!t )or !e' custo&er or i!applicable (e&ployee
&a!a#er )or co&pa!y preside!t.
8! passi!#, !ote that there is a proposal to have t'o di))ere!t ki!ds o) !ull
values )or these t'o cases.
8) i!sert a ro' (i!sert state&e!t, to co&e 'i thout speci)yi !# so&e col u&!
values, !ulls 'ill be placed )or those values
;!less colu&! de)i!i ti o! i! create table speci)ies !ot !ull as )or cid i!
Appe!di ( A, p#. E"$ ? 2he! the i!sert state&e!t 'ill !ot 'ork. 3( *.E.E:
i!sert i!to custo&ers (cid, c!a&e, city
values (Gc//EG , G<i!di (G , GDallasG C
2he disc!t value is !ot speci)ied, so 'ill be placed as !ull value. 0ote that it
is 012 usuall y possible 'ith curre!t products to speci)y !ull as value )or
disc!t (1P i! S@4F." sta!dard.
A !ull value has 8761R2A02 i&plicati o!s. 2'o )ollo'i !# are di))ere!t:
select cou!t(W )ro& custo&ersC
F$.F
select cou!t(W )ro& custo&ers 'here (disc!t U - or disc!t TI -C
<hyM Because !ull values )or disc!t 'ill !ot be selected i! seco!d state F
&e!t, eve! thou#h the co!di ti o! see&s to be e(hausti ve.
A !ull value i! a!y co&pariso! e(pressio! evaluates to ;0P01<0, rather
tha! 2R;3 or >A4S3. 8! a Select state&e!t, o!l y ro's )or 'hich the 'here
co!di ti o! is 2R;3 are retrieved. (See p#. 1$* )or reaso! )or ;0P01<0.
2his &ea!s that the !ull value o) a! i!te#er type variable ca!!ot be kept
si&pl y as so&e value patter!, because all patter!s are take! up by real
i!te#er values. 0eed special >4AG byte )or a colu&! to see 'he! it is !ull.
So&e older products did!G t have !ull, represe!ted u!k!o'! !u&eric values
by Bero a!d u!k!o'! char values by GG (!ull stri!#. 1bviousl y that does!G t
have the ri#ht properti es because o) set )u!ctio!s.
0ote a!other i&porta!t propert y: the set )u!cti o!s 8G01R3 !ull values. 8)
'e 'ri te:
select av#(dollars )ro& orders 'here aid I Ga/1G C
or
select su&(dollarsNcou!t(dol l ars )ro& orders 'here aid I Ga/"G C
a!d so&e ro's have ;0P01<0 dollars values, the! the cou!t, su&, a!d
av# )u!cti o!s 'ill all skip over those values. 8) the values 'ere Bero i!stead
o) !ull, clearl y the av# 'ould be lo'er.
F+/F
Class !!.
%o&e'ork due Class 1*, 7o!day, 1ct. "/. Solutio!s o!li!e that eve!i!#.
3(a& 1, <ed!esday, 1ct. "".
Sectio! *.-. Groups o) ro's .
S@4 allo's a select state&e!t to serve a ki!d o) report )u!cti o!. Groups
ro's o) a table based o! a co&&o! values a!d per)or&s a##re#ate
)u!cti o!s o! ro's #rouped. 3.#.
select pid, su&(qt y )ro& orders #roup by pidC
0e' GR1;6 B: clause. 6ri!t out as i) )ollo'i !# lo#ic 'as )ollo'ed:
>1R 3AC% D8S280C2 5A4;3 v 1> pid 80 ordersC
select pid, su&(qt y )ro& orders 'here pid I vC
30D >1RC
See p#. 1". )or table pri!ted out. 0ote, i! Select ca!G t i!clude a!ythi !# i!
tar#etF list that is !ot si!#leF valued )or the #roups created i! the GR1;6 B:
clause.
select pid, aid, su&(qty )ro& orders #roup by pidC WW 8443GA4 WW
2his is because a si!#le pid i! orders &ay be associated 'i th &ul ti pl e aid
values. %o'ever, 'e ca! have &ore )i!el y a##re#ated #roups: 'e ca!
GR1;6 B: aid a!d pid both:
select pid, aid, su&(qty as total )ro& orders #roup by pid, aidC
2his has the e))ect o) the loop:
>1R 3AC% D8S280C2 pair o) values (v, ' equal to (pid, aid i! orders
select pid, aid, su&(qty )ro& orders 'here pid I v a!d aid I 'C
30D >1RC
See table retrieved i! 3(a&pl e *.-.1, p#. 1*/ i! te(t.
0o' a surpriseQ 8) say:
select p.pid, p!a&e, su&(qt y )ro& orders o, products p
'here o.pid I p.pid #roup by p.pidC
F+1F
8t 'o!G t 'orkQ ?#0&D (Class. 3ve! thou#h p!a&e is u!iquel y valued )or
each #roup (u!ique pid value, S@4 does!G t reco#!iBe that.
2his is true eve! thou#h 'e de)i!e i! Create 2able that pid is pri &ary key.
7ust say:
select p.pid, p!a&e, su&(qt y )ro& orders o, products p
'here o.pid I p.pid #roup by p.pid, p!a&eC
0ote that 'e ca! have a <%3R3 clause at the sa&e ti &e as a GR1;6 B::
select pid, su&(qt y )ro& orders 'here aid I Ga/*G
#roup by pidC
1RD3R: (1 2ake Cartesia! productC (" cross out ro's !ot selected i!
<%3R3 clauseC (* Group re&ai!i !# ro's i! accorda!ce 'i th GR1;6 B:
clauseC ($ evaluate e(pressio!s i! tar#et list (a##re#ate )u!ctio! values
depe!d o! #roups.
<ould see co!tri buti o!s )ro& )our ro's o) the table o! p#. 1*/, 'here aid I
a/*, but t'o o) these are both )or product p/* a!d 'ould be co&bi !ed i!
this resul t.
0o'. <hat i) 'a!t to eli&i !ate ro's 'here su&(qty is too s&all i!:
select pid, su&(qt y )ro& orders #roup by pidC
Ca!G t do it by sayi!#:
select pid, su&(qt y )ro& orders
'here su&(qt y TI 1/// #roup by pidC WW 8443GA4 WW
Because !ot allo'ed to use set )u!cti o! i! <%3R3 clause. Also, <here acts
be)ore #roupi !# occurs a!d lo!# be)ore tar#etF list e(pressio!s evaluated.
0eed to i!ve!t a !e' clause to act as <here a)ter #rouped qua!ti ti es have
bee! calculated: called a %avi!# clause:
select pid, su&(qt y )ro& orders
#roup by pid havi!# su&(qt y TI 1/// C
So !o': (+ 3li&i!ate ro's i! tar#etF list that do !ot obey the %A580G clause
requi re&e!ts.
(W 0ote this is a klu#e: 5ery &uch !ot recursive: ca!G t GR1;6 B: the resul t
o) a GR1;6 B: a)ter a %A580G. But &i#ht 'a!t to.
F+"F
0ote that A0S8 S@4F." allo's us to Select >R17 a Subquery, thus ca!
GR1;6 B: a#ai! o! a seco!d pass. But ca!G t do this yet o! a!dy product.
A %A580G restricti o! !eed !ot re)er to a set )u!cti o! actuall y i! the tar#et
list. A %A580G clause is Hust used to restrict a)ter a GR1;6 B:.
3(a&ple *.-.$ (varia!t. Get ids o) all products purchased by &ore tha! t'o
custo&ers.
select pid )ro& orders #roup by pid havi !# cou!t(disti !ct cid T "C
8t certai !l y )eels risky to &e!ti o! cid i! #roup by pid, but cou!t( &akes it
si!#leF val ued. 2his is a! eve! &ore e))icie!t Select tha! *.E.,.
select pid )ro& products p 'here " UI (select cou!t(disti !ct cid
)ro& orders 'here pid I p.pidC
0e' &ethod 'ith %A580G clause retrieves )ro& o!l y o!e table, but old
&ethod retri eves )ro& t'o a!d correlated subquery does a Hoi!.
-ection '.; . 0o' co&plete descripti o! o) Select state&e!t, p#. 1*+.
Re&e&ber: idea is to #ive you co!)ide!ce ? everythi !# has bee! covered.
But you &ust &ake special e))ort !o' to #ai! that co!)ide!ce )or yoursel ).
53R: close to te(t here, probabl y !o !eed )or o'! !otes.
Subquery Ge!eral >or&, see p#. 1*+.
S343C2 J A44]D8S280C2K e(pr JJASK cLaliasK R, e(pr JJASK cLaliasK S
>R17 tablere) R, tabletr) S
J<%3R3 searchLco!di ti o!K
JGR1;6 B: colu&! R, colu&!S K
J%A580G searchLco!di ti o!K
] subquery J;0810 JA44K ] 8023RS3C2 JA44K ] 39C362 JA44KK
JC1RR3S610D80G JB:K (col!a&e R, col!a&e . . .SK subqueryC
Recall tablere) i! >R17 clause 'as de)i!ed i! Sectio! *.,, >i#. *.11, p#. 11E.
(B;2 012 A44 S:02A9 8S 6R3S302 80 A0: 6R1D;C2. 2he &ost basic S@4
)or& (supported by all databases is Hust
2ablere) :I table!a&e JcorrL!a&eK
Square brackets &ea!s do!G t have to have the ite&. <%3R3, GR1;6 B:,
etc, all optio!al.
F+*F
2hus J A44]D8S280C2K: the phrase is optio!al C i) 'e speci)y, 'e &ust choose
e(actl y o!e )or&, A44 (duplicates allo'ed i! tar#etF list or D8S280C2 (!o
duplicates. But si!ce A44 is u!derli !ed, it is the de)aul t, so i) the phrase is
!ot speci)ied, it 'ill be as i) A44 'as speci)ied.
search_condi ti on i! the <%3R3 clause is a co&ple( obHectC &ost o) 'hat
)ollo's #ives details o! this, all the various Subqueries a!d predicates, 'ith
a )e' !e' o!es.
1P, this is a -ub=uery )or&: &ea!s ca! occur i! subquery (part o)
searchLco!di ti o!, also part o) )ull select as )ollo's.
7ull -elect >eneral 7orm
subquery
J1RD3R B: resul tLcol J ASC]D3SCK R, resul tLcol J ASC] D3SCK S K
2he 1RD3R B: clause is !e', a!d allo's us to order the ro's output by a
successio! o) resul tF colu&! values, le)t&ost )irst.
3(plai! dictio!ary order. 0ote that resul tLcol ca! be a !u&ber, re)erri !# to
!u&bered col i! the tar#et list.
8! a ;0810, etc. o) di) )ere!t Subqueri es, 'e do !ot have to assu&e that
correspo!di !# colu&!s have the sa&e quali)ied !a&es throu#h out.
(Althou#h 'e could use colu&! aliases )or all retrieved ro's, sa&e )or all
subqueri es.
2here)ore, the resul tLcol i! this case ca! be o!e o) the colu&! !u& bers 1
throu#h !, 'here ! colu&!s occur i! the resul t. (Breaks rel rule.
0ote that i! the J ASC]D3SCK choice (asce!di!# order, or desce!di !# order,
ASC is the de)aul t.
0o' everythi !# i! a Subquery co&es be)ore 1RD3R B: a!d the order o)
clauses i! a Subquery carries over to the co!ceptual order o) evalu atio!.
Reaso!able, si!ce a! orderi !# o) ro's by colu&! values is clearl y a )i!al
step be)ore display.
See >i#ure *.1+. Co!ceptual order o) evaluati o! o) a Select state&e!t.
o >irst the Cartesia! product o) all tables i! the >R17 clause is )or&ed.
o >ro& this, ro's !ot satis)yi !# the <%3R3 co!di tio! are eli&i !ated.
F+$F
o 2he re&ai!i !# ro's are #rouped i! accorda!ce 'ith the GR1;6 B: clause.
o Groups !ot satis)yi !# the %A580G clause are the! eli &i!ated.
o 2he e(pressio!s o) the Select clause tar#et list are evaluated.
o 8) the key 'ord D8S280C2 is prese!t, duplicate ro's are !o' eli&i !ated.
o 2he ;0810, 8023RS3C2, 39C362 is take! a)ter each subquery is evaluated.
o >i!all y, the set o) all selected ro's is sorted i) a! 1RD3R B: is prese!t.
E:ample '.;. !. 4ist all custo&ers, a#e!ts, a!d the dollar sales )or pairs o)
custo&ers a!d a#e!ts, a!d order the resul t )ro& lar#est to s&allest sales
totals. Retai! o!l y those pairs )or 'hich the dollar a&ou!t is at least .//.//.
select c.c!a&e, c.cid, a.a!a&e, a.aid, su&(o.dollars
)ro& custo&ers c, orders o, a#e!ts a 'here c.cid I o.cid a!d o.aid I a.aid
#roup by c.c!a&e, c.cid, a.a!a&e, a.aid
havi!# su&(o.dollars TI .//.//
order by + descC
E:ample '.!!. ) ()ro& later. Create table called e&ployees (see p#. 1+.
o) te(t.
create table e&ployees (eid char($ !ot !ull, e!a&e varchar(1,,
&#ri d char($C
Dra' tree. <a!t to select everyo!e belo' so&e !ode, e.#. e&ployee 'ith
eid I Ge//1G , i! tree.
select e.eid )ro& e&ployees e 'here e.&#ri d chai!
#oes up to Ge//1G C
Ca!G t, ca! o!l y retri eve e&ployees o!e level do'!:
select e.eid )ro& e&ployees 'here e.&#ri d I Ge//1G C
or t'o levels,
select e.eid )ro& e&ployees 'here e.&#ri d i!
(select ).eid )ro& e&ployees ) 'here ).&#ri d I Ge//1G C (typo
or . . .. Ca!G t leave !u&ber o) levels )le(ible. 8! pro#ra&, o) course, ca!
'alk the tree.
2his capabili ty is specGd i! S@4.. (called Recursive @ueries. ( Ne$
%ave this capabili ty i! 1racle !o'Q But !ot 'i th !e' sta!dard sy!ta(F Fit
predates that sta!dard.
F++F
Create e&ployees table as o! pa#e 1+.. 0o' to per)or& a depth )irst
search o) the reports to tree starti !# 'ith the 6reside!t, Aacqueli!e:
select e!a&e, eid, &#ri d )ro& e&ployees
start 'ith eid I Ge//1G
co!!ect by prior eid I &#ri dC
Added to !e(t ho&e'ork: 2ry this o! 1racle.
0o' e(pressio!s, predicates a!d searchLco!di ti o!. Start 'i th e(pres sio!s:
!u&eric value e(pressio!s, stri!# value e(pressio!, dateti &e e( pressio!,
i!terval value e(pressio!, a!d co!di ti o!al e(pressio!.
e(pr I !u&e(pr ] strve(pr ] date(pr ] i!tve(pr ] co!de(pr
!u&e(pr, ari th&eti c stu)), >i#ure *.1,a, p# 1*E. De)i!ed very &uch like a
pro#ra&&i !# la!#ua#e e(pressio!, e(cept allo' colu&!!a&e (like variable
a!d setL)u!cti o!(!u&e(pr.
A! !u&e(pr ca! be a co!sta!t, or colu&!a&e, or quali)ier.col u&!a&e or
(recursi vel y: !u&e(pr ari thLop !u&e(pr, (!u&e(pr, )u!cti o!(!u&e(pr,
setL)u!cti o!(!u&e(pr
Si&ilarl y )or strve(pr, >i#. *.1*b. Co!cate!ate t'o stri!#s 'i th a ]] (so&e
products use \ like stri!# co!cate!ati o! i! Aava a!d Basic.
8 a& #oi!# to skip other types )or !o'. 0ote data types )or colu&!s de)i!ed
i! Appe!di ( A.*.
Arith )u!cti o!s i! &ost products, >i# *.1E. abs(!, &od(!,b, sqrt(!C 012
sta!dardiBed i! S@4F...
Char )u!cti o!s AR3 sta!dardiBed, but !ot all products yet )ollo' the
sta!dard, see >i#ure *.1-:
C%ARL430G2%(str
S;BS2R80G(strval >R17 start J>1R le!#thK
2R87(R43AD80G]2RA8480G] B12%S char >R17 strval
61S82810(str1 80 str"
;663R(strval , 41<3R(strval
0ote that co&pa!ies so&eti &es have u!usual capi taliBati o!: is it 2ip2op or
2iptopM 8t is co&&o! to store o!l y uppercase, retri eve i! that )or&.
F+,F
8) so&eti &es caps are i&porta!t, store colu&! c1 'ith ;pperNlo'er, re trieve
aski!# )or upper(c1 I G286216G (but this ca! be i!e))icie!t si!ce ca!G t use
i!de( easily ? could also create seco!d ;ppercase colu&!.
0o', 6redicates: 3valuate to 2R;3, >A4S3, or ;0P01<0 (!ot yet i! !otes
co&pariso! predicate: e(pr1 q e(pr" ] e(pr1 q (Subquery
bet'ee! predicate: c.disc!t bet'ee! 1/ a!d 1"
i! predicate: e(pr J!otK i! (subquery
@ua!ti )i ed predicate: e(pr qJall]a!y]so&eK (subquery
e(ists predicateC e(ists(subquery
is !ull predicate: colu&! is J!otK !ull
like predicate colu&!a&e J!otK like Gpatter!G
Ca! 'e have &ore tha! o!e predicate i!vol vi !# a subquery. ( !ot i!
(subquery a!d y Uso&e (subquery. A!s'er: 0o proble&.
F+EF
Class !".
%o&e'ork * due !e(t ti &e (o!e 'eek. 3(a& o! Class 1* (7o!day, 1/N"1.
Retur!ed i! o!e 'eek. %a!d out 6ractice 3(a&.
<e are #oi!# throu#h a list o) 6redicates allo'ed )or S@4. Subqueries !e(t.
Read these care)ull y, 'ill ask 3(a& @uestio!s about this.
Co&pariso! predicate : e.#., e(pr1 U (e(pr" ] subquery
Subquery &ust retur! at &ost 103 value (&aybe V3R1, or &ust use qua! F
ti)ied predicate. 8) e&pt y set, resul t o) co&pariso! is ;, ;0P01<0
@uestio!. 8! co&pariso! predicate is it !ecessary to al'ays place
co&pariso! predicate )irst a!d predicate i!vol vi !# subquery lastM :es.
Ca! 'e have &ore tha! o!e predicate i!vol vi !# a subqueryM 8 thou#ht
there 'as a restricti o! )or so&e ti &e. ( !ot i! (subquery a!d y Uso&e
(subquery. A!s'er: 0o proble&.
0ote that )or !ot equal, 'e co&&o!l y use (UT ? this is the &ost sta!dard
? also ca! be i!dicated by (QI or ( ^I o! &a!y database syste&s.
2R;2% 5A4;3S: 2, >, a!d ;
A ro' is ruled out i! a <%3R3 clause i) the searchLco!di ti o! is !ot 2, i.e., i) it
is > or ;. %o'ever, ; is !ot equival e!t to >.
4ook at A0D, 1R, 012 operator de)i!i ti o!s o! p# 1$$, >i#. *."1.
A0D 2 > ; 1R 2 > ; 012
2 2 > ; 2 2 2 2 2 >
> > > > > 2 > ; > 2
; ; > ; ; 2 ; ; ; ;
; Hust acts like thereGs doubt, &i#ht be 2 or >, a!d re&ai !s ; i! resul t i)
doubt re&ai !s. But o) course > A0D ; is !ot i! doubt: it resul ts i! >, si!ce
this is the case 'hether ; is 2 or ; is >.
But !o' 'hy do 'e !eed ;M <hy ca!G t 'e #et alo!# 'ith 2 a!d >M Co!sider
the query to retri eve orders &ade by custo&ers 'hose city co&e a)ter G7G i!
the alphabet.
select W )ro& orders o 'here G7G U (select city )ro& custo&ers c
'here c.cid I o.cidC
F+-F
2his 'ould see& to have the sa&e resul t (by our rules o) 'hat !ot &ea!s
as:
select W )ro& orders o 'here !ot( G7G TI (select city )ro& custo&ers c
'here c.cid I o.cidC
But look at the )irst query above. <hat i) 'e have a! orders ro' i! the
outer select so 'e are retri evi !# a custo&er city 'i th c.cid I o.cid 'here the
city is !ot )illed i! (is !ullM <hat do 'e 'a!t to do i! the queryM Retrieve
the order or !otM (ClassM
0o, because 'e o!ly 'a!t to retrieve the order i) the custo&er city !a&e
1B3:S so&e propert y.
1P, but does that &ea! that the ro' )or orders i! the )irst query above
does!G t #et retri eved because G7G U (select city )ro& custo&ers c 'here
c.cid I o.cid is >A4S3M
But the! is!G t it also )alse that G7G TI (select city )ro& custo&ers c 'here
c.cid I o.cidM 2here is !o city, so the propert y is !ot 1B3:3D.
But i) 'e assu&e > i! the case 'ith OG7G TI. . .O, the! i! the seco!d query
(!ot(G 7G TI. . ., !ot(> is 2, a!d 'e D1 retri eve the orders ro'.
So&ethi !# 'ro!# here. A!s'er is, as 'eG ve said, that G7G TI (subquery,
'he! the subquery is e&pty, is ;, ;0P01<0, !ot >. A!d !ot(; is ;. Do!G t
retrieve orders ro' u!less #et 2, so ; is a &ore sticky !o! 2 value tha! >.
2he Bet'ee! 6redicate .
e(pr1 J!otK bet'ee! e(pr" a!d e(pr *
Co!ceptual l y equival e!t to ('i thout !ot:
e(pr1 TI e(pr" a!d e(pr1 UI e(pr*
0o #ood reaso! )or e(isti !#C or#i!all y Hust &ore e))icie!t to evaluate (did!G t
trust query opti &iBer to !otice the seco!d 'as a ra!#e bet'ee!.
@ua!ti )i ed 6redicate .
e(pr qJso&e ] all ] a!yK (subquery
See! be)ore. 1!ly tricky part is that e(pr: qall (subquery is 2R;3 i)
subquery is e&pt y, a!d e(pr qso&e (subquery (or qa!y (subqueryis
>A4S3 i) subquery is e&pty.
F+.F
8dea is that qall is 2R;3 i) there is !o C1;023RF39A7643, but )or _a!y to be
true, !eed at least o!e 39A7643.
3(. *...*. Retrieve &a(i &u& discou!t o) all custo&ers.
select disc!t )ro& custo&ers c 'here disc!t TIal l
(select disc!t )ro& custo&ers d 'here d.cid UT c.cidC
0ever &i!d i) youG d do it that 'ay. 8tGs valid i! &ost cases. But i) there
'ere o!l y a si!#le custo&er ro'M 3&pty set. :es, still 1P. But o!l y i) TIal l
is true )or e&pty set i! subquery.
Skip 8! predicate (ide!ti cal to Ia!y, 3(ists predicate.
8s !ull 6redicate .
colu&!a&e is J!otK !ull
012 valid to say: colu&!a&e I !ull or UT !ull. S@4 'o!G t #et it.
4ike predicate .
colu&!a&e J!otK like 'pattern'
Build up patter!s usi!# !or&al characters a!d 'ildcards. 4ike ;089 )ile
'ildcards: ls W.c (W sta!ds )or a!y stri!#
;!derscore (L a!y si!#le character
6erce!t ([ Bero or &ore characters o) a!y )or& (;089 W
6lus (\ is a! Oescape characterO
All other chars represe!t the&sel ves
select W )ro& custo&ers c 'here city like G0L'[kG C
G0e' :orkG a!d G0e'arkG are 1P, but !ot G0o&eG or G0ovo' SibirskG .
8! 1racle, i) use [ or L t'ice i! a ro', it &ea!s R3A44: G[G or OLG. 2hus,
select W )ro& custo&ers 'here city like G0e'L L[GC
<ill retrieve 0e'LRochelle, or 0e'LCaledo!ia, but !ot 0e' :ork.
3scape character i! 80GR3S (a!d ;089 is backslash (ZC to say 'e R3A44:
73A0 [, 'e 'ri te . . .Z[. . . 8! DB", G \[G &ea!s R3A44: the character [.
2o type R3A44: \ , !eed to type G \\G .
F,/F
Sectio! *.1/ .
8!sert, ;pdate, a!d Delete state&e!ts per)or& data &odi )icati o!s to
e(isti !# tables. Called as a #roup the Oupdate state&e!tsO. 0eed to tell
di))ere!ce bet'ee! a! Oupdata state&e!t O a!d the ;pdate state&e!t.
0eed update privile#e o! a table to update it, di))ere!t tha! read privile#e.
:ou al'ays have all privile#es i) you created the table.
Sectio! *.1/ 2he 8!sert, ;pdate a!d Delete State&e!ts.
i!sert i!to table!a&e J(colu&! R, colu&!`S K
R val ues (e(pr ] !ull Re(pr ] !ull`SK ] subqueryS C
1!e o) the t'o )or&s &ust be used, values ` or subquery. 0ote that !ull
itsel) does !ot quali)y as a! e(pressio!, so it has to be e(plici tl y allo'ed as
a! alter!ati ve here. Colu&!s !a&ed a!d e(pressio!s &ust &atch i! !u&ber
a!d type.
3(a&ple *.1/.1.
i!sert i!to orders (ord!o, &o!th, cid, aid, pid
values(G 11/EG , Gau#G , Gc//,G , Ga/$G , Gp/1G C
0otice, !o qty or dollars, so o! this !e' ro' they are !ull.
But i) 'e are i!serti !# A44 the colu&!s i! a table, ca! leave out colu&! list
(!ote itGs optio!alM
See 3(a&ple *.1/." )or other )or&. 8 ca! create !e' table s'custs, like
custo&ers, the! 'ri te:
i!sert i!to s'custs select W )ro& custo&ers 'here
city i! (GDallasG , GAusti!G C
8 ca! i!sert a 412 at 10C3, build up arbi trari l y )ro& e(isti !# tables usi!#
select. 2his is a! i&porta!t capabili ty Q
2he ;pdate state&e!t . Basic S@4 )or& is this.
update table!a&e JcorrL!a&eK
set col!a&e I Re(pr ] !ull ] (subquery S R, Rcol u&! I e(pr ]!ull ]
(subquery S`S
J'here searchLco!di ti o!KC
F,1F
2he searchLco!di ti o! i! the <here clause deter&i !es ro's i! the updated
table to cha!#e. 2he e(pressio!s used ca! re)ere!ce o!ly colu&! values o!
the speci)ic ro' o) the table curre!tl y bei!# updated.
update a#e!ts set perce!t I 1.1Wperce!t 'here city I G0e' :orkGC
<ith the (subquery optio! 'e ca! re)ere!ce values )ro& other ro's, as 'e
did 'ith a subquery i! i!sert. %o'ever here it &ust be e!closed i!
pare!theses.
update s'custs set disc!t I (select disc!t )ro& custo&ers c
'here c.cid I s'custs.cidC
Delete state&e!t .
delete )ro& table!a&e
J'here searchLco!di ti o!KC
3.#., >ire all a#e!ts i! 0e' :ork.
delete )ro& a#e!ts 'here city I G0e' :orkGC
1!ce a#ai! S@4F." allo's search co!di ti o! to re)ere!ce other tables.
3(. Delete all a#e!ts 'i th total orders less tha! Y,//.//
delete )ro& a#e!ts 'here aid i! (select aid )ro& orders #roup by
aid havi !# su&(dollars U ,//C
Capter ( ObEectK /elational Databases
1ntroduction
8! the se!se that 'e 'ill use it, a! obHect holds to#ether a bu!dle o) related
i!)or&atio!. A! e&ployee has a !a&e, id, address, etc., so 'e ca! have a!
e&ployee obHect )or each e&ployee. A docu&e!t has a title, author, le!#th, a!d
date, so 'e ca! have a set o) docu&e!t obHects i! a library obHect.
All the e&ployee obHects are o) a de)i!ed obEect type . Associated
'i th a! obHect type are the data attri butes it has a!d the )u!ctio!s
(&ethods de)i!ed )or it.
%ere 'e use OobHectO i! a loose se!se, $itout encapsulation , eve!
thou#h so&e obHectF orie!ted e(perts 'ould say that e!capsulati o! is
so esse!tial to obHects that 'e are &isF usi!# the 'ord obHect.
F,"F
3!capsulati o! is also k!o'! as dataF hidi!#, a!d is a so)t'are
tech!olo#y that builds 'alls arou!d a co&bi!ati o! o) code a!d data
a!d o!l y allo's i!teracti o! 'ith other parts o) the code via &ethods or
&essa#es. 8! Aava, a class 'ith o!ly private data &e&bers is
e!capsulated.
2he obHect types 'e 'ill be usi!# are like Aava classes 'i th all public
data &e&bers, or structs i! CC the data i!side ca! be accessed easil y.
2he obHect ca! be thou#ht to organize the data 'i thout policing it.
Relatio!al databases have al'ays had the outlook that all data is
accessible (e(cept )or secret data so that you do!G t have to k!o'
ahead o) ti &e ho' you &i#ht use it. 2he data is stro!#l y or#a!iBed
i!to !eat tables o) ro's a!d colu&!s. 2he obHect types #ive us a !e'
'ay to or#a!iBe data ite&s.
But it does break RR1 by allo'i !# co&bi !ati o! data ite&s. 2he
collectio! types break RR1 a!other 'ay by allo'i!# 'hole sets o) data
ite&s as colu&! values. 4ook at >i#ure $.1. Also read Chapter 1.
<e 'ill cover:
Sectio! $.".1F F1RAC43 obHect types.
Sectio! $.*.1F F1RAC43 collectio!s
<e do!G t have ti &e to cover ho' to 'ri te obHect &ethods (Sectio!
$.$.1 i! 64NS@4, but e!coura#e you to read it, a!d also read the last
sectio! o! packa#ed so)t'are.
4ook at 3(a&ple $.".1. 4ike a C struct declarati o! or Aava class
declarati o!. 4ike these, it provi des a ne$ type, a! obEect type, out
o) a co&bi !ati o! o) other types, called &e&bers i! CNAava, attri but es
i! 1racle obHect types.
2his !e' obHect type ca! be used 'herever ordi!ary S@4 data types
ca! be used, )or e(., as a colu&! type.
3(. $."." create table teachers ... 0ote that teachers is a
!or&al Orelati o!al tableO that has a colu&! type that is a! obHect type.
A)ter this, have a! e&pt y table.
0e(t, i!sert so&e ro's. 0o' )i!all y have OobHectsO, !ot Hust obHect
types. 1bHects are i!sta!ces o) obHect types, that is, real data held
to#ether as prescribed by the type. %ere the !a&eLt obHects are held
as colu&! values o) the t!a&e colu&!.
select * from teachers;
F,*F
2his sho's all the colu&! values, i!cludi!# the t!a&e colu&! o)
obHect type !a&eLt:
tid t!a&e roo&
FFFF FFFFFFF FFFFFF
1"*$ !a&eLt(G3i!stei !G , GAlbertG , G3G 1"/
1"*+ !a&eF t(... 1$$
...
0ote the display o) the !a&eLt obHects. Si!ce the !a&eLt obHects are
colu&! values, they are called column obEects .
select t.tname.fname from teachers t;
2his pri!ts out Hust the )!a&e. 0ote the dotted e(pressio! that di#s
do'! )ro& the table alias all the 'ay to the )!a&e.
0123: 2he table alias is !eeded here a!d a!y'here &ore tha! o!e dot
is used, i! 1racle.
select room from teachers
where t.tname.lname like '%Neil';
3(a&ple $.".*F Fhere 'e have a! obHect type i! a! obHect type.
2able teachers 'as still a relatio!al table, but table people is a!
obEect tabl e , a !e' idea. 3ach ro' o) people is o) type perso!Lt, a!d
thus is called a ro$ obEect.
<e see that ro's ('i th thei r colu&!s a!d obHect types ('i th thei r
attri butes both provi de 'ays o) #roupi !# several typed values
to#ether.
2able people has ro' obHects o) type perso!Lt, a!d each o) these has
attri butes ss!o, p!a&e, a!d a#e. 2hese attri bute !a&es &ap
!aturall y to colu&! !a&es. So 'e ca! say table people has colu&!s
ss!o, p!a&e, a!d a#e derived )ro& the ro' obHect attri butes.
1bHect table
FF each )ull ro' is o) a! obHect type
FF each ro' is a ro' obHect 'ith certai ! attri butes
FF the attri butes o) the ro' obHect provi de e))ecti ve colu&! !a&es
FF the table provi des a !atural ho&e )or obHects o) that obHect type

Relatio!al table
F,$F
FF each )ull ro' has o!e or &ore colu&!s each o) so&e data type, but
!o type describes the 'hole ro'.
FF so&e colu&!s &ay have obHect types, provi di !# colu&! obHects.
See >i#ure $." )or a picture o) this.
2here is a Oduali tyO here:
a! obHect table is, at the sa&e ti &e:
FFa table o) ro' obHects
FFa table o) ro's a!d colu&!s 'here the colu&! !a&es are the topF
level attri bute !a&es o) the ro' obHect.
4ook at 3(a&ples a)ter >i#ure $.".
2he 'hole ro' obHect ca! be evaluated by the e(pressio! Oval ue(pO
'here p is the table alias )or the obHect table.
select value(p vs. select W: select ro' obHect vs allFtheF colu&!sF Fsee
>i#ures $.* a!d $.$. 0ote ho' the 'hole obHect is represe!ted by
!a&eLt(G Del a!eyG , G6atrickG , G9G.
2his is also the )or& o) the obHect co!structor, sho'! i! actio! i!
3(a&ple $.".+ to create a !a&e to &atch i! the query.
Dot !otati o!
Aust as i! C a!d Aava, 'e use dots to drill do'! i!to structured types.
>or e(a&pl e p.p!a&e.)!a&e speci)ies the )!a&e attri bute o) the
p!a&e !a&eLt obHect 'ithi ! the perso!Lt ro' obHect re)ere!ced by
the table alias p. 4ook at 3(a&ple $."., to see this i! use i! a query.
1racle is very picky about the use o) the table alias i! dotted
e(pressio!s. 2he easy rule is si&pl y to al'ays use a table alias 'i th
dotted paths. 8! )act, 1racle does support si&ple cases 'ithout table
aliases, to be co!siste!t 'ith the relatio!al case. 4ook at the
e(a&pl es o! p. 1-*.
2o i!sert i!to a! obHect table, 'e ca! vie' it as havi!# various
colu&!s a!d provi di !# values )or the& Hust as i! the relatio!al case,
as sho'! i! 3(a&pl e $.".E.
8) 'e vie' the sa&e obHect table i! ter&s o) ro' obHects, it 'ould
see& !atural to be able to i!sert a 'hole perso!Lt obHect i! all at
o!ce. %o'ever, this does!G t )it the 8!sert state&e!t sy!ta(, 'hich
allo's either a values clause over the colu&!s or a subquery, a!d
F,+F
1racle has !ot stretched the sy!ta( out to cover this possibili ty (as o)
versio! -.1.+, a!y'ay.
%o'ever, 1racle has )ou!d a 'ay to do a )ullF obHect ;pdate o) a ro'
by letti !# the table alias sy&boliBe the 'hole ro', as sho'! o! p. 1-$.
update scie!tists s set s I !a&eLt(` 'here `C
As you ca! see )ro& Appe!di ( C, p. EE*, the use o) the table alias i!
the ;pdate state&e!t is a! e(te!sio! o) the sta!dard )or&.
ObEect /eferences
2he ro' obHects o) obHect tables are so&e'hat special i! that they
have u!ique ide!ti )i ers (this is related to the )act that they are )ullF
)led#ed database ro's, 'hereas the colu&! obHects are k!o'! o!l y
by thei r values. <e ca! utiliBe this topF level ide!ti )icati o! i!)or&ati o!
by de)i!i!# R3>s that, i! e))ect, poi!t directl y to a ro' obHect.
2he R3> )eature provi des !e' buil tF i! datat ypes, o) type Ore)
obHectLtypeO, so 'e ca! have table colu&!s that poi!t to ro' obHects
i! a! appropri ate obHect table.
>or e(a&pl e i! a !e' versio! o) the CA6 database, 'e ca! have a Ore)
productLtO colu&! i! orders that poi!ts to a 'hole ro' obHect
represe!ti !# a product.
4ook at 3(a&ple $.".., 'here the details o) this !e' versio! o) CA6
are laid out. 3ach order obHect has R3>s to its custo&er, a#e!t, a!d
product obHects. <e still have the old relati o!al ide!ti )iers as 'ell, to
help 'i th loadi!# the data. But 'e 'ill utiliBe the R3>s i! queries
'herever possible.
0ote the scope clauses i! the create table )or orders. 2hese are !ot
requi red, but allo' shorter (thus )aster R3>s 'he! they all (o) o!e
type poi!t i!to o!e table.
<e 'ill #loss over the issue o) loadi!# the table !o' a!d Hu&p i!to
doi!# queries o! a )ull y loaded table. 2he dotted e(pressio!s (or
OpathsO ca! !o' )ollo' a R3> across to a!other table. >or e(a&pl e
o.ordcust.ci t y
('here o is a table alias )or orders accesses the city attri bute o) the
poi!tedF to custo&er obHect via the R3> o.ordcust housed i! the order
obHect.
F,,F
8! relatio!al CA6, 'e !eeded a Hoi! o! cid to &ake this associati o!F F
here it is Hust a dot a'ay. >urther, 'e k!o' that it is based o! a
u!ique ide!ti )i er that is a Olocator.O 8t has i!)or&ati o! o! ho' to )i!d
the ro', !ot Hust a u!ique patter! o) bits to search )or.
A!y relati o!al query that does Hoi!s o! orders 'ith custo&ers o! cid,
'i th a#e!ts o! aid, a!dNor 'ith products 'ith pid is si&pli)ied by usi!#
R3>s here.
>or e(a&pl e, to list all c!a&e, p!a&e pairs 'here the custo&er
ordered the product, 'e ca! Hust thi!k o) the c!a&e a!d p!a&e
Oha!#i !# o))O the order obHect:
select disti!ct o.ordcust.c!a&e, o.ordprod.p!a&e )ro& orders oC
2'o Hoi!s are saved here over the relatio!al query.
Ad&i ttedl y, o!l y the easiest Hoi!s are saved. 7a!y harder queries are
!ot si&pli )i ed. 8! 3(a&ple $.".1*, the !otorious divisio! query is
re'ri tte! 'i th R3>s. 8t does &ake a little clearer ho' the ( ro'
brid#es )ro& c to a, but the hard stu)) is still all there.
<e have to be a'are o) the possibili ty o) Oda!#li !#O R3>s. 8) a! order
has a R3> poi!ti !# to a certai ! custo&er obHect, a!d that ro' is
deleted )ro& the custo&ers table, the! the R3> i! the order obHect is
le)t Oda!#li !#O, poi!ti !# !o'here, but !ot 0;44.
1racle has added a !e' predicate Ois da!#li!#O so that these R3>s ca!
be )ou!d a!d 'eeded out. 4ook at 3(a&pl e $.".1$.
R3> Depe!de!cies
<he! you e(ecute a Create 2ype that de)i!es R3> attri butes, like
orderLt, the R3>Ftar#et types (like custo&erLt, etc. !eed to be
already de)i!ed. <e say that type orderLt has a OR3> type
depe!de!cyO o! custo&erLt, a#e!tLt, a!d productLt. Si&ilarl y, 'e
!eed to drop the& i! the opposi te order, orderLt )irst, the! the others
(i! a!y order.
2his 'ould see& to preclude circular R3> type depe!de!ci es, but
there is a loophole i! the syste& to allo' the&, si!ce they are so
use)ul i! certai ! circu&sta!ces.
>or e(a&pl e, a!d e&ployeeLt type could have a R3> depart &e!tLt
attri bute )or the e&ployeeG s depart &e!t, 'hile the depart &e!tLt type
could have a R3> e&ployeeLt attri bute )or the depart &e!t G s &a!a#er.
F,EF
See p#. 1./ )or the trick to set this upF F'e ca! hal)F 'ay de)i!e
e&ployeeLt, the! )ull y de)i!e depart &e!tLt, a!d the! )i!ish de)i!i!#
e&ployeeLt. 2o drop these types, a special >1RC3 key'ord !eeds to
be used.
4oadi!# 2ables 'i th R3>s
Si!ce 'e are o!ly usi!# R3>s as e(tra colu&!s i! the CA6 e(a&pl e, 'e
ca! do the old relati o!al load a!d the! use a! ;pdate state&e!t to
set all the R3>s, as sho'! i! 3(a&ple $.".1E. <e see it is usi!#
&atchi !# o! the stillF e(iste!t relati o!al ids.
8! #e!eral 'e !eed a 'ay to speci)y the tar#et o) a R3> u!iquel y by
o!e or &ore colu&!s, 'hich o) course &ea!s speci)yi !# a key i! the
tar#et obHect table. A!d this key value &ust be obtai !able )ro& the
source obHect, to do the &atchF up. 2hus the si&plest approach is to
use R3> colu&!s i! parallel 'i th ordi!ary relatio!al keys
Collectio! 2ypes i! 1racle
<e have talked about the idea o) &ul tiF valued attri butes be)ore i!
Chapter ", a!d ho' they break Relatio!al Rule 1. See p#. *".
Relatio!al tech!i ques #et rid o) &ul tiF valued attri butes by setti !# up
addi ti o!al tables. 2he! 'e are co!sta!tl y Hoi!i!# the& back to#ether
to #et our 'ork do!e. 0o', i!stead, 'eGll allo' &ul ti pl e values i!side
o!e colu&! value by usi!# collectio! types.
4ook at >i#ure $.11 to see collectio!s o) depe!de!ts )or each
e&ployee.
1racle has O!ested tablesO, &ea!i!# a 'hole table (o) depe!de!ts, )or
e(a&pl e i! a colu&! value o) a!other table. 2o &ake this 'ork, !e'
datat ypes are &ade available, Otable typesO such as Otable o)
perso!LtO or Otable o) i!tO that ca! be used as colu&! datat ypes.
As 'ith ordi!ary tables, each !ested table is a set o) ro's, 'ith !o
order i! the ro's. 0ested tables ca! be obHect tables or tables o) a
si!#le colu&! o) a buil tF i! type such as i!t.
8) order !eeds to be &ai!tai !ed i! a collectio! i!side a colu&! value,
1racle provi des 5ARRA:s. <eGll look i!to the& a)ter !ested tables.
1racle (versio! -.1.+ allo's o!ly o!e level o) !esti !# )or !ested
tables. 3&ployees ca! have a !ested table o) depe!de!tLt obHects,
F,-F
but these depe!de!ts ca!G t have a !ested table o) hobbies. 2he
e&ployee ca! have a !ested table o) hobbies as 'ell as depe!de!ts,
si!ce that still cou!ts as si!#leF level !esti !#.
2o create a table that co!tai !s a !ested table 'i thi ! it, 'e are )irst
e(pected to de)i!e a table type a!d the! co&e up 'i th t'o
table!a&es. 1!e table!a&e is )or the 'hole table (the Opare!tO table
a!d the other is a helper (or OchildO table to hold the ro's o) all the
!ested tables o) o!e colu&!. 8) 'e have t'o !ested table colu&!s, 'e
have to co&e up 'ith three table!a&es.
See 3(a&ple $.*.1 )or the e&ployees a!d depe!de!ts e(a&pl e.
2he sy!ta( )or the Create 2able state&e!t 'ith !ested tables is like
the previous Create 2able state&e!ts 'ith a! addi tio!al clause or
clauses o) the )or& O!ested table col!a&e store as tab!a&eO. %ere
the col!a&e &atches the colu&!!a&e o) a colu&! o) table type.
Althou#h 'e ca! see the child tables as database obHects (i! say
Oselect tableL!a&e )ro& userLtablesCOF Fsee p#. $$-, 'e ca!G t use
the& i! queries directl y. <e al'ays !eed to access the data throu#h
the pare!t table.
3ach !ested table is itsel) a colu&! value o) so&e table type. 8t is
easy to display 'hole !ested tables )ro& S@4, as i! 3(a&ples $.*."
a!d $.*.*:
select depe!de!ts )ro& e&ployees 'here eid I 1/1C FF o!e
!ested table
select depe!de!ts )ro& e&ployeesC FF o!e !ested table per ro'
>i#ure $.1$ sho's the output )or&at )or these retrieved !ested tables.
>ollo'i !# the obHect type display )or&, 'e see the type !a&e a!d
the! a pare!thesiBed list o) the co&po!e!t parts, i! this case the ro'
obHects o) the !ested table. As 'ith obHect types, 'e ca! use the
sa&e )or& )or co!structi !# !ested table obHects.
0o' the challe!#i !# part is retrievi !# Hust the desired i!)or&ati o!
)ro& !ested tables rather tha! the 'hole tables. <e ca! use
2AB43(e.depe!de!ts to co!vert the !ested table colu&! value (a
si!#le database ite& value e.depe!de!ts i!to a queryF co&pati bl e
table o) its co!te!ts, but !ote that there is one of these for each row
of the employees table.
<e ca! use 2AB43(e.depe!de!ts i! a subquery, such as
select W )ro& table(e.depe!de!ts FF subquery usi!# 2AB43
F,.F
but this ca!!ot sta!d as a topF level select because the table alias e is
!ot de)i!ed. <e ca! #ive e a de)i!i ti o! by e&beddi !# this subquery i!
a! outer query o! e&ployess:
select eid )ro& e&ployees e
'here e(ists (select W )ro& table(e.depe!de!tsC
2his query )i!ds all e&ployees 'i th depe!de!ts. 7a!y queries o!
!ested tables have this )or&at: o!e select ru!s over the pare!t table
a!d the other over o!e or &ore child tables. See 3(a&pl es $.*.$ a!d
$.*.+.
<e do have to be care)ul about usi!# 2AB43( i! the ri#ht spots.
3(a&ple $.*., sho's a query that produces di))ere!t resul ts based o!
'hether or !ot 2AB43( is applied to e.depe!de!ts. <ith it, 'e are
accessi!# the ro's o) the table, 'hereas 'ithout it, 'e are accessi!#
the 'hole table obHect, a si!#le thi!# 'i th cou!t o) 1.
8! 3(a&pl e $.*.E, 'e query o!e depe!de!ts table:
select d.ss!o )ro& table(select e.depe!de!ts )ro& e&ployees 'here
e.eid I 1/1 dC
<hat happe!s i) 'e re&ove the O'here e.eid I 1/1O that speci)ied a
si!#le !ested tableM 2he! 2AB43( re)uses to 'ork 'i th the &ul ti pl e
!ested tables tryi !# to )lo' i!to it. 2AB43( requires a si!#le !ested
table as a! ar#u&e!t.
1P, but o! a &ore lo#ical level, ho' ca! 'e re&ove this speci)icati o!
o) eid I 1/1 a!d retrieve all the ss!oGs )or all the depe!de!tsM 2his is
the Otabl e o) tablesO data retrieval case. <e are aski!# )or data ite&s
)ro& all the ro's o) all the !ested tables i! all the ro's o) the pare!t
table.
8t tur!s out there are t'o 'ays to do this i! 1racle. 2he )irst a!d o)te!
easier &ethod is by usi!# a table product bet'ee! the pare!t table
a!d the child tables, )or e(a&pl e:
select e.eid, d.ss!o )ro& e&ployees e, table(e.depe!de!ts dC
2his table product #e!erates a resul t ro' )or each depe!de!t. 2he
resul t ro' co!tai !s o!e child table ele&e!t value (here a depe!de!tLt
obHect plus all the colu&! values o) its ro' i! the pare!t table, eve!
the colu&! value co!tai !i !# the )ull !ested table obHect.
FE/F
2hus a! e&ployee 'ith * depe!de!ts 'ould #et * ro's i! this resul t
set, 'ith triplicated e&ployee colu&! values. But a! e&ployee 'i th
!o depe!de!ts does!G t sho' up at all Q
2o i!clude the depe!de!tF less e&ployees, 'ith !ull o) course 'here
the depe!de!tLt obHect should sho' up, 'e put a (\ i! the ri#ht spot,
as )ollo's:
select e.eid, d.ss!o )ro& e&ployees e, table(e.depe!de!ts (\
dC
2hus this table product has !eatl y )illed out a bi# table 'i th all (or
!earl y all, i) you leave out the (\ o) the i!)or&ati o! o) all the tables.
2his bi# table ca! be queried like a!y other.
>or e(a&pl e, to )i!d all the possibili ti es i! #roups o) depe!de!ts o)
e(actl y the sa&e a#e, 'here 'e are o!l y i!terested i! #roups o) * or
&ore ()or pla!!i!# play #roups, say, 'e could put:
select d.a#e, cou!t(W )ro& e&ployees e, table(e.depe!de!ts d
#roup by d.a#e havi!# cou!t(W TI * order by d.a#eC
But e(actl y 'ho are these depe!de!tsM <e have thei r a#es )ro& this
query, a!d that ca! be used to )i!d out thei r ide!ti ti es:
select d1.ss!o, d1.a#e )ro& e&ployees e1, table(e1.depe!de!ts
d1
'here d1.a#e i! (select d.a#e )ro& e&ployees e,
table(e.depe!de!ts d
#roup by d.a#e havi!# cou!t(W TI *
order by d1.a#eC
0ote that table products tur! a Otable o) tablesO i!to a! ordi!ary table,
so it &aps us back i!to the !or&al relati o!al 'orld. 2hatG s #reat )or
lots o) cases, but so&eti &es 'e 'ould like to &ai!tai ! the hierarchical
relatio!shi p o) pare!t to child all the 'ay throu#h.
2he 1racle C;RS1R )acili ty allo's hierarchical prese!tati o! o) query
resul ts (eve! )ro& purel y relati o!al tables. ;!)ortu!atel y, its pri!tout
)ro& Sql6lus is prett y u#ly. See >i#ure $.1E. 6erhaps this 'ill i&prove
i! later releases.
2he idea o) the hierarchical output is si&ple: output the pare!t
i!)or&ati o! o!ce, a!d the! pri!t a table o) all the releva!t child ro's
)or that pare!t, the! #o o! to the !e(t pare!t, a!d so o!.
FE1F
>or our curre!t e(a&pl e, e&ployee 1/1 has t'o depe!de!ts a!d
e&ployee 1/" has o!e, so 'e see the 1/1, the! the table o) t'o
depe!de!t ro's, the! 1/", a!d )i!all y a table o) o!e ro'.
2he )irst thi!# to !ote is that this resul t set is !ot a relatio!al tableQ A
relatio!al table has ro's that all have the sa&e colu&! types, very
recta!#ul ar.
<e see that the C;RS1R provi des a!other 'ay o) ru!!i !# a loop over
ro's, thus co&peti !# 'ith S343C2 )or this privile#e. But it ru!s &a!y
s&aller loops, o!e )or each ro' o) the outer S343C2, a!d each loop
e(ecuti o! produces a littl e table )or that pare!t ro'.
<e see that a C;RS1R ru!s i!side a S343C2. 8! )act, it ca! o!ly be
used i! a selectF list o) a topF level S343C2, as i! select e(pr, e(pr, `,
cursor (`. See 3(a&pl es $.*.. a!d $.*.1/ )or &ore e(a&pl es.
.%//%0s
5ARRA:s are collecti o! obHects, like !ested tables i! &a!y 'ays. But
they di))er i! three i&porta!t 'ays:
5ARRA:s &ai!tai ! the order o) thei r ele&e!ts
5ARRA:s have a &a(i &u& !u&ber o) ele&e!ts, set at type
de)i!i ti o! ti &e
5ARRA:s are held i! the &ai! table (usuall y, !ot i! a separate
stora#e table
8! versio! -.1.+ or later, 5ARRA:s 'ork very &uch like !ested tables
i! queries. :ou ca! use 2AB43( to co!vert a 5ARRA: colu&! value
i!to a queryF co&pati bl e table. 8! earlier versio!s, 5ARRA:s had to be
cast to !ested tables )irst, a terri ble i!co!ve!ie!ce.
Study the e(a&pl es )or detailsF Fthere are !o real !e' ideas here.
1nserts and 4pdates
Collectio! co!structors ca! be used i! the e(pected 'ay to speci)y
values )or !ested colu&!s. See 3(a&pl e $.*.1+. 2he ;pdate there
should be:
update pho!ebook pb set e(te!sio!s I e(te!sio!sLt(*$+,...
'here pb.phperso!.ss!o I 1"*-.EE,,C
FE"F
2he &ore i!teresti !# capabili t y is i!serti !# a!d updati !# the i!di vi dual
ele&e!ts i! !ested tables or 5ARRA:s. <e ca! use 2AB43( to tur! a
si!#le collectio!F value i!to a table that ca! be updated. <e use a
subquery to select the tar#et collecti o!F value, such as
2AB43(select e.depe!de!ts )ro& e&ployees e 'here
e.eperso!.ss!o I 1"*-.EE,,
a!d the! e&bed this i! a! i!sert state&e!t, as i! 3(a&ple $.*.1,:
i!sert i!to
table (select e.depe!de!ts )ro& e&ployees e 'here
e.eperso!.ss!o I 1"*-.EE,, values (*$$++.11",
!a&eLt(GS&i thG , GDia!eG , !ull, /C
Capter *C Embedded -AL Programs .
3&bedded S@4 &ea!s S@4 state&e!ts e&bedded i! host language (C
i! our case. 2he ori#i!al idea 'as )or e!dF users to access a database
throu#h S@4. Called casual users.
But this is !ot a #ood idea. 2akes too &uch co!ce!trati o!. Ca! you
picture airli!e reservati o! clerk doi!# Hob 'i th S@4M Custo&ers
'ai ti !#. Booki!# )li#ht (update o) seat table. Billi!#M
F 0eed to k!o' all tables D colu&!s, create co&ple( sy!ta( (>1R
A44
F too &uch risk o) &istakes, especiall y 'i th updates
8!stead, 'e have a! Application rogrammers create &e!u
applicati o!s, per)or& selects a!d updates pro#ra&&at i cal l y.
6ro#ra&&ers ca! spe!d a lot o) ti &e &aki!# sure the ri#ht S@4
state&e!t is usedC pro#ra&&ers are te&pera&e!tal l y suited to this
ki!d o) 'ork. 1) course !ice to have i!teracti ve S@4 )or so&e
situati o!s.
Ai& )or this chapter is to be able to i&ple&e!t A0: C10C385AB43
A4G1R82%7 usi!# a C pro#ra& that ca! #et at data throu#h S@4
state&e!ts.
Gets prett y co&plicated, but do!G t #et lost FF thereGs a #ood reaso! )or
each !e' piece o) co&ple(i t y i) you Hust u!dersta!d it.
+.1 S@4 state&e!ts i! C (#e!eral host la!#ua#e have sli#htl y
di))ere!t sy!ta( tha! the S@4 'eG ve see! so )ar.
e(ec sql select cou!t(W i!to :hostLvar )ro& custo&ersC
FE*F
State&e!t starts 'i th e(ec sql. 5ariable (hostLvar !eeded to receive
a!s'er, so !e' 8!to clauseC colo! sho's DB7S this is a pro#ra&
variable.
C co&piler does!G t reco#!iBe S@4 state&e!ts. 2he Oe(ec sqlO phrase
is a si#!al to a! e(plici t S@4 preco&pi l er that tur!s S@4 i!to real C
calls.
(Already have preco&pi l er acti!# )irst 'he! #ive #cc co&&a!d )or
thi!#s like =i !cl ude, =de)i !eC this is a di))ere!t o!e.
8! 1racle, start 'i th p#&.pc (&ea!i !# has e&bedded S@4 state&e!ts:
e(ec sql . . .. 2he! #ive co&&a!d (See Appe!di ( B
proc i!a&eIp#&. pc
Creates !e' source )ile, p#&.c, 'i th e(ec sql state&e!ts tur!ed i!to
pure C: they are !o' calls to 1RAC43 &o!i tor )u!cti o!s.
0e(t do true co&pilati o! a!d li!ka#e ('i th pro&pt.c, )or e(a&pl e,
create e(ecutabl e pro#ra& p#&.o(. %ave &ake)ile. <8ll create
p#&.o:
#cc F# Fc try.c
A!d so o!. 2he provi ded &ake)ile k!o's all the details. :ou ca! do
everythi !# i! the ho&e'ork usi!# the &ake)ile i!cluded. Aust create
your p#&.pc a!d type:
&ake 3Ip#& (01 spaces o! either side o) OIOC p#&.pc &ust
e(ist
+.1.1. A si&ple pro#ra&.
Declare sectio! . 0eed to declare C variables so they are
u!dersta!dabl e to 1RAC43 (1RAC43 !eeds to k!o' type o) variabl e.
%ere is ho' 'e 'ould set a variable used i! a search co!di tio! a!d
select i!to t'o others.
custLid I Oc//1OC NW or pro&pt user )or cust id WN
e(ec sql select c!a&e, disc!t i!to :custL!a&e, :custLdisc!t
)ro& custo&ers 'here cid I :custLidC
FE$F
0ote use o) colo! 'he! variabl e appears i! various positio!s i! select
state&e!t. 8&porta!t i! <here clause so ca! tell :custLid is!G t a
co!sta!t.
At ru!ti &e, custL!a&e a!d custLcity 'ill be )illed i! 'i th values
O2ip2opO a!d ODuluthO. Could !o' 'ri te i! pro#ra&:
pri!t)(OCusto&er !a&e is [s, a!d custo&er city is [sZ!O,
custL!a&e, custLcityC
0ote do!G t use colo!s here (used i! e(ec sql as hi!t to du&b
preco&pi ler. 8! order to use C variables i! S@4, &ust put i! Declare
Sectio!:
e(ec sql be#i! declare sectio!C
char custLidJ+K I Oc//1O, custL!a&eJ1$KC
)loat custLdisc!tC
e(ec sql e!d declare sectio!C
2he character arrays !eed to co!tai ! e!ou#h characters to hold stri!#
values o) colu&!s 64;S 103 )or ter&i !al GZ/G character. 3.#., cid 'as
declared as colu&! char($, 'ith values like Gc//1G i! S@4.
8! C 'he! 'e speci)y a stri!# such as Oc//1O (!ote double quotes,
this i!cludes a ter&i !al Bero value (GZ/G at the e!d: this serves as a
si#!al to )u!cti o!s such a pri!t) that the stri!# has e!ded.
Co!versio! ca! also occur i) declare
i!t custLdisc!tC
i! Declare sectio!. 2he! )loat value 1/.// 'ill beco&e 1/, but 1/.+/
'ill also beco&e 1/ ? lose )ractio!al si#!i)ica!ce.
S@4 co!!ect a!d disco!!ect. Ca! use sqlplus Scott to attach,
strcpy(user!a&e, OScottOC NW set up user!a&e a!d pass'ord
WN
strcpy(pass'ord, O2i#erOC NW )or 1racle lo#i!
WN
e(ec sql co!!ect :user!a&e ide!ti )i ed by :pass'ordC
2his is 'hat is used i! 1RAC43 (0ote: ca!G t use literal !a&es, &ust be
i! character stri!#. 0ote too that C1;023D stri!# (i! te(t is 012
!eededQQ Q Q
FE+F
2he co&&a!d to disco!!ect is represe!ted by:
e(ec sql co&&i t releaseC NW 1RAC43 disco!!ect WN
0o' look at 3(a&pl e +.1.1, >i#ure +.1, p# "/E . 2ask is to pro&pt
repeatedl y )or custo&er cid a!d pri!t out the custo&er !a&e a!d city.
%alt 'he! user i!puts a !ull li!e (Hust hits CR.
=i !cl ude Ustdi o.hT
e(ec sql i!clude sqlcaC
e(ec sql 'he!ever sqlerror stopC FFcovered later
Co!!ect, 4oop, pro&pt, select, co&&i t, pri!t. Co&&i t releases locks.
4oop is 'hile pro&pt( T /, 'hich is le!#th o) toke! i! stri!# i!put by
user ('hi te space does!G t cou!t. <he! user types CR, e!d loop.
%o' is 1RAC43 pro#ra& +.1 di))ere!t )ro& S@4F." Sta!dardM
8! 1RAC43 ca!G t use literal !a&e i! Oe(ec sql co!!ectO. &ust be i! a
character stri!#. Also &ust i!clude pass'ord.
See pro&pt( )u!ctio! e(plai!ed o! pa#e ",. a!d also i! Appe!di ( B.
3(pected to use i! ho&e'ork (available o!li!e, ha!dled by &ake)ile.
6ut out pro&pt J K stri!# to ter&i !al to i!sert a!y !u&ber P o) toke!s
(separated o!e )ro& a!other by 'hi tespace: GZ!G, G G (S6, GZtG . >or&at:
int promptchar prompt!str"#,int ntokens, ...$;
5ariableF le!#th ar#u&e!t list represe!ted by ellipsis ( ... (See PDR.
Ar#u&e!t !toke!s sho's !u&ber o) toke!s to i!put.
3llipsis (. . . represe!ts a variabl e !u&ber o) ar#u&e!t pairs: bu)1,
le!1, bu)", le!", . . ., bu)0, le!0.
%ere, bu)P, P I 1 to 0, is a character array to co!tai ! the Pth toke!
(al 'ays char str, a!d le!P is the &a(i &u& allo'ed le!#th o) the Pth
toke!.
All toke!s 'ill be read i!to character arrays by the pro&pt(
8) so&e o) these should be i!terpreted as i!t or )loat !u&bers, use
A0S8 sta!ard C library )u!ctio! ssca!)(, 'i th appropriate co!versio!
stri!#:
FE,F
3.#.: [d )or deci &al i!te#er a!d [) )or double or )loat.
Ca! look at code: uses )#ets, ssca!). Could use sca!) but &ul ti pl e
toke!s o! a li!e (i!put by co!)used user 'ill cause thi!#s to #et out
o) sy!ch.
)#ets bri!#s keyboard i!put i!to array li!eC ssca!) parses P toke!s
i!to (. (A!y other toke!s are lost. 1! p#, "1, is pro&pt" i) !eed to
i!put t'o values o! o!e li!e.
1P, up to !o', retrieved o!ly a si!#le ro' i!to a variabl e:
e(ec sql select c!a&e, disc!t i!to :custL!a&e, :custLdisc!t
)ro& custo&ers 'here cid I :custLidC
Selecti !# &ul ti pl e ro's 'ith a Cursor. (p#. "E/, pro#ra& i! 7ig *.",
pg. "+'
2he Select state&e!t used above ca! o!l y retrieve a S80G43 value.
2o per)or& a select o) &ul ti pl e ro's, !eed to create a C;RS1R.
8t is a! 3rror to use array to retrieve all ro's.
103FR1<FA2FAF2873 6R80C86A4: 0eed to retri eve o!e ro' at a ti &e:
cursor keeps track o) 'here 'e are i! the selected ro's to retri eve.
C17710 B3G8003RGS 78S2AP3, to try to #et all the data >893D i! a!
array. >i!d the &a( o) a! i!put seque!ce o) i!te#ers (do!G t k!o' ho'
&a!y.
%ere: Declare a cursor, ope! the cursor, )etch ro's )ro& the cursor,
close the cursor. 3.#., declare a cursor to retrieve aids o) a#e!ts 'ho
place a! order )or a #ive! cid, a!d the total dollars o) orders )or each
such a#e!t.
e(ec sql declare a#e!tLdollars cursor )or
select aid, su&(dollars )ro& orders
'here cid I :custLid #roup by aidC
But this is o!l y a declarati o!. >irst Ru!ti &e state&e!t is 1pe! Cursor:
e(ec sql ope! a#e!tLdollarsC
53R: 8761R2A02: 'he! ope! cursor, variabl es used are evaluated at
that &o&e!t a!d ro's to be retrieved are deter&i !ed.
FEEF
8) cha!#e variables ri#ht a)ter 1pe! Cursor state&e!t is e(ecuted, this
'ill !ot a))ect ro's acti ve i! cursor.
0o' )etch ro's o!e a)ter a!other:
e(ec sql )etch a#e!tLdollars i!to :a#e!tLid, :dollarLsu&C
2he 8!to clause !o' associated 'ith )etch rather tha! ope!, to #ive
&a(i &u& )le(i bili ty. (7i#ht 'a!t to )etch i!to di))ere!t variabl es
u!der certai ! co!di tio!s.
A cursor ca! be thou#ht o) as al'ays poi!ti !# to a C;RR302 R1<.
<he! Hust ope!ed, poi!ts to positio! Hust be)ore )irst ro'. <he! )etch,
i!cre&e!t ro' a!d )etch !e' ro' values.
8) !o !e' ro', #et ru!ti &e 'ar!i!# a!d !o retur!ed value (as 'ith
#etchar( i! C, value o) 31>.
0ote Opoi!t be)ore ro' to be retri evedO better behavior tha! Oread
values o! ro', the! i!cre&e!tO.
2his 'ay, i) ru!ti &e 'ar!i !# (adva!ced past e!d o) ro's i! cursor
the! there are !o !e' values to process, ca! leave loop i&&edi atel y.
%o' do 'e !otice ru!ti &e 'ar!i !#M S@4 Co&&u!i cati o! Area, S@4CA
(!o' deprecated by S@4F.", but stick 'i th. Recall i! p#&. +.1.1 had:
e(ec sql i!clude sqlcaC
2his creates a &e&ory struct i! the C pro#ra& that is )illed i! 'ith
!e' values as a resul t o) every e(ec sql ru!ti &e callC tells status o)
call.
FE-F
Class "*.
See 7ig *.", pg. "+' .
Revie': Declare a cursor, ope! the cursor, )etch ro's )ro& the cursor,
close the cursor.
3.#., declare a cursor to retrieve aids o) a#e!ts 'ho place a! order )or
a #ive! cid, a!d the total dollars o) orders )or each such a#e!t.
e(ec sql declare a#e!tLdollars cursor )or
select aid, su&(dollars )ro& orders
'here cid I :custLid #roup by aidC
But this is o!l y a declarati o!. >irst Ru!ti &e state&e!t is 1pe! Cursor:
e(ec sql ope! a#e!tLdollarsC
<he! ope! cursor, variables used are evaluated at that &o&e!t a!d
ro's to be retri eved are deter&i !ed.
0o' )etch ro's o!e a)ter a!other:
e(ec sql )etch a#e!tLdollars i!to :a#e!tLid, :dollarLsu&C
A cursor ca! be thou#ht o) as al'ays poi!ti !# the C;RR302 R1< (i)
a!y, the ro' last )etched. <he! )etch a!d #et 01 DA2A R32;R03D,
&ea!s e!d loop ri#ht a'ay.
0123, i! #e!eral, 'e ca!!ot pass a cursor as a! ar#u&e!t. But ca!
&ake it e(ter!al to a!y )u!ctio! i! a )ile, so all )u!ctio!s ca! #et at it.
%o' do 'e tell that the &ost rece!t )etch did !ot retri eve a!y dataM
Re&e&ber e(ec sql i!clude sqlcaM Co&&u!icati o! area
Decprecated, but sqlca.sqlcode is still a! i&porta!t variable.
Still 'orks to test the value o) sqlca.sqlcode a)ter delete, update,
i!sert, etc. to )i#ure out 'hat errors occurred. M%0 be replaced by
S@4S2A23.
1RAC43 has i&ple&e!ted S@4S2A23, but still uses sqlca.sqlcode
because #ives &ore error codes. Ca! use S@4S2A23 i! A0S8 versio! o)
1racle.
FE.F
>or portabi li t y best to use di))ere!t &ethod )or &ost thi!#s:
<%30353R state&e!t, belo'.
0123 that sqlca &ust be declared 'here all )u!cti o!s that !eed to ca!
access it: usuall y e(ter!al to a!y )u!cti o!. Go throu#h lo#ic.
Starti !# to talk about +." 3rror ha!dli !# .
393C S@4 80C4;D3 S@4CA creates a C struct thatG s rather
co&plicated, a!d de!e#rated, but 'e deal &ai!l y 'ith a si!#le
co&po!e!t, sqlca.sqlcode.
2his tells 'hether
sqlca.sqlcode II /, success)ul call
U /, error, e.#., )ro& co!!ect, database does !ot e(ist F1,
(listed as i) positi ve
T /, 'ar!i!#, e.#., !o ro's retri eved )ro& )etch
(sa' this already, tell 'he! cursor e(hausted
3rror is o)te! a co!ceptual error i! the pro#ra& code, so i&porta!t to
pri!t out error &essa#e i! pro#ra&s youG re debu##i !#. Co&e to this.
See 3(a&ple +.".$ i! te(t.
8! #e!eral, there are a !u&ber o) co!di tio!s that a <he!ever
state&e!t ca! test )or a!d actio!s it ca! take. Ge!eral )or& o)
<he!ever state&e!t:
e(ec sql 'he!ever Uco!di ti o!T Uacti o!T
Co!di ti o!s.
o S@43RR1R 2ests i) sqlca.sqlcode U /
o 012 >1;0D 2ests i) !o data a))ected by >etch, Select, ;pdate,
etc.
o S@4<AR080G 2ests i) sqlca.sqlcode T / (di))ere!t tha! !ot
)ou!d
Actio!s
o C10280;3 Do !othi !#, de)aul t actio!
o G121 label Go to labeled state&e!t i! pro#ra&
o S216 8! 1RAC43, 6ri!ts out error &s# a!d aborts
pro#ra&
o D1 )u!cLcall (80 1RAC43 104:Call !a&ed )u!ctio!C very
use)ul
F-/F
4ook at e(ercise due: +.*. P!o' ho' to test )or custLid value that
does!G t e(ist.
2he reaso! the call actio! is so ha!dy is that <%30353R state&e!ts
do!G t respect )u!cti o! divisio!s . 8) 'e 'ri te a si!#le 'he!ever at the
be#i!!i !# o) a pro#ra& 'i th a lot o) )u!ctio!s:
'he!ever sqlerror #oto ha!dleLerror
&ai!( R
. . . S
)u!c1( R
. . .S
)u!c"( R
. . .
;si!# the above trick, the e))ect o) the <%30353R state&e!t 'ill spa!
the scope o) the di))ere!t )u!cti o!s. %o'ever, D1 is !ot portable, a!d
the portabl e 'ay requi res a G121 tar#et i! each )u!cti o!. 2his G121F
tar#et code o) course ca! have a )u!cti o! call.
2he <%30353R state&e!t is ha!dled by the preco&pi l er, 'hich puts
i! tests a)ter all ru!ti &e calls (e(ec sql select` or 'hatever a!d
does!G t care about 'hat )u!cti o! itGs i! (it does!G t eve! P01<. >or
e(a&pl e:
'he!ever sqlerror #oto ha!dleLerrorC NW belo' this, 'ill do this
G121 o! error WN
Co!ti!ues doi!# this u!til its actio!s are overri dde! by a <%30353R
'i th a di))ere!t actio! )or the sa&e co!di tio!:
'he!ever sqlerror co!ti !ueC NW belo' this, 'ill do !othi !# WN
But !ote i! e(a&pl e above, there &ust be a label ha!dleLerror i! all
these )u!ctio!s. A call to a ha!dleLerror )u!cti o! 'ould save a lot o)
code.
@uestio!. <hat happe!s here, 'here o!ly t'o 'he!ever state&e!ts
are listed e(plici tl yM
&ai!(
R
e(ec sql 'he!ever sqlerror stopC
. . .
#oto label1C
. . .
F-1F
e(ec sql 'he!ever sqlerror co!ti !ueC
label1: e(ec sql update a#e!ts set perce!t I perce!t \1C
8) 'e arri ve at label1 by &ea!s o) the #oto state&e!t, 'hich 'he!ever
state&e!t 'ill be i! )orce )or co!di tio! sqlerrorM 3.#., 'hat i) &ispell
colu&!a&e perce!tM (A!s'er is: co!ti !ue.
<hat i) did:
&ai!(
R
e(ec sql 'he!ever sqlerror #oto ha!dleLerrorC
e(ec sql create table custo&ers
(cid charJ$K !ot !ull, c!a&e varchar(1*, . . .
. . .
ha!dleLerror:
e(ec sql drop custo&ersC
e(ec sql disco!!ectC
e(it(1C
<hatGs the proble&M 6ossible i!)i!i te loop because #oto actio! still i!
)orce 'he! drop table (&ay be error. 0eed to put i!:
e(ec sql 'he!ever sqlerror co!ti !ueC
Ri#ht at be#i!!i !# o) ha!dleLerror. 1verrides #oto actio! ('o!G t do
a!ythi !#. (De)aul t e))ect i) do!G t say a!ythi !# about 'he!ever.
Also !eed to take de)aul t !o!F actio! to per)or& speci)ic error
checki!#. 7aybe have alter!ati ve actio! i! case i!su))icie!t disk
space 'he! try to create table:
e(ec sql 'he!ever sqlerror #oto ha!dleLerrorC
e(ec sql create table custo&ers ( . . .
i) (sqlca.sqlcode II F-11" NW i) i!su))icie!t disk space WN
Uha!dl e this proble&T
But this 'o!G t 'ork, because esqlc places test
i)(sqlca.sqlcode U /
Ucal l stop)!T
8773D8A234: a)ter create table, so it #ets there )irst a!d test )or
sqlca.sqlcode I F-11" !ever #ets e!tered (U / #ets there )irst.
So have to do 'he!ever sqlerror co!ti !ue be)ore testi !# )or speci)ic
error.
F-"F
Book tells you ho' to #et a speci)ic error &s#. Good use i! case do!G t
'a!t to Hust pri!t out error &s#. (Discuss applicati o! e!vi ro!&e!t )or
!aive user: !ever leave applicatio! e!viro!&e!t, 'ri te errors to lo#
)ile.
Recall that i) disc!t i! custo&ers has a !ull value )or a ro', do !ot
'a!t to retrieve this ro' 'ith:
select W )ro& custo&ers 'here disc!t UI 1/ or disc!t TI 1/C
But !o' say 'e per)or&ed this test i! pro#ra& lo#ic:
e(ec sql select disc!t, Uother colsT i!to :cdisc!t, Uot her varsT
'here cid I :custi dC
0o' 'e decide to pri!t out these col values i! situati o!:
i)(cdisc!t UI 1/ ]] cdisc!t T 1/ pri!t) (. . ..
0o' lookQ %ost variabl e cdisc!t is a )loat var, A0D A44 73A080G>;4
B82 C17B80A2810S AR3 2AP30 ;6 <82% R3A4 5A4;3S. 2hereGs !o
'ay that cdis!t 'ill )ail the i) test above.
0eed to add so&e 'ay to test i) the variabl e cdis!t has bee! )illed i!
'i th a !ull value (the! i!clude A0D 012 0;44 i! i) test. Ca! do this
'i th i!dicator variabl e, retrieve alo!# 'i th disc!t:
short i!t cdisc!tLi!dC NW )or& o) declarati o! WN
2he! cha!#e select:
e(ec sql select disc!t, Uother colsT i!to :cdisc!t:cdisc!tLi!d, NW
1RAC43 sy!ta( WN
Uot her varsT 'here cid I :custidC
0o' i) cdisc!tLi!d II F1, value o) cdisc!t is reall y !ull. Add test that
cdisc!t is !ot !ull by:
i)((cdisc!t UI 1/ ]] cdisc!t T 1/ DD cdisc!tLi!d UT F1
pri!t) (. . ..
0ote ca! also S21R3 a !ull value by setti !# a !ull i!dicator cdisc!tLi!d
a!d 'ri ti !#
F-*F
e(ec sql update custo&ers set disc!t I :cdisc!t:cdisc!tLi!d 'here
. . .
1!e other co&&o! use )or i!dicator variabl e )or char colu&! bei!#
read i! to array, is to !oti)i y tru!cati o! by value T /, usuall y le!#th o)
colu&! stri!#.
F-$F
Class ").
1ndicator .ariabl es
Sectio! +.*. Co&plete descripti o!s o) S@4 State&e!ts.
Select. See Sectio! +.*, p# "-1, >i#ure +.*. CA0 104: R32R8353
V3R1 1R 103 R1<.
4ook at #e!eral )or&: 0o ;0810 or 1RD3R B: or GR1;6 B: i! Basic
S@4 )or& . like Subquery )or& o! p#. 1$$, add i!to clause. (%o' use
GR1;6 B: 'i th o!l y o!e ro' retrievedM Still part o) )ull sta!dard
See >i#ure +.$, correspo!di !# (a!alo#ouus C types )or colu&! type.
Co!versio! do!e i) i!te#er col type, )loat C var.
0ote that i! #e!eral a variabl e ca! be used to build up a! e(pressio!
i! a searchLco!di ti o!:
select c!a&e i!to :custL!a&e 'here cid I :custi d a!d city I
:c!a&eC
But 'e cannot use caracter string to contai n part of statement
re=uiri ng parsing :
char co!dJ K I O'here cid I Gc//*G a!d city I GDetroi tG OC
e(ec sql select c!a&e i!to :cust!a&e 'here :co!dC
012 43GA4. 2he abili ty to do this is called ODy!a&i c S@4O, covered
later.
Declare Cursor state&e!t, p#. "-*. ;sed 'he! retri evi !# &ore tha!
o!e ro' i! select, so basicall y a! e(te!sio! o) i!teracti ve Select.
%as GR1;6 B:, %A580G, ;0810, 1RD3R B:. Adds clause: )or update
o) , !eed later.
Cursor ca! o!l y &ove >1R<ARD throu#h a set o) ro's. Ca! close a!d
reope! cursor to #o throu#h a seco!d ti &e.
2'o )or&s o) delete, p#. "-*, Searched Delete a!d 6ositio!ed Delete :
e(ec sql delete )ro& table!a&e JcorrL!a&eK
J'here searchLco!di ti o! ] 'here curre!t o) cursorL!a&eKC
F-+F
A)ter Searched Delete, used to be 'ould e(a&i !e sqlca.sqlerrdJ"K to
deter&i !e !u&ber o) ro's a))ected. 0e' 'ay e(ists (Do!G t k!o' yet.
A)ter 6ositio!ed delete, cursor 'ill poi!t to e&pt y slot i! cursor ro'
seque!ce, like ri#ht a)ter ope! or 'he! have ru! throu#h cursor.
Ready to adva!ce to !e(t ro' o! )etch. <orks Hust ri#ht to delete
everythi !# a)ter look at it i! loop:
4116
e(ec sql )etch delcust i!to :custLidC
U'orkT
e(ec sql delete )ro& custo&ers 'here curre!t o) delcustC
30D 4116
8) cursor &oved )or'ard to !e(t ro' (say a)ter delete, 'ould o!l y be
deleti !# every 12%3R ro'.
Could create cursor to select all custo&ers i! Detroi t to delete the&,
or could select all custo&ers a!d the! check i) city is Detroi t. >irst is
&ore e))icie!t. 6R80C86A4: do all searchLco!di ti o!s be)ore retri evi !#.
But probabl y a Searched Delete is &ost e))icie!t o) all )or Detroi t
custo&ers FF save %A580G to s'itch threads, do all i! query opti &iBer.
Seco!d para#raph o! p "-$: 8! order )or positio!ed delete to 'ork,
the cursor &ust (1 be already ope!ed a!d poi!ti !# to a real ro', ("
be a! updatabl e curso! (!ot R3AD 104:, (* >R17 clauses o) delete
&ust re)er to sa&e table as >R17 clause o) cursor select.
2'o )or&s o) ;pdate state&e!t, p#. "-+: Searched ;pdate a!d
6ositio!ed ;pdate. Sa&e ideas as 'i th Delete (sqlca.sqlerrdJ K 'ill
co!tai ! cou!t o) ro's a))ected.
>irst pri!ti !#: the &ost #e!eral ;pdate sy!ta( is o! p#. EE*, >i#ures
C.** a!d C.*$.
Sa&e ;pdate )or& as earlier, p#. 1$.: each colu&! value ca! be
deter&i !ed by a scalar e(pressio!, a! e(plici t !ull, or a value #ive! by
a scalar subquery.
8!sert, p#. "-,. 0o positio!ed i!sert, because positio! deter&i !ed i!
other 'ays.
1pe!, >etch, Close, p#. "-,. 1pe! evaluates e(pressio!s, sets up ro'
list to retrieve )ro&, u!cha!#i !# eve! i) e(pressio! values cha!#e.
F-,F
Create table, drop table, co!!ect, disco!!ect. 0ot create database,
because that is !ot a! S@4 co&&a!d. S33 A6630D89 C )or &ore i!)o.
-ection *.( 8dea o) co!curre!cy , ba!k tellers have si&ul ta!eous
access to data. 6roble&: 3(a&ple +.$.1 . 8!co!siste!t vie' o) data.
2able A o) accou!ts, A.bala!ce 'here A.aid I A" 'ill be called A".
bala!ce. 1!e process 'a!ts to &ove &o!ey )ro& o!e accou!t to
a!other. Say Y$//.//.
S1: A1.bala!ce II Y.//.// A".bala!ce II Y1//.//
S": A1.bala!ce II Y+//.// A".bala!ce II Y1//.//
S*: A1.bala!ce II Y+//.// A".bala!ce II Y+//.//
8) a!other process 'a!ts to do a credi t check, adds A1.bala!ce a!d
A".bala!ce, should!G t see S" or &ay )ail credi t check. See 3(a&ple
+.$.1, p#. "$+.
Create a! idea called tra!sacti o!s. 6ro#ra&&i !# 'i th tra!sacti o!s
&akes #uara!tees to pro#ra&&er, o!e o) 'hich is 8solatio!. 7ea!s
every tra!sactio! acts as i) all data accesses it &akes co&e i! serial
order 'i th !o i!terve!i !# accesses )ro& others. 3.#.:
21: R1(A1 <1(A1 R1(A" <1(A" (e(plai! !otati o!
2": R"(A1 R"(A"
2ur! ;pdate i! to R the! <. 2ur! Select i!to R o!l y. 6roble& o) S"
co&es because o) schedule:
R1(A1 <1(A1 R"(A1 R"(A" R1(A" <1(A"
But this is !ot allo'ed to happe! 'i th tra!sacti o!s, &ust act like:
21 the! 2" (2" sees state S* or 2" the! 21 (2" sees S1.
Do!G t care 'hich. Called SerialiBabili t y .
F-EF
Class "+.
Revie' idea last ti &e o) co!curre!cy.
R1(A1 <1(A1 R"(A1 R"(A" R1(A" <1(A"
But this is !ot allo'ed to happe! 'i th tra!sacti o!s, &ust act like:
21 the! 2" (2" sees state S* or 2" the! 21 (2" sees S1.
Do!G t care 'hich. Called SerialiBabili t y .
(<hy do 'e 'a!t to have real co!curre!cy, classM <hy !ot true serial
e(ecuti o!M 8dea o) keepi!# C6; busy. 7ore !e(t ter&.
2'o !e' S@4 state&e!ts to cause tra!sacti o!s to occur.
e(ec sql co&&i t 'orkC Success)ul co&&i t, ro's updated, beco&e
co!curre!tl y visible.
e(ec sql rollbackC ;!success)ul abort , ro' value updates rolled back
a!d beco&e co!curre!tl y visible.
2ra!sactio!s S2AR2 'he! )irst access is &ade to table (select, update,
etc. a)ter co!!ect or prior co&&i t or abort. 3!ds 'ith !e(t co&&i t
'ork or rollback state&e!t or syste& abort )or other reaso!.
Recall idea that 'e hold locks, user has S@4 state&e!ts to say: lo#ical
task is co&plete: you ca! &ake updates per&a!e!t a!d drop locks
!o'.
2ypicall y, applicati o!s loop arou!d i! lo#ic, 'ith user i!teracti o!,
tra!sactio! e(te!ds )ro& o!e i!teracti o! to the !e(t. But &i#ht have
&ul ti pl e i!teracti o!s bet'ee! i!teracti o!s. D10G t typicall y hold
tra!sactio! duri!# user i!teracti o! (livelock.
;ser i!teracti o! &ay set o)) a lot o) accou!t bala!ci!#: each 2(
subtracts &o!ey )ro& !F1 acct bala!ces, adds &o!ey to a! e!d o!e.
Say should abort i) o!e acct is u!der bala!ce.
Clearl y syste& ca!G t #uess 'he! o!e set o) accts has bee! bala!ced
o)) a!d start i&&edi atel y o! a!other set. ;ser &ust tell <%30 to
release locks.
5alue o) rollback is so do!G t have to 'ri te code to reverse all prior
cha!#es.
F--F
%tomicity. 2he set o) updates per)or&ed by a 2( are ato&ic, that
is, i!di visible. 8) so&ethi !# bad happe!s (ter&i !al #oes do'! 'ill
abort.
Consistency. 3.#., &o!ey is !ei ther created !or destroyed by
lo#ic, the! 'o!G t happe!.
1solation. As i) serial set o) 2(s.
Durabili ty. 3ve! i) lose po'er D &e&ory (crash, lose place i!
'hat doi!#, 'ill R3C153R )ro& the crash a!d #uara!tee ato&i ci ty.
41CP80G. Si&ple versio! here. (a <he! a 2( accesses a ro' R, )irst
&ust #et lock. (b 4ocks are held u!til 2( e!ds (Co&&i t. (* D $
4ocks are e(clusi ve, so seco!d locker 'ill 'ai t i) it ca!.
E:ample *.(. ". Recall proble& o) i!co!siste!t vie'.
R1(A1 <1(A1 R"(A1 R"(A" C" R1(A" <1(A" C1
'here add Ci )or co&&i t. 0o' add locks.
41(A1 R1(A1 <1(A1 4"(A1 (co!)lict, &ust 'ai t 41(A" R1(A"
<1(A" C1 (Releases 21 locks i!cludi!# 41(AC 0o' lock request 4"(A1
ca! succeed R"(A1 R"(A" C"
Result is 2" sees State S*. 0o &atter 'hat arra!#e&e!t o) atte&pts,
2" 'ill see either S1 or S*. But 0e' idea: D3AD41CP. <hat i)
rearra!#e:
R1(A1 <1(A1 R"(A" R"(A1 C" R1(A" <1(A" C1
0o' locki!# proceeds as )ollo's:
41(A1 R1(A1 <1(A1 4"(A" R"(A" 4"(A1 (prior 41(A1, so 2"
&ust 'ai t 41(A" (prior 4"(A" so 21 &ust 'ai t: D3AD41CPQ
illustrate
(&ust choose o!e 2( to abort, say 2", 41(A" is success)ul R1(A"
<1(A" C" (0o' 2" audi t atte&pt retrys as 2* 4*(A" R*(A" 4*(A1
R*(A1 C*
F-.F
A!d state see! by 2" (retri ed as 2* is S*. 8) aborted other o!e, state
see! by 2" 'ould be S1. 0ote that i! #e!eral, locks held are &uch
&ore sophisticated tha! these e(clusive locks.
2his &ea!s that pro#ra&&er &ust 'atch out )or deadlock error
retur!, atte&pt a retry. See 3(a&pl e +.$.$, p# ".,.
0ote ca!G t ru! i!to a deadlock abort u!til already %14D a lock a!d try
to #et a!other o!e. 1) course this could happe! i! a si!#le i!structi o!
i! (e.#. Searched ;pdate 'i th &ul ti pl e ro's. But i) o!l y si!#le ro's
accessed, !o
0o user i!teracti o! duri!# tra!sacti o! or 4ivelock. >or e(a&pl e, i)
retrieve data o) qua!ti t y available )or a! order a!d the! co!)er 'ith
custo&er. See 3(a&ple +.$.+, pro#ra& p#. *//But proble& as a
resul t.
2o avoid this, typicall y co&&i t tra!sactio! a)ter retri evi !# data, the!
co!)er 'ith custo&er, the start !e' tra!sacti o! )or update based o!
custo&er 'ishes. But !e' proble&. See 3(a&pl e +.$.,, pro#ra& p#
*/1
Class !(. E:am !
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
Class !*
Capter ), Database Design . <e !o' tackle the )ollo'i !#:
@: ho' do 'e a!al yBe a! e!terprise a!d list the data ite&s )or a
database, the! decide ho' to place these data ite&s colu&!s i!
relatio!al tables .
;p to !o', 'eG ve had this do!e )or us, but !o' 'e have to decide. >or
e(a&pl e should 'e Hust put the& all i! the sa&e tableM
A!s'er is !o, because (as 'eG ll see the data does!G t behave 'ell.
Co!sider the CA6 database. Could 'e Hust have a si!#le table,
CA61RD3RS,
CA61RD3RS :I C 9 A 9 6 9 1 'here C.cid I 1.cid a!d A.aid I
1.aid
a!d 6.pid I 1.pid
See a!y proble&s 'ith thatM 1!e proble& is redu!de!cy ? 3very ro'
o) CA61RD3RS &ust duplicate all product i!)or&ati o!, a!d this
happe!s lots o) ti &es. Ditto )or custo&ers a!d a#e!ts.
F./F
8) you look at the !u&ber o) colu&!s a!d assu&e lots o) orders )or
each C, A, 6, see B8G 'aste o) disk space co&pared to separate tables.
Bad because c!a&e, city, p!a&e, etc., are lo!# co&pared to 1RD3RS
colu&!s.
>urther, !ote there is i! 6R1D;C2S a qua!ti t y colu&!, &ea!i !#
qua!ti t y o! ha!d. Assu&e !eeds updati !# every ti &e a !e' order is
placed.
But the! i! CA61RD3RS 'i th popular product, !u&ber or ro's #ro's
'i th !u&ber o) orders (thousa!dsQ. A44 o) the& have to be ordered
each ti &e. B8G i!e))icie!cy co&pared to separate table )or products.
A!d 'hat do happe!s 'he! 'e place a !e' order FF do 'e ask the
applicati o! pro#ra&&er to #et the user (key e!try clerk to e!ter all
the i!)or&ati o! about custo&er, a#e!t, product i! CA61RD3RS every
ti &eM
Does!G t &ake se!se FF 'aste o) ti &e FF clerk &i#ht #et it 'ro!#.
WWWW So the pro#ra& looks up data (say )or product a!d e!ters it i!
CA61RD3RSM <here does it look it upM >i!d a!other ro' 'ith the
sa&e product a!d copy dataM
0ote that 'he! have separate table )or products, Hust e!ter pid ('hich
ca! be looked up i! products table i) start 'ith product
!a&eNdescripti o!.
<hat i) so&e product is out o) stock a!d takes a lo!# ti &e to reorder
so all the orders )or that product have bee! )illed )ro& CA61RD3RS
('e do!G t keep these orders arou!d )orever.
But !o' 'here do 'e )i!d product i!)or&ati o!M %ave 'e )or#ot te!
about this product because 'e have!G t had orders )or a'hileM 0ote
solved i) 'e have disti!ct products table.
2here are also a !u&ber o) Obusi!ess rulesO that it 'ould be !ice to
have the database syste& #uara!tee FF si&ple thi!#s, but qui te
subHect to error i! data e!try.
>or e(a&pl e, 'e say that the pid colu&! is a u!ique ide!ti )i er )or a
product. 2hatGs a busi!ess rule. 0o t'o products have the sa&e pid.
F.1F
But revie' !o' the idea o) CA61RD3RS FF everythi !# Hoi!ed. >or each
orders ro', have all i!)o o! custo&er, a#e!t, a!d product. 6ut up
picture.
8tGs a littl e hard to )i#ure out 'hat that &ea!s i! the CA61RD3RS table
'here pid 'ill be duplicated o! every ro' 'here a #ive! product is
ordered, but letGs try.
8! the database desi#! 'here 6R1D;C2S is a separate table, 'e say
that that pid is a OkeyO )or the table, i.e. it is u!ique.
8) 'e thi!k i! ter&s o) ro's i! 6R1D;C2S bei!# duplicated i!
CA61RD3RS ()or every order that deals 'i th the sa&e product, the!
ho' 'ould 'e characteriBe pidM
Busi!ess rule: every u!ique pid value is associated 'i th a u!ique
qua!ti t y (it 'ould be a bad thi!# i) i! CA61RD3RS t'o ro's 'ith the
sa&e pid had di))ere!t qua!ti t y values. 8s the reverse trueM
<e 'ri te this: pid FT qua!ti t y, a!d say pid functionall y determi nes
qua!ti t y, or qua!ti t y is functionall y determi ned by pid.
8s it also true that pid FT p!a&eM pid FT cityM pid FT priceM Rules like
this are called >Ds. 2hese are 'hat 'e re)er to as 8!terrel ati o!shi ps
bet'ee! data ite&s i! the te(t.
<riti!# do'! a set o) rules like this is the be#i!!i !# o) a process
called O!or&al iBati o!O, a relati vel y &echa!ical process 'hich leads to
a 'ellF behaved breakdo'! o) data ite&s i!to tables.
1!ce these tables have bee! created, all >Ds are &ai!tai !ed by
co!strai !ts de)i!ed 'i th Create 2able state&e!t, ca!didate key
(u!ique value co!strai !t a!d pri &ary key co!strai !t (see >i#ure ,.1,
p#. **1.
A di))ere!t desi#! approach, called 3!tityF Relatio!ship &odelli!#, is
&ore i!tui ti ve, less &echa!ical, but basicall y leads to the sa&e e!d
desi#!.
8!tui ti vel y, 'e thi!k that there is a realF 'orl d obHect class )or the set o)
products 'i th ide!tical properti es 'e 'a!t to track.
<e create a table 'here 'e have o!e ro' )or each type. 2he cid
colu&! is the Oide!ti )i erO o) the ro'.
F."F
2his i!tui ti ve co!cept is the be#i!!i !# o) 'hat 'e call 3!tityF
Relatio!shi p &odelli !# ? products are a si!#le 3!tity (or 3!tity set.
0ote that products are so&e'hat di))ere!t )ro& custo&ers a!d
a#e!ts. 2here is o!e ro' i! AG302S )or each disti!ct a#e!t a!d ditto
custo&ers.
But disti!ct products are !ot thou#ht 'orth tracki !# ? 'e deal 'ith a
ro' )or each Cate#ory o) products.
Sectio! ,.1. 8!troducti o! to 3FR
De). ,.1.1 3!tity. A! e!ti t y is a collecti o! o) disti!#uishable realF 'orld
obHects 'i th co&&o! properti es.
3.#., colle#e re#istrati o! database: Stude!ts, 8!structors,
ClassLroo&s, Courses, CourseLsectio!s (di))ere!t o))eri!#s o) a si!#le
course, #e!erall y at di))ere!t ti &es by di))ere!t i!structors,
ClassLperiods.
0ote that 'e CapitaliBe e!ti t y !a&es.
ClassLroo&s is a #ood e(a&pl e o) a! e!ti ty. Disti!#uishable (by
locatio!. %ave co&&o! properti es, such as seati !# capaci ty, 'hich
'ill tur! i!to a colu&! o) a table (di))ere!t values o) these co&&o!
properti es.
ClassLperiods is a! e!ti t yM A! i!terval o) ti &e is a realF 'orld obHectM
A bit 'eird, but basicall y thi!k o) assi#!i!# a stude!t to a classroo&
duri!# a class period, so ti &e i!terval is treated Hust like classroo&.
0or&all y, a! e!ti t y such as ClassLroo&s or Custo&ers is &apped to a
relatio!al table, a!d each ro' is a! e!ti t y occurre!ce , or e!ti t y
i!sta!ce , represe!ti !# a particular obHect.
8! the case o) 6roducts, a! e!ti t y i!sta!ce is a cate#ory o) obHects sold
by our 'holesale co&pa!y.
8t is u!usual i! the )ield to use a plural )or e!ti t y a!d table, Custo&ers
i!stead o) Custo&er. <e do this to e&phasiBe that the e!ti t y
represe!ts a Set o) obHects.
De). ,.1." Attri bute. A! attri bute is a data ite& that describes a
propert y o) a! e!ti t y or a relatio!shi p (to )ollo'.
F.*F
0ote that 'e have special ter&i !ol o#y )or special ki!ds o) attri butes,
see p#. */,.
A! ide!ti )i er is a! attri bute or set o) attri butes that u!iquel y ide!ti )i es
a! e!ti ty i!sta!ce. 4ike cid )or Custo&ers. Ca! have pri &ary
ide!ti )i er .
A descriptor is a !o!F key attri bute, descripti ve. 3.#., city a custo&er is
i!, color o) a car, seati !# capaci ty o) a classroo&, qty o) a! order.
A &ul tiF valued attri bute is o!e 'hich ca! take o! several values
si&ul ta!eousl y )or a! e!ti t y i!sta!ce. 3.#., key'ord )or
Aour!alLarticles or hobby )or 3&ployees. Disallo'ed by relati o!al rule
1, but i! 1RDB7S.
8! 3FR, 'e ca! have a co&posi te attri bute (like a !ested struct i!side
the ro', e.#., c!a&e ca! have )!a&e, l!a&e, &idi!i t as three parts.
0ote that the relatio!al &odel, rule 1, 'hich disallo's &ul tiF valued
colu&!s, also disallo's co&posi te colu&!s. 1P i! 1RDB7S, but &ust
&ap co&posi te attri bute to &ul ti pl e colu&!s i! relatio!al &odel.
0ote that ter& attri bute is also used i! relatio!s, but ideas correspo!d
i! the &appi !# o) e!ti ti es a!d relati o!s i!to relatio!al tables.
0ote that 'hile e!ti ty i!sta!ces 'i thi ! a! e!ti t y are said to be disti!ct,
but this is o!ly a &athe&ati cal idea u!til 'e have ide!tier attri butes.
<e 'ri te 3 is a! e!ti ty, 'ith e!ti ty i!sta!ces Re
1
, e
"
, . . ., e
!
S. 0eed
a! ide!ti )i er attri bute de)i!ed, u!ique )or each occurre!ce e
i
.
6ut up dia#ra& )ro& p#. ***, >i#ure ,.".
2ra!s)or&i !# 3!tities a!d Attri butes to Relatio!s is prett y obvious, as
&e!ti o!ed previ ousl y.
2ra!s)or&ati o! Rule 1. A! e!ti ty is &apped to a si!#le table. 2he
si!#leF valued attri butes o) the 3!tity are &apped to colu&!s
(co&posi te attri butes are &apped to &ul ti pl e si&ple colu&!s. 3!tity
occurre!ces beco&e ro's o) the table.
2ra!s)or&ati o! Rule ". A &ul tiF valued attri bute &ust be &apped to
its o'! table. See botto& o) p#. **$. (0o lo!#er true i! 1RDB7S.
0ot too &uch po'er so )ar, but relatio!shi p adds real &odeli !# po'er.
F.$F
De). ,.1.*. Relatio!ship (p#. **+. Give! a! ordered list o) & e!ti ti es,
3
1
, 3
"
, . . . , 3
&
, ('here the sa&e e!ti t y &ay occur &ore tha! o!ce i!
the list, a relatio!shi p R de)i!es a rule o) correspo!de!ce bet'ee!
the i!sta!ces o) these e!ti ti es. Speci)icall y, R represe!ts a set o) &F
tuples, a subset o) the Cartesia! product o) e!ti ty i!sta!ces.
8!structors teaches CourseLsectio!s
3&ployees 'orksLo! 6roHects (attri bute, perce!t (o) ti &e
3&ployees &a!a#es 3&ployees (ri!#, or recursive relatio!shi p
See top o) p# **, )or dia#ra&. 0ote OrolesO o) labeled co!!ecti !#
li!es i! case o) recursive relatio!shi p.
3(a&ple ,.1.*. 2he orders table i! the CA6 database does 012
represe!t a relatio!shi p. Reaso! is that orders ro's do !ot
correspo!d to a subset o) the e!ti ti es i!vol ved. 7ulti pl e orders ca!
e(ist 'i th sa&e cid, aid, pid.
2he orders table is reall y a! e!ti ty, 'i th ide!ti )i er ord!o.
1) course, there is a relati o!shi p bet'ee! 1rders a!d each o) (p# *.1
Custo&ers requests 1rders, A#e!ts places 1rders, 1rders ships
6roducts.
0ote labels try to describe le)t to ri#ht, top do'! order. Could cha!#e
to 1rders placedLby A#e!ts.
Could have a ter!ary relati o!shi p, see 3(a&pl e ,.1.$, de)i!ed i! ter&s
o) orders table.
create table yearlies(cid char($, aid char(*, pid char(*,
totqt y i!te#er, totqt y )loatC
i!sert i!to yearlies select cid, aid, pid, su&(qt y, su&(dollars
)ro& orders #roup by cid, aid, pidC
2ra!s)or&ati o! rules are &ore di))icul t )or relati o!shi ps. 2he yearlies
relatio!shi p is tra!s)or&ed i!to a yearlies table. %o'ever, !o
separate table )or &a!a#es relati o!shi p )ro& e&ployees to
e&ployees.
8!stead, put &#ri d colu&! i! e&ployees table (si!ce every e&ployee
has at &ost o!e &a!a#er, this 'orks. See top o) pa#e **-, >i#. ,.$.
2he idea o) co&&o! properti es is that all 'e !eed to k!o' ca! be
listed i! labeled colu&!s. Sta!dard busi!ess )or&.
F.+F
Class !).
Re&e&ber the 3FR dia#ra& o! p# **, 'i th the relati o!shi p that says
3&ployees 'orksLo! 6roHects, 'here 'orksLo! is a relatio!shi p.
'orksLo! has the co!!ected attri bute perce!t. Dra' it.
0ote: perce!t, associated 'ith relati o!shi p, i.e., a value 'i th each
relatio!ship i!sta!ce.
2he relatio!shi p i!sta!ce represe!ts a speci)ic pairi!# o) a!
3&ployees i!sta!ce 'ith a 6roHects i!sta!ceC perce!t represe!ts the
perce!t o) ti &e a! e&ployee i!sta!ce 'orks o! that proHect.
Clearl y have the busi!ess rule that a! e&ployee ca! 'ork o! &ore
tha! o!e proHect. Also have rule that &ore tha! o!e e&ployee ca!
'ork o! each proHect. 2his bi!ary relatio!shi p is said to be 7a!yF toF
7a!y .
0o' itGs #oi!# to tur! out that )or relati o!shi ps that are 7a!yF toF 7a!y,
a table is !eeded i! the relatio!al &odel to represe!t the relatio!shi p.
But this is 012 al'ays true i) the relatio!shi p is !ot 7a!yF toF 7a!y.
Co!sider the relati o!shi p (also o! p#. **,: 8!structors teaches
CourseLsectio!s.
Say 'e have the rule that a! 8!structor ca! teach &ore tha! o!e
course sectio! (usuall y does, u!)ortu!atel y )or &e, but 'e &ake the
rule that o!l y o!e i!structor is associated 'i th every course sectio!.
2his &ea!s that i) there are t'o i!structors teachi !# a class, o!e o)
the t'o is actuall y respo!sible )or the course, a!d the tea& approach
is u!o) )icial.
0o' 'e k!o' )ro& tra!s)or&ati o! rule 1 that both e!ti ti es 8!structors
a!d CourseLsectio!s &ap to relatio!al tables.
(Dra' this, 'i th so&e attri butes: iid )or i!structors, csid )or
courseLsectio!s FF assu&e !o &ul tiF valued attri butes so these are o!ly
t'o tables.
0o' the questio! is, do 'e !eed a!other table )or the relati o!shi p
teaches.
A!s'er: 0o. <e ca! put a colu&! i! the courseLsectio!s table that
u!iquel y ide!ti )i es the i!structor teachi !# each ro' (i!sta!ce. 2his is
do!e 'i th a! iid colu&!.
F.,F
0ote that the iid colu&! i! the courseLsectio!s table is 012 a!
attri bute o) the CourseLsectio!s e!ti t y. 2he iid colu&! i!stead
represe!ts the teaches relatio!shi p .
8! relatio!al ter&i !ol o#y, this colu&! is k!o'! as a )orei#! key i! the
courseLsectio!s table (!ot a key )or courseLsectio!s but o!e )or the
)or ei#! table i!structors.
1P, 'hatG s the di))ere!ceM <hy did o!e relatio!shi p, 3&ployees
'orksLo! 6roHects requi re a table )or 'orksLo!, a!d the other,
8!structors teaches CourseLsectio!s, require !o !e' tableM
Because o!e relatio!shi p is 7a!yF toF 7a!y a!d the other is 7a!yF toF
1!eQ 2he 7a!yF toF 1!e relatio!shi p ca! be do!e 'ith a )orei#! key
because 'e o!ly !eed to ide!ti )y (at &ost 103 co!!ecti !# i!sta!ce
o! o!e side.
0ote these ideas are all B;S803SS R;43S. 2hey are i&posed by the
DBA )or all ti &e by the de)i!i ti o! o) the tables. <e shall see ho'
shortl y.
4ook at >i#ure ,.,. 3!tities 3 a!d >, relatio!shi p R. 4i!es bet'ee!
dots. Dots are e!ti t y i!sta!ces. 4i!es are relati o!shi p i!sta!ces.
8) all dots i! the e!ti t y 3 have A2 71S2 o!e li!e co&i!# out, 'e say:
&a(F card(3, R I 1.
8) &ore tha! o!e li!e out is possible, 'e say &a(F card(3, R I 0.
8) all dots i! the e!ti t y 3 have A2 43AS2 o!e li!e co&i!# out, 'e say:
&i!F card(3, R I 1.
8) so&e dots &i#ht !ot have a li!e co&i!# out, 'e say &i!F card(3, R
I /.
<e co&bi !e these, by sayi!# card(3, R I ((, y i) &i!F card(3, R I (
a!d &a(F card(3, R I y. (( is either / or 1 a!d y is either 1 or 0.
Go over >i#ure ,.E o! p# *$1. 0ote that )or recursi ve relatio!shi p
&a! a#es, i!clude role: card(e&pl oyees(reportsLto, &a!a#es I (/,
1
0ote that sayi!# &i!F card(3, R I / is reall y 012 7AP80G A
R3S2R8C2810. 2here &i#ht be !o li!es leavi!# a dot, there &i#ht be
o!e, or &ore (&i!F card 0353R says a!ythi !# about &a(i &u&
!u&ber.
F.EF
Sayi!# &a(F card(3, R I 0 is also !ot &aki !# a restricti o!. 2here
do!G t have to be a lot o) li!es leavi!# ? 0 &i#ht be Bero ? Hust sayi!#
'e are !ot restricti !# the &a(i &u& to o!e.
2he &ost restricti ve thi!# 'e ca! say is that card(3, R I (1, 1. 2he!
co&es (1, 0 a!d (/, 1. 2he! (/, 0, 'hich &ea!s !o restricti o!s.
<e ca! o!ly !ote )ro& a speci)ic e(a&pl e (co!te!t o) a #ive!
&o&e!t o) a relatio!shi p R 'i th re#ard to a! e!ti t y 3 i) a restricti o! is
BR1P30. %o' 'ould 'e !otice card(3, R I (/, 0M (Dra' it.
8) 'e had a situati o! that see&ed to say card(3, R I (1, 1, ca!G t
k!o' this 'ill co!ti !ue to hold i! )uture FF &ust k!o' desi#!erG s
i!te!ti o!.
A!other ter& used, De) ,.".", i) &a(F card(3, R I 1 the! 3 is said to
have si!#leF valued participati o! i! R. 8) 0, the! &ul tiF valued
partici pati o! .
De). ,.".*. 8) &i!F card(3, R I 1, 3 is said to have &a!datory
partici pati o! i! R, i) /, the! optio!al partici pati o! .
1!eFtoF 1!e (1F 1 relatio!shi p i) both e!ti ti es are si!#leF valued i! the
relatio!shi p (&a(F card co!cept o!l y. 7a!yF toF 7a!y (0F0 i) both
e!ti ti es are &ul tiF valued. 7a!yF toF 1!e (0F1 i) o!e e!ti t y is &ul tiF
valued a!d o!e is si!#le valued. Dra'.
Do !ot usuall y say 'hich side is 7a!y a!d 'hich 1!e by sayi!#
relatio!shi p is 7a!yF toF 1!e, 0F1. 7i#ht actuall y be 1!eFtoF 7a!y, but
do!G t use that ter&.
%1<353R FF the 7A0: side i! a 7a!yF toF 1!e relatio!shi p is the o!e
'i th si!#leF valued participati o!. (there are 7a!y o) these e!ti ti es
possibl y co!!ected to 1!e o) the e!ti ti es o! the other side.
1P, !o' a bu!ch o) 2ra!s)or&ati o! rules a!d e(a&pl es, starti !# o!
p#. *1/.
0F0 relatio!shi p al'ays tra!s)or&s to a table o) its o'!. 7a!y
8!structors teach 7a!y CourseLsectio!s. Direct )li#hts relate cities i!
3urope to cities i! the ;S. Ca!G t represe!t si&pl y: rich co&ple(
structure.
0F1 relatio!shi ps, ca! represe!t 'ith )orei#! key i! e!ti ty 'i th si!#le
valued participati o! (the 7a!y side.
F.-F
1F1. 8s it optio!al o! o!e side or is it &a!datory o! both sidesM
1ptio!al o! o!e side. 6ost&e! carry 7ailba#s. 3very post&a! carries
o!e a!d o!l y o!e &ailba#, a!d every &ailba# is carried by at &ost
o!e post &a!, but there &i#ht be so&e spares i! stock that are carried
by !o!e.
Represe!t as t'o tables, )orei#! key colu&! i! o!e 'ith &a!datory
partic ipatio!: colu&! de)i!ed to be 012 0;44. Ca! )ai th)ul l y
represe!t &a!da tory partici pati o!. Clearl y represe!ti !# si!#leF valued
partici pati o!.
(8dea o) )ai th)ul represe!tati o!: pro#ra&&er ca!G t break the rule eve!
i) 'ri tes pro#ra& 'i th bu#. 0ote ca! 012 )aith)ul l y represe!t si!#leF
value partici pati o! )or both &ailF ba#s A0D post&e!.
1F1 a!d 7a!datory o! both sides: !ever ca! break apart. 8tGs
appropriate to thi!k o) this as t'o e!ti ti es i! a si!#le table. 3.#.
couples o! a da!ce )loor FF !oFo!e 353R is co!sidered to be 'i thout a
part!er. Avoids )orei#! keys.
(Reall yM But &i#ht cha!#e part!ers a!d so&e i!)o &i#ht be speci)ic
to i!di vi duals o) part!ers F his hei#ht, a#e, 'ei#ht F her hei#ht, a#e,
'ei#ht.
2his lo#ical desi#! is &ore co!cer!ed 'i th !ot bei!# able to e!d up
'i th a &istake tha! &aki !# a tra!s)or&ati o! easy.
Sectio! ,.*, Addi tio!al 3FR co!cepts.
Attri butes ca! use idea o) cardi!ali t y as 'ell. See >i#ure ,.1/.
(/, y &ea!s do!G t have to say !ot !ull, (1, y &ea!s do.
((, 1 &ost co&&o!, si!#le valued attri bute, ((, 0 &ul tiF valued.
<eak e!ti ti es. A! e!ti ty that ca!G t e(ist u!less a!other e!ti ty e(ists.
Depe!ds )or its e(iste!ce a!d ide!ti )icati o! o! a!other e!ti ty.
3.#., 4i!eF ite&s o! a! 1rder. Custo&er places a! order, orders
several products at o!ce. 1rder has &ul ti pl e li!e ite&s. 4i!eLite&s is
a 'eak e!ti t y depe!de!t o! the e!ti t y 1rders. See >i#ure ,.11.
2here is a! 0F1 relatio!shi p, hasLite&, that relates o!e 1rders
i!sta!ce to &a!y 4i!eLite&s i!sta!ces.
F..F
2here)ore, by tra!s)or&ati o! rules, 4i!eLite&s se!t to table, 1rders
se!t to table, )orei#! key i! 7a!y side, li!eLite&s table.
0ote that the ide!ti )i er )or a 4i!eLite&s, li!e!o, is e!ou#h i! 3FR &odel
to ide!ti )y the 'eak e!ti t y, si!ce ca! #o back throu#h hasLite&
relatio!shi p to )i!d 'hat order it belo!#s to.
8! relatio!al &odel, &ust be ide!ti )i ed by colu&! value. 0ote ord!o is
!ot a! attri bute o) li!eLite&s but a )orei#! key, a!d li!e!o a!d ord!o
&ust be used to#ether as a key )or li!eLite&sQQ Q
1P, thi!k. <hatG s the di))ere!ce bet'ee! the t'o situati o!s:
1rders hasLite& 4i!eL8te&s a!d 3&ployees 'i th &ul tiF value attri bute
hobbiesM 7ap the sa&e 'ay i!to relati o!al tablesQ
6ossibl y very little di))ere!ce. 1!e &a!G s attri bute is a!other &a!G s
e!ti ty. 8) 8 cared about tracki!# all hobbies i! the co&pa!y so 8 could
provide 'ell thou#ht out rela(ati o! roo&s, &i#ht say hobbies are
e!ti ti es.
8! case o) li!e !u&ber, there are usuall y several attri butes i!vol ved,
li!e!o, product ordered, qua!ti t y o) product, cost, so see&s
reaso!able to say 4i!eLite&s is a! e!ti ty, albei t a 'eak o!e.
F1//F
Class !+.
Skip Ge!eraliBatio! %ierarchies )or !o' to #o o! to Case Study.
Si&ple airli!e reservati o! database, data ite&s 'e have to track: pas F
sa!#ers, )li#hts, departure #ates, seat assi#!&e!ts.
Could #et &uch &ore co&ple(: a )li#ht bri!#s to#ether a )li#ht cre',
a #rou!d cre', a! airpla!e, a set o) passe!#ers, a #ate. 2he #ates
have to be clea!ed a!d serviced, etc.
>or si&plici ty, say represe!t situati o! 'ith a )e' si&ple e!ti ti es:
3!tity >li#hts , pri &ary ide!ti )i er )li#ht!o , descripti ve attri bute
departLti &e (e.#., 0ov 1., .:*" 67. (0ote the airpla!e is assu&ed
#ive!.
3!tity 6asse!#ers , pri &ary ide!ti )i er ticket!o .
3!tity Seats, ide!ti )i ed by seat!o , valid o!l y )or a speci)ic )li#ht. %i!t,
Seats is a 'eak e!ti ty depe!di !# o! >li#hts. <e say the 7a!yF toF 1!e
relatio!shi p here is: >li#hts hasLseat Seats.
3!tity Gates, 'ith pri &ary ide!ti )i er #ate!o .
6asse!#ers &ust be assi#!ed to Seats, a!d this is by a relatio!shi p
seatLassi#! . Dra' belo' 'i thout relati !# Gates, >li#hts, 6asse!#ers.
Gates
has_seat Seats Flights
Passengers
seat_assign
marshalls
seatno flightno
ticketno
gateno
depart_time
dtime ddate
travels_on
>i#. ,.1*, p#. *+1 (but leave out &arshalls a!d travelsLo!, a!d !ote
Gates o)) by itsel). But clearl y passe!#ers #o to a #ate to &eet a
)li#ht.
F1/1F
8s this a ter!ary relati o!shi p, the!, relati !# these threeM <e say it is
!ot, because it is possible to set up t'o 0F1 bi!ary relatio!shi ps that
&ore )aith)ul l y represe!t the situati o!.
8 'ould!G t 'a!t to assi#! t'o #ates to o!e )li#ht, )or e(a&pl e, or t'o
)li#hts to o!e passe!#er. 2here)ore:
See >i#ure ,.1*, p#. *+1. &arshalls a!d travels o!. 2er!ery 'ould be
0F0F0.
0o' 'ork out cardi !ali ti es o) relatio!shi ps.
A #ate &i#ht !ot be used or it &i#ht be used )or &ul ti pl e )li#hts, so
have (/, 0 partici pati o! i! &arshalls. A )li#ht &ust have a #ate a!d
ca! have o!l y o!e #ate, so (1, 1.
A passe!#er &ust have a )li#ht a!d o!l y o!e, so (1, 1 partici pati o! i!
travelsLo!. A )li#ht &ust have a passe!#er (or it 'ill certai !l y be ca! F
celled a!d &ay (probabl y 'ill have &a!y, so (1, 0.
1thers are clear. 3ach passe!#er &ust have o!e a!d o!l y o!e seat, a
seat &ay or &ay !ot be used. 3ach seat is o! so&e )li#ht, but each
)li#ht has &ul ti pl e seats.
0o' tra!s)or& i!to relatio!al tables. 7ap e!ti ti es, see botto& o) p#.
*"1. Dra' o! board.
Si!ce seats is 'eak e!ti t y (si!#leF valued partici pati o! i! hasLseat,
add )li#ht!o to seats table. 0o' that relati o!shi p is take! care o).
6asse!#ers has si!#leF valued partici pati o! i! t'o relatio!shi ps, so add
seat!o a!d )li#ht!o to passe!#ers (seat!o a!d )li#ht!o are !eeded )or
seatLassi#!&e!t, a!d )li#ht!o is !eeded )or travelsLo!, so o!e
cou!ted t'ice. 0o' those relatio!shi ps are take! care o).
1!ly o!e le)t is Gates &arshalls >li#hts, a!d put #ate!o as )orei#! key
i! )li#hts table. Do!e.
See ho' three (1, 1 participati o!s ca! be )aith)ul l y represe!ted 'i th
!otF !ull )or seat!o a!d )li#ht!o i! passe!#ers, !ot !ull )or )li#ht!o i!
seats, !ot !ull )or #ate!o i! )li#hts.
Sectio! ,.+. 6reli &i!ari es )or 0or&aliBati o!.
F1/"F
8dea i! !or&al iBati o!, start 'ith data ite& !a&es (to be colu&!s i!
so&e tables to#ether 'i th a list o) rules o) related!ess. 2he! start
'i th all data ite&s i! o!e table (u!i versal table.
Rules o) related!ess a!d a desire to avoid certai ! types o) bad
behavi or (a!o&ali es causes us to )actor this bi# table i!to s&aller
tables, achiev i!# &ore a!d &ore restricti ve )or&s (0or&al >or&s.
10>, "0>, *0>, BC0>.
<ill !ot cover $0>, +0> ()reque!tl y !ot co!sidered i! co&&erci al use.
2he idea is that )ro& the )actored tables ca! al'ays #et back all the
ori#i!al data by Hoi!sC this is called a Olossless deco&posi ti o!O.
A seco!d desire 'e have is that the database syste& 'ill be able to
check the rules o) related!ess as si&pl y as possible (#e!erall y by
si&pl y e!)orc i!# u!ique!ess o) a colu&! i! a table.
6oi!t o) 0or&aliBati o! is that reach the sa&e desi#! as 'ith 3FR, but it
is &ore cut a!d dried, uses i!tui ti o! less (#ive! the set o) rules ? a
bi# #ive!Q. Both 3FR a!d 0or&aliBati o! have thei r poi!ts.
2o start. 10> &ea!s !o repeati !# )ields i! tablesC !o relatio!al
products allo' such repeati !# rules (7o!ta#e, !o' 8llustra, does,
sho'ever.
1P. Ru!!i!# 3(a&ple: 3&ployee 8!)or&ati o!. See p#. *+$.
3(plai! each: e&pLid, e&pL!a&e, e&pLpho!e, deptL!a&e,
deptLpho!e, deptL&#r!a&e skillLid, skillL!a&e, skillLdate, skillLlvl
Desi#! )ro& a! 3FR sta!dpoi !t is easy. 3!ti ti es 3&ps, Depts, Skills,
a!d relatio!shi p bet'ee! 3&ps a!d Skills, hasLskill, 0F0 so o'! table.
But letGs take !or&al iBati o! approach. Start 'i th ;!iversal tableC see
>i# ,.1,, e&pLi!)o, o! p# *++.
Bad desi#!. 2ur!s out key is e&pLid skillLid. %o' do 'e k!o' thatM
By rules o) related!ess. <hatGs the proble& 'ith thatM See )ollo'i !#.
A!o&alies . 2here is replicati o! o) e&ployee data o! di))ere!t ro's o)
e&pLi!)o, a!d this see&s u!!atural. But 'hy is that badM
Co!sider 'hat 'ould happe! i) skills had to be re!e'ed 'ith tests
(#ivi !# skillLlevel a!d so&e e&ployee 'as dilatory, lost last skill by
F1/*F
)aili!# to take a test. <ould )or#et about all e&ployee details 'he!
deleted last skill. Called D34323 A017A4:.
2he other )ace o) this proble&, ca!!ot add a !e' e&ployee, say a
trai !ee, u!til a skill )or that e&ployee e(ists. 80S3R2 A017A4:.
Also have ;6DA23 A017A4:, i) deptL!a&e cha!#es )or a! e&ployee,
&i#ht have to cha!#e a lot o) ro's. 0o proble& i) setF orie!ted update
update e&pLi!)o set deptL!a&e I :!e'dept 'here e&pLid I :eidC
e(cept that it &i#ht be i!e))icie!t to update a lot o) ro's. Recall i! caF
porders table 'he! qua!ti t y )or products cha!#ed.
JBut there &ay be a serious proble& i) 'e are i! the &iddle o) a
cursor )etch a!d !otice that the deptL!a&e value should cha!#e. <e
do!G t 'a!t to cha!#e so&e but !ot all o) the deptL!a&e values )or
this e&ployee.K
2he )act that 'e 'ill have to update o!e co!ceptual )act i! &a!y spots
is k!o'! as the ;6DA23 A017A4:.
<e Hu&p ahead to a solutio!: create e&pLi!)o database 'i th t'o
tables, e&ps a!d skills. (>actor tables. >i#ure ,.1E, p#. *+-.
e&ps table: e&pLid, e&pL!a&e, e&pLpho!e, deptL!a&e,
deptLpho!e, deptL&#r!a&e
skills table: skillLid, skillL!a&e, skillLdate, skillLlvl
e&ps table has key e&pLid (ca!G t lose i!)o about e&ployee i) !o skill
skills table has key e&pLid skillLid (skillLlvl deter&i !ed by both.
4ossless Hoi!. Get back i! Hoi! o) skills a!d e&ps e(actl y 'hat 'e had
i! e&pLi!)o. 0ot do!e thou#h.
%o' ca! 'e prove all thisM 0eed a 412 o) &achi!ery, very
&athe&ati cal.
F1/$F
Class !,.
Sectio! ,.,. >u!ctio!al Depe!de!cies.
Re&e&ber 'hat it &ea!t to say that the ide!ti )ier pid is a key
(ide!ti )i er )or product i!)or&ati o! i! the CA61RD3RS tableM
2he pid value is repeated &a!y ti &es, so !ot u!ique as a key. But
still, pid u!iquel y deter &i!es qua!ti t y a!d p!a&e a!d pcity Dc. say
pid FT qua!ti t y.
De) ,.,.1 . Give! a table 2 'i th at least t'o attri butes A a!d B, 'e say
that A FT B (A )u!cti o!all y deter&i !es B, or B is )u!cti o!al l y
depe!de!t o! A i)) it is the i!te!t o) the desi#!er that )or a!y set o)
ro's that &i#ht e(ist i! the table, t'o ro's i! 2 ca!!ot a#ree o! A
a!d disa#ree o! B.
7ore )or&all y, #ive! t'o ro's r
1
a!d r
"
i! 2, i) r
1
(A I r
"
(A the! r
1
(B
I r
"
(B.
8dea o) )u!cti o! i! calculus ? #raphs. See itGs the sa&e, do&ai! FT
ra!#e.
3(. ,.,.1. i! e&pLi!)o table: e&pLid FT e&pL!a&e, e&pLid FT
e&pLpho!e, e&pLid FT deptL!a&e.
2hi!k 'hat this &ea!s i! 3FR ter&s. Co!sider your i!tuti o! F i) t'o
ro's 'i th sa&e e&pLid a!d di))ere!t e&pLpho!e, assu&e data
corrupted, but i) sa&e e&pLpho!e a!d di))ere!t e&pLid say O1h, so
e&ployees ca! share a pho!e.O 2he! 'e say, e&pLpho!e FNFT
e&pLid
De). ,.,." . 1P, have 'he! o!e attri bute A )u!ctio!al l y deter&i !es
a!other B. 0o' sets o) attri butes: 9 I A
1
A
"
. . . A
k
, a!d : I B
1
B
"
. . .
B
&
. Say 9 FT : i)) it is the i!te!ti o! o) the desi#!er that t'o ro's
ca!!ot si &ul ta!eously a#ree o! 9 a!d disa#ree o! :.
Sa&e 'ordi !# as De). ,.,.1, but !ote that )or a set 9, a#rees i) a!d
o!l y i) a#rees o! A44 colu&! value, disa#rees i) disa#rees o! A0:
colu&! value.
3(. ,.,.*. <e clai& 'hat )ollo's is all >Ds o) e&pLi!)o. 8!terpret by
3FR.
(1 e&pLid FT e&pL!a&e e&pLpho!e deptL!a&e
F1/+F
(" deptL!a&e FT deptLpho!e deptL&#r!a&e
(* skillLid FT skillL!a&e
($ e&pLid skillLid FT skillLdate skillLlvl
0ote that i) 'e k!o' e&pLid FT e&pL!a&e, e&pLid FT e&pLpho!e,
a!d e&pLid FT deptL!a&e, the! k!o' e&pLid FT e&pL!a&e
e&pLpho!e deptL!a&e. 3asy to see by de)i!i ti o!, but D13S require
thi!ki !# about de)i!i ti o!. 2hree )acts about si!#leto! attri bute >Ds
lead to 9 FT : )act.
0ote that 'e ca! co!clude )ro& above that desi#!er does !ot i!te!d
that skillL!a&e should be u!ique )or a particul ar skill. skillL!a&e FT
skillLid is !ot there, !or is it i&plied by this set (!o skillL!a&e o! le)t.
0ote that a set o) >Ds has lo#ical i&plicati o!s to derive 12%3R >Ds.
3.#., e&pLid FT e&pL!a&e e&pLpho!e deptL!a&e above. 2here are
R;43S )or ho' to deri ve so&e >Ds )ro& others. 2he si&plest o!e
)illo's.
2heore& ,.,.* . 8!clusio! rule. Give! 2 'ith %ead(2. 8) 9 a!d : are
sets i! %ead(2 a!d : 9, the! 9 FT : ()or A0: co!te!t )or 2. (5e!!
Dia#ra&.
6roo). By de), !eed o!ly de&o!strate that i) t'o ro's u a!d v a#ree
o! 9 they &ust a#ree o! :. But : is a subset o) 9, so see&s obvious.
De). ,.,.$ . A tri vi al depe!de!cy is a! >D o) the )or& 9 FT : that holds
)or a!y table 2 'here 9 ;0810 : %ead(2. (Assu&e A0: co!te!t )or
2.
2heore& ,.,.+ . Give! a tri vi al depe!de!cy 9 FT : i! 2, it &ust be the
case that : 9. (5e!! Dia#ra&, 9, : disHoi!t, sho' A.
6roo). Create a table 2 'i th %ead(2 I 9 ;0810 : a!d co!sider the
attri butes i! : F 9. Assu&e it is !o!F e&pt y (so it is )alse that : 9
a!d 'e )i!d a co!tradicti o!. 4et A be a! attri bute i! : F 9. A tri vi al
depe!de!cy &ust hold )or a!y possible co!te!t o) 2. But si!ce A is !ot
i! 9, it is possible to co!struct t'o ro's u a!d v i! 2 alike i! all values
)or 9 but havi!# di))er e!t values i! A. 2he! 9 FT : does !ot hold )or
this co!structed co!te!ts, i! co!tradicti o! to its tri vi ali t y.
De). ,.,.,. Ar&stro!#G s A(io&s . >ro& the )ollo'i !# s&all set o) basic
rules o) i&plicati o! a&o!# >Ds, 'e ca! derive all others that are true.
J1K 8!clusio! rule: i) : 9, the! 9 FT :
F1/,F
J"K 2ra!siti vi t y rule: i) 9 FT : a!d : FT V, the! 9 FT V
J*K Au#&e!tati o! rule: i) 9 FT :, the! 9 V FT : V
(LE%.E 4P: 0ote 9 V )or sets is the sa&e as 9 ;0810 V.
7ost o) these rules are like rules about ho' to operate i! al#ebra:
i) (y I (B a!d ( a /, the! y I B.
1r Geo&etry: i) t'o tria!#l es ABC a!d 9:V have side AB equal to side
9:, side BC equal to :V a!d a!#le B equal to a!#le :, the! the t'o
tria!#l es are co!#rue!t.
Re&e&ber, )ro& 3uclidGs 11 a(io&s ca! deri ve all other true )acts.
Ar&stro!#G s a(io&s are like that. 2hese a(io&s are said to be
C1764323, &ea!i !# that !o other a(io&s ca! be added to i!crease
thei r e))ecti ve!ess.
3verythi !# 'e ca! say about >Ds is i&plied by these a(io&s.
4etGs prove o!e o) these a(io&s. <e 'ill be appeali !# to de)i!i ti o!s o)
>u!ctio!al Depe!de!cy here to prove a! Ar&stro!# A(io&.
6rove Au#&e!tati o! Rule. 8) 9 FT : the! 9 V FT : V. Co!sider t'o
ro's u a!d v that a#ree o! 9 V, that is they a#ree o! all attri butes i! 9
;0810 V. 2he! si!ce u a!d v a#ree o! all attri butes o) 9 they a#ree
o! all attri butes o) : (si!ce 9 FT :. Si&ilarl y, si!ce it is #ive! that
they a#ree o! all attri butes o) V a!d 'e have Hust sho'! they a#ree
o! all attri butes o) :, they a#ree o! all attri butes o) : ;0810 V. 2hus,
)ro& the )act that u a!d v a#ree o! all attri butes o) 9 ;0810 V 'e ca!
co!clude that they also a#ree o! all attri butes o) : ;0810 V, a!d by
de)i!i ti o! 'e have 9 V FT : V.
So&e i&plicati o!s o) Ar&stro!#G s A(io&s. p#. **,. 4ook atQ 2he idea
here is that 'e stop appeali!# to the de)i!i ti o!s a!d use o!l y the
A(io&s.
(1 4nion /uleC 8) 9 > : a!d 9 > V the! 9 > : V
(" Decomposi tion /uleC 8) 9 > : V the! 9 > : a!d 9 > V
(* Pseudotransi ti 5i t y /uleC 8) 9 > : a!d < : > V the! 9 < >
V
($ %ccumul ati on /uleC 8) 9 > : V a!d V > B < the! 9 > : V B
0123: 8) you have a! old e!ou#h te(t, proo) o) Accu&ul ati o! Rule is
'ro!#Q 4etGs build up to it. 1!e 3(a&pl e:
F1/EF
J1K ;!io! Rule: 8) 9 FT : a!d 9 FT V, the! 9 FT : V.
6roo): <e have (a 9 FT : a!d (b 9 FT V. By Ar&stro!#G s
Au#&e!tati o! rule a!d (a, 'e have (c 9 9 FT 9 :. But 9 9 is 9 ;0810
9 I 9, so (c ca! be re'ri tte! (d 9 FT 9 :. 0o' by (b a!d
au#&e!tati o!, 'e have (e 9 : FT : V. A!d by (d a!d (e a!d
tra!si ti vi t y, 'e have 9 FT : V, the desired resul t. J"K D J$K 6roved i!
te(t.
2he idea is that 'e ca! use these !e' Rules as i) they 'ere a(io&s, as
)acts to prove yet other rules or special cases. 4ike provi !# theore&
i! Geo&etry, ca! the! use to prove other theore&s.
8! 'hat )ollo's, 'he! 'e list a set o) >Ds, 'e !or&all y try to list a
78087A4 set, so that a s&aller set does!G t e(ist that 'ill i&pl y these .
8t 'ill tur! out that )i!di !# a &i!i &al set o) >Ds is very i&porta!t i!
)i!di!# the ri#ht relati o!al desi#! by 0or&aliBati o!.
3(a&ple ,.,.$ . 6#. **-. Co!sider the table 2 belo' )i(ed i! co!te!t
)or all ti &e so the i!te!ded >Ds ca! be read o)) (53R: ;0;S;A4.
4etGs try to list a &i!i &al set o) >Ds.
2able 2
ro' = A B C D
1 a1 b1 c1 d1
" a1 b1 c" d"
* a" b1 c1 d*
$ a" b1 c* d$
%nalysis . Start by co!sideri !# >Ds 'i th a si!#le attri bute o! the le)t.
Al'ays have the tri vial >Ds, A > A, B > B, C > C, a!d D > D, but do!G t
list tri vi al >Ds i! a &i!i &al set.
(a All values o) the B attri bute are the sa&e, so it ca! !ever happe! )or
a!y other at tribute 6 (i.e., 'here 6 represe!ts A, C, or D that r
1
(6 I
r
"
(6 'hile r
1
(B a r
"
(BC thus 'e see that A > B, C > B, a!d D > B.
At the sa&e ti &e !o other attri bute 6 is )u!ctio!al l y depe!de!t o! B
si!ce they all have at least t'o disti !ct values, a!d so there are al 'ays
t'o ro's r
1
a!d r
"
such that r
1
(6 a r
"
(6 'hile r
1
(B I r
"
(BC thus: B FNFT
A, B FNFT C, a!d B FNFT D.
F1/-F
(b Because the D values are all di))ere!t, i! addi tio! to D > B o) part
(a, 'e also have D > A a!d D > C. <e state: a P3: (D )u!cti o!all y
deter&i !es everythi !# else, 'hich 'ill tur! out to be the poi!t.
At the sa&e ti &e D is !ot )u!c tio!all y depe!de!t o! a!ythi !# else si!ce
all other attri butes have at least t'o duplicate values, so i! addi tio! to B
FNFT D o) part (a, 'e have A FNFT D, a!d C FNFT D.
4ist all belo' 'ithout A to C a!d C to A a!d sho' ho' 'e k!o'.
(c <e have A FNFT C (because o) ro's 1 a!d " a!d C FNFT A (because o)
ro's 1 a!d *. 2here)ore, 'e ca! list all >Ds (a!d )ailed >Ds 'ith a
si!#le attri bute o! the le)t ('e provi de a letter i! pare!theses keyed to
the para#raph above that #ive us each )act.
(a A > B (a B FNFT A (c C FNFT A (b D > A
(c A FNFT C (a B FNFT C (a C > B (a D > B
(b A FNFT D (a B FNFT D (b C FNFT D (b D > C
By the u!io! rule, 'he!ever a si!#le attri bute o! the le)t )u!ctio!all y
deter&i !es several other at tributes, as 'i th D above, 'e ca! co&bi!e
the attri butes o! the ri#ht: D > A B C. >ro& the a!al ysis so )ar, 'e have
the )ollo'i !# set o) >Ds ('hich 'e believe to be &i!i &al :
(1 A > B, (" C > B, (* D > A B C
(<hat is the key )or this tableM 0ote, reall y D FT A B C D
0o' co!sider >Ds 'ith pairs o) attri butes o! the le)t. (d A!y pair
co!tai !i !# D deter&i !es all other attri butes, by >D (* above a!d the
au#&e!tati o! rule, so there is !o !e' >D 'ith D o! the le)t that is !ot
already i&plied.
(e 2he attri bute B co&bi !ed 'ith a!y other at tribute 6 o! the le)t, still
)u!cti o!all y deter&i !es o!ly those attri butes already deter&i !ed by 6, as
'e see by the )ollo'i !# ar#u&e!t. 8) 6 FNFT @ this &ea!s there are ro's
r
1
a!d r
"
such that r
1
(@ a r
"
(@ 'hile r
1
(6 I r
"
(6. But because B has
equal values o! all ro's, 'e k!o' that r
1
(B 6 I r
"
(B 6 as 'ell, so B 6
FNFT @. 2hus 'e #et !o !e' >Ds 'ith B o! the le)t.
() 0o' the o!l y pair o) attri butes that does !ot co!tai ! B or D is A C, a!d
si!ce A C has disti!ct values o! each ro' (e(a&i !e table 2 a#ai!Q, 'e
k!o' that A C > A B C D. 2his is !e', but is it &i!i &alM
F1/.F
8t is tri vi al that A C > A, a!d AC > C, a!d 'e already k!e' that A > B, so
it is easy to sho' that A C > B. (A C > B C by au#&e!tati o!, B C > B
by i!clusio!, so A C > B by tra!si ti v ity. 2hus the o!ly !e' )act 'e #et
)ro& seei!# that A C > A B C D is that A C > D.
0o' co!sider looki!# )or >Ds 'i th triples o) attri butes o! the le)t. A!y
triple that does !ot co!tai ! D ('hich 'ould assure that it )u!cti o!all y
deter&i !e all other attri butes &ust co! tai! A C (a!d there)ore )u!c F
tio!all y deter&i !es all other attri butes. So everythi !# 'ith three
attri butes o! the le)t is already ha!dled. Clearl y the sa&e holds )or a!y
set o) )our attri butes o! the le)t.
2he co&plete set o) >Ds i&plici t i! the table 2 is there)ore the )ollo' i!#:
(1 A > B, (" C > B, (* D > A B C, ($ A C > D.
2he )irst three >Ds co&e )ro& the earlier list o) >Ds 'i th si!#le at tributes
o! the le)t, 'hile the last >D, A C > D, is the !e' o!e #e!er ated 'ith t'o
attri butes listed o! the le)t. 8t 'ill tur! out that this set o) >Ds is !ot quite
&i!i &al , despi te all our e))orts to derive a &i!i &al set. <e 'ill see this
a)ter 'e have had a cha!ce to better de)i!e 'hat 'e &ea! by a &i! i&al
set o) >Ds. 2his is 'hy 'e !eed ri#or.
F11/F
Class !;.
Revie' e!d o) last ti &e, )i!ished 3(a&ple ,.,.$, p# *,+. Got >Ds:
(1 A > B, (" C > B, (* D > A B C, ($ A C > D.
Rather startli !# )act that this is 012 a &i!i &al set, althou#h 'e tried
very hard. A!ybody see 'hyM Solved 3(ercise ,.1$ (a, #et (reduced
set:
(1 A > B, (" C > B, (* D > A C, ($ A C > D
3verybody see 'hy itGs a reduced setM (4ess is said.
A!ybody see 'hy the reduced set i&plies the ori#i!al setM (* D > A
C is equival e!t (by deco&posi ti o! a!d u!io! to (a D > A a!d (b D
> C. >urther&ore, si!ce (a D > A a!d (1 A > B, by tra!si ti vi t y (c D
> B. 0o' by (* a!d u!io!, D > A B C.
But ho' did 'e arri ve at this reduced setM Do 'e al'ays have to thi!k
super hard about itM 0o. 2here is a! al#ori th& to )i!d the &i!i &al set
o) >Ds )or 0or&aliBati o!. 0eed &ore de)i!i ti o!s.
De) ,.,... Closure o) a set o) >Ds. Give! a set > o) >Ds o! attri butes
o) a table 2, 'e de)i!e the C41S;R3 o) >, sy&boliBed by >
\
, to be the
set o) all >Ds i&plied by >.
3.#., i! above, D > A B C 'as i! the closure o) the reduced set o) >Ds.
2he 78087A4 set o) >Ds is 'hat 'e 'a!t, so take out i&plied o!esQ
2here ca! be a 412 o) >Ds i! a closure. 3(. ,.,.+, >ro& > I RA FT B, B
FT C, C FT D, D FT 3, 3 FT >, > FT G, G FT %S, 'e ca! add tri vial
depe!de!ci es A FT A, B FT B, etc., a!d by tra!si ti vi t y a!d u!io! #et A
FT A B, A FT A B C, . . .
8! )act a!y letter FT a!y subset o) letters equal a!d to itGs ri#ht i! the
al phabet. C FT C D 3 > , C FT D >, B FT C D > G.
8! )act, i) 'e !a&e a!y t'o sets o) letters, o!e o) the& has a! earliest
letter i! the alphabet a!d FT the other (&aybe they both have that
letter a!d FT each other.
So the !u&ber o) i&plied >Ds is the !u&ber o) subsets o) let ters
squared, about ("
!

"
I "
"!
. 3.#., )ro& A FT B, B FT C, . . . , AFT P, 1/
F111F
>Ds, #et "
"/
>Ds, about a &illio!. 8) #o up to . . . , 9 > :, : > V, #et
"
+"
>Ds, about $,///,///,///,///,/// >Ds ()our quadrillio!.
4ots. 3(po!e!ti al e(plosio! i! !u&ber o) attri butes. 3ve! i) start 'i th
a &a!a#eabl e !u&ber 'i th co&&o!se!se rules, ca! #et proble&.
Really o!l y 'a!t to arrive at a 78087A4 set o) >Ds, so 'e try to avoid
this ki!d o) e(plosio!. Still 'e !eed all these co!cepts.
De). ,.,.1/. >D Set Cover . A set > o) >Ds o! a table 2 is said to
C153R a!other set G o) >Ds o! 2 i) the set G ca! be deri ved by
i&plicati o! rules )ro& the set >, i.e., i) G >
\
. 8) > covers G a!d G
covers >, 'e say the t'o sets o) >Ds are equi vale!t, > G.
3(. ,.,.,. > I RB FT C D, A D FT 3, B FT AS a!d
G I RB FT C D 3, B FT A B C, A D FT 3S.
De&ostrate i! book ho' > covers G. But also G covers >. See 'hyM B
FT C D 3 i&plies B FT C D a!d B FT 3 by deco&posi ti o! rule. So have
)irst >D i! >.
A better behaved de)i!i ti o! )ro& a sta!dpoi !t o) e(plosio! to
characteriBe a set o) >Ds is the )ollo'i !#.
De). ,.,.11. Closure o) a set o) attri butes . Give! a set 9 o) attri butes
i! a table 2 a!d a set > o) >Ds o! 2, 'e de)i!e the C41S;R3 o) the set
9 (u!der >, de!oted by 9
\
, as the lar#est set o) attri butes : such that
9 FT : is i! >
\
.
<e 'ill study closure o) set o) ATTRIBUTES, !ot closure o) set o) FDs.
Al#ori th& to deter&i !e set closure, p#. *$". 6retty i!tui ti ve: Start
'i th 9 I 9
\
a!d Hust keep loopi!# throu#h the (hope)ull y s&all set o)
>Ds as lo!# as !e' attri butes ca! be added to 9
\
.
6#. *$1F $". %lgori tm ).). !". -et Closure. Al#ori th& to deter&i !e 9
\
,
the closure o) a #ive! set o) attri butes 9, u!der a #ive! set > o) >Ds.
% = &; '"&# = '; (* integer %, attr. set '"&#
*(
)*+*,- (* loop to find larger '"%#
*(
F11"F
% = % . /; (* new %
*(
'"%# = '"%0/#; (* initiali1e new '"%#
*(
23) ,44 5 > 6 in 2 (* loop on all 27s 5 > 6 in 2
*(
%2 5 '"%# (* if 5 contained in '"%#
*(
-8*N '"%# = '"%# 6; (* add attrib9tes in 6 to '"%#
*(
*N7 23) (* end loop on 27s
*(
:N-%4 '"%# = '"%0/#; (* loop tillno new attrib9tes
*(
)*-:)N '
. =
'"%#
; (* ret9rn clos9re of '
*(
0ote that the step i! Al#ori th& + ,.1" that adds attri butes to 9J8K is
based o! a si&ple i!)er e!ce rule that 'e call the Set Accumulati on !ule,
stated thus: 8) 9 > : V a!d V > < the! 9 > : V <.
8! our al#ori th& 'e are sayi!# that si!ce 9 > 9J8K (our i!ducti ve asF
su&pti o! a!d 9J8K ca! be represe!ted as : V (because V 9J8K, 'e ca!
'ri te 9 > 9J8K as 9 > : V, a!d si!ce > co!tai!s the >D V > <, 'e co!F
clude by the set accu&ul ati o! rule that 9 > : V < or i! other 'ords 9 >
9J8K ;0810 <.
E:ample ).). + . 8! 3(a&ple ,.,.,, 'e 'ere #ive! the set > o) >Ds:
> I RB > C D, A D > 3, B > AS (Get B\ I B C D (1 A (* 3 ("
(Shortha!d (1, (": 1RD3R 8761R2A02
Give! 9 I B, 'e deter&i !e that 9
\
I A B C D 3. 8! ter&s o) Al#ori th&
,.,.1", 'e start 'i th 9J/K I B. 2he! 9J1K I B, a!d 'e be#i! to loop
throu#h the >Ds. Because o) B > C D, 9J1K I B C D.
2he !e(t >D, A D > 3, does !ot appl y at this ti &e, si!ce A D is !ot a
subset o) 9J1K. 0e(t because o) B > A, 'e #et 9J1K > A B C D.
0o' 9J/K is strictl y co!tai!ed i! 9J1K (i.e., 9J8F1K 9J8K so 9J8F1K a 9J8K.
2hus 'e have &ade pro#ress i! this last pass o) the loop a!d #o o! to a
!e' pass, setti !# 9J"K I 9J1K I A B C D.
F11*F
4oopi!# throu#h the >Ds a#ai!, 'e see that all o) the& ca! be applied
('e could skip the o!es that have bee! applied be)ore si!ce they 'ill
have !o !e' e))ect, 'ith the o!l y !e' >D, A D > 3, #ivi!# us 9J"K I A B
C D 3.
At the e!d o) this loop, the al #ori th& !otes that 9J1K 9J"K, pro#ress
has bee! &ade, so 'e #o o! to create 9J*K a!d loop thou#h the >Ds
a#ai!, e!di!# up this pass 'i th 9J*K I 9J"K.
Si!ce all o) the >Ds had bee! applied already, 'e could o&i t this pass by
!oti !# this )act. 0ote that a di))ere!t 1RD3R80G o) the >Ds i! > ca!
cha!#e the details o) e(e cutio! )or this al#ori th&.
7inding a Minimal Co5er of a set of 7Ds .
Al#ori th& ,.,.1*, p#. *,.. 3%-1C 2O NO/M%L1 L%21 ON D Give! a set
> o) >Ds, co!struct a set 7 o) >Ds that is &i!i &al a!d covers >.
4etGs appl y this to the (!o!F reduced set o) >Ds above.
>: (1 A > B, (" C > B, (* D > A B C, ($ A C > D
^ Re&e&ber, this ca! co&e outQQ
Step 1, >ro& the set > o) >Ds, create a! equi vale!t set % 'ith o!ly
si!#le >Ds o! the ri#ht. ;se deco&posi ti o! rule. See step 1, p#. *$*.
%: (1 A > B, (" C > B, (* D > A, ($ D > B, (+ D > C, (, A C >
D
Step ". Re&ove i!esse!ti al >Ds )ro& the set % to #et the set A.
Deter&i !e i!esse!ti al 9 > A i) A is i! 9
\
u!der >Ds 'ithout 9 FT A.
2ry re&ovi !# (1 A > B, leavi !# o!l y (" C > B, (* D > A, ($ D > B,
(+ D > C, (, A C > D. 2ake 9 I A i! closure al#ori th&, clearl y #et
o!l y 9
\
I A, because !o other >D has its le)t side co!tai !ed i! 9. So
!eed (1.
2ry re&ovi !# others. (" stays, si!ce !o other C o! le)t. 2here)ore i)
set 9 I C could!G t #et 9\ to co!tai ! &ore tha! C. (2hat reaso!i!# is
1P.
%o' about (*M <ould be le)t 'ith o!ly: (1 A > B, (" C > B, ($ D >
B, (+ D > C, (, A C > D. Co!sider 9 I D. Get 9
\
I D B ($ C (+.
2he! stop. 8! )act A !ot o! ri#ht o) a!y >Ds i) take out (*, so (*
!eeded.
F11$F
0o' try re&ovi !# ($. Peep: (1 A > B, (" C > B, (* D > A, (+ D >
C, (, A C > D. Ca! 'e derive D FT BM 9 I D. Get: D A (* C (+ B
(". 2here)ore, #ot D FT B. So ca! leave ($ out .
Ca!G t leave out (+ because C !ot o! ri#ht o) a!y other >D. Ca!G t
leave out (, because D !ot o! ri#ht o) a!y other. 2here)ore ca! o!l y
reduce set to:
% I (1 A > B, (" C > B, (* D > A, ($ D > C, (+ A C > D
(Re!u&ber
Step *. Successivel y replace >Ds i! % 'ith >Ds that have a s&aller
!u&ber o) >Ds o! the le)tF ha!d side so lo!# as %
\
re&ai !s the sa&e.
2est this by successivel y re&ovi !# si!#le attri butes )ro& &ul tiF
attri bute le)t ha!d sides o) >Ds, cha!#i !# 9 > A to : > A, the!
checki!# i) :
\
u!der !e' >D set is u!cha!#ed.
(Clearl y i) 'e assu&e : > A, a!d : 9, ca! derive everythi !# used to
be able to: still true that 9 > A. 104: R8SP is that : > A &i#ht i&pl y
211 7;C%. 8.e., &i#ht have :
\
is 4ARG3R tha! be)oreQ
1!ly o!e to try is (+. 8) 'e cha!#e this to A > D, does D cha!#eM
used to be A
\
I A B, !o', A
\
I A B D C. 0o #ood. %o' about
cha!#i !# A C > D to C > DM Does C
\
cha!#eM ;sed to be C
\
I C B.
0o' C
\
I C B D A. So !o #ood, ca!G t reduce. (01 033D 21 2R: S236
" AGA80.
8> <3 D8D R3D;C3 a!d created a !e' >D, let us say A FT D to replace
A C FT D, 'e 'ould !eed to appl y Step " a#ai! to test i) A FT D could
be re&ovedQQ Q
Step $. Apply ;!io! rules to bri!# thi!#s back to#ether o! the ri#ht
)or co&&o! sets o) attri butes o! the le)t o) >Ds, re!a&ed 7.
%: (1 A > B, (" C > B, (* D > A, ($ D > C, (+ A C > D
7: (1 A > B, (" C > B, (* D > A C, ($ A C > D
2his is the reduced set, above.
1P, !o' have al#ori th& to )i!d a &i!i &al cover )ro& a!y set o) >Ds.
Al&ost ready to do 0or&aliBati o!. But !eed o!e &ore co!cept.
F11+F
Sectio! ,.E. 4ossy a!d 4ossless deco&posi ti o!. <eGre #oi!# to be
)actori !# tables i!to s&aller tables (proHecti !# o!to t'o subsets o)
colu&!s that cover all colu&!s a!d have so&e colu&!s i! co&&o!,
but it does!G t al 'ays 'ork 'he! Hoi! back that keep all i!)or&ati o! o)
ori#i!al table.
Al'ays #et A44 ro's back, but &i#ht #et 71R3. 4ose 8!)or&ati o! .
See 3(a&ple ,.E.1 i! te(t, 6#. *E$, a 4ossy deco&posi ti o!.
E: ).+. !. % Lossy Decomposi tion . Co!sider table, ABC:
2able ABC
A B C
a1 1// c1
a" "// c"
a* *// c*
a$ "// c$
8) 'e )actor this table i!to t'o parts, AB a!d BC, 'e #et the )ollo'i !#
table co!te!ts:
2able AB 2able BC
A B B C
a1 1// 1// c1
a" "// "// c"
a* *// *// c*
a$ "// "// c$
%o'ever, the resul t o) Hoi!i!# these t'o tables is
AB A180 BC
A B C
a1 1// c1
a" "// c"
a" "// c$
a* *// c*
a$ "// c"
a$ "// c$
2his is 012 the ori#i!al table co!te!t )or ABCQ 0ote that the sa&e
deco&posed tables AB a!d BC 'ould have arise! i) the table 'e had
started 'i th 'as ABC9, 'i th co!te!t equal to AB A180 BC above, or either
o) t'o other tables, ABC: or ABCV:
F11,F
ABC: ABCV
A B C A B C
a1 1// c1 a1 1// c1
a" "// c" a" "// c"
a" "// c$ a* *// c*
a* *// c* a$ "// c"
a$ "// c$ a$ "// c$
Si!ce 'e ca!G t tell 'hat table co!te!t 'e started )ro&, i!)or&ati o! has
bee! lost by this deco&posi ti o! a!d the subseque!t Hoi!.
2his is k!o'! as a "ossy #ecomposi ti on , or so&eti &es a "ossy$ %oin
#ecomposi ti on .
Reaso! 'e lose i!)or&ati o! is &a!y to &a!y &atchi !# o! Hoi!
colu&!s. 4ose 'hich o!e o! the le)t 'as 'ith 'hich o!e o! the ri#ht.
3.#. a", "// o! le)t a!d a$, "// o! le)t &atch 'i th "//, c" a!d "//,
c$ o! ri#ht.
<ould be 1P i) al'ays had 0F1 relati o!shi ps o! Hoi! colu&!s (or 1F1.
3.#., CA6, orders ca! have &ul ti pl e ro's 'i th sa&e pid, but pid is
u!ique )or products. 7ust A4<A:S be u!ique o! o!e side, !ot a!
accide!t, so !eed Hoi! colu&!s to be S;63RP3: o! at least 103 S8D3
o) Hoi!.
2heore& ,.E.*. Give! a table 2 'i th a set o) >Ds > a!d a set o)
attri butes 9 i! %ead(2 the! 9 is a superkey o) 2 i)) 9 )u!cti o!al l y
deter&i !es all attri butes i! 2, i.e., 9 FT %ead(2 is i! >
\
.
2heore& ,.$.E. Give! a table 2 'i th a set > o) >Ds valid o! 2, the! a
deco&posi ti o! o) 2 i!to t'o tables R2
1
, 2
"
S is a lossless deco&posi ti o!
i) o!e o) the )ollo'i !# )u!cti o!al depe!de!ci es is i&plied by >:
(1 %ead(2
1
%ead(2
"
FT %ead(2
"
, or
(" %ead(2
1
%ead(2
"
FT %ead(2
"

2his is proved i! the te(t, 'ill leave u!dersta!di !# to you.


01R7A4 >1R7S 0392 2873, R3AD A%3AD. 2hrou#h ,.- o! 3(a& ".
F11EF
Class "I.
Sectio! ,.-. 0or&al >or&s
1P, 'hat is the poi!t o) !or&aliBati o!M <e start 'ith a ;!iversal table
2 a!d a set o) >Ds that hold o! 2. <e the! create a lossless
deco&posti o!:
2 I 2
1
A180 2
"
A180 . . . A180 2
!
so that i! each o) the tables 2
i
the a!o&alies 'e studied earlier !ot
prese!t. Co!sider the )ollo'i !# ;!iversal table called e&pLi!)o:
e&pLid, e&pL!a&e, e&pLpho!e, deptL!a&e, deptLpho!e,
deptL&#r!a&e, skillLid, skillL!a&e, skillLdate, skillLlvl
(1 e&pLid FT e&pL!a&e e&pLpho!e deptL!a&e
(" deptL!a&e FT deptLpho!e deptL&#r!a&e
(* skillLid FT skillL!a&e
($ e&pLid skillLid FT skillLdate skillLlvl
8! 'hat )ollo's, 'e 'ill )actor e&pLi!)o i!to s&aller tables 'hich )or&
a lossless Hoi!. A set o) table headi!#s (deco&posi ti o! tables
to#ether 'ith a set o) >Ds o! those headi!# attri butes is called a
Database Sche&a .
0ote that the >Ds (1F ($ above are the ki!d that de)i!e a key )or a
table. 8) the attri butes o! the le)t a!d ri#ht ha!d side o) >D (1 are the
o!l y o!es i! the table, the! e&pLid is a key )or that table. (;!ique, so
pri &ary key.
8t is i&porta!t that the >Ds i! our list )or& a &i!i &al cover. 3.#., i) i!
above cha!#ed (1 to i!clude deptLpho!e o! the ri#ht, 'ould have a
prob le& 'ith approach to co&e.
1P, !o' 'a!t to )actor 2 i!to s&aller tables to avoid a!o&alies o)
repre se!tati o!.
(Ca! co!ce!trate o! delete a!o&al y ? i) delete last ro' i! so&e
table, do!G t 'a!t to lose descripti ve attri bute values describi !# a!
e!ti t y i!sta!ce.
3(a&ple, re&e&ber CA61RD3RS, i) delete last order )or so&e
product, do!G t 'a!t to lose qua!ti t y o! ha!d )or that product.
F11-F
8! e&pLi!)o, i) delete last e&ployee 'i th #ive! skill, lose i!)or&ati o!
about that skill (i! this case, the !a&e o) the skill. So have to have
separate table )or skills.
<hat 'e are reall y sayi!# here is that e&pLid skillLid is the key )or
e&pLi!)o, but a S;BS32 o) this key )u!cti o!al l y deter&i !es so&e skills
attri butes. 2his is to be avoided because o) the delete a!o&al y.
2hereGs a cute 'ay o) sayi!# 'hat >Ds should appear i! a !or&al iBed
table: every attri bute &ust be deter&i !ed by Othe key, the 'hole key,
a!d !othi !# but the key.O 0eed separate table )or skills because o)
Othe 'hole keyO.
Si&ilarl y, bu!ch o) attri butes are )u!cti o!al l y deter&i !ed by e&pLid
(i!cludi !# a bu!ch o) dept i!)or&ati o! by tra!si ti vi t y. 2hese
attri butes also do!G t belo!# i! e&pLi!)o 'ith key o) t'o attri butes.
Does this lead to a delete a!o&al yM
:es. 8) delete last skill )or a! e&ployee, lose i!)or&ati o! about
e&ployee. 2here)ore have to have separate table )or e&ployees.
(8!clude dept i!)o.
0o' have (p#. *-+: e&ps table (e&pLid, e&pL!a&e, e&pLpho!e,
deptL!a&e, deptLpho!e, deptL&#r!a&e, skills table (skillLid,
skillL!a&e, a!d e&pLskills table (e&pLid, skillLid, skillLdate, skillLlvl.
0o' i! every o!e o) these tables, all attri butes are )u!cti o!all y deter F
&i!ed by the key ('oul d !ot be true i) put skillLdate i! e&ps table,
say, a!d the 'hole key (eve! dept i!)o deter&i !ed by e&pLid.
A!y proble&sM :es, i) delete last e&ployee i! a depart &e!t (duri !#
reor #a!iBati o!, lose i!)o about the depart &e!t, such as depart &e!tal
pho!e !u&ber.
<hatGs the proble& i! ter&s o) >DsM Re&e&ber, every attri bute is
depe! de!t o! Othe key, the 'hole key, a!d !othi !# but the keyOM
6roble& here is that deptLpho!e, )or e(a&pl e, is depe!de!t o!
deptL!a&e. So )ail o! O!othi !# but the keyO.
2his is 'hat is k!o'! as a Otra!si ti ve depe!de!cyO or Otra!si ti ve >DO,
a!d ca!!ot e(ist i! a 'ellF behave table 'ith !o delete a!o&al y.
1P, !o' >i#ure ,.",, p#. *-,: e&ps table, depts table, e&pLskills
table, skills table. 2hree e!ti ti es a!d a relati o!shi p. 0ote that there is
o!e table )or each >D. 0ot a! accide!t.
F11.F
8s it a lossless Hoi!M :es, because co!sider )actori !#. 1P to )actor
skills )ro& e&pLskills because i!tersecti o! o) %eadi!#s co!tai !s key
)or o!e.
Si&ilarl y )or e&pLskills a!d e&ps. A!d )or e&ps a!d depts.
(Aust to step back to 3FR &odel )or a &o&e!t, !ote that the
i!tersecti o!s o) headi!#s 'e have Hust !a&ed are )orei#! keys i! o!e
table, keys i! the other.
8! 3FR &odel, have e&pLskills table represe!ts a relati o!shi p bet'ee!
3&ps a!d Skills e!ti ti es. 2here is also a relatio!shi p &e&berLo)
bet'ee! 3&ps a!d Depts that is 0F1 a!d represe!ted by a )orei#!
key. 6icture.
1P, !o' !eed to de)i!e 0or&al >or&s (i! reverse order to their
stre!#th.
BC0>, *0>, "0>
"0> is too 'eak, !o DBA 'ould stop there. 3sse!tiall y, &ea!s
stoppi !# 'i th depts i! e&ps table. De)i!e !ear e!d o) sectio! as !ote.
BC0> de)i!i ti o! is &ore i!tui ti ve tha! *0>, but *0> a!d BC0> are
equiva le!t )or the sche&a 'e have Hust bee! deali!# 'i th. 0eed
special situa tio! )or di))ere!ce to arise.
BC0> is i! #e!eral so&e'hat &ore restricti ve tha! *0> 'here the
situa tio! arises, but has a proble&. 1)te! stop 'i th *0> i! real
desi#!.
De). ,.-.$. BoyceF Codd 0or&al >or&, or BC0>. A table 2 i! a
database sche&a 'i th >D set > is i! BC0> i)), )or a!y >D 9 FT A i! >
\

that lies i! 2 (all attri butes o) 9 a!d A i! 2, A is a si!#le attri bute !ot
i! 9, the! 9 &ust be a superkey )or 2.
1R: >or a !o!F tri vial >D 9 FT : 'here 9 is &i!i &al, 9 &ust be the key
)or 2.
2he &i!i &al cover )or the >Ds 'hose attri butes lie i! > are all o) the
)or& 9 FT A, 'here 9 is a key )or 2. 0ote 012 sayi!# it is a 6R87AR:
key ? &i#ht be &ore tha! o!e key.
1P, this see&s to be 'hat 'e 'ere tryi !# to do: 3very attri bute i! 2 is
deter&i !ed by a key (de)i!i ti o!, the 'hole key (!o subset o) it a!d
!oth i!# but the key (a!y >D deter&i !i !# A tur!s out to co!tai ! a key.
F1"/F
All o) the tables i! the )i!al 3&ployee 8!)or&ati o! database sche&a
resulti !# above are i! BC0>. <he! all the tables o) a database
sche&a are i! BC0> (*0>, "0>, etc 'e say the database sche&a is i!
BC0> (*0>, etc.
1P, !o' assu&e add attri butes to e&ps table attri butes to keep track
o) the street address (e&pLstraddr, the city a!d state (e&pLci tyst
a!d V86C1D3 (e&pLBip. 2hese are all deter&i !ed by e&pLid as !o':
(1 e&pLid FT e&pL!a&e e&pLpho!e deptL!a&e e&pLstraddr
e&pLcityst e&pLBip (every e&ployee has o!l y o!e &aili!# address.
0o' the 'ay the 61 has assi#!ed V86C1D3S, tur! out have !e' >Ds:
(+ e&pLcityst e&pLstraddr FT e&pLBip
(, e&pLBip FT e&pLcityst (Dra' 6icture
Co!sider the e(te!ded e&ps table 'i th these added attri butes:
e&ps table: e&pLid e&pL!a&e e&pLpho!e deptL!a&e e&pLstraddr
e&pLcityst e&pLBip (4eave this up
2his still has key e&pLid, but !o' there are >Ds that are 012
deter&i !ed solel y by the key.
2hus e&ps &ust )actor (database sche&a 1, 'hile all other tables
sa&e:
e&ps table: e&pLid e&pL!a&e e&pLpho!e deptL!a&e e&pLstraddr
e&pLcityst (!o' !o e(tra!eous >Ds i! this table.
e&padds table: e&pLstraddr e&pLcityst e&pLBip (41SS43SS
0ote e&pLcityst, e&pLstraddr is key )or e&padds a!d i! i!tersecti o!
o) )actored tables (it is a )orei#! key i! e&ps that deter&i !es the
address held i! e&padds, so lossless.
But >D (, still is!G t derived )ro& key i! e&padds. So )or BC0>,
e&padds &ust )actor )urther (database sche&a ", e&ps sa&e
e&padds table: e&pLBip e&pLstraddr
Bip table: e&pLBip e&pLcityst
F1"1F
0o' e&padds has a key co!sisti !# o) both colu&!s (there is !o >D
'hose le)tF ha!d side is i! e&padds. Bip table has e&pLBip as key (>D
(,.
(Ask Class: <hy BC0>M <hy 4osslessM
6R1B437: >D + does!G t lie i! a!y table. But 'e al'ays try to
OpreserveO each >D 9 FT :, &ea!i !# that 9 u!io! : lies i! the head o)
so&e 2
i
.
Allo's us ()or e(a&pl e to update the street address, city, D state )or
a! e&ployee a!d test i! o!e table that V86 'as e!tered correctl y.
2hus BC0> has a bad propert y, a!d 'e 'a!t to )all back to desi#! o)
database sche&a 1. 2hat table does!G t )it BC0> de)i!i ti o! because
there is a >D 9 FT A 'here 9 is !ot a superkey o) the table. (9 is
e&pLBip, A is e&pLcityst. 0eed a !e' !or&al )or&.
De). ,.-.+. A 6R873 A22R8B;23 o) a table 2 is a!y attri bute that is part
o) a key )or that table (!ot !ecessaril y a pri &ary key.
De). ,.-.,. 2hird 0or&al >or& (*0> . A table 2 i! a database sche&a
'i th >D set > is i! *0> i)), )or a!y >D 9 FT A i&plied by > that lies i! 2,
i) A is a si!#le !o!F pri &e attri bute !ot i! 9, the! 9 &ust be a superkey
)or 2.
(8! book, &akes it clear that this is the sa&e de) as BC0> 'ith a!
escape clause, that ca! allo' the i&plied co!di ti o!, 9 &ust be a
superkey o) 2, to )ail i) A is a pri &e attri bute.
1P, !o' 'ith 'eake!ed *0> a!d t'o >Ds:
(+ e&pLcityst e&pLstraddr FT e&pLBip
(, e&pLBip FT e&pLcityst
Ca! have all three attri butes i! a si!#le e&padds table, si!ce
e&pLcityst e&pLstraddr is the key, a!d >D (, does!G t break the rule
)or 9 FT A because A I e&pLcityst is a pri &e attri bute.
0o' have: every !o!F pri &e attri bute is deter&i !ed by the key, the
'hole key, a!d !othi !# but the key.
1P, Al#ori th& ,.-.- to achieved 'ell behaved *0> deco&posi ti o!, p#
*.*
F1""F
Give! a u!iversal table 2 a!d a set o) >Ds >, #e!erate a set S o)
headi!#s )or a database sche&a i! *0> that is a lossless
deco&posi ti o! a!d preserves all >Ds i! >.
4ook at p#. *.*. Start by )i!di!# &i!i &al cover. 2he! loop o! all >Ds,
a!d )or every >D, i) there is !ot already a table headi!# that co!tai!s
the >D, create a !e' headi!#. >i!all y, i) there is a key P )or 2 that is
!ot i! a!y table, create a !e' table that co!tai !s it.
De). ,.-.-. Seco!d 0or&al >or& ("0> . A table 2 'ith >D set > is i!
"0> i)): )or a!y 9 FT A i&plied by > that lies i! 2, 'here A is a si!#le
!o!F pri &e attri bute !ot i! 9, 9 is !ot properl y co!tai!ed i! a!y key o)
2.
A database sche&a is "0> i)) all o) its tables are "0>.
F1"*F
Class "!.
0ote o! 7i!i &al cover Al#ori th&, p# *,.
0ote i&porta!ce o) per)or&i !# Step " a#ai! a)ter reduci!# lhs i! Step
*. Co!sider ;!iversal table 'ith attri butes A B C. >D set
> I RA B FT C, C FT B, A FT BS
Step 1. Deco&posi ti o! o! the ri#ht. 0o cha!#e.
Step ". 2ake out si!#le >Ds. Ca! 'e take out A B FT CM 0o, si!ce
!othi !# else FT C, a!d thus i) 9 I A B, the! 9
\
I A B. 2ake out C FT BM
0o, si!ce !o' C FNFT a!ythi !#, a!d i) 9 I C, the! 9
\
I C. 2ake out A
FT BM But !o' i) 9 I A, 9
\
I A..
Step *. Reduce sets o! the le)t. A B reduced to AM 0o' have A FT C.
Does this i!crease 9
\
'he! 9 I A Be)ore had A FT B so 9
\
I A B a!d
the! A B FT C so 9
\
I A B C. So reduci!# to A FT C, u!cha!#ed.
Do you believe thatM Ca! also test all other sets 9 )or set o) 9
\
, see
u!cha!#ed. 2o cha!#e, it 'ould have to i!vol ve the !e' >D A FT C
a!d !ot be i&plied by A B FT C. But si!ce A FT B, do!G t see ho' that is
possible.
Step $, ;!io!. 0o e))ect. 2here)ore 7i!i &al cover is:
A FT C, C FT B, A FT B.
Ridiculous, si!ce >Ds 1 a!d " #ive * by tra!si ti vi t y.
<ork out 3(ercise ,.1-.
(,.1- (a Assu&e 'e 'ish to co!struct a database )ro& a set o) data
ite&s, RA, B, C, D, 3, >, G, %S ('hich 'ill beco&e attri butes i! tables,
a!d a set > o) >Ds #ive! by:
>: (1 A > B C , (" A B 3 > C D G %, (* C > G D, ($ D > G, (+ 3
> >
(a >i!d the &i!i &al cover )or this set o) >Ds, a!d !a&e this set G.
Step 1.
% I (1 A>B, (" A>C, (* AB3>C, ($ AB3>D, (+ AB3>G, (, AB3>%, (E
C>G, (- C>D, (. D>G, (1/ 3>>
F1"$F
Step ". (1 A>B: AI rest, 9
\
I AC("G(+D(-, !ot co!tai !i !# B, so keep.
(" A>C, AI rest, 9
\
I A, keep.
(* AB3>C, 9
\
I AB3C("D($G(+>(,, co!tai!i !# C, drop.
0e' %: (1 A>B, (" A>C, ($ AB3>D, (+ AB3>G, (, AB3>%, (E C>G, (-
C>D, (. D>G, (1/ 3>>
($ AB3>D: 9
\
I AB3C("G(+D(->(1/, co!tai !i !# D, drop.
0e' %: (1 A>B, (" A>C, (+ AB3>G, (, AB3>%, (E C>G, (- C>D, (.
D>G, (1/ 3>>
(+ AB3>G: 9
\
I AB3C("G(ED(->(1/G, co!tai !i !# G, drop.
0e' %: (1 A>B, (" A>C, (, AB3>%, (E C>G, (- C>D, (. D>G, (1/
3>>
(, AB3>%: 9
\
I AB3C("G(ED(->(1/, !ot co!tai !i !# %, keep.
(E C>G: 9
\
I CD(-G(. co!tai !i !# G, drop.
0e' %: (1 A>B, (" A>C, (, AB3>%, (- C>D, (. D>G, (1/ 3>>
C>D: 9
\
I C, keep.
D>G: 9
\
I D, keep.
3>>: (
\
I 3, keep.
Result %: (1 A>B, (" A>C, (* AB3>%, ($ C>D, (+ D>G, (, 3>>
Step *. 2ry to drop so&ethi !# )ro& lhs o) (* AB3>%:
AB> % i!stead i! %G: AB
\
u!der %G I ABC("%(*D($G(+
AB u!der % I ABC("D($G(+ Do!G t #et %. 0ot the sa&e, so ca!G t use.
A3>% i!stead i! %G: A3
\
u!der %G I A3B(1C("%(*D($G(+>(,,
A3
\
u!der % I A3B(1C("%(*D($G(+>(1/. Sa&e. Drop B )ro& lhs.
0e' %: (1 A>B, (" A>C, (* A3>%, ($ C>D, (+ D>G, (, 3>>.
7ust repeat Step ".
(1 A>B. <ithout (1 !o B o! rhs.
(" A>C. <ithout (" !o C o! rhs.
(* A3>%. <ithout (* !o % o! rhs.
($ C>D. <ithout ($ !o D o! rhs.
(+ D>G. <ithout (+ !o G o! rhs.
(, 3>>. <ithout (, !o > o! rhs.
Step $. G: (1 A>BC, (" A3>%, (* C>D, ($ D>G, (, 3>>
(b Start 'i th the table 2 co!tai!i !# all these at tributes, a!d per)or& a
lossless deco&posi ti o! i!to a "0> but !ot *0> sche&a. 4ist care)ull y the keys
F1"+F
)or each table (2, 2
1
, a!d 2
"
, a!d the >Ds that lie i! each table. Austi)y the
)act that the deco&posi ti o! is lossless. 3(plai! 'hy it is "0> but !ot *0>.
De). ,.-.-. Seco!d 0or&al >or& ("0> . A table 2 'ith >D set > is i!
"0> i)): )or a!y 9 > A i&plied by > that lies i! 2, 'here A is a si!#le
!o!F pri &e attri bute !ot i! 9, 9 is !ot properl y co!tai!ed i! a!y key o)
2.
(0ote that the properl y 'as le)t out o) the te(t. A database sche&a
is "0> i)) all o) its tables are "0>.
Re&e&ber *0> says, )or each table, >Ds are deter&i !ed by Othe key,
the 'hole key, a!d !othi !# but the keyO. "0> #ives us Othe 'hole
keyO. Still allo' tra!si ti ve depe!de!cies, so do!G t have O!othi !# but
the keyO. %ave.
G: (1 A>BC, (" A3>%, (* C>D, ($ D>G, (+ 3>>
2 I (A, B, C, D, 3, >, G, %. Si!ce A a!d 3 o!l y occur o! lhs o) >Ds i! G, A3
&ust be i! a!y key , a!d A3
\
I A3BC(1%("D(*G($>(+ I ABCD3>G%, so A3
is the key )or 2.
>D (+, 3>>, co!sti tutes a keyF properF subset depe!de!cy, a!d thus !eeds
deco&posi ti o! )or "0>. 3
\
I 3>(+, a!d thatG s it. 4et 21 I (3,> a!d 2" I
(A,B,C,D,3,G,% a!d 2he! 3 is the key )or 21 a!d A3 is the key )or 2".
0ote 'e leave Pey i! old table )or i!tersecti o!, but take out depe!de!t
attri bute that 'ould cause bad >D to e(ist, 3>>.
Si&ilarl y (1 A>BC is also a keyF properF subset depe!de!cy, a!d A
\
I
ABC(1D(*G($, so 'e )urther deco&pose: 21I (3,>, 2" I (A,3,%, 2* I
(A,B,C,D,G.
0ote % re&ai !s 'i thi ! 2" because 'e have!G t )ou!d a!y other ho&e )or it: 8t
is !ot true that AFT% or 3FT%. Aust pull out thi!#s o) that ki!d.
0o' >D 3>> lies i! 21, >D A3>% lies i! 2", a!d >Ds A>BC, C>D a!d D>G
lie i! 2*. C>D a!d D>G are tra!si ti ve depe!de!cies, allo'ed i! "0>, so 'e
#o !o )urther here.
2hese are lossless deco&posi ti o!s by 2heore& ,.E.$. 8! the )irst, 'e !ote
that head(21 8023RS3C2 head(2" I 3, the key )or 21. 8! the seco!d,
head(2" 8023RS3C2 head(2* I A, the key )or 2*.
F1",F
2he idea here is that A a!d 3 are Odi &e!sio!sO o! 'hich other attri butes are
depe!de!t: !eed A3, A, a!d 3 all separate i) there are depe!de!cies i! all
cases.
3.#., 14A6 vie' o) data o! di&e!sio!s City, 7o!th, a!d >uel: YCost, Y;sa#e
depe! de!t o! all threeC 7ea!Lte&p depe!de!t o! City a!d 7o!thC
:earl yLusa#e depe!de!t o! City a!d >uelC >1BLcost depe!de!t o! >uel a!d
7o!thC 4ati tude depe!de!t o! City o!lyC B2;LperLu!i t depe!de!t o! >uel
o!l yC >uelLdays depe!de!t o! 7o!th o!ly.
(c Co!ti!ue deco&posi ti o! to bri!# this database to *0>. 8s this table also
BC0>M
2o co&plete the deco&posi ti o! to *0>, 'e !eed to )actor out the tra!si ti ve
depe!de!ci es. %ave 2* I (A,B,C,D,G, 'ith >Ds: (* C>D a!d ($ D>G.
Cha!#e 2* to (A,B,C a!d create !e' tables 2$ I (C,D a!d 2+I(D,G. Ca!
you say 'hy Hoi! is !o' lossless, classM
0o' 'e have o!e >D co!tai !ed i! each table. 8! each case the lhs is a key
)or its table, so the sche&a is i! BC0>. (Do!G t have to 'orry about >Ds 'i th
!o!F pri &e lhs.
(d ;se Al#ori th& ,.-.- a!d the set G o) >Ds to achieve a lossless *0> de F
co&posi ti o! that preserves >Ds o) G. 8s this the sa&e as the deco&posi ti o!
i! part (cM
S I !ullset. 4oop throu#h all the >Ds o) G:
G I R A>BC, A3>%, C>D, D>G, 3>>S
(4oop throu#h >Ds, a!d i) !ot already co!tai !ed i! table o) S, create !e'
A>BC: S I RABCS.
A3>%: A3% !ot co!tai!ed i! ABC, so add it: SIRABC,A3%S
C>D: CD !ot co!tai !ed i! either yet, so add it: SI RABC,A3%,CDS
D>G: DG !ot co!tai!ed i! a!y yet, so add it: S I RABC,A3%,CD,DGS
3>>: 3> !ot co!tai !ed i! a!y yet, so add it: S I RABC,A3%,CD,DG,3>S
A3 is the o!ly ca!didate key, a!d it is co!tai !ed i! A3%, so do!e.
8s this is the sa&e deco&posi ti o! as i! cM
F1"EF
Class "!.
4ast Class.
Reco!sider Airli!e Reservati o! desi#! o) >i#ure ,.1$, p#. *+1
<hat is the ;!iversal tableM
reservati o!s: ticket!o #ate!o )li#ht!o ddate dti &e seat!o
Gates
has_seat Seats Flights
Passengers
seat_assign
seatno flightno
ticketno
gateno
depart_time
dtime ddate
(1, 0
(1, 1
(/, 1
(1, 1
marshalls travels_on
(1, 1
(/, 0
(1, 0
(1, 1
<hat are >DsM %ave e!ti t y >li#hts 'i th ide!ti )i er )li#ht!o, a!d )act
that each >li#ht has a u!ique Gate assi#!ed:
(1 )li#ht!o > ddate dti &e #ate!o (or )! > dd dt #a
%ave e!ti t y 6asse!#ers 'ith ide!ti )i er ticket!o a!d )act that every
passe!#er has a particul ar seat assi#!ed a!d travels o! a speci)ic
)li#ht:
(" ticket!o > seat!o )li#ht!o (or t! > s! )!
<eG ve bee! treati !# 'eak e!ti t y Seats as i) all seat!o values are
di))ere!t )or di))ere!t )li#hts (each belo!#s to (1, 1 >li#hts so:
(* seat!o > )li#ht!o (or s! > )!
>or this u!iversal table, key 'ould be: ticket!o
7i!i &al coverM Step 1. <rite out. Step *, !o &ul tiF attri bute sets o!
le)t to reduce. Step ": Ca! #et rid o) ticket!o > )li#ht!o. Step $,
;!io!.
F1"-F
>i!al: (1 )! FT dd dt #a, (" t! FT s!, (* s! FT )!
"0> is: t! dd dt #a s! )!, but t'o tra!si ti ve depe!de!cies.
2he >Ds 'ould #ive us tables (see p#. *"": passe!#ers (ticket!o,
seat!o, )li#hts ()li#ht!o, #ate!o, ddate, dti &e, a!d seats (seat!o,
)li#ht!o.
%ave do!e a'ay 'i th relatio!ahi p travelsLo! a!d resul ti !# )orei#!
key, 'hich is i!deed u!!ecessary.
3ut for =uery efficiency, migt $ant to keep it . >e'er Hoi!s that
'ay 'he! ask 'hat #ate passe!#er should #o to. 2his is called
ODe!or&al iBatio!O.
:ou see, it helps to #et &i!i &al cover a!d do !or&al iBati o! i! thi!ki !#
about di))icul t 3FR i!tui ti o!.
0o #ates table. 0or&aliBati o! is sayi!# that #ates is!G t a! e!ti t y
because #ate!o does!G t )u!ctio!al l y deter&i !e a!ythi !#. 8) had:
#ate!o > #atecapaci ty, the! 'ould have a table.
Recall that i! e&ps table, the &ul tiF valued attri bute hobby did !ot
quali)y as a! e!ti ty. <e &i#ht disa#ree, but o!l y i) so&eo!e 'a!ted
to look up all hobbys that ever e(isted )or so&e reaso!. 4ike #ate!o.
<ithout havi!# #ate!o re&e&bered i! #ates table, 'ill lose track o)
#ate !u&bers (delete a!o&al y. 7i#ht be i!appropri ate (&i#ht be
used i! pro#ra&s to assi#! #ates to )li#hts, do!G t 'a!t to lose track.
But this )act does!G t co&e out o) !or&al iBati o! as it sta!ds. Co&es
out o) i!tui ti o! o) 3FR. So both approaches valuabl e.
1P, !o' Sectio! ,... Co!sider CA6 database, 3FR desi#! o) >i#ure E.",
p#. $+1. A!d co!sider the )ollo'i !# >D that holds i! the ori#i!al
;!iversal CA61RD3RS table:
(1 qty price disc!t > dollars
2his is true because dollars is calculated )or i!sert state&e!t by:
e(ec sql i!sert i!to orders
values (:ord!o, :&o!th, :cid, :aid, :pid, :qty,
:qtyW:pri ce F ./1W:disc!tW:qt yW: pri ceC
F1".F
<here :price has bee! deter&i !ed )ro& products )or this pid a!d
:disc!t )ro& custo&ers )or this cid.
But !o' does this &ea! 'e le)t a >D out o) our !or&al iBati o!M %o'
'ould 'e cha!#e the desi#! o) ".", custo&ers, a#e!ts, products,
ordersM
0eed to add a !e' table 'hich 8 'ill call ddollars ()or Odeter&i !ed dol F
larsO 'ith the headi!#: qty price disc!t dollars, a!d re&ove dollars
)ro& orders. 2he ddollars table has key: qty price disc!t
0o' 'hat is the value o) thisM <he! i!sert a !e' order, the key 'ill
#uara!tee that the value dollars is u!ique. But o) lots o) di))ere!t qty,
price, disc!t triples, &a!y such ro's &i#ht be u!ique a!y'ay.
Stra!#e approach to validati !#. 8 'ould be happier to depe!d o! the
i!sert state&e!t ('hich also has to calculate :disc!t a!d :price.
6roble& here is that a very co&&o! query )or a! order 'ith dollars
total 'ill !o' require a Hoi! o) orders, products, custo&ers, a!d
ddollars, i!stead o) Hust bei!# read o)) a ro' o) orders.
8 'ould leave >D out o) the desi#!. 8&proves queries at the e(pe!se
o) a validati o! o) questio!able value.
Desi#! tools. 7ost o) the& are based o! 3FR. 8 brou#ht o!e, the
C%30 <orkbe!ch 7ethodol o#y Guide. 2ool 'ill the! do thi!#s like
auto&ati cal l y tra!slate i!to Create 2able de)i!i ti o!s, 'ith appropri ate
co!strai !ts.
7icroso)t Access 'ill take a )e' hu!dred ro's o) data i! a u!iversal
table a!d deter&i !e all >Ds that see& to hold (o!l y statistical . 1)te!
user 'ould &iss these.
Lecture Notes for Database -ystems, part "
by Patrick E. O'Neil
Class !.
4ast ter& covered Chapters "F,C this ter& cover Chapters EF11.
8 'ill assu&e everythi !# i! te(t throu#h Chap ter ,. AdFhoc S@4 (Chap. *, 1F
R S@4 (Chap. $, 3&bedded S@4 (Chap. +, Database Desi#! (Chap. ,. Note
solved a!d u!solved 3(ercises at e!d o) chapters.
F1*/F
Starti !# o! Chapter E. %o&e'ork assi#!&e!t o!li!e. Dotted proble&
solutio!s are at e!d o) Chapter.
O42L1NE. Revie' 'hat is to be covered this ter&. Chapter EF11.
E. DBA co&&a!ds. ;se e(a&pl es )ro& co&&erci al products a!d o'! Basic
S@4 sta!dard, also 9N1pe!, A0S8 S@4F.", S@4F...
Start E.1, 8!te#ri t y Co!strai !ts, #e!erall y as part o) Create 2able co&&a!d.
(Rules )or a! 3!terprise )ro& 4o#ical Desi#!: revie' Chapter ,.
E.", 5ie's: virtual tables to &ake it easier )or DBA to set up data )or ac cess
by users. Base tables vs. 5ie' tables.
E.*, Securi ty. <ho #ets to access data, ho' access is co!trol led by DBA.
E.$, Syste& catalo#s. 8) you co&e to a !e' syste&, !e' database, ho' )i# F
ure out 'hat tables are, 'hat colu&!s &ea!, etc. All i!)o carried i! catalo#s,
relatio!al data that tells about data (so&eti &es called 7etadata.
Chapter -. 8!troducti o! to physical structure a!d 8!de(i!#. Records o! disk
are like books i! a library, a!d i!de(es are like old style card catalo#ues.
Ge!erall y, 'hat 'e are tryi !# to save 'i th i!de(i !# is disk accesses (very
slo' FF a!alo#ous to 'alki!# to library shelves to #et books.
4ots o) di))ere!t types o) i!de(i !# )or di))ere!t products, !ot covered at all by
a!y sta!dards. So&e'hat co&ple( but )u! topic.
Chapter .. @uery 1pti &iBati o!. %o' the @uery 1pti &iBer actuall y creates a
@uery pla! to access the data. Basic access steps it ca! use.
Aust as a! e(a&ple, seque!ti al pre)etch. Aoi! al#ori th&s. %o' to read @uery
3(ecutio! 6la!s. &mportant s'ill when we cover thisis learning to add quic'l y
and ma'e numeri c esti mates of how many &()s are requi red .
@uery 1pti &iBati o! is a 53R: co&ple( topic. All e(a&pl es i! te(t )ro& 75S
DB". <ill try to add e(a&pl es )ro& other ve!dor products.
3!d o) Chapter ., see @uery per)or&a!ce &easured i! Set @uery be!ch&ark.
Chapter 1/. ;pdate 2ra!sacti o!s. 2ra!sacti o!al histori es. 6er)or&a!ce
value o) concurrent e*ecuti on . Co!curre!cy theory.
AC8D properti es, tra!sactio!s &ake #uara!tees to pro#ra&&er. Ato&ic,
Co!siste!t, 8solated, a!d Durable. 26CFA be!ch&ark.
F1*1F
2ra!sactio!al Recovery. 8dea is that i) the syste& crashes, 'a!t to save
resul ts. 2ra!sactio!s are allForF !othi !#, like tra!s)er o) &o!ey. Do!G t 'a!t to
be chopped o)) i! the &iddle.
Chapter 11, 6arallel a!d Distri buted databases. <ebll see ho' &uch 'e ca!
do o! this.
Capter +.! 1ntegri t y Constrai nts
Sectio! E.1: 8!te#ri t y co!strai !ts. Revie' Chapter ,, Database Desi#!, )ro&
last ter&, si!ce co!cepts are applied here i! Chapter E.
8dea i! Chapter , 'as that DBA per)or&s 4o#ical database desi#!, a!al yBi!#
a! e!terprise to be co&puteriBed:
o listi!# the data ite&s to be kept track o)
o the rules o) i!terrel ated!ess o) these data ite&s (>Ds
o apporti o!i !# the data ite&s to di))ere!t tables (3FR or 0or&aliBati o!
A)ter do this, Chapter E tells you ho' to actuall y co!struct the tables a!d load
the&. Build rules i!to table so S@4 update state&e!ts can' t brea' the rules
o) i!terrelated!ess (a!d other rules 'eG ll co&e up 'i th. Call this a
faithful represent at i on
2he 'ay to achieve this is 'ith constrai nt clauses i! a Create 2able
state&e!t. Sho' Basic S@4 to re)lect 1RAC43, DB" ;DB, a!d 80>1R789.
See p# $1+, )or the 3FR dia#ra& o) our CA6 database.
3!tities: Custo&ers, A#e!ts, 6roducts, a!d 1rders. Attri butes o) e!ti ti esC
Co!ce!trate o! custo&ers: look at card( desi#!ati o!s:
o cid (pri &ary key attri bute, &i!F card 1 so &ust e(ist, &a(F card 1 so at &ost
o!e cid per e!ti ty i!sta!ce
o disc!t (&i!F card /, so ca! e!ter a record 'i th !o disc!t k!o'!, !ull value
allo'ed. A!d so o! . . .
%ere is Create 2able state&e!t o) last ter& ('i th pri &ary key co!strai !t:
create table custo&ers (cid char($ !ot !ull, c!a&e varchar(1*,
city varchar("/, disc!t real check(disc!t UI 1+./, pri &ary key(ci dC
2hree co!strai !ts here: !ot !ull )or cid (also pri &ary key cid, 'hich i&plies
!ot !ull, check disc!t UI 1+.// (!ot allo'ed to #o over 1+.//.
F1*"F
6ri&ary key clause i&plies u!ique A0D !ot !ull. 8) say pri &ary, do!G t say
u!ique. 1ld products required !ot !ull 'ith pri &ary key, still the &ost robust
approach.
Could also declare cid (cid char($ !ot !ull u!ique i) it 'ere Hust a!y old
ca!didate key rather tha! a pri &ary key.
8> %N0 /4LE 1- 3/O6EN by !e' ro' i! custo&ers resul ti !# )ro& S@4 upF
date state&e!t (8!sert, Delete, a!d ;pdate, update 'o!G t OtakeO ? 'ill )ail
a!d #ive error co!di ti o!. 0ot true )or load, ho'ever.
F1**F
Class ".
Coveri!# idea o) Create 2able co!strai !t clauses D )aith)ul represe!tati o!.
A#ai!: Relatio!ships i! >i#ure E.", p#. $1+. 3ach o) C, A, 6 is related to 1.
Custo&ers requests 1rders, &a(F card(Custo&ers, requests I 0C ca! have
7a!y li!ks out o) each Custo&er i!sta!ce, but &a(F card(1rders, requests I
1, o!ly o!e i!to each order: single$ valued partici pati on . 7a!yF 1!e, 0F1.
<hich side is 7a!yM 1rdersQ Side that has si!#leF valued participati o!Q 1!e
custo&er places 7A0: orders, reverse !ot true. 2he 7A0: side is the side
that ca! co!tai ! a )orei#! key i! a relatio!al tableQ
Represe!tati o! i! relatio!al &odelM 0F1 relati o!shi p repre se!ted by )or ei#!
key i! orders re)ere!ci !# pri &ary key i! custo&ers.
create table orders ( ord!o i!te#er !ot !ull, &o!th char(*,
cid char($ !ot !ull, aid char(* !ot !ull,
pid char(* !ot !ull, qty i!te#er !ot !ull check(qt y TI /,
dollars )loat de)aul t /./ check(dollars TI /./,
pri &ary key ( ord!o ,
)orei#! key (cid re)ere!ces custo&ers,
)orei#! key (aid re)ere!ces a#e!ts,
)orei#! key (pid re)ere!ces productsC
8! Create 2able )or orders, see ord!o is a pri &ary key, a!d at e!d says cid is
)orei#! key re)ere!ces custo&ers. 2he i&plicati o! is that cid i! orders &ust
&atch a pri &ary key value i! custo&ers (!a&e !eed!G t be cid.
8) 'e 'a!ted to &atch 'i th a ca!didate key, col!a&e", could i!stead say:
)orei#! key (col!a&e1 re)ere!ces table!a&e (col!a&e"
Ca! also have lar#er tuples &atchi !#:
Create table e&ployees ( . . .
)orei#! key (cityst, staddr, Bip re)ere!ces Biptst(ci tyst, staddr, BipC
0o' 'i th this >1R38G0 P3: . . . R3>3R30C3S clause )or cid i! orders table, i)
try to i!sert a! orders ro' 'i th cid value that is!G t i! custo&ers, i!sert 'ill )ail
a!d #ive a! error co!di tio!.
>or #e!eral )or& o) Create 2able co&&a!d, see p# $11, >i#ure E.1. 2his is
our Basic S@4 sta!dard. (See )irst pa#e o) Chapter * )or de)i!i ti o! o) Basic
S@4
F1*$F
6ut o! board o!e sectio! at a ti &e. %ere is Create 2able speci)icati o! block:
7igure +.!. Clauses of te Create 2able command .
CR3A23 2AB43 table!a&e
((col!a&e datatype JD3>A;42 Rde)aul tLco!sta!t ] 0;44SK
JcolLco!str RcolLco!str. . . SK
] tableLco!str
R, Rcol !a&e datat ype JD3>A;42 Rde)aul tLco!sta!t ] 0;44SK
JcolLco!str RcolLco!str. . . SK
] tableLco!str S
. . .SC
Recall CA6S &ea!s literal. Cover typo#raphical co!ve!ti o!s, Chap *, p# -+
Start by !a&i !# the table. 2he! list o) colu&! !a&es 1R table co!strai !ts.
2he colu&! !a&es are #ive! 'i th datat ype a!d possibl y a D3>A;42 clause:
speci)ies a value the syste& 'ill suppl y i) a! S@4 8!sert state&e!t does !ot
)ur!ish a value. (4oad co&&a!d is !ot co!strai !ed to provi de this value.
Colu&! Co!strai !ts ca! be thou#ht o) as shortha!d )or 2able Co!strai!ts.
2able Co!strai !ts are a bit &ore )le(i ble (e(cept )or 012 0;44.
0ote that both co!strai !ts have Oco!strai !t!a&eO )or later re)ere!ce. Ca!
DR16 both usi!# later Alter 2able, ca! o!l y ADD 2able Co!strai !t (!ot CC.
Ca! tell di))ere!ce bet'ee! the t'o: Colu&! Co!strai !ts stay 'ith col!a&e
datat ype de)i!i ti o!, !o separati !# co&&a. See Create 2able orders o! board.
2able Co!strai !t has separati !# co&&a (like co&&a de)i!i ti o!. CC keeps
co!strai !t !ear obHect co!strai !ed, 2C &i#ht be )ar a'ay i! lar#e table.
Def +.!. ". Column Constrai nts .
2he colLco!str )or& that co!strai !s a si!#le colu&! value )ollo's:
R012 0;44 ]
JC10S2RA802 co!strai !t!a&eK
;08@;3
] 6R87AR: P3:
] C%3CP (searchLco!d
] R3>3R30C3S table!a&e J(col!a&e K
J10 D34323 CASCAD3K S
F1*+F
2he 012 0;44 co!di tio! has already bee! e(plai!ed (&ea!s &i!F card I 1,
&a!datory partici pati o! o) attri bute. Does!b t #et a co!strai !t!a&e 8) 012
0;44 appears, de)aul t clause ca!G t speci)y !ull.
A colu&! co!strai !t is either 012 0;44 or o!e o) the others (;08@;3, etc.
optio!all y pre)i (ed by a OC10S2RA802 co!strai !t!a&eO clause. 2he )i!al OSO
o) the sy!ta( )or& &atches 'i th the ORO be)ore the 012 0;44 a!d the )irst O]O,
althou#h ad&i ttedl y this is a&bi#uous, si!ce the other O]OGs lie at the sa&e
relati ve positio!.
A &ore care)ul speci)icati o! 'ould be:
R012 0;44 ]
JC10S2RA802 co!strai !t!a&eK
R;08@;3
] 6R87AR: P3:
] C%3CP (searchLco!d
] R3>3R30C3S table!a&e J(col!a&e K
J10 D34323 CASCAD3K S S
Co!strai !t !a&es ca! #o be)ore A0: S80G43 103 o) the )ollo'i !#. (Repeated
colLco!tr ele&e!ts are allo'ed i! Create 2able speci)icati o! block.
3ither ;08@;3 or 6R87AR: P3: ca! be speci)ied, but !ot both. 0ot !ull u!ique
&ea!s ca!didate key.
;08@;3 is possible 'i thout 012 0;44, the! &ul ti pl e !ulls are possible but
!o!F !ull values &ust all be u!ique. ;08@;3 012 0;44 &ea!s ca!didate key.
C%3CP clause de)i!es searchLco!di ti o! that &ust be true )or !e' ro's i! the
curre!t table, e.#. qty TI /. (1!ly allo'ed to re)ere!ce o'! colu&!M
Ca! o!ly re)ere!ce co!sta!ts a!d the value o) this colu&! o! the si!#le ro'
bei!# i!sertedNupdated to per)or& a check )or a colu&!.
Create table orders ( . . . ,
cid char($ !ot !ull check (cid i! (select cid )ro& custo&ers, 1N.%L1 D
2he R3>3R30C3S clause &ea!s values i! this colu&! &ust appear as o!e o)
the values i! the table!a&e re)ere!ced, a colu&! declared u!ique i! that
table. A !o!F u!ique colu&! 'o!b t 'ork, but !ulls are 1P.
7ultiF colu&! equi vale!t, use tableLco!str: >1R38G0 P3: . . . R3>3R30C3S.
Colu&! co!strai !t Ore)ere!cesO is Hust a shortha!d )or si!#le colu&! !a&e.
F1*,F
2he optio!al 10 D34323 CASCAD3 clause says that 'he! a ro' i! the
re)ere!ced table is deleted that is bei!# re)ere!ced by ro's i! the re)er e!ci!#
table, the! those ro's i! the re)ere!ci !# table are deletedQ
8) &issi!#, de)aul t cR3S2R8C2sd delete o) a re)ere!ced ro' (disallo's it.
Def +.!. '. 2able Constrai nts.
2he tableLco!str )or& that co!strai !s &ul ti pl e colu&!s at o!ce )ollo's:
JC10S2RA802 co!strai !t!a&eK
R;08@;3 (col!a&e R, col!a&e. . .S
] 6R87AR: P3: (col!a&e R, col!a&e. . . S
] C%3CP (searchLco!di ti o!
] >1R38G0 P3: (col!a&e R, col!a&e. . .S
R3>3R30C3S table!a&e J(col!a&e R, col!a&e. . . SK
J10 D34323 CASCAD3K S
2he ;08@;3 clause is the sa&e as ;08@;3 )or colu&!, but ca! !a&e a set o)
colu&!s i! co&bi !ati o!. 8t is possible to have !ull values i! so&e o)
the colu&!s, but sets o) ro's 'ith !o !ulls &ust be u!ique i! co&bi!ati o!.
;08@;3 &ul tiF colu&!, all colu&!s declared 012 0;44 is 'hat 'e &ea! by a
Ca!didate Pey. 8) have &ul ti pl e colu&! ca!didate key, (c1, c", . . ., cP, &ust
de)i!e each colu&! 012 0;44 D table co!str. u!ique (c1, c", . . ., cP
2he 6R87AR: P3: clause speci)ies a !o!F e&pt y set o) colu&!s to be a pri &ary
key. 4iterall y, this &ea!s that a >1R38G0 P3: . . . R3>3R30C3S clause 'ill
re)er to this set o) colu&!s by de)aul t i) !o colu&!s !a&ed.
3very colu&! that participates i! a pri &ary key is i&plici tl y de)i!ed 012
0;44. <e ca! speci)y per colu&! too. 2here ca! be at &ost o!e 6R87AR: P3:
clause i! Create 2able. ;08@;3 ca!!ot also be 'ri tte! )or this co&bi !ati o!.
2he C%3CP clause ca! o!ly be a restricti on condi ti on : ca! o!l y re)ere!ce
other colu&! values 10 2%3 SA73 R1< i! searchLco!di ti o!.
A >1R38G0 P3: . . . R3>3R30C3S clause. 2he )orei#! key CA0 co!tai ! !ulls i!
so&e o) its colu&!s, but i) all !o!F !ull tha! &ust &atch re)ere!ced table
colu&! values i! so&e ro'.
1ptio!al 10 D34323 CASCAD3 &ea!s sa&e here as i! Colu&! Co!strai!t.
F1*EF
Class '.
E:ample , e&ployees 'orksLo! proHects )ro& >i#ure ,.-
create table e&ployees (eid char(* !ot !ull, staddr varchar("/, . . .
pri &ary key (eidC
create table proHects (prid char (* !ot !ull, proHL!a&e varchar(1,,
dueLdate char(-, pri &ary key (pridC
create table 'orksLo! (eid char(* !ot !ull, prid char(* !ot !ull,
perce!t real, )orei#! key (eid re)ere!ces e&ployees,
)orei#! key (prid re)ere!ces proHects, u!ique (eid, prid C
0ote there is !o pri &ary key (o!l y ca!didate key )or 'orksLo!C !o!e !eeded
si!ce !ot tar#et o) re)ere!ti al i!te#ri t y. Could &ake (eid, prid pri &ary key.
(So&e practi ti o!ers thi!k itGs a')ul to have table 'ithout pri &ary key.
%noter e:ampl e , )ro& >i#ure ,.E, 3&ployees &a!a#es 3&ployees. (2392
create table e&ployees (eid char(* !ot !ull, e!a&e varchar("$,
&#ri d char(*, pri &ary key (eid,
)orei#! key (&#ri d re)ere!ces e&ployeesC
A )orei#! key i! a table can reference a pri &ary key in te same table .
<hat do 'e !eed to do )or 0or&aliBati o! o) Ch. ,M All >Ds should be
depe!de!t o! pri &ary key )or table (1P: ro's have uni=ue key colu&!s.
Recall idea o) 4ossless Deco&posi ti o!: this is 'hat 'e !eed Re)ere!ti al
8!te#ri t y ()orei#! key )or. 2o be lossless, i!tersecti o! o) t'o tables (o! 'hich
Hoi! &ust be uni=ue o! o!e o) the&Q 7ust re)ere!ce to pri &ary keyQ
Create 2able -tatement in O/%CLE, see 7igure +.' @pg ("'B .
3(tra disk stora#e a!d other clausesC all products have this: disk stora#e 'ill
be covered i! !e(t chapter.
Create 2able AS S;B@;3R:: table ca! be created as Subquery )ro& e(isti!#
table. Do!G t need a!y colu&! !a&es, datat ypes, etc.: i!heri t )ro& Select.
1RAC43 has 30AB43 a!d D8SAB43 clauses )or co!strai !ts. Ca! de)i!e table
'i th !a&ed co!strai !t Disabled, later 3!able it (a)ter load table.
F1*-F
So&eti &es ca!b t load tables i) have all co!strai !ts 'orki !#: e.#. tables )or
boys a!d #irls at a da!ce, each &ust have part!er )ro& other table. A
re)ere!ti al i!te#ri t y co!strai !t that &ust )ail 'he! i!sert )irst ro'.
2here)ore Create 2able 'i th co!strai !t D8SAB43bd. 4ater use Alter 2able to
30AB43 it. 0ice idea, si!ce Co!strai!t stays i! Catalo# 2able.
6roble& that this is !ot portable, so 'e depe!d o! ADDi!# such a co!strai !t
later 'i th Alter 2able (ca!b t de)i!e it earl y i) load by 8!sert.
1N7O/M1 8 and D3" Create 2able
80>1R789 has &i!or variati o! as o) this versio!: all colu&! de)i!i ti o!s &ust
precede table co!strai !t de)i!i ti o!s (old 'ay, ca!b t be &i(ed i! a!y order.
DB", see 7igure +.), pg ("! . Adds to >1R38G0 P3: . . . R3>3R30C3S.
>1R38G0 P3:
(col!a&e R, col!a&eS R3>3R30C3S
table!a&e J 10 D34323 J 01 AC2810 ] CASCAD3 ] S32 0;44 ] R3S2R8C2KK
<hat i) delete a ro' i! custo&ers that cid values i! orders re)ere!ceM
01 AC2810, R3S2R8C2 &ea!s delete o) custo&ers ro' 'o!G t take 'hile
)orei#! keys re)ere!ce it. (Subtle di))ere!ce bet'ee! the t'o.
S32 0;44 &ea!s delete o) custo&ers ro' 'ill 'ork a!d re)ere!ci !# cid val ues
i! orders 'ill be set to !ull.
CASCAD3 &ea!s delete o) custo&ers 'ill 'ork a!d 'ill also delete re)er F
e!ci!# orders ro's. 0ote that this &i#ht i&pl y cascadi!# (recursi ve de F
letes, i) other )orei#! key re)ere!ces colu&! i! orders.
De)aul t is 01 AC2810. 6resu&ed by products that do!G t o))er the choice. See
>i#. E., )or sta!dard optio!s i! 9N1pe!, >ull S@4F.. is sa&e as DB".
/eferenti al 1ntegri t y
See p#. $1., De)i!i ti o! E.1.$. <e de)i!e a! ordered set o) colu&!s > to &ake
up a >orei#! key i! table 21 to &atch 'ith a! ordered set o) colu&!s 6 i!
table 2". ()orei #! key . . . re)ere!ces . . .
A re)ere!ti al i!te#ri t y co!strai !t is i! )orce i) the colu&!s o) a )orei#! key > i!
a!y ro' o) 21 &ust either (1 have a !ull value i! at least o!e colu&! that
per&i ts !ull values, or (" have !o !ull values a!d equate to the values o) a
pri &ary key 6 o! so&e ro' o) 2".
F1*.F
2hus i) 'e 'a!t Oopti o!al partici pati o!O i! a relati o!shi p )ro& the re)er e!ci!#
table, &ust allo' at least o!e colu&! o) > to be !ullable.
E:ample +.!. ( . ;se Re)ere!ti al 8!te#ri t y to de)i!e a! e!u&erated Do&ai!.
create table cities(ci ty varchar("/ !ot !ull,
pri &ary key (city C
create table custo&ers (cid char($ !ot !ull, c!a&e varchar(1*,
city varchar("/, disc!t real check(disc!t UI 1+./,
pri &ary key (cid, )orei#! key city re)ere!ces citiesC
8dea here is that city!a&e ca!G t appear as city colu&! o) custo&ers u!less it
also appears i! cities. 4ist all valid cities i! )irst table, i&pose a! e!u &erated
do&ai !. (0ote it takes &ore e))ort to check.
8 'ill skip e(plai !i !# rest o) Re)ere!ti al 8!te#ri t y subsectio! i! classC read it o!
your o'! (!othi !# startli !#.
2e %lter 2able -tatement
<ith Alter 2able state&e!t, ca! cha!#e the structure o) a! e(isti !# table.
7ust be o'!er o) table or have Alter privile#es (other privile#es as 'ell.
2he earlier sta!dards did!b t cover the Alter 2able state&e!t, a!d the Alter
table state&e!t i! S@4F." is too #e!eral.
So there are lots o) di))ere!ces bet'ee! products i! Alter 2able. <e cover the
di))ere!ces, a!d do!b t atte&pt a Basic S@4 )or& to bri!# the& to#ether.
O/%CLE Alter 2able, >i#. E.E, p#. $"*.
A423R 2AB43 tbl!a&e
JADD (Rcol !a&e datatype JD3>A;42 Rde)aul tLco!st] 0;44S K
JcolLco!str RcolLco!str ...S K
] tableLco!str S FF choice o) col!a&eF de). or tableLco!str
R, ...SK FF Bero or &ore added col!a&eF de)s. or tableLco!strs.
JDR16 RC14;70 colu&!!a&e ] (colu&!!a&e R, colu&!!a&e`S S K
J71D8>: (colu&!!a&e dataF type
JD3>A;42 Rde)aul tLco!st] 0;44S K JJ012K 0;44K
R, . . . SK FF Bero or &ore added col!a&eF de)s.
JDR16 C10S2RA802 co!strL!a&eK
JDR16 6R87AR: P3:K
Jdisk stora#e a!d other clauses (!ot covered, or de)erredK
Ja!y clause above ca! be repeated, i! a!y orderK
F1$/F
J30AB43 a!d D8SAB43 clauses )or co!strai !tsKC
7igure +.+ 1RAC43 Sta!dard )or Alter 2able Sy!ta(
Ca! ADD a colu&! 'i th colu&! co!strai !ts, ADD !a&ed table co!strai !ts.
(Ca!G t ADD col co!strs., but Hust shortha!d )or table co!strai !ts.
Ca! 71D8>: colu&! to !e' de)i!i ti o!: data type cha!#e )ro& varchar to
varchar", #ive !e' set o) colu&!Lco!strb s (Ca!b t do i! other products.
Ca! DR16 colu&! (i! versio! -.1.+, !a&ed co!strai !t or pri &ary key.
30AB43 a!d D8SAB43 clauses as &e!ti o!ed be)ore i! Create 2able, but 'e
do!G t use the& (portabi l i t y.
<he! 'ill C%3CP co!strai!t allo' A0: search co!di tio!M 0ot i! Core S@4F..
but a !a&ed )eature. 0o product has it. Ca! do re)ere!ti al i!te#ri t y.
D3" 4D3 Alter 2able, >i#. E.-, p#. $"*. <hat di))ere!ces )ro& 1RAC43M
Ca!b t DR16 or 71D8>: a colu&!. 2his &ay be added, so check your curre!t
docu&e!tati o!.
0ote both ADD arbi trary !u&ber o) colu&!s or tableLco!strs (48782 e(ists.
But 1RAC43 requires pare!s arou!d col!a&e] tabl eLco!str list. DB" ;DB Hust
repeats ADD phrase to set o)) di))ere!t o!es.
1N7O/M1 8 Alter 2able, >i#. E.., p#. $"*. Ca!b t 71D8>: colu&! as i! 1RAC43.
8!)or&i ( does allo' us to DR16 colu&!.
NonK Procedural and Procedural 1ntegri t y Constrai nts
0ote that !o!F procedural co!strai !ts are cdataF likedC easy to look up a!d
u!dersta!d, u!like code lo#ic (arbi trari l y co&ple(, but also lack po'er.
>or &ore )le(i bili ty, procedural co!strai !ts: 2ri##ers. 6rovided by Sybase )irst
(2FS@4, DB" ;DB, 1racle, 8!)or&i ( a!d 7icroso)t S@4 Server.
2ri##ers 'ere!b t i! S@4F.", but !o' i! S@4F... 7ig. +.!I, pg ("*.
CR3A23 2R8GG3R tri##erL!a&e B3>1R3 ] A>23R
R80S3R2 ] D34323 ] ;6DA23 J1> col!a&e R, col!a&e... S K S
10 table!a&e JR3>3R30C80G corrL!a&eLde) R, corrL!a&eLde)... S K
J>1R 3AC% R1< ] >1R 3AC% S2A237302K
J<%30 (searchLco!di ti o!K
Rstat e&e!t FF actio! (si!#le state&e!t
] B3G80 A2178C state&e!tC R state&e!tC... S 30DS FF actio! (&ul t. st&ts.
F1$1F
2he corrL!a&eLde) i! the R3>3R30C80G clause looks like:
R14D JR1<K JASK oldLro'LcorrL!a&e
] 03< JR1<K JASK !e'Lro'LcorrL!a&e
] 14D 2AB43 JASK oldLtableLcorrL!a&e
] 03< 2AB43 JASK !e'LtableLcorrL!a&eS
Ca! also use co&&a!d: DR16 2R8GG3R tri##erL!a&eC
2he tri##er is fired (e(ecuted either B3>1R3 or A>23R o!e o) the eve!ts
R80S3R2 ] D34323 ] ;6DA23 Jo) col!a&e R, col!a&e . . . SK S to the 0A73D
2AB43.
<he! tri##er is )ired, optio!al <%30 searchLco!d (i) prese!t is e(ecuted to
deter&i !e i) the curre!t subset o) ro's a))ected should e(ecute the &ul tiF
state&e!t block starti !# 'i th B3G80 A2178C. (3(a&pl e belo' )or 1RAC43.
Au&p to J>1R 3AC% R1< ] >1R 3AC% S2A237302KC ;pdate state&e!t &i#ht
update &ul ti pl e ro's, so de)aul t )ires o!l y o!ce )or &ul ti pl e ro' ;pdate.
R3>3R30C80G #ives !a&es to old ro'N!e' ro' (or old tableN!e' table, so
k!o' 'hich is re)ere!ced i! pro#ra& block.
Ro'LcorrL!a&e also used as table aliases i! <%3R3 clause (i! custo&ers,
oldLro'LcorrL!a&e (, the! (.disc!t is old value o) disc!t o! ro'.
CA0012 use ro'LcorrL!a&e o! >1R 3AC% S2A237302. (So >1R 3AC% R1< is
&ore co&&o!l y used i! practice. 2ableLcorrL!a&e is used i! >R17 clause.
0ote: you should!G t try to put a!y other table i!to this <%30 searchLco!d e
!ot eve! 'ith a Subquery.
(3.#., i) ;pdate custo&ers i! 2e(as, )ire tri##er a!d search )or custo&ers 'ith
disc!t T +, o!ly act o! ro's 'i th A0D o) those t'oC i) update adds 1 to disc!t,
clearl y i&porta!t i) 'e are talki !# aboout oldF corrF !a&es or !e'.
3ither have si!#le state&e!t o) a block o) state&e!ts bet'ee! B3G80 A2178C
a!d 30D that e(ecutes 'he! tri##eredC ca! re)er to corrL!a&es.
Ca! 'ri te procedural pro#ra& usi!# S@4F.. procedural la!#ua#e S@4N6S7
(sta!ds )or O6ersiste!t Stored 7oduleO.
1RAC43 uses 64NS@4 (B3G80 a!d 30D as i! usual 64NS@4 pro#r&s, Chapter $.
DB" has !o )ull procedural la!#ua#e but i!ve!ts si&ple o!e )or tri##ers.
0o' 1RAC43 Create 2ri##er state&e!t. 2here 'ill be ho&e'ork o! this.
F1$"F
CR3A23 J1R R364AC3K 2R8GG3R tri##erL!a&e RB3>1R3 ] A>23R ] 80S23AD
1>S
R80S3R2 ] D34323 ] ;6DA23 J1> colu&!!a&e R, colu&!!a&e... S K S
10 table!a&e JR3>3R30C80G corrL!a&eLde) R, corrL!a&eLde)... S K
R>1R 3AC% R1< ] >1R 3AC% S2A237302S
J<%30 (searchLco!di ti o!K
B3G80 state&e!t Rstate&e!tC . . . S 30DC
2he corrL!a&eLde) that provi des ro' correlati o! !a&es )ollo's:
R14D oldLro'LcorrL!a&e
] 03< !e'Lro'LcorrL!a&eS
7igure +.!" 1RAC43 Create 2ri##er Sy!ta(
0ote di))ere!ces )ro& S@4F... Ca! R364AC3 old !a&ed tri##er 'i th a !e'
o!e. 2ri##er actio! ca! occur 80S23AD 1> the 80S3R2, D34323, or ;6DA23.
Do!G t have 2AB43 corrL!a&e optio!s, o!ly R1<. 2here)ore do!G t have access
to old table i! A>23R tri##er.
1RAC43 3(a&ples usi!# 64NS@4 )or procedures e see Chapter $.
E:ample +.!. *
;se a! 1RAC43 tri##er to C%3CP the disc!t value o) a !e' custo&ers ro'
does !ot e(ceed 1+./.
create tri##er disc!tL&a( a)ter i!sert o! custo&ers
re)ere!ci!# !e' (
)or each ro' 'he! ((.disc!t T 1+./
be#i!
raiseLapplicati o!Lerror(F "///*, Gi!vali d discou!t o! i!sertG C
e!dC
N
3!ter this i! S@4W6lus, !eed ONO a)ter it to &ake it 'ork. 0ote that 2ri##er is
lo!#F lived, but could save Create 2ri##er to .c&d )ile as 'i th loadcaps.
2he raiseLapplicati o!Lerror is special )or& i! so&e products to retur! errorC
here e"///* is a! applicatio! error !u&ber pro#ra&&ers ca! assi#!.
0ote i) 'a!t to #uara!tee co!strai !t 'i th ;pdate state&e!t as 'ell, !eed to
cha!#e A>23R clause o) 3(a&ple E.1.+ Oa)ter i!sert or updateO.
E:ample +.!. )
<e use a! 1RAC43 tri##er to i&ple&e!t a re)ere!ti al i!te#ri t y policy Oo!
delete set !ullO i! the custo&ersF orders )orei#! key.
F1$*F
create tri##er )orei#!ci d a)ter delete o! custo&ers
re)ere!ci!# old ocust
)or each ro' FF !o <%30 clause
FF64NS@4 )or& starts here
be#i!
update orders set cid I !ull 'here cid I :ocust.cidC
e!dC
2his tri##er 'orks as a B3>1R3 tri##er, but accordi !# to 1RAC43
docu&e!tati o!, A>23R tri##ers ru! )aster i! #e!eral.
1P !o' 6ros a!d Co!s o) 0o!F 6rocedural a!d 6rocedural co!strai !ts starti !#
p#. $*E. <hat is the value o) havi!# these co!strai !tsM
Stops S@4 update state&e!t )ro& &aki !# a &istake, that &i#ht corrupt the
data. Re)ere!ti al i!te#ri t y is a R;43 o) the busi!ess, a!d o!e error destroys
the Oi!te#ri t yO o) the data.
Greatest da!#er is adFhoc update state&e!t. Could &ake rule that adF hoc
updates are !ot allo'ed, leave i!te#ri t y to pro#ra&&ers. But 'hat i)
i!e(peri e!ced pro#ra&&er, subtle bu#M
1R 'e could create 4A:3R o) calls, everyo!e has to access data throu#h call
layer, a!d it &akes !ecessary tests to #uara!tee R;43S. 3.#., lo#ic checks
re)ere!ti al i!te#ri t y holds. <ill have a! e(ercise to do that. 2alk about.
1!e proble& is that this &ust be a C;S217 4A:3R )or a shopC so&e shopbs
pro#ra&&i !# sta)) 'o!b t k!o' e!ou#h to do this ri#ht.
2here is a!other issue o) co!trol. 7a!a#ers o)te! a)raid o) 'hat
pro#ra&&ers &i#ht do, a!d 'a!t to see the rei!s o) co!trol i! a separate
place.
2his c>ear, ;!certai !t y, a!d Doubt d ar#u&e!t about pro#ra&&ers 'as a
&ai! 'ay i! 'hich co!strai !ts 'ere ori#i!all y sold to busi!ess &a!a#ers.
But there is real value to havi !# a li&i ted !u&ber o) !o!F procedural
co!strai !ts, 'i th a )e' para&eters: ;08@;3, >1R38G0 P3: . . . R3>3R30C3S.
Acts like DA2AQ Ca! place all co!strai !ts i! tables (Syste& Catalo#s, check
that theyG re all there. 0ot Hust 41G8C (hard to #rasp but DA2A.
But proble& 'eG ve &et be)ore. 4ots o) R;43s 'eG d like that ca!G t be put i!
ter&s o) the !o!F procedural co!strai !ts 'eG ve listed: !ot e!ou#h 61<3RQ
F1$$F
8 poi!t out t'o 'eak!esses o) !o!F procedural co!strai !ts: (1 Speci)yi !#
co!strai !t )ailure alter!ati ve, (" Guara!teei !# tra!sactio!al Co!siste!cy.
Co!strai !t )ailure alter!ati ve . 3.#., try to add !e' order a!d co!strai !t )ails F
&isspelli!# o) cid. 0o' 'hat do 'e do 'i th the order, assu&i !# 'e have the
'ro!# Bip code )or a !e' prospect addressM 2hro' it a'ayM
6ro#ra&&er has to ha!dle this, a!d itbs a pai! to ha!dle it o! every retur!
)ro& applicati o! usi!# S@4. (But 'e ca! ha!dle this usi!# 2ri##ers !o'.
2ra!sactio!al Co!siste!cy . 2here is a! accou!ti !# !eed to have Assets I
4iabili ties \ 6roprietorshi p (1'!ers 3quity. 4ots o) R;43S are i&plied.
R;43: i! &o!ey tra!s)er, &o!ey is !ei ther created !or destroyed. 8! &idst o)
tra!sactio!, &ay create or destroy &o!ey 'ithi ! si!#le accou!ts, but &ust
bala!ce out at co&&i t ti &e.
But there is !o co!strai !t (eve! 'i th tri##ers to #uara!tee co!siste!cy o) a
tra!sac tio! at co&&i t ti &e. A!d this ca! be a 53R: hard proble&.
So 'e a5e to trust pro#ra&&ers )or these hardF toF co!strai ! proble&s: <hy
try to seco!dF #uess the& )or the easy proble&s.
2he a!s'er probabl y is that !o!F procedural co!strai !ts ca! solve so&e easy
thi!#s a!d thus si&pli )y thi!#s )or pro#ra&&ers. 7ost pro#ra&&ers 'ould
support the idea o) cdataF like co!strai !tsd.
3(a&ple E.1.E. %ere 'e assu&e each CA6 'i th orders havi!# a correspo!di !#
set o) li!eF ite&s i! a table li!ei te&s 'i th )orei#! key ord!u&. 2he order is
ori#i!all y i!serted 'i th !Lite&s set to Bero, a!d 'e keep the !u&ber curre!t
as !e' li!ei te& ro's are i!serted )or a #ive! order.
create tri##er i!corder!i te&s a)ter i!sert o! li!ei te&s
re)ere!ci!# old oldli
)or each ro'
be#i! FF)or 1RAC43, leave out )or DB" ;DB
update orders set !Lite&s I !Lite&s \ 1 'here ord!o I :oldli.ord!oC
e!dC FF)or 1RAC43
create tri##er decorder!i te&s a)ter delete o! li!ei te&s
re)ere!ci!# old oldli
)or each ro'
be#i!
update orders set !Lite&s I !Lite&s F 1 'here ord!o I :oldli.ord!oC
e!dC
F1$+F
Class *.
+." .ie$s 8dea is that si!ce a Select state&e!t looks like a 5irtual 2able,
'a!t to be able to use this table i! >R17 clause o) other Select.
(Ca! do this already i! adva!ced S@4: allo' Subquery i! the >R17 clauseC
check you ca! )i!d this i! Sectio! *.,, >i#ure *.11, p# 11E
A O5ie' tableO or Hust O5ie'O &akes the Select a lo!#F lived virtual table: !o
data stora#e i! its o'! ri#ht, Hust 'i!do' o! data it selects )ro&.
5alue: si&pli )y 'hat pro#ra&&er !eeds to k!o' about, allo' us to create
virtual versio!s o) obsolete tables so pro#ra& lo#ic does!G t have to cha!#e,
add a )ield securi ty capabili ty.
Base table is created table 'ith real ro's o! disk. <eak!ess o) vie' is that it
is !ot like a base table i! every respect (li &i ts to vie' ;pdates.
%ere is e(a&pl e vie', E:ample +.". ! :
create vie' a#e!torders (ord!o, &o!th, cid, aid, pid, qty,
char#e, a!a&e, acity, perce!t
as select o.ord!o, o.&o!th, o.cid, o.aid, o.pid, o.qty,
o.dollars, a.a!a&e, a.city, a.perce!t
)ro& orders o, a#e!ts a 'here o.aid I a.aidC
0o' ca! 'ri te query (E:ampl e +.". " :
select su&(char#e )ro& a#e!torders 'here acity I G2oledoG C
Syste& basicall y tur!s this query i!to
select su&( o.dollars )ro& orders o, a#e!ts a
'here o.aid I a.aid a!d a.city I G2oledoG C
Sy!ta( i! >i#ure E.1*, p#. $*$.
CR3A23 583< vie'L!a&e J(col!a&e R,col !a&e`S K
AS subquery J <82% C%3CP 162810KC
Ca! leave out list o) col!a&es i) tar#et list o) subquery has col!a&es that
requi re !o quali)iers. 0eed to #ive !a&es )or e(pressio!s or )or a&bi#uous
col!a&es (c.city vs a.city. Ca! re!a&e colu&! i! a!y 'ay at all.
Aliases )or e(pressio!s 'ill #ive !a&es to e(pressio!s i! DB" ;DB a!d
1RAC43 (!ot 80>1R789 at prese!t, so do!G t !eed col!a&es i! Create 5ie'.
F1$,F
Recall that Subquery, de)i!ed at the be#i!!i !# o) Sectio! *.., D13S allo'
;0810 (check this is i! your !otes, class, does not allo' 1RD3R B:.
2he <82% C%3CP 162810 'ill !ot per&i t a! update or i!sert (throu#h a le#all y
updatabl e vie' that 'ould be i!visible i! the resul ti !# vie'.
E:ample +.". ( . Assu&e 'e had !o C%3CP clause o! disc!t 'he! 'e created
custo&ers. Ca! create updatabl e vie' custs that has this e))ect.
create vie' custs as select W )ro& custo&ers
'here disc!t UI 1+./ 'i th check optio!C
0o' ca!!ot i!sertNupdate a ro' i!to custs 'i th disc!t T 1+./ that 'ill take o!
ta ble custo&ers . 2his 'ill )ail )or custo&ers cid I Gc//"G (disc!t I 1":
update custs set disc!t I disc!t \ $./C
Ca! !est vie's, create a vie' depe!di !# o! other vie's. E:ample +.". * :
create vie' acorders (ord!o, &o!th, cid, aid, pid, qty,
dollars, a!a&e, c!a&e
as select ord!o, &o!th, ao.cid as cid, aid, pid, qty,
char#e, a!a&e, c!a&e
)ro& a#e!torders ao, custo&ers c 'here ao.cid I c.cidC
4isti!# vie's. All obHects o) a database are listed i! the catalo# tables
(sectio! E.$. Ca! list all base tables, all vie's, all !a&ed colu&!s, o)te! all
co!strai !ts (or !a&ed co!strai !ts, a!y'ay.
8! 9N1pe! sta!dard, 'ould list vie's as )ollo's:
select table!a&e )ro& i!)oLsche&.tabl es 'here tableLtype I G583<GC
8! 1RAC43: select vie'L!a&e )ro& userLvie'sC
1!ce you have a vie'L!a&e (or tableL!a&e )ro& userLtables ca! 'ri te i!
1RAC43:
D3SCR8B3 R vi e'L!a&e ] tableL!a&eS C
2o )i!d out e(actl y ho' the vie'Ntabl e 'as de)i!ed i! 1RAC43:
select te(t )ro& userLvie's 'here vie'L!a&e I GAG3021RD3RSGC
(0123: !a&e &ust be i! ;pper Case
F1$EF
8) you )i!d you do!G t #et the 'hole vie' de)i!i ti o! (itGs chopped o)), use the
1RAC43 state&e!t: set lo!# 1///, the! try a#ai!.
8! DB" ;DB, 'ay to list vie's )or user eo!eil is:
select vie'!a&e )ro& syscat.vi e's 'here de)i!er I G310384GC
2o delete a vie', or table (or later, i!de(, sta!dard state&e!t is:
DR16 R 2AB43 table!a&e ] 583< vie'!a&eS C
<he! table or vie' is dropped, 'hat happe!s to other obHects (vie's,
co!strai !ts, tri##ers 'hose de)i!i ti o!s depe!d o! the table or vie'M
By de)aul t i! 1RAC43, i) drop table or vie', other vie's a!d tri##ers that
depe!d o! it are &arked i!valid.
4ater, i) the table or vie' is recreated 'ith the sa&e releva!t colu&! !a&es,
those vie's a!d tri##ers ca! beco&e usable a#ai!.
<ith JCASCAD3 C10S2RA802SK i! 1RAC43, co!strai !ts re)erri !# to this table
(e.#., )orei#! key re)ere!ces are dropped. (See >i#ure E.1$.
8) there is !o such clause, the! the Drop )ails (R3S2R8C2, e))ect !a&ed i!
80>1R789 a!d S@4F...
8! 80>1R789 have J CASCAD3 ] R3S2R8C2KC re)ers to co!strai !ts.a!d also
!esti!# vie's.
8! DB", !o optio!s. 8!validates vie's D tri##ers depe!di !# o! 'hat droppedC
retai! de)i!i ti o! i! syste& catalo#, but &ust be recreated later.
4pdat abl e and /eadK Only .ie$s.
2he proble& is that a 5ie' is the resul t o) Select, &aybe a Hoi!. %o' do 'e
tra!slate updates o! the 5ie' i!to cha!#es o! the base tablesM
2o be updatabl e (as opposed to a readF o!ly, a vie' &ust have the )ollo'i !#
li&i tati o!s i! its vie' de)i!i ti o! i! the 9N1pe! sta!dard (>i#. E.1+, p# $$+:
(1 2he >R17 clause &ust have o!ly a si!#le table (i) vie', updatabl e.
(" 0either the GR1;6 B: !or %A580G clause is prese!t.
(* 2he D8S280C2 key'ord is !ot prese!t.
($ 2he <%3R3 clause has !o Subquery re)ere!ci !# a table i! >R17 clause.
(+ Result colu&!s are si&ple: !o e(pressio!s, !o col appears T o!ce.
F1$-F
>or e(a&pl e, i) disobey (*, the disti!ct clause is prese!t, 'ill select o!e o)
t'o ide!tical ro's. 0o' i) update that ro' i! the vie', 'hich o!e #ets up F
dated i! base tablesM BothM 0ot co&pletel y clear 'hat is 'a!ted.
(;pdate custo&er S&ith to have lar#er disc!t. 2'o custo&ers 'i th ide! ti cal
!a&es, disc!ts i! 5ie', but di))ere!t cid (!ot i! vie'. 8!e(peri e!ced proF
#ra&&er &i#ht update throu#h this vie', !ot di))ere!ti ate.
4i&i tati o!s (1 &ea!s 104: 103 2AB43 8051453D i! vie'. 3(a&pl e E.".,
sho's 'hy &i#ht 'a!t this, because o) vie' colocated:
create vie' colocated as select cid, c!a&e, aid, a!a&e, a.city as acity
)ro& custo&ers c, a#e!ts a, 'here c.city I a.cityC
3(a&ple ro's ()ro& Chapt ". %ave:
c//" Basics a/, S&ith Dallas
a!d
c//* Allied a/, S&ith Dallas
Co!sider delete o) seco!d ro'. %o' achieveM Delete c//*M cha!#e city o)
c//* (to 'hatM. Delete a/,M 2he! delete )irst ro' o) vie' as 'ell.
012 C43AR <%A2 R3@;3S21R <A02SQQ S8D3F3>>3C2S &i#ht be u!e(pected.
6roble& o) GR1;6 B: (". 3(a&pl e E.".-. Recall a#e!tsales vie':
create vie' a#e!tsales (aid, totsales as select aid, su&(dollars
)ro& orders #roup by aidC
0o' co!sider update state&e!t:
update a#e!ts set totsales I totsales \ 1///./ 'here aid I Ga/*G C
%o' achieve thisM Add !e' sale o) Y1///./M 2o 'ho&M <hat productM
Cha!#e a&ou!ts o) curre!t ordersM <hich o!esM By ho' &uchM
AGA80 012 C43AR <%A2 ;S3R <A02S.
But li&i tati o!s are too severe as they sta!d. Assu&e that a#e!tsales
co!tai !ed a#e!t city ('i th aid a!d 'e 'a!ted to up date the city )or aidI
Ga/*G . Clear 'hat user 'a!ts but 012 A441<3D by rules o) >i#. E.1+.
2e .alue of .ie$s
F1$.F
1ri#i!al idea 'as to provi de a 'ay to si&pli)y queries )or u!sophisticated
users (allo' libararia! access to stude!t records a!d &ai!tai ! old obsolete
tables so code 'ould co!ti !ue to 'ork.
But i) !eed to do updates, ca!G t Hoi! tables. 0ot as use)ul as it should be.
%o&e'ork o! 'he! updates 'ork o! 1RAC43 Hoi!ed tables. Also as S@4F..
special )eature (!ot i! Core a!d i! 1DBC.
8! 1RAC43 ca! update Hoi! vie's i) Hoi! is 0F1 a!d table o! 0 side has pri &ary
key de)i!ed (lossless Hoi!.
Still !o )u!cti o! operators (Set or Scalar or other co&ple( e(pressio! resul t
colu&!s, GR1;6 B:, or D8S280C2C %ereGs the idea i! 0F1 Aoi!.
Co!sider vie' ords(a#e!ts (assu&i !# a#e!ts has pri &ary key aid:
create vie' ords(a#e!ts as
select ord!o, cid, (.aid as aid, pid, dollars, a!a&e, perce!t
)ro& orders (, a#e!ts a 'here (.aid I a.aid a!d dollars T +//.//C
Ca! update all colu&!s )ro& the orders table (the 0 side o) the 0F1 Hoi! !ot
i! Hoi! co!di tio! ((.aid, but !o!e )ro& a#e!ts (1 side o) the 0F1 Hoi!.
8) #ive the )ollo'i !# co&&a!d to list the updatabl e colu&!s, 'ill #et:
select colu&!L!a&e, updatabl e )ro& userLupdatabl eLcolu&!s
'here tableL!a&e I G1RDS9AG302SGC
C14;70L0A73 ;6D
FFFFFFFFFFFFFFF FFF
C8D :3S0123: ca! update o!ly colu&!s i! table
A8D 01 'i th si!#leF valued participati o!, i.e..
68D :3Sorders, e(cept colu&!s i!vol ved i! Hoi!.
D144ARS :3S
A0A73 01
63RC302 01
1RD01 :3S
E ro's selected.
F1+/F
Class ).
+.' -ecuri ty. Basic S@4 (9N1pe! Sta!dard. p# $$*
GRA02 R A44 6R85843G3S ] privile#e R,pri vi l e#e`S S
o! J2AB43K table!a&e ] vie'!a&e
21 R 6;B48C ] userF !a&e R, userF !a&e`S S J <82% GRA02 162810K
(1racle adds roleF !a&e i! 21 list.
privile#es are: S343C2, D34323, 80S3R2, ;6DA23 J(col!a&e R, col!a&eS K ,
R3>3R30C3S J(col!a&e R, col!a&eS K (this #ra!ts the ri#ht to re)ere!ce the
speci)ied colu&!s )ro& a )orei#! key
2he <82% GRA02 162810 &ea!s user ca! #ra!t other user these sa&e priv F
ili#es.
2he o'!er o) a table auto&ati cal l y has all privili#es, a!d they ca!!ot be
revoked. 3(a&ple E.*." (that 8 &i#ht #ive:
#ra!t select, update, i!sert o! orders to eo!eilC
#ra!t all privile#es o! products to eo!eilC
2he! eo!eil ca! 'ri te:
select W )ro& po!eil.ordersC
2he po!eil. pre)i ( is called the sche&a !a&e FF See p#. $+* i! te(t.
:ou ca! access a!y userGs tables i! the sa&e database (all o) us are i! the
sa&e database i) you have appropriate privili #es.
Ca! li&i t user select access to speci)ic colu&!s o) a base table by creat i!# a
5ie' a!d the! #ra!ti !# access to the vie' but 012 the u!derl yi !# base ta ble.
3(a&ple E.*.*
create vie' custvie' as select cid, c!a&e, city )ro& custo&ersC
#ra!t select, delete, i!sert, update (c!a&e, city o! custvi e' to eo!eilC
0o' eo!eil has Select access to cid, c!a&e, city B;2 012 disc!t o!
po!eil.custo &ers. J CL%--: 2hi!k ho' to create 3(a& @uestio! out o) this.K
8761R2A02: ;ser does 012 !eed privile#e o! a base table )or privile#es
#ra!ted throu#h a vie' to OtakeO.
F1+1F
Could #ive stude!t librari a! access to other stu de!tsG address, pho!e 'ithout
#ivi!# access to &ore private i!)o.
Ca! eve! #ive access to &a!a#er only to e&ployees &a!a#ed.
create vie' &#rLe//* as select W )ro& e&ployees 'here &#ri d I e//*C
#ra!t all o! &#erLe//* to idLe//*C
Sta!dard is to drop privile#es by co&&a!d:
R351P3 R A44 6R85843G3S ] priv R, priv`S S o! table!a&e ] vie'!a&e
>R17 R 6;B48C ] user R, user`S S JCASCAD3 ] R3S2R8C2KC
8! 9N1pe!, &ust speci)y either CASCAD3 or R3S2R8C2 at e!d i) privile#es )or
other tables are based o! the privile#es bei!# revoked.
But i! 1RAC43, de)aul t is R3S2R8C2. 8) i!clude clause CASCAD3 R3S2RA802S
the! re)ere!ti al i!te#ri t y co!strai !ts 'ill be dropped i! cascade )asio!.
2here are lots o) other privile#es supplied by DB" a!d 1RAC43, e.#.:
Create Database privile#es (DBAD7 i! 1RAC43, privile#es to look at or
cha!#e query e(ecuti o! pla!s, per)or& 41AD opera tio!s o! tables, etc.
8! order to look at a!other perso!Gs tables, !eed !ot be i! hisNher database
(actuall y usuall y all i! sa&e database 'i th 1RAC43. Gra!t #ives you access.
+.( -ystem Catalogs.
8dea clear: all obHects created by S@4 co&&a!ds are listed as obHects i! tables
&ai!tai !ed by the syste&. 1racle calls this: data dictio!ary
2he !a&es o) these tables are very !o!F sta!dard, but at least there is a ta ble
)or tables (A44L2AB43S, DBAL2AB43S, a!d ;S3RL2AB43S i! 1racle a!d a
table )or colu&!s (e.#., ;S3RL2ABLC14;70S.
DBA visi ti!# a!other site 'ould look at catalo# tables (&etaF data to )i!d out
'hat tables e(ist, 'hat colu&!s i! the&, the si#!i)ica!ce o) the col u&!s
(descripti ve te(t stri!#, a!d so o!.
1RAC43 Pey )or A44L2AB43S is 2AB43L0A73, key )or A44L2ABLC14;70S is
2AB43L0A73 C14;70L0A73. Descripti o!s o) keys i! A44L2ABLC14;70S a!d
A44LC14LC177302S. 2ables )or privili #es #ra!ted, co!strai !ts, i!de(es.
8! 1racle, ;S3RL2AB43S, )or e(a&pl e, co!tai !s: !u&ber o) ro's, a!d disk
space usa#e statistics, such as avera#e ro' le!#th i! bytes, etc.
F1+"F
D3SCR8B3 co&&a!d to )i!d colu&!!a&es D colu&!t ypes o) a user table.
describe custo&ersC
Could also do this throu#h userLtabLcolu&!s, but &ore o) a pai!, !ot as
!icely laid out. 8) 'a!t to describe, say, userLtabLcolu&!s, 'ri te:
describe userLtabLcolu&!sC
8t used to be, prior to 1RAC43 -.i, that 'e had to 'ri te:
describe O6;B48CO.userLtabLcolu&!sC
2he double quotes arou!d O6;B48CO 'ere !ecessaryQ <hat other data
dictio!ary tables are thereM Actuall y they are vie'sQ 2ry this:
spool vie'.!a&es
select vie'L!a&e )ro& allLvie's 'here o'!er I GS:SG a!d
vie'L!a&e like GA44L[G or vie'L!a&e like G;S3RL[GC
0ote that a!y si!#leF quoted values like GA44L[G &ust be i! CA6S. %ere are
vie'.!a&es you &i#ht )i!d. 3.#.:
583<L0A73
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
;S3RLR3S1;RC3L48782S
;S3RLR143L6R85S
;S3RLS3G7302S
;S3RLS3@;30C3S
;S3RLS0A6S%12S
;S3RLS0A6S%12L41GS
;S3RLS0A6S%12LR3>R3S%L2873S
;S3RLS1;RC3
;S3RLS:010:7S
;S3RLS:SL6R85S
;S3RL2AB43S UII I
583<L0A73
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
;S3RL2AB43S6AC3S UIII
;S3RL2ABLC14;70S UII I
;S3RL2ABLC14LS2A28S28CS
;S3RL2ABLC177302S UII I
;S3RL2ABL%8S21GRA7S
;S3RL2ABL6AR282810S
;S3RL2ABL6R85S UII I
;S3RL2ABL6R85SL7AD3
;S3RL2ABL6R85SLR3CD
;S3RL2R8GG3RS UIII
F1+*F
;S3RL2R8GG3RLC14S UII I
583<L0A73
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
;S3RL2SL@;12AS
;S3RL2:63S
;S3RL2:63LA22RS
;S3RL2:63L732%1DS
;S3RL;6DA2AB43LC14;70S UII I
;S3RL;S3RS UII I
;S3RL583<S UII I
2hi!k about ho' i! a pro#ra& to e(plore !e' databases (7icroso)t 3(plorer
'ould 'a!t to use dy!a&ic S@4, )i!d out 'hat tables are arou!d, )i!d all
colu&!s i! the&, #ive 8co! i!ter)ace, list vie's, de)i!i ti o!s, etc.
DB" has provi ded the capabili ty to cha!#e the @uery Access 6la!.
DB" Catalo# 2ables are too co&ple( to #o over 'ithout a! 8B7 &a!ual.
8 'ill skip over covera#e o) obHectF relati o!al catalo#s. 5ery i&porta!t 'he!
youG re actuall y usi!# obHectF relatio!al obHects, o) courseQ
Capter ,. 1nde:ing.
-.1 1vervi e'. ;sually, a! i!de( is like a card catalo# i! a library. 3ach card
(e!try has:
(keyval ue, ro'F poi!ter (keyval ue is )or lookup, call ro'F poi!ter R1<8D
(R1<8D is e!ou#h to locate ro' o! disk: o!e 8N1
3!tries are placed i! Alphabeti cal order by lookup key i! OBFtreeO (usuall y,
e(plai!ed belo'. Also &i#ht be hashed.
A! i!de( is a lot like &e&ory reside!t structures youG ve see! )or lookup:
bi!ary tree, "F*Ftree. But i!de( is disk reside!t. 4ike the data itsel), o) te!
'o!G t all )it i! &e&ory at o!ce.
9N1630 Sy!ta(, >i#. -.1, p#. $,E, is e(tre&el y basic (!ot &uch to sta!dard.
CR3A23 J ;08@;3K 80D39 i!de(!a&e 10 table!a&e (col!a&e J ASC ] D3SCK
R,col !a&e J ASC ] D3SCK . . .SC
&nde* 'ey created is a co!cate!ati o! o) colu&! values. 3ach i!de( e!try
looks like (keyval ue, recordpoi !ter )or a ro' i! custo&ers table.
F1+$F
A! i!de( e!try looks like a s&all ro' o) a table o) its o'!. 8) created
co!cate!ated i!de(:
create i!de( a#!a&ci t o! a#e!ts (a!a&e, cityC
a!d had (a!a&e, city )or t'o ro's eual to (S782%, 3A210 a!d (S782%3,
A210, the syste& 'ould be able to tell the di))ere!ce. <hich o!ce co&es
earlier alphabeti call yM
A)ter bei!# created, i!de( is sorted a!d placed o! disk. Sort is by colu&!
value asc or desc, as i! S1R2 B: descripti o! o) Select state&e!t.
0123: 4A23R C%A0G3S to a table are i&&edi atel y re)lected i! the i!de(,
do!G t have to create a !e' i!de(.
3( -.1.1. Create a! i!de( o! the city colu&! o) the custo&ers table.
create i!de( cities( o! custo&ers (cityC
0ote that a si!#le city value ca! correspo!d to &a!y ro's o) custo&ers ta ble.
2here)ore ter& i!de( key is quite di))ere!t )ro& relatio!al co!cept o) pri &ary
key or ca!didate key . 412S 1> C10>;S810 AB1;2 2%8S.
<ith !o i!de( i) you #ive the query:
select W )ro& custo&ers 'here city I GBosto!G
a!d disc!t bet'ee! 1" a!d 1$C
0eed to look at every ro' o) custo&ers table o! disk, check i) predicates hold
(2AB43 SCA0 or 2AB43S6AC3 SCA0.
6robabl y !ot such a bad thi!# 'i th o!l y a )e' ro's as i! our CA6 da tabase
e(a&pl e, >i# ".". (But bad i! DB" )ro& sta!dpoi !t o) co!curre!cy.
A!d i) 'e have a &illio! ro's, tre&e!dous task to look at all ro's. 4ike i!
4ibrary. <ith i!de( o! city value, ca! 'i!!o' do'! to (&aybe 1/,///
custo&ers i! Bosto!, like searchi!# a s&all )ractio! o) the volu&es.
;ser does!G t have to say a!ythi !# about usi!# a! 8!de(C Hust #ives Select
state&e!t above.
@uery 1pti &iBer )i#ures out a! i!de( e(ists to help, creates a @uery pla!
(Access pla! to take adva!ta#e o) it FF a! Access pla! is like a 6R1GRA7 that
e(tracts !eeded data to a!s'er the Select.
F1++F
Class +.
Recall 9N1630 )or& o) Create 8!de(:
CR3A23 J ;08@;3K 80D39 i!de(!a&e 10 table!a&e (col!a&e J ASC ] D3SCK
R,col !a&e J ASC ] D3SCK . . .SC
2he u!ique clause o) create i!de( ca! be used to #uara!tee u!ique!ess o) a
ca!didate table key. 3(a&ple -.1.", i!stead o) declari!# cid a pri &ary key i!
the Create 2able state&e!t:
create u!ique i!de( cid( o! custo&ers (cidC
1P i) &ul ti pl e !ulls, like Create 2able 'i th colu&! ;08@;3.
<he! use Create 2able 'i th ;08@;3 or 6R87AR: P3: clause, this causes a
u!ique i!de( to be created u!der the covers. Ca! check this, queryi !#:
;S3RL80D393S
;S3RL80DLC14;70S
1P, !o' the 9N1630 sta!dard does!G t say &uch about 'hatG s possible 'ith
i!de(es. >oreshado'i !#s.
Di))ere!t i!de( types: BFtree, %ashed. ;sually, BFtree (reall y B
\
Ftree.
6ri&ary i!de(, seco!dary i!de(, clustered i!de(. 6ri&ary i!de( &ea!s ro's
are actuall y i! the i!de( structure i! the place o) e!tries poi!ti !# to ro's.
Clustered i!de( &ea!s ro's i! sa&e order as i!de( e!tries (volu&es o!
shalves )or Dicke!s all i! o!e area: &ay be pri &ary i!de(, &ay !ot.
Di))icul t to appreciate the i!de( structures 'ithout u!dersta!di !# disk access
properti es: disk 8N1 is e(cruciati !#l y slo', &ost o) i!de( desi#! is directed at
savi!# disk 8N1: eve! bi!ary search o) list i!appropri ate o! disk.
,." Disk storage.
Co&puter &e&ory is very )ast but 5olatile stora#e. (4ose data i) po'er
i!terrupti o!. Disk stora#e is very slo' but !o!F volati le (like, &ove data )ro&
o!e co&puter to a!other o! a diskette a!d very cheap .
7odel 1// 786S co&puter. 2akes .///////1 seco!ds to acess &e&ory a!d
per)or& a! i!structi o!. Getti !# )aster, like car )or Y. to take you to the
&oo! o! a #allo! o) #as.
F1+,F
Disk o! the other ha!d is a &echa!ical device ? has!G t kept up.
(Dra' picture. rotati !# platters, &ul ti pl e sur)aces. disk ar& 'i th head
asse&blies that ca! access a!y sur)ace.
Disk ar& &oves i! a!d out like oldF style pho!o#raph ar& (top vie'.
<he! disk ar& i! o!e positio!, cyli!der o) access (pictureM. 1! o!e sur )ace
is a circle called a track. A!#ular se#&e!t called a sector.
2o access data, &ove disk ar& i! or out u!til reach ri#ht track (Seek ti &e
<ait )or disk to rotate u!til ri#ht sector u!der head (rotati o!al late!cy
Bri!# head do'! o! sur)ace a!d tra!s)er data )ro& a D8SP 6AG3 (" PB or $PB:
data block, i! 1RAC43 (2ra!s)er ti &e. Rou#h idea o) ti &e:
Seek ti &e: .//- seco!ds
Rotatio!al late!cy: .//$ seco!ds (a!al yBe
2ra!s)er ti &e: .///+ seco!ds ()e' &illio! bytesNsec: e' P bytes
2otal is ./1"+ seco!ds I 1N-/ seco!d. %u#e co&pared to &e&ory access.
2ypicall y a disk u!i t addresses te! Gi#abytes a!d costs 1 thousa!d dollars
(!e' )i#ure 'ith disk ar& attachedC !ot Hust pack 'hich is &uch cheaper.
+1" bytes per sector, "// sectors per track, so 1//,/// bytes per track. 1/
sur)aces so 1,///,/// bytes per cyli!der. 1/,/// cyli! ders per disk pack.
2otal is 1/ GB (i.e., #i#abytes
0o' takes ./1"+ seco!ds to bri!# i! data )ro& disk, .///G ///G /1 seco!ds to
access (byte, lo!#'ord o) data i! &e&ory. %o' to picture thisM
A!alo#y o) 5oltai reG s secretary. Copyi!# letters at o!e 'ord a seco!d. Ru!
i!to 'ord ca!G t spell. Se!d letter to St 6etersber#, si( 'eeks to #et re spo!se
(1E-/. Ca!G t do 'ork u!til respo!se ('ork o! other proHects.
>ro& this see 'hy read i! 'hat are called pa#es, "P bytes o! 1RAC43, $P
bytes i! DB" ;DB. <a!t to &ake sure 5oltaire a!s'ers all our questio!s i!
o!e letter. Structure 8!de(es so take adva!ta#e o) a lot o) data to#ether.
3uffer Lookaside
Si&ilarl y, idea o) bu))er lookaside. Read pa#es i!to &e&ory bu))er so ca!
access the&. 1!ce to ri#ht place o! disk, tra!s)er ti &e is cheap.
F1+EF
3veryti &e 'a!t a pa#e )ro& disk, hash o! dkp#addr, h(dkp#addr to e!try i!
%ashlookaside table to see i) that pa#e is already i! bu))er. (6#. $E*
8) so, saved disk 8N1. 8) !ot, drop so&e pa#e )ro& bu))er to read i! re quested
disk pa#e. 2ry to )i( it so popular pa#es re&ai ! i! bu))er.
A!other poi!t here is that 'e 'a!t to )i!d so&ethi !# )or C6; to do 'hile
'ai ti !# )or 8N1. 4ike havi !# other tasks )or 5oltai reG s secretary.
2his is o!e o) the adva!ta#es o) &ul tiF user ti &eshari !#. Ca! do C6; 'ork )or
other users 'hile 'ai ti !# )or this disk 8N1.
8) o!ly have /.+ &s o) C6; 'ork )or o!e user, the! 'ai t 1".+ &s )or a!other
8N1. Ca! i&prove o! by havi!# at least "+ disks, tryi !# to keep the& all busy,
s'itchi !# to di))ere!t users 'he! disk access do!e, ready to ru!.
<hy bother 'i th all thisM 8) &e&ory is )aster, 'hy !ot Hust use it )or da tabase
stora#eM 5olatili t y, but solved. Cost !o lo!#er bi# )actor:
7e&ory stora#e costs about a Y$/// per #i#abyte.
Disk stora#e ('i th disk ar&s costs about 1// dollars a Gi#abyte.
So could buy e!ou#h &e&ory so bri!# all data i!to bu))ers (!o proble& about
)ast access 'i th &illio!s o) &e&ory bu))ersC very e))icie!t access.
Co&i!# close to thisC probabl y 'ill do it soo! e!ou#h. Ri#ht !o', bei!# li&i ted
by $ GBytes o) addressi!# o! a *" bit &achi!e.
F1+-F
Class ,. Create 2ablespace
1P, !o' DBA a!d disk resource allocati o! i! 1RAC43.
<e have avoided disk resource co!siderati o!s up to !o' because so hard to
ha!dle i! sta!dard. All the co&&ercial syste&s are qui te di))ere!t i! de tail.
But a lot o) co&&o! proble&s.
A tablespace is buil t out o) 1S )iles (or ra' disk parti ti o! J2392K, a!d ca!
cross )iles (disks See >i#. -.*, p#. $E,. Se#&e!t ca! also cross )iles.
All the products use so&ethi !# like a tablespace. DB" uses tablespace.
8!)or&i ( uses dbspaces.
2ablespaces have Hust the ri#ht properti es, !o &atter 'hat 1S reall y liv i!# o!
FF #e!eral layer i!sulates )ro& 1S speci)ics. 2ypicall y used to de)i!e a table
that crosses disks.
See >i#. -.* a#ai!. <he! table created, it is #ive! a data se#&e!t, 8!de( a!
i!de( se#&e!t. A se#&e!t is a u!i t o) allocatio! )ro& a tablespace.
DBA pla!. Co!struct 2ablespace )ro& operati !# syste& )iles (or disk parti F
tio!s. Ca! speci)y tablespace crosses disks, stays o! o!e disk, etc.
2ablespace is the basic resource o) disk stora#e, ca! #ra!t user R3S1;RC3
privile#e i! 1RAC43 to use tablespace i! creati !# a table.
A!y 1RAC43 database has at least o!e tablespace, !a&ed S:S237, created
'i th Create Database: holds syste& tables. 0o' see >i# -.$, p#. $E,.
J Lea5e 4pK
CR3A23 2AB43S6AC3 tblsp!a&e
DA2A>843 G)ile!a&eG JS8V3 ! JP]7KK JR3;S3K JA;2139230D 1>>
] A;2139230D 10 J0392 ! JP]7K J7A9S8V3 R;0487823D ] ! JP]7K S K
R, G)ile!a&eG . . . S
FF the )ollo'i !# optio!al clauses ca! co&e i! a!y order
J104803 ] 1>>4803K
JD3>A;42 S21RAG3 (J80828A4 !K J0392 !K J780392302S !K
J7A9392302S R!];0487823DSK J6C280CR3AS3 !K
(addi ti o!al D3>A;42 S21RAG3 optio!s !ot coveredK
J78087;7 392302 ! JP]7KK
Jother optio!al clauses !ot coveredKC
1perati !# syste&s )iles !a&ed i! data)il e clause. 1RAC43 is capable o)
creati !# the& itsel)C the! DBA loses abili ty to speci)y particular disks.
F1+.F
2he idea here is that 1RAC43 (or a!y database syste& CA0 create its o'!
O)ilesO )or a tablespace.
8) S8V3 key'ord o&i tted, data )iles &ust already e(ist, 1RAC43 'ill use. 8)
S8V3 is de)i!ed,1RAC43 'ill !or&all y create )ile. R3;S3 &ea!s use e(isti !#
)iles !a&ed eve! 'he! S8V3 is de)i!edC the! 1RAC43 'ill check siBe is ri#ht.
8) A;2139230D 10, syste& ca! e(te!d siBe o) data)ile. 2he 0392 ! JP]7K
clause #ives siBe o) e(pa!sio! 'he! !e' e(te!t is created. 7A9S8V3 li&i t.
8) tablespace created o))li!e, ca!!ot i&&edi atel y use )or table creati o!. Ca!
alter o))li!eNo!li !e later )or recovery purposes, reor#a!iBati o!, etc., 'i thout
bri!#i !# do'! 'hole database.
S:S237 tablespace, created 'ith Create Database, !ever o))li!e.
<he! table )irst created, #ive! a! i!itial disk space allocatio!. Called a!
i!itial e*tent . <he! load or i!sert ru!s out o) roo&, addi tio!al allocati o!s are
provi ded, each called a O!e(t e(te!t O a!d !u&bered )ro& 1.
Create 2ablespace #ives D3>A;42 values )or e(te!t siBes a!d #ro'th i!
S21RAG3 clauseC Create 2able ca! overri de these.
80828A4 !: siBe i! bytes o) i!itial e(te!t: de)aul t 1/"$/
0392 !: siBe i! bytes o) !e(t e(te!t 1. (sa&e 7ay #ro'.
7A9392302S !: &a(i &u& !u&ber o) e(te!ts se#&e!t ca! #et
780392302S !: start at creati o! 'ith this !u&ber o) e(te!ts
6C280CR3AS3 !: i!crease )ro& o!e e(te!t to !e(t. de)aul t +/.
7i!i &u& possible e(te!t siBe is "/$- bytes, &a( is $/.+ 7e#abytes, all
e(te!ts are rou!ded to !earest block (pa#e siBe.
2he 78087;7 392302 clause #uara!tees that Create 2able 'o!G t be able to
overri de 'i th too s&all a! e(te!t: e(te!t belo' this siBe ca!G t happe!.
0e(t, Create 2able i! 1RAC43, >i#ure -.+, p#. $E-. ( Lea5e 4p
CR3A23 2AB43 Jsche&a.K tabl e!a&e
(Rcol !a&e datat ype J D3>A;42 Rco!sta!t ]0;44S K JcolLco!strK R,
colLco!str`S
] tableLco!str S
R, col!a&e datat ype etc. . . . S
J 1RGA08VA2810 %3A6 ] 1RGA08VA2810 80D39 ('i th clauses !ot coveredK
J 2AB43S6AC3 tblsp!a&eK
J S21RAG3 (J 80828A4 ! JP]7KK J 0392 ! JP]7KK J 780392302S !K
F1,/F
J 7A9392302S !K J 6C280CR3AS3 !K K
J6C2>R33 !K J6C2;S3D !K
Jother disk stora#e a!d update t( clauses !ot covered or de)erredK
J AS subqueryK
1RGA08VA2810 %3A6 is de)aul t: as i!sert !e' ro's, !or&all y placed le)t to
ri#ht. 2race ho' !e' pa#es (data blocks are allocated, e(te!ts. 0ote i)
space o! old block )ro& deleteG s, etc., &i#ht )ill i! those.
1RGA08VA2810 80D39 &ea!s place ro's i! A BF2R33 80D39 ('ill see i! place
o) e!tri es. 1RD3R B: pri &ary keyQ
2he S21RAG3 clause describes ho' i!itial a!d successive allocati o!s o) disk
space occur to table (data se#&e!t. 2here ca! be a de)aul t stora#e clause
'i th a tablespace, i!heri ted by table u!less overri dde!.
2he 6C2>R33 ! clause deter&i !es ho' &uch space o! each pa#e used )or
i!serts be)ore stop (leave space )or varchar e(pa!d, Alter table !e' cols.
6C2>R33 !, ! #oes )ro& / to .., de)aul t 1/.
2he 6C2;S3D ! clause speci)ies a co!di tio! 'here i) pa#e (block #ets e&pty
e!ou#h, i!serts 'ill start a#ai!Q Ra!#e ! )ro& 1 to .., de)aul t $/.
Require 6C2>R33 \ 6C2;S3D U 1//, or i!vali d. 6roble& o) hysteresisC do!G t
al'ays 'a!t to be s'itchi !# )ro& i!serti !# to !ot i!serti !# a!d back.
3.#., i) 6C2>R33 1/ 6C2;S3D ./, the! stop i!serrts 'he! T./[ )ull, start
'he! U./[ )ull. 8) "/, ./, illFde)i!ed bet'ee! -/ a!d ./.
Data -torage Pages and /o$ PointersC 7ig. ,.)
1 2 . . . N
Row N Row N-1
. . .
Row 1
Header
Info Row Directory
Data Rows
FREE SPACE
2ypicall y, a)ter table is created, e(te!ts are allocated, ro's are placed o!e
a)ter a!other o! successive disk pa#es (i! 80GR3S, call heap stora#e. 7ost
products have very little capabili ty to place ro's i! a!y other 'ay.
A ro' o! &ost archi tectures is a co!ti#uous seque!ce o) bytes. See >i#ure
-., (p#. $-/: 0 ro's placed o! o!e pa#e (called a +loc' i! 1RAC43.
F1,1F
%eader i!)o !a&es the ki!d o) pa#e (data se#&e!t, i!de( se#&e!t, a!d the
pa#e !u&ber (i! 1RAC43 the 1S )ile a!d pa#e !u&ber. Ro's added ri#ht to
le)t )ro& ri#ht e!d o! block.
Ro' Directory e!tries le)t to ri#ht o! le)t a)ter header. Give o))sets o)
correspo!di !# ro' be#i!!i !#s. 6rovide !u&ber o) ro' slot o! pa#e.
<he! !e' ro' added, tell i&&edi atel y i) 'e have )ree space )or !e' direc F
tory e!try a!d !e' ro'. Co!ceptual l y, all space i! &iddle, i&plies 'he!
delete ro' a!d reclai & space &ust shi)t to )ill i! #ap. (Ca! de)er that.
Also &i#ht have O2able DirectoryO i! block 'he! have C4;S23R. (4ater.
2hereGs overhead )or colu&! values 'ithi ! a ro' (o))sets, !ot sho'! here.
7ust &ove ro's o! p# i) updates cha!#es char varyi !# )ield to lar#er siBe.
F1,"F
Class ;. /e5ie$ 7ig. ,.) abo5e @put on board, discussB
Disk 6oi!ter. R8D (DB", R1<8D (1RAC43, 28D (80GR3S. A ro' i! a table ca!
be u!iquel y speci)ied 'i th the pa#e !u&ber (6 a!d slot !u&ber (S. 8!
80GR3S, have 28D (2uple 8D, #ive! by:
28D I +1"W6 \ S
6a#es i! 80GR3S are !u&bered successivel y 'ithi ! the table allocati o!, )ro&
Bero to "
"*
F 1: all 1Gs i! "* bit positio!s. Slots are / to +11. all 1Gs i! . bit
positio!s: total co!tai !er is *" bits, u!si#!ed i!t, $ bytes.
So i) ro's are +// bytes lo!# (s&all variati o!, "P byte pa#es i! ;089 'ill
co!tai ! o!l y $ ro's, a!d 28Ds #o: /, 1, ", *, +1", +1*, +1$, +1+, 1/"$, 1/"+, .
. .
Note 5alue of informati on iding . Give ro' ptr i! ter&s o) 6a#e a!d Slot
!u&berC i) #ave byte o))set i!stead o) slot !u&ber 'ould have to cha!#e
R8DNR1<8DN28D 'he! reor#a!iBed pa#e
E:ample ,.". !, pg (,!, @5ari ant B . 80GR3S DBA (or a!y user ca! check
space cal culatio!s are ri#ht ("// byte ro's 1/ to " PB disk pa#e, &ake sure
k!o's 'hatG s #oi!# o! by selecti!# 28D )ro& table )or successive ro's:
select tid )ro& e&ployees 'here tid UI 1/"$C
0ote tid is a 5irtual colu&! associated 'i th every table, so ca! select it. <ill
appear i! order (tablespace sca! #oes to )irst p#, )irst ro', the! . . .
A DB" record poi!ter is called a R8D, also $ bytes, e!codi!# pa#e !u&ber
'i thi ! 2ablespace a!d slot !u&ber, but DB" R8D structure is !ot public, a!d
'e ca!G t Select a DB" R8D )ro& a tableas a virtual colu&!.
8! 1RAC43, ro' poi!ter is called R1<8D. a!d is !or&all y ) bytes longDD
Restricted R1<8D display represe!tati o! is &ade up o) Block !u&ber (pa#e
'i thi ! 1S )ile, Slot !u&ber i! block, D )ile !o. ('hyM:
BBBBBBBB.RRRR.>>>> (each he(adeci &al, total o) - bytes )or R1<8D
(Block !u&ber, Ro' (Slot !u&ber, >ile !u&ber
2he R1<8D value )or each ro' ca! be retrieved as a virtual colu&! by a! S@4
Select state&e!t o! a!y table, as 'i th the )ollo'i !# query:
select c!a&e, ro'i d )ro& custo&ers 'here city I GDallasG C
F1,*F
'hich &i#ht retur! the )ollo'i !# ro' i!)or&ati o! (i) the R1<8D retri eved is i!
restricted )or&:
CNAME CROWID
------------ ------------------
Basics 00000EF3.0000.0001
Allied 00000EF3.0001.0001
2he alter!ati ve ce(te!ded R1<8Dd )or& is displayed as a stri!# o) )our
co&po!e!ts havi!# the )ollo'i !# layout, 'ith letters i! each co&po!e!t
represe!ti !# a baseF ,$ e!codi!#:
111111>>>BBBBBRRR
%ere 111111 is the data obHect !u&ber, a!d represe!ts the database
se#&e!t (e.#., a table. 2he co&po!e!ts >>>, BBBBB, a!d RRR represe!t the
)ile !u&ber, block !u&ber, a!d ro' !u&ber (slot !u&ber.
%ere is the cbaseF ,$d e!codi!#, co&parabl e to he(adeci &al represe!tati o!
e(cept that 'e have ,$ di#i ts. 2he di#i ts are pri!tabl e characters:
D8G82S C%ARAC23RS
FFFFFFFFFFFF FFFFFFFFFFFFFFFFFF
/ to "+ A to V (Capi tal letters
", to +1 a to B (lo'er case letters
+" to ,1 / to . (deci &al di#i ts
," a!d ,* \ a!d N respecti vel y
>or e(a&pl e, AAAA&+AABAAA3t7AAB represe!ts obHect AAAA&+ I *-W,$ \
+, )ile AAb I 1, block AA3t7 I $W,$" \ $$W,$ \ 1*, a!d slot 1. 2he query:
select c!a&e, ro'i d )ro& custo&ers 'here city I GDallasG C
&i#ht retur! the ro' i!)or&ati o! (di))ere!t values tha! restricted R1<8D:
CNAME ROWID
------------- ------------------
Basics AAAAm5AABAAAEtMAAB
Allied AAAAm5AABAAAEtMAAC
Si!ce a! i!de( is )or a speci)ic table, do!G t !eed e(te!ded R1<8D 'i th obHect
!u&ber o) table se#&e!t. But Select state&e!t co&&o!l y displays the
e(te!ded )or&. >u!ctio!s e(ist to co!vert (!eed special library.
<e use R1<8D !o&e!clature #e!ericall y i) database i!deter&i !ate.
F1,$F
E:ample ,.". ". -ee 3&bedded S@4 pro#ra& o! p#. $-*. 8dea is that a)ter
)i!d a ro' (throu#h a! i!de( or slo'er &ea!s ca! retrieve it a seco!d ti &e
throu#h R1<8D i! 1RAC43 (!ot i! DB". 2his saves ti &e.
Da!#er to use R1<8D i) so&eo!e &i#ht co&e a!d delete a ro' 'ith a #ive!
R1<8D, the! reclai & space a!d !e' ro' #ets i!serted i! that slot. 8) out o)
date R1<8D could re)er to 'ro!# ro'.
But certai!l y sa)e to re&e&ber R1<8D )or le!#th o) a tra!sacti o! that has a
re)ere!ced ro' locked.
/o$s E:tendi ng O5er -e5eral PagesC Product .ariations
8! 1RAC43, allo' ro's that are lar#er tha! a!y si!#le pa#e (" PB.
8) a ro' #ets too lar#e to )it i!to itGs ho&e block, it is split to a seco!d block.
Seco!d part o) ro' has !e' R1<8D, !ot available to a!y e( ter!al &ethod,
o!l y to chai!er.
7ea!s that ra!do& access requires t'o or &ore pa#e reads (5oltai re
proble&. DB" o! other ha!d li&i ts siBe o) ro' to disk pa#e ($//+ bytes.
Still &ay !eed to &ove ro' 'he! it #ro's, leave )or'ardi !# poi!ter at
ori#i!al R8D positio! ()or'ardi !# poi!ter called Oover)l o' recordO.
<hy do you thi!k DB" does thisM
But o!ly ever !eed 103 redirecti o!, update )or'ardi !# poi!ter i! ori#i!al
positio! i) ro' &oves a#ai!, !o chai! o) )or'ardi !# poi!ters.
,.' 3Ktree 1nde:
7ost co&&o! i!de( is BFtree )or&. Assu&ed by 9N1630 Create 8!de(. <e
'ill see other types ? particularl y hashed, but BFtree is &ost )le(i bl e.
2he BFtree is like the ("F* tree i! &e&ory, e(cept that !odes o) the tree take
up a )ull disk pa#e a!d have a lot o) fanout . (6icture o! board.
1RAC43 >or& o) Create 8!de( >i#ure -.E, p#. $-+ (leave o! board:
CR3A23 J ;08@;3 ] B827A6K 80D39 Jsche&a.Ki !de(!a&e 10 table!a&e
(col!a&e J ASC ] D3SCK R,col !a&e J ASC ] D3SCK`S
J 2AB43S6AC3 tblespaceK
J S21RAG3 . . . K (see p#. $E-, par $ )), overri de 2ablespace de)aul t
J 6C2>R33 !K
Jother disk stora#e a!d update t( clauses !ot covered or de)erredK
F1,+F
J 01S1R2K
<e 'ill discuss the co!cept o) a B827A6 8!de( later. 0o!FBit&ap )or !o'.
0ote ASC ] D3SC is !ot reall y used. 1RAC43 BFtree is usable i! both directio!s
(lea) sibli!# poi!ters le)t a!d ri#ht.
<hat Create 8!de( does: reads throu#h all ro's o! disk (assu&e 0, pulls out
(keyval ue, ro'id pairs )or each ro'. Get )ol lo'i!# list put out o! disk.
(keyval 1, ro'i d1 (keyval ", ro'id" . . . (keyval0, ro'i d0
0o' sort these o! disk, so i! order by kevalues. 3(plai! 01S1R2 clause
(telli!# 1RAC43 ro's are i! ri#ht order, so do!G t have to sort, but error re F
tur!ed i) 1RAC43 !otices this is )alse.
0o' idea o) bi!ary search, 3(a&ple -.*.1, p#. $-, )). Assu&e 0 I E,
searchi!# array o) structs: arrJPK.keyval , arrJPK.ro'i d (list o) pairs !a&ed
above. 1rdered by keyval P I / to ,.
NW bi!search: retur! P so that arrJPK.keyval II (, or F1 i) !o &atchC WN
NW arr is assu&ed to be e(ter!al, siBe o) E is 'ired i! WN
i!t bi!search(i !t (
R
i!t probe I *, NW )irst probe positio! at subscript P I * WN
di)) I "C
'hile (di)) T / R NW loop u!til P to retur! WN
i) (probe UI , DD ( T arrJprobeK.keyval
probe I probe \ di))C
else
probe I probe F di))C
di)) I di))N"C
S NW 'e have reached )i!al P WN
i) (probe UI , DD ( II arrJprobeK.keyval NW have 'e )ou!d itM
WN
retur! probeC
else i) (probe\1 UI , DD ( II arrJprobe\1K . keyval NW &aybe !e(t WN
retur! probe \ 1C
else retur! F1C NW other'ise, retur! )ailure WN
S
7igure ,., >u!ctio! bi!search, 'i th E e!tries 'ired i!
Co!sider the seque!ce o) keyval values R1, E, E, -, ., ., 1/S at subscript po F
sitio!s /F,.
F1,,F
<ork throu#h i) ( I 1, bi!search 'ill probe successive subscripts *, 1, a!d /
a!d 'ill retur! /. 8) ( I E, the successive subscript probes 'ill be *, 1, /.
(u!dershoot ? see 'hyM retur! 1. 8) ( I +, retur! F1. 7oreM
Give! duplicate values i! the array, bi!search 'ill al'ays retur! the small est
subscript P such that ( II arrJPK.keyval ( e(ercise.
2he bi!search )u!cti o! #e!eraliBes easil y: #ive! 0 e!tries rather tha! E,
choose & so that "
&F 1
U 0 f "
&
C the! i!i tialiBe probe to "
&F 1
F1 a!d di)) to
"
&F"
. 2ests i) probe UI , or probe \1 UI , beco&e UI 0F1.
1pti &al !u&ber o) co&pariso!s (i) all di))ere!t. 4o#
"
0. But !ot opti &al i!
ter&s o) disk accesses.
3(a&ple -.*.". Assu&e 1 &illio! e!tri es. >irst probe is to e!try +"$,"-E
("
1.
F1. Seco!d prove is "
1-
I ",",1$$ e!tries a'ay. 4ook at list p#. $--.
But 'i th "P byte pa#es, assu&e keyval ue is $ bytes (si&ple i!t, R1<8D is $
bytes ('ill be either , or E i! 1RAC43, the! - bytes )or 'hole e!tryC "///N- is
about "+/ e!tries per pa#e. (17N"+/ I $/// p#s. 2here)ore successive
probes are al'ays to di))ere!t pa#es u!til probe 1$ (&aybe.
2hatGs 1* 8N1sQQ A year o) letters to 5oltai re. <e ca! &ake it . 'eeksQ
F1,EF
Class !I.
4ast ti &e, talked about bi!ary search o! 17 e!tri es o) - bytes each assu& i!#
search list is o! disk, 3(a&ple -.*.", p#. $-E. Required 1* 8N1s.
1P, !o' 'e ca! i&prove o! that. 4ayout 17 e!tries o! $/// p#s. 2hese are
called lea) !odes o) the BFtree.
3ach p# has a ra!#e o) keyval ues, a!d 'a!t to create opti &al directory to #et
to proper lea) !ode. 0ode ptrs !p a!d separators.

. . . !p ""1 !p *EE !p $.- . . .
""$ "E- */+ *$, *EE $11 $$. $.-
8!de( (Directory
4evel
4ea) 4evel
7igure ,.!I Directory structure to lea) level !odes
Search )or e!try keyval ue */+. Bi! search i! i!de( (directory )or lar#est key
s&aller tha! (. 2he! )ollo'i !# separator poi!ts to proper !ode.
1P, !o' put directory o! disk pa#es. 3ach e!try is: (sepkeyval , !p. About
sa&e siBe as BFtree e!try, - bytes. 0u&ber o) pa#es is $///N"+/ I 1,.
Say have #otte! to ri#ht i!de( directory !odeC do bi!ary search o! i!de(
!ode )or !p to )ollo', the! )ollo' it to proper lea) pa#e.
0o' repeat trick 'ith %8G%3R level directory to i!de( !odes Hust created.
Sa&e types o) e!tries, o!ly 1, poi!ters !eeded , all o! o!e p#.
root l evel !ode
l evel "
l ea) l evel !odes
i !de( !odes, or di rectory !odes
7igure ,.!! Sche&atic 6icture o) a three level BFtree
Above leaf nodes , have inde* nodes or directory nodes. Depth o) *.
Search al#ori th& is to start at root, )i!d separators surrou!di !#, )ollo' !p
do'!, u!til lea). 2he! search )or e!try 'i th keyval II ( i) e(ists.
F1,-F
1!ly * 8N1s. Get the &ost out o) all i!)or&ati o! o! (i!de( !ode pa#e to loF
cate appropriate subordi !ate pa#e. "+/ 'ay )a!out. Say this is a bushy tree
rather tha! sparse tree o) bi!ary search, a!d there)ore )lat. >a!out ):
depth I lo#
)
(0 ? e.#., lo#
"
(1,///,/// I "/, lo#
)
(1,///,/// I * i) ) T1//
Actuall y, 'ill have co&&o!l y accessed i!de( pa#es reside!t i! bu))er. 1 \ 1,
i! ) I "+/ case )its, but !ot $/// lea) !odes: o!l y 103 8N1.
Get sa&e savi!#s i! bi!ary search. >irst several levels have1 \ " \ $ \ -
\1, !odes, but to #et do'! to o!e 8N1 'ould have to have "/// pa#es i!
bu))er. 2hereGs a li&i t o! bu))er, &ore likel y save ,FE 8N1s out o) 1*.
(1) course "/// pa#es )its i! o!l y $ 7B o) bu))er, !ot &uch !o'adays. But i!
a co&&erci al database 'e &ay have %;0DR3DS o) i!de(es )or D1V30S o)
tablesQ 2hus bu))er space &ust be shared a!d beco&es &ore li&i ted.
Dynamic canges in te 3Ktree
1P, !o' sa' ho' to create a! i!de( 'he! k!o' all i! adva!ce. Sort e! tries
o! lea) !odes a!d created directory, a!d directory to directory.
But a BFtree also #ro's i! a! eve!, bala!ced 'ay. Co!sider a seque!ce o)
i!serts, -EE 7igure ,.!", pg. (;!.
("F*Ftree. All !odes belo' root co!tai! " or * e!tries, split i) #o to $. 2his is
a "F* tree, BFtree is probabl y &ore like 1//F "//.
(>ollo' alo!# i! 2e(t. 8!sert !e' e!tri es at lea) level. Start 'ith si&ple tree,
root I lea) (do!G t sho' ro'id values. 8!sert E, .,, $1. Peep ordered. 8!sert
*., split. 3tc. Correct botto& tree, )irst separator is *..
0ote separator key does!G t %A53 to be equal to so&e keyval ue belo', so
do!G t correct it i) later delete e!try 'i th keyval ue *..
8!sert --, ,+, ++, ," (double split.
0ote: stays bala!ced because o!l y 'ay depth ca! i!crease is 'he! root !ode
splits. All lea) !odes stay sa&e dista!ce do'! )ro& root.
All !odes are bet'ee! hal) )ull (ri#ht a)ter split a!d )ull (Hust be)ore split i!
#ro'i !# tree. Avera#e is .E/E )ull: S@R2("N".
0o' e(plai! 'hat happe!s 'he! a! e!try is deleted )ro& a BFtree (because
the correspo!di !# ro' is deleted.
F1,.F
8! a ("F*Ftree i) !u&ber o) e!tri es )alls belo' " (to 1, either B1RR1< )ro&
sibli!# e!try or 73RG3 'i th sibli!# e!try. Ca! R3D;C3 depth.
2his does!G t actuall y #et i&ple&e!ted i! &ost BFtree products: !odes &i#ht
have very s&all !u&ber o) e!tries. 0ote BFtree !eeds &allocN)ree )or disk
pa#es, &alloc !e' pa#e i) !ode splits, retur! )ree pa#e 'he! e&pt y.
8! cases 'here lots o) pa#es beco&e A471S2 e&pty, DBA 'ill si&pl y reor#a F
!iBe i!de(.
6roperti es o) the BFtree, p#. $.. )), 2alk throu#h. (Actuall y, B\tree.
g 3very !ode is diskF pa#e siBed a!d resides i! a 'ellF de)i!ed locatio!
o! the disk.
g 0odes above the lea) level co!tai ! directory e!tri es, 'i th ! e 1
separator keys a!d ! disk poi!ters to lo'erF level BFtree !odes.
g 0odes at the lea) level co!tai ! e!tries 'ith (keyval , R1<8D pairs
poi!ti !# to i!di vi dual ro's i!de(ed.
g All !odes belo' the root are at least hal) )ull 'i th e!try i!)or&ati o!.
g 2he root !ode co!tai!s at least t'o e!tries (e(cept 'he! o!l y o!e ro' is
i!de(ed a!d the root is a lea) !ode.
1nde: Node Layout and 7ree -pace on a page
See >i#ure -.1*, p#. $.$. Al'ays k!o' ho' &uch )ree space 'e have o! a
!ode. ;sually use )ree space (!ot e!try cou!t to decide 'he! to split.
2his is because 'e are assu&i!# that keyval ca! be variabl e le!#th, so e!try
is variabl e le!#thC o)te! i#!ored i! research papers.
1 2 . . . N keyval rid
. . . Header
Info Entry Directory
B-tree emtroes
FREE
SPACE
keyval rid keyval rid
0ote >i#ure looks like ro's i! blockC 3!try directory slots )or e!tri es #ive
opportu!i t y to per)or& bi!ary search althou#h e!tri es are var. le!#th.
8! 4oad, leave so&e )ree space 'he! create i!de( so do!G t start 'ith stri!# o)
splits o! i!serts. See >i#ure -.E, p#. $-+, 6C2>R33 !.
8dea is, 'he! load, stop 'he! pct)ree space le)t. Split to !e' !ode. >ree
space available )or later i!serts.
F1E/F
E:ample ,.'. ( . Corrected BFtree structure )or a &illio! i!de( e!tri es.
8!de( e!try o) - bytes, pa#e (!ode o) "/$- bytes, assu&e $- bytes )or
header i!)o (p#. +//, a!d !ode is E/[ )ull.
2hus 1$// bytes o) e!tries per !ode, 1$//N- I 1E+ e!tries. <ith 1,///,///
e!tri es o! lea), this requires 1,///,///N1E+ I +E1+ lea) !ode pa#es. Rou!d
up i! divisio!, C384(1,///,///N1E+, say 'hy.
0e(t level up e!tri es are al&ost the sa&e i! )or&, (sepkeyval ue, !ptr, say -
bytes, a!d +E1+ e!tries, so +E1+N1E+ I ** i!de( !ode pa#es.
1! !e(t level up, 'ill )it ** e!tri es o! o!e !ode (root pa#e. 2hus have BFtree
o) depth *.
/O4># C%LC4L%21ON- L16E 2#1- %/E 71NE. <e 'ill be usi!# such cal F
culatio!s a lotQ (8t tur!s out, theyG re quite pre cise.
Ge!erall y &ost e))icie!t 'ay to create i!de(es )or tables, to )irst load the
tables, the! create the i!de(es (a!d R31RG i! DB".
2his is because it is e))icie!t to sort a!d build le)tF toF ri#ht, si!ce &ul ti ple
e!tri es are e(tracted )ro& each pa#e or ro's i! the table.
Create 8!de( State&e!ts i! 1RAC43 a!d DB". See >i#ures -.1$, a!d -.1+.,
p#. $., D $.E.
1RAC43 &akes a! e))ort to be co&pati bl e 'i th DB". %ave &e!ti o!ed 6C2F
>R33 be)ore, leaves )ree space o! a pa#e )or )uture e(pa!sio!.
DB" adds " clauses, 80C4;D3 colu&!a&eF list to carry e(tra i!)o i! keyval a!d
C4;S23R to speed up ra!#e retrieval 'he! ro's &ust be accessed. See ho'
this 'orks belo'.
Class !!.
7issed a class. 3(a& 1 is <ed, 7arch "". (A)ter 5ac. %' " due by 7o!day,
7arch "/, solutio!s put up o! 2uesday to allo' you to study.
8) you are late, your h' " does!G t cou!t. NO E8CEP21ON-. 24/N 1N ?#%2
0O4 #%.E /E%D0D
Duplicate 6ey5alues in an 1nde: .
<hat happe!s i) have duplicate keyval ues i! a! i!de(M 2heoreticall y 1P,
but di))ers )ro& o!e product to a!other.
F1E1F
4ook at >i# -.1", p# $.-, ri#htF ha!d, a!d assu&e add 'e a bu!ch o) !e'
ro's 'i th duplicate keyval ues --. Get e.#. >i# -.1, )ollo'i !# o! p# +/$.
np 88
62 65 88 88
88 96
np 88 np
(<ork out slo'l y ho' #et it. See&s ki!d o) stra!#e to call -- a separator
value, si!ce -- o! both !odes belo'. 0o' co!sider Select state&e!t:
select W )ro& tbl 'here keyval I --C
%ave to )i!d 'ay do'! to list o) --s. Do!e i! bi! search, re&e&berM
4e)t&ost o!e G3 --. 7ust )ollo' !p to le)t o) sepkey I --. (0o -- at that
lea) level, but there could be o!e si!ce dups allo'ed
2he! 'ill #o ri#ht o! lea) level 'hile keyval UI --, )ollo'i !# sibli!#
poi!ters, access R1<8Ds a!d read i! ro's. 6oi!t o) this is that everythi !#
'orks )i!e 'i th &ul ti pl e duplicates: you should thi!k about ho'.
Co!sider the process o) deleti !# a ro': &ust delete its i!de( e!tri es as 'ell
(or i!de( e!tries poi!t to !o!F e(iste!t ro'C could Hust keep slot e&pt y, but
the! 'aste ti &e later i! retri evals.
2hus 'he! i!sert ro', &ust )i!d 'ay to lea) level o) A44 i!de(es a!d i! sert
e!tri esC 'he! delete ro' &ust do the sa&e. 1verhead.
2reat update o) colu&! value i!vol ved i! i!de( key as delete the! i!sert.
6oi!t is that 'he! there are &ul ti pl e duplicate values i! i!de(, )i!d it %ARD
to delete speci)ic e!try &atchi !# a particul ar ro'. 4ook up e!try 'i th #ive!
value, search a&o!# A44 duplicates )or ri#ht R8D.
1u#ht to be easy: keep e!tries ordered by keyval ue]]R8D (u!ique. But
&ost co&&erci al database BFtrees do!G t do that. (Bit&ap i!de( solves this.
Said be)ore, there e(ists a! al#ori th& )or e))icie!t sel)F &odi)yi !# BFtree
'he! delete, opposi te o) split !odes, &er#e !odes. But !ot &a!y products
use it: o!ly DB" ;DB see&s to. (6icture BFtree shri!ki !# )ro& " levels to
o!e i! >i#ure -.1"Q A)ter put i! *., take out $1, e.#., retur! to root o!l y.
4ot o) 8N1: ar#ues a#ai!st i!de( use e(cept )or table 'i th !o updates. But i!
)act &ost updates o) tables occur to i!cre&e!tF decre&e!t )ields.
F1E"F
3(a&ple. Credit card ro's. 8!de(ed by locatio! (state ]] city ]] staddress,
credi tF card !u&ber, socsec!o, !a&e (l!a&e ]] )!a&e ]] &idi !i t, a!d possi F
bly &a!y others.
But !ot by bala!ce. <hy 'ould 'e 'a!t to look up all custo&ers 'ho have
a bala!ce due o) e(actl y 1//E.+1M 7i#ht 'a!t all custo&ers 'ho have
atte&pted to overdra' thei r bala!ce but this 'ould use a! (i!de(ed )la#.
A!y'ay, &ost co&&o! cha!#e to record is bala!ce. Ca! hold o)) &ost
other updates (cha!#e address, !a&e u!til over!i #ht processi!#, a!d reor F
#a!iBe i!de(es. 6robabl y &ore e))icie!t.
DB" has !ice co&pressio! )or &ul ti pl e duplicate keyval ues i! a! i!de(.
See >i#. -.1E o! p# +/+ )or data pa#e layout 'i th duplicate keyval ues.
F1E*F %eader
8!)o
Peyval R8D R8D
R8D
6r(
. . .
Peyval R8D R8D
R8D
6r(
. . .
F1E$F
3ach block has keyval o!ce a!d the! a list o) R8D values, up to "++. 6r(
co!tai !s poi!ter to prior block (" bytes, !e(t block (" bytes a!d cou!t o)
R8Ds i! this block (1 bytes plus 1 byte u!used. J2392: !o' 1 blockNpa#eMK
Si!ce Peyval ca! be quite lo!# (l!a&e ]] )!a&e ]] &idi!t, say */ chars or
bytes, #et #reat space savi!# (i) &ul ti pl e duplicates. <ith "++ R8Ds )or
each keyval, do'! !earl y to $ bytesNe!try i!stead o) *$ bytesNe!try.
Oracle 3itmap 1nde:
A bit&ap (or bit vector ca! be used i! a! AD2 to represe!t a set, a!d this
leads to very )ast u!io!, i!tersecti o!, a!d &e&ber operati o!s.
A bit&ap i!de( uses 103 bit&ap )or each disti!ct keyval, like DB" 'ith R8D
list. A bit&ap ta'es the place of a !&# list , speci)yi !# a set o) ro's.
See e(a&pl e, p#. +/+F +/,, e&ps table 'ith various colu&!s. 4o' card
colu&!s are #ood ca!didates )or bit&ap i!de(es. 3asy to build the&.
create table e&ps (eid char(+ !ot !ull pri &ary key,
e!a&e varchar(1,, &#ri d char(+ re)ere!ces e&ps,(3RR1R 80 292
#e!der char(1, salarycat s&alli!t, dept char(+C
create bit&ap i!de( #e!der( o! use&ps(#e!derC (" values, G7G DG>G
create bit&ap i!de( sal( o! use&ps(sal rycatC (1/ values, 1F1/
create bit&ap i!de( dept( o! use&ps(deptC (1" vals, + char: GACC02G
2hese colu&!s are said to have low cardinali ty (cardi !ali t y is !u&ber o)
values i! a colu&!.
>or structure o) a bit&ap i!de(, thi!k o) assi#!i!# ordi!al !u&bers to 0
ro's, /, 1, ", *, . . . Peep bits i! load order )or ro's (order as they lie o!
diskM. Require a )u!cti o! to #o )ro& ordi!al !u&ber to R1<8D a!d back.
2he! bit&ap )or a propert y such as #e!der I G>G is a seque!ce o) 0 bits (-
bitsNbyte so bit i! positio! H is 1 i)) ro' H has propert y, / other'ise.
1P, have bit&aps )or each colu&! value . 6ut o!e )or each keyval ue i! BF
tree to &ake a! i!de(M
Bit&ap i!de(, >i#. -.1-, p#. +/,. 0ote that 1//,/// ro's #ives 1",+//
bytes o) bit&apC Bit&ap broke! i!to Segments o! successive lea) pa#es.
Sa&e idea as Blocks o) R8DFlists i! DB" ;DB.
F1E+F
8dea o) bit&ap de!si ty: !u&ber o) 1Fbits divided by 0 (total = bits
8! DB", R8D !eeds I $ bytes. 2hus R8DFlist ca! represe!t o!e ro' 'ith *"
bits. At de!sity I 1N*" , bit&ap ca! represe!t i! sa&e space.
But i) de!sity is a lot lo'er tha! 1N*", say 1N1/// (out o) 1//,/// ro's
!eed 1/// 0Fbit bit&aps. 2otal R8DFlist stays the sa&e le!#th 'i th 1///
colu&! values, but (5erbati & bit&ap i!de( requires a lot &ore space.
1RAC43 uses co&pressio! )or lo'F de!si ty bit&aps, so do!G t 'aste space.
Call bit&ap Overbati &O i) !ot co&pressed (&ea!s &oderatel y hi#h de!sity.
>ast A0D a!d 1R o) verbati & bit&aps speeds queries. 8dea is: overlay
u!si#!ed i!t array o! bit&ap, loop throu#h t'o arrays A0Di!# array (D i!
C, a!d produci !# resul t o) A0D o) predicates. 6arallelis& speeds thi!#s.
But )or updates, bit&aps ca! cause a slo'do'! 'he! the bit&aps are
co&pressed (!eed to be deco&pressed, &ay reco&press di))ere!tl y.
Do!G t use bit&ap i!de(es i) have )reque!t updates (1426 situati o!.
,.( Clustered and NonK Clustered 1nde:es
2he idea o) a clustered i!de( is that the ro's o) the table are i! the sa&e
order as the i!de( e!tries ? by keyval ue.
8! library, order )ictio! o! shelves by Author (l!a&e ]] )!a&e ]] &idi !i t ]]
ti tl e. 8) look up !ovels by Dicke!s, #o to shelves, theyG re all to#ether.
8! &ost database products, de)aul t place&e! o) ro's o! data pa#es o! disk
is i! order by load or by i!sertio! (heap. 8) 'e did that 'ith books i! a
library, 'ould be hard to collect up all Dicke!s.
4ook at >i#ure -.1., p# +1/. Adva!ta#e o) clusteri !# i! ra!#e search is that
ro's are !ot o! ra!do& pa#es but i! appropri ate order to read i! successive
ro's (do!G t !eed to per)or& !e' pa#e: &ul ti pl e ro'sNpa#e.
E:ample ,.(. ! . Adva!ta#e o) Clusteri !#. 4ar#e depart &e!t store 'i th
hu!dreds o) bra!ch stores, has records )or 1/ &illio! custo&ers.
Assu&e each ro' is 1// bytes. 1 Gbyte o) data. 6robabl y o!ly s&all )racti o!
o) data is i! &e&ory bu))ers (&ost syste&s have less tha! "// 7Bytes o)
bu))er available )or a si!#le table.
Bosto! has 1N+/ o) the 1/7 custo&ers, or "//,///. Do a &aili!#,
F1E,F
select !a&e, staddress, city, state, Bip )ro& custo&ers
'here city I GBosto!G a!d a#e bet'ee! 1- a!d +/ a!d hobby i!
(Gracket sportsG , GHo##i!#G , Ghiki!#G , Gbodybui l di !#G , Goutdoor sportsG C
1P, so each ro' is o! a ra!do& pa#e i! !o!F clustered case, !ot )ou!d i!
bu))er. Say read all ro's i! Bosto! ("//,/// or 1N+/ o) 1/ 7.
8) each ro' is a ra!do& pa#e, thatG s "//,/// real 8N1s. But "//,/// 8N1s
divided by -/ (8N1sN(8N1sNsec I secs I "+// seco!ds, about $/ &i!utes.
8! this calculati o! 'e assu&ed o!l y o!e disk ar& &ovi !# at a ti &e. 3ve! i)
&ore i! &otio!, 'ill a!s'er 2%8S query &ore quickl y, but i&porta!t poi!t is
resource use: &i#ht have &ul ti pl e users co!te!di !# )or disk ar&s.
8! clustered case, )it 1// byte ro's t'e!t y to a pa#e. "/ ro'sNpa#e
("PBytesNpa#e. Si!ce clustered, ri#ht !e(t to o!e a!other, place "//,///
ro's o! "//,///N"/ I 1/,/// pa#es. (pa#esN(ro'sNpa#e I pa#es.
0o' readi!# all ro's i! Bosto! takes (1/,///N-/ I 1"+ secs. 2'o &i!utes.
"/ ti &es )aster.
2hatGs readi!# all the ro's )or Bosto!. 8) 'e have i!de(es o! a#e a!d hobby
'e do!G t !eed to read all these ro's. Suppose these predicates eli&i !ate
-/[ o) the ro's, leavi !# 1N+ o) the& to read, or $/,/// ro's.
8! the !o!F clustered case, $/,/// reads take $/,///N-/ I +// secs.
8! the clustered case, 'e &i#ht still have to read the 1/,/// pa#es: depe!ds
i) hobby or a#e cluster city I GBosto!G )urtherC probabl y !ot.
So still !eed 1"+ seco!ds (assu&e every pa#e still co!tai !s at least o!e ro',
'he! 1N+ are hit. But still + ti &es )aster tha! !o!F clustered.
Clustered 1nde: in D3"
0o' ho' do 'e arra!#e clustered i!de(M 8! DB" ;DB, have C4;S23R clause
i! Create 8!de( state&e!t (>i# -."/, p# +1".
At &ost o!e cluster i!de(. Create i!de( 'ith e&pty table, the! 41AD table
'i th sorted ro's (or R31RG a)ter load 'i th u!sorted ro's.
2his is 012 a pri &ary key i!de(: DB" ;DB places all ro's i! data pa#es
rather tha! o! lea) o) BFtree. 0o #uara!tee that !e'l y i!serted ro's 'ill be
placed i! clustered orderQ
F1EEF
DB" ;DB leaves space o! data pa#es usi!# 6C2>R33 spec, so !e'l y
i!serted ro' ca! be guided to a slot i! the ri#ht order. <he! ru! out o)
e(tra space #et i!serted ro's )ar a'ay o! disk )ro& thei r cluster orderi!#
positio!.
DB" uses &ul ti pa#e reads to speed thi!#s up: Opre)etch 8N1OC &ore o! this i!
!e(t chapter.
O/%CLE 1nde:K OrganiMed 2ables
2his ca! provi de a pri mary 'ey clustered i!de(, 'i th table ro's o! lea) level
o) a BFtree, kept i! order by BFtree splitti !# eve! 'he! there are lots o)
cha!#es to table. See p#. +1*F +1$: 1RGA08VA2810 80D39
create table sche&a.tabl e!a&e (Rcol u&!!a&e datatype . . .
J1RGA08VA2810 %3A6 ] 1RGA08VA2810 80D39 ('i th clauses !ot coveredK
(Back be)ore 1RAC43 -.i, used !ot to be able to have seco!dary i!de(es at
the sa&e ti &e. But thatG s !o lo!#er a proble&.
1RGA08VA2810 80D39 does require a pri &ary key i!de(. 2he pri &ary key is
used auto&ati cal l y.
F1E-F
Class !".
O/%CLE N2abl eO ClustersC @ Not Clusteri ng B, 7ig. ,."!, pg. *!(
CR3A23 C4;S23R Jsche&a.Kcl uster!a&e FF this is B3>1R3 a!y tableQ
(col!a&e datatype R, . . . S
JclusterLclause R. . . SK C
2e clusterPclauses are cosen from te follo$ingC
J6C2>R33 !K J6C2;S3D !K
JS21RAG3 (J80828A4 ! JP]7KK J0392 ! JP]7KK J780392302S !K
J7A9392302S !K
J6C280CR3AS3 !KK
JS8V3 ! JP]7KK FF disk space )or o!e keyval : de)aul t to 1 disk block
J2AB43S6AC3 tblspace!a&eK
J 80D39 ] %AS%P3:S ! J%AS% is e(prK K
Jother clauses !ot coveredKC
D/OP CL4-2E/ statementC
DR16 C4;S23R Jsche&a.Kcl uster!a&e
J80C4;D80G 2AB43S JCASCAD3 C10S2RA802SKKC
Co!sider e(a&pl e o) e&ployees a!d depart &e!ts, 'here the t'o tables are
very co&&o!l y Hoi!ed by dept!o. 3(a&pl e -.$.", p#. +1,.
create cluster depte&p
(dept!o i!t
siBe "///C
create table e&ps
( e&p!o i!t pri &ary key,
e!a&e varchar(1/ !ot !ull,
&#r i!t re)ere!ces e&ps,
dept!o i!t !ot !ull
cluster depte&p (dept!oC
create table depts
( dept!o i!t pri &ary key,
d!a&e varchar(.,
address varchar("/
cluster depte&p (dept!oC
F1E.F
<he! 'e use dept!o as a cluster key )or these t'o tables, the! all data )or
each dept!o, i!cludi!# the depart &e!ts ro' a!d all e&ployee ro's, 'ill be
clustered to#ether o! disk.
Steps:
1. create a cluster (a! i!de( cluster, the de)aul t type
". create the tables i! the cluster
*. create a! i!de( o! the cluster
$. load the data, treati !# the tables !or&all y.
+. 6ossibl y, add &ore (seco!dary i!de(es.
Step 1. i!cludes all the physicalF level speci)icati o!s (S21RAG3 clauses, so
these are !ot do!e i! step ".
CR3A23 2AB43 Jsche&a.K tabl e!a&e
(colu&! de)i!i ti o!s as i! Basic S@4, see >i#ure -.+
C4;S23R cluster!a&e (colu&!!a&e R, colu&!!a&eS
FF table colu&!s listed here that &ap to cluster key
JAS subqueryKC
2he cluster o'!s the table data. 8t does !ot o'! the i!de( data, so step *
ca! speci)y tablespace or S21RAG3 specs.
create i!de( depte&p( o! cluster depte&pC
0ote this does 012 #ive us the clustered i!de( )eatures &e!ti o!ed earlier.
R1<S AR3 012 6;2 80 1RD3R B: P3:5A4;3 <%30 41AD3D 80 C4;S23R.
All ro's 'i th si!#le keyval ue are stored to#ether, but there is !o orderi !#
atte&ptedC i!de( o! keyval yue is separate idea )ro& cluster.
,.* #as Primary 6ey 1nde:
1P, !o' )or a co&plete departure. See p# +1E, 1RAC43 Cluster 'ith use o)
optio!al clause
%AS%P3:S ! J%AS% is e(prK
Ro's i! a table located i! hash cluster are placed i! pseudoF ra!do& data
pa#e slots usi!# a hash )u!ctio!, a!d looked up the sa&e 'ay, o)te! 'i th
o!l y o!e 8N1. 2%3R3 8S 01 D8R3C21R: AB153 R1< 43534.
Also, !o order by keyval ue. Successive keyvals are !ot close to#ether,
probabl y o! e!ti rel y di))ere!t pa#es (depe!ds o! hash )u!ctio!.
F1-/F
Ca!G t retri eve, O!e(t ro' up i! keyval ue,O !eed to k!o' e(act value. Serious
li&i tati o!, but 1 8N1 lookup is i&porta!t )or certai ! applicati o!s.
8dea o) 6ri&ary 8!de(. Deter&i !es place&e!t o) ro's o) a table. Cluster
i!de( does this, but i&plies that ro's close i! keyval ue are close o! disk.
7ore correct to say 6ri&ary 8!de( 'he! hash or#a!iBati o!.
8! hash cluster, %AS%P3:S ! is the proposed !u&ber o) slots S )or ro's to
#o to (like darts thro'! at ra!do& slots, but repeatabl e 'he! search.
1RAC43 equates the value ! 'ith O%AS%P3:SO i! itGs re)ere!ces.
8t 'ill actuall y choose the !u&ber o) slots S to be the )irst pri &e !u&ber TI
%AS%P3:S, called 6R873R11>(%AS%P3:S. 3(a&pl e -.+.1 has this:
create cluster acctclust (accti d i!t
siBe -/
hashkeys 1/////C
Ca! )i#ure out ho' &a!y slots actuall y allocated:
select hashkeys )ro& userLclusters
'here clusterL!a&e I GACC2C4;S2GC
>i!d out it is 1////*. 0ote 'e ca! create our o'! hash )u!ctio! (!ot
!or&al l y advisable, add to Create Cluster above:
create cluster acctclust (accti d i!t
siBe -/
hashkeys 1///// hash is &od(accti d, 1////*C
0o' i! 3(a&ple, create si!#le table i! this cluster 'ith u!ique hashkey:
create table accou!ts
( acctid i!te#er pri &ary key,
. . . FF list o) other colu&!s here
cluster acctclust (acctidC
0ote that i! accou!ts table, BFtree seco!dary i!de( created o! acctid
because itGs a pri &ary key. Access !or&all y throu#h hash thou#h.
%AS%P3:S a!d S8V3 deter&i !e ho' &a!y disk blocks are i!itiall y allocated
to cluster. >irst deter&i !e ho' &a!y slots S I 6R873R11>(%AS%P3:S.
>i#ure ho' &a!y slots B 'ill )it o! a disk block: J2392K J<AS 3RR1RK
F1-1F
B I 7A9(>411R(0u&ber o) usable bytes per disk blockNS8V3, 1
2he !u&ber o) usable byte i! a disk block o! ;7B &achi!e db is 1.,". <ill
cha!#e. Ca! use "/// )or calculati o!s o! h', 3(a&.
0e(t, calculate ho' &a!y Disk blocks !eeded )or S slots 'he! B to a block.
D I C384(SNB
(So S8V3 a!d %AS%P3:S deter&i !e !u&ber o) disk blocks i! hash clusterC
ca!G t cha!#e %AS%P3:S laterC S8V3 o!ly advisory.
Assi#! o!e or &ore tables to hash cluster 'i th (co&&o! hashkey (i.e.,
clusterkey )or hash cluster. 8 'ill te!d to speak o) o!e table, u!ique key.
3(a&ple #ive! i! 3(. -.+.1 is si!#le accou!ts table 'i th accou!ti d hashkey.
Pey values 'ill be hashed to so&e slot a!d ro' se!t to that slot.
s!1 I h(keyval 1
Could have t'o disti!ct key values hashed to sa&e slot (collisio!.
(<he! &ul ti pl e tables i!vol ved, could clearl y have &ul ti pl e ro's 'ith SA73
keyval ue #o to sa&e slot, !ot a collisio! but &ust be ha!dled.
(3ve! i) u!ique, i) !u&ber o) disti!ct keys #ro's beyo!d !u&ber o) slots,
&ust have a lot o) collisio!s. <ill have collisio!s be)ore that.
2he 'ay 1RAC43 ha!dles this is to e(pa!d its slots o! a pa#e as lo!# as
space re&ai !s, a!d a)ter that over)lo' to !e' pa#es. See >i#. -."*, p#
+1,.
(Dra' a picture o) Root pa#es ori#i!all y allocated a!d over)lo' pa#es.
8! hashi!#, try !ot to over)l o'. 8) lot o) ro's 'ould hash to sa&e slot, !ot
appropriate )or hashi!#. 3.#., deptsF e&ps 'he! lot o) e&ps per dept!o.
8! accou!ts case 'he! lot o) collisio!s, 'hile &ost ro's o! root block, @uery
pla! uses hashed accessC a)ter a lot o) over)l o', tries to use seco!dary
i!de( o! acctid.
;suall y try to create e!ou#h slots o) su))icie!t siBe so collisio! relati vel y
rare, disk block pa#es o!l y hal) )ull.
8! ;!ique hashkey case, &ake e!ou#h slots so o! avera#e o!l y hal)F )ull.
F1-"F
No 1ncrement al Canges in -iMe of #as 2able
%o' create )u!ctio! h(keyval ue )or / to SF1. %ave &ore #e!eric )u!cti o!
r(( (ra!do& !u&ber based o! (, so r(keyval ue is i! ra!#e (/./ to 1./.
(3asy ? Hust #e!erate ra!do& &a!tissa, characteristic is "
/
. 0o' set:
h(keyval ue I 8023G3RL6AR2L1>(SWr(keyval ue
But !ote that 'e ca!G t i!cre&e!tal l y i!crease siBe o) hash table based o!
this. 8) have di))ere!t !u&ber o) slots SG, !e' hG 'o!G t #ive sa&e slots )or
old values. 8) r(keyval ue I /.$., 802($W/.$. I 1, i!t(+W/.$. I ".
2here)ore ca!G t e!lar#e !u&ber o) slots i!cre&e!tal l y to reduce !u&ber o)
collisio!s. (2here is a k!o'! al#ori th& allo'i!# dy!a&i c hash table #ro'th,
used )or e(a&pl e i! Sybase 8@.
,.) 2ro$ing Darts at /andom -lots
Co!ceptual e(peri &e!t o) thro'i !# darts at slots )ro& a 410G 'ay a'ay.
%ash i!to slots as i! 1RAC43 (ra!do& !u&bers, ho' &a!y slots e&pt yM
2he case o) 1RAC43 is the e(peri &e!t: ;!li&i ted Slot 1ccupa!cy, questio!
o) ho' &a!y slots are occupied. 0u&ber o) darts 0, !u&ber o) slots 7.
@uestio! is: ho' &a!y slots occupied. 0ot Hust 7 F 0 because so&e slots 'ill
be occupied t'ice, so&e three ti &es.
6r(slot s #ets hit by dart d I 1N7
6r(slot s does !ot #et hit by dart d I (1 e 1N7
6r(slot s does !ot #et hit by 0 darts thro'! i! successio! I (1 e 1N7
0
6r(slot s does #et hit by o!e or &ore o) 0 darts I 1 F (1 e 1N7
0
3(S I 3(pected !u&ber o) slots hit by 0 darts I 7(1 F (1 e 1N7
0

Sho' that (appro(i &atel y N,. ). 'O e


F1
I (1 e 1N7
7
N,. ). (O 3(S I 7 (1 e e
e0N7

N,. ). *O 3(Slots !ot hit I 7 e


e0N7
F1-*F
2he! solve questio! 'he! slot occupa!cy o) 1: ho' &a!y retries to #et last
dart i! slot (this is questio! o) Retry Chai!, 'ill use later.
3asy to )i#ure out that as !u&ber o) darts #ets closer a!d closer to !u&ber
o) slots, !u&ber o) retries beco&es lo!#er a!d lo!#er.
8) (0F1N7 close to 1, 4e!#th o) Retry Chai! 3(4 to place 0th dart
approaches i!)i!i ty. 8) 0F1 I 7, ca!G t do it. Call (0F1N7 the >;4403SS, >.
2he! N,. ). !!O 3(4 I 1N(1 F >
>i!all y: <he! Do %ash 6a#es >ill ;p. Say disk pa#e ca! co!tai ! "/ ro's,
#ets avera#e o) 1/ ro's hashed to it.
<he! 'ill it #et &ore tha! it ca! hold (hash over)l o', both cases aboveM
Say 'e have 1,///,/// ro's to hash. %ash the& to 1//,/// pa#es. 2his is
a bi!ary distri buti o!, appro(i &ated by 0or&al Distri buti o!.
3ach ro' has 1N1//,/// probabili ty 6 o) hitti !# a particular pa#e.
3(pected !u&ber o) ro's that hit pa#e is 6W1,///,/// I 1/.
Sta!dard Deviati o! is: S@R2(6W(1F 6W1,///,/// I S@R2(1/ I *.1,".
6robili ty o) "/ or &ore ro's #oi!# to o!e pa#e is 6%8(1/N*.1," I .///-*1,
but thatG s !ot u!likel y 'i th 1//,/// pa#es: -* o) the&.
Capter ;. Auery Opti miMati on
@uery opti &iBer creates a procedural access pla! )or a query. A)ter sub&i t
a Select state&e!t, three steps.
select W )ro& custo&ers 'here city I GBosto!G
a!d disc!t bet'ee! 1/ a!d 1"C
(1 6arse (as i! B0>, look up tables, colu&!s, vie's (query &odi)icati o!,
check privile#es, veri)y i!te#ri t y co!strai !ts.
(" @uery 1pti &iBati o!. 4ook up statistics i! Syste& tables. >i#ure out 'hat
to do. Basicall y co!sider co&peti !# pla!s, choose the cheapest.
(<hat do 'e &ea! by cheapestM <hat are 'e &i!i &iBi!#M See shortl y.
(* 0o' Create the Opro#ra&O (code #e!erati o!.
2he! @uery 3(ecutio!.
F1-$F
<e 'ill be talki !# about <%: a particular pla! is chose!, !ot %1< all the
pla! search space is co!sidered. @uestio! o) ho' co&piler chooses best
pla! is beyo!d scope o) course. Assu&e @162 )ollo's hu&a! reaso!i!#.
>or !o', co!sideri !# o!l y queries, !ot updates.
-ection ;.! . 0o' 'hat do 'e &ea! by cheapest alter!ati ve pla!M <hat
resources are 'e tryi !# to &i!i &iBeM C6; a!d 8N1. Give! a 64A0, talk about
C1S2
C6;
(64A0 a!d C1S2
8N1
(64A0.
But t'o pla!s 64A0
1
a!d 64A0
"
ca! have i!co&parabl e resource use 'i th
this pair o) &easures. See >i#ure ..1. ( p#. +*+
C1S2
C6;
(64A0 C1S2
8N1
(64A0
64A0
1
.." C6; seco!ds 1/* reads
64A0
"
1.E C6; seco!ds -./ reads
WWW <hich is cheaperM Depe!ds o! 'hich resources have a bottl e!eck.
DB" takes a 'ei#hted su& o) these )or the total cost, C1S2(64A0:
C1S2(64A0 I <
1
.
C1S2
C6;
(64A0 \ <
"
.
C1S2
8N1
(64A0
A DBA should try to avoid bottl e!ecks by u!dersta!di !# the ,)!-")A# )or
a syste&. 3sti &ated total C6; a!d 8N1 )or all users at peak 'ork ti &e.
>i#ure out various queries a!d rate o) sub&issio! per seco!d. @
k
, RA23(@
k

as i! >i#ure ..". (p#. +*E.
@uery 2ype RA23(@uery i! Sub&issio!sNseco!d
@1 $/./
@" "/./
7igure ;." Si&ple <orkload 'ith t'o @ueries
2he! 'ork out C1S2
C6;
(@
k
a!d C1S2
C6;
(@
k
)or pla! that database syste&
'ill choose. 0o' ca! )i#ure out total avera#e C6; or 8N1 !eeds per seco!d:
RA23(C6; I
K
RA23(@
k

.
C1S2
C6;
(@
k

Si&ilarl y )or 8N1. 2ells us ho' &a!y disks to buy, ho' po'er)ul a C6;.
(Costs #o up appro(i &atel y li!earl y 'i th C6; po'er 'ithi ! s&all ra!#es.
F1-+F
8deall y, the 'ei#hts <
1
a!d <
"
used to calculate C1S2(64A0 )ro& 8N1 a!d
C6; costs 'ill re)lect actual costs o) equip&e!t
Good DBA tries to esti &ate 'orkload i! adva!ce to &ake purchases, have
equip&e!t ready )or a !e' applicati o!.
WWW 0ote that o!e other &aHor e(pe!se is respo!se ti &e. Could have poor
respo!se ti &e (1F " &i!utes eve! o! li#htl y loaded i!e(pe!si ve syste&.
2his is because &a!y queries !eed to per)or& a 412 o) 8N1, a!d so&e
co&&ercial database syste&s have !o parallelis&: all 8N1s i! seque!ce.
But lo!# respo!se ti &e also has a cost, i! that it 'astes user ti &e. 6ay
'orkers )or 'asted ti &e. 3&ployees qui t )ro& )rustrati o! a!d others &ust
be trai!ed. 5e!dors are tryi !# hard to reduce respo!se ti &es.
;suall y there e(ist parallel versio!s o) these syste&s as 'ellC 'orth it i)
e(tre&el y heavy 8N1 a!d respo!se ti &e is a proble&, 'hile !u&ber o)
queries ru!!i !# at o!ce is s&all co&pared to !u&ber o) disks.
0ote that i! pla!s that )ollo', 'e 'ill !ot try to esti &ate C6;. 2oo hard.
Assu&e choose pla! 'i th best 8N1 a!d try to esti &ate that. 1)te! total cost
is proporti o!al to 8N1 si!ce each 8N1 e!tails e(tra C6; cost as 'ell.
-tatistics . 0eed to #ather, put i! Syste& tables. Syste& does !ot auto F
&aticall y #ather the& 'i th table load, i!de( create, updates to table.
8! DB", use ;tili ty called R;0S2A2S. >i#. ..*, p#. +*-.
R;0S2A2S 10 2AB43 user!a&e.tabl e!a&e
J<82% D8S2R8B;2810 JA0D D32A843DK R80D393S A44 ] 80D39 i!de(!a&eS K
Jother clauses !ot covered or de)erredK
e.#.: ru!stats o! table po!eil.custo&ersC
Syste& lear!s ho' &a!y ro's, ho' &a!y data pa#es, stu)) about i!de(es,
etc., placed i! catalo# tables.
1RAC43 uses A0A4:V3 co&&a!d. >i#. ..$, p#. +*-.
A0A4:V3 R80D39 ] 2AB43 ] C4;S23RS
Jsche&a.K Ri !de(!a&e ] table!a&e ] cluster!a&eS
RC176;23 S2A28S28CS ] other alter!ati ves !ot coveredS
R>1R 2AB43 ] >1R A44 J80D393DK C14;70S JS8V3 !K
] other alter!ati ves !ot coveredS
F1-,F
<ill see ho' statistics kept i! catalo# tables i! DB" a bit later.
Retrievi !# the @uery 6la!s. 8! DB" a!d 1RAC43, per)or& S@4 state&e!t. 8!
DB":
3964A80 64A0 JS32 @;3R:01 I !K JS32 @;3R:2AG I Gstri !#G K >1R
e(plai !ableF sqlFstate&e!tC
>or e(a&pl e:
e(plai! pla! set query!o I 1/// )or
select W )ro& custo&ers
'here city I GBosto!G a!d disc!t bet'ee! 1" a!d 1$C
2he 3(plai! 6la! state&e!t puts ro's i! a Opla!Ltabl eO to represe!t
i!di vi dual procedural steps i! a query pla!. Ca! #et ro's back by:
select W )ro& pla!Ltable 'here query!o I 1///C
Recall that a @uery 6la! is a seque!ce o) procedural access steps that carry
out a pro#ra& to a!s'er the query. Steps are peculiar to the DB7S.
>ro& o!e DB7S to a!other, di))ere!ce i! steps used is like di))ere!ce
bet'ee! pro#ra&&i !# la!#ua#es. Ca!G t lear! t'o la!#ua#es at o!ce.
<e 'ill stick to a speci)ic DB7S i! 'hat )ollo's, 75S DB", so 'e ca! e!d up
'i th a! i!)or&ati ve be!ch&ark 'e had i! the )irst editio!.
But 'e 'ill have occasio!al re)ere!ces to 1RAC43, DB" ;DB. %ere is the
1RAC43 3(plai! 6la! sy!ta(.
3964A80 64A0 JS32 S2A237302L8D I G te(tF ide!ti )i erG K J8021
Jsche&a.K tabl e!a&eK
>1R e(plai !ableF sqlFstate&e!tC
2his i!serts a seque!ce o) state&e!ts i!to a user created DB"N1RAC43 table
k!o'! as 64A0L2AB43 o!e ro' )or each access step. 2o lear! &ore about
this, see 1RAC43- docu&e!tati o! !a&ed i! te(t.
0eed to u!dersta!d 'hat basic procedural access steps AR3 i! the particular
product youG re 'orki !# 'i th.
2he set o) steps allo'ed is the Oba# o) tricksO the query opti &iBer ca! use.
2hi!k o) these procedural steps as the Oi!structi o!sO a co&piler ca! use to
create OobHect codeO i! co&pili!# a hi#herF level request.
F1-EF
A syste& that has a s&aller ba# o) tricks is likel y to have less e))icie!t acF
cess pla!s )or so&e queries.
75S DB" (a!d the archi tectural l y allied DB" ;DB have a 'ide ra!#e o)
tricks, but !ot bit&ap i!de(i !# or hashi!# capabili ty.
Still, very !ice capabili ti es )or ra!#e search queries, a!d probabl y the &ost
sophisticated query opti &iBer.
Basic procedrual steps covered i! the !e(t )e' Sectio!s (thu&b!ai l :
2able Sca! 4ook throu#h all ro's o) table
;!ique 8!de( Sca! Retrieve ro' throu#h u!ique i!de(
;!clustered 7atchi!# 8!de( Sca! Retrieve &ul ti pl e ro's throu#h a
!o!F u!ique i!de(, ro's !ot sa&e order
Clustered 7atchi!# 8!de( Sca! Retrieve &ul ti pl e ro's throu#h a
!o!F u!ique clustered i!de(
8!de(F 1!ly Sca! @uery a!s'ered i! i!de(, !ot ro's
0ote that the steps 'e have listed access all the ro's restricted by the
<%3R3 clause i! so&e si!#le table query.
0eed t'o tables i! the >R17 clause to requi re t'o steps o) this ki!d a!d
Aoi!s co&e later. A &ul tiF step pla! )or a si!#le table query 'ill also be
covered later.
Such a &ul tiF step pla! o! a si!#le table is o!e that co&bi!es &ul ti pl e
i!de(es to retrieve data. ;p to the!, o!l y o!e i!de( per table ca! be used.
;." 2able -pace -cans and 1JO
Si!#le step. 2he pla! table (pla!Ltable 'ill have a colu&! ACC3SS2:63
'i th value R (ACC3SS2:63 I R )or short.
E:ample ;.". !. 2able -pace -can -tep . 4ook throu#h all ro's i! table
to a!s'er query, &aybe because there is !o i!de( that 'ill help.
Assu&e i! DB" a! e&ployees table 'ith "//,/// ro's, each ro' o) "//
bytes, each $ PByte pa#e Hust E/[ )ull. 2hus "-// usable pa#es, 1$
ro'sNp#. 0eed C384("//,///N1$ I 1$,"-, pa#es.
Co!sider the query:
select eid, e!a&e )ro& e&ployees 'here socsec!o I 11**+*1E.C
F1--F
8) there is !o i!de( o! socsec!o, o!ly 'ay to a!s'er query is by readi!# i!
all ro's o) table. (Stupid !ot to have a! i!de( i) this query occurs 'i th a!y
)reque!cy at allQ
8! 2able Space Sca!, &i#ht !ot stop 'he! )i!d proper ro', si!ce statistics
)or a !o!F i!de(ed colu&! &i#ht !ot k!o' socsec!o is u!ique.
2here)ore have to read all pa#es i! table i! )ro& disk, a!d C1S2
8N1
(64A0
I 1$"-, 8N1s. Does this &ea! 1$"-, ra!do& 8N1sM 7aybe !ot.
But i) 'e assu&e ra!do& 8N1, the! at -/ 8N1s per seco!d, !eed about
1$"-,N-/ I 1E-., seco!ds, a bit u!der three &i!utes. 2his do&i !ates C6;
by a lar#e )actor a!d 'ould predict elapsed ti &e quite 'ell.
%o&e'ork $, !o!F dotted 3(ercises throu#h 3(ercise .... 2his is due 'he!
'e )i!ish Sectio! ..,..
%ssumptions about 1JO
<e are !o' #oi!# to talk about 8N1 assu&pti o!s a bit. >irst, there &i#ht be
6ARA44348S7 i! per)or&i !# ra!do& 8N1s )ro& disk.
2he pa#es o) a table &i#ht be striped across several di))ere!t disks, 'ith the
database syste& &aki!# requests i! parallel )or a si!#le query to keep all
the disk ar&s busy. See >i#ure ..+, p#. +$"
<he! the )irst editio! ca&e out, it 'as rare )or &ost DB7S syste&s to &ake
&ul ti pl e requests at o!ce (a )or& o) parallelis&, !o' itGs co&&o!.
DB" has a special )or& o) seque!ti al pre)etch !o' 'here it stripes *" pa#es
at a ti &e o! &ul ti pl e disks, requests the& all at o!ce.
<hile parallelis& speeds up 212A4 8N1 per seco!d (e(peciall y i) thereGs o!y
o!e user process ru!!i!#, it does!G t reall y save a!y R3S1;RC3 C1S2.
8) it takes 1".+ &s (/./1"+ seco!ds to do a ra!do& 8N1, does!G t save
resources to do 1/ ra!do& 8N1s at o!ce o! 1/ di))ere!t disks.
Still have to &ake all the sa&e disk ar& &ove&e!ts, cost to re!t the disk
ar&s is the sa&e i) there is parallelis&: Hust spe!d &ore per seco!d.
<ill speed thi!#s up i) there are )e' queries ru!!i!#, )e'er tha! the !u&ber
o) disks, a!d there is e(tra C6; !ot utiliBed. 2he! ca! use &ore o) the disk
ar&s a!d C6;s 'i th this sort o) parallelis&.
6arallelis& sho's up best 'he! there is o!ly o!e query ru!!i !#Q
F1-.F
But i) there are lots o) queries co&pared to !u&ber o) disks a!d accessed
pa#es are ra!do&l y placed o! disks, probabl y keep all disk ar&s busy
already.
But thereGs a!other )actor operati !#. 2'o disk pa#es that are close to each
other o! o!e disk ca! be read )aster because thereGs a shorter seek ti &e.
Recall that the syste& tries to &ake e(te!ts co!ti #uous o! disk, so 8N1s i!
seque!ce are )aster. 2hus, a table that is &ade up o) a seque!ce o) (&ai!l y
co!ti#uous pa#es, o!e a)ter a!other 'i thi ! a track, 'ill take &uch less ti &e
to read i!.
8! )act it see&s 'e should be able to read i! successive pa#es at )ull
tra!s)er speed 'ould take about .//1"+ secs per pa#e.
;sed to be that by the ti &e the disk co!trol ler has read i! the pa#e to a
&e&ory bu))er a!d looked to see 'hat the !e(t pa#e request is, the pa#e
i&&edi atel y )ollo'i !# has already passed by u!der the head.
But !o' 'i th &ul ti pl e requests to the disk outsta!di !#, 'e reall y C1;4D #et
the disk ar& to read i! the !e(t disk pa#e i! seque!ce 'i thout a &iss.
A!other )actor supports this speedup: the typical disk co!troll er bu))ers a!
e!ti re track i! itGs &e&ory 'he!ever a disk pa#e is requested.
Reads i! 'hole track co!tai !i !# the disk pa#e, retur!s the pa#e requested,
the! i) later request is )or pa#e i! track does!G t have to access disk a#ai!.
So 'he! 'eG re readi!# i! pa#es o!e a)ter a!other o! disk, itGs like 'eG re
readi!# )ro& the disk a! e!tire track at a ti &e.
8N1 is about 230 2873S )aster )or disk pa#es i! seque!ce co&pared to
ra!do&l y place 8N1. (Accurate e!ou#h )or rule o) thu&b.
P42 ON 3O%/D: <e ca! do -// 8N1s per seco!d 'he! pa#es i! seque!ce
(S i!stead o) -/ )or ra!do&l y placed pa#es (R. Seque!ti al 8N1 takes
/.//1"+ secs i!stead o) /./1"+ secs )or ra!do& 8N1.
DB" Seque!ti al 6re)etch &akes this possible eve! i) tur! o)) bu))eri !# o!
disk ('hich actuall y hurts per)or&a!ce o) ra!do& 8N1, si!ce reads 'hole
track it does!G t !eed: adds /.//- sec to ra!do& 8N1 o) /./1"+ sec
8B7 puts a lot o) e))ort i!to &aki !# 8N1 requests seque!ti al l y i! a query pla!
to #ai! this 8N1 adva!ta#eQ
F1./F
E:ample ;.". ". 2able -pace -can $it -e=uenti al %d5ant age . 2he
1$"-,R o) 3(a&pl e ..".1 beco&es 1$"-,S (S )or Seque!ti al 6re)etch 8N1
i!stead o) Ra!do& 8N1. A!d 1$"-,S requires 1$"-,N-// I 1E.-, seco!ds
i!stead o) the 1E-., seco!ds o) 1$""-,R. 0ote that this is a R3A4 C1S2
SA580GS, that 'e are actuall y usi!# the disk ar& )or a s&aller period.
Stripi!# reduces elapsed ti &e but !ot C1S2.
Cover idea o) 4ist 6re)etch. *" pa#es, !ot i! per)ect seque!ce, but rela tivel y
close to#ether. Di))icul t to predict ti &e.
<e use the rule o) thu&b that 4ist 6re)etch reads i! "// pa#es per seco!d .
See >i#ure ..1/, pa#e +$,, )or table.
6la! table ro' )or a! access step 'ill have 6R3>32C% I S )or seque!ti al
pre)etch, 6R3>32C% I 4 )or list pre)etch, 6R3>32C% I bla!k i) ra!do& 8N1.
See >i#ure ..1/. A!d o) course ACC3SS2:63 I R 'he! reall y Ra!do& 8N1.
0ote that seque!ti al pre)etch is Hust beco&i !# available o! ;089 database
syste&s. 1)te! Hust put a lot o) requests out i! parallel a!d depe!d o!
s&art 8N1 syste& to use ar& e))icie!tl y
F1.1F
Class !(.
3(a& 1.
Class !*.
;.' -imple 1nde:ed %ccess in D3".
8!de( helps e))icie!cy o) query pla!. 2here is a #read deal o) co&ple(i t y
here. Re&e&ber, 'e are !ot yet coveri!# queries 'ith Hoi!s: o!ly o!e table
i! >R17 clause a!d 01 subquery i! <%3R3 clause.
3(a&ples 'ith tables: 21, 2", . . , colu&!s C1, C", C*, . .
E:ample ;.'. ! . Assu&e i!de( C19 e(ists o! colu&! C1 o) table 21 (al'ays
a BFtree seco!dary i!de( i! DB". Co!sider:
select W )ro& 21 'here C1 I 1/C
2his is a .atching &nde* Scan. 8! pla! table: ACC3SS2:63 I 8, ACC3SS0A73
I C19, 7A2C%C14S I 1. (7A2C%C14S &i#ht be T1 i! &ul ti pl e colu&!
i!de(.
6er)or& &atchi !# i!de( sca! by 'alki!# do'! BFtree to 43>271S2 e!try o)
C19 'i th C1 I 1/. Retrieve ro' poi!ted to.
4oop throu#h e!tries at lea) level )ro& le)t to ri#ht u!til ru! out o) e! tries
'i th C1 I 1/. >or each such e!try, retrieve ro' poi!ted to. 0o assu&pti o!
about clusteri !# or !o!F clusteri !# o) ro's here.
8! 3(a&pl e ..*.", assu&e other restricti o!s i! <%3R3 clause, but &atchi !#
i!de( sca! used o! C19. 2he! other restricti o!s are validated as ro's are
accessed (ro' is qualifi ed: look at ro', check i) &atches restricti o!s.
0ot all predicates are inde*able . 8! DB", i!de(abl e predicate is o!e that ca!
be used i! a matchi ng inde* scan, i.e. a lookup that uses a co!ti #uous
sectio! o) a! i!de(. Covered i! )ull i! Sectio! ..+.
>or e(a&pl e, looki!# up 'ords i! the dictio!ary that start 'i th the letters
GpreG is a &atchi !# i!de( sca!. 4ooki!# up 'ords e!di!# 'i th Gtio!G is !ot.
DB" co!siders the predicate C1 UT 1/ to be !o!F i!de(able. 8t is !ot i&F
possible that a! i!de( 'ill be used i! a query 'ith this predicate:
select W )ro& 21 'here C1 UT 1/C
F1."F
But the statistics usuall y 'ei#h a#ai!st itGs use a!d so the query 'ill be
per)or&ed by a table space sca!. 7ore o! i!de(abl e predicates later.
1P, !o' 'hat about query:
select W )ro& 21 'here C1 I 1/ a!d C" bet'ee! 1// a!d "//
a!d C* like GA[G C
2hese three predicates are all i!de(able. 8) have o!ly C19, 'ill be like
previous e(a&pl e 'i th retrieved ro's restricted by tests o! other t'o
predicates.
8) have i!de( co&bi !(, created by:
create i!de( co&bi !( o! 21 (C1, C", C* . . .
<ill be able to li&i t ()ilter R8Ds o) ro's to retri eve &uch &ore co&pletel y
be)ore #oi!# to data. 4ike books i! a card catalo#, looki!# up
authorl !a&e I GAa&esG (c1 I 1/ a!d author)!a&e bet'ee! G%G a!d GPG
a!d title be#i!s 'ith letter GAG
>i!all y, 'e 'ill cover the questio! o) ho' to )il ter the R8Ds o) ro's to
retrieve i) 'e have three i!de(es, C19, C"9, a!d C*9. 2his is !ot si&ple .
See ho' to do this by taki!# out cards )or each i!de(, orderi !# by R8D, the!
&er#eF i!tersecti !#.
8t is a! i!teresti !# query opti &iBati o! proble& 'hether this is 'orth it.
1P, !o' so&e e(a&pl es o) si&ple i!de( sca!s.
E:ample ;.'. '. 1nde: -can -tep, 4ni=ue Matc . Co!ti!ui !# 'ith
3(a&ple ..".1, e&ployees table 'i th "//,/// ro's o) "// bytes a!d pct )ree
I */, so 1$ ro'sNp# a!d C384("//,///N1$ I1$,"-, data pa#es. Assu&e i!
i!de( o! eid, also have pct)ree I */, a!d eid]]R8D takes up 1/ bytes, so "-/
e!tri es per p#, a!d C384("//,///N"-/ I E1+ lea) level pa#es. 0e(t level up
C384(E1+N"-/ I *. Root !e(t level up. <rite o! board:
e&ployees table: 1$,"-, data pa#es
i!de( o! eid, eid(: E1+ lea) !odes, * level " !odes, 1 root !ode.
0o' query: select e!a&e )ro& e&ployees 'here eid I G1"./1AG C
Root, o! level " !ode, 1 lea) !ode, 1 data pa#e. See&s like $R. But 'hat
about bu))ered pa#esM 7i5e minute rule says should purchase e!ou#h
F1.*F
&e&ory so pa#es re)ere!ced &ore )reque!tl y tha! about o!ce every 1"/
seco!ds (popular pa#es should stay i! &e&ory. Assu&e 'e have do!e
this. 8) 'orkload assu&es 1 query per seco!d o) this o! e!a&e 'ith eid I
predi cate (!o others o! this table, the! lea) !odes a!d data pa#es !ot
bu))ered, but upper !odes o) eid( are. So reall y "R is cost o) query.
2his @uery 6la! is a si!#le step, 'i th ACC3SS2:63 I 8, ACC3SS0A73 I eid(,
7A2C%C14S I 1.
F1.$F
Class !).
1P !o' 'e i!troduce a !e' table called prospects. Based o! direct &ail
applicati o!s (Hu!k &ail. 6eople )ill out 'arre!t y cards, !a&e hobbies,
salary ra!#e, address, etc.
+/7 ro's o) $// bytes each. >;44 data pa#es (pct)ree I / a!d o! all i! F
de(es: 1/ ro's o! $ PByte pa#e, so +7 data pa#es.
prospects table: +7 data pa#es
0o': create i!de( addr( o! prospects (Bipcode, city, straddr cluster . . .C
Bipcode is i!te#er or $ bytes, city requires 1" bytes, straddr "/ bytes, R8D $
bytes, a!d assu&e 01 duplicate values so !o co&pressio!.
2hus each e!try requires $/ bytes, a!d 'e ca! )it 1// o! a $ PByte pa#e.
<ith +/7 total e!tries, that &ea!s +//,/// lea) pa#es. +/// directory
!odes at level ". +/ level * !ode pa#es. 2he! root pa#e. >our levels.
Also assu&e a !o!clusteri !# hobby( i!de( o! hobbies, 1// disti!ct hobbies
(. . . cards, chess, coi! collecti !#, . . .. <e say CARD(hobby I 1//.
(4ike 'e say CARD(Bipcode I 1//,///. 0ot all possible i!te#er Bipcodes
ca! be used, but )or si&plici ty say they are.
Duplicate co&pressio! o! hobby(, each key (- bytesM a&ortiBed over "++
R8DS (&oreM, so ca! )it .-$ R8Ds (or &ore per $ PByte pa#e, call it 1///.
2hus 1/// e!tries per lea) pa#e. <ith +/7 e!tri es, have +/,/// lea) pa#es.
2he! +/ !odes at level ". 2he! root.
i!de( o! eid, eid(: E1+ lea) !odes, * level " !odes, 1 root !ode.
prospects table addr: inde: obby: inde:
+/,///,/// ro's +//,/// lea) pa#es +/,/// lea) pa#es
+,///,/// data pa#es +,/// level * !odes 1+1 level " !odes
(1/ ro's per pa#e +/ level " !odes 1 root !ode
1 root !ode (1/// e!triesNlea)
CARD(Bipcode I 1//,/// CARD(hobby I1//
7igure ;.!" . So&e statistics )or the prospects table, pa#e ++"
E:ample ;.'. (. Matcing 1nde: -can -tep, 4nclustered Matc .
Co!sider the )ollo'i !# query:
F1.+F
select !a&e, straddr )ro& prospects 'here hobby I GchessGC
@uery opti &iBer assu&es each o) 1// hobbies equall y likel y (k!o's there
are 1// )ro& R;0S2A2S, so restricti o! cuts +/7 ro's do'! to +//,///.
<alk do'! hobby i!de( ("R )or directory !odes a!d across +//,/// e! tries
(1/// per pa#e so +// lea) pa#es, seque!ti al pre)eth so +//S.
>or every e!try, read i! ro' FF !o! clustered so all ra!do& choices out o) +7
data pa#es, +//,/// disti!ct 8N1s (!ot i! order, so R, +//,///R.
2otal 8N1 is +//S \ +//,//"R. 2i&e is +//N-// \ +//,//"N-/, about
+//,///N-/ I ,"+/ seco!ds. 1r about 1.E+ hours (" hrs I E"// secs.
Really o!l y picki!# up +//,/// disti !ct pa#es, 'ill lie o! less tha! +//,///
pa#es (out o) + 7. <ould this &ea! less tha! +//,/// R because bu))eri !#
keeps so&e pa#es arou!d )or doubleNtri pl e hitsM
53R: 2R858A4 3>>3C2Q %ours o) access, 1"/ seco!ds pa#es stay i! bu))er.
Ca! #e!erall y assu&e that upper level i!de( pa#es are bu))er reside!t (skip
"R but lea) level pa#es a!d &aybe o!e level up are !ot. Should calculate
i!de( ti &e a!d ca! the! i#!ore it i) i!si#!i )ica!t.
8) 'e used a table space sca! )or 3(a&pl e ..*.$, quali)yi !# ro's to e!sure
hobby I Gchess, ho' 'ould ti &e co&pare to 'hat 'e Hust calculatedM
Si&ple: +7 pa#es usi!# seque!ti al pre)etch, +,///,///N-// I ,"+ seco!ds.
(:es, C6; is still i#!ored ? i! )act is relati vel y i!si#!i)ica!t.
But this is the sa&e elapsed ti &e as )or i!de(ed access o) 1N1// o) ro'sQQ
:es, surprisi!#. But 1/ ro's per pa#e so about 1N1/ as &a!y pa#es hit, a!d
S is 1/ ti &es as )ast as R.
@uery opti &iBer co&pares these t'o approaches a!d chooses the )aster
o!e. <ould probabl y select 2able Space Sca! here But &i!or variati o! i!
CARD(hobby could &ake either pla! a better choice.
E:ample ;.'. *. Matcing 1nde: -can -tep, Clustered Matc .
Co!sider the )ollo'i !# query:
select !a&e, straddr )ro& prospects
'here Bipcode bet'ee! /"1+. a!d /*1+-C
F1.,F
Recall CARD(Bipcode I 1//,///. Ra!#e o) Bipcodes is 1///. 2here)ore, cut
!u&ber o) ro's do'! by a )actor o) 1N1//. SA73 AS ..*.$.
Bi##er i!de( e!tri es. <alk do'! to lea) level a!d 'alk across 1N1// o) lea)
level: +//,/// lea) pa#es, so +/// pa#es traversed. 8N1 o) +///S.
A!d data is clustered by i!de(, so 'alk across 1N1// o) +7 data pa#es,
+/,/// data pa#es, a!d theyG re i! seque!ce o! disk, so +/,///S.
Co&pared to 0o!&atchi !# i!de( sca! o) 3(a&ple ..*.$, 'alk across 1N1/ as
&a!y pa#es a!d do it 'ith S 8N1 i!stead o) R. 8#!ore directory 'alk.
2he! 8N1 cost is ++,///S, 'i th elapsed ti &e ++,///N+// I 1*E.+ seco!ds, a
bit over " &i!utes, co&pared 'i th 1.E+ hrs )or u!clustered i!de( sca!.
2he di))ere!ce bet'ee! 3(a&ples ..*.$ a!d ..*.+ does!G t sho' up i! the
64A0 table. %ave to look at ACC3SS0A73 I addr( a!d !ote that this i!de(
is clustered, (clusterrati o 'hereas ACC3SS0A73 I hobby( is !ot.
(1 Clusterrati o deter&i !es i) i!de( still clustered i! case ro's e(ist that
do!G t )ollo' clusteri !# rule. (8!serted 'he! !o space le)t o! pa#e.
(" 0ote that e!tri es i! addr( are $/ bytes, ro's o) prospects are $// bytes.
See&s !atural that +///S )or i!de(, +/,///S )or ro's.
6roperti es o) i!de(:
1. 8!de( has directory structure, ca! retri eve ra!#e o) values
". 8!de( e!tri es are A4<A:S clustered by values
*. 8!de( e!tri es are s&aller tha! the ro's.
E:ample ;.'. ) . Concatenat ed 1nde:, 1nde:K Only -can . Assu&e (Hust
)or this e(a&pl e a !e' i!de(, !addr(:
create i!de( !addr( o! prospects (Bipcode, city, straddr, !a&e
. . . cluster . . .C
0o' sa&e query as be)ore:
select !a&e, straddr )ro& prospects 'here Bipcode
bet'ee! /"1+. a!d /*1+-C
Ca! be a!s'ered i! 80D39 104: (because )i!d ra!#e o) Bipcodes a!d read
!a&e a!d straddr o)) co&po!e!ts o) i!de(: Sho' co&po!e!ts:
!addr( keyval ue: Bipcodeval.ci tyval .straddrval .!a&eval
F1.EF
2his is called a! 8!de( 1!ly sca!, a!d 'i th 3964A80 pla! table #ets !e'
colu&!: 80D39104: I : (ACC3SS2:63 I 8, ACC3SS0A73 I !addr(.
6revious pla!s had 80D39104: I 0.
(All these colu&!s al'ays reportedC 8 Hust &e!ti o! the& 'he! releva!t.
2i&eM Assu&e !addr( takes ,/ bytes i!stead o) $/ bytes, the! a&ou!t
read i! i!de(, i!stead o) +///S is E+//S, elapsed ti &e E+//N-// I ..$
seco!ds. Co&pare to ,".+ seco!ds 'i th 3(a&pl e ..*.+.
5aluable idea, 8!de( 1!ly. Select cou!t(W )ro& . . . is al'ays i!de( o!ly i)
i!de( ca! do i! a si!#le step at all, si!ce cou!t e!tries.
But ca!G t build i!de( o! the spur o) the &o&e!t. 8) do!G t have !eeded o!e
already, out o) luck. 3.#., co!sider query:
select !a&e, straddr, a#e )ro& prospects 'here Bipcode
bet'ee! /"1+. a!d /""+-C
0o' !addr( does!G t have all !eeded co&po!e!ts. 1ut o) luck.
8) try to )oresee all !eeded co&po!e!ts i! a! i!de(, esse!ti all y duplicat i!#
the ro's, a!d lose per)or&a!ce boost )ro& siBe.
8!de(es cost so&ethi !#. Disk &edia cost (!ot co&&o!l y crucial. <ith i! F
serts or updates o) i!de(ed ro's, lot o) e(tra 8N1 (!ot co&&o!.
<ith readF o!ly, like prospects table, load ti &e i!creases. Still o)te! have
every col o) a readF o!ly table i!de(ed.
Capter ;.(. 7ilter 7actors and -tatistics
Recall, esti &ated probabili ty that a ra!do& ro' &ade so&e predicate true.
By statistics, deter&i !e the )racti o! (>>(pred o) ro's re trieved.
3.#., hobby colu&! has 1// values. Ge!erall y assu&e u!i)or& distri buti o!,
a!d #et: >>(hobby I co!st I 1N1// I ./1.
A!d Bipcode colu&! has 1//,/// values, >>(Bipcode I co!st I 1N1//,///.
>>(Bipcode bet'ee! /"1+. a!d /*1+- I 1///
.
(1N1//,/// I 1N1//.
%o' does the DB" query opti &iBer &ake these esti &atesM
DB" statistics.
F1.-F
-ee >i#ure ..1*, p#. ++-. A)ter use R;0S2A2S, these statistics are up to
date. (0e(t p#. o) these !otes 1ther statistics as 'ell, !ot covered.
D10G2 <R823 2%8S 10 B1ARD FF S33 80 B11P
Catalog
Name
-tatistic
Name
Defaul t
.alue
Descripti on
S:S2AB43S CARD
06AG3S
1/,///
C384(1\CARDN"/
0u&ber o) ro's i! the table
0u&ber o) data pa#es co!tai !i !# ro's
S:SC14;70S C14CARD
%8G%"P3:
41<"P3:
"+
!Na
!Na
0u&ber o) disti !ct values i! this colu&!
Seco!d hi#hest value i! this colu&!
Seco!d lo'est value i! this colu&!
S:S80D393S 043534S
043A>
>8RS2P3:F
CARD
>;44P3:F
CARD
C4;S23RF
RA281
/
CARDN*//
"+
"+
/[ i) C4;S23R3D I G0G
.+[ i) C4;S23R3D I
G:G
0u&ber o) 4evels o) the 8!de( BFtree
0u&ber o) lea) pa#es i! the 8!de( BFtree
0u&ber o) disti !ct values i! the )irst
colu&!, C1, o) this key
0u&ber o) disti !ct values i! the )ull
key, all co&po!e!ts: e.#. C1.C".C*
6erce!ta#e o) ro's o) the table that are
clustered by these i!de( values
7igure ;.!'. So&e Statistics #athered by R;0S2A2S used )or access pla!
deter&i !ati o!
Statistics #athered i!to DB" Catalo# 2ables !a&ed. Assu&e that i!de(
&i#ht be co&posi te, (C1, C", C*
Go over table. CARD, 06AG3S )or table. >or colu&!, C14CARD, %8G%"P3:,
41<"P3:. >or 8!de(es, 043534S, 043A>, >8RS2P3:CARD, >;44P3:CARD,
C4;S23RRA281. 3.#., )ro& >i#ure ..1", statistics )or prospects table (#ive!
o! pp. ++"F *. <rite these o! Board.
S:S2AB43S
0A73 CARD 06AG3S
. . . . . . . . .
prospects +/,///,/// +,///,///
. . . . . . . . .
S:SC14;70S
0A73 2B0A73 C14CARD %8G%"P3: 41<"P3:
. . . . . . . . . . . . . . .
hobby prospects 1// <i!es Bicycli !#
Bipcode prospects 1///// ....- ////1
. . . . . . . . . . . . . . .
S:S80D393S
0A73 2B0A73 043534S 043A> >8RS2P3:
CARD
>;44P3:
CARD
C4;S23R
RA281
. . . . . . . . . . . . . . . . . . . . .
addr( prospects $ +//,/// 1//,/// +/,///,/// 1//
F1..F
hobby( prospects * +/,/// 1// 1// /
. . . . . . . . . . . . . . . . . . . . .
C4;S23RRA281 is a &easure o) ho' 'ell the clusteri !# propert y holds )or a!
i!de(. ,ith /0 or more , 'ill use Seque!ti al 6re)etch i! retrievi !# ro's.
8!de(able 6redicates i! DB" a!d their >ilter >actors
Look at >i#ure ..1$, p#. +,/. @162 #uesses at >ilter >actor. 6roduct rule
assu&es i!depe!de!t distri buti o!s o) colu&!s. Still !o subquery predicate.
Predicate 2ype 7ilter 7actor Notes
Col I co!st 1NC14CARD OCol UT co!stO sa&e as O!ot (Col I co!stO
Col co!st
8!terpol ati o! )or&ul a
OO is a!y co&pariso! predicate other
tha! equali tyC a! e(a&pl e )ollo's
Col U co!st or
Col UI co!st
41<"P3: a!d %8G%"P3: are esti &ates )or
e(tre&e poi!ts o) the ra!#e o) Col values
Col bet'ee! co!st1
a!d co!st"
OCol !ot bet'ee! co!st1 a!d co!st"O sa&e
as O!ot (Col bet'ee! co!st1 a!d co!st"O
Col i! list (list siBeNC14CARD OCol !ot i! listO sa&e as O!ot (Col i! listO
Col is !ull 1NC14CARD OCol is !ot !ullO sa&e as O!ot(Col is !ullO
Col like Gpatter!G 8!terpol ati o! >or&ula Based o! the alphabet
6red1 a!d 6red"
>>(6red1
.
>>(6red"
As i! probabi l i t y
6red1 or 6red" >>(6red1 \>>(6red"
F>>(6red1
.
>>(6red"
As i! probabi l i t y
!ot 6red1 1 F >>(6red1 As i! probabi l i t y
7igure ;."I. 7ilter 7actor formul as for 5arious predicate types Class !+.
7atchi!# 8!de( Sca!s 'ith Co&posite 8!de(es
(>i!ish FT .., Class 1., ho&e'ork due Class "/ (<ed, April 1"
Assu&e !e' i!de( &ail (:
create i!de( &ail ( o! prospects (Bipcode, hobby, i!co&eclass, a#eC
012 clustered. colu&! i!co&eclass has 1/ disti!ct values, a#e has +/.
>;44P3:CARD(&ail ( could be as &uch as
CARD(Bipcode
.
CARD(hobby
.
CARD(i!co&eclass
.
CARD(a#e I
1//,///
.
1//
.
1/
.
+/ I 1,///,///,///.
Ca!G t be that &uch, o!l y +/,///,/// ro's, so assu&e >;44P3:CARD is
+/,///,///, 'i th !o duplicate ro's. (Actuall y, +/7 darts i! +G slots. About
1N1// o) slots hit, so o!l y about 1[ duplicate keyval ues.
F"//F
3!tries )or &ail ( have le!#th: $ (i!te#er Bipcode \ - (hobby \ "
(i!co&eclass \ " (a#e \ $ (R8D I "/ bytes. So "// e!tri es per pa#e.
043A> I +/,///,///N1// I +//,/// pa#es. 0e(t level up has +,/// !odes,
!e(t level +/, !e(t is root, so 043534S I $.
S:S80D393S
0A73 2B0A73 043534S 043A> >8RS2P3:
CARD
>;44P3:
CARD
C4;S23R
RA281
. . . . . . . . . . . . . . . . . . . . .
&ail ( prospects $ "+/,/// 1//,/// +/,///,/// /
. . . . . . . . . . . . . . . . . . . . .
E:ample ;.*. !. Concatenat ed 1nde:, Matcing 1nde: -can .
select !a&e straddr )ro& prospects 'here
Bipcode I /"1+. a!d hobby I GchessG a!d i!co&eclass I 1/C
7atchi !# 8!de( Sca! here &ea!s that the three predicates i! the <%3R3
clause &atch the 80828A4 colu&! i! co!cate!ated &ail ( i!de(.
Ar#ue that matchi ng &ea!s all e!tries to be retri eved are co!ti #uous i! the
i!de(.
>ull )ilter )actor )or three predicates #ive! is 1N1//,///
.
1N1//
.
1N1/ I
1N1//7, 'i th +/7 ro's, so o!l y /.+ ro's selected. /.+R
8!terpret this probabilisticall y, a!d e(pected ti &e )or retri evi !# ro's is o!l y
1N-/ seco!d. %ave to add i!de( 8N1 o) course. "R, ./+ sec.
E:ample ;.*. " . Concatenat ed 1nde:, Matcing inde: scan .
select !a&e straddr )ro& prospects
'here Bipcode bet'ee! /"1+. a!d /$1+-
a!d hobby I GchessG a!d i!co&eclass I 1/C
0o', i&porta!t. 0ot o!e co!ti #uous i!terval i! i!de(. 2here is o!e i!ter val
)or: B I /"1+. a!d h I GcG a!d i!c I 1/, a!d the! a!other )or B I /"1,/ a!d
h I GcG a!d i!c I 1/, a!d . . . But there is stu)) bet'ee! the&.
A!alo#y i! telepho!e directory: last !a&e bet'ee! GS&aG a!d GS&BG a!d
)irst !a&e GAoh!G. 4ot o) di rectory to look throu#h, !ot all &atches.
@uery opti &iBer here traverses )ro& le)t&ost B I /"1+. to ri#ht&ost B I
/$1+- a!d uses h I GcG a!d i!c I 1/ as scree!i !# predicates.
F"/1F
<e say the )irst predicate is a 7A2C%80G predicate (used )or cutti !# do'!
i!terval o) i!de( co!sidered a!d other t'o are SCR33080G predicates.
(2his 7A2C%80G predicate is 'hat 'e &ea! by 7atchi!# 8!de( Sca!.
So i!de( traversed is: ("///N1//,/// ()ilter )actor o) +//,/// lea) pa#es,
I 1/,/// lea) pa#es. @uery opti &iBer actuall y calculates >> as
(/$1+-F /"1+.(%8G%"P3:F41<"P3: I "///N(....-F ////1
I "//N....E or appro(i &atel y "///N1//,/// I 1N+/
%ave to look throu#h 1N+/
.
043A> I +/// pa#es, 8N1 cost is +,///S 'i th
elapsed ti &e: +,///N$// I 1".+ seco!ds.
%o' &a!y ro's retrievedM (1N+/(1N1//(1N1/ I (1N+/,/// 'i th +/7 ro's,
so 1/// ro's retrieved. Seque!ti al, classM
0o. 1///R, 'ith elapsed ti &e 1///N$/ I "+ seco!ds. 2otal elapsed ti &e is
*E.+ secs.
E:ample ;.*. '. Concatenat ed 1nde:, NonK Matci ng 1nde: -can .
select !a&e straddr )ro& prospects 'here
hobby I GchessG a!d i!co&eclass I 1/ a!d a#e I $/C
4ike sayi!# >irst !a&e I GAoh!G a!d City I G<altha&G a!d street I G7ai!G .
%ave to look throu#h 'hole i!de(, !o &atchi !# colu&!, o!l y scree!i!#
predicates.
Still #et s&all !u&ber o) ro's back, but have to look throu#h 'hole i!de(.
"+/,///S. 3lapsed ti &e "+/,///N$// I ,"+ seco!ds, about 1/.+ &i!utes.
0u&ber o) ro's retri eved: (1N1//(1N1/(1N+/(+/,///,/// I 1///. 1///R
I "+ seco!ds.
8! 64A0 2AB43, )or 3(a&ple ..+.", have ACC3SS2:63 I 8, ACC3SS0A73 I
&ail (, 7A2C%C14S I 1C 8! 3(a&pl e ..+.*, have 7A2C%C14S I /.
Defini tion ;.*. ! . 7atchi!# 8!de( Sca!. A pla! to e(ecute a query 'here at
least o!e i!de(able predicate &ust &atch the )irst colu&! o) a! i!de(
(k!o'! as &atchi !# predicate, &atchi !# i!de(. 7ay be &ore.
<hat is a! i!de(able predicateM 3qual &atch predicate is o!e: Col I co!st
See De)i!i ti o! ..+.*. 6#. +,+
F"/"F
Say have i!de( C1"*$9 o! table 2, co&posi te i!de( o! colu&!s (C1, C", C*,
C$. Co!sider )ollo'i !# co&pou!d predicates.
C1 I 1/ a!d C" I + a!d C* I "/ a!d C$ I "+ (&atches all colu&!s
C" I + a!d C* I "/ a!d C1 I 1/ (&atches )irst three: !eed!G t be i!
order
C" I + a!d C$ I "" a!d C1 I 1/ a!d C, I *+ (&atches )irst t'o
C" I + a!d C* I "/ a!d C$ I "+ (012 a &atchi !# i!de( sca!
Scree!i!# predicates are o!es that &atch !o!F leadi!# colu&!s i! i!de(.
3.#., i! )irst e(a&pl e all are &atchi !#, i! seco!d all are &atchi !#, i! third,
t'o are &atchi !#, o!e is scree!i!#, a!d o!e is !ot i! i!de(, i! )ourth all
three are scree!i!#.
>i!ish throu#h Sectio! .., by !e(t class. %o&e'ork due !e(t class
(<ed!esday a)ter 6atriotG s day. 0392 ho&e'ork is rest o) Chapter . !o!F
dotted e(ercises i) you 'a!t to 'ork ahead.
Defini tion ;.*. " . Basic Rules o) 7atchi!# 6redicates
(1 A matchi ng predicate &ust be a! inde*abl e predicate. See p#. +,/,
2able ..1$ )or a list o) i!de(abl e predicates.
(" 7atchi !# predicates &ust &atch successive colu&!s, C1, C", . . . o) a!
i!de(. 6rocedure: 4ook at i!de( colu&!s )ro& le)tF to ri#ht. 8) )i!d a
&atchi !# predicate )or this colu&!, the! this is a &atchi !# colu&!. As soo!
as colu&! )ails to be &atchi !# ter&i !ate the search.
8dea is that seque!ce o) &atchi !# predicates cuts do'! i!de( search to
s&aller co!ti#uous ra!#e. (1!e e(cepti o!: 8!Flist predicate, covered
shortl y.
(* A !o!F &atchi !# predicate i! a! i!de( sca! ca! still be a scree!i!#
predicate.
4ook at rule (1 a#ai!. 2his is actuall y a ki!d o) circular de)i!i ti o!. >or a
predicate to be matchi ng it &ust be inde*able a!d:
Defini tion ;.*. 'C A! inde*abl e predicate is o!e that ca! be used to &atch
a colu&! i! a &atchi !# i!de( sca!.
Calli!# such a predicate inde*abl e is co!)usi!#. 3ve! i) a predicate is !ot
i!de(able, the predicate ca! use the i!de( )or scree!i !#.
F"/*F
<ould be &uch better to call such predicates matchabl e , but this !o&e! F
clature is e&bedded i! the )ield )or !o'.
<he! P leadi!# colu&!s o) i!de( C1"*$9 are &atchi !# )or a query, 3964A80
i!to pla! table, #et ACC3SS2:63 I 8, ACC3SS0A73 I C1"*$9, 7A2C%C14S
I P. <he! !o!F &atchi !# i!de( sca!, 7A2C%C14S I /.
Recall 8!de(able 6redicates i! >i#ure .. 1$, p#. +,/, a!d relate to telepho!e
directory. Does the predicate #ive you a co!ti #uous ra!#eM
Col T co!stM bet'ee!M 8!Flist is special. like Gpatter!G 'i th !o leadi!# 'ild
cardM Col1 like Col"M (sa&e &iddle !a&e as street !a&e 6redicate a!dM
6redicate orM 6redicate !otM
1P, a )e' &ore rules o! ho' to deter&i !e &atchi !# predicates. 6a#e +,,,
Def ;.*. ( . 7atch cols. i! i!de( le)t to ri#ht u!til ru! out o) predicates. But
(* Stop at )irst ra!#e predicate (bet'ee!, U, T, UI, TI, like.
($ At &ost o!e 8!Flist predicate.
8!Flist is special because it is co!sidered a seque!ce o) equal &atchi !#
predicates that the query opti &iBer a#rees to brid#e i! the access pla!
C1 i! (,, -, 1/ a!d C" I + a!d C* I "/ is like C1 I , a!d . . .C
6la! )or C1 I - a!d . . .C the! C1 I 1/ a!d . . .C etc.
But the )ollo'i !# has o!ly t'o &atchi !# colu&!s si!ce o!l y o!e i!Flist ca!
be used.
C1 i! (,, -, 1/ a!d C" I + a!d C* i! ("/, */, $/
<he! 8!Flist is used, say ACC3SS2:63 I G0G.
E:ample ;.*. ( . 8! )ollo'i !# e(a&pl es, have i!de(es C1"*$9, C+,9,
;!ique i!de( CE9.
(1 select C1, C+, C- )ro& 2 'here C1 I + a!d C" I E a!d C* UT .C
ACC3SS2:63 I 8, ACC3SS0A73 I C1"*$9, 7A2C%C14S I "
(" select C1, C+, C- )ro& 2 'here C1 I + a!d C" TI E a!d C* I .C
C* predicate is i!de(abl e but stop at ra!#e predicate.
ACC3SS2:63 I 8, ACC3SS0A73 I C1"*$9, 7A2C%C14S I "
(* select C1, C+, C- )ro& 2
'here C1 I + a!d C" I E a!d C+ I - a!d C, I 1*C
F"/$F
<e i#!ore )or !o' the possibili ty o) co&bi !i !# &ul ti pl e i!de(es
0ote, 'e do!G t k!o' 'hat @162 'ill choose u!til 'e see pla! table ro'
ACC3SS2:63 I 8, ACC3SS0A73 I C+,9, 7A2C%C14S I "
($ select C1, C$ )ro& 2
'here C1 I 1/ a!d C" i! (+, , a!d (C* I 1/ or C$ I 11C
C1 a!d C" predicates are &atchi !#. 2he OorO operator does!G t #ive
i!de(able predicate, but 'ould be used as scree!i !# predicate (!ot
&e!ti o!ed i! pla! table, but all predicates used to )ilter, o!es that
e(ist i! i!de( certai !l y 'ould be used 'he! possible. ACC3SS2:63 I G0G,
ACC3SS0A73 I C1"*$9, 7A2C%C14S I " Also: 80D39104: I G:G
(+ select C1, C+, C- )ro& 2 'here C1 I + a!d C" I E a!d CE I 1/1C
ACC3SS2:63 I 8, ACC3SS0A73 I CE9, 7A2C%C14S I 1
(Because u!ique &atch, but !othi !# said about this i! pla! table
(, select C1, C+, C- )ro& 2
'here C" I E a!d C* I 1/ a!d C$ I 1" a!d C+ I 1,C
<ill see ca!G t be &ul ti pl e i!de(. 3ither !o!F &atchi !# i! C1"*$9 or
&atchi !# o! C+,9. ACC3SS2:63 I 8, ACC3SS0A73 I C1"*$9,
7A2C%C14S I "
So&e Special 6redicates
6atter! 7atch Search. 4eadi!# 'ildcards !ot i!de(abl e.
OC1 like Gpatter!G O 'ith a leadi!# G[G i! patter! (or leadi!# GLGM like looki!# i!
dictio!ary )or all 'ord e!di!# i! Gtio!G . 0o!F&atchi !# sca! (!o!F i!de( able
predicate.
3(ist dictio!aries that i!de( by back'ard spelli!#, a!d DBA ca! use this
trick: look )or 'ord 'i th &atch: Back'ards I G!oi t[G
3(pressio!s. ;se i! predicate &akes !ot i!de(abl e.
select W )ro& 2 'here "WC1 UI +,C
DB" does!G t do al#ebra. :ou ca! reF'ri te: 'here C1 UI "-C
0ever i!de(abl e i) t'o di))ere!t colu&!s are used i! predicate: C1 I C".
1!eF>etch Access. Select &i!N&a( . . .
select &i!(C1 )ro& 2C
F"/+F
4ook at i!de( C1"*$9, le)t&ost value, read o)) value o) C1. Say have i!de(
C1"D*9 o! 2 (C1, C" D3SC, C*. 3ach o) )ollo'i !# qs has o!eF )etch access.
select &i!(C1 )ro& 2 'here C1 T +C (012 obviousl y +
select &i!(C1 )ro& 2 'here C1 bet'ee! + a!d ,C
select &a((C" )ro& 2 'here C1 I +C
select &a((C" )ro& 2 'here C1 I + a!d C" U */C
select &i!(C* )ro& 2 'here C1 I , a!d C" I "/ a!d C* bet'ee! , a!d .C
F"/,F
Class !,.
;.) Mul ti pl e 1nde: %ccess
Assu&e i!de( C19 o! (C1, C"9 o! (C", C*$+9 o! (C*, C$, C+, query:
(..,.1 select W )ro& 2 'here C1 I "/ a!d C" I + a!d C* I 11C
By 'hat 'eG ve see! up to !o', 'ould have to choose o!e o) these i!de(es.
2he! o!ly o!e o) these predicates could be &atched.
1ther t'o predicates are !ot eve! scree!i !# predicates. Do!G t appear i!
i!de(, so &ust retrieve ro's a!d validate predicates )ro& that.
B;2 i) o!l y use >> o) o!e predicate, terri bl e i!e))icie!cy &ay occur. Say 2
has 1//,///,/// ro's, >> )or each o) these predicates is 1N1//.
2he! a)ter appl yi !# o!e predicate, #et 1,///,/// ro's retrieved. 8) !o!e o)
i!de(es are clustered, 'ill take elapsed ti &e o): 1,///,///N$/ I "+,///
seco!ds, or !earl y seve! hours.
8) so&eho' 'e could co&bi !e the )ilter )actors, #et co&posi te )ilter )actor o)
(1N1//(1N1//(1N1// I 1N1,///,///, o!l y retrieve 1// ro's.
2rick. 7ulti pl e i!de( access. >or each predicate, &atchi !# o! di))ere!t
i!de(, e(tract R8D list. Sort it by R8D value.
(Dra' a picture F three e(tracts )ro& lea) o) 'ed#e i!to lists
8!tersect (A0D all R8D lists (6icture (easy i! sorted order. Result is list o)
R8Ds )or a!s'er ro's. ;se 4ist pre)etch to read i! pa#es. (6icture
2his is our )irst &ul ti pl e step access pla!.
See >i#ure ..1+. 6ut o! board.
5 0ote
2N%ME %CCE--20PE M%2C#COL- %CCE--N%ME P/E7E2C# M18OP-EA
2 7 / 4 /
2 79 1 C19 S 1
2 79 1 C"9 S "
2 79 1 C*$+9 S *
2 78 / $
2 78 / +
7igure ;.!* 6la! table ro's o) a 7ulti pl e 8!de( Access pla! )or @uery (..,.1
Ro' 7, start 7ulti pl e i!de( access. %appe!s at the e!d, a)ter 8!tersect
Dia#ra& steps i! picture. R8D lists placed i! R8D 6ool i! &e&ory.
F"/EF
0ote 6la! acts like reverse polish calculati o!: push R8D lists as created by
79 stepC 'ith 78, pop t'o a!d i!tersect , push resul t back o! stack.
79 steps require reads )ro& i!de(. 78 steps require !o 8N1: already i!
&e&ory, &e&ory area k!o'! as R8D 6ool.
>i!al ro' access uses 4ist pre)etch, pro#ra& disk ar&s )or &ost e))icie!t
path to bri!# i! *" pa#e blocks that are !ot co!ti #uous, but seque!ti al l y
listed. 7ost e))icie!t disk ar& &ove&e!ts.
(Re&e&ber that a! R8D co!sists o) (pa#eL!u&ber, slotL!u&ber, so i) R8Ds
are placed i! asce!di!# order, ca! Hust read o)) successive pa#e !u&bers.
Speed o) 8N1 depe!ds o! dista!ce apart. Rule o) thu&b )or Ra!do& 8N1 is
$/Nsec, Seq. pre)etch $//Nsec, 4ist pre)etch is 1//Nsec.
F"/-F
%ave &e!ti o!ed ACC3SS2:63s I 7, 79, 78. 1!e other type )or &ul ti pl e
i!de( access, k!o'! as 7; (to take the ;!io! (1R o) t'o R8D lists. 3.#.
(.. ,." select W )ro& 2 'here C1 I "/ a!d (C" I + or C* I 11C
%ere is @uery 6la! (>i#ure .. 1,:
2N%ME %CCE--20PE M%2C#COL- %CCE--N%ME P/E7E2C# M18OP-EA
2 7 / 4 /
2 79 1 C19 S 1
2 79 1 C"9 S "
2 79 1 C*$+9 S *
2 7; / $
2 78 / +
7igure ;.!) 6la! table ro's o) a 7ulti pl e 8!de( Access pla! )or @uery (..,."
Actuall y, query opti &iBer 'ould!G t #e!erate three lists i! a ro' i) avoid able.
2ries !ot to have T " i! e(iste!ce (!ot al'ays possible.
>i#ure .. 1E. 1. 79 C"9, ". 79 C*$+9, *. 7;, $. 79 C19, +. 78
E:ample ;.). ! . Multipl e inde: access . prospects table, addr( i!de(,
hobby( i!de( (see >i#ure ..11, p +$$, i!de( o! a#e (a#e( a!d i!co&eclass
(i!co&e(. <hat is 043A> )or these t'o, classM (4ike hobby(: +/,///.
select !a&e, straddr )ro& prospects
'here Bipcode I /"1+. a!d hobby I GchessG a!d i!co&eclass I 1/C
Sa&e query as 3(a&ple ..+.1 'he! had Bhobi!ca#e i!de(, ti!y cost, "R i! F
de(, o!l y .+R )or ro'. But !o' have three di))ere!t i!de(es: 64A0.
2N%ME %CCE--20PE M%2C#COL- %CCE--N%ME P/E7E2C# M18OP-EA
2 7 / 4 /
2 79 1 hobby( S 1
2 79 1 addr( S "
2 78 / *
2 79 1 i!co&e( S $
2 78 / +
7igure ;.!, 6la! table ro's o) 7ulti pl e 8!de( Access pla! )or 3(a&pl e ..,.1
Calculate 8N1 cost. >>(hobby I GchessG I 1N1//, o! hobby( (043A> I
+/,///, +//S (a!d i#!ore directory 'alk.
>or 78916S3@ I ", >>(Bipcode I /"1+. I 1N1//,/// o! addr( (043A> I
+//,/// , so +S.
F"/.F
8!tersect steps 78 requires !o 8N1.
>>(i!co&eclass I 1/ I 1N1/, o! i!co&e( (043A> I +/,///, so +,///S.
Still e!d up 'ith /.+ ro's at the e!d by taki!# product o) >>s )or three
predicates: (1N1//,///(1N1//(1N1/ I 1N1//,///,///
1!ly +/,///,//// ro'sC i#!ore list pre)etch o) .+4.
So +,+/+, elapsed ti &e +,+/+N$// I 1*.- seco!ds.
List Prefetc and /1D Pool
All the thi!#s 'eG ve bee! &e!ti o!i !# up to !o' have bee! relati vel y u!i F
versal, but R8D list rules are quite product speci)ic. 6robabl y 'o!G t see all
this duplicated i! 1RAC43.
FT <e !eed R8D e(tracti o! to access the ro's 'i th 4ist 6re)etch , but si!ce
this is )aster tha! Ra!do& 8N1 could!G t 'e al'ays use R8D lists, eve! i!
si!#le i!de( sca! to #et at ro's 'i th 4ist 6re)etch i! the e!dM
:3S, B;2: 2here are restricti ve rules that &ake it di))icul t. 7ost o) these
rules arise because R8D list space is a scarce resource i! &e&ory.
FT 2he R8D 6ool is a separate &e&ory stora#e area that is +/[ the siBe o)
all )our Disk Bu))er 6ools, e(cept ca!!ot e(ceed "// 7Bytes.
2hus i) DBA sets aside "/// Disk Bu))er 6a#es, 'ill have 1/// pa#es (about
$ 7Bytes o) R8D 6ool. (Di))ere!t &e&ory area, thou#h.
Def. ;.). ! . /ules for /1D List 4se .
(1 @162, 'he! it co!structs query pla!, predicts siBe o) R8Ds acti ve at a!y
ti &e. Ca!!ot use T +/[ o) total capaci ty. 8) #uess 'ro!#, abort duri!#
ru!ti &e.
(" 0o Scree!i!# predicates ca! be used i! a! 8!de( sca! that e(tracts a
R8D 4ist.
(* A! 8!Flist predicate ca!!ot be used 'he! e(tract a R8D list.
E:ample ;.). '. /1D List -iMe Limit . addr(, hobby(, i!co&e(, add se((
o! colu&! se(. 2'o values, G7G a!d G>G, u!i)or&l y distri buted.
select !a&e, straddr )ro& prospects 'here Bipcode bet'ee!
F"1/F
/"1+. a!d /"*+- a!d i!co&eclass I 1/ a!d se( I G>GC
2ry &ul ti pl e i!de( access. 3(tract R8D lists )or each predicate a!d i!ter sect
the lists.
But co!sider, se(( has +/,/// lea) pa#es. (Sa&e as hobby(, )ro&
+/,///,/// R8D siBe e!tri es. 2here)ore se( I G>G e(tracts "+,/// pa#e R8D
list. $ Pbytes each pa#e, !earl y 1// 7Bytes.
Ca! o!ly use hal) o) R8D 6ool, so !eed "// 7Byte R8D 6ool.
0ot u!reaso!able. A proo) that 'e should use lar#e bu))ers.
E:ample ;.). *. No 1nde: -creening . Recall Bhobi!ca#e i!de( i!
3(a&ple ..*.-.
select !a&e, straddr )ro& prospects 'here Bipcode bet'ee! /"1+.
a!d /$1+- a!d hobby I GchessG a!d i!co&eclass I 1/C
2'o latter predicates 'ere used as scree!i!# predicates. But scree!i!#
predicates ca!G t be used i! 4ist 6re)etch. 2o do 4ist 6re)etch, &ust re solve
t'o latter predicates a)ter bri!# i! ro's.
2he GBipcode bet'ee!G predicate has a )ilter )actor o) "///N1//,/// I 1N+/,
so 'i th +/ 7 ro's, #et 17 ro's, R8D list siBe o) $ 7Bytes, 1/// bu))er pa#es,
at least "/// i! pool. Assu&e R8D pool is lar#e e!ou#h.
So ca! do 4ist 6re)etch, 1,///,///N1// I 1/,/// seco!ds, !earl y * hours.
8) use scree!i !# predicates, co&pou!d >> is (1N+/(1N1//(1N1/ I 1N+/,///.
3!d 'i th 1/// ro's. Ca!G t use 4ist 6re)etch, so 1///R but
1///N$/ is o!l y "+ seco!ds. Sa&e i!de( 8N1 cost i! both cases. Clearl y
better !ot to use R8D list.
<hat is reaso! !ot to allo' 4ist 6re)etch 'i th Scree!i!# 6redicatesM Aust
because resource is scarce, do!G t 'a!t to tie up R8D list space )or a lo!#
ti &e 'hile add !e' R8Ds slo'l y. 8) re)use scree!i!#, @162 &i#ht use other
pla!s 'ithout R8Ds.
Point of Diminising /eturns in M8 %ccess.
<a!t to use R8D lists i! 789. TT 2alk it
Def. ;.). " . (1 8! 'hat )ollo's, assu&e have &ul ti pl e i!de(es, each 'ith a
disHoi!t set o) &atchi !# predicates )ro& a query. <a!t to use R8D lists.
F"11F
(" <ay @162 sees it: 4ist R8D lists )ro& s&allest siBe up (by i!creasi!#
>ilter >actor. 2hus start 'i th s&aller i!de( 8N1 costs (&atchi !# o!ly a!d
lar#er e))ect i! savi!# data pa#e 8N1.
(* Ge!erate successive R8D lists a!d calculate costsC stop 'he! cost o)
#e!erati !# !e' R8D list does!G t save e!ou#h i! eve!tual data pa#e reads by
reduci!# R8D set.
E:ample ;.). ) .
select !a&e straddr )ro& prospects
'here prospects bet'ee! o"1+. a!d /",+.
a!d a#e I $/ a!d hobby I GchessG a!d i!co&eclass I 1/C
(1 >>(Bipcode bet'ee! /"1+. a!d /",+- I +//N1//,/// I 1N"//.
(" >>(hobby I GchessG I 1N1//
(* >>(a#e I $/ I 1N+/ (2ypo i! te(t
($ >>(i!co&eclass I 1/ I 1N1/
Appl y predicate 1. (1N"// (+/7 ro's retrieved, "+/,/// ro's, all o!
separate pa#es )ro& +7, a!d "+/,///4 is "+// seco!ds. 8#!ore the i!de(
cost.
Appl y predicate " a)ter 1. 4ea) pa#es o) hobby( sca!!ed are (1N1//
(+/,/// I +//S, taki!# +//N$// I 1."+ seco!ds. Reduce !u&ber o) ro's
retrieved )ro& "+/,/// to about "+//, all o! separate pa#es, a!d "+//4
takes about "+ seco!ds. So at cost o) 1."+ seco!ds o) i!de( 8N1, reduce
table pa#e 8N1 )ro& "+// seco!ds to "+ seco!ds. Clearl y 'orth it.
Appl y predicate * a)ter 1 a!d ". 4ea) pa#es o) a#e( sca!!ed are (1N+/
(+/,/// I 1///S, taki!# 1///N$// I ".+ seco!ds. Ro's retrieved do'! to
(1N+/ ("+// I +/, +/4 is .+ seco!ds. 2hus at cost o) ".+ seco!ds o) i!de(
8N1 reduce table pa#e 8N1 )ro& "+ seco!ds to /.+ seco!ds. <orth it.
<ith predicate $, !eed i!de( 8N1 at lea) level o) (1N1/ (+/,/// I +///S or
1".+ seco!ds. 0ot e!ou#h table pa#e 8N1 le)t (/.+ seco!ds to pay )or it.
Do!G t use predicate $. <e have reached poi!t o) di&i!ishi!# retur!s.
Capter !I
Class "I.
<e have e!cou!tered the idea o) a tra!sactio! be)ore i! 3&bedded S@4.
F"1"F
Def. !I. ! 2ransaction . A tra!sactio! is a &ea!s to packa#e to#ether a
!u&ber o) database operati o!s per)or&ed by a process, so the database
syste& ca! provi de several #uara!tees, called the AC8D properti es.
2hi!k o) 'ri ti !#: B3G80 2RA0SAC2810 op1 op" . . . op0 30D 2RA0SAC2810
2he! all ops 'i thi ! the tra!sactio! are packa#ed to#ether.
2here is !o actual B3G80 2RA0SAC2810 state&e!t i! S@4. A tra!sacti o! is
be#u! by a syste& 'he! there is !o!e i! pro#ress a!d the applicatio! )irst
per)or&s a! operati o! that accesses data: Select, 8!sert, ;pdate, etc.
2he applicati o! lo#ic ca! e!d a tra!sactio! success)ull y by e(ecuti !#:
e(ec sql co&&i t 'orkC NW called si&pl y a Commi t WN

2he! a!y updates per)or&ed by operati o!s i! the tra!sactio! are successF
)ull y co&pleted a!d &ade per&a!e!t a!d all locks o! data ite&s are re F
leased. Alter!ati vel y:
e(ec sql rollback 'orkC NW called a! Abort WN
&ea!s that the tra!sacti o! 'as u!success)ul: all updates are reversed, a!d
locks o! data ite&s are released.
2he AC8D #uara!tees are e(tre&el y i&porta!t FF 2his a!d S@4 is 'hat
di))ere!ti ates a database )ro& a )ile syste&.
8&a#i !e that you are tryi !# to do ba!ki!# applicati o!s o! the ;089 )ile
syste& ('hich has itGs o'! bu))ers, but !o tra!sactio!s. 2here 'ill be a
!u&ber o) proble&s, the ki!d that )aced database practi ti o!ers i! the +/s.
1. 1nconsistent result . 1ur applicati o! is tra!s)erri !# &o!ey )ro& o!e
accou!t to a!other (di))ere!t pa#es. 1!e accou!t bala!ce #ets out to disk
(ru! out o) bu))er space a!d the! the co&puter crashes.
<he! bri!# co&puter up a#ai!, have !o idea 'hat used to be i! &e&ory
bu))er, a!d o! disk 'e have destroyed &o!ey.
". Errors of concurrent e:ecution . (1!e ki!d: 8!co!siste!t A!alysis.
2eller 1 tra!s)ers &o!ey )ro& Acct A to Acct B o) the sa&e custo&er, 'hile
2eller " is per)or&i !# a credi t check by addi!# bala!ces o) A a!d B. 2eller "
ca! see A a)ter tra!s)er subtracted, B be)ore tra!s)er added.
F"1*F
*. 4ncertai nt y as to $en canges become permanent . At the very
least, 'e 'a!t to k!o' 'he! it is sa)e to ha!d out &o!ey: do!G t 'a!t to
)or#et 'e did it i) syste& crashes, the! o!ly data o! disk is sa)e.
<a!t this to happe! at tra!sacti o! co&&i t. A!d do!G t 'a!t to have to 'ri te
out all ro's i!vol ved i! tra!sacti o! (teller cash bala!ce FF very popular, 'e
bu))er it to save reads a!d 'a!t to save 'ri tes as 'ell.
2o solve these proble&s, syste&s a!al ysts ca&e up 'ith idea o) tra!sac tio!
()or&aliBed i! 1.E/s. %ere are AC8D #uara!tees:
%tomicity . 2he set o) record updates that are part o) a tra!sactio! are
i!di visible (either they all happe! or !o!e happe!. 2his is true eve! i!
prese!ce o) a crash (see Durabili ty, belo'.
Consistency . 8) all the i!di vi dual processes )ollo' certai ! rules (&o!ey is
!ei ther created !or destroyed a!d use tra!sactio!s ri#ht, the! the rules
'o!G t be broke! by a!y set o) tra!sactio!s acti!# to#ether. 8&plied by
8solatio!, belo'.
1 solation . 7ea!s that operati o!s o) di))ere!t tra!sactio!s see& !ot to be
i!terleaved i! ti &e FF as i) A44 operati o!s o) o!e 2( be)ore or a)ter all
operati o!s o) a!y other 2(.
Durabili ty . <he! the syste& retur!s to the lo#ic a)ter a Co&&i t <ork
state&e!t, it #uara!tees that all 2( ;pdates are o! disk. 0o' A27 &a chi!e
ca! ha!d out &o!ey.
2he syste& is ki!d o) clever about Durabili ty. 8t does!G t 'a!t to )orce all
updated disk pa#es out o) bu))er o!to disk 'i th each 2( Co&&i t.
So it 'ri tes a set o) !otes to itsel) o! disk (called lo#s. A)ter crash ru!
!ecovery (also called !estart a!d &akes sure !otes tra!slate i!to apF
propri ate updates.
<hat about ReadF 1!ly 2(M (0o data updates, o!l y Selects. Ato&ici ty a!d
Durabili t y have !o e))ect, but 8solatio! does.
7o!ey spe!t o! 2ra!sacti o!al syste&s today is about S89 B844810 D144ARS
A :3AR. <eGre bei!# ri#orous about so&e o) this )or a B;S803SS reaso!.
!I. ! 2ransactional #istories .
Reads a!d <rites o) data ite&s. A data ite& &i#ht be a ro' o) a table or it
&i#ht be a! i!de( e!try or set o) e!tri es. >or !o' talki !# about ro's.
F"1$F
Read a data ite& 'he! access it 'i thout cha!#i !# it. 1)te! a select.
select val i!to :p#&val 1 )ro& 21 'here u!iquei d I AC
<e 'ill 'ri te this as R
i
(A: tra!sactio! 'i th ide!ti )icati o! !u&ber i reads
data ite& A. Pi!d o) rou#h ? 'o!G t al'ays have be retri evi !# by u!iquei d
I A. But it &ea!s that 'e are readi!# a ro' ide!ti )i ed as A. 0o':
update 21 set val I p#&val " 'here u!iquei d I BC
'e 'ill 'ri te this as <
H
(BC 2( H 'ri tes BC say ;pdate resul ts i! <rite.
Ca! #et co&plicated. Really readi!# a! i!de( e!try as 'ell to 'ri te B.
Co!sider:
update 21 set val I val \ " 'here u!iqueid I BC
%ave to read a! i!de( e!try, R
H
(predicate: u!iquei d I B, the! a pair o) ro'
operati o!s: R
H
(B (have to read it )irst, the! update it <
H
(B. %ave to read
it i! this case be)ore ca! 'ri te it.
update 2 set val I val \ " 'here u!iquei d bet'ee! :lo' a!d :hi#hC
<ill resul t i! a lot o) operati o!s: R
H
(predicate: u!iquei d bet'ee! :lo' a!d
:hi#h, the! R
H
(B1 <
H
(B1 R
H
(B" <
H
(B" . . . R
H
(B0 <
H
(B0.
TT2he reaso! )or this !otati o! is that o)te! have to co!sider co&ple( i! ter F
leaved histories o) co!curre!t tra!sacti o!sC 3(a&ple history:
(1/.1." . . . R
"
(A <
"
(A R
1
(A R
1
(B R
"
(B <
"
(B C
1
C
"
. . .
0ote C
i
&ea!s co&&i t by 2( i. A seque!ce o) operati o!s like this is k!o'!
as a 1istory or so&eti &es a Schedule .
A history resul ts )ro& a series o) operati o!s sub&i t ted by users, tra!s lated
i!to R D < operati o!s at the level o) the Scheduler. See >i#. 1/.1 .
8t is the Hob o) the scheduler to look at the history o) operati o!s as it co&es
i! a!d provi de the 8solatio! #uara!tee , by so&eti &es delayi !# so&e
operati o!s, a!d occasio!all y i!sisti !# that so&e tra!sactio!s be aborted.
By this &ea!s it assures that the seque!ce o) operati o!s is equi vale!t i!
e))ect to so&e serial schedule (all ops o) a 2( are per)or&ed i! seque!ce
'i th !o i!terleavi !# 'i th other tra!sactio!s. See >i#ure 1/.1, p#. ,$/.
F"1+F
8! )act, (1/.1." above is a! 8443GA4 schedule. Because 'e ca! 2%80P o) a
situati o! 'here this seque!ce o) operati o!s #ives a! i!co!siste!t resul t.
3(a&ple 1/.1.1 . Say that the t'o ele&e!ts A a!d B i! (1/.1." are Acct
records 'i th each havi!# bala!ce +/ to be#i! 'i th. 8!co!siste!t A!alysis.
2
1
is addi!# up bala!ces o) t'o accou!ts, 2
"
is tra!s)erri !# */ u!i ts )ro& A
to B.
. . . R
"
(A, +/ <
"
(A, "/ R
1
(A, "/ R
1
(B, +/ R
"
(B, +/ <
"
(B, -/ C
1
C
"
. . .
A!d 2 deter&i !es that the custo&er )ails the credi t check (because u!der
bala!ce total o) -/, say.
But this could !ever have happe!ed i! a serial schedule, 'here all opera tio!
o) 2
"
occurred be)ore or a)ter all operati o!s o) 2
"
.
. . . R
"
(A, +/ <
"
(A, "/ R
"
(B, +/ <
"
(B, -/ C
"
R
1
(A, "/ R
1
(B, -/ C
"
. . .
or
. . . R
1
(A, +/ R
1
(B, +/ C
1
R
"
(A, +/ <
"
(A, "/ R
"
(B, +/ <
"
(B, -/ C
"
. . .
A!d i! both cases, 2
1
sees total o) 1//, a Co!siste!t 5ie'.
0otice 'e 8023R6R323D the Reads a!d <rites o) (1/.1." to create a &odel
o) 'hat 'as bei!# read a!d 'ri tte! to sho' there 'as a! i!co!siste!cy.
2his 'ould !ot be do!e by the Scheduler. 8t si&pl y )ollo's a !u&ber o)
rules 'e e(plai! shortl y. <e &ai!tai ! that a serial history is al'ays
co!siste!t u!der a!y i!terpretati o!.
!I. ". 1nterl ea5ed /eadJ ?ri t e Operati ons
@uickTT
8) a serial history is al'ays co!siste!t, 'hy do!G t 'e Hust e!)orce serial
histories.
2he scheduler could take the )irst operati o! that it e!cou!ters o) a #ive!
tra!sactio! (2
"
i! the above e(a&pl e a!d delay all ops o) other 2(s (the
Scheduler is allo'ed to do this u!til all operati o!s o) 2
"
are co&pleted a!d
the tra!sactio! co&&i ts (C
"
.
Reaso! 'e do!G t do thisM 6er)or&a!ce. 8t tur!s out that a! avera#e 2( has
relati vel y s&all C6; bursts a!d the! 8N1 duri!# 'hich C6; has !othi !# to do.
See >i# 1/.*, p#. ,$$. <he! 8N1 is co&plete, C6; ca! start up a#ai!.
F"1,F
<hat do 'e 'a!t to doM 4et a!other 2( ru! (call this a!other thread duri!#
slack C6; ti &e. (8!terleave. Does!G t help &uch i) have o!l y o!e disk (disk
is bottl e!eck. See >i# 1/.$, p#. ,$$.
But i) 'e have t'o disks i! use all the ti &e 'e #et about t'ice the
throu#hput. >i# 1/.+, p#. ,$+.
A!d i) 'e have &a!y disks i! use, 'e ca! keep the C6; 1//[ occupied. >i#
1/.,, p# ,$,.
8! actuali t y, everythi !# does!G t 'ork out per)ectl y eve!l y as i! >i# 1/.,.
%ave &ul ti pl e threads a!d &ul ti pl e disks, a!d like thro'i !# darts at slots.
2ry to have e!ou#h threads ru!!i !# to keep lots o) disk occupied so C6; is
./[ occupied. <he! o!e thread does a! 8N1, 'a!t to )i!d a!other thread
'i th co&pleted 8N1 ready to ru! a#ai!.
4eave this to you FF covered i! %o&e'ork.
!I. ' -erialiMabili ty and te Precedence >rap .
<e 'a!t to co&e up 'i th a set o) rules )or the Scheduler to allo' opera tio!s
by i!terl eaved tra!sacti o!s a!d #uara!tee Serializabili ty .
SerialiBabili t y &ea!s the series o) operati o!s is 3@;85A4302 to a Serial
schedule, 'here operati o!s o) 2( are !ot i!terleaved.
%o' ca! 'e #uara!tee thisM >irst !otice that i) t'o tra!sactio!s !ever
access the sa&e data ite&s, it does!G t &atter that theyG re i!terleaved .
<e ca! co&&ute ops i! the history o) requests per&i t ted by the scheduler
u!til all ops o) o!e 2( are to#ether (serial history. 2he operati o!s do!G t
a))ect each other, a!d order does!G t &atter.
<e say that the Scheduler is readi!# a history % (order operati o!s are
sub&i tted a!d is #oi!# to create a serialiBable history S(% (by delay, etc.
'here operati o!s ca! be co&&uted to a serial history.
1P, !o' i) 'e have operati o!s by t'o di))ere!t tra!sactio!s that do a) )ect
the sa&e data ite&, 'hat the!M
2here are o!l y )our possibili ti es: R or < by 2
1
)ollo'ed by R or < by 2
"
.
Co!sider history:
. . . R
1
(A . . . <
"
(A . . .
F"1EF
<ould it &atter i) the order 'ere reversedM :3S. Ca! easil y i&a#i !e a!
i!terpretati o! 'here 2
"
cha!#es data 2
1
reads: i) 2
1
reads it )irst, sees old
versio!, i) reads it a)ter 2
"
cha!#es it, sees later versio!.
<e use the !otati o!:
R
1
(A UU
%
<
"
(A
to &ea! that R
1
(A co&es be)ore <
"
(A i! %, a!d 'hat 'e have Hust !oticed
is that 'he!ever 'e have the orderi !# i! % 'e &ust also have:
R
1
(A UU
S(%
<
"
(A
2hat is, these ops &ust occur i! the sa&e order i! the serialiBable sched ule
put out by the Scheduler. 8) R
1
(A UU
%
<
"
(A the! R
1
(A UU
%
<
"
(A.
0o' these tra!sacti o! !u&bers are Hust arbi trari l y assi#!ed labels, so it is
clear 'e could have 'ri tte! the above as )ollo's:
8) R
"
(A UU
%
<
1
(A the! R
"
(A UU
%
<
1
(A.
%ere 2( 1 a!d 2( " have e(cha!#ed labels. 2his is a!other o!e o) the )our
cases. 0o' 'hat ca! 'e say about the )ollo'i !#M
R
1
(A UU
%
R
"
(A
2his ca! be co&&uted FF reads ca! co&e i! a!y order si!ce they do!G t a) )ect
each other. 0ote that i) there is a third tra!sacti o!, 2
*
, 'here:
R
1
(A UU
%
<
*
(A UU
%
R
"
(A
2he! the reads ca!!ot be co&&uted (because 'e ca!!ot co&&ute either
o!e 'i th <
*
(A, but this is because o) applicati o! o) the earlier rules, !ot
depe!di !# o! the reads as they a))ect each other.
>i!all y, 'e co!sider:
<
1
(A UU
%
<
"
(A
A!d it should be clear that these t'o operati o!s ca!!ot co&&ute. 2he ul F
ti &ate outco&e o) the value o) A 'ould cha!#e. 2hat is:
8) <
1
(A UU
%
<
"
(A the! <
1
(A UU
S(%
<
"
(A
F"1-F
2o su&&ariBe our discussio!, 'e have De)i!i ti o! 1/.*.1, p#. ,+/.
De). 1/.*.1. 2'o operati o!s 9
i
(A a!d :
H
(B i! a history are said to conflict
(i.e., the order &atters i) a!d o!l y i) the )ollo'i !# three co!di tio!s hold:
(1 A B. 1peratio!s o! disti!ct data ite&s !ever co!)lict.
(" i a H. 1perati o!s co!)lict o!l y i) they are per)or&ed by di))ere!t 2(s.
(* 1!e o) the t'o operati o!s 9 or : is a 'ri te, <. (1ther ca! be R or <.
0ote i! co!!ectio! 'i th (" that t'o operati o!s o) the SA73 tra!sactio! also
ca!!ot be co&&uted i! a history, but !ot because they co!)lict. 8) the
scheduler delays the )irst, the seco!d o!e 'ill !ot be sub&i t ted.
F"1.F
Class "!.
<e have Hust de)i!ed the idea o) t'o co!)licti !# operati o!s. (RepeatM
<e shall !o' sho' ho' so&e histories ca! be sho'! !ot !ot to be serial F
iBable. 2he! 'e sho' that such histori es ca! be characteriBed by a! eas ily
ide!ti )i ed characteristi c i! ter&s o) co!)licti !# operati o!s.
2o sho' that a history is !ot serialiBable (SR, 'e use a! interpretati on o)
the history.
De). 1/.*.". A! i!terpretati o! o) a! arbi trary history % co!sists o) * parts.
(1 A descripti o! o) the purpose o) the lo#ic bei!# per)or&ed. (" Spec F
i)icati o! o) precise values )or data ite&s bei!# read a!d 'ri tte! i! the
history. (* A co!siste!cy rule, a propert y that is obviousl y preserved by
isolated tra!sactio!s o) the lo#ic de)i!ed i! (1.
3(. 1/.*.1. 3(a&ple 1/.*.1. %ere is a history, %1, 'e clai! is !ot SR.
%1 I R
"
(A <
"
(A R
1
(A R
1
(B R
"
(B <
"
(B C
1
C
"
%ere is a! i!terpretati o!. 2
1
is doi!# a credi t check, addi!# up the bala!ces
o) A a!d B. 2 is tra!s)erri !# &o!ey )ro& A to B. %ere is the co!siste!cy
rule: 0either tra!sactio! creates or destroys &o!ey. 5alues )or %1 are:
%1G I R
"
(A,+/ <
"
(A,"/ R
1
(A,"/ R
1
(B,+/ R
"
(B,+/ <
"
(B,E/ C
1
C
"
2he schedule %1 is !ot SR because %1G sho's a! inconsistent resul t2 su& o)
E/ )or bala!ces A a!d B, thou#h !o &o!ey 'as destroyed by 2
"
i! the
tra!s)er )ro& A to B. 2his could !ot have occurred i! a serial e(ecuti o!.
2he co!cept o) co!)licti !# operati o!s #ives us a direct 'ay to co!)i r& that
the history %1 is !ot SR. 0ote the seco!d a!d thi rd operati o!s o) %1,
<
"
(A a!d R
1
(A. Si!ce <(A co&es be)ore R(A i! %1, 'ri tte!:
<
"
(A UU
%1
R
1
(A
<e k!o' si!ce these operati o!s co!)lict that they &ust occur i! the sa&e
order i! a!y equival e!t serial history, S(%1, i.e.: <
"
(A UU
S(%1
R
1
(A
0o' i! a serial history, all operati o!s o) a tra!sactio! occur to#ether
2hus <
"
(A UU
S(%1
R
1
(A &ea!s that 2
"
UU
S(%1
2
1
, i.e. 2
"
occurs be)ore 2
1
i! a!y serial history S(%1 (there &i#ht be &ore tha! o!e.
F""/F
But !o' co!sider the )ourth a!d si(th operati o!s o) %1. <e have:
R
1
(B UU
%1
<
"
(B
Si!ce these operati o!s co!)lict, 'e also have R
1
(B UU
S(%1
<
"
(B
But this i&plies that 2
1
co&es be)ore 2
"
, 2
1
UU
S(%1
2
"
, i! a!y equival e!t
serial history %1. A!d this is at odds 'i th our previous co!clusio!.
8! a!y serial history S(%1, either 2
1
UU
S(%1
2
"
or 2
"
UU
S(%1
2
1
, !ot both.
Si!ce 'e co!clude )ro& e(a&i !i !# %1 that both occur, S(%1 &ust !ot reall y
e(ist. 2here)ore, %1 is !ot SR.
<e illustrate this tech!i que a )e' &ore ti &es, a!d the! prove a #e!eral
char acteriBati o! o) SR histori es i! ter&s o) co!)licti !# operati o!s.
3(. 1/.*.". Co!sider the history:
%" I R
1
(A R
"
(A <
1
(A <
"
(A C
1
C
"
<e #ive a i!terpretati o! o) this history as a paradi#& called a lost update .
Assu&e that A is a ba!k bala!ce starti !# 'i th the value 1// a!d 21 tries to
add $/ to the bala!ce at the sa&e ti &e that 2" tries to add +/.
%"G I R
1
(A, 1// R
"
(A, 1// <
1
(A, 1$/ <
"
(A, 1+/ C
1
C
"
Clearl y the )i!al resul t is 1+/, a!d 'e have lost the update 'here 'e added
$/. 2his could!G t happe! i! a serial schedule, so %1 is !o!F SR.
8! ter&s o) co!)licti !# operati o!s, !ote that operati o!s 1 a!d $ i&pl y that 21
UU
S(%"
2". But operati o!s " a!d * i&pl y that 2" UU
S(%"
21. 0o SR
schedule could have both these properti es, there)ore %" is !o!F SR.
By the 'ay, this e(a&pl e illustrates that a co!)licti !# pair o) the )or& R
1
(A .
. . <
"
(A does i!deed i&pose a! order o! the tra!sactio!s, 21 UU 2", i! a!y
equival e!t serial history.
%" has !o other types o) pairs that C1;4D co!)lict a!d &ake %" !o!F SR.
3( 1/.*.*. Co!sider the history:
%* I <
1
(A <
"
(A <
"
(B <
1
(B C
1
C
"
F""1F
2his e(a&pl e 'ill illustrate that a co!)licti !# pair <
1
(A . . . <
"
(A ca!
i&pose a! order o! the tra!sacti o!s 21 UU 2" i! a!y equi vale!t SR history.
;!dersta!d that these are 'hat are k!o'! as OBli!d <ritesO: there are !o
Reads at all i!vol ved i! the tra!sactio!s.
Assu&e the lo#ic o) the pro#ra& is that 21 a!d 2" are both &ea!t to Otop
upO the t'o accou!ts A a!d B, setti !# the su& o) the bala!ces to 1//.
21 does this by setti !# A a!d B both to +/, 2" does it by setti !# A to -/ a!d
B to "/. %ere is the resul t )or the i!terl eaved history %*.
%*G I <
1
(A, +/ <
"
(A, -/ <
"
(B, -/ <
1
(B, +/ C
1
C
"
Clearl y i! a!y serial e(ecuti o!, the resul t 'ould have A \ B I 1//. But 'i th
%*G the e!d value )or A is -/ a!d )or B is +/.
2o sho' that %* is !o!F SR by usi!# co!)licti !# operati o!s, !ote that op F
eratio!s 1 a!d " i&pl y 21 UU 2", a!d operati o!s * a!d $ that 2" UU 21.
See&i!#l y, the ar#u&e!t that a! i!terl eaved history % is !o!F SR see&s to
reduce to looki!# at co!)licti !# pairs o) operati o!s a!d keepi!# track o) the
order i! 'hich the tra!sactio!s 'ill occur i! a! equi vale!t S(%.
<he! there are t'o tra!sacti o!s, 21 a!d 2", 'e e(pect to )i!d i! a !o!F SR
schedule that 21 UU
S(%
2" a!d 2" UU
S(%
21, a! i&possibili ty.
8) 'e do!G t have such a! i&possibili ty arise )ro& co!)licti !# operati o!s i! a
history %, does that &ea! that % is SRM
A!d 'hat about histori es 'i th * or &ore tra!sactio!s i!vol vedM <8ll 'e ever
see so&ethi !# i&possible other tha! 21 UU
S(%
2" a!d 2" UU
S(%
21M
<e start by de)i!i!# a 6recede!ce Graph. 2he idea here is that this allo's
us to track all co!)licti !# pairs o) operati o!s i! a history %.
De). 1/.*.*. 2he 6recede!ce Graph. A precede!ce #raph )or a history % is a
directed #raph de!oted by 6G(%.
2he vertices o) 6G(% correspo!d to the tra!sactio!s that have C1778223D
i! %: that is, tra!sactio! 2i 'here C e(ists as a! operati o! i! %.
A! ed#e 2i FT 2H e(ists i! 6G(% 'he!ever t'o co!)licti !# operati o!s 9
i
a!d
:
H
occur i! that order i! %. 2hus, 2i FT 2H should be i!ter preted to &ea! that
2i &ust precede 2H i! a!y equival e!t serial history S(%.
F"""F
<he!ever a pair o) operati o!s co!)lict i! % )or C1778223D tra!sactio!s, 'e
ca! dra' the correspo!di !# direct arc i! the 6recede!ce Graph, 6G(%.
2he reaso! u!co&&i t t ed tra!sacti o!s do!G t cou!t is that 'eG re tryi !# to
)i#ure out 'hat the scheduler ca! allo'. ;!co&&i t t ed tra!sactio!s ca!
al'ays be aborted, a!d the! it 'ill be as i) they did!G t happe!.
8t 'ould be u!)ai r to hold u!co&&i t ted tra!sactio!s a#ai!st the scheduler
by sayi!# the history is !o!F SR because o) the&.
2he e(a&pl es 'e have #ive! above o) i&possible co!di tio!s arisi!# )ro&
co!)licti !# operati o!s look like this:
-/ 0000000000000000000000; -<
=>000000000000000000000000(
1) course this is 'hat is called a circui t i! a directed #raph (a di#raph. 2his
su##ests other proble&s that could arise 'ith * or &ore 2(s.
8t should be clear that i) 6G(% has a circui t, there is !o 'ay to put the
tra!sactio!s i! serial order so 2i al'ays co&es be)or 2H )or all ed#es 2i FT
2H i! the circui t.
2hereG ll al'ays be o!e ed#e Opoi!ti !# back'ardO i! ti &e, a!d thatG s a co! F
tradicti o!, si!ce 2i FT 2H &ea!s 2i should co&e B3>1R3 2H i! S(%.
%o' do 'e &ake this i!tui ti o! ri#orousM A!d i) 6G(% does!G t have a cir cuit,
does that &ea! the history is SRM
2h&. 1/.*.$. 2e -erialiMabili ty 2eorem . A history % has a! equi vale!t
serial e(ecuti o! S(% i)) the precede!ce #raph 6G(% co!tai!s !o circui t.
6roo). 4eave o!l y i) )or e(ercises at e!d o) chapter. 8.e., 'ill sho' there
that a circui t i! 6G(% i&plies there is !o serial orderi !# o) tra!sactio!s.
%ere 'e prove that i) 6G(% co!tai!s !o circui t, there is a serial orderi !# o)
the tra!sactio!s so !o ed#e o) 6G(% ever poi!ts )ro& a later to a! ear lier
tra!sactio!.
Assu&e there are & tra!sactio!s i!vol ved, a!d label the& 21, 2", . . ., 2&.
<e are tryi !# to )i!d a reorderi !# o) the i!te#ers 1 to &, i(1, i(", . . ., i(&,
so that 2i(1, 2i(", . . ., 2i(& is the desired serial schedule.
F""*F
Assu&e a le&&a to prove later: 8! a!y directed #raph 'ith !o circui t there
is al'ays a verte( 'ith !o ed#e e!teri !# it.
1P, so 'e are assu&i !# 6G(% has !o circui t, a!d thus there is a verte(, or
tra!sactio!, 2k, 'ith !o ed#e e!teri !# it. <e choose 2k to be 2i(1.
0ote that si!ce 2i(1 has !o ed#e e!teri !# it, there is !o co!)lict i! % that
)orces so&e other tra!sactio! to co&e earlier.
(2his )its our i!tui ti o! per)ectl y. All other tra!sactio!s ca! be placed a)ter it
i! ti &e, a!d there 'o!G t be a! ed#e #oi!# back'ard i! ti &e.
0o' re&ove this verte(, 2i(1, )ro& 6G(% a!d all ed#es leavi !# it. Call the
resul ti !# #raph 6G
1
(%.
By the 4e&&a, there is !o' a verte( i! 6G
1
(% 'ith !o ed#e e!teri !# it.
Call that verte( 2i(".
(0ote that a! ed#e )ro& 2i(1 &i#ht e!ter 2i(", but that ed#e does!G t cou!t
because itGs bee! re&oved )ro& 6G
1
(%.
Co!ti!ue i! this )ashio!, re&ovi !# 2i(" a!d all itGs ed#es to )or& 6G
"
(%,
a!d so o!, choosi!# 2i(* )ro& 6G
"
(%, . . ., 2i(& )ro& 6G
&F 1
(%.
By co!structi o!, !o ed#e o) 6G(% 'ill ever poi!t back'ard i! the seque!ce
S(%, )ro& 2i(& to 2i(!, & T !.
2he al#ori th& 'e have used to deter&i !e this seque!ce is k!o'! as a
topological sort . 2his 'as a hiri!# questio! 8 sa' asked at 7icroso)t.
2he proo) is co&plete, a!d 'e !o' k!o' ho' to create a! equival e! SR
schedule )ro& a history 'hose precede!ce #raph has !o circui t.
4e&&a 1/.*.+. 8! a!y )i!i te directed acyclic #raph G there is al'ays a
verte( 'ith !o ed#es e!teri !# it.
6roo). Choose a!y verte( v1 )ro& G. 3ither this has the desired propert y, or
there is a! ed#e e!teri !# it )ro& a!other verte( v".
(2here &i#ht be several ed#es e!teri !# v1, but choose o!e.
0o' v" either has the desired propert y or there is a! ed#e e!teri !# it )ro&
verte( v*. <e co!ti !ue i! this 'ay, a!d either the seque!ce stops at so&e
verte( v&, or the seque!ce co!ti !ues )orever.
F""$F
8) the seque!ce stops at a verte( v&, thatG s because there is !o ed#e e! F
teri!# v&, a!d 'e have )ou!d the desired verte(.
But i) the seque!ce co!ti!ues )orever, si!ce this is a )i!i te #raph, soo!er or
later i! the seque!ce 'e 'ill have to have a repeated verte(.
Say that 'he! 'e add verte( v!, it is the sa&e verte( as so&e previousl y
&e!ti o!ed verte( i! the seque!ce, vi.
2he! there is a path )ro& v! FT v(!F 1 FT . . . v(i \1 FTvi , 'here vi v!. But
this is the de)i!i ti o! o) a circui t, 'hich 'e said 'as i&possible.
2here)ore the seque!ce had to ter&i !ate 'i th v& a!d that verte( 'as the
o!e desired 'i th !o ed#es e!teri !#.
F""+F
Class "".
!I. ( Locking Ensures -erialiMabil t y
See >i#. 1/.-, p#. ,/.. 27 passes o! calls such as )etch, select, i!sert,
delete, abortC Scheduler i!terprets the& as: R
i
(A, <
H
(B.
8t is the Hob o) the scheduler to &ake sure that !o !o!F SR schedules #et
past. 2his is !or&al l y do!e 'i th 3wo$ hase "oc'ing , or "64.
De). 1/.$.1. "64. 4ocks take! i! released )ollo'i !# three rules.
(1 Be)ore 2( i ca! read a data ite&, R
i
(A, scheduler atte&pts to Read 4ock
the ite& o! itGs behal), R4
i
(AC be)ore <
i
(A, try <rite 4ock, <4
i
(A.
(" 8) co!)licti !# lock o! ite& e(ists, requesti !# 2( &ust <A82. (Co! )lict i!#
locks correspo!di !# to co!)licti !# ops: t'o locks o! a data ite& co!)lict i)
they are atte&pt ed by di))ere!t 2(s a!d at least o!e o) the& is a <4.
(* 2here are t'o phases to locki!#, the #ro'i !# phase a!d the shri!ki !#
phase ('he! locks are released: R;
i
(AC 2he scheduler &ust e!sure that
ca!G t shri!k (drop a lock a!d the! #ro' a#ai! (take a !e' lock.
Rule (* i&plies ca! release locks be)ore Co&&i tC 7ore usual to release all
locks at o!ce o! Co&&i t, a!d 'e shall assu&e this i! 'hat )ollo's.
0ote that a tra!sacti o! ca! !ever co!)lict 'i th its o'! locks Q 8) 2i holds R4
o! A, ca! #et <4 so lo!# as !o other 2( holds a lock (&ust be R4.
A 2( 'i th a <4 does!G t !eed a R4 (<4 &ore po'er)ul tha! R4.
Clearl y locki!# is de)i!ed to #uara!tee that a circui t i! the 6recede!ce
Graph ca! !ever occur. 2he )irst 2( to lock a! ite& )orces a!y other 2( that
#ets to it seco!d to Oco&e laterO i! a!y SG.
But 'hat i) other 2( already holds a lock the )irst o!e !o' !eedsM 2his
'ould &ea! a circui t, but i! the <A82 rules o) locki!# it &ea!s 0382%3R 2(
CA0 353R G1 >1R<ARD AGA80. 2his is a D3AD41CP. 3(a&ple shortl y.
Side e))ect o) "64 is that Deadlocks ca! occur: <he! a deadlock occurs,
scheduler 'ill reco#!iBe it a!d )orce o!e o) the 2(s i!vol ved to Abort.
(0ote, there &i#ht be &ore tha! " 2(s i!vol ved i! a Deadlock.
3(. %ere is history !ot SR (3rror i! te(t: this is a varia!t o) 3(. 1/.$.1.
F"",F
%$ I R
1
(A R
"
(A <
"
(A R
"
(B <
"
(B R
1
(B C
1
C
"
Sa&e idea as 1/.*.1 'hy it is !o!F SR: 2" reads t'o bala!ces that start out
AI+/ a!d BI+/, 21 &oves */ )ro& A to B. 0o!FSR history because 21 sees
AI+/ a!d BI-/. 0o' try locki!# a!d releasi!# locks at co&&i t.
R4
1
(A R
1
(A R4
"
(A R
"
(A <4
"
(A (co!)licti !# lock held by 21 so 2" &ust
<A82 R4
1
(B R
1
(B C
1
(!o' 2" ca! #et <4
"
(A <
"
(A R4
"
(B R
"
(B <4
"
(B
<
"
(B C
"
<orks )i!e: 21 !o' sees AI+/, BI+/. Serial schedule, 21 the! 2".
But 'hat i) allo'ed to ;!lock a!d the! acquire &ore locks later. Get !o!F SR
schedule. Sho's !ecessity o) "64 Rule (*.
R4
1
(A R
1
(A R;
1
(A R4
"
(A R
"
(A <4
"
(A <
"
(A <;
"
(A R4
"
(B R
"
(B <4
"
(B
<
"
(B <;
"
(B R4
1
(B R
1
(B C
1
C
"
So %$ above is possible. But o!l y "64 rule broke! is that 21 a!d 2" u!lock
ro's, the! lock other ro's later.
2he <aitsF >or Graph. %o' scheduler checks i) deadlock occurs. 5ertices
are curre!tl y acti ve 2(s, Directed ed#s 2i FT 2H i)) 2i is 'ai ti !# )or a lock held
by 2H.
(0ote, &i#ht be 'ai ti !# )or lock held by several other 2(s. A!d possibl y #et
i! queue )or < lock behi!d others 'ho are also 'aiti !#. Dra' picture.
2he scheduler per)or&s lock operati o!s a!d i) 2( requi red to 'ait, dra's
!e' directed ed#es resul ti !#, the! checks )or circui t.
3( 1/.$.". %ere is schedule like %$ above, 'here 2" reverses order it
touches A a!d B (!o' touches B )irst, but sa&e e(a&pl e sho's !o!F SR.
%+ I R
1
(A R
"
(B <
"
(B R
"
(A <
"
(A R
1
(B C
1
C
"
4ocki!# resul t:
R4
1
(A R
1
(A R4
"
(B R
"
(B <4
"
(B <
"
(B R4
"
(A R
"
(A <4
"
(A (>ails: R41(A
held, 2" &ust <A82 )or 21 to co&plete a!d release locks R4
1
(B (>ails:
<4"(B held, 21 &ust 'ait )or 2" to co&plete: But this is a deadlockQ
Choose 2" as victi & (21 chose! i! te(t A" (!o' R4
1
(B 'ill succeed
R1(B C1 (start 2" over, retry, it #ets 2( !u&ber * R4
*
(B R
*
(B <4
*
(B
<
*
(B R4
*
(A R
*
(A <4
*
(A <*(A C*.
F""EF
4ocki!# serialiBed 21, the! 2" (retried as 2*.
2m. !I. (. ". 4ocki!# 2heore&. A history o) tra!sactio!al operati o!s that
)ollo's the "64 discipli!e is SR.
>irst, Lemma !I. (. ' . 8) % is a 4ocki!# 3(te!ded %istory that is "64 a!d the
ed#e 2i FT 2H is i! 6G(%, the! there &ust e(ist a data ite& D a!d t'o
co!)licti !# operati o!s 9i(D a!d :H(D such that 9;i(D UU% :4H(D.
Proof . Si!ce 2i FT 2H i! 6G(%, there &ust be t'o co!)licti !# ops 9i(D a!d
:H(D such that 9i(D UU% :H(D.
By the de)i!i ti o! o) "64, there &ust be locki!# a!d u!locki!# ops o! either
side o) both ops, e.#.: 94i(D UU% 9i(D UU% 9;i(D.
0o' bet'ee! the lock a!d u!lock )or 9i(D, the 9 lock is held by 2i a!d
si&ilarl y )or :H(D a!d 2H. Si!ce 9 a!d : co!)lict, the locks co!)lict a!d the
i!tervals ca!!ot overlap. 2hus, si!ce 9i(D UU% :H(D, 'e &ust have:
94i(D UU% 9i(D UU% 9;i(D UU% :4H(D UU% :H(D UU% :;H(D
A!d i! particular 9;i(D UU% :4H(D.
Proof of 2m. !I. (. " . <e 'a!t to sho' that every "64 history % is SR.
Assu&e i! co!tradicti o! that there is a cycle 21 FT 2" FT . . . FT 2! FT 21 i!
6G(%. By the 4e&&a, )or each pair 2k FT 2(k\1, there is a data ite& Dk
'here 9;k(Dk UU% :4(k\1(Dk. <e 'ri te this out as )ollo's:
1. 9;1(D1 UU% :4"(D1
". 9;"(D" UU% :4*(D"
. . .
!F1. 9;(!F 1(D(!F 1 UU% :4!(D(!F 1
!. 9;!(D! UU% :41(D! (0ote, 21 is 2(!\1 too.
But !o' have (i! 1. a! u!lock o) a data ite& by 21 be)ore (i! !. a lock o) a
data ite&. So !ot "64 a)ter all. Co!tradicti o!.
2here)ore % is "64 i&plies !o circui t i! the 6G(%, a!d thus % is SR.
0ote that !ot all SR schedules 'ould obey "64. 3.#., the )ollo'i !# is SR:
%E I R1(A <1(A R"(A R1(B <1(B R"(B C1 C"
F""-F
But it is !ot "64 (2" breaks throu#h locks held by 21. <e ca! opti &isti cally
allo' a 2( to break throu#h locks i! the hopes that a circui t 'o!G t
occur i! 6G(%. But &ost databases do!G t do that.
F"".F
Class "'.
!I. * Le5els of 1solation
2he idea o) 8solatio! 4evels, de)i!ed i! A0S8 S@4F.", is that people &i#ht
'a!t to #ai! &ore co!curre!cy, eve! at the e(pe!se o) i&per)ect isolatio!.
A paper by 2ay sho'ed that 'he! there is serious loss o) throu#hput due to
locki!#, it is #e!erall y !ot because o) deadlock aborts (havi !# to retry but
si&pl y because o) tra!sactio!s bei!# bloc'ed a!d havi!# to 'ai t.
Recall that the reaso! )or i!terleavi !# tra!sacti o! operati o!s, rather tha!
Hust i!sisti !# o! serial schedules, 'as so 'e could keep the C6; busy.
<e 'a!t there to al'ays be a !e' tra!sactio! to ru! 'he! the ru!!i!#
tra!sactio! did a! 8N1 'ai t.
But i) 'e assu&e that a lot o) tra!sacti o!s are 'aiti !# )or locks, 'e lose this.
2here &i#ht be o!ly o!e tra!sactio! ru!!i !# eve! i) 'e have "/ try i!# to
ru!. All but o!e o) the tra!sactio!s are i! a 'ai t chai!Q
So the idea is to be less strict about locki!# a!d let &ore tra!sacti o!s ru!.
2he proble& is that droppi !# proper "64 &i#ht cause S3R81;S errors i!
applicati o!s. But people S2844 do it.
2he idea behi!d A0S8 S@4F.. 8solatio! 4evels is to 'eake! ho' locks are
held. 4ocks are!G t al'ays take!, a!d eve! 'he! they are, &a!y locks are
released be)ore 312.
A!d &ore locks are take! a)ter so&e locks are released i! these sche&es.
0ot 2'oF 6hase, so !ot per)ect 8solatio!.
(0ote i! passi!#, that A0S8 S@4F." 'as ori#i!all y i!te!ded to de)i!e isolatio!
levels that did !ot require locki!#, but it has bee! sho'! that the de)i!i ti o!s
)ailed to do this. 2hus the locki!# i!terpretati o! is ri#ht.
De)i!e short$ term loc's to &ea! a lock is take! prior to the operati o! (R or
< a!d released 8773D8A234: A>23R<ARD. 2his is the o!l y alter!ati ve to
long$ term loc's, 'hich are held u!til 312.
2he! A0S8 S@4F." 8solatio! levels are de)i!ed as )ollo's (>i#. 1/.. FF so&e
di))ere!ce )ro& the te(t:
<rite locks o!
ro's o) a table
are lo!# ter&
Read 4ocks o!
ro's o) a table
are lo!# ter&
Read locks o!
predicates are
lo!# ter&
F"*/F
Read ;!co&&i t ted
(Dirty Reads
0A
(Read 1!ly
0o Read 4ocks
take! at all
0o Read 4ocks
take! at all
Read Co&&i tted :es 0o 0o
Repeatabl e Read :es :es 0o
SerialiBable :es :es :es
Note tat ?ri te Predicate Locks are taken and eld longK term in all
isolation le5els listed . <hat this &ea!s is e(plai !ed later.
8! Read ;!co&&i t ted (R;, !o Read locks are take!, thus ca! read data o!
'hich <rite lock e(ists (!othi !# to stop you i) do!G t have to <A82 )or R4.
2hus ca! read u!co&&i t t ed dataC it 'ill be 'ro!# i) 2( that cha!#ed it later
aborts. But R; is Hust to #et a S2A28S28CA4 idea o) sales duri!# the day
(say. C31 'a!ts to k!o' ballpark )i#ure FF 1P i) !ot e(act.
8! Read Co&&i t ted (RC, 'e take <rite locks a!d hold the& to 312, a!d
Read 4ocks o! ro's read a!d predicates a!d release i&&edi atel y. (Cover
predicates belo'.
6roble& that ca! arise is serious o!e, 4ost ;pdate (3(a&pl e 1/.*.":
...R
1
(A,1// R
"
(A,1// <
1
(A,1$/ <
"
(A,1+/ C
1
C
"
...
Si!ce R locks are released i&&edi atel y, !othi !# stops the later <rites, a!d
the i!cre&e!t o) $/ is over'ri t te! by a! i!cre&e!t o) +/, i!stead o) the t'o
i!cre&e!ts addi!# to #ive ./.
Call this the ScholarG s 4ost ;pdate A!o&al y (si!ce &a!y people say 4ost
;pdate o!ly happe!s at Read ;!co&&i t ted.
2his is 392R3734: serious, obviousl y, a!d a! e(a&pl e o) lost update i! S@4
is #ive! i! >i#ure 1/.1" (p#. ,,, )or a sli#htl y &ore restricti ve level: Cursor
Stabili ty. Applicati o!s that use RC &ust avoid this ki!d o) update.
8! >i#ure 1/.11, 'e see ho' to avoid this by doi!# the ;pdate i!di visibl y i! a
si!#le operati o!.
0ot all ;pdates ca! be do!e this 'ay, ho'ever, because o) co&ple( cases
'here the ro's to bue updated ca!!ot be deter&i !ed by a Boolea! search
co!di ti o!, or 'here the a&ou!t to update is !ot a si&ple )u!cti o!.
8t tur!s out the 8B7Gs Cursor Stabili ty #uara!tees a special lock 'ill be held
o! curre!t ro' u!der cursor, a!d at )irst it 'as thou#ht that A0S8 Read
Co&&i tted #uara!teed that, but it does !ot.
F"*1F
6robabl y &ost products i&ple&e!t a lock o! curre!t o) cursor ro', but there
is !o #uara!tee. 033D 21 23S2 i) #oi!# to depe!d o! this.
8! Repeatabl e Read 8solatio! , this is the isolati o! level that &ost people
thi!k is all that is &ea!d by "64. All data ite&s read a!d 'ri tte! have R4s
a!d <4s take! a!d held lo!#F ter&, u!til 312.
So 'hatG s 'ro!#M <hat ca! happe!M
3(a&ple 1/.+.*, p#. ,,,, 6ha!to& ;pdate A!o&al y.
R
1
(predicate: bra!chLid I GS>BayG R
1
(A1,1//.// R
1
(A", 1//.//
R
1
(A*,1//.// 8
"
(A$, bra!chLid I GS>BayG, bala!ce I1//.//
R
"
(bra!chLtotals, bra!chLid I S>Bay, *//.//
<
"
(bra!chLtotals, bra!chLid I S>Bay ,$//.// C
"
R
1
(bra!chLtotals, bra!chLid I S>Bay, $// (6ri!ts out error &essa#e C
1
21 is readi!# all the accou!ts 'i th bra!chLid I S>Bay a!d testi !# that the
su& o) bala!ces equals the bra!chLtotal )or that bra!ch (accou!ts a!d
bra!chLtotals are i! di))ere!t tables
A)ter 21 has #o!e throu#h the ro's o) accou!ts, 2" i!serts a!other ro' i!to
accou!ts 'i th bra!chLid I S>Bay (21 'ill !ot see this as itGs already sca!!ed
past the poi!t 'here it is i!serted, 2" the! updates the bra!chLtotal )or
S>Bay, a!d co&&i ts.
0o' 21, havi!# &issed the !e' accou!t, looks at the bra!chLtotal a!d sees
a! error.
2here is !o error reall y, Hust a !e' accou!t ro' that 21 did!G t see.
0ote that !obody is breaki !# a!y rules about data ite& locks. 2he i!sert by
2" holds a 'ri te lock o! a !e' accou!t ro' that 21 !ever read. 2" locks the
bra!chLtotal ro', but the! co&&i ts be)ore 21 tries to read it.
0o data ite& lock C1;4D help 'i th this proble&. But 'e have !o!F SR be F
havior !o!ethel ess.
2he solutio! is this: <he! 21 reads the predicate bra!chLid I S>Bay o!
accou!ts, it takes a Read lock 10 2%A2 6R3D8CA23, that is to say a Read
lock o! the S32 o) ro's to be retur!ed )ro& that Select state&e!t.
0o' 'he! 2" tries to 8!sert a !e' ro' i! accou!ts that 'ill cha!#e the set
o) ro's to be retur!ed )or S>Bay, it &ust take a <rite lock o! that predicate.
F"*"F
Clearl y this <rite lock a!d Read lock 'ill co!)lict. 2here)ore 2" 'ill have to
'ai t u!til 21 reaches 312 a!d releases all locks.
So the history o) 3(a&pl e 1/.+.* ca!G t happe!. (8! reali ty, use a type o)
locki!# called PeyF Ra!#e locki!# to #uara!tee predicate locks. Cover i!
Database 8&ple&e!tati o! course.
A0S8 Repeatable Read 8solatio! does!G t provi de 6redicate 4ocks, but A0S8
SerialiBable does.
0ote that 1racle does!G t ever per)or& predicate locks. 1racleGs
S3R8A48VAB43 isolati o! level uses a di))ere!t approach, based o! s!apshot
reads, that is beyo!d 'hat 'e ca! e(plai! i! this course.
!I. ) 2ransactional /eco5ery .
2he idea o) tra!sactio!al recovery is this.
7e&ory is O5olatileO, &ea!i!# that at u!schedul ed ti &es 'e 'ill lose
&e&ory co!te!ts (or beco&e usure o) the validi ty.
But a database tra!sactio!, i! order to 'ork o! data )ro& disk, &ust read it
i!to &e&ory bu))ers.
Re&e&ber that a tra!sacti o! is Oato&i cO, &ea!i !# that all update opera F
tio!a a tra!sacti o! per)or&s &ust either A44 succeed or A44 )ail.
8) 'e read t'o pa#es i!to &e&ory duri!# a tra!sactio! a!d update the&
both, 'e &i#ht (because o) bu))eri !# have o!e o) the pa#es #o back out to
disk be)ore 'e co&&i t.
<hat are 'e to do about this a)ter a crashM A! update has occurred to disk
'here the tra!sacti o! did !ot co&&i t. %o' do 'e put the old pa#e back i!
placeM
%o' do 'e eve! k!o' 'hat happe!edM 2hat 'e did!G t co&&i tM
A si&ilar proble& arises i) 'e have t'o pa#es i! &e&ory a!d a)ter co&&i t
'e &a!a#e to 'ri te o!e o) the pa#es back to disk, but !ot the other.
(8! )act, 'e al'ays atte&pt to &i!i &iBe disk 'ri tes )ro& popular bu))ers,
Hust as 'e &i!i &iBe disk reads.
%o' do 'e )i( it so that the pa#e that did!G t #et 'ri tte! out to disk #ets out
duri!# recoveryM
F"**F
2he a!s'er is that as a tra!sacti o! pro#resses 'e 'ri te !otes to ourselves
about 'hat cha!#es have bee! &ade to disk pa#es. <e e!sure that these
!otes #et out to disk to allo' us to correct a!y errors a)ter a crash.
2hese !otes are called Olo#sO, or Olo# e!tri esO. 2he lo# e!tries co!tai !
OBe)ore 8&a#esO a!d OA)ter 8&a#esO o) every update &ade by a 2ra!sacti o!.
8! recovery, 'e ca! back up a! update that should!G t have #otte! to disk
(the tra!sactio! did!G t co&&i t by appl yi !# a Be)ore 8&a#e.
Si&ilarl y, 'e ca! appl y A)ter 8&a#es to correct )or disk pa#es that should
have #otte! to disk (the tra!sactio! did co&&i t but !ever &ade it.
2here is a Olo# bu))erO i! &e&ory (qui te lo!#, a!d 'e 'ri te the lo# bu))er
out to the Olo# o! diskO every ti &e o!e o) )ollo'i !# eve!ts occur.
(1 2he lo# bu))er )ills up. <e 'ri te it to disk a!d &ea!'hi l e co!ti!ue )illi!#
a!other lo# bu))er 'i th lo#s. 2his is k!o'! as Odoubl e bu))eri !#O a!d saves
us )ro& havi!# to 'ai t u!til the disk 'ri te co&pletes.
(" So&e tra!sactio! co&&i ts. <e 'ri te the lo# bu))er, i!cludi!# all lo#s up
to the prese!t ti &e, be)ore 'e retur! )ro& co&&i t to the applicati o! (a!d
the applicati o! ha!ds out the &o!ey at the A27. 2his 'ay 'eG re sure 'e
'o!G t )or#et 'hat happe!ed.
3verythi !# else i! the !e(t )e' sectio!s is details: 'hat do the lo#s look like,
ho' does recovery take place, ho' ca! 'e speed up recovery, etc.
!I. + /eco5ery in DetailC Log 7ormats .
Co!sider the )ollo'i !# history %+ o) operati o!s as see! by the sched uler:
(1/.E.1 %+ I R
1
(A,+/ <
1
(A,"/ R
"
(C,1// <
"
(C,+/ C" R
1
(B,+/ <
1
(B,-/ C
1
Because o) bu))eri !#, so&e o) the updates sho'! here &i#ht !ot #et out to
disk as o) the seco!d co&&i t, C1. Assu&e the syste& crashes i&&edi atel y
a)ter. %o' do 'e recover all these lost updatesM
<hile the tra!sactio! 'as occurri !#, 'e 'rote out the )ollo'i !# lo#s as each
operati o! occurred (>i#ure 1/.1*, p#. ,E*.
OPE/%K LO> EN2/0 <<< LE%.E 4P ON 3O%/D <<<
21ON
F"*$F
R
1
(A,+/ (S, 1 ? Start tra!sacti o! 2
1
F !o lo# e!try is 'ri tte!
)or a Read operati o!, but this operati o! is the start o) 2
1
<
1
(A,"/ (<, 1, A, +/, "/ ? 2
1
<rite lo# )or update o) A.bala!ce.
2he value +/ is the 3efore 1mage (31 )or A.bala!ce
colu&! i! ro' A, "/ is the %fter 1mage (%1 )or A.bala!ce
R
"
(C,1// (S, ", a!other start tra!sacti o! lo# e!try.
<
"
(C,+/ (<, ", C, 1//, +/, a!other <rite lo# e!try.
C" (C, " ? Co&&i t 2
"
lo# e!try. (Write og Buffer to og
File .

R
1
(B,+/ 0o lo# e!try.
<
1
(B,-/ (<, 1, B, +/, -/
C
1
(C, 1 Co&&i t 2
1
(Write og Buffer to og File .
Assu&e that a Syste& Crash occurred i&&edi atel y a)ter the <
1
(B,-/ opF
eratio!.
2his &ea!s that the lo# e!try (<, 1, B, +/, -/ has bee! placed i! the lo#
bu))er, but the last poi!t at 'hich the lo# bu))er 'as 'ri tte! out to disk 'as
'i th the lo# e!try (C, "
2his is the )i!al lo# e!try 'e 'ill )i!d 'he! 'e be#i! to recover )ro& the
crash. Assu&e that the values out o! disk are A I "/ (the update to "/
dri)ted out to disk, B I +/ (update did!G t #et to disk, a!d C I 1// (sa&e.
8) you look care)ull y at the seque!ce, 'here 2" co&&i t t ed a!d 21 did!G t,
you 'ill see that the values should be: A I +/, B I +/, C I +/.
A)ter the crash, a co&&e!d is #ive! by the syste& operator that i!itiates
recovery. 2his is usuall y called the R3S2AR2 co&&a!d.
2he process o) recovery takes place i! t'o phases, !oll +ac' a!d !oll
4orward .. 2he Roll Back phase backs out updates by u!co&&i t t ed tra!s acF
tio!s a!d Roll >or'ard reapplies updates o) co&&i t ted tra!sactio!s.
8! Roll Back, the e!tri es i! the disk lo# are read back'ard to the be#i! !i!#,
Syste& Startup, 'he! A I +/, B I +/, a!d C I 1//.
F"*+F
8! Roll Back, the syste& &akes a list o) all tra!sactio!s that did a!d did !ot
co&&i t. 2his is used to decide 'hat #ets backed out a!d reapplied.
LO> EN2/0 /OLL 3%C6J/OLL 7O/?%/D %C21ON PE/7O/MED
1. (C, " 6ut 2
"
i!to OCo&&i t ted 4istO
". (<, ", C,1//,+/ Si!ce 2
"
is o! OCo&&i t ted 4istO, 'e do !othi !#.
*. (S, " 7ake a !ote that 2
"
is !o lo!#er OActi veO
$. (<, 1, A, +/, "/ 2ra!sactio! 2
1
has !ever co&&i t ted (itGs last
operati o! 'as a <rite. 2here)ore, the syste&
per)or&s ;0D1 o) this update by <riti !# the
Be)ore 8&a#e value (+/ i!to data ite& B.
6ut 2
1
i!to O;!co&&i t ted 4istO
+. (S, 1 7ake a !ote that 2
1
is !o lo!#er OActi veO. 0o'
that !o tra!sacti o!s 'ere acti ve, 'e ca! e!d the
R144 BACP phase.
/OLL 7O/?%/D
,. (S, 1 0o actio! requi red
E. (<, 1, A, +/, "/ 2
1
is ;!co&&i t t ed ? 0o actio! required
-. (S, " 0o actio! requi red
.. (<, ", C,1//,+/ Si!ce 2
"
is o! Co&&i tted 4ist, 'e R3D1 this
update by 'ri ti !# A)ter 8&a#e value (+/ i!to
data ite& C
1/ (C, " 0o actio! requi red
11 <e !ote that 'e have rolled )or'ard throu#h all
lo# e!tries a!d ter&i !ate Recovery.
0ote that at this poi!t, A I +/, B I +/, a!d C I +/.
>uarant ees 2at Needed Log Entries are on Disk
%o' could a proble& occur 'i th our &ethod o) 'ri ti !# lo#s a!d recover i!#M
4ook at the history earlier a#ai! a!d thi!k 'hat 'ould happe! i) 'e e!ded
up 'i th B I -/ because the )i!al 'ri tte! value o) B #ot out to disk.
F"*,F
Si!ce 'e have bee! assu&i !# that the lo# (<, 1, B, +/, -/ did 012 #et out
to disk, 'e 'ould!G t be able to ;0D1 this update ('hich should !ot occur,
si!ce 21 did !ot co&&i t.
2his is a proble& that 'e solve 'i th a policy that ties the database bu))er
'ri tes to the 4o#. 2he policy is called ,rite$ Ahead "og 5,A"6 .
8t #uara!tees that !o bu))er dirty pa#e #ets 'ri tte! back to disk be)ore the
4o# that 'ould be able to ;0D1 it #ets 'ri tte! to the disk 4o# )ile.
1P, this 'ould solve the proble& o) ;0D1s. Do 'e ever have a proble&
'i th R3D1sM 0o, because 'e al'ays 'ri te the 4o# bu))er to the 4o# )ile as
part o) the Co&&i t. So 'eG re sa)e i! doi!# R3D1 )or co&&i t t ed 2(s.
2he te(t has a Oproo)O that recovery 'ill 'ork, #ive! these lo# )or&ats, the
R3S2AR2 procedure o) Roll BackNRoll >or'ard, a!d the <A4 policy.
F"*EF
Class "(.
!I. , Ceckpoints
8! the recovery process 'e Hust covered, 'e per)or&ed R144BACP to
System Startup 3ime , 'he! 'e assu&e that all data is valid.
<e assu&e that Syste& Startup occurs i! the &or!i !#, a!d database pro F
cessi!# co!ti !ues duri!# the day a!d everythi !# co&pletes at the e!d o)
day.
(2his is a questio!able assu&pti o! !o'adays, 'ith &a!y co&pa!i es !eed F
i!# to per)or& "$9E processi!#, "$ hours a day, E days a 'eek, 'ith !o
ti &e 'he! tra!sactio!s are #uara!teed to !ot be acti ve.
3ve! i) 'e have a! -Fhour day o) processi!#, ho'ever, 'e ca! ru! i!to real
proble&s recoveri !# a busy tra!sacti o!al syste& (heavy update throu#h F
put 'i th the approach 'eG ve outli!ed so )ar.
2he proble& is that it takes !earl y as &uch processi!# ti &e to R3C153R a
tra!sactio! as it did to ru! it i! the )irst place.
8) our syste& is strai!ed to the li&i t keepi!# up 'ith updates )ro& .:// A7
to +:// 67, a!d the syste& crashes at $:+., it 'ill take !earl y 38G%2 %1;RS
21 R3C153R.
2his is the reaso! )or checkpoi!ti !#. <he! a OCheckpoi!tO is take! at a
#ive! ti &e ($:*/ 67 this &akes it possible )or Recovery to li&i t the lo#s it
!eeds to R144BACP a!d R144>1R<ARD.
A si&ple type o) Checkpoi!t, a OCo&&i t Co!siste!t Checkpoi !t,O &erel y
duplicates the process o) shutti !# do'! )or the !i#ht, but the! tra!sac tio!s
start ri#ht up a#ai!.
2he proble& is that it &i#ht take &i!utes to take a Co&&i t Co!siste!t
Checkpoi !t, a!d duri!# that ti &e 01 03< 2RA0SAC2810S CA0 S2AR2 ;6.
>or this reaso!, database syste&s pro#ra&&ers have devised t'o other
&aHor checkpoi !ti !# sche&es that reduce the OhiccupO i! tra!sactio! pro F
cessi!# that occurs 'hile a checkpoi!t is bei!# per)or&ed.
2he Co&&i t Co!siste!t Checkpoi !t is i&proved o! by usi!# so&ethi !#
called a OCache Co!siste!t Checkpoi !tO. 2he! a! eve! &ore co&plicated
checkpoi !t called a O>uBBy Checkpoi !tO i&proves the situati o! )urther.
So this is 'hat 'e 'ill cover !o', i! order (put o! board:
F"*-F
Commi t Consistent Ceckpoint
Cace Consistent Ceckpoint
7uMMy Ceckpoint
<e de)i!e the Checkpoi!t 6rocess. >ro& ti &e to ti &e, a Checkpoi!t is
tri##ered, probabl y by a ti &e si!ce last checkpoi!t syste& clock eve!t.
Def !I. ,. !. Commi t Consistent Ceckpoint steps . A)ter the Oper F
)or&i !# checkpoi!t stateO is e!tered, 'e have the )ollo'i !# rules.
(1 0o !e' tra!sactio!s ca! start u!til the checkpoi!t is co&plete.
(" Database operati o! processi!# co!ti!ues u!til all e(isti !# tra!sac F
tio!s Co&&i t, a!d all thei r lo# e!tri es are 'ri tte! to disk. (2hus 'e are
Commi t Consistent .
(* 2he! the curre!t lo# bu))er is 'ri tte! out to the lo# )ile, a!d a)ter this
the syste& e!sures that all dirty pa#es i! bu))ers have bee! 'ri t te! out
to disk.
($ <he! steps (1F (* have bee! per)or&ed, the syste& 'ri tes a special lo#
e!try, (CP62, to disk, a!d the Checkpoi !t is co&plete. h
8t should be clear that these steps are basicall y the sa&e o!es that 'ould be
per)or&ed to BR80G 2%3 S:S237 D1<0 )or the eve!i !#.
<e allo' tra!s actio!s i! pro#ress to )i!ish, but do!G t allo' !e' o!es, a!d
everythi !# i! volatil e &e&ory that re)lects a disk state is put out to disk.
As a &atter o) )act, the Disk 4o# >ile ca! !o' be e&pti ed. <e !eeded it
'hile 'e 'ere per)or&i !# the checkpoi !t i! case 'e crashed i! the &id dle,
but !o' 'e do!G t !eed it a!y lo!#er.
<e 'ill !ever !eed the 4o# >ile a#ai! to ;0D1 u!co&&i t t ed tra!sactio!s
that have data o! disk (there are !o such u!co&&i t ted tra!sactio!s or
R3D1 co&&i t t ed tra!sactio!s that are &issi!# updates o! disk (all up dates
have #o!e out to disk already.
>ro& this, it should be clear that 'e ca! &odi)y the Recovery approach 'e
have bee! talki !# about so that i!stead o) a R144BACP to the Be#i!!i !# o)
the 4o# >ile at Syste& Startup, 'e R144BACP to the 4AS2 C%3CP61802QQQ
8) 'e take a Checkpoi!t every )ive &i!utes, 'e 'ill !ever have to recover
&ore tha! )ive &i!utes o) lo##ed updates, so recovery 'ill be )ast.
F"*.F
2he proble& is that the Checkpoi !t 6rocess itsel) &i#ht !ot be very )ast.
0ote that 'e have to allo' all tra!sacti o!s i! pro#ress to co&plete be)ore
'e ca! per)or& successive steps. 8) all applicati o!s 'e have use very short
tra!sactio!s, there should be !o proble&.
Cace Consistent Ceckpoint
But 'hat i) so&e tra!sacti o!s take &ore tha! )ive &i!utes to e(ecuteM
2he! clearl y 'e ca!G t #uara!tee a Checkpoi !t every )ive &i!utesQQ Q
<orse, 'hile the checkpoi !t is #oi!# o! (a!d the last )e' tra!sacti o!s are
'i!di !# up !obody else ca! start a!y S%1R2 tra!sactio!s to read a! acF
cou!t bala!ce or &ake a deposi tQ
<e address this proble& 'i th so&ethi !# called the OCache Co!siste!t
Checkpoi !tO. <ith this sche&e, tra!sacti o!s ca! co!ti !ue acti ve throu#h
the checkpoi !t. <e do!G t have to 'ai t )or the& all to )i!ish a!d co&&i t.
Defini tion !I. ,. ". Cace Consistent Ceckpoint procedure steps .
(1 0o !e' tra!sactio!s are per&i t ted to start.
(" 3(isti!# tra!sactio!s are !ot per&i t ted to start a!y !e' operati o!s.
(* 2he curre!t lo# bu))er is 'ri tte! out to disk, a!d a)ter this the syste&
e!sures that all dirty pa#es i! cache bu))ers have bee! 'ri tte! out to
disk. (2hus, 'e are OCacheO (i.e., Bu))er Co!siste!t o! disk.
($ >i!all y, a special lo# e!try, (CP62, 4ist is 'ri tte! out to disk, a!d the
Checkpoi !t is co&plete. 0123: this (CP62 lo# e!try co!tai !s a list o)
acti ve tra!sactio!s at the ti &e the Checkpoi!t occurs. h
2he recovery procedure usi!# Cache Co!siste!t Checkpoi!ts di))ers )ro&
Co&&i t Co!siste!t Checkpoi !t recovery i! a !u&ber o) 'ays.
E: !I. ,. ! Cache Co!siste!t Checkpoi!t Recovery.
Co!sider the history %+:
%+: R
1
(A, 1/ <
1
(A, 1 C
1
R
"
(A, 1 R
*
(B, " <
"
(A, * R
$
(C, + CP62
<
*
(B, $ C
*
R
$
(B, $ <
$
(C, , C
$
CRAS%
F"$/F
%ere is the series o) lo# e!try eve!ts resul ti !# )ro& this history. 2he last
o!e that #ets out to disk is the (C, * lo# e!try.
(S, 1 (<, 1, A, 1/, 1 (C, 1 (S " (S, * (<, ", A, 1, * (S, $
(CP62, (48S2 I 2
"
, 2
$
(<, *, B, ", $ (C, * (<, $, C, +, , (C, $
At the ti &e 'e take the Cache Co!siste!t Checkpoi !t, 'e 'ill have val ues
out o! disk: A I *, B I " , C I +. (2he dirty pa#e i! cache co!tai !i !# A
at checkpoi !t ti &e is 'ri tte! to disk.
Assu&e that !o other updates &ake it out to disk be)ore the crash, a!d
so the data ite& values re&ai ! the sa&e.
%ere is a dia#ra& o) the ti &e scale o) the various eve!ts. 2ra!sacti o! 2
k
be#i!s 'i th the (S, k lo#, a!d e!ds 'ith (C, k. WW43A53 10 B1ARDWW
Checkpoi!t Crash
21 ]FFFFFFF]
2
"
]FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
2
*
]FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF]
2
$
]FFFFFFFFF]
0e(t, 'e outli!e the actio!s take! i! recovery, starti !# 'ith R144 BACP.
/OLL 3%C6
1. (C, * 0ote 2
*
is a co&&i t t ed 2( i! acti ve list.
". (<, *, B, ", $ Co&&i tted tra!sactio!, 'ai t )or R144 >1R<ARD.
*. (CP62, (48S2 I 2
"
, 2
$
0ote acti ve tra!sactio!s 2
"
a!d 2
$
C
2%3S3 %A53 012 C1778223D (!o (C, " or
(C, $ lo#s have bee! e!cou!tered
$. (S, $ 4ist o) Active tra!sacti o!s !o' shorter: R2
"
, 2
*
S
+. (<, ", A, 1, * 0ot Co&&i t ted. ;0D1: A I 1
,. (S, * 4ist o) Active 2ra!sactio!s shorter.
E. (S, " 4ist o) Active 2ra!sactio!s e&pty. S216 R144BACP.
<ith a Cache Co!siste!t Checkpoi !t, 'he! R144 BACP e!cou!ters the
CP62 lo# e!try the syste& takes !ote o) the tra!sacti o!s that 'ere
acti ve, eve! thou#h 'e have !ever see! a!y operati o!s i! the lo# )ile.
<e !o' take our list o) acti ve tra!sacti o!s, re&ove those that 'e have
see! co&&i t t ed, a!d have a list o) tra!sactio!s 'hose updates 'e !eed
F"$1F
to ;0D1. Si!ce 2ra!sacti o!s ca! live throu#h Checkpoi!ts 'e have to #o
back 6R81R to the Checkpoi!t, 'hile ;0D1 steps &i#ht re&ai !.
<e co!ti !ue i! the R144 BACP phase u!til 'e co&plete all such ;0D1
actio!s. <e ca! be sure 'he! this happe!s because as 'e e! cou!ter (S,
k lo#s, rolli!# back'ard.
<he! all Active ;!co&&i t t ed 2
k
have bee! re&oved, the R144 BACP is
co&plete, eve! thou#h there &ay be &ore e!tries occurri !# earlier i! the
lo# )ile.
/OLL 7O/?%/D
-. (CP62, (48S2 I 2
"
, 2
*
Skip )or'ard i! lo# )ile to this e!try, start
a)ter this.
.. (<, $, C, +, , Roll >or'ard: C I ,.
1-. (C, $ 0o Actio!. 4ast e!try: R144 >1R<ARD is
co&plete.
8! starti !# the Roll >or'ard 6hase, 'e &erel y !eed to R3D1 all updates by
co&&i t ted tra!s actio!s that &i#ht !ot have #o!e out to disk.
<e ca! Hu&p )or'ard to the )irst opera tio! a)ter the Checkpoi!t, si!ce 'e
k!o' that all earlier updates 'ere )lushed )ro& bu))ers.
Roll >or'ard co!ti!ues to the e!d o) the Bu))er >ile. Recall that the val ues
o! disk at the ti &e o) the crash 'ere: A I *, B I " , C I +. At the e!d o)
Recovery, 'e have set A I 1 (Step +, a!d C I , (Step ..
<e still have B I $. A #la!ce at the ti &e scale )i#ure sho's us that 'e 'a!t
updates per)or&ed by 2
$
to be applied, a!d those by 2
"
a!d 2
*
to be backed
out. 2here 'ere !o 'ri tes per)or&ed by 2
$
that #ot out to disk, so 'e have
achieved 'hat is !ecessary )or recovery: A I 1, B I $, C I ,.
7uMMy Ceckpoint .
A proble& ca! still arise that &akes the Cache Co!siste!t Checkpoi !t a
&aHor hiccup i! 2ra!sacti o! 6rocessi!#.
0ote i! the procedure that 'e ca!G t let a!y Active 2ra!sacti o!s co!ti!ue, or
start a!y !e' o!es, u!til all bu))ers are 'ri tte! to disk. <hat i) there are a
412 o) Bu))ersM
F"$"F
So&e !e' &achi !es have several GBytes o) &e&oryQ 2hatG s probabl y
7i!utes o) 8N1, eve! i) 'e have a lot o) disks. D8SP 8N1 is S41<QQQ
1P, 'ith a >uBBy Checkpoi !t, each checkpoi !t, 'he! it co&pletes, &akes
the 6R3581;S checkpoi !t a valid place to stop R144BACP.
Defini tion !I. ,. '. 7uMMy Ceckpoint procedure steps .
(1 6rior to Checkpoi !t start, the re&ai !i !# pa#es that 'ere dirty at the
prior checkpoi!t 'ill be )orced out to disk (but the rate o) 'ri tes should
leave 8N1 capaci ty to support curre!t tra!sactio!s i! pro#ressC there is !o
critical hurry i! doi!# this.
(" 0o !e' tra!sactio!s are per&i t ted to start. 3(isti!# tra!sacti o!s are
!ot per&i t ted to start a!y !e' operati o!s.
(* 2he curre!t lo# bu))er is 'ri tte! out to disk 'i th a! appe!ded lo#
e!try, (CP62
0
, 4ist, as i! the Cache Co!siste!t Checkpoi!t procedure.
($ 2he set o) pa#es i! bu))er that have beco&e dirty si!ce the last
checkpoi !t lo#, CP62
0F1
, is !oted.
2his 'ill probabl y be acco&plished by special )la#s o! the Bu))er
directory. 2here is !o !eed )or this i!)or&ati o! to be &ade disk reside!t,
si!ce it 'ill be used o!ly to per)or& the !e(t checkpoi!t, !ot i! case o)
recovery. At this poi!t the Checkpoi !t is co&plete. h
As e(plai!ed above, the recovery procedure 'i th >uBBy Checkpoi!ts di))ers
)ro& the procedure 'i th Co&&i t Co!siste!t Checkpoi !ts o!l y that R144
>1R<ARD &ust start 'i th the )irst lo# e!try )ollo'i !# the S3C10D to last
checkpoi !t lo#. <e have ho&eo'ork o! this.
F"$*F
Class "*.
Covered last ti &e the various Checkpoi!ts: Co&&i t Co!siste!t, Cache
Co!siste!t, a!d >uBBy. A!y questio!sM
<hat reall y happe!s 'i th co&&ercial databases. ;sed to be all Co&&i t
Co!siste!t, !o' o)te! >uBBy.
Also used to be 53R: physical. B8 a!d A8 &ea!t physical copies o) the e!tire
6AG3. Still !eed to do this so&eti &es, but )or lo!#F ter& lo# ca! be &ore
Olo#ical O.
8!stead o) O%ere is the 'ay the pa#e looked a)ter this updateNbe)ore this
updateO, have: this update 'as ADD 1/ to Colu&! A o) ro' 'ith R8D 1"*$+
'i th versio! !u&ber 11"1.
5ersio! !u&ber is i&porta!t to keep updates ide&pote!t.
0ote that recovery is i!tert 'i !ed 'i th the type o) recovery. 8t does!G t do
a!y #ood to have ro'F level locki!# i) have pa#e level recovery.
21 cha!#es Ro' 1"*$+ colu&! A )ro& 1"* to 1"$, a!d lo# #ives 6AG3 B8
'i th A I 1"*, 21 has!G t co&&i t t ed yet.
2" cha!#es Ro' 1"*$E (sa&e pa#e colu&! B )ro& *"1 to *** a!d
Co&&i ts, lo# #ives Ro' 1"*$+ 'i th 1"$, Ro' 1"*$E colu&! B 'i th *** o!
pa#e A8.
2ra!sactio! 2" co&&i ts, 21 does!G t, the! have crash. <hat do 'e doM
6ut A8 o) 2" i! placeM Gives 'ro!# value to A. 6ut B8 o) 21 i! placeM <ro!#
value o) B.
6a#e level lo##i!# i&plies pa#e level locki!# is all 'e ca! do.
Sybase S@4 Server S2844 does!G t have ro'F level locki!#.
!I. ; Medi a /eco5ery
6roble& is that Disk ca! )ail. (0ot Hust stop ru!!i !#: head ca! score disk
sur)ace. %o' do 'e recoverM
>irst, 'e 'ri te our 4o# to 2<1 disk backups. 2ry to &ake sure t'o disks
have 8!depe!de!t >ailure 7odes (!ot sa&e co!trol ler, sa&e po'er suppl y.
F"$$F
<e say that stora#e that has t'o duplicates is called stable storage , as
co&pared to nonvolati l e storage )or a !or&al disk copy.
Be)ore Syste& Startup ru! BACP;6 (bulk copy o) disksNdatabases.
2he!, 'he! per)or& Recovery )ro& 7edia )ailure, put backup disk i! place
a!d ru! R144 >1R<ARD )ro& that disk, as i) )ro& startup i! the &or!i !#.
As i) !or&al recovery o! this disk e(cept that all pa#es o! this disk 'ere
53R: popular a!d !ever #ot out )ro& disk. Do!G t !eed Roll Back e(cept
co!ceptual l y.
RA8D Disks
So&ethi !# they have !o'adays is RA8D disks. RA8D sta!ds )or Redu!da!t
Arrays o) 8!e(pe!si ve Disks. 8!ve!ted at Berkeley. 2he 8!e(pe!si ve part
rather #ot lost 'he! this idea 'e!t co&&erci al.
2he si&plest ki!d, RA8D 1, &irrors <rites. Could have t'o disks that 'ri te
everythi !# to i! t'o copies. 3very ti &e 'e <rite, !eed " <rites.
So i) o!e disk lost, Hust use other disk. 6ut a!other bla!k disk i! )or &irror,
a!d 'hile operati !# 'ith !or&al Reads a!d <rites do BACP;6 to !e' disk
u!til have &irror.
2his approach saves the ti &e !eeded to do &edia recovery. A!d o) course
'orks )or 1S )iles, 'here there 8S !o &edia recovery.
:ou ca! buy these !o'. As co&ple( syste&s #et &ore a!d &ore disks, 'ill
eve!tual l y !eed RA8D. 2he &ore u!its there are o! a syste&, the &ore
)reque!t the )ailures.
0ote that &irrored <rites are ha!dled by co!troll er. Does!G t 'aste ti &e o)
Syste& to do " <rites.
But 'he! Read, ca! Read 382%3R C16:. ;se disk ar&s i!depe!de!tl y.
So 'e take t'ice as &uch &edia, a!d i) all 'e do is <rites, !eed t'ice as
&a!y disk ar&s to do the sa&e 'ork.
But i) all 'e do is Reads, #et i!depe!de!t disk ar& &ove&e!ts, so #et t'ice
as &a!y Reads too.
But i! order )or t'ice as &a!y Reads to 'ork, !eed 'ar& data, 'here disk
capacil ty is !ot the bottl e!eck, but disk ar& &ove&e!t is.
F"$+F
De)i!i tel y lose the capaci ty i! RA8D 1. But i) 'e 'ere o!l y #oi!# to use hal)
the capaci ty o) the disk because 'e have too 7a!y Reads, RA8D 1 is )i!e.
2here is a! alter!ati ve )or& o) RA8D, RA8D +, that uses less capaci ty tha!
&irrori !#. 2rick is to have , disks, + real copies o) pa#e, o!e checksu&.
;se 91R )or Checksu&. CP I D1 91R D" 91R D* 91R D$ 91R D+.
8) (say D1 disappears, ca! )i#ure out 'hat it 'as:
D1 I CP 91R D" 91R D* 91R D$ 91R D+
(6rove this: A I B 91R C, the! B I A 91R C a!d C I A 91R B.
1 I 1 91R / IT 1 I 1 91R / a!d / I 1 91R 1
1 I / 91R 1 IT / I 1 91R 1 a!d 1 I 1 91R /
/ I 1 91R 1 IT etc.
/ I / 91R /
So i) o!e disk drops out, keep accessi!# data o! it usi!# 91R o) other +.
Recover all disk pa#es o! disk i! sa&e 'ay. 2his takes a lot o) ti &e to
recover, but it D13S save disk &edia.
!I. !I 2PCK % 3encmark
2he 26CFA Be!ch&ark is !o' out o) date. 0e'er 26CFC Be!ch&ark: &ore
co&ple(.
See >i# 1/.1,, p#. ,-,. SiBe o) tables deter&i !ed by 26S.
See >i# 1/.1E. p#. ,-E. All threads do the sa&e thi!#. Ru! i!to each other
i! co!curre!cy co!trol because o) Bra!ch table a!d %istory table.
Be!ch&ark speci)ies ho' &a!y threads there are, ho' o)te! each thread
ru!s a 2(, costs o) ter&i !als, etc.
1! a #ood syste&, Hust add disks u!til use .+[ o) C6;. 1! a bad syste&,
ru! i!to bottl e!ecks.
;lti &ate &easure is 26S a!d YN26S
F"$,F

You might also like