Download as pdf or txt
Download as pdf or txt
You are on page 1of 34

Inf2 Tutorial 8

Sharon Wulff

April 23, 2009

Sharon Wulff Inf2 Tutorial 8


Databases

• Beginning of this field - end of the sixties


• Huge increase in the amount of data stored in computers,
⇒ need for an efficient and convenient data storage
• This led to a rapid development of databases systems.

Sharon Wulff Inf2 Tutorial 8


The Relational Database Model

See Relational model definitions from the lecture slides!

Sharon Wulff Inf2 Tutorial 8


Primary Key

Each relational database table must have a field or a combination of fields that
holds a value that uniquely identifies each record.

For example, a customer number would uniquely identify each customer.

The primary key serves several purposes in a RDBMS.


• Ensure that there are no duplicate records in the database - each value is
unique.
• Establish table relationships and, thereby, connect the data in related
records held in different tables
• Records are stored in order by the primary key

Sharon Wulff Inf2 Tutorial 8


Primary Key II

Primary key has several properties:

• are never null (Null means a missing or unknown value)


• never change
• includes as few fields as possible
• all non-key attributes of the relation functionally depend on it

In practice, the primary key is usually an automatically assigned sequential


number (by RDBMS) with no real-world meaning.

Sharon Wulff Inf2 Tutorial 8


Identifying The Primary Key

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

2. Map all of the functional dependencies (??) between the attributes,


Good candidates are attributes such that all other attributes are
dependant upon.

(Functional dependencies.. coming soon!)

Sharon Wulff Inf2 Tutorial 8


Composite Key

Def. A composite key is a primary key that consists of more than one attribute
(that have to be unique as a tuple).

Example: File permission system

Sharon Wulff Inf2 Tutorial 8


Foreign Key

Def. A foreign key is an attribute (possibly composite) in a relation of a


database that serves as the primary key of another relation in the same
database.

A foreign key is used when we must represent the relationship between two
tables

Sharon Wulff Inf2 Tutorial 8


Database Update Anomalies

The Problems resulting from data redundancy in an un-normalized database


table are known as databases anomalies.

Three different kind of anomalies:

1. insertion
2. deletion
3. update
All might result in an inconsistent state of the database.

Consider the above Staff-Branch table

Sharon Wulff Inf2 Tutorial 8


Database Anomalies - Insertion Example

• To insert the details of a new member of staff located at branch B1,


we must enter the correct details of branch number B1
so that the branch details are consistent with the values
for branch B1 in other rows.

• 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.

Sharon Wulff Inf2 Tutorial 8


Database Anomalies - Deletion Example

• If we delete a row that represents the last member of staff located at a


certain branch, the details about that branch are also lost from the
Database.

Sharon Wulff Inf2 Tutorial 8


Database Anomalies - Update Example

• In to change the address of a particular branch, we must update the rows


of all staff located at that branch.
If this modification is not carried out on all the relevant rows, the database
will become inconsistent.

Sharon Wulff Inf2 Tutorial 8


Database Anomalies - Solution!

Break relation Tbl-Staff-Branch into two smaller relations:


Tbl-Staff & Tbl-Branch.

Target: Find a decomposition of the relation such that there are


no more data anomalies.

Sharon Wulff Inf2 Tutorial 8


Normalizing Databases

Normalization is the process of efficiently organizing data in a database and


reducing design flaws (data anomalies)

Two goals of normalization:

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).

In practice, normalizing your tables drastically


improve the performance of your DBMS!

Sharon Wulff Inf2 Tutorial 8


Normalizing Databases II

The database community has developed a series of guidelines for ensuring that
databases are normalized.

These are referred to as: Normal Forms

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.

Sharon Wulff Inf2 Tutorial 8


First Normal Form

1NF sets the very basic rules for an organized database:

1. Eliminate duplicative columns from the same table


2. No multi-valued columns (e.g. attribute Lang for people knowing more
than one language)

The first rule essentially say that we must not duplicate data within the same
row of a table

Sharon Wulff Inf2 Tutorial 8


1NF Example

Scenario: Human resource table which depicts Manager-Employee relationship

Assumptions:
1. Each manager has 1 or more employees
2. Each employee has exactly 1 manager

Sharon Wulff Inf2 Tutorial 8


1NF Example II

Wrong solution #1:

Manager Employee1 Employee2 Employee3 Employee4


Sharon Brett Peter Ashley ?

Sharon Wulff Inf2 Tutorial 8


1NF Example II

Wrong solution #1:

Manager Employee1 Employee2 Employee3 Employee4


Sharon Brett Peter Ashley ?

Anomalies:

• Case the manager only has 1 employee


Employee1-Employee4 are simply wasted storage space
• Case the Manager already has 4 employees and she wants to hire 1 more..

The first rule of 1NF - duplicate columns is violated


⇒ the table is not in 1NF

Sharon Wulff Inf2 Tutorial 8


1NF Example III

Wrong solution #2:

Manager Employees
Brett
Sharon Peter
Ashley

Sharon Wulff Inf2 Tutorial 8


1NF Example III

Wrong solution #2:

Manager Employees
Brett
Sharon Peter
Ashley

Anomalies:

• To remove an employee we have to go over the entire list..


Now what happens if Sharon manages 1,000 people?!
The second rule of 1NF - no multi-valued columns is violated
⇒ the table is not in 1NF

Sharon Wulff Inf2 Tutorial 8


1NF Example IV

Table in 1NF:

Manager Employees
Sharon Brett
Sharon Peter
Sharon Ashley

Sharon Wulff Inf2 Tutorial 8


Functional Dependency

Def. Given a relation R, a set of attributes X in R is said to functionally


determine another attribute Y, if and only if each X value is associated with
precisely one Y value.

Denoted as X → Y

Example:

System of track vehicles and the capacity of their engines:


• Primary key: Tid (Truck unique id)
• The engine capacity is dependant on the Tid -
Each truck has exactly one engine capacity.
• Capacity does not depend on the Tid -
engine capacity can be the same for several vehicles.

Tid → engine capacity

engine capacity 9 Tid


Sharon Wulff Inf2 Tutorial 8
Second Normal Form

For a relation to be in the 2NF it has to fulfill:

• Be in the 1NF
• each non-key attribute in the relation must be functionally dependent
upon the entire primary key.

To turn a relation into 2NF:


1. Move any non-dependent attributes into a smaller (subset) table.
2. Use the primary key as a foreign key to connect the two relations.

Sharon Wulff Inf2 Tutorial 8


2NF Example

The following relation depicting beer sale table is in 1NF but not in 2NF

Beer Name Bar Name Bar Address Total


Heineken The sketchy fox 10 nowhere alley $134.23
Carlsberg The sketchy fox 10 nowhere alley $521.24
Heineken The nasty eagle 21 dark side st. $1042.42
Heineken The dodgy donkey 3b. smelly cr. $928.53

Can you figure out why?

Sharon Wulff Inf2 Tutorial 8


2NF Example II

The Primary key in this table is composed of (Beer Name, Bar Name)

Total is indeed dependent upon (Beer Name, Bar Name),


the total amount is specific for each Beer and Bar.

(Beer Name, Bar Name) → {Total}

However, Bar Address is dependent upon Bar Name only.

Sharon Wulff Inf2 Tutorial 8


2NF Example III

Normalizing it:

Beer Name Bar Name Total


Heineken The sketchy fox $134.23
Carlsberg The sketchy fox $521.24
Heineken The nasty eagle $1042.42
Heineken The dodgy donkey $928.53

Bar Name Bar Address


The sketchy fox 10 nowhere alley
The sketchy fox 10 nowhere alley
The nasty eagle 21 dark side st.
The dodgy donkey 3b. smelly cr.

Sharon Wulff Inf2 Tutorial 8


Third Normal Form
Def. Transitive dependence: an attribute is functionally dependent on
attributes that are not a key

For a relation to be in the 3NF it has to fulfill:

• 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

Sharon Wulff Inf2 Tutorial 8


3NF Example

Consider the following Orders table,


Is the following table in 3NF?

Order No. Customer ID Customer Name Contact Person Total


1 7 Acme Widgets John Doe $834.23
2 9 ABC Corporation Fred Flintstone $85921.24
3 7 Acme Widgets John Doe $134.42
4 7 Acme Widgets John Doe $2258.53

Sharon Wulff Inf2 Tutorial 8


3NF Example II

Not really..

In Orders, Order No. is the primary key,


Customer Name and Contact Person however, are dependant upon Customer
ID.

Customer ID → {Customer Name, Contact Person}

Sharon Wulff Inf2 Tutorial 8


3NF Example III

Solution :

Order No. Customer ID Total


1 7 $834.23
2 9 $85921.24
3 7 $134.42
4 7 $2258.53

Customer ID Customer Name Contact Person


7 Acme Widgets John Doe
9 ABC Corporation Fred Flintstone
7 Acme Widgets John Doe
7 Acme Widgets John Doe

Sharon Wulff Inf2 Tutorial 8


From The Lecture (and important!)

Codds Law:

The values in a row are dependent on the key, the whole key, and nothing but
the key.

2NF: whole key

3NF: nothing but the key

The aim is to derive a database structure where Codd’s law applies to every
table

Sharon Wulff Inf2 Tutorial 8


Home Work 8.2

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. Transform the given table to the first normal form


2. Are there any additional functional dependencies on parts of the keys?
Transform the table to the second normal form.
3. List all the functional dependencies and transform the table to the third
normal form.
4. Which anomalies are possible despite the normalization steps

Sharon Wulff Inf2 Tutorial 8


Home Work 8.2 Key Steps

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?

Sharon Wulff Inf2 Tutorial 8

You might also like