boyce-codd normal form · boyce-codd normal form ! we say a relation r is in bcnf if whenever x →...
TRANSCRIPT
![Page 1: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/1.jpg)
1
Boyce-Codd Normal Form
§ We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey § Remember: nontrivial means Y is not
contained in X § Remember, a superkey is any superset of
a key (not necessarily a proper superset)
![Page 2: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/2.jpg)
2
Example
Drinkers(name, addr, beersLiked, manf, favBeer) FD’s: name → addr favBeer, beersLiked → manf
§ Only key is {name, beersLiked} § In each FD, the left side is not a
superkey § Any one of these FD’s shows Drinkers
is not in BCNF
![Page 3: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/3.jpg)
3
Another Example
Beers(name, manf, manfAddr) FD’s: name → manf, manf → manfAddr § Only key is {name} § Name → manf does not violate BCNF, but
manf → manfAddr does
![Page 4: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/4.jpg)
4
Decomposition into BCNF
§ Given: relation R with FD’s F § Look among the given FD’s for a BCNF
violation X → Y § If any FD following from F violates BCNF,
then there will surely be an FD in F itself that violates BCNF
§ Compute X + § Not all attributes, or else X is a superkey
![Page 5: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/5.jpg)
5
Decompose R Using X → Y
§ Replace R by relations with schemas: 1. R1 = X + 2. R2 = R – (X + – X )
§ Project given FD’s F onto the two new relations
![Page 6: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/6.jpg)
6
Decomposition Picture
R-X + X X +-X
R2
R1
R
![Page 7: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/7.jpg)
7
Example: BCNF Decomposition
Drinkers(name, addr, beersLiked, manf, favBeer) F = name → addr, name → favBeers
beersLiked → manf § Pick BCNF violation name → addr § Close the left side:
{name}+ = {name, addr, favBeer} § Decomposed relations:
1. Drinkers1(name, addr, favBeer) 2. Drinkers2(name, beersLiked, manf)
![Page 8: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/8.jpg)
8
Example: BCNF Decomposition
§ We are not done; we need to check Drinkers1 and Drinkers2 for BCNF
§ Projecting FD’s is easy here § For Drinkers1(name, addr, favBeer),
relevant FD’s are name → addr and name → favBeer § Thus, {name} is the only key and Drinkers1
is in BCNF
![Page 9: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/9.jpg)
9
Example: BCNF Decomposition
§ For Drinkers2(name, beersLiked, manf), the only FD is beersLiked → manf, and the only key is {name, beersLiked} § Violation of BCNF
§ beersLiked+ = {beersLiked, manf}, so we decompose Drinkers2 into:
1. Drinkers3(beersLiked, manf) 2. Drinkers4(name, beersLiked)
![Page 10: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/10.jpg)
10
Example: BCNF Decomposition § The resulting decomposition of Drinkers:
1. Drinkers1(name, addr, favBeer) 2. Drinkers3(beersLiked, manf) 3. Drinkers4(name, beersLiked) § Notice: Drinkers1 tells us about drinkers,
Drinkers3 tells us about beers, and Drinkers4 tells us the relationship between drinkers and the beers they like
§ Compare with running example: 1. Drinkers(name, addr, phone) 2. Beers(name, manf) 3. Likes(drinker,beer)
![Page 11: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/11.jpg)
11
Third Normal Form – Motivation
§ There is one structure of FD’s that causes trouble when we decompose
§ AB → C and C → B § Example:
A = street address, B = city, C = post code
§ There are two keys, {A,B } and {A,C } § C → B is a BCNF violation, so we must
decompose into AC, BC
![Page 12: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/12.jpg)
12
We Cannot Enforce FD’s
§ The problem is that if we use AC and BC as our database schema, we cannot enforce the FD AB → C by checking FD’s in these decomposed relations
§ Example with A = street, B = city, and C = post code on the next slide
![Page 13: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/13.jpg)
13
An Unenforceable FD
street post Campusvej 5230 Vestergade 5000
city post Odense 5230 Odense 5000
Join tuples with equal post codes
street city post Campusvej Odense 5230 Vestergade Odense 5000
No FD’s were violated in the decomposed relations and FD street city → post holds for the database as a whole
![Page 14: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/14.jpg)
14
An Unenforceable FD
street post Hjallesevej 5230 Hjallesevej 5000
city post Odense 5230 Odense 5000
Join tuples with equal post codes
street city post Hjallesevej Odense 5230 Hjallesevej Odense 5000
Although no FD’s were violated in the decomposed relations, FD street city → post is violated by the database as a whole
![Page 15: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/15.jpg)
15
Another Unenforcable FD
§ Departures(time, track, train) § time track → train and train → track § Two keys, {time,track} and {time,train} § train → track is a BCNF violation, so we
must decompose into Departures1(time, train) Departures2(track,train)
![Page 16: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/16.jpg)
16
Another Unenforceable FD
time train 19:08 ICL54 19:16 IC852
track train 4 ICL54 3 IC852
Join tuples with equal train code
time track train 19:08 4 ICL54 19:16 3 IC852
No FD’s were violated in the decomposed relations, FD time track → train holds for the database as a whole
![Page 17: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/17.jpg)
17
Another Unenforceable FD
time train 19:08 ICL54 19:08 IC 42
track train 4 ICL54 4 IC 42
Join tuples with equal train code
time track train 19:08 4 ICL54 19:08 4 IC 42
Although no FD’s were violated in the decomposed relations, FD time track → train is violated by the database as a whole
![Page 18: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/18.jpg)
18
3NF Let’s Us Avoid This Problem
§ 3rd Normal Form (3NF) modifies the BCNF condition so we do not have to decompose in this problem situation
§ An attribute is prime if it is a member of any key
§ X → A violates 3NF if and only if X is not a superkey, and also A is not prime
![Page 19: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/19.jpg)
19
Example: 3NF
§ In our problem situation with FD’s AB → C and C → B, we have keys AB and AC
§ Thus A, B, and C are each prime § Although C → B violates BCNF, it does
not violate 3NF
![Page 20: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/20.jpg)
20
What 3NF and BCNF Give You
§ There are two important properties of a decomposition:
1. Lossless Join: it should be possible to project the original relations onto the decomposed schema, and then reconstruct the original
2. Dependency Preservation: it should be possible to check in the projected relations whether all the given FD’s are satisfied
![Page 21: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/21.jpg)
21
3NF and BCNF – Continued
§ We can get (1) with a BCNF decomposition § We can get both (1) and (2) with a 3NF
decomposition § But we can’t always get (1) and (2) with a
BCNF decomposition § street-city-post is an example § time-track-train is another example
![Page 22: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/22.jpg)
22
Testing for a Lossless Join
§ If we project R onto R1, R2,…, Rk , can we recover R by rejoining?
§ Any tuple in R can be recovered from its projected fragments
§ So the only question is: when we rejoin, do we ever get back something we didn’t have originally?
![Page 23: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/23.jpg)
23
The Chase Test
§ Suppose tuple t comes back in the join § Then t is the join of projections of
some tuples of R, one for each Ri of the decomposition
§ Can we use the given FD’s to show that one of these tuples must be t ?
![Page 24: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/24.jpg)
24
The Chase – (2)
§ Start by assuming t = abc… . § For each i, there is a tuple si of R that
has a, b, c,… in the attributes of Ri § si can have any values in other
attributes § We’ll use the same letter as in t, but
with a subscript, for these components
![Page 25: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/25.jpg)
25
Example: The Chase
§ Let R = ABCD, and the decomposition be AB, BC, and CD
§ Let the given FD’s be C → D and B → A
§ Suppose the tuple t = abcd is the join of tuples projected onto AB, BC, CD
![Page 26: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/26.jpg)
A B C D a b c1 d1
a2 b c d2
a3 b3 c d d
Use C → D
a
Use B → A
26
The Tableau
We’ve proved the second tuple must be t
The tuples of R pro- jected onto AB, BC, CD
![Page 27: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/27.jpg)
27
Summary of the Chase
1. If two rows agree in the left side of a FD, make their right sides agree too
2. Always replace a subscripted symbol by the corresponding unsubscripted one, if possible
3. If we ever get an unsubscripted row, we know any tuple in the project-join is in the original (the join is lossless)
4. Otherwise, the final tableau is a counterexample
![Page 28: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/28.jpg)
28
Example: Lossy Join
§ Same relation R = ABCD and same decomposition.
§ But with only the FD C → D
![Page 29: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/29.jpg)
29
The Tableau
A B C D a b c1 d1
a2 b c d2
a3 b3 c d d
Use C → D These three tuples are an example R that shows the join lossy abcd is not in R, but we can project and rejoin to get abcd
These projections rejoin to form abcd
![Page 30: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/30.jpg)
30
3NF Synthesis Algorithm
§ We can always construct a decomposition into 3NF relations with a lossless join and dependency preservation
§ Need minimal basis for the FD’s: 1. Right sides are single attributes 2. No FD can be removed 3. No attribute can be removed from a left side
![Page 31: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/31.jpg)
31
Constructing a Minimal Basis
1. Split right sides 2. Repeatedly try to remove an FD and
see if the remaining FD’s are equivalent to the original
3. Repeatedly try to remove an attribute from a left side and see if the resulting FD’s are equivalent to the original
![Page 32: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/32.jpg)
32
3NF Synthesis – (2)
§ One relation for each FD in the minimal basis § Schema is the union of the left and right
sides
§ If no key is contained in an FD, then add one relation whose schema is some key
![Page 33: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/33.jpg)
33
Example: 3NF Synthesis
§ Relation R = ABCD § FD’s A → B and A → C § Decomposition: AB and AC from the
FD’s, plus AD for a key
![Page 34: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/34.jpg)
34
Why It Works
§ Preserves dependencies: each FD from a minimal basis is contained in a relation, thus preserved
§ Lossless Join: use the chase to show that the row for the relation that contains a key can be made all-unsubscripted variables
§ 3NF: hard part – a property of minimal bases
![Page 35: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/35.jpg)
Summary 5
More things you should know: § Functional Dependency § Key, Superkey § Update Anomaly, Deletion Anomaly § BCNF, Closure, Decomposition § Chase Algorithm § 3rd Normal Form
35
![Page 36: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/36.jpg)
36
Entity-Relationship Model
![Page 37: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/37.jpg)
37
Purpose of E/R Model
§ The E/R model allows us to sketch database schema designs § Includes some constraints, but not
operations
§ Designs are pictures called entity-relationship diagrams
§ Later: convert E/R designs to relational DB designs
![Page 38: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/38.jpg)
38
Framework for E/R
§ Design is a serious business § The “boss” knows they want a
database, but they don’t know what they want in it
§ Sketching the key components is an efficient way to develop a working database
![Page 39: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/39.jpg)
39
Entity Sets
§ Entity = “thing” or object § Entity set = collection of similar entities
§ Similar to a class in object-oriented languages
§ Attribute = property of (the entities of) an entity set § Attributes are simple values, e.g. integers or
character strings, not structs, sets, etc.
![Page 40: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/40.jpg)
40
E/R Diagrams
§ In an entity-relationship diagram: § Entity set = rectangle § Attribute = oval, with a line to the
rectangle representing its entity set
![Page 41: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/41.jpg)
41
Example:
§ Entity set Beers has two attributes, name and
manf (manufacturer) § Each Beers entity has values for these two
attributes, e.g. (Odense Classic, Albani)
Beers
name manf
![Page 42: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/42.jpg)
42
Relationships
§ A relationship connects two or more entity sets
§ It is represented by a diamond, with lines to each of the entity sets involved
![Page 43: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/43.jpg)
43
Example: Relationships
Drinkers addr name
Beers
manf name
Bars
name
license
addr
Note: license = beer, full, none
Sells Bars sell some beers
Likes Drinkers like some beers Frequents
Drinkers frequent some bars
![Page 44: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/44.jpg)
44
Relationship Set
§ The current “value” of an entity set is the set of entities that belong to it § Example: the set of all bars in our
database
§ The “value” of a relationship is a relationship set, a set of tuples with one component for each related entity set
![Page 45: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/45.jpg)
45
Example: Relationship Set
§ For the relationship Sells, we might have a relationship set like:
Bar Beer C.Ch. Od.Cl. C.Ch. Erd.Wei. C.Bio. Od.Cl. Brygg. Pilsener C4 Erd.Wei.
![Page 46: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/46.jpg)
46
Multiway Relationships
§ Sometimes, we need a relationship that connects more than two entity sets
§ Suppose that drinkers will only drink certain beers at certain bars § Our three binary relationships Likes, Sells,
and Frequents do not allow us to make this distinction
§ But a 3-way relationship would
![Page 47: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/47.jpg)
47
Example: 3-Way Relationship
Bars Beers
Drinkers
name name addr manf
name addr
license
Preferences
![Page 48: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/48.jpg)
48
A Typical Relationship Set
Bar Drinker Beer C.Ch. Peter Erd.Wei. C.Ch. Lars Od.Cl. C.Bio. Peter Od.Cl. Brygg. Peter Pilsener C4 Peter Erd.Wei. C.Bio. Lars Tuborg Brygg. Lars Ale
![Page 49: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/49.jpg)
49
Many-Many Relationships
§ Focus: binary relationships, such as Sells between Bars and Beers
§ In a many-many relationship, an entity of either set can be connected to many entities of the other set § E.g., a bar sells many beers; a beer is sold
by many bars
![Page 50: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/50.jpg)
50
In Pictures:
many-many
![Page 51: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/51.jpg)
51
Many-One Relationships
§ Some binary relationships are many -one from one entity set to another
§ Each entity of the first set is connected to at most one entity of the second set
§ But an entity of the second set can be connected to zero, one, or many entities of the first set
![Page 52: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/52.jpg)
52
In Pictures:
many-one
![Page 53: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/53.jpg)
53
Example: Many-One Relationship § Favorite, from Drinkers to Beers is
many-one § A drinker has at most one favorite beer § But a beer can be the favorite of any
number of drinkers, including zero
![Page 54: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/54.jpg)
54
One-One Relationships
§ In a one-one relationship, each entity of either entity set is related to at most one entity of the other set
§ Example: Relationship Best-seller between entity sets Manfs (manufacturer) and Beers § A beer cannot be made by more than one
manufacturer, and no manufacturer can have more than one best-seller (assume no ties)
![Page 55: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/55.jpg)
55
In Pictures:
one-one
![Page 56: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/56.jpg)
56
Representing “Multiplicity”
§ Show a many-one relationship by an arrow entering the “one” side § Remember: Like a functional dependency
§ Show a one-one relationship by arrows entering both entity sets
§ Rounded arrow = “exactly one,” i.e., each entity of the first set is related to exactly one entity of the target set
![Page 57: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/57.jpg)
57
Example: Many-One Relationship
Drinkers Beers Likes
Favorite Notice: two relationships connect the same entity sets, but are different
![Page 58: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/58.jpg)
58
Example: One-One Relationship § Consider Best-seller between Manfs
and Beers § Some beers are not the best-seller of
any manufacturer, so a rounded arrow to Manfs would be inappropriate.
§ But a beer manufacturer has to have a best-seller
![Page 59: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/59.jpg)
59
In the E/R Diagram
Manfs Beers Best- seller
A manufacturer has exactly one best seller
A beer is the best- seller for 0 or 1 manufacturer(s)
![Page 60: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/60.jpg)
60
Attributes on Relationships
§ Sometimes it is useful to attach an attribute to a relationship
§ Think of this attribute as a property of tuples in the relationship set
![Page 61: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/61.jpg)
61
Example: Attribute on Relationship
Bars Beers Sells
price
Price is a function of both the bar and the beer, not of one alone
![Page 62: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/62.jpg)
62
Equivalent Diagrams Without Attributes on Relationships
§ Create an entity set representing values of the attribute
§ Make that entity set participate in the relationship
![Page 63: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/63.jpg)
63
Example: Removing an Attribute from a Relationship
Bars Beers Sells
price
Prices Note convention: arrow from multiway relationship = “all other entity sets together determine a unique one of these”
![Page 64: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/64.jpg)
64
Roles
§ Sometimes an entity set appears more than once in a relationship
§ Label the edges between the relationship and the entity set with names called roles
![Page 65: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/65.jpg)
65
Example: Roles
Drinkers
Married
husband wife
Relationship Set Husband Wife Lars Lene Kim Joan … …
![Page 66: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/66.jpg)
66
Example: Roles
Drinkers
Buddies
1 2
Relationship Set Buddy1 Buddy2 Peter Lars Peter Pepe Pepe Bea Bea Rafa … …
![Page 67: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/67.jpg)
67
Subclasses
§ Subclass = special case = fewer entities = more properties
§ Example: Ales are a kind of beer § Not every beer is an ale, but some are § Let us suppose that in addition to all the
properties (attributes and relationships) of beers, ales also have the attribute color
![Page 68: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/68.jpg)
68
Subclasses in E/R Diagrams
§ Assume subclasses form a tree § I.e., no multiple inheritance
§ Isa triangles indicate the subclass relationship § Point to the superclass
![Page 69: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/69.jpg)
69
Example: Subclasses
Beers
Ales
isa
name manf
color
![Page 70: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/70.jpg)
70
E/R Vs. Object-Oriented Subclasses
§ In OO, objects are in one class only § Subclasses inherit from superclasses.
§ In contrast, E/R entities have representatives in all subclasses to which they belong § Rule: if entity e is represented in a subclass,
then e is represented in the superclass (and recursively up the tree)
![Page 71: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/71.jpg)
71
Example: Representatives of Entities
Beers
Ales
isa
name manf
color
Pete’s Ale
![Page 72: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/72.jpg)
72
Keys
§ A key is a set of attributes for one entity set such that no two entities in this set agree on all the attributes of the key § It is allowed for two entities to agree on
some, but not all, of the key attributes
§ We must designate a key for every entity set
![Page 73: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/73.jpg)
73
Keys in E/R Diagrams
§ Underline the key attribute(s) § In an Isa hierarchy, only the root entity
set has a key, and it must serve as the key for all entities in the hierarchy
![Page 74: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/74.jpg)
74
Example: name is Key for Beers
Beers
Ales
isa
name manf
color
![Page 75: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/75.jpg)
75
Example: a Multi-attribute Key
Courses
dept number hours room
• Note that hours and room could also serve as a key, but we must select only one key
![Page 76: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/76.jpg)
76
Weak Entity Sets
§ Occasionally, entities of an entity set need “help” to identify them uniquely
§ Entity set E is said to be weak if in order to identify entities of E uniquely, we need to follow one or more many-one relationships from E and include the key of the related entities from the connected entity sets
![Page 77: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/77.jpg)
77
Example: Weak Entity Set
§ name is almost a key for football players, but there might be two with the same name
§ number is certainly not a key, since players on two teams could have the same number.
§ But number, together with the team name related to the player by Plays-on should be unique
![Page 78: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/78.jpg)
78
In E/R Diagrams
Players Teams Plays- on
name name number
• Double diamond for supporting many-one relationship • Double rectangle for the weak entity set
Note: must be rounded because each player needs a team to help with the key
![Page 79: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/79.jpg)
79
Weak Entity-Set Rules
§ A weak entity set has one or more many-one relationships to other (supporting) entity sets § Not every many-one relationship from a
weak entity set need be supporting § But supporting relationships must have a
rounded arrow (entity at the “one” end is guaranteed)
![Page 80: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/80.jpg)
80
Weak Entity-Set Rules – (2)
§ The key for a weak entity set is its own underlined attributes and the keys for the supporting entity sets § E.g., (player) number and (team) name is
a key for Players in the previous example
![Page 81: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/81.jpg)
81
Design Techniques
1. Avoid redundancy 2. Limit the use of weak entity sets 3. Don’t use an entity set when an
attribute will do
![Page 82: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/82.jpg)
82
Avoiding Redundancy
§ Redundancy = saying the same thing in two (or more) different ways
§ Wastes space and (more importantly) encourages inconsistency § Two representations of the same fact
become inconsistent if we change one and forget to change the other
§ Recall anomalies due to FD’s
![Page 83: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/83.jpg)
83
Example: Good
Beers Manfs ManfBy
name
This design gives the address of each manufacturer exactly once
name addr
![Page 84: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/84.jpg)
84
Example: Bad
Beers Manfs ManfBy
name
This design states the manufacturer of a beer twice: as an attribute and as a related entity.
name
manf
addr
![Page 85: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/85.jpg)
85
Example: Bad
Beers
name
This design repeats the manufacturer’s address once for each beer and loses the address if there are temporarily no beers for a manufacturer
manf manfAddr
![Page 86: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/86.jpg)
86
Entity Sets Versus Attributes
§ An entity set should satisfy at least one of the following conditions: § It is more than the name of something; it
has at least one nonkey attribute or
§ It is the “many” in a many-one or many-many relationship
![Page 87: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/87.jpg)
87
Example: Good
Beers Manfs ManfBy
name
• Manfs deserves to be an entity set because of the nonkey attribute addr • Beers deserves to be an entity set because it is the “many” of the many-one relationship ManfBy
name addr
![Page 88: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/88.jpg)
88
Example: Good
Beers
name
There is no need to make the manufacturer an entity set, because we record nothing about manufacturers besides their name
manf
![Page 89: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/89.jpg)
89
Example: Bad
Beers Manfs ManfBy
name
Since the manufacturer is nothing but a name, and is not at the “many” end of any relationship, it should not be an entity set
name
![Page 90: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/90.jpg)
90
Don’t Overuse Weak Entity Sets
§ Beginning database designers often doubt that anything could be a key by itself § They make all entity sets weak, supported by all
other entity sets to which they are linked
§ In reality, we usually create unique ID’s for entity sets § Examples include CPR numbers, car’s license
plates, etc.
![Page 91: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/91.jpg)
91
When Do We Need Weak Entity Sets?
§ The usual reason is that there is no global authority capable of creating unique ID’s
§ Example: it is unlikely that there could be an agreement to assign unique player numbers across all football teams in the world
![Page 92: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/92.jpg)
92
From E/R Diagrams to Relations § Entity set → relation
§ Attributes → attributes
§ Relationships → relations whose attributes are only: § The keys of the connected entity sets § Attributes of the relationship itself
![Page 93: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/93.jpg)
93
Entity Set → Relation
Relation: Beers(name, manf)
Beers
name manf
![Page 94: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/94.jpg)
94
Relationship → Relation
Drinkers Beers Likes
Likes(drinker, beer) Favorite
Favorite(drinker, beer)
Married
husband
wife
Married(husband, wife)
name addr name manf
Buddies
1 2
Buddies(name1, name2)
![Page 95: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/95.jpg)
95
Combining Relations
§ OK to combine into one relation: 1. The relation for an entity-set E 2. The relations for many-one relationships
of which E is the “many”
§ Example: Drinkers(name, addr) and Favorite(drinker, beer) combine to make Drinker1(name, addr, favBeer)
![Page 96: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/96.jpg)
Redundancy
96
Risk with Many-Many Relationships § Combining Drinkers with Likes would be
a mistake. It leads to redundancy, as:
name addr beer Peter Campusvej Od.Cl. Peter Campusvej Erd.W.
![Page 97: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/97.jpg)
97
Handling Weak Entity Sets
§ Relation for a weak entity set must include attributes for its complete key (including those belonging to other entity sets), as well as its own, nonkey attributes
§ A supporting relationship is redundant and yields no relation (unless it has attributes)
![Page 98: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/98.jpg)
98
Example: Weak Entity Set → Relation
Logins Hosts At
name name
Hosts(hostName, location) Logins(loginName, hostName, expiry) At(loginName, hostName, hostName2)
Must be the same
expiry
At becomes part of Logins
location
![Page 99: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/99.jpg)
99
Subclasses: Three Approaches
1. Object-oriented : One relation per subset of subclasses, with all relevant attributes
2. Use nulls : One relation; entities have NULL in attributes that don’t belong to them
3. E/R style : One relation for each subclass: § Key attribute(s) § Attributes of that subclass
![Page 100: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/100.jpg)
100
Example: Subclass → Relations
Beers
Ales
isa
name manf
color
![Page 101: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/101.jpg)
101
Object-Oriented
name manf Odense Classic Albani
Beers name manf color HC Andersen Albani red
Ales
Good for queries like “find the color of ales made by Albani”
![Page 102: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/102.jpg)
102
E/R Style name manf Odense Classic Albani HC Andersen Albani
Beers name color HC Andersen red
Ales
Good for queries like “find all beers (including ales) made by Albani”
![Page 103: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/103.jpg)
103
Using Nulls
name manf color Odense Classic Albani NULL HC Andersen Albani red
Beers
Saves space unless there are lots of attributes that are usually NULL
![Page 104: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/104.jpg)
Summary 6
More things you should know: § Entities, Attributes, Entity Sets, § Relationships, Multiplicity, Keys § Roles, Subclasses, Weak Entity Sets § Design guidelines § E/R diagrams → relational model
104
![Page 105: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/105.jpg)
The Project
105
![Page 106: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/106.jpg)
Purpose of the Project
§ To try in practice the process of designing and creating a relational database application
§ This process includes: § development of an E/R model § transfer to the relational model § normalization of relations § implementation in a DBMS § programming of an application
106
![Page 107: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/107.jpg)
Project as part of The Exam
§ Part of the exam and grading! § The project must be done individually § No cooperation is allowed beyond what
is explicitly stated in the description
107
![Page 108: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/108.jpg)
Subject of the Project
§ To create an electronic inventory for a computer store
§ Keep information about complete computer systems and components
§ System should be able to § calculate prices for components and
computer systems § make lists of components to order from the
distributor 108
![Page 109: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/109.jpg)
Objects of the System
§ component: name, kind, price § kind is one of CPU, RAM, graphics card,
mainboard, case § CPU: socket, bus speed § RAM: type, bus speed § mainboard: CPU socket, RAM type, on-
board graphics?, form factor § case: form factor
109
![Page 110: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/110.jpg)
Objects of the System
§ computer system: catchy name, list of components § requires a case, a mainboard, a CPU, RAM,
optionally a graphics card § sockets, bus speed, RAM type, and form
factor must match § if there is no on-board graphics, a graphics
card must be included
110
![Page 111: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/111.jpg)
Objects of the System
§ current stock: list of components and their current amount
§ minimum inventory: list of components, their allowed minimum amount, and their preferred amount after restocking
111
![Page 112: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/112.jpg)
Intended Use of the System
§ Print a daily price list for components and computer systems
§ Give quotes for custom orders § Print out a list of components for
restocking on Saturday morning (computer store restocks his inventory every Saturday at his distributor)
112
![Page 113: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/113.jpg)
Selling Price
§ Selling price for a component is the price + 30%
§ Selling price for a computer system is sum of the selling prices of the components rounded up to next ’99‘
§ Rebate System: § total price is reduced by 2% for each
additional computer system ordered § maximal 20% rebate
113
![Page 114: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/114.jpg)
Example: Selling Price
§ computer system for which the components are worth DKK 1984
§ the selling price of the components is 1984*1.3 = 2579.2
§ It would be sold for DKK 2599 § Order of 3 systems: DKK 7485, i.e.,
DKK 2495 per system § Order of 11, 23, or 42 systems:
DKK 2079 per system 114
![Page 115: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/115.jpg)
Functionality of the System
§ List of all components in the system and their current amount
§ List of all computer systems in the system and how many of each could be build from the current stock
§ Price list including all components and their selling prices grouped by kind all computers systems that could be build from the current stock including their components and selling price 115
![Page 116: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/116.jpg)
Functionality of the System
§ Price offer given the computer system and the quantity
§ Sell a component or a computer system by updating the current stock
§ Restocking list including names and amounts of all components needed for restocking to the preferred level
116
![Page 117: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/117.jpg)
Limitations for the Project
§ No facilities for updating are required except for the Selling mentioned explicitly
§ Only a simple command-line based interface for user interaction is required § Choices by the user can be input by showing
a numbered list of alternatives or by prompting for component names, etc.
§ You are welcome to include update facilities or make a better user interface but this will not influence the final grade! 117
![Page 118: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/118.jpg)
Tasks
1. Develop an appropriate E/R model 2. Transfer to a relational model 3. Ensure that all relations are in 3NF
(decompose and refine the E/R model) 4. Implement in PostgreSQL DBMS
(ensuring the constraints hold) 5. Program in Java or Python an
application for the user interaction providing all functionality from above
118
![Page 119: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/119.jpg)
Test Data
§ Can be made up as you need it § At least in the order of 8 computer
systems and 30 components § Sharing data with other participants in
the course is explicitly allowed and encouraged
119
![Page 120: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/120.jpg)
Formalities
§ Printed report of approx. 10 pages § design choices and reasoning § structure of the final solution § Must include:
§ A diagram of your E/R model § Schemas of your relations § Arguments showing that these are in 3NF § Central parts of your SQL code + explanation § A (very) short user manual for the application § Documentation of testing
120
![Page 121: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/121.jpg)
Milestones
§ There are two stages: 1. Tasks 1-3, deadline March 11
Preliminary report describing design choices, E/R model, resulting relational model (will be commented on and handed back)
2. Tasks 4-5, deadline March 25 Final report as correction and extension of the preliminary report
§ Grade for the project will be based both on the preliminary and on the final report
121
![Page 122: Boyce-Codd Normal Form · Boyce-Codd Normal Form ! We say a relation R is in BCNF if whenever X → Y is a nontrivial FD that holds in R, X is a superkey ! Remember: nontrivial means](https://reader033.vdocuments.mx/reader033/viewer/2022051512/603cf0d849bae8765662e96e/html5/thumbnails/122.jpg)
Implementation
§ Java with fx JDBC as DB interface § Python with fx psycopg2 as DB interface § SQL and Java/Python code handed in
electronically with report in Blackboard § Database for testing must be available
on the PostgreSQL server § Testing during grading will use your
program and the data on that server
122