Documente Academic
Documente Profesional
Documente Cultură
CSC-221
Instructor
Malik Khizar Hayat
Faculty of Engineering and Information Technology
Department of Software Engineering
Foundation University Rawalpindi Campus
4
Logical Database Design
and the Relational Model
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
The Relational Data Model 4
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
The Relational Data Model 5
• Components
• Data Structure
• Data organization in the form of tables
• Data Manipulation
• Operations (using SQL) performed on the data
• Data Integrity
• Accuracy and consistency of data while manipulation
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
The Relational Data Model 6
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
The Relational Data Model 7
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
The Relational Data Model 8
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
The Relational Data Model 9
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
The Relational Data Model 10
• Properties of Relations
1. Each relation (or table) in a database has a unique name
2. Attribute values must be atomic – not multi-valued
3. Each row in a relation is unique
4. Each attribute (column) within a table has a unique name
5. Sequence of columns (left to right) is insignificant
6. Sequence of rows (top to bottom) is insignificant
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
The Relational Data Model 11
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
The Relational Data Model 12
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
The Relational Data Model 13
• Schema
• Description of a particular data collection using a given data model
• Database Schema
• Description of the overall logical structure of the database
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
The Relational Data Model 14
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
The Relational Data Model 15
• Schema – An Example
• Relations – CUSTOMER, ORDER, ORDER LINE, PRODUCT
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
Integrity Constraints 16
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
Integrity Constraints 17
• Domain Constraints
• Domain is a set of values that may be assigned to an attribute
• All values that appear in a column must be from the same domain
• Following are the components of the domain definition:
• Domain name
• Meaning
• Data type
• Size or length
• Allowable values or range (if applicable)
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
Integrity Constraints 18
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
Integrity Constraints 19
• Entity Integrity
• It is to ensure that every relation has a primary key
• Every primary key attribute is NOT NULL
• An attribute cannot be a primary key for which – the values are
• Not applicable
• Unknown
Entity Integrity Rule Indicates
absence of a value
No primary key attribute
or component of a primary key Neither numeric 0
may be NULL nor empty string
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
Integrity Constraints 20
• Referential Integrity
• It is to maintain the consistency among rows of two relations
• Involves primary-foreign key associations among rows in relations
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
Integrity Constraints 21
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
Integrity Constraints 22
• Referential Integrity
• How to know – foreign key is allowed to be NULL?
• In a mandatory relationship – foreign key cannot be NULL
• In an optional relationship – foreign key can be NULL
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
Integrity Constraints 23
• Referential Integrity
• How to delete a foreign key related row in a relation?
• For instance, delete a row based on the relations – ORDER and CUSTOMER
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
Integrity Constraints 24
• Referential Integrity
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
Integrity Constraints 25
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
Integrity Constraints 26
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
Integrity Constraints 27
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
Integrity Constraints 28
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
Integrity Constraints 29
• Well-structured Relations
• A relation that contains minimal redundancy
• Allow users to insert, modify, and delete rows in a table
• Without errors or inconsistencies
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
Integrity Constraints 30
• Well-structured Relations
• Redundancy in a table may result in errors called anomalies
• Types of anomalies
1. Insertion anomaly
2. Deletion anomaly
3. Modification anomaly
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
Integrity Constraints 31
• Well-structured Relations
1. Insertion Anomaly
• Certain attributes cannot be inserted without presence of others attributes
2. Deletion Anomaly
• Certain attributes are lost because of deletion of other attributes
3. Modification Anomaly
• One or more instances of duplicated data updated, but not all
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
Transforming EER Model into Relations 32
Logical Relational
ER/EER
Design Schema
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
Transforming EER Model into Relations 33
• Database Modeling
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
Transforming EER Model into Relations 34
• Transformation Steps
1. Map Regular Entities
2. Map Weak Entities
3. Map Binary Relationships
4. Map Associative Entities
5. Map Unary Relationships
6. Map Ternary (and n-ary) Relationships
7. Map Supertype/Subtype Relationships
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
Transforming EER Model into Relations 35
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
Transforming EER Model into Relations 36
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
Transforming EER Model into Relations 37
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
Transforming EER Model into Relations 38
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
Transforming EER Model into Relations 39
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
Transforming EER Model into Relations 40
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
Transforming EER Model into Relations 41
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
Transforming EER Model into Relations 42
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
Transforming EER Model into Relations 43
A mnemonic
The primary key migrates to the many-side
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
Transforming EER Model into Relations 44
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
Transforming EER Model into Relations 45
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
Transforming EER Model into Relations 46
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
Transforming EER Model into Relations 47
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
Transforming EER Model into Relations 48
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
Transforming EER Model into Relations 49
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
Transforming EER Model into Relations 50
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
Transforming EER Model into Relations 51
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
Transforming EER Model into Relations 52
Identifier
Not Assigned
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
Transforming EER Model into Relations 53
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
Transforming EER Model into Relations 54
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
Transforming EER Model into Relations 55
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
Transforming EER Model into Relations 56
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
Transforming EER Model into Relations 57
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
Transforming EER Model into Relations 58
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
Transforming EER Model into Relations 59
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
Transforming EER Model into Relations 60
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
Transforming EER Model into Relations 61
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
Transforming EER Model into Relations 62
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
Normalization 63
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
Normalization 64
Normalization
A systematic approach of
decomposing relations into smaller ones
to eliminate data redundancy, dependency,
insertion, update, and deletion anomalies
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
Normalization 65
• Goals
• Minimize data redundancy – avoid anomalies, save storage space
• Simplify the enforcement of referential integrity constraints
• Make it easier to maintain data (insert, update, and delete)
• Provide better design – improved representation of the real world
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
Normalization 66
• Functional Dependency
• A constraint between two attributes in which the value of one
attribute is determined by the value of another attribute
• Determinant
• The attribute(s) used to determine the values assigned to other
attribute(s) in the same row
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
Normalization 67
• Functional Dependency
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
Normalization 68
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
Normalization 69
• Transitive Dependency
• A functional dependency between the primary key and one or
more non-key attributes that are dependent on the primary key
via another non-key attribute
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
Normalization 70
• Normal Form
• Normalization can be accomplished in stages
• Each stage corresponds to a normal form
Normal Form
A state of relation that
requires the certain rules are satisfied
regarding relationships between attributes
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
Normalization 71
• Steps
• First Normal Form (1NF)
• Relation has a primary key and does not contain repeating groups of attributes
• Second Normal Form (2NF)
• Relation is in 1NF and have no partial functional dependencies
• Third Normal Form (3-NF)
• Relation is in 2NF and have no transitive dependencies
• Boyce-Codd Normal Form (BCNF)
• Relation is in 3NF and no anomalies because of multiple candidate keys
• Fourth Normal Form (4NF)
• Relation is in BCNF and have no multi-valued dependencies
• Fifth Normal Form (5NF)
• Relation is in 4NF and have no projection join dependencies
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
Normalization 72
• Steps
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
Normalization 73
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
Normalization 74
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
Normalization 75
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
Normalization 76
• Convert to 1NF
• Remove the repeating groups
• Same as ‘removing multi-valued attributes’
• Select the primary key
• Non-redundant
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
Normalization 77
• Convert to 2NF
• Removing Partial Dependencies
1. Create a new relation for each primary key attribute(s) that is
determinant in a partial dependency
2. Move the non-key attributes that are dependent on the primary key
attribute(s) from the old relation to the new relation
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
Normalization 78
• Convert to 2NF
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
Normalization 79
• 1NF 2NF
• A relation in 1NF will be 2NF, if any one of the following applies:
1. Primary key attribute consists of only one attribute – no partial dependency
2. No non-key attributes exist in the relation – no functional dependency
3. Every non-key attribute is functionally dependent on full set of primary keys
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
Normalization 80
• Convert to 3NF
• Removing Transitive Dependencies
1. Create a new relation for each non-key attribute(s) that is determinant
in a relation. That attribute(s) becomes the primary key of the new
relation
2. Move all attributes that are functionally dependent on primary key of
the new relation from the old to the new relation
3. Leave the attribute that serves as a primary key in the new relation in
the old relation to serve as a foreign key
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
Normalization 81
• Convert to 3NF
• Removing Transitive Dependencies
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
Normalization 82
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
Merging Relations 83
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
Merging Relations 84
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
Merging Relations 85
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
Merging Relations 86
4 – Logical Database Design and the Relational Model Malik Khizar Hayat
87
Merging Relations 87
4 – Logical Database Design and the Relational Model Malik Khizar Hayat