Академический Документы
Профессиональный Документы
Культура Документы
NORMALIZATION
(LOGICAL DATABASE DESIGN)
M. Ehtasham Ul Haq
1
RELATION
A relation is a named, two-dimensional table of data.
A table consists of rows (records) and columns (attributes or
fields).
Requirements for a table to qualify as a relation:
It must have a unique name.
Every attribute value must be atomic (not multivalued, not composite).
Every row must be unique (two rows can’t have exactly same values).
Attributes (columns) in tables must have unique names.
The order of the columns must be irrelevant.
The order of the rows must be irrelevant.
2
CORRESPONDENCE WITH E-R MODEL
3
KEY FIELDS
Keys are special fields that serve two main
purposes:
Primary keys. Unique identifiers of the relation.
Examples include employee numbers, social security
numbers, etc. This guarantees that all rows are
unique.
Foreign keys .Identifiers that enable a dependent
relation (on the many side of a relationship) to refer to
its parent relation (on the one side of the
relationship).
Serves as a primary key of another relation
Keys can be simple (a single field) or composite
(more than one field).
4
Schema for four relations
Primary Key
Foreign Key
(implements 1:N relationship
between customer and order)
5
INTEGRITY CONSTRAINTS
Domain Constraints
All value must be from the same domain (allowable values)
Domain is a collection of all possible values of one or more attributes
Entity Integrity
No primary key attribute can be null. All primary key fields
MUST have data
6
INTEGRITY CONSTRAINTS
Referential Integrity–rule states that any foreign key
value (on the relation of the many side) MUST match a
primary key value in the relation of the one side. (Or the
foreign key can be null)
For example: Delete Rules
Restrict–don’t allow delete of “parent” side if related rows
exist in “dependent” side
Cascade–automatically delete “dependent” side rows that
correspond with the “parent” side row to be deleted
Set-to-Null–set the foreign key in the dependent side to null
if deleting from the parent side
7
Referential integrity constraints
Referential
integrity
constraints are
drawn via arrows
from dependent to
parent table
8
TERMINOLOGIES
Null is a value that may be assigned to an attribute
when no other value applies or when the applicable
value is unknown
Well-Structure Relation contains minimal redundancy
and allows users to insert, modify and delete rows in
a table without errors
Anomaly: An error or inconsistency that may result
when a user attempts to update a table that contains
redundant data
Surrogate Primary key: A serial number or other
system-assigned primary key for a relation
9
TRANSFORMING EER DIAGRAMS INTO
RELATIONS
Mapping Regular Entities to Relations
Simple attributes: E-R attributes map
directly onto the relation
Composite attributes: Use only their
simple, component attributes
Multivalued Attribute: Becomes a
separate relation with a foreign key
taken from the superior entity
10
Mapping a regular entity
(a) CUSTOMER
entity type with
simple
attributes
11
Mapping a composite attribute
(a) CUSTOMER
entity type with
composite
attribute
12
Mapping an entity with a multivalued attribute
(a)
(b)
13
TRANSFORMING EER DIAGRAMS INTO
RELATIONS (CONT.)
Mapping Weak Entities
Becomes a separate relation with a foreign
key taken from the superior entity
Primary key composed of:
Partial
identifier of weak entity
Primary key of identifying relation (strong entity)
14
Example of mapping a weak entity
a) Weak entity DEPENDENT
17
Example of mapping an M:N relationship
a) Completes relationship (M:N)
19
TRANSFORMING EER DIAGRAMS INTO
RELATIONS (CONT.)
Mapping Associative Entities
Identifier Not Assigned
Default primary key for the associative
relation is composed of the primary keys of
the two entities (as in M:N relationship)
Identifier Assigned
It
is natural and familiar to end-users
Default identifier may not be unique
20
Example of mapping an associative entity
a) An associative entity
21
Example of mapping associative entity with identifier
a) SHIPMENT associative entity
22
TRANSFORMING EER DIAGRAMS INTO
RELATIONS (CONT.)
Mapping Unary Relationships
One-to-Many–Recursive foreign key in the
same relation
Many-to-Many–Two relations:
(a) EMPLOYEE
entity with unary
relationship
24
Mapping a unary M:N relationship
25
TRANSFORMING EER DIAGRAMS INTO
RELATIONS (CONT.)
Mapping Ternary Relationships
One relation for each entity and one for
the associative entity
Associative entity has foreign keys to
each entity in the relationship
26
Mapping a ternary relationship
a) PATIENT TREATMENT Ternary relationship with associative entity
Treatment date and time are included to make unique composite key
But this makes a very cumbersome key…
It would be better to create a Surrogate key like Treatment#
27
TRANSFORMING EER DIAGRAMS
INTO RELATIONS (CONT.)
Mapping Supertype/Subtype Relationships
One relation for supertype and for each subtype
Supertype attributes (including identifier and subtype
discriminator) go into supertype relation, Subtype
attributes go into each subtype relation
primary key of supertype relation also becomes
primary key of subtype relation
1:1 relationship established between supertype and
each subtype, with supertype as primary table
28
Supertype/subtype relationships
29
Mapping supertype/subtype relationships to relations
30
TRANSLATE ER-DIAGRAM INTO RELATIONAL SCHEMA
33
Steps in normalization
34
Table with multivalued attributes, not in 1st normal form
35
FIRST NORMAL FORM
No multivalued attributes (atomic value)
All rows should be unique
No multivalued attributes and unique rows, its in 1st normal form
36
ANOMALIES IN THIS TABLE
Insertion–if new product is ordered for order 1007 of
existing customer, customer data must be re-entered,
causing duplication
Deletion–if we delete the Dining Table from Order
1006, we lose information concerning this product like
price
Update–changing the price of product ID 4 requires
update in multiple records
38
CANDIDATE KEYS
Candidate Key: An attribute that uniquely
identifies a row in a relation
A unique identifier.
One of the candidate keys will become the
primary key
e.g. if both credit card number and SS# are present
in a table then both are candidate keys
Each non-key field is functionally dependent on
every candidate key
39
Functional dependency diagram for INVOICE
40
SECOND NORMAL FORM
1NF PLUS every non-key attribute is fully
functionally dependent on the ENTIRE primary
key
Every non-key attribute must be defined by the
entire key, not by only part of the key
No partial functional dependencies
41
Removing partial dependencies
Getting it into
Second Normal Form
42
EXAMPLE EXERCISE
Accountant Skill Proficiency Skill Account Accountant Group Group Group
Number Number Category Name Age Number City Supervisor
21 113 3 Systems Waseem 30 52 GJR Ali
35 113 5 Systems Ehtasham 31 44 LHR Naeem
50 179 1 Tax Saeed 40 44 LHR Naeem
77 204 6 Audit Asad 35 52 GJR Ali
Which Functional Dependency should be eliminated?
Convert it into 2NF
Accountant Account Accountant Group Group City Group Skill Skill
Number Name Age Number Supervisor Number Category
21 Waseem 30 52 GJR Ali 113 Systems
35 Ehtasham 31 44 LHR Naeem 179 Tax
50 Saeed 40 44 LHR Naeem 204 Audit
77 Asad 35 52 GJR Ali
44
Removing transitive dependencies
Getting it into
Third Normal
Form
45
RELATIONAL MODEL
Presented by E.F. Codd in 1970
In Relational model, data is stored in relations.
46
EXERCISE
CustomerInvoice (InvoiceID, CustomerID, CustomerName, CustomerAddress)
47
EXAMPLE EXERCISE
Accountant Account Accountant Group Group City Group
Number Name Age Number Supervisor
Relations in 3NF
48