Professional Documents
Culture Documents
Tutorial 8
Tutorial 8
Sharon Wulff
Each relational database table must have a field or a combination of fields that
holds a value that uniquely identifies each record.
1. Look for a single field which holds a value that is unique for each record
For example, orders table of a company would have something like Order
ID as a primary key. Order ID would be unique for each order
Def. A composite key is a primary key that consists of more than one attribute
(that have to be unique as a tuple).
A foreign key is used when we must represent the relationship between two
tables
1. insertion
2. deletion
3. update
All might result in an inconsistent state of the database.
• To insert the details of a new branch that currently has no members of staff,
it is necessary to enter nulls for the staff details.
This is not allowed since staffID is the primary key.
1. eliminating redundant data (for example, storing the same data in more than
one table)
2. ensure data dependencies make sense (storing only relevant data in a table).
The database community has developed a series of guidelines for ensuring that
databases are normalized.
Normal Forms are numbered 1NF, 2NF, 3NF (up till 5!) from the lowest form
of normalization to the highest.
In practical applications, 1NF, 2NF, and 3NF are very common, 4NF is
occasionally used.
The first rule essentially say that we must not duplicate data within the same
row of a table
Assumptions:
1. Each manager has 1 or more employees
2. Each employee has exactly 1 manager
Anomalies:
Manager Employees
Brett
Sharon Peter
Ashley
Manager Employees
Brett
Sharon Peter
Ashley
Anomalies:
Table in 1NF:
Manager Employees
Sharon Brett
Sharon Peter
Sharon Ashley
Denoted as X → Y
Example:
• Be in the 1NF
• each non-key attribute in the relation must be functionally dependent
upon the entire primary key.
The following relation depicting beer sale table is in 1NF but not in 2NF
The Primary key in this table is composed of (Beer Name, Bar Name)
Normalizing it:
• Be in the 2NF
• Attributes are dependant only on the primary key.
i.e. Transitive dependence does not exist
To turn a relation into 3NF:
1. Move any transitive dependant attributes into a smaller (subset) table.
2. Again Use the primary key as a foreign key to connect the two tables
Not really..
Solution :
Codds Law:
The values in a row are dependent on the key, the whole key, and nothing but
the key.
The aim is to derive a database structure where Codd’s law applies to every
table
CourseAssignment
(Student-ID,Name) Course Room Building Lecturer
{(1,Bernd),(2,Birte),(3,Bjrn)} P G1 HPH LD
{(2,Birte),(4,Karin),(5,Jo)} ET E1 HPH WK
{(2,Birte),(4,Karin)} RT H44 ML LG
Important: Assume that each course is taking place in a single room and that a
single lecturer is teaching the course.
1. Write down all dependencies (after 1NF), use the given assumption and
common sense.
2. Identify the primary key, good candidate will be attributes which are not
dependant on any other attribute
3. 2NF: Seperate into smaller tables according to the dependencies.
s.t in each table all attributes depends on the key
4. List dependencies again, are there any transitive dependencies? fix it for
3NF
5. In each normalization step, are there update/insert/delete anomalies?