Professional Documents
Culture Documents
Lec 7 - IDB
Lec 7 - IDB
Lec 7 - IDB
Introduction to
Database Systems
Mohammad Imran
Lecturer
Department of Information Technology
Balochistan University of Information Technology, Engineering & Management Sciences
Introduction to Database Systems Spring 2015 Mohammad Imran June 17, 2015
Balochistan University of Information Technology, Engineering & Management Sciences
Lecture 7
Database Normalization
Introduction to Database Systems Spring 2015 Mohammad Imran June 17, 2015 2
Balochistan University of Information Technology, Engineering & Management Sciences
Keys
• Before diving into Normalization, lets first clear some
concepts of Keys in Database
o Candidate Key
o Primary Key
o Foreign Key
o Alternate Key
o Composite Key
o Prime Attribute
o Non-Prime Attribute
Introduction to Database Systems Spring 2015 Mohammad Imran June 17, 2015 3
Balochistan University of Information Technology, Engineering & Management Sciences
Candidate Key
• Keys which are candidates for primary key of a table
• Type of keys which full fill all the requirements of primary
key
• So, the Key which is not null and have unique records is a
candidate for primary key
• Every table must have at least one candidate key but at
the same time can have several
• Example: STUDENT {SID, FNAME, LNAME, COURSEID}
o Candidate Keys: SID or FNAME+LNAME
Introduction to Database Systems Spring 2015 Mohammad Imran June 17, 2015 4
Balochistan University of Information Technology, Engineering & Management Sciences
Primary Key
• Type of candidate key which is chosen as a representing
key for table is known as primary key
• There is only one primary key per table
• Example: STUDENT {SID, FNAME, LNAME, COURSEID}
o Candidate Keys: SID or FNAME+LNAME
o Primary Key: SID (Chosen)
Introduction to Database Systems Spring 2015 Mohammad Imran June 17, 2015 5
Balochistan University of Information Technology, Engineering & Management Sciences
Foreign Key
• Keys which is used to define relationship between two tables
• When we want to implement relationship between two tables then we use
concept of foreign key also known as referential integrity
• There can be more than one foreign key per table
• Foreign key is generally a primary key from one table that appears as a
field in another where the first table has a relationship to the second
• Example: STUDENT {SID, FNAME, LNAME, COURSEID}
o Candidate Keys: SID or FNAME+LAME
o Primary Key: SID
o Foreign Key: COURSEID
Introduction to Database Systems Spring 2015 Mohammad Imran June 17, 2015 6
Balochistan University of Information Technology, Engineering & Management Sciences
Alternate Key
• If any table have more than one candidate key, then after
choosing primary key from those candidate key, rest of
candidate keys are known as an alternate key of that table
• Example: We have a table named Employee which has two
columns EmpID and EmpMail, both have not null attributes
and unique value
o So both columns are treated as candidate key
o We chosen and made EmpID as a primary key to that table
then EmpMail will be known as alternate key
Introduction to Database Systems Spring 2015 Mohammad Imran June 17, 2015 7
Balochistan University of Information Technology, Engineering & Management Sciences
Composite Key
• When we create keys on more than one column then that
key is known as composite key
• Example:
o I have a table Student which has two columns SID and
SRefNo and we make primary key on these two column
o Now SID & SRefNo known as composite key
Introduction to Database Systems Spring 2015 Mohammad Imran June 17, 2015 8
Balochistan University of Information Technology, Engineering & Management Sciences
Keys Identified
• Example: STUDENT {SID, FNAME, LNAME, COURSEID}
• Here in STUDENT table keys are: Candidate, Primary,
Foreign, Alternate, Composite
o Candidate keys are SID or FNAME+LNAME
o Primary Key: SID
o Foreign Key: COURSEID
o Alternate Key: FNAME+LNAME
o Composite Key: FNAME+LNAME
Introduction to Database Systems Spring 2015 Mohammad Imran June 17, 2015 9
Balochistan University of Information Technology, Engineering & Management Sciences
Anomalies
• An anomaly is an inconsistent, incomplete, or
contradictory state of the database
• Insertion anomaly – user is unable to insert a new record when it
should be possible to do so
• Deletion anomaly – when a record is deleted, other information
that is tied to it is also deleted
• Update anomaly –a record is updated, but other appearances
of the same items are not updated
Introduction to Database Systems Spring 2015 Mohammad Imran June 17, 2015 11
Balochistan University of Information Technology, Engineering & Management Sciences
Introduction to Database Systems Spring 2015 Mohammad Imran June 17, 2015 12
Balochistan University of Information Technology, Engineering & Management Sciences
Introduction to Database Systems Spring 2015 Mohammad Imran June 17, 2015 13
Balochistan University of Information Technology, Engineering & Management Sciences
Introduction to Database Systems Spring 2015 Mohammad Imran June 17, 2015 14
Balochistan University of Information Technology, Engineering & Management Sciences
Introduction to Database Systems Spring 2015 Mohammad Imran June 17, 2015 15
Balochistan University of Information Technology, Engineering & Management Sciences
Introduction to Database Systems Spring 2015 Mohammad Imran June 17, 2015 16
Balochistan University of Information Technology, Engineering & Management Sciences
Database Normalization
• Normalization is a process for evaluating and correcting
table structures to minimize data redundancies, thereby
reducing the likelihood of data anomalies
Introduction to Database Systems Spring 2015 Mohammad Imran June 17, 2015 17
Balochistan University of Information Technology, Engineering & Management Sciences
Introduction to Database Systems Spring 2015 Mohammad Imran June 17, 2015 18
Balochistan University of Information Technology, Engineering & Management Sciences
Results of Normalization
• Reduce data redundancy
• Reduce chances of data becoming inconsistent
• A table is said to be normalized if it satisfies certain
constraints
Introduction to Database Systems Spring 2015 Mohammad Imran June 17, 2015 19
Balochistan University of Information Technology, Engineering & Management Sciences
Normal Forms
• A series of guidelines for ensuring that databases are normalized
referred to as normal forms
• Numbered from one (the lowest form of normalization, referred to
as first normal form or 1NF) through five (fifth normal form or 5NF)
• For most purposes in business database design, 3NF is as high as
you need to go in the normalization process
• So, We will discuss 1NF, 2NF, and 3NF
• Remember! 2NF is better than 1NF, and 3NF is better than 2NF
means every level is better than its previous level
Introduction to Database Systems Spring 2015 Mohammad Imran June 17, 2015 20
Balochistan University of Information Technology, Engineering & Management Sciences
Introduction to Database Systems Spring 2015 Mohammad Imran June 17, 2015 21
Balochistan University of Information Technology, Engineering & Management Sciences
Denormalization
• Denormalization produces a lower normal form; that is, a
3NF will be converted to a 2NF through Denormalization.
• Price for increased performance through Denormalization:
o greater data redundancy
Introduction to Database Systems Spring 2015 Mohammad Imran June 17, 2015 22
Balochistan University of Information Technology, Engineering & Management Sciences
Introduction to Database Systems Spring 2015 Mohammad Imran June 17, 2015 23
Balochistan University of Information Technology, Engineering & Management Sciences
Before 1NF
• In First Normal Form, any
row must not have a Student Age Subject
column in which more
than one value is saved, Biology,
Shahid 15
like separated with Maths
commas. Rather than
Talha 14 Maths
that, we must separate
such data into multiple
Usama 17 Maths
rows.
Introduction to Database Systems Spring 2015 Mohammad Imran June 17, 2015 24
Balochistan University of Information Technology, Engineering & Management Sciences
After 1NF
• Repeating groups split and PK
is also defined as Composite
Key (Student + Subject) Student Subject Age
Shahid Biology 15
• Using the First Normal Form,
data redundancy increases, Shahid Maths 15
as there will be many Talha Maths 14
columns with same data in Usama Maths 17
multiple rows but each row as
a whole will be unique
Introduction to Database Systems Spring 2015 Mohammad Imran June 17, 2015 25
Balochistan University of Information Technology, Engineering & Management Sciences
Functional Dependency
• Before moving on to 2NF, lets see what functional
dependency is
• Functional Dependency means that the value of one or
more attributes determines the value of one or more
other attributes
• The standard notation for representing the relationship
between STU_NUM and STU_LNAME is:
o CMSID → STU_LNAME
Introduction to Database Systems Spring 2015 Mohammad Imran June 17, 2015 26
Balochistan University of Information Technology, Engineering & Management Sciences
Introduction to Database Systems Spring 2015 Mohammad Imran June 17, 2015 27
Balochistan University of Information Technology, Engineering & Management Sciences
Introduction to Database Systems Spring 2015 Mohammad Imran June 17, 2015 28
Balochistan University of Information Technology, Engineering & Management Sciences
Introduction to Database Systems Spring 2015 Mohammad Imran June 17, 2015 30
Balochistan University of Information Technology, Engineering & Management Sciences
Transitive Dependency
• When a non-key attribute determines another non-key
attribute, it is called Transitive Dependency Transitive
Dependency
Transitive Dependency
• Consider Relation Customer
• Primary Key Emp_ID determinaes F_Name, L_Name & Dept_ID
• But Dept_ID (Non Key Attribute) determines Dept_Name (another
non key attribute)
• So Transitive Dependency Exists
• We say that A → B → C, So A → C Transitive
Dependency
Introduction to Database Systems Spring 2015 Mohammad Imran June 17, 2015 34
Balochistan University of Information Technology, Engineering & Management Sciences
Subject}
• Age of Student only
Student Subject Age
depends on Student Shahid Biology 15
column (Partial Dependency), Shahid Maths 15
which is incorrect as per Talha Maths 14
Second Normal Form Usama Maths 17
Introduction to Database Systems Spring 2015 Mohammad Imran June 17, 2015 35
Balochistan University of Information Technology, Engineering & Management Sciences
Introduction to Database Systems Spring 2015 Mohammad Imran June 17, 2015 36
Balochistan University of Information Technology, Engineering & Management Sciences
Introduction to Database Systems Spring 2015 Mohammad Imran June 17, 2015 38
Balochistan University of Information Technology, Engineering & Management Sciences
Introduction to Database Systems Spring 2015 Mohammad Imran June 17, 2015 39
Balochistan University of Information Technology, Engineering & Management Sciences
Introduction to Database Systems Spring 2015 Mohammad Imran June 17, 2015 42
Balochistan University of Information Technology, Engineering & Management Sciences
Introduction to Database Systems Spring 2015 Mohammad Imran June 17, 2015 43
Balochistan University of Information Technology, Engineering & Management Sciences
Introduction to Database Systems Spring 2015 Mohammad Imran June 17, 2015 45
Balochistan University of Information Technology, Engineering & Management Sciences
Introduction to Database Systems Spring 2015 Mohammad Imran June 17, 2015 46
Balochistan University of Information Technology, Engineering & Management Sciences
Introduction to Database Systems Spring 2015 Mohammad Imran June 17, 2015 47
Balochistan University of Information Technology, Engineering & Management Sciences
Student Table
Address Table
Zip city state
Introduction to Database Systems Spring 2015 Mohammad Imran June 17, 2015 48
Balochistan University of Information Technology, Engineering & Management Sciences
Introduction to Database Systems Spring 2015 Mohammad Imran June 17, 2015 49
Balochistan University of Information Technology, Engineering & Management Sciences
References
• Database Systems, Design Implementation &
Management 10/11th Edition (Chapter 6)
Introduction to Database Systems Spring 2015 Mohammad Imran June 17, 2015 50
Balochistan University of Information Technology, Engineering & Management Sciences
Thank you
51
Introduction to Database Systems Spring 2015 Mohammad Imran June 17, 2015