Norm DBAlg
Norm DBAlg
LECTURE NOTES
Definition
Redundancy
◦ First Normal Form (1NF)
Number of Tables
◦ Second Normal Form (2NF)
◦ Third Normal Form (3NF)
Complexity
◦ Boyce-Codd Normal Form (BCNF)
◦ Fourth Normal Form (4NF)
◦ Fifth Normal Form (5NF)
◦ Domain Key Normal Form (DKNF)
Most
Mostdatabases
databasesshould
shouldbe
be3NF
3NFor
orBCNF
BCNFin inorder
orderto
toavoid
avoidthe
thedatabase
database
anomalies.
anomalies.
Levels of Normalization
1NF
2NF
3NF
4NF
5NF
DKNF
Each
Eachhigher
higherlevel
levelisisaasubset
subsetof
ofthe
thelower
lowerlevel
level
First Normal Form (1NF)
Author
Authorand
andAuPhone
AuPhonecolumns
columnsare
arenot
notscalar
scalar
1NF - Decomposition
1. Place all items that appear in the repeating group in a new table
2. Designate a primary key for each new table produced.
3. Duplicate in the new table the primary key of the table from which the
repeating group was extracted or vice versa.
Example (1NF) ISBN AuName AuPhone
0-55-123456-9 Main Street Small House 714-000-0000 $22.95 0-55-123456-9 Jones 123-333-3333
Example
ISBN 1 Title Price Table Scheme: {ISBN, Title, Price}
0-321-32132-1 Balloon $34.00 Functional Dependencies: {ISBN} {Title}
0-55-123456-9 Main Street $22.95 {ISBN}
0-123-45678-0 Ulysses $34.00
{Price}
1-22-233700-0 Visual $25.00
Basic
Functional Dependencies
Example 2
PubID PubName PubPhone
1 Big House 999-999-9999
Table Scheme: {PubID, PubName, PubPhone}
2 Small House 123-456-7890 Functional Dependencies: {PubId} {PubPhone}
3 Alpha Press 111-111-1111 {PubId}
{PubName}
{PubName, PubPhone}
Example 3 {PubID}
AuID AuName AuPhone
1 Sleepy 321-321-1111
Table Scheme: {AuID, AuName, AuPhone}
2 Snoopy 232-234-1234 Functional Dependencies: {AuId} {AuPhone}
3 Grumpy 665-235-6532 {AuId}
4 Jones 123-333-3333 {AuName}
5 Smith 654-223-3455 {AuName, AuPhone} {AuID}
6 Joyce 666-666-6666
7 Roman 444-444-4444
FD – Example
Functional Dependencies
◦ AuthNo AuthName, AuthEmail, AuthAddress
◦ AuthEmail AuthNo
◦ PaperNo Primary-AuthNo, Title, Abstract, Status
◦ RevNo RevName, RevEmail, RevAddress
◦ RevEmail RevNo
◦ RevNo, PaperNo AuthComm, Prog-Comm, Date,
Rating1, Rating2, Rating3, Rating4, Rating5
Second Normal Form (2NF)
This form dictates that all non-key attributes of a table must be functionally
dependent on a candidate key i.e. there can be no interdependencies among
non-key attributes.
For a table to be in 3NF, there are two requirements
◦ The table should be second normal form
◦ No attribute is transitively dependent on the primary key
Use
Useyour
yourown
ownjudgment
judgmentwhen
whendecomposing
decomposingschemas
schemas
BCNF - Decomposition
2. Each manager can have more than one child Mary Bob Jane
3. Each manager can supervise more than one employee Mary NULL Adam
4. 4NF Violated
Fifth normal form is satisfied when all tables are broken into
as many tables as possible in order to avoid redundancy.
Once it is in fifth normal form it cannot be broken into
smaller relations without changing the facts or the meaning.
Domain Key Normal Form (DKNF)
Declartive
Declartive
query
query Algebra
Algebra Implementation
Implementation
language
language
P Name,Salary (Employee)
Name Salary
John 20000
John 60000
5. Cartesian Product
Each tuple in R1 with each tuple in R2
Notation: R1 R2
Example:
◦ Employee Dependents
Very rare in practice; mainly used to express joins
Cartesian Product Example
Employee
Name SSN
John 999999999
Tony 777777777
Dependents
EmployeeSSN Dname
999999999 Emily
777777777 Joe
Employee x Dependents
Name SSN EmployeeSSN Dname
John 999999999 999999999 Emily
John 999999999 777777777 Joe
Tony 777777777 999999999 Emily
Tony 777777777 777777777 Joe
Relational Algebra
Five operators:
◦ Union:
◦ Difference: -
◦ Selection: s
◦ Projection: P
◦ Cartesian Product:
Dependents
SSN Dname
999999999 Emily
777777777 Joe
Employee Dependents =
PName, SSN, Dname(s SSN=SSN2(Employee x rSSN2, Dname(Dependents))
Name SSN Dname
John 999999999 Emily
Tony 777777777 Joe
Natural Join
R= A B S= B C
Z U
X Y
X Z V W
Y Z Z V
Z V
R || S= A B C
X Z U
X Z V
Y Z U
Y Z V
Z V W
Natural Join
1. Given the schemas R(A, B, C, D), and S(A, C, E), what
is the schema of R || S ?
2. Given R(A, B, C), S(D, E), what is R || S ?
3. Given R(A, B), S(A, B), what is R || S ?
Theta Join
A join that involves a predicate
R1 || q R2 = s q (R1 R2)
Here q can be any condition
Equality - join
A theta join where q is an equality
R1 || A=B R2 = s A=B (R1 R2)
Example:
◦ Employee || SSN=SSN Dependents
Dependents
Employee
SSN Dname Age
SSN Name ... ...
... ... network
Employee |
Employee | ssn=ssn (s age>71(Dependents))
ssn=ssn (s age>71 (Dependents))
T = P SSN s age>71 (Dependents)
R = Employee | T
Answer = R || Dependents
Complex RA Expressions
P name
buyer-ssn=ssn
pid=pid
seller-ssn=ssn
P ssn P pid
sname=fred sname=gizmo