dbms 2
dbms 2
Two perspectives
Top-down
Data model is derived from an intimate understanding of
the business.
Bottom-up
Data model is derived by reviewing specifications and
business documents.
Entity-Relationship (ER) Modeling :
Examples of entities:
Person: EMPLOYEE, STUDENT, PATIENT
Country
Check is it Entity or not :
Game
Check is it Entity or not :
Nurse
Check is it Entity or not :
Car
Check is it Entity or not :
Bank Account
Check is it Entity or not :
Order
Check is it Entity or not :
Santro
Check is it Entity or not :
Location
Check is it Entity or not :
Region
Check is it Entity or not :
Artist
Check is it Entity or not :
Song
Check is it Entity or not :
Hrithik Roshan
Check is it Entity or not :
Publisher
Check is it Entity or not :
Book
Check is it Entity or not :
Author
Check is it Entity or not :
Name
Check is it Entity or not :
Movie
Check is it Entity or not :
Shipping
Check is it Entity or not :
Credit Card
Check is it Entity or not :
Agent
Check is it Entity or not :
MITCOM
Check is it Entity or not :
Job
Check is it Entity or not :
Position
Check is it Entity or not :
Apartment
Check is it Entity or not :
Loan
Entity Set ?
Entity Set :
Entity
Entity Set :
– Example:
student
Attribute of relationship
Relationship Sets
student
Attribute of relationship
Relationship Sets with Attributes
Relationship Sets with Attributes
Relationship name:
writes
Author Book
Each entity may have its own value for each attribute.
Attributes :
Engine_no Car
Simple attribute
Simple Attribute :
Here, all the attributes are simple attributes as they can not be
divided further.
Simple Attribute :
2) Complex Attribute
Complex Attribute :
Definition:
- Attributes that can be divided into sub-parts like a tree structure
- Required to store detailed information of an attribute.
- This attribute have its own attribute
Representation:
- Attribute having child attribute
Composite Attributes :
Simple
Attributes
Composite
Attributes
Composite Attributes :
Composite Attributes :
Flight_Number Date
No._of_passengers
Flight_ID
Flight
Composite Attributes :
Composite Attributes :
3) Single-valued Attributes
3) Single-valued Attributes :
Representation:
-> Shown by dual oval.
4) Multi-valued Attributes
Multi Valued
Attribute
4) Multi-valued Attributes
4) Multi-valued Attributes
4) Multi-valued Attributes
4) Multi-valued Attributes
For instance, let us say that the instructor entity set has
an attribute students advised, which represents how
many students an instructor advises.
5) Derived Attributes
Representation:
Shown by dashed oval.
Symbol:
5) Derived Attributes
Emp_name
Emp_address
EMP_ID
Years_Employed
Skill
Employee
5) Derived Attributes
city
pin
Person_nam
e BirthDate
street
Address
Age
Favourite
Person
food
NULL Value :
Physical model
Conceptual model
Mapping Cardinalities
Mapping Cardinalities :
M to represent many
M
Mapping Cardinalities :
For a binary relationship set R between entity sets A and B, the mapping
cardinality must be one of the following:
1) One-to-one :
One-to-one
Mapping Cardinalities :
For a binary relationship set R between entity sets A and B, the mapping
cardinality must be one of the following:
1) One-to-one :
One-to-one
The employee can manage only one department, and each department can be managed by one employee only:
Mapping Cardinalities :
For a binary relationship set R between entity sets A and B, the mapping
cardinality must be one of the following:
1) One-to-one :
One-to-one
Mapping Cardinalities :
For a binary relationship set R between entity sets A and B, the mapping
cardinality must be one of the following:
1) One-to-one :
One-to-one
Mapping Cardinalities :
For a binary relationship set R between entity sets A and B, the mapping
cardinality must be one of the following:
One-to-many
Mapping Cardinalities :
For a binary relationship set R between entity sets A and B, the mapping
cardinality must be one of the following:
One-to-many
Mapping Cardinalities :
For a binary relationship set R between entity sets A and B, the mapping
cardinality must be one of the following:
2) One-to-many :
One-to-many
Mapping Cardinalities :
For a binary relationship set R between entity sets A and B, the mapping
cardinality must be one of the following:
2) One-to-many :
One-to-many
Mapping Cardinalities :
For a binary relationship set R between entity sets A and B, the mapping
cardinality must be one of the following:
3) Many to one : When more than one instances of an
entity is associated with a single instance of another
entity then it is called many to one relationship.
Many to one
Mapping Cardinalities :
For a binary relationship set R between entity sets A and B, the mapping
cardinality must be one of the following:
3) Many to one : For example – many students can
study in a single college but a student cannot study in
many colleges at the same time.
Many to one
Mapping Cardinalities :
For a binary relationship set R between entity sets A and B, the mapping
cardinality must be one of the following:
3) Many to one :
Many to one
Mapping Cardinalities :
For a binary relationship set R between entity sets A and B, the mapping
cardinality must be one of the following:
3) Many to one :
Many to one
Mapping Cardinalities :
For a binary relationship set R between entity sets A and B, the mapping
cardinality must be one of the following:
4) Many to many : When more than one instances of
an entity is associated with more than one instances of
another entity then it is called many to many
relationship.
Many to many
Mapping Cardinalities :
For a binary relationship set R between entity sets A and B, the mapping
cardinality must be one of the following:
4) Many to many : For example, a can be assigned to
many projects and a project can be assigned to many
students.
Many to many
Mapping Cardinalities :
For a binary relationship set R between entity sets A and B, the mapping
cardinality must be one of the following:
4) Many to many :
Many to many
Explain following examples ?
Problem………..?
• Draw er diagram for each employee can work
in at most one project and each project
employees at most one employee.
Problem………..?
Draw er diagram for each customer has at
most one account and each account
corresponds to at most one customer
Problem………..?
• Draw er diagram for each engineer can work
in at most one project, each project can
employ many engineers
Problem………..?
• Draw er diagram for each teacher teaches
more than one student and more than one
teacher teaches each student in a semester.
Problem………..?
• Draw er diagram for consider that in your
library there are many copies of the
textbook and each student can issue more
than one book at a given time. Similarly,
more than one student issues each book
Problem……….!
• Draw an ER diagram for order processing
system where a person can give order for
many items by specifying its quantity
Relationship Sets with Attributes
Work on N
M
employee project
1
1
manages
position
salary
budget
Multiple Relationship
advisor
professor student
tutorial
examiner
Types of Key
Types of Key :
Super Key :
Super key is a set of an attribute which can uniquely
identify a tuple. Super key is a superset of a candidate key.
1) {Roll_no}
2) {Registration_no}
3) {Roll_no, Registration_no},
4) {Roll_no, Name}
5) {Name, Registration_no}
6) {Roll_no, Name, Registration_no}
All the above keys are able to uniquely identify each row.
Candidate Key :
A candidate key is an attribute or set of an attribute
which can uniquely identify a tuple.
The remaining attributes except for primary key are
considered as a candidate key. The candidate keys are
as strong as the primary key.
Candidate Key :
A candidate is a subset of a super key.
Like strong entity, weak entity does not have any primary
key, It has partial discriminator key.
Weak Entity Sets :
Acc_id
Address color
Accident associated
M
Date
Place
2 nd answer…..!
Cust_name Cust_id Chasis_no Model
Acc_no
Address color
Accident associated
M
Date
Place
Problem………..?
Technocrates Limited manufacture specialty engineering
components for automobiles. They are considering
computerization of their purchase ordering system.
During the requirement analysis, it was observed that the
company purchases a large number of items. The materials
planning department generates indents for the items which are to
be procured. These items have to be procured from the vendors.
Based on Indents, the purchase orders are prepared. Draw ER
diagram.
Answer…………
• Entity :
Indent
Item
Purchase Order
Vendor
Answer…………
1 M
Can have Vendor have
• Entity :
Doctor
Patient
Room
Test
Path Lab
Pat_id
Name Answer………… Test_id
T_name
Descri.
1 M
Patient perform Test
1
n
Admitted to M
1
Carried into
Room
Treated by 1
Rate
Type
Room_id Lab
m
Doctor Lab_id
Ph_no Name
Address
Name Address
Doc_id
T_name
Test_id
Pat_id
Name
2nd answer…..! Descri.
1 M
Patient perform Test
1
n
Admitted to M
1
Carried into
Room
Treated by 1
Rate
Type
Room_id Lab
m
Doctor Lab_id
Ph_no Name
Address
Name Address
Doc_id
Problem………..?
M 1
M 1
M
N
Problem……!
M N
actor Act movie movie
1 M
Actor_id
A_address Directed by
1
Director
D_address
Dir_id
D_name
Problem………..?
• Entity :
Flight
Passenger
Agent
Ticket
Answer…………
M
Passenger Book from
Book 1 1
Book Agent
M
Ticket
M
1
for Flight
Problem………..? Example: Department Store
N
N
CUSTOMER
Customer Name
Address
HOLD IN-CITIES
Phone Number
Qty Held Qty_in_cities
N N
1
ITEMS
Item ID N N ORDERS N
ITEMS ORDERED
Description Order Number PLACE
Qty Ordered
Size Order Date
Color
Problem……?
on Budgets
N Uniform N Shoe 1
size size
support manges
Player for
TName
1 1
Team
practices
N
capacity
N N Play N
N
attends againts
Stadium
date attendance
1
Game Take Place
N N
Contract-Supplies System
Attribute ?
Relational Model Concepts :
Tables ?
Relational Model Concepts :
Tuple ?
Relational Model Concepts :
Relation Schema ?
Relational Model Concepts :
1) Domain Constraints
2) Key Constraints
Performance :
Always the performance of the relational database depends on the
number of tables. If there are more number of tables, the
response given to the queries will be slower. Additionally, more
data presence not only slows down the machine, it eventually
makes it complex to find information. Thus, a relational database is
known to be a slower database.
Disadvantages of Relational Model :
Physical Storage :
A relational database also requires tremendous amount of physical
memory since it is with rows and columns. Each of the operations
depend on separate physical storage. Only through proper
optimization, the targeted applications can be made to have
maximum physical memory.
Disadvantages of Relational Model :
Complexity :
Although a relational database is free from complex structuring,
occasionally it may become complex too. When the amount of
data in a relational database increases, it eventually makes the
system more complicated. Each and every data is been complex
since the data is arranged using common characteristics.
Disadvantages of Relational Model :
Information Loss :
Large organizations tends to use more number of number of
database systems with more tables. These information can be
used to be transferred from one system to another. This could
pose a risk of data loss.
Disadvantages of Relational Model :
Structure Limitations :
The fields that is present on a relational database is with
limitations. Limitations in essence means that it cannot
accommodate more information. Despite if more information are
provided, it may lead to data loss. Therefore, it is necessary to
describe the exact amount of data volume which the field will be
given.
Disadvantages of Relational Model :
customer(cust_id, name )
cust_addrs (cust_id, street, city, state, zipcode )
– Large primary keys aren’t ideal – tend to be costly
Database Design using E-R
E-R to Relational
A ( a1 , a2 )
BR ( b1 , b2, a1 (FK) )
Database Design using E-R
E-R to Relational
AR ( a1 , a2 , b1(FK) )
B ( b1 , b2 )
Database Design using E-R
E-R to Relational
employee (
employee_id,
name,
manager_id(FK of same table)
)
Database Design using E-R
E-R to Relational
B (b1 , b2)
C (c1 , c2)
R3 (b1 , c1)
Database Design using E-R to Relational
Update Anomaly :
In the above table we have two rows for employee Rick as he belongs to two departments
of the company. If we want to update the address of Rick then we have to update the same
in two rows or the data will become inconsistent. If somehow, the correct address gets
updated in one department but not in other then as per the database, Rick would be having
two different addresses, which is not correct and would lead to inconsistent data.
Anomalies in DBMS
emp_id emp_name emp_address emp_dept
Is it in 1NF?
Normalization – 1NF
1NF
Example
Normalization – 1NF
Is it in 1NF ?
Normalization – 1NF
1NF
Normalization – 1NF
IS IT in 1NF ?
Normalization – 2NF
Normalization – 2NF
In the 2NF, relational must be in 1NF.
25 Chemistry 30
25 Biology 30
47 English 35
83 Math 38
83 Computer 38
Is it in 1NF ?
Normalization – 2NF
25 Chemistry 30
25 Biology 30
47 English 35
83 Math 38
83 Computer 38
25 Chemistry 30
25 Biology 30
47 English 35
83 Math 38
83 Computer 38
25 Chemistry 30
25 Biology 30
47 English 35
83 Math 38
83 Computer 38
25 Chemistry 30
25 Biology 30
47 English 35
83 Math 38
83 Computer 38
25 Chemistry
25 Biology
47 English
83 Math
83 Computer
TEACHER_ID(PK) TEACHER_AGE
25 30
47 35
83 38
Normalization – 2NF
TEACHER_ID SUBJECT TEACHER_AGE
25 Chemistry 30
25 Biology 30
47 English 35
83 Math 38
83 Computer 38
Candidate keys :
{STUD_NO, COURSE_NO}
Normalization – 2NF
STUD_NO COURSE_NO COURSE_FEE
1 C1 1000
2 C2 1500
1 C4 2000
4 C3 1000
4 C1 1000
2 C5 2000
X is a super key.
That's why we need to move the EMP_CITY and EMP_STATE to the new
<EMPLOYEE_ZIP> table, with EMP_ZIP as a Primary key.
Normalization – 3NF
EMP_ID EMP_NAME EMP_ZIP(FK)
201010 UP Noida
02228 US Boston
60007 US Chicago
06389 UK Norwich
462007 MP Bhopal
Normalization – 3NF
Is it in 1NF?
Normalization – 3NF
PK – Genre ID
PK – Book ID
FK – Genre ID
Case Studies
Case Study - 1
Case study 1
Solve up to 3NF?
Case study 1
Is it in 1NF ?
Case study 1
1NF
1) Find out Candidate Keys ?
2) Find out primary key ?
Case study 1
1NF
Primary Key (Proj_no+Emp_no)
Case study 1
Case study 1
Case study 1
FK 2NF
Case study 1
Case study 1
Case study 1
FK
3NF
Case study 1 with all tables
Case Study - 2
Case Study 2
Case Study 2
UNF
1NF = Each Cell contain Atomic Value
Case Study 2
1NF
2NF = 1NF + NO PARTIAL DEPENDENCY
Find out Candidate Keys ?
Decide Primary key :
2NF
3NF = 2NF + NO TRANSITIVE DEPENDENCY
Case Study 2
3NF
Case Study - 3
Case Study 3
Order no Order Invoice Invoice Product Product Qty
date no date no descritpion order
25 15/8/97 1568 28/8/97 4567 Computer 5
form
128 16/8/97 1569 28/8/97 4568 ribbon 10
4567 Computer 20
form
4555 A4 paper 25
UNF
1NF = Each Cell contain Atomic Value
Case Study 3
Order no Order Invoice Invoice Product Product Qty
date no date no descritpion order
25 15/8/97 1568 28/8/97 4567 Computer 5
form
128 16/8/97 1569 28/8/97 4568 ribbon 10
Functional Dependency
Order no order date, invoice no, invoice date
Transitivity Dependency
Order no invoice no
invoice no invoice date
Case Study 3
Order Product Qty Order no Order Invoice
no no order date no
25 4567 5 25 15/8/97 1568
3NF
Case Study - 4
Case Study 4
A software contract and consultancy firm maintains details of all the various projects in
which its employees are currently involved. These details comprise:
Employee Number
Employee Name
Date of Birth
Department Code
Department Name
Project Code
Project Description
Project Supervisor
1NF
R1(Employee Number, Employee Name, Date of Birth,
Department Code, Department Name, Project Code,
Project Description, Project Supervisor)
2NF = 1NF + NO PARTIAL DEPENDENCY
Find out Candidate Keys ?
Decide Primary key :
2NF
R2(Employee Number, Employee Name, Date of Birth,
Department Code, Department Name)
3NF
R5(Employee Number, Employee Name, Date of Birth, Department
Code)
Assume that each lecturer may teach many subjects but may not
belong to more than one department.
Subject Code, Subject Name and Subject Level are repeating
fields.
Normalise this data to Third Normal Form.
Case Study - 5
UNF
R1 (Lecturer Number , Lecturer_Name, Lecturer_Grade ,
Department Code,Department Name, Subject Code,
Subject_Name, Subject Level)
1NF = Each Cell contain Atomic Value
Case Study - 5
1NF
R1 (Lecturer Number , Lecturer_Name, Lecturer_Grade ,
Department Code,Department Name, Subject Code,
Subject_Name, Subject Level)
2NF = 1NF + NO PARTIAL DEPENDENCY
Case Study - 5
1NF
Lecturer Number + Subject Code
Case Study - 5
2NF
R2(Lecturer Number, Lecturer Name, Lecturer Grade, Department
Code, Department Name )
3NF
R5(Lecturer Number,Lecturer Name,Lecturer Grade,
Department code)
R6(Department Code, Department Name)
R3(Lecturer Number, Subject Code )
R4(Subject Code,Subject Name,Subject Level)
1NF
2NF
3NF
A B C D
A+B → C,D
C→B
A table that is in 3NF but not in BCNF ( Boyce–Codd Normal Form)
BCNF ( Boyce–Codd Normal Form) :
Definition :
One of the more desirable normal forms that we can obtain is Boyce–
Codd normal form (BCNF). A relation schema R is in BCNF with respect
to a set F of functional dependencies if, for all functional dependencies
in F+ of the form α → β, where α ⊆ R and β ⊆ R, at least one of the
following holds :
Candidate key :
member+sport
Or
member+coach
BCNF example
member sport