Physical Database Design Lecture Slides
Physical Database Design Lecture Slides
Introduction
● The relational data model was first introduced in 1970 by E. F. Codd, then
of IBM (Codd, 1970).
● The relational data model represents data in the form of tables.
● The relational model is based on mathematical theory and therefore has a
solid theoretical foundation.
Relational Data Model
● Composite key: A primary key that consists of more than one attribute.
● For example, the primary key for a relation DEPENDENT would likely
consist of the combination EmpID and DependentName.
Relational Keys
A textual schema for four relations at Pine Valley Furniture Company is shown.
Sample Database
● All of the values that appear in a column of a relation must be from the
same domain.
● A domain is the set of values that may be assigned to an attribute.
● A domain definition usually consists of the following components: domain
name, meaning, data type, size (or length), and allowable values or
allowable range (if applicable).
Domain Constraints
Entity Integrity
● The entity integrity rule is designed to ensure that every relation has a
primary key and that the data values for that primary key are all valid.
● In particular, it guarantees that every primary key attribute is non-null.
● The entity integrity rule states the following:
Either each foreign key value must match a primary key value in another
relation or the foreign key value must be null.
Referential Integrity
● The graphical version of the relational schema provides a simple technique for
identifying associations where referential integrity must be enforced.
● An arrow has to be drawn from each foreign key to the associated primary key.
● A referential integrity constraint must be defined for each of these arrows in the
schema using SQL.
Referential Integrity
● If each order must have a customer (a mandatory relationship), then the foreign
key CustomerID cannot be null in the ORDER relation.
● If the relationship is optional, then the foreign key could be null. Whether a
foreign key can be null must be specified as a property of the foreign key
attribute when the database is defined.
Well-Structured Relation
● An error or inconsistency that may result when a user attempts to update a table
that contains redundant data.
● The three types of anomalies are insertion, deletion, and modification
anomalies.
Insertion Anomaly
● Suppose that the data for employee number 140 are deleted from the table.
● This will result in losing the information that this employee completed a course
(Tax Acc) on 12/8/2015.
● In fact, it results in losing the information that this course had an offering that
completed on that date
Modification Anomaly
● During logical design, you transform the E-R (and EER) diagrams that were
developed during conceptual design into relational database schemas.
● The inputs to this process are the entity-relationship (and enhanced E-R)
diagrams.
● The outputs are the relational schemas.
● In the upcoming slides, we discuss the steps in the transformation of E-R (and
EER) diagram into relational schema.
Step 1: Mapping Regular Entities
● The first of these attributes is the primary key from the first relation, which
becomes a foreign key in the second relation. The second is the multivalued
attribute.
● The name of the second relation should capture the meaning of the
multivalued attribute.
● If an entity type contains multiple multivalued attributes, each of them will be
converted to a separate relation.
Step 1: Mapping Regular Entities
Step 2: Mapping Weak Entities
● Recall that a weak entity type does not have an independent existence but
exists only through an identifying relationship with another entity type called
the owner.
● A weak entity type does not have a complete identifier but must have an
attribute called a partial identifier that permits distinguishing the various
occurrences of the weak entity for each owner entity instance.
Step 2: Mapping Weak Entities
● For each weak entity type, create a new relation and include all of the simple
attributes (or simple components of composite attributes) as attributes of this
relation.
● Then include the primary key of the identifying relation as a foreign key
attribute in this new relation.
● The primary key of the new relation is the combination of this primary key of
the identifying relation and the partial identifier of the weak entity type.
Step 2: Mapping Weak Entities
● Include the primary key attribute (or attributes) of the entity on the one-side
of the relationship as a foreign key in the relation that is on the many-side of
the relationship.
Map Binary One-to-One Relations
● The first step is to create three relations: one for each of the two participating
entity types and a third for the associative entity. We refer to the relation
formed from the associative entity as the associative relation.
● The second step then depends on whether on the E-R diagram an identifier
was assigned to the associative entity
Mapping Associative Entities
● Identifier not Assigned: If an identifier was not assigned, the default primary
key for the associative relation is a composite key that consists of the two
primary key attributes from the other two relations. These attributes are then
foreign keys that reference the other two relations
Mapping Associative Entities
● With this type of relationship, two relations are created: one to represent the
entity type in the relationship and an associative relation to represent the M:N
relationship itself.
● The primary key of the associative relation consists of two attributes. These
attributes (which need not have the same name) both take their values from
the primary key of the other relation.
● Any non-key attribute of the relationship is included in the associative
relation.
Step 6: Map Ternary Relationships
● Create a separate relation for the supertype and for each of its subtypes.
● Assign to the relation created for the supertype the attributes that are common
to all members of the supertype, including the primary key.
● Assign to the relation for each subtype the primary key of the supertype and
only those attributes that are unique to that subtype.
● Assign one (or more) attributes of the supertype to function as the subtype
discriminator.
DIY: Convert ERD to Relational
Schema
● Following the steps outlined previously for transforming EER diagrams into
relations typically results in well-structured relations.
● However, there is no guarantee that all anomalies are removed by following
these steps.
● Normalization is a formal process for deciding which attributes should be
grouped together in a relation so that all anomalies are removed.
Keys in Database
● Super Key
● Candidate Key
● Primary Key
● Composite Key
The types of keys present in each table can help identify dependencies that are
either beneficial or detrimental to the normalization of a database.
Dependency
Table: Employee
EmpID EmpName EmpAge
E01 Ali 28
E02 Asif 31
E03 Ali 24
Table: Student_Result
1 121 Science 80
1 131 Math 65
2 131 Math 95
2 141 English 75
Partial Dependency
● If the value of a non-primary attribute other than the candidate key can be
defined using another non-primary attribute then it is called a transitive
dependency.
● When any attribute does not require primary key and can easily get value
using another non-primary attribute then it is called as Transitive
Dependency.
● A →B , B → C then C is transitively dependent on A
Transitive Dependency
Student
A relation is in first normal form (1NF) if the following two constraints both
apply:
1. There are no repeating groups in the relation (thus, there is a single fact at
the intersection of each row and column of the table).
2. A primary key has been defined, which uniquely identifies each row in the
relation.
Step 1: Convert to First Normal Form
● Although repeating groups have been removed, the data still contain
considerable redundancy.
● For example, CustomerID, CustomerName, and CustomerAddress for Value
Furniture are recorded in three rows (at least) in the table.
● As a result of these redundancies, manipulating the data in the table can lead
to anomalies such as the following:
Anomalies in 1NF
● Insertion anomaly:
1. With this table structure, the company is not able to introduce a new product
(say, Breakfast Table with ProductID 8) and add it to the database before it is
ordered the first time: No entries can be added to the table without both
ProductID and OrderID.
2. As another example, if a customer calls and requests another product be
added to his OrderID 1007, a new row must be inserted in which the order
date and all of the customer information must be repeated. This leads to data
replication and potential data entry errors (e.g., the customer name may be
entered as “Valley Furniture”).
Anomalies in 1NF
● Deletion anomaly:
If a customer calls and requests that the Dining Table be deleted from her
OrderID 1006, this row must be deleted from the relation, and we lose the
information concerning this item’s finish (Natural Ash) and price ($800.00).
Anomalies in 1NF
● Update Anomaly:
If Pine Valley Furniture (as part of a price adjustment) increases the price of
the Entertainment Center (ProductID 4) to $750.00, this change must be
recorded in all rows containing that item.
Functional Dependency diagram for INVOICE
Step 2: Convert to Second Normal Form
1. Create a new relation for each primary key attribute (or combination of
attributes) that is a determinant in a partial dependency. That attribute is the
primary key in the new relation.
2. Move the nonkey attributes that are only dependent on this primary key
attribute (or attributes) from the old relation to the new relation.
Step 2: Convert to Second Normal Form
Step 3: Convert to Third Normal Form
Figure shows the performance ranking for Motion Bank. Convert this user view to
a set of 3NF relations using step by step normalization. Assume the following:
1. A department can be located on multiple branches. A branch can have
multiple departments.
2. An employee works at one branch in one department.
DIY-2
DIY-2