0% found this document useful (0 votes)
32 views68 pages

DBMS Unit 4

Unit 4 notes

Uploaded by

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

DBMS Unit 4

Unit 4 notes

Uploaded by

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

UNIT 4

Anomalies in relational database design.

Introduction
Database anomaly is a flaw in databases because of poor planning and
storing everything in a flat database. Anomalies occur when there is too
much redundancy in the database. Poor table design has related data
scattered over various tables. Any new change in the database should be
updated in many places. It is also possible that the information is only half
present. It's there in one table, but missing in another one.
In this article, we will dive deeper into learning in database anomalies. Start
reading with What are Anomalies in DBMS first.

What are the Anomalies in DBMS?


We are already aware about the term normalization. Normalization is the
process of splitting the relations into smaller relations to do the operations
more efficiently without any inconsistency. Without normalization, the
integrity of the table will not be maintained over time. When attempting to
load an integrated conceptual model into the DBMS, several issues can
arise without normalization. Anomalies are the relations that cause these
issues because they are produced straight from user views. So we can say
that A Database anomaly is a flaw that frequently results from poor
planning and storing everything in a flat database.
You can also read about Aggregation in DBMS here.
Example
Assume a manufacturing company stores employee details in a table called
Employee having four attributes:
 Emp_id for employee's id.
 Emp_name for employee's name.
 Emp_address for employee's address.
 Emp_dept for the department details in which the employee works.

The table will look like this. The table given below is not normalized. We will
see how problems arise when a table is not normalized.
e_id e_name e_address e_dept

101 Rick Delhi D001

101 Rick Delhi D002


e_id e_name e_address e_dept

123 Maggie Agra D890

166 Glenn Chennai D900

166 Glenn Chennai D004

Types of Anomalies in DBMS


There are 3 types of database anomalies:
1. Insertion Anomalies
2. Deletion Anomalies
3. Update Anomalies
 Insert Anomaly: The term "insertion anomaly" is used to describe
when a new row is added to a table and it causes an inconsistency.
 Update Anomaly: If there are some changes in the database, we
have to apply that change in all the rows. And if we miss any row, we
will have one more field, creating an update anomaly in the database.
 Delete Anomaly: The term "deletion anomaly in the database" is used
when we delete some rows from a table and any necessary additional
information or data is also lost from the database.
Insert anomaly
If a tuple is inserted in referencing relation and referencing attribute value is
not present in referenced attribute, it will not allow inserting in referencing
relation.
Example
Assume that a new employee is joining the company under training and not
assigned to any department. Then, we would not insert the data into the
table if the emp_dept field doesn't allow nulls.
Update anomaly
If a tuple is updated from referenced relation and the referenced attribute
value is used by referencing attribute in referencing relation. In that case, it
will not allow updating the tuple from referenced relation.
Example
In the given table, we have two rows for an employee named Rick, and he
belongs to two different departments of the company. If we need to update
Rick's address, we must update the same address in two rows. Otherwise,
the data will become inconsistent.
If, in some way, we can update the correct address in one department but
not the other, then according to the database, Rick will have two different
addresses, which is not correct and would lead to inconsistent data.
Delete anomaly
If a tuple is deleted from referenced relation and the referenced attribute
value is used by referencing attribute in referencing relation, it will not allow
deleting the tuple from referenced relation.
Example
Assume that if the company closes the department D890, then deleting the
rows that have emp_dept as D890 would also delete the information of
employee Maggie since she is assigned only to this department.
Also see, Recursive Relationship in DBMS
What are the Common Causes of Database
Anomalies?
The common causes of database anomalies are mentioned below:
1. Redundancy: Duplicate data entries lead to inconsistencies during
updates.
2. Update Anomalies: Inconsistent modifications may occur due to
incomplete updates.
3. Insertion Anomalies: Challenges arise when attempting to insert data
into tables with missing required attributes.
4. Deletion Anomalies: Removing data may unintentionally eliminate
necessary information.
5. Improper Structural Design: Poorly designed table structures can
contribute to anomalies.
How to Avoid Database Anomalies?
To avoid database anomalies, adopting proper normalization techniques is
crucial. Normalization involves organizing data to minimize redundancy and
dependency issues. By breaking down tables into smaller, more
manageable entities and establishing relationships, normalization ensures
data integrity.
Additionally, enforcing referential integrity constraints, utilizing primary and
foreign keys, and conducting thorough data modeling contribute to a robust
database structure, mitigating anomalies and enhancing overall system
reliability. Regular maintenance and periodic reviews of the database
design help identify and address potential anomalies as the system
evolves.
Removal of Anomalies
To prevent anomalies, we need to normalize the database by efficiently
organizing the data in a database. Normalization is a systematic approach
to eliminate data redundancy and Insertion, Modification, and Deletion
Anomalies by decomposing tables. The database designer organizes the
data to eliminate unnecessary duplications and provides a quick search
path to all necessary information.
According to Edgar F Codd, the inventor of relational databases, the goals
of normalization include:
 removing all redundant (or repeated) data from the database
 removing undesirable insertions, updates, and deletion dependencies
 reducing the need to restructure the entire database every time new
fields are added to it
 making the relationships between tables more useful and
understandable.
You can also read about the Checkpoint in DBMS
Frequently Asked Questions
How can normalization help prevent anomalies in
DBMS?
Normalization in DBMS helps prevent anomalies by organizing data to
eliminate redundancy and dependencies. It ensures data consistency,
minimizes update anomalies, and maintains relational integrity, reducing
the risk of inconsistencies and errors in the database.
How do the anomalies affect the database?
Relational databases are affected by 3 types of anomalies, Insertion,
Updation, and Deletion anomaly. They occur due to the presence of data
redundancy and inconsistent data. Anomalies can lead to incorrect query
results and difficulty in the management of your database.
How do you overcome anomalies in DBMS?
You can overcome anomalies in database by using the following measures,
normalization techniques, performing validation on user-submitted data,
using transactions wherever they are necessary, and avoiding
denormalization while attempting to improve performance.
Why do we need to detect anomalies?
Anomalies in your database can be detected by implementing data quality
checks and using validation rules to identify inconsistent values present in
your database. SQL queries can be used for applying quality checks and
validating the data for detecting anomalie
Chapter 7: Normalization

Database System Concepts, 7th Ed.


©Silberschatz, Korth and Sudarshan
See www.db-book.com for conditions on re-use
Outline

§ Features of Good Relational Design


§ Functional Dependencies
§ Decomposition Using Functional Dependencies
§ Normal Forms
§ Functional Dependency Theory
§ Algorithms for Decomposition using Functional Dependencies
§ Decomposition Using Multivalued Dependencies
§ More Normal Form
§ Atomic Domains and First Normal Form
§ Database-Design Process
§ Modeling Temporal Data

Database System Concepts - 7th Edition 7.2 ©Silberschatz, Korth and Sudarshan
Overview of Normalization

Database System Concepts - 7th Edition 7.3 ©Silberschatz, Korth and Sudarshan
Overview

§ Normalization is the process of removing redundant data from your


tables to improve storage efficiency, data integrity, and scalability.
§ Normalization generally involves splitting existing tables into multiple
ones, which must be re-joined or linked each time a query is issued.
• This is called “decomposition”
§ Why is relevant?
• Unnormalized data may hide ambiguities, redundancies, and
inconsistencies
• The problem usually happens when an existing system uses
unstructured file, e.g. in MS Excel.
• The relation derived from the user view or data store will most likely
be denormalized.

Database System Concepts - 7th Edition 7.4 ©Silberschatz, Korth and Sudarshan
Features of Good Relational Designs

§ Suppose we combine instructor and department into in_dep, which


represents the natural join on the relations instructor and department

§ There is repetition of information


§ Need to use null values (if we add a new department with no instructors)

Database System Concepts - 7th Edition 7.5 ©Silberschatz, Korth and Sudarshan
A Combined Schema Without Repetition

Not all combined schemas result in repetition of information

§ Consider combining relations


• sec_class(sec_id, building, room_number) and
• section(course_id, sec_id, semester, year)
into one relation
• section(course_id, sec_id, semester, year,
building, room_number)
§ No repetition in this case

Database System Concepts - 7th Edition 7.6 ©Silberschatz, Korth and Sudarshan
Decomposition

§ The only way to avoid the repetition-of-information problem in the in_dep


schema is to decompose it into two schemas – instructor and department
schemas.
§ Not all decompositions are good. Suppose we decompose

employee(ID, name, street, city, salary)


into
employee1 (ID, name)
employee2 (name, street, city, salary)

The problem arises when we have two employees with the same name
§ The next slide shows how we lose information -- we cannot reconstruct
the original employee relation -- and so, this is a lossy decomposition.

Database System Concepts - 7th Edition 7.7 ©Silberschatz, Korth and Sudarshan
Normalization Theory

§ Decide whether a particular relation R is in “good” form.


§ In the case that a relation R is not in “good” form, decompose it into set
of relations {R1, R2, ..., Rn} such that
• Each relation is in good form
• The decomposition is good (what does it mean?)

Database System Concepts - 7th Edition 7.8 ©Silberschatz, Korth and Sudarshan
A Lossy Decomposition

Database System Concepts - 7th Edition 7.9 ©Silberschatz, Korth and Sudarshan
Lossless Decomposition

§ Let R be a relation schema and let R1 and R2 form a decomposition of R .


That is R = R1 U R2
§ We say that the decomposition is a lossless decomposition if there is
no loss of information by replacing R with the two relation schemas R1
U R2
§ Formally,
Õ R1 (r) Õ R2 (r) = r
§ And, conversely a decomposition is lossy if
r Ì Õ R1 (r) Õ R2 (r) = r

Database System Concepts - 7th Edition 7.10 ©Silberschatz, Korth and Sudarshan
Example of Lossless Decomposition

§ Decomposition of R = (A, B, C)
R1 = (A, B) R2 = (B, C)

Database System Concepts - 7th Edition 7.11 ©Silberschatz, Korth and Sudarshan
Normalization Theory

§ Decide whether a particular relation R is in “good” form.


§ In the case that a relation R is not in “good” form, decompose it into set
of relations {R1, R2, ..., Rn} such that
• Each relation is in good form
• The decomposition is a lossless decomposition
§ Our theory is based on:
• Functional dependencies
• Multivalued dependencies (we won’t discuss them)

Database System Concepts - 7th Edition 7.12 ©Silberschatz, Korth and Sudarshan
Functional Dependencies

§ There are usually a variety of constraints (rules) on the data in the real
world.
§ For example, some of the constraints that are expected to hold in a
university database are:
• Students and instructors are uniquely identified by their ID.
• Each student and instructor has only one name.
• Each instructor and student is (primarily) associated with only one
department.
• Each department has only one value for its budget, and only one
associated building.

Database System Concepts - 7th Edition 7.13 ©Silberschatz, Korth and Sudarshan
Functional Dependencies (Cont.)

§ An instance of a relation that satisfies all such real-world constraints is


called a legal instance of the relation;
§ A legal instance of a database is one where all the relation instances are
legal instances
§ Constraints on the set of legal relations.
§ Require that the value for a certain set of attributes determines uniquely
the value for another set of attributes.
§ A functional dependency is a generalization of the notion of a key.

Database System Concepts - 7th Edition 7.14 ©Silberschatz, Korth and Sudarshan
Functional Dependencies Definition
§ Let R be a relation schema
a Í R and b Í R
§ The functional dependency
a®b
holds on R if and only if for any legal relations r(R), whenever any two
tuples t1 and t2 of r agree on the attributes a, they also agree on the
attributes b. That is,

t1[a] = t2 [a] Þ t1[b ] = t2 [b ]

§ Example: Consider r(A,B ) with the following instance of r.

1 4
1 5
3 7

§ On this instance, B ® A hold; A ® B does NOT hold,

Database System Concepts - 7th Edition 7.15 ©Silberschatz, Korth and Sudarshan
Closure of a Set of Functional Dependencies

§ Given a set F set of functional dependencies, there are certain other


functional dependencies that are logically implied by F.
• If A ® B and B ® C, then we can infer that A ® C
• etc.
§ The set of all functional dependencies logically implied by F is the
closure of F.
§ We denote the closure of F by F+.

Database System Concepts - 7th Edition 7.16 ©Silberschatz, Korth and Sudarshan
Keys and Functional Dependencies

§ K is a superkey for relation schema R if and only if K ® R


§ K is a candidate key for R if and only if
• K ® R, and
• for no a Ì K, a ® R
§ Functional dependencies allow us to express constraints that cannot be
expressed using superkeys. Consider the schema:
in_dep (ID, name, salary, dept_name, building, budget ).
We expect these functional dependencies to hold:
dept_name® building
ID à building
but would not expect the following to hold:
dept_name ® salary

Database System Concepts - 7th Edition 7.17 ©Silberschatz, Korth and Sudarshan
Use of Functional Dependencies

§ Functional Dependencies are a form of constraint.


§ We use functional dependencies to:
• To test relations to see if they are legal
§ If a relation r is legal under a set F of functional dependencies,
we say that r satisfies F.
• To 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 functional dependencies F.
§ Note: A specific instance of a relation schema may satisfy a functional
dependency even if the functional dependency does not hold on all legal
instances.
• For example, a specific instance of instructor may, by chance, satisfy
name ® ID.

Database System Concepts - 7th Edition 7.18 ©Silberschatz, Korth and Sudarshan
Trivial Functional Dependencies

§ A functional dependency is trivial if it is satisfied by all instances of a


relation
§ Example:
• ID, name ® ID
• name ® name
§ In general, a ® b is trivial if b Í a

Database System Concepts - 7th Edition 7.19 ©Silberschatz, Korth and Sudarshan
Lossless Decomposition

§ We can use functional dependencies to show when certain


decomposition are lossless.
§ For the case of R = (R1, R2), we require that for all possible relations r on
schema R
r = ÕR1 (r ) ÕR2 (r )
§ A decomposition of R into R1 and R2 is lossless decomposition if at least
one of the following dependencies is in F+:
• R1 Ç R2 ® R1
• R1 Ç R2 ® R2
§ The above functional dependencies are a sufficient condition for lossless
join decomposition; the dependencies are a necessary condition only if all
constraints are functional dependencies

Database System Concepts - 7th Edition 7.20 ©Silberschatz, Korth and Sudarshan
Example

§ R = (A, B, C)
F = {A ® B, B ® C)
§ R1 = (A, B), R2 = (B, C)
• Lossless decomposition:
R1 Ç R2 = {B} and B ® BC
§ R1 = (A, B), R2 = (A, C)
• Lossless decomposition:
R1 Ç R2 = {A} and A ® AB
§ Note:
• B ® BC
is a shorthand notation for
• B ® {B, C}

Database System Concepts - 7th Edition 7.21 ©Silberschatz, Korth and Sudarshan
Dependency Preservation

§ Testing functional dependency constraints each time the database is


updated can be costly
§ It is useful to design the database in a way that constraints can be
tested efficiently.
§ If testing a functional dependency can be done by considering just one
relation, then the cost of testing this constraint is low
§ When decomposing a relation it is possible that it is no longer possible
to do the testing without having to perform a Cartesian Produced.
§ A decomposition that makes it computationally hard to enforce
functional dependency is said to be NOT dependency preserving.

Database System Concepts - 7th Edition 7.22 ©Silberschatz, Korth and Sudarshan
Dependency Preservation Example

§ Consider a schema:
dept_advisor(s_ID, i_ID, department_name)
§ With function dependencies:
i_ID ® dept_name
s_ID, dept_name ® i_ID
§ In the above design we are forced to repeat the department name once
for each time an instructor participates in a dept_advisor relationship.
§ To fix this, we need to decompose dept_advisor
§ Any decomposition will not include all the attributes in
s_ID, dept_name ® i_ID
§ Thus, the composition NOT be dependency preserving

Database System Concepts - 7th Edition 7.23 ©Silberschatz, Korth and Sudarshan
Steps of
Normalization

Database System Concepts - 7th Edition 7.24 ©Silberschatz, Korth and Sudarshan
Normal Forms

§ First Normal Form (1NF)


§ Second Normal Form (2NF)
§ Third Normal Form (3NF)
§ Boyce-Codd Normal Form (BCNF)
§ Fourth Normal Form (4NF)
§ Fifth Normal Form (5NF)
§ Domain Key Normal Form (DKNF)

In practice, 1NF, 2NF, 3NF, and BCNF are enough for database.
So we will focus on them!

Database System Concepts - 7th Edition 7.25 ©Silberschatz, Korth and Sudarshan
Normal Forms

§ First Normal Form (1NF) – all tables are flat


§ Second Normal Form (2NF)
§ Third Normal Form (3NF)
§ Boyce-Codd Normal Form (BCNF)
§ Fourth Normal Form (4NF)
§ Fifth Normal Form (5NF)
§ Domain Key Normal Form (DKNF)

In practice, 1NF, 2NF, 3NF, and BCNF are enough for database.
So we will focus on them!

Database System Concepts - 7th Edition 7.26 ©Silberschatz, Korth and Sudarshan
Normal Forms

§ First Normal Form (1NF)


DB designs based on
§
§
Second Normal Form (2NF)
Third Normal Form (3NF) } functional
dependencies,
intended to prevent
§ Boyce-Codd Normal Form (BCNF)
data anomalies
§ Fourth Normal Form (4NF)
§ Fifth Normal Form (5NF)

In practice, 1NF, 2NF, 3NF, and BCNF are enough for database.
So we will focus on them!

Database System Concepts - 7th Edition 7.27 ©Silberschatz, Korth and Sudarshan
Normal Forms

Database System Concepts - 7th Edition 7.28 ©Silberschatz, Korth and Sudarshan
Goals of Normalization

§ Let R be a relation scheme with a set F of functional dependencies.


§ Decide whether a relation scheme R is in “good” form.
§ In the case that a relation scheme R is not in “good” form, need to
decompose it into a set of relation scheme {R1, R2, ..., Rn} such that:
• Each relation scheme is in good form
• The decomposition is a lossless decomposition
• Preferably, the decomposition should be dependency preserving.

Database System Concepts - 7th Edition 7.29 ©Silberschatz, Korth and Sudarshan
First Normal Form (1NF)

The official qualifications for 1NF are:


1. Each attribute name must be unique.
2. Each attribute value must be single.
3. Each row must be unique.
4. There is no repeating groups.

Additional:
• Choose a primary key.

Reminder:
• A primary key is unique, not null, unchanged.
• A primary key can be either an attribute or combined attributes.

Database System Concepts - 7th Edition 7.30 ©Silberschatz, Korth and Sudarshan
First Normal Form (formally)

§ Domain is atomic if its elements are considered to be indivisible units


• Examples of non-atomic domains:
§ Set of names, composite attributes
§ Identification numbers like CS101 that can be broken up into parts
§ A relational schema R is in first normal form if the domains of all attributes
of R are atomic
§ Non-atomic values complicate storage and encourage redundant
(repeated) storage of data
• Example: Set of accounts stored with each customer, and set of
owners stored with each account
• We assume all relations are in first normal form (and revisit this in
Chapter 22: Object Based Databases)

Database System Concepts - 7th Edition 7.31 ©Silberschatz, Korth and Sudarshan
First Normal Form (1NF)

Database System Concepts - 7th Edition 7.32 ©Silberschatz, Korth and Sudarshan
Second Normal Form (2NF)

The official qualifications for 2NF are:


1. A table is already in 1NF.
2. All non-key attributes are fully dependent on the primary key.

All partial dependencies (PD) are removed to place in another table.


A PD is a functional dependency whose determinant is part of the primary
key (but not all of it)

Database System Concepts - 7th Edition 7.33 ©Silberschatz, Korth and Sudarshan
Second Normal Form (2NF)

The Course Name depends on only CourseID, a part of the primary key not
the whole primary {CourseID, SemesterID}. It’s called partial dependency.

Solution:
Remove CourseID and Course Name together to create a new table.

Database System Concepts - 7th Edition 7.34 ©Silberschatz, Korth and Sudarshan
Second Normal Form (2NF)

The Course Name depends on only CourseID, a part of the primary key not
the whole primary {CourseID, SemesterID}. It’s called partial dependency.

Solution:
Remove CourseID and Course Name together to create a new table.

Database System Concepts - 7th Edition 7.35 ©Silberschatz, Korth and Sudarshan
Third Normal Form (3NF)

The official qualifications for 3NF are:


1. A table is already in 2NF
2. Non-primary key attributes do not depend on other non-primary key
attributes (i.e. no transitive dependencies)

All transitive dependencies are removed to place in another table.


A Transitive dependency is a functional dependency whose determinant is not
the primary key, part of the primary key, or a candidate key.

Transitive functionality is a functional dependency in which a non-key attribute


is determined by another non-key attribute.

Database System Concepts - 7th Edition 7.36 ©Silberschatz, Korth and Sudarshan
Third Normal Form

§ A relation schema R is in third normal form (3NF) if for all:


a ® b in F+

at least one of the following holds:


• a ® b is trivial (i.e., b Î a)
• a is a superkey for R
• Each attribute A in b – a is contained in a candidate key for R.
(NOTE: each attribute may be in a different candidate key)

Database System Concepts - 7th Edition 7.37 ©Silberschatz, Korth and Sudarshan
Third Normal Form (3NF)

Database System Concepts - 7th Edition 7.38 ©Silberschatz, Korth and Sudarshan
Second Normal Form (2NF)

Database System Concepts - 7th Edition 7.39 ©Silberschatz, Korth and Sudarshan
3NF Example

§ Consider a schema:
dept_advisor(s_ID, i_ID, dept_name)
§ With function dependencies:
i_ID ® dept_name
s_ID, dept_name ® i_ID
§ Two candidate keys = {s_ID, dept_name}, {s_ID, i_ID }
§ We have seen before that dept_advisor is not in BCNF
§ R, however, is in 3NF
• s_ID, dept_name is a superkey
• i_ID ® dept_name and i_ID is NOT a superkey, but:
§ { dept_name} – {i_ID } = {dept_name } and
§ dept_name is contained in a candidate key

Database System Concepts - 7th Edition 7.40 ©Silberschatz, Korth and Sudarshan
Redundancy in 3NF

§ Consider the schema R below, which is in 3NF


• R = (J, K, L )
• F = {JK ® L, L ® K }
• And an instance table:

§ What is wrong with the table?


• Repetition of information
• Need to use null values (e.g., to represent the relationship l2, k2
where there is no corresponding value for J)

Database System Concepts - 7th Edition 7.41 ©Silberschatz, Korth and Sudarshan
Boyce-Codd Normal Form

§ The official qualifications for BCNF are:


1. A table is already in 3NF.
2. All determinants must be superkeys.
3. All determinants that are not superkeys are removed to place in
another table.

§ K is a superkey for relation R if K functionally determines all of R.

§ K is a (candidate)key for R if K is a superkey, but no proper subset of K


is a superkey.

Database System Concepts - 7th Edition 7.42 ©Silberschatz, Korth and Sudarshan
Boyce-Codd Normal Form

§ A relation schema R is in BCNF with respect to a set F of functional


dependencies if for all functional dependencies in F+ of the form
a®b
where a Í R and b Í R, at least one of the following holds:
• a ® b is trivial (i.e., b Í a)
• a is a superkey for R

Database System Concepts - 7th Edition 7.43 ©Silberschatz, Korth and Sudarshan
Boyce-Codd Normal Form (Cont.)

§ Example schema that is not in BCNF:


in_dep (ID, name, salary, dept_name, building, budget )
because :
• dept_name® building, budget
§ holds on in_dep
§ but
• dept_name is not a superkey
§ When decompose in_dept into instructor and department
• instructor is in BCNF
• department is in BCNF

Database System Concepts - 7th Edition 7.44 ©Silberschatz, Korth and Sudarshan
Decomposing a Schema into BCNF

§ Let R be a schema R that is not in BCNF. Let a ®b be the FD that


causes a violation of BCNF.
§ We decompose R into:
• (a U b )
• (R-(b-a))
§ In our example of in_dep,
• a = dept_name
• b = building, budget
and in_dep is replaced by
• (a U b ) = ( dept_name, building, budget )
• ( R - ( b - a ) ) = ( ID, name, dept_name, salary )

Database System Concepts - 7th Edition 7.45 ©Silberschatz, Korth and Sudarshan
Example

§ R = (A, B, C)
F = {A ® B, B ® C)
§ R1 = (A, B), R2 = (B, C)
• Lossless-join decomposition:
R1 Ç R2 = {B} and B ® BC
• Dependency preserving
§ R1 = (A, B), R2 = (A, C)
• Lossless-join decomposition:
R1 Ç R2 = {A} and A ® AB
• Not depend ency preserving
(cannot check B ® C without computing R1 R2)

Database System Concepts - 7th Edition 7.46 ©Silberschatz, Korth and Sudarshan
Example

§ Key: {Student, Course}


§ Functional Dependency:
• {Student, Course} ->Teacher
• Teacher -> Course
§ Problem: Teacher is not a superkey but determines Course.

Database System Concepts - 7th Edition 7.47 ©Silberschatz, Korth and Sudarshan
Example

Database System Concepts - 7th Edition 7.48 ©Silberschatz, Korth and Sudarshan
BCNF and Dependency Preservation

§ It is not always possible to achieve both BCNF and dependency


preservation
§ Consider a schema:
dept_advisor(s_ID, i_ID, department_name)
§ With function dependencies:
i_ID ® dept_name
s_ID, dept_name ® i_ID
§ dept_advisor is not in BCNF
• i_ID is not a superkey.
§ Any decomposition of dept_advisor will not include all the attributes in
s_ID, dept_name ® i_ID
§ Thus, the composition is NOT be dependency preserving

Database System Concepts - 7th Edition 7.49 ©Silberschatz, Korth and Sudarshan
Comparison of BCNF and 3NF

§ If a relation is in BCNF it is in 3NF (since in BCNF one of the first two


conditions above must hold).
§ Third condition is a minimal relaxation of BCNF to ensure dependency
preservation (will see why later).
§ Advantages to 3NF over BCNF. It is always possible to obtain a 3NF
design without sacrificing losslessness or dependency preservation.
§ Disadvantages to 3NF.
• We may have to use null values to represent some of the possible
meaningful relationships among data items.
• There is the problem of repetition of information.

Database System Concepts - 7th Edition 7.50 ©Silberschatz, Korth and Sudarshan
How good is BCNF?

§ There are database schemas in BCNF that do not seem to be


sufficiently normalized
§ Consider a relation
inst_info (ID, child_name, phone)
• where an instructor may have more than one phone and can have
multiple children
• Instance of inst_info

Database System Concepts - 7th Edition 7.51 ©Silberschatz, Korth and Sudarshan
How good is BCNF? (Cont.)

§ There are no non-trivial functional dependencies and therefore the


relation is in BCNF
§ Insertion anomalies – i.e., if we add a phone 981-992-3443 to 99999, we
need to add two tuples
(99999, David, 981-992-3443)
(99999, William, 981-992-3443)

Database System Concepts - 7th Edition 7.52 ©Silberschatz, Korth and Sudarshan
Higher Normal Forms

§ It is better to decompose inst_info into:


• inst_child:

• inst_phone:

§ This suggests the need for higher normal forms, such as Fourth
Normal Form (4NF), which we shall see later

Database System Concepts - 7th Edition 7.53 ©Silberschatz, Korth and Sudarshan
Design Goals

§ Goal for a relational database design is:


• BCNF.
• Lossless join.
• Dependency preservation.
§ If we cannot achieve this, we accept one of
• Lack of dependency preservation
• Redundancy due to use of 3NF
§ Interestingly, SQL does not provide a direct way of specifying functional
dependencies other than superkeys.
Can specify FDs using assertions, but they are expensive to test, (and
currently not supported by any of the widely used databases!)
§ Even if we had a dependency preserving decomposition, using SQL we
would not be able to efficiently test a functional dependency whose left
hand side is not a key.

Database System Concepts - 7th Edition 7.54 ©Silberschatz, Korth and Sudarshan
Testing for BCNF

§ To check if a non-trivial dependency a ®b causes a violation of BCNF


1. compute a+ (the attribute closure of a), and
2. verify that it includes all attributes of R, that is, it is a superkey of R.
§ Simplified test: To check if a relation schema R is in BCNF, it suffices to
check only the dependencies in the given set F for violation of BCNF,
rather than checking all dependencies in F+.
• If none of the dependencies in F causes a violation of BCNF, then
none of the dependencies in F+ will cause a violation of BCNF either.
§ However, simplified test using only F is incorrect when testing a relation
in a decomposition of R
• 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
(A,C,D,E) so we might be mislead into thinking R2 satisfies BCNF.
§ In fact, dependency AC ® D in F+ shows R2 is not in BCNF.

Database System Concepts - 7th Edition 7.56 ©Silberschatz, Korth and Sudarshan
Testing Decomposition for BCNF

To check if a relation Ri in a decomposition of R is in BCNF

§ Either test Ri for BCNF with respect to the restriction of F+ to Ri (that


is, all FDs in F+ that contain only attributes from Ri)
§ Or use the original set of dependencies F that hold on R, but with the
following test:
§ for every set of attributes a Í Ri, check that a+ (the attribute
closure of a) either includes no attribute of Ri- a, or includes all
attributes of Ri.
• If the condition is violated by some a ® b in F+, the dependency
a ® (a+ - a) Ç Ri
can be shown to hold on Ri, and Ri violates BCNF.
• We use above dependency to decompose Ri

Database System Concepts - 7th Edition 7.57 ©Silberschatz, Korth and Sudarshan
BCNF Decomposition Algorithm

result := {R };
done := false;
compute F +;
while (not done) do
if (there is a schema Ri in result that is not in BCNF)
then begin
let a ® b be a nontrivial functional dependency that
holds on Ri such that a ® Ri is not in F +,
and a Ç b = Æ;
result := (result – Ri ) È (Ri – b) È (a, b );
end
else done := true;

Note: each Ri is in BCNF, and decomposition is lossless-join.

Database System Concepts - 7th Edition 7.58 ©Silberschatz, Korth and Sudarshan
Example of BCNF Decomposition

§ class (course_id, title, dept_name, credits, sec_id, semester, year,


building, room_number, capacity, time_slot_id)
§ Functional dependencies:
• course_id→ title, dept_name, credits
• building, room_number→capacity
• course_id, sec_id, semester, year→building, room_number,
time_slot_id
§ A candidate key {course_id, sec_id, semester, year}.
§ BCNF Decomposition:
• course_id→ title, dept_name, credits holds
§ but course_id is not a superkey.
• We replace class by:
§ course(course_id, title, dept_name, credits)
§ class-1 (course_id, sec_id, semester, year, building,
room_number, capacity, time_slot_id)

Database System Concepts - 7th Edition 7.59 ©Silberschatz, Korth and Sudarshan
BCNF Decomposition (Cont.)

§ course is in BCNF
• How do we know this?
§ building, room_number→capacity holds on class-1
• but {building, room_number} is not a superkey for class-1.
• We replace class-1 by:
§ classroom (building, room_number, capacity)
§ section (course_id, sec_id, semester, year, building,
room_number, time_slot_id)
§ classroom and section are in BCNF.

Database System Concepts - 7th Edition 7.60 ©Silberschatz, Korth and Sudarshan
Overall Database Design Process

We have assumed schema R is given


§ R could have been generated when converting E-R diagram to a set
of tables.
§ R could have been a single relation containing all attributes that are of
interest (called universal relation).
§ Normalization breaks R into smaller relations.
§ R could have been the result of some ad hoc design of relations,
which we then test/convert to normal form.

Database System Concepts - 7th Edition 7.61 ©Silberschatz, Korth and Sudarshan
ER Model and Normalization

§ When an E-R diagram is carefully designed, identifying all entities


correctly, the tables generated from the E-R diagram should not need
further normalization.
§ However, in a real (imperfect) design, there can be functional
dependencies from non-key attributes of an entity to other attributes of the
entity
• Example: an employee entity with
§ attributes
department_name and building,
§ functional dependency
department_name® building
§ Good design would have made department an entity
§ Functional dependencies from non-key attributes of a relationship set
possible, but rare --- most relationships are binary

Database System Concepts - 7th Edition 7.62 ©Silberschatz, Korth and Sudarshan
Denormalization for Performance

§ May want to use non-normalized schema for performance


§ For example, displaying prereqs along with course_id, and title requires
join of course with prereq
§ Alternative 1: Use denormalized relation containing attributes of course
as well as prereq with all above attributes
• faster lookup
• extra space and extra execution time for updates
• extra coding work for programmer and possibility of error in extra
code
§ Alternative 2: use a materialized view defined a course prereq
• Benefits and drawbacks same as above, except no extra coding work
for programmer and avoids possible errors

Database System Concepts - 7th Edition 7.63 ©Silberschatz, Korth and Sudarshan
Other Design Issues

§ Some aspects of database design are not caught by normalization


§ Examples of bad database design, to be avoided:
Instead of earnings (company_id, year, amount ), use
• earnings_2004, earnings_2005, earnings_2006, etc., all on the
schema (company_id, earnings).
§ Above are in BCNF, but make querying across years difficult and
needs new table each year
• company_year (company_id, earnings_2004, earnings_2005,
earnings_2006)
§ Also in BCNF, but also makes querying across years difficult and
requires new attribute each year.
§ Is an example of a crosstab, where values for one attribute
become column names
§ Used in spreadsheets, and in data analysis tools

Database System Concepts - 7th Edition 7.64 ©Silberschatz, Korth and Sudarshan
End of Chapter 7

Database System Concepts - 7th Edition 7.65 ©Silberschatz, Korth and Sudarshan

You might also like