UCSD CSE132B Slide 1
CSE 132B CSE 132B Database Systems Applications Database Systems Applications
Alin Deutsch Database Design and Normal Forms
- !"#$%&
'())&*
CSE 132B CSE 132B Database Systems Applications Database Systems - - PowerPoint PPT Presentation
CSE 132B CSE 132B Database Systems Applications Database Systems Applications Alin Deutsch Database Design and Normal Forms
UCSD CSE132B Slide 1
'())&*
UCSD CSE132B Slide 2
UCSD CSE132B Slide 3
UCSD CSE132B Slide 4
UCSD CSE132B Slide 5
UCSD CSE132B Slide 6
UCSD CSE132B Slide 7
UCSD CSE132B Slide 8
UCSD CSE132B Slide 9
UCSD CSE132B Slide 10
UCSD CSE132B Slide 11
e.g.: “Every student is a person”
inclusion dependency
“Each employee works in no more than one department”
NAME → DEPARTMENT functional dependency
check data integrity query optimization schema design → “normal forms”
UCSD CSE132B Slide 12
UCSD CSE132B Slide 13
expression X → Y where X, Y ⊆ att(R)
UCSD CSE132B Slide 14
K → R, and for no α ⊂ K, α → R
UCSD CSE132B Slide 15
UCSD CSE132B Slide 16
test relations to see if they are legal under a given set of functional
If a relation r is legal under a set F of functional dependencies, we say
specify constraints on the set of legal relations We say that F holds on R if all legal relations on R satisfy the set of
For example, a specific instance of loan may, by chance, satisfy
UCSD CSE132B Slide 17
UCSD CSE132B Slide 18
Let R be a relation schema and F a set of FD’s over R. Key: X⊆ att(R) such that X→att(R). Minimal key: X ⊆ att(R) s.t. X→att(R)
A ∈ att(R) is prime: A ∈ X where X is a minimal key A is non-prime: A is not a member of any minimal key.
Eliminate problems of redundancy and anomalies.
UCSD CSE132B Slide 19
UCSD CSE132B Slide 20
not in BCNF wrt F:
S(S#, SCITY, SNAME) is in BCNF wrt S# →SNAME SCITY P(P#, PCITY, PNAME) is in BCNF wrt P# →PNAME PCITY SP(S# P# QTY) is in BCNF wrt S# P# → QTY
UCSD CSE132B Slide 21
( R - ( β - α ) ) = ( customer_id, loan_number )
UCSD CSE132B Slide 22
UCSD CSE132B Slide 23
Not every relation schema can be decomposed into BCNF relation
A relation scheme R is in Third Normal Form wrt a set F of fd’s over
UCSD CSE132B Slide 24
α → β is trivial (i.e., β ∈ α) α is a superkey for R Each attribute A in β – α is contained in a candidate key for R.
since in BCNF one of the first two conditions above must hold.
UCSD CSE132B Slide 25
UCSD CSE132B Slide 26
UCSD CSE132B Slide 27
UCSD CSE132B Slide 28
A → H by transitivity from A → B and B → H AG → I by augmenting A → C with G, to get AG → CG
CG → HI by augmenting CG → I to infer CG → CGI,
UCSD CSE132B Slide 29
UCSD CSE132B Slide 30
UCSD CSE132B Slide 31
UCSD CSE132B Slide 32
A
UCSD CSE132B Slide 33
To test if α is a superkey, we compute α+, and check if α+ contains
To check if a functional dependency α → β holds (or, in other
That is, we compute α+ by using attribute closure, and then check
Is a simple and cheap test, and very useful
For each γ ⊆ R, we find the closure γ+, and for each S ⊆ γ+, we
UCSD CSE132B Slide 34
Attribute A is extraneous in α if A ∈ α
Attribute A is extraneous in β if A ∈ β
B is extraneous in AB → C because {A → C, AB → C} logically implies
C is extraneous in AB → CD since
UCSD CSE132B Slide 35
UCSD CSE132B Slide 36
For example: A → C is redundant in: {A → B, B → C} Parts of a functional dependency may be redundant E.g.: on RHS: {A → B, B → C, A → CD} can be simplified
E.g.: on LHS: {A → B, B → C, AC → D} can be simplified
UCSD CSE132B Slide 37
F logically implies all dependencies in Fc, and Fc logically implies all dependencies in F, and No functional dependency in Fc contains an extraneous attribute, and Each left side of functional dependency in Fc is unique.
UCSD CSE132B Slide 38
F = {A → BC B → C A → B AB → C}
dependencies
Yes: in fact, B → C is already present!
Yes: using transitivity on A → B and B → C.
– Can use attribute closure of A in more complex cases
A → B B → C
UCSD CSE132B Slide 39
UCSD CSE132B Slide 40
Can be decomposed in two different ways
Lossless-join decomposition:
Dependency preserving
Lossless-join decomposition:
Not dependency preserving
UCSD CSE132B Slide 41
UCSD CSE132B Slide 42
result = α
If result contains all attributes in β, then the functional dependency
UCSD CSE132B Slide 43
UCSD CSE132B Slide 44
If none of the dependencies in F causes a violation of BCNF, then
Consider R = (A, B, C, D, E), with F = { A → B, BC → D} Decompose R into R1 = (A,B) and R2 = (A,C,D, E) Neither of the dependencies in F contain only attributes from
In fact, dependency AC → D in F+ shows R2 is not in BCNF.
UCSD CSE132B Slide 45
Either test Ri for BCNF with respect to the restriction of F to Ri (that
If the condition is violated by some α → β in F, the dependency
We use above dependency to decompose Ri
UCSD CSE132B Slide 46
UCSD CSE132B Slide 47
UCSD CSE132B Slide 48
UCSD CSE132B Slide 49
(cont.) CTHRSG Key = HS, C→T CS →G, HR →C, HS →R, TH →R CSG key = CS CS →G CTHRS key = SH C→T TH→R HR →C HS →R CT key = C C→T CHRS key = SH CH →R HR →C HS →R CHR key = CH, CH →R
CHS key = SH SH →C
UCSD CSE132B Slide 50
UCSD CSE132B Slide 51
R1 = (branch_name, branch_city, assets ) R2 = (branch_name, customer_name, loan_number, amount ) R3 = (branch_name, loan_number, amount ) R4 = (customer_name, loan_number )
UCSD CSE132B Slide 52
UCSD CSE132B Slide 53
BCNF is not dependency preserving, and efficient checking for FD violation on updates is important
Allows some redundancy (with resultant problems; we will
But functional dependencies can be checked on individual
There is always a lossless-join, dependency-preserving
UCSD CSE132B Slide 54
UCSD CSE132B Slide 55
R = (J, K, L)
UCSD CSE132B Slide 56
this test is rather more expensive, since it involve finding candidate
testing for 3NF has been shown to be NP-hard Interestingly, decomposition into third normal form (described shortly)
UCSD CSE132B Slide 57
UCSD CSE132B Slide 58
(Cont.)
UCSD CSE132B Slide 59
UCSD CSE132B Slide 60
UCSD CSE132B Slide 61
BCNF. Lossless join. Dependency preservation.
Lack of dependency preservation Redundancy due to use of 3NF
UCSD CSE132B Slide 62
UCSD CSE132B Slide 63
tuples need to be inserted (database, Marilyn, DB Concepts) (database, Marilyn, Ullman)
UCSD CSE132B Slide 64
(Cont.)