02 Relational Sp 08
Transcript of 02 Relational Sp 08
-
8/18/2019 02 Relational Sp 08
1/48
Entity-Relationship Diagramsand the Relational Model
CS 186, Fall 2007, e!t"re 2
R # $, Chaps% 2#&
' relationship, ( thin), is li)e ashar), yo" )no*+ (t has to!onstantly moe or*ard or it dies%'nd ( thin) *hat *e got on o"rhands is a dead shar)%
-
8/18/2019 02 Relational Sp 08
2/48
Reie*
3 .hy "se a D4MS+ 5S proidesR'M and dis)
-
8/18/2019 02 Relational Sp 08
3/48
Reie*
3 .hy "se a D4MS+ 5S proidesR'M and dis) Con!"rren!y
Re!oery 'stra!tion, Data (ndependen!e
"ery ang"ages
E9!ien!y /or most tas)s Se!"rity
Data (ntegrity
-
8/18/2019 02 Relational Sp 08
4/48
Data Models
3 D4MS models real *orld3 Data Model is lin)
et*een "ser:s ie* o
the *orld and itsstored in !omp"ter
3 Many models e;ist3 .e thin) in terms o%%
Relational Model /!leanand !ommon
Entity-Relationship model/design
-
8/18/2019 02 Relational Sp 08
5/48
.hy St"dy the RelationalModel+
3 Most *idely "sed model%3 =ega!y systems> in older models
e%g%, (4M:s (MS
3 5?e!t-oriented !on!epts merged in =5?e!t-Relational> t*o ariants
3 5?e!t model )no*n to the D4MS
3 5?e!t-Relational Mapping /5RM o"tside the D4MS ' la Rails
3
-
8/18/2019 02 Relational Sp 08
6/48
Steps in Dataase Design
3 ReA"irements 'nalysis "ser needsB *hat m"st dataase do+
3 Con!ept"al Design high leel des!ription /oten done *@ER model
Rails encourages you to work here3 ogi!al Design
translate ER into D4MS data model Rails requires you to work here too
3 S!hema Renement !onsisten!y, normaliation
3 hysi!al Design - inde;es, dis) layo"t3 Se!"rity Design - *ho a!!esses *hat, and ho*
-
8/18/2019 02 Relational Sp 08
7/48
Con!ept"al Design
3 .hat are the entities and relationships in the enterprise+
3 .hat inormation ao"t these entities and relationships
sho"ld *e store in the dataase+
3 .hat integrity constraints or business rules hold+3 ' dataase s!hema: in the ER Model !an e represented
pi!torially /ER diagrams%
3 Can map an ER diagram into a relational s!hema%
-
8/18/2019 02 Relational Sp 08
8/48
ER Model 4asi!s
3 Entity: Real-*orld o?e!t, disting"ishalerom other o?e!ts% 'n entity is des!ried
"sing a set o attributes%3 Entity Set G ' !olle!tion o similar entities%
E%g%, all employees% 'll entities in an entity set hae the same
set o attri"tes% /Hntil *e !onsiderhierar!hies, any*ayI
Ea!h entity set has a key (underlined)% Ea!h attri"te has a domain%
Employees
ssnname
lot
-
8/18/2019 02 Relational Sp 08
9/48
ER Model 4asi!s /Contd%
3 RelationshipG 'sso!iation among t*o or moreentities% E%g%, 'ttishoo *or)s in harma!ydepartment%
relationships !an hae their o*n attri"tes%
3 Relationship Set G Colle!tion o similar relationships% 'n n-ary relationship set R relates n entity sets E !!! En B
ea!h relationship in R inoles entities e ∈ E, %%%, en ∈ En
lot
name
Employees
ssn
Works_In
since dname
budgetdid
Departments
-
8/18/2019 02 Relational Sp 08
10/48
-
8/18/2019 02 Relational Sp 08
11/48
Key Constraints
'n employee!an *or) inmany
departmentsB adept !an haemanyemployees%
1-to-11-to ManyMany-to-
Many
since
Manages
dname
budgetdid
Departments
since
Works_In
lot
name
ssn
Employees
(n !ontrast, ea!hdept has at mostone manager,a!!ording to the
key constraint onMana es%
-
8/18/2019 02 Relational Sp 08
12/48
Lto e !learL
3 Re!all that ea!h relationship hase;a!tly one element o ea!h EntitySet
=1-M> is a !onstraint on theRelationship Set, not ea!hrelationship
3 hin) o 1-M-M ternary relationship
-
8/18/2019 02 Relational Sp 08
13/48
arti!ipation Constraints3 Does eery employee *or) in a department+3 ( so, this is a participation constraint
the parti!ipation o Employees in .or)sN(n is said to e total /s% partial .hat i eery department has an employee *or)ing in it+
3 4asi!ally means =at least one>
lot
name dname
budgetdid
sincename dname
budgetdid
since
Manages
since
DepartmentsEmployees
ssn
Works_In
MeansG =e;a!tly one
-
8/18/2019 02 Relational Sp 08
14/48
.ea) Entities' weak entity !an e identied "niA"ely only y !onsidering the
primary )ey o another /owner entity%
5*ner entity set and *ea) entity set m"st parti!ipate in a one-to-many relationship set /one o*ner, many *ea) entities%
.ea) entity set m"st hae total parti!ipation in this identi"yingrelationship set%
lot
nameagepname
DependentsEmployees
ssn
Policy
cost
.ea) entities hae only a =partial )ey> /dashed "nder
-
8/18/2019 02 Relational Sp 08
15/48
4inary s% ernary Relationships
( ea!h poli!y iso*ned y ?"st 1employeeG 4ad design
ene!iciary
agepname
Dependents
policyid cost
Policies
Purc"aser
name
Employees
ssn lot
4etter design
3 Think throughall the constraintsin the 2nddiagram!
Policies
policyid cost
agepname
Dependents#overs
name
Employees
ssn lot
Key constraint
on Policieswould meanpolicy can onlycover 1dependent!
-
8/18/2019 02 Relational Sp 08
16/48
4inary s% ernary Relationships/Contd%
3 reio"s e;ample ill"strated a !ase *hen t*oinary relationships *ere etter than oneternary relationship%
3 'n e;ample in the other dire!tionG a ternaryrelation Contra!ts relates entity sets arts,
Departments and S"ppliers, and hasdes!riptie attri"te qty % Oo !omination oinary relationships is an adeA"ate s"stit"te%/.ith no ne* entity setsI
-
8/18/2019 02 Relational Sp 08
17/48
4inary s% ernary Relationships/Contd%
S =!an-s"pply> , D =needs> , and D =deals-*ith> S does notimply that D has agreed to "y rom S%
o* do *e re!ord qty +
$uppliers
%ty
Departments#ontr actParts
$uppliers
Departments
deals-&it"
Parts
can-supply
VS.
needs
-
8/18/2019 02 Relational Sp 08
18/48
S"mmary so ar
3 Entities and Entity Set /o;es
3 Relationships and Relationship sets/diamonds
inary
n-ary
3 Key !onstraints /1-1,1-O, M-O, arro*s
3 arti!ipation !onstraints /old or otal3 .ea) entities - reA"ire strong entity or )ey
-
8/18/2019 02 Relational Sp 08
19/48
Relational DataaseGDenitions
3 Relational dataaseG a set o relations%3 RelationG made "p o 2 partsG
S!hema G spe!ies name o relation, pl"s
name and type o ea!h !ol"mn%3 E%g% St"dents/sidG string, nameG string, loginGstring, ageG integer, gpaG real
(nstan!e G a tale, *ith ro*s and !ol"mns%3 Pro*s Q !ardinality
3 Pelds Q degree @ arity
3 Can thin) o a relation as a set o ro*sor t"ples%
i%e%, all ro*s are distin!t
-
8/18/2019 02 Relational Sp 08
20/48
E;G (nstan!e o St"dents Relation
sid name login age gpa
&666 ones ?onesT!s 18 &%U
&688 Smith smithTee!s 18 &%2&60 Smith smithTmath 1 &%8
3 Cardinality Q &, arity Q , all ro*s distin!t3 Do all al"es in ea!h !ol"mn o a relation instan!e
hae to e distin!t+
-
8/18/2019 02 Relational Sp 08
21/48
S - ' lang"age or RelationalD4s
3 S /a%)%a% =SeA"el>, standardlang"age
3 Data Denition ang"age /DD
!reate, modiy, delete relations spe!iy !onstraints administer "sers, se!"rity, et!%
3 Data Manip"lation ang"age /DM Spe!iy A"eries to nd t"ples thatsatisy !riteria
add, modiy, remoe t"ples
-
8/18/2019 02 Relational Sp 08
22/48
S 5erie*
3 CRE'E '4E VnameW / VeldW VdomainW, L
3 (OSER (O5 VnameW /Veld namesW X'HES /Veld al"esW
3 DEEE FR5M VnameW.ERE V!onditionW
3 HD'E VnameWSE Veld nameW Q Val"eW
.ERE V!onditionW
3 SEEC VeldsWFR5M VnameW
.ERE V!onditionW
-
8/18/2019 02 Relational Sp 08
23/48
Creating Relations in S
3Creates the St"dents relation% OoteG the type /domain o ea!h eld is
spe!ied, and enor!ed y the D4MS
*heneer t"ples are added or modied%CREATE TABLE Students
(sid CHAR(20),
name CHAR(20),login CHAR(10),
age INTEGER,
ga !L"AT)
-
8/18/2019 02 Relational Sp 08
24/48
ale Creation /!ontin"ed
3 'nother e;ampleG the Enrolledtale holds inormation ao"t!o"rses st"dents ta)e%
CREATE TABLE En#olled(sid CHAR(20),
$id CHAR(20),g#ade CHAR(2))
-
8/18/2019 02 Relational Sp 08
25/48
-
8/18/2019 02 Relational Sp 08
26/48
Keys
3 Keys are a *ay to asso!iate t"ples indiJerent relations3 Keys are one orm o integrity
!onstraint /(C
sid name login age gpa
&666ones ?onesT!s 18 &%U
&688Smith smithTee!s 18 &%2
&60Smith smithTmath 1 &%8
sid cid grade
53666 Carnatic101 C
53666 Reggae203 B53650 Topology112 A53666 History105 B
Enrolled St"dents
R(M'RY KeyF5RE($O Key
-
8/18/2019 02 Relational Sp 08
27/48
rimary Keys
3 ' set o elds is a s"per)ey iG Oo t*o distin!t t"ples !an hae same al"es in all
)ey elds
3 ' set o elds is a )ey or a relation i G
(t is a s"per)ey Oo s"set o the elds is a s"per)ey
3 *hat i W1 )ey or a relation+ 5ne o the )eys is !hosen /y D4' to e the primary
)ey% 5ther )eys are !alled !andidate )eys%
3 E%g% sid is a )ey or St"dents%
.hat ao"t name+
he set Zsid, gpa[ is a s"per)ey%
i d C did t K i
-
8/18/2019 02 Relational Sp 08
28/48
rimary and Candidate Keys inS
3 ossily many candidate keys /spe!ied "sing
HO(HE, one o *hi!h is !hosen as the primary key %
3 Keys m"st e "sed !are"llyI
3 =For a gien st"dent and !o"rse, there is a single
grade%>
=St"dents !an ta)e only one !o"rse, and no t*ost"dents in a !o"rse re!eie the same grade%>
CREATE TABLE En#olled (sid CHAR(20) $id CHAR(20), g#ade CHAR(2), 4RIAR5 6E5 (sid,$id))
CREATE TABLE En#olled (sid CHAR(20) $id CHAR(20), g#ade CHAR(2),
4RIAR5 6E5 (sid), &NI7&E ($id, g#ade))
s%
F i K R ti l
-
8/18/2019 02 Relational Sp 08
29/48
Foreign Keys, Reerential(ntegrity
3 #oreign key G Set o elds in onerelation that is "sed to reer: to at"ple in another relation% M"st !orrespond to the primary )ey o the
other relation% i)e a logi!al pointer:%
3( all oreign )ey !onstraints areenor!ed, re"erential integrity isa!hieed /i%e%, no dangling reeren!es%
-
8/18/2019 02 Relational Sp 08
30/48
Foreign Keys in S
3 '.g. nly students listed in the "tudents relation should
%e allowed to enroll for courses. sid is a oreign )ey reerring to St"dentsG
CREATE TABLE En#olled(sid CHAR(20),$id CHAR(20),g#ade CHAR(2),
4RIAR5 6E5 (sid,$id), !"REIGN 6E5 (sid) RE!ERENCES Students )
sid cid grade
53666 Carnatic101 C
53666 Reggae203 B
53650 Topology112 A
53666 History105 B
Enrolled
sid name login age gpa&666ones ?onesT!s 18 &%U
&688Smith smithTee!s 18 &%2
&60Smith smithTmath 1 &%8
St"dents
11111 English102
-
8/18/2019 02 Relational Sp 08
31/48
Enor!ing Reerential (ntegrity
3 Consider St"dents and EnrolledB sid in Enrolled is aoreign )ey that reeren!es St"dents%
3 .hat sho"ld e done i an Enrolled t"ple *ith anon-e;istent st"dent id is inserted+ /Re$ect it%
3 .hat sho"ld e done i a St"dents t"ple is deleted+ 'lso delete all Enrolled t"ples that reer to it+ Disallo* deletion o a St"dents t"ple that is reerred to+ Set sid in Enrolled t"ples that reer to it to a de"ault sid+
/(n S, alsoG Set sid in Enrolled t"ples that reer to it to aspe!ial al"e null& denoting 'unknown or 'inapplicable %
3 Similar iss"es arise i primary )ey o St"dents t"pleis "pdated%
-
8/18/2019 02 Relational Sp 08
32/48
(ntegrity Constraints /(Cs
3 (CG !ondition that m"st e tr"e or any instan!eo the dataaseB e%g%, domain constraints! (Cs are spe!ied *hen s!hema is dened% (Cs are !he!)ed *hen relations are modied%
3 ' legal instan!e o a relation is one that
satises all spe!ied (Cs% D4MS sho"ld not allo* illegal instan!es%
3 ( the D4MS !he!)s (Cs, stored data is moreaith"l to real-*orld meaning%
'oids data entry errors, tooI
-
8/18/2019 02 Relational Sp 08
33/48
.here do (Cs Come From+
3 (Cs are ased "pon the semanti!s o the real-*orld that is eing des!ried in the dataaserelations%
3 .e !an !he!) a dataase instan!e to see i an
(C is iolated, "t *e !an OEXER iner that an(C is tr"e y loo)ing at an instan!e% 'n (C is a statement ao"t all possile instan!esI
From e;ample, *e )no* name is not a )ey, "t theassertion that sid is a )ey is gien to "s%
3 Key and oreign )ey (Cs are the most !ommonBmore general (Cs s"pported too%
3 (n the real *orld, sometimes the !onstraintsho"ld hold "t doesn:t --W data !leaningI
-
8/18/2019 02 Relational Sp 08
34/48
Relational "ery ang"ages
3 ' ma?or strength o the relational modelGs"pports simple, po*er"l querying o data%
3 "eries !an e *ritten int"itiely, and theD4MS is responsile or e9!ient eal"ation% he )eyG pre!ise semanti!s or relational A"eries%
'llo*s the optimier to e;tensiely re-orderoperations, and still ens"re that the ans*er doesnot !hange%
-
8/18/2019 02 Relational Sp 08
35/48
he S "ery ang"age
3 he most *idely "sed relational A"erylang"age% C"rrent std is SG200&B S2 is a asi!
s"set
3 o nd all 18 year old st"dents, *e !an*riteGSELECT 8 !R" Students S HERE S/age31+
3 o nd ?"st names and logins, repla!e the rst lineG
SELECT S/name, S/login
sid name age gpa
&666ones 18 &%U&688
Smith
18
&%2
&60Smith
login
?onesT!ssmithTee
smithTmath1 &%8
-
8/18/2019 02 Relational Sp 08
36/48
"erying M"ltiple Relations
3 .hat does the ollo*ing A"ery!omp"te+SELECT S/name, E/$id
!R" Students S, En#olled E HERE S/sid3E/sid AN E/g#ade39A9
sid cid grade
53831 Carnatic101 C
53831 Reggae203 B
53650 Topology112 A53666 History105 B
$ien the ollo*inginstan!e o Enrolled
S.name .cid
Smit! Topology112*e getG
-
8/18/2019 02 Relational Sp 08
37/48
Semanti!s o a "ery
3 ' conceptual ealuation method or the preio"sA"eryG
1% do FR5M !la"seG !omp"te cross-product o St"dentsand Enrolled
2% do .ERE !la"seG Che!) !onditions, dis!ard t"ples thatail
&% do SEEC !la"seG Delete "n*anted elds3 Rememer, this is conceptual% '!t"al eal"ation
*ill e much more e9!ient, "t m"st prod"!e thesame ans*ers%
-
8/18/2019 02 Relational Sp 08
38/48
-
8/18/2019 02 Relational Sp 08
39/48
Relational ModelG S"mmary
3 ' ta"lar representation o data%3 Simple and int"itie, !"rrently the most *idely
"sed 5?e!t-relational s"pport in most prod"!ts
-
8/18/2019 02 Relational Sp 08
40/48
$5SH4
-
8/18/2019 02 Relational Sp 08
41/48
Dataases or rogrammers
3 rogrammers thin) ao"t o?e!ts/str"!ts Oested and interleaed
3 5ten *ant to =persist> these things3 5ptions
en!ode opaA"ely and store
translate to a str"!t"red orm3 relational D4,
-
8/18/2019 02 Relational Sp 08
42/48
\YHCKII
3 o* do ( =relationalie> my o?e!ts+3 ae to *rite a !onerter or ea!h
!lass+
3 hin) ao"t *hen to sae thingsinto the D4+
3 $ood ne*sG Can all e a"tomated
.ith arying amo"nts o tro"le
-
8/18/2019 02 Relational Sp 08
43/48
5?e!t-Relational Mappings
3 Ro"ghlyG Class ] Entity Set
(nstan!e ] Entity
Data memer ] 'ttri"te Reeren!e ] Foreign Key
-
8/18/2019 02 Relational Sp 08
44/48
Details, details
3 .e hae to map this do*n to tales3 .hi!h tale holds *hi!h !lass o
o?e!t+
3 .hat ao"t relationships+3 Sol"tion P1G De!laratie Cong"ration
.rite a des!ription le /oten in
-
8/18/2019 02 Relational Sp 08
45/48
R"y on Rails
3 R"yG an 55 s!ripting lang"age and a pretty ni!e one, too
3 RailsG a rame*or) or *e apps
=!onention oer !ong"ration>3 great or standard *e-app st"JI
allo*s oerriding as needed
3 Xery ER-li)e
-
8/18/2019 02 Relational Sp 08
46/48
Rails and ER
3 Models Employees
Departments
lot
name
Employees
ssn
Works_In
sincedname
budgetdid
Departments
-
8/18/2019 02 Relational Sp 08
47/48
Some Rails =Models>
app/models/state.rb$lass State : A$ti;eRe$o#d : A$ti;eRe$o#d
-
8/18/2019 02 Relational Sp 08
48/48
F"rther Reading
3 Chapter 18 /thro"gh 18%& in *gile+eb Deelopment with Rails