You are on page 1of 35

EABS (Pune)

RDBMS

Relation model and normalization

26 Aug 2010

Zarir J Daruwalla
zarirdaru@hotmail.com

Version 0.1
Agenda

• Relation
• Functional Dependency
• Normalization
– 1NF, 2NF, 3NF, BCNF
• Other Normal Form
• Synthesis of Relations
• De-Normalization
• Controlled Redundancy

12/07/21 2
Relation

• Two Dimensional Table


• Cells of table can have only a single value
• All entries in a column must be of same type
• Each column has unique name
• Order of column is insignificant
• Order of rows is insignificant
• No two rows (ie value of all columns) may be
identical

12/07/21 3
Functional Dependency

• Relationship between attributes


• X Y (attribute Y is functionally dependent on
attribute X),
ie if value of Y is determined by value of X

• E.g.
– Emp No  Emp Name
– City  Country
– Building  Plot number,
– Emp No  (Emp Name, City, Country)

12/07/21 4
Functional dependency (example)

Emp No Emp Name City Country

10001 Vijay Singh New Delhi India


10010 Arun Thakur Mumbai India
10234 Suresh Rao Sidney Australia
23221 Meena Desai New York USA

12/07/21 5
Functional dependencies (Multiple attributes)

• Consider table
Student_Grade (Student ID, Course Name,
Grade)
(Student ID, Course Name)  Grade

Person_work_place (employee id, building


name, Street name, City, Country)

12/07/21 6
Functional dependencies (Keys and uniqueness)

• Every relation must have one key (based on


single or multiple attributes).

• A key functionally determines the entire row

12/07/21 7
Activity Entity

SID Activity Fee


100 Skiing 200
150 Swimming 50
175 Squash 50
200 Swimming 50

12/07/21 8
Relation Observations

• All relations are not desired.


• They suffer from Modification Anomalies
• Deletion Anomaly: If SID = 100 deleted, we loose
information that Skiing is an activity and the fee
for that is Rs. 200
• Insertion Anomaly: Unless student signs up for
‘Scuba’, one can not record that ‘Scuba’ activity
costs Rs. 500
• Updation: Changes in Fees of swimming needs
to be changed at both places
12/07/21 9
Relation Anomalies

SID Activity
• Problem is because Activity
Relation has two themes 100 Skiing
• Anomalies can be eliminated 150 Swimming
by redefining relation into two
or more relations 175 Squash
• One has to take care of 200 Swimming
Referential Integrity (While
Adding SID, check activity
record exists, while Deleting
Activity Fee
Activity, check Student record
does not exist) Skiing 200
Swimming 50
Squash 50
12/07/21 10
1 Normal Form (1NF)

• First normal form (1NF) – an entity whose


attributes have no more than one value for a
single instance of that entity.

• Thus any definition of Relation ensures 1NF !

12/07/21 11
1NF issues

Emp_name Project • Need to create separate


instance for each
Ramesh Payroll
– repeating value of Project

Ramesh Inventory • How do we find out all


projects on which
Mahesh Payroll • Ramesh is working?

Meena Accounting

Meena Performance

12/07/21 12
Second Normal Form (2NF)

Second normal form (2NF):


• 1NF +

• Non-key attributes are dependent on the full


key.
• Any non-key attributes that are dependent on
only part of the primary key should be moved to
any entity where that partial key is actually the
full key. This may require creating a new entity
and relationship on the model.
12/07/21 13
Normalization to 2NF

Supplier_No Product_No Product_Description


125 RX234 Car Engine

Primary Key
Product_No Product_Description
RX234 Car Engine

Supplier_No Product_No
22532 RX234

12/07/21 14
Third Normal Form (3NF)

Third normal form (3NF)


• 2NF +

• No Transitive Dependency
– One need to separate those attributes in separate
relation.
• Supplier Name  City and City  State

12/07/21 15
Normalization to 3NF

Supplier_Name City State


Ramesh Mumbai MH
Paresh Mumbai MH

City State
Mumbai MH

Supplier_Name City
Ramesh Mumbai
Paresh Mumbai
12/07/21 16
Boyce-CODD Normal Form (BCNF)

BCNF
• 3NF +
• Every determinant is candidate key.
(Determinant  some attribute depends upon
the same)

– New entities and relationships may have to be


added to the data model.

12/07/21 17
BCNF

SID Major Faculty SID Faculty


100 Math Cauchy 100 Couchy
150 Psycho Jung 150 Jung
200 Math Riemann ….
250 Math Cauchy
300 Psycho Perls
350 Math Riemann Faculty Major
Couchy Math
Jung Psycho

12/07/21 18
BCNF Anomalies

• Key (Primary): (SID, Major)


• Key (Candidate): (SID, Faculty)
• Functional Dependency (FD): Faculty  Major
• Deletion Anomaly: If SID = 300 record is deleted,
we loose information that Perls advises in
Psycho
• Insertion Anomaly: Unless some student majors
in Economics, we can not add that ‘Keynes’
advises in Economics

12/07/21 19
Recap of NF 1/2

1NF
• Eliminate repeating Groups. Attributes must have only
atomic values.

2NF
• Eliminate partial dependencies. All attributes must be
functionally dependent on the complete primary key.

3NF
• Eliminates transitive dependencies. All non-key domains
must be mutually exclusive.

12/07/21 20
Recap of NF 2/2

BCNF
• Eliminate functional dependencies in primary attributes in
the consequent. All determinants must be super-keys.

4NF
• Eliminate independent multi-valued attributes.

5NF
• Eliminate join dependencies. When-ever a non-loss
decomposition is possible, it must be carried out.

12/07/21 21
Synthesis of Relation

Case 1: One to One


Two attributes determine each other, i.e. A  B and B  A

• A and B can occur together in one relation?


– Yes. They should occur together.
• Can add other attributes that depend upon A (or B)
– Yes
• A and B should occur together in more than one relation?
– Not Desirable
• Examples
– Emp_No and Social_Security_No could be such attributes.
– Country Code and Country Name could be such attributes
12/07/21 22
Synthesis of Relation

Case 2: Many to One


A  B but B -/-> A (B does not determine A)
• Have one relation where A and B are attributes.
A is the key
• Add other attributes that depend on A only, in
that relation
• E.g. ‘Student ID’ ’Depart Name’. Have relation
where ‘Student ID’ and ‘Department Name’ are
attributes. You can add ‘Student Name’ to this
relation, but not ‘Department Address’
12/07/21 23
Synthesis of Relation

Case 3: Many to Many


A -/-> B and B -/-> A
• Have one relation where A and B are attributes. (A,B) is
the composite key
• Add other attributes that depend on (A,B), but not on A
alone or B alone
• E.g. ‘Item No’ and ‘Supplier No’. Put them in one relation.
You can add ‘Price’ in that relation, but not ‘Supplier
Name’ or ‘Item Description’.
• (Item No, Supplier No)  Price
• Item No  Item Description; Supplier No  Supplier
Name
12/07/21 24
De-normalization Tips 1/2

• Customer_Geog (Customer Name, Pin Code,


City, State) is not in 3NF
– Customer Name is key, but City  State
• Normalization will lead towards
– Customer_Pincode (Customer Name, Pin Code)
– Pincode_City (Pin Code, City)
– City_State (City, State)
• To find out all customers staying in Gujarat,
involve joining of tables. Will lead towards
performance problem
• Conclusion: stay with Customer_Geog

12/07/21 25
De-normalization Tips 2/2

• If new state is created, or name of city changes,


one need to ensure changes at number of
places(Controlled Redundancy)
• De-normalization  Need to control redundancy
• De-normalization  Updation is ‘slow’. (multiple
entities)
• De-normalization  Query is faster

12/07/21 26
Exercise Employee Information System

• Employee has a unique ‘Emp Code’. Employee also has ‘Last Name’
as other attribute, but it is not unique. Other attributes consist of
‘Birth Date’, ‘Hire Date’, and ‘Basic Salary’.
• Employee’s education is described in terms of various ‘Degrees’
(e.g. SSC, LLB, etc.) Employee can have multiple degrees. Some
employees may not have any degree.
• Employee works in one department. Every department has one
manager, who is also an employee.
• Employee is assigned one ‘Job Class’. Each ‘Job Class’ is described
in detail under ‘Title’.
• Employee might have one or more dependents. Dependent can be
spouse, child or parent.
• Employee might be given one or more ‘level’ of training (‘L1’, ‘L2’
etc.)

• Work out normalized relations.


12/07/21 27
Quiz 1/6

• Which of the following phrase describe ‘Normalization’


most appropriately?
a) It results in set of relations that do not have modification
anomalies
b) It results in set of relations that are very easy to understand
c) It results in set of relations that gives best performance
d) None of the above
• Ans: (a)
• Relation that is in third normal form, has to be in
a) 1st Normal Form
b) 2nd Normal Form
c) Both of ‘a’ and ‘b’
d) None of the above
• Ans: (c)

12/07/21 28
Quiz 2/6

• Consider relation Student having two attributes,


‘Student Number’ and ‘Marks’. ‘Marks’ attribute
can have up to seven values (representing
marks in seven subjects). This relation is in
a) 1st Normal Form
b) BCNF
c) 5th Normal Form
d) None of the above
– Ans: (d)

12/07/21 29
Quiz 3/6

• Consider Relation Customer (‘Customer Code’,


‘Customer Name’, ‘City’, ‘State’, ‘Pin Code’).
Functional dependency is as follows:
‘Customer Code’ is key
Pin Code  City, State

a) The relation is up to (highest Normal Form)


b) 1st Normal Form
c) 2nd Normal Form
d) 3rd Normal Form
e) BCNF
• Ans: (b)

12/07/21 30
Quiz 4/6

• A relation is in BCNF if it is in 3rd Normal Form,


and
a) Every determinant is a candidate key
b) There is no multi-value dependency
c) There are no transitive dependency
d) None of the above
• Ans: (a)

12/07/21 31
Quiz 5/6

• Which one of the following is true?


a) You should have all relations normalized up to at
least 5th Normal Form
b) You should have all relations normalized up to at
least 4th Normal Form
c) You should have all relations normalized up to at
least 3rd Normal Form
d) None of the above
• Ans: (d)

12/07/21 32
Quiz 6/6

• De-normalization normally implies


a) Better query performance
b) Data Redundancy
c) Both of ‘a’ and ‘b’
d) None of the above
• Ans: (c)

12/07/21 33
Exercise ACID test related.

• Does the following database systems satisfy the


‘ACID’ test? If so show provide the feature list, if
not the mention why not?
– MS SQL Server 2005
– MS Access
– MS Excel
– Oracle RDBMS V10g

12/07/21 34
Questions

&

Thank-you

12/07/21 35

You might also like