Professional Documents
Culture Documents
DBMS Ch2 ERDiagram Sreevidya
DBMS Ch2 ERDiagram Sreevidya
Chapter 2
Entity Relationship Diagram
1
Sreevidya B., Department of CSE, Amrita School of Engineering, Bengaluru
2
Sreevidya B., Department of CSE, Amrita School of Engineering, Bengaluru
3
Why use ER Diagrams?
Sreevidya B., Department of CSE, Amrita School of Engineering, Bengaluru
ER Diagram is allowed you to communicate with the logical structure of the
database to users
Provide a preview of how all your tables should connect, what fields are going to
be on each table
ER diagrams are translatable into relational tables which allows you to build
databases quickly
4
ER Diagram : Basic Skeleton
Sreevidya B., Department of CSE, Amrita School of Engineering, Bengaluru
What is an Entity……?????
What is Relationship…….?????
What is an Attribute…….?????
5
Sreevidya B., Department of CSE, Amrita School of Engineering, Bengaluru
Attributes
Student
Entity Set
6
Entity:
Sreevidya B., Department of CSE, Amrita School of Engineering, Bengaluru
7
Sreevidya B., Department of CSE, Amrita School of Engineering, Bengaluru
Examples of Entities:
• Person: Employee, Student, Patient
8
Entity:
Sreevidya B., Department of CSE, Amrita School of Engineering, Bengaluru
9
Example
Sreevidya B., Department of CSE, Amrita School of Engineering, Bengaluru
10
Sreevidya B., Department of CSE, Amrita School of Engineering, Bengaluru
11
Example:
Sreevidya B., Department of CSE, Amrita School of Engineering, Bengaluru
Student entity has attributes such as Stu_Id, Stu_Name & Stu_Addr and
College entity has attributes such as Col_ID & Col_Name.
The relationship between Student and College is that the students study in colleges.
12
Sreevidya B., Department of CSE, Amrita School of Engineering, Bengaluru
Ellipses: Attributes
13
Lines: They link attributes to Entity Sets
Sreevidya B., Department of CSE, Amrita School of Engineering, Bengaluru
14
Sreevidya B., Department of CSE, Amrita School of Engineering, Bengaluru
15
Attributes:
Sreevidya B., Department of CSE, Amrita School of Engineering, Bengaluru
16
1. Key attribute:
Sreevidya B., Department of CSE, Amrita School of Engineering, Bengaluru
• For example, student roll number can uniquely identify a student from a set of students.
• Key attribute is represented by oval same as other attributes however the text of key
attribute is underlined.
17
2. Composite attribute:
Sreevidya B., Department of CSE, Amrita School of Engineering, Bengaluru
• For example, In student entity, the student address is a composite attribute as an address is
composed of other attributes such as pin code, state, country.
Pin code
State
Country 18
3. Multivalued attribute:
Sreevidya B., Department of CSE, Amrita School of Engineering, Bengaluru
19
4. Derived attribute:
Sreevidya B., Department of CSE, Amrita School of Engineering, Bengaluru
20
Sreevidya B., Department of CSE, Amrita School of Engineering, Bengaluru
21
Sreevidya B., Department of CSE, Amrita School of Engineering, Bengaluru
1:1
1:1
22
Sreevidya B., Department of CSE, Amrita School of Engineering, Bengaluru
PersonPassport 1:1
Person Passport 1:1
Aadhar No Name PP No
A1 AAA P1
A2 BBB P2
A3 CCC
23
Sreevidya B., Department of CSE, Amrita School of Engineering, Bengaluru
• One to Many ( 1: M)
24
Sreevidya B., Department of CSE, Amrita School of Engineering, Bengaluru
CustOrder 1:M
Cust Order 1:1
1:M
C No Name Order No
C1 AAA O1
C2 BBB O2
C3 CCC O3
25
Sreevidya B., Department of CSE, Amrita School of Engineering, Bengaluru
• Many to One ( M: 1)
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.
For example – many students can study in a single college but a student
cannot study in many colleges at the same time.
26
Sreevidya B., Department of CSE, Amrita School of Engineering, Bengaluru
• Many to One ( M: 1)
StudColl 1:1
Stud Coll M:1
M:1
S1-c1 s2-c1 s3-c3 1:1 c1-s1,s2 c2-s3 c3- 1:M
Reg No Name Coll Code
S1 AAA c1
S2 BBB
c2
S3 CCC
c3
27
Sreevidya B., Department of CSE, Amrita School of Engineering, Bengaluru
• Many to Many ( M: N)
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.
28
• Many to Many ( M: N)
Sreevidya B., Department of CSE, Amrita School of Engineering, Bengaluru
StudProject 1:M
Stud Project M:1
M: M / M:N
S1- P1,P2 S2 - P1 S3 - P2, P3
P3
29
• Many to Many ( M: N)
Sreevidya B., Department of CSE, Amrita School of Engineering, Bengaluru
Studcourse 1:M
Stud course M:1
M: M / M:N
S1-DB,OOPs S2-DB S3- Oops, PR 1:M(1 stud reg multiple courses)
DB-s1,s2 Oops-s1,s3 PR –S3 1:M(1 course is reg by multiple stud)
Reg No Name course
S1 AAA DB
S2 BBB
OOPs
S3 CCC
PR
30
Sreevidya B., Department of CSE, Amrita School of Engineering, Bengaluru
Participation Constraints :
Specifies if all the instances or only few of the instances of an entity are participating in a
relationship.
2. Partial Participation
31
Total Participation
Sreevidya B., Department of CSE, Amrita School of Engineering, Bengaluru
32
Sreevidya B., Department of CSE, Amrita School of Engineering, Bengaluru
Total Participation
A Total participation of an entity set represents that each instance in entity
must participate in the relationship
For example: In the below diagram each Passport must belong to a person,
hence total participation
33
Sreevidya B., Department of CSE, Amrita School of Engineering, Bengaluru
Partial Participation
A Partial participation of an entity set represents that only few or none of the
instances participate in the relationship.
For example: In the below diagram every person may not have a Passport,
hence partial participation
34
Specialization and Generalization:
Sreevidya B., Department of CSE, Amrita School of Engineering, Bengaluru
35
Specialization :
Sreevidya B., Department of CSE, Amrita School of Engineering, Bengaluru
36
Specialization :
Sreevidya B., Department of CSE, Amrita School of Engineering, Bengaluru
Generalization
• Generalization is a bottom-up approach in which multiple lower-level entities are
combined to form a single higher-level entity.
• Generalization is usually used to find common attributes among entities to form a
generalized entity. It can also be thought of as the opposite of specialization.
• The following enhanced entity relationship diagram expresses entities in a
hierarchical database to demonstrate generalization:
38
Generalization :
Sreevidya B., Department of CSE, Amrita School of Engineering, Bengaluru
Cust:
Name
Phone
Address
Credit
Bottom –up Email
Approach cid
Emp:
Name
Ph
Address
Eid
Sal
Generalization
39
Sreevidya B., Department of CSE, Amrita School of Engineering, Bengaluru
• Overlapping Vs Disjoint
40
disjoint entities
Sreevidya B., Department of CSE, Amrita School of Engineering, Bengaluru
Mobile
PAN No
No
Employee
The lower level entities
will not have any
instances overlapping.
i.e., the instance can IS A
belong to only one of
the lower entities
Adhar Mobile
card No No
Person
Name
IS A
The lower level entities
may have overlapping
instances.
42
condition based entities Exp
Sreevidya B., Department of CSE, Amrita School of Engineering, Bengaluru
ENo Degree
Faculty Name
IS A
43
Name
user defined entities
Sreevidya B., Department of CSE, Amrita School of Engineering, Bengaluru
RegNo Sem
Student Branch
IS A
No. of
The lower level Executive Secretary of events
entities have no well members the club conducted
defined criteria
No. of
Exec_Id
events Sec_Id
volunteered
44
Sreevidya B., Department of CSE, Amrita School of Engineering, Bengaluru
• Partial Participation
45
Total Participation
Sreevidya B., Department of CSE, Amrita School of Engineering, Bengaluru
Mobile
PAN No
No
No. of
Salary
Hours
46
Partial Participation
Sreevidya B., Department of CSE, Amrita School of Engineering, Bengaluru
Mobile
PAN No
No
Person-30
Person Emp-10
All Entities from higher level
entity may not participate in Cust-15
any of the lower level entities.
IS A
Ex: I may have details of all
people in a locality of a
departmental store. Some of
whom may be customer of the
store, few may be employees of Employee Customer
that store
Order
Salary
No.
47
Sreevidya B., Department of CSE, Amrita School of Engineering, Bengaluru
Identify and describe relationships. Look at two entities, are they related? How are the entities
related? Draw an action diamond between the two entities on the line you just added. In the diamond
write a brief description of how they are related.
Cardinality: Specify the mapping cardinalities between the entities in relationship , also specify the
participation constraints.
Add attributes. Any key attributes of entities should be added using oval-shaped symbols.
Complete the diagram. Continue to connect the entities with lines, and adding diamonds to describe
each relationship until all relationships have been described. Each of your entities may not have any
relationships, some may have multiple relationships. That is okay.
48
Sreevidya B., Department of CSE, Amrita School of Engineering, Bengaluru
49
Sreevidya B., Department of CSE, Amrita School of Engineering, Bengaluru
• Make sure that each entity only appears once per diagram.
50
Sreevidya B., Department of CSE, Amrita School of Engineering, Bengaluru
51
Case Study 1:
Sreevidya B., Department of CSE, Amrita School of Engineering, Bengaluru
52
In a University, there are multiple departments each of which is headed by a staff.
Sreevidya B., Department of CSE, Amrita School of Engineering, Bengaluru
1 1
Cse-dr.s Dept Headed
Staff
by
ECE-dr. n
There are multiple staffs employed in each department.
-> 1:m 1 Has
m
Dept Staff
<- 1:1
There are various courses offered by the department taught by teachers of the respective department. Adb,ip,ncp,ss
1 m Course
Dept o
offers
1
staff m o
teaches Course
h
Students register for various courses. (Assumption : Teachers teach only one course)
m n
stud o
registers Course
53
Dname
Sreevidya B., Department of CSE, Amrita School of Engineering, Bengaluru
Dept Ccode
Dloc
course
Cname
Credit
Fname Name
Name
Lname Stud
Staff RegNo
Sid
Ph No
Ph No
Age DoB
54
Sreevidya B., Department of CSE, Amrita School of Engineering, Bengaluru
Credit Ph No
grade DoB
Age
55
Sreevidya B., Department of CSE, Amrita School of Engineering, Bengaluru
56
Weak Entity, Discriminator and Identifying Relationship.
Sreevidya B., Department of CSE, Amrita School of Engineering, Bengaluru
• The entity sets which do not have sufficient attributes to form a primary key are
known as weak entity sets and the entity sets which have a primary key are
known as strong entity sets.
• As the weak entities do not have any primary key, they cannot be identified on their own, so they
depend on some other entity (known as owner entity/ strong entity). The weak entities have
total participation constraint (existence dependency) in its identifying relationship with owner
• Partial Keys are set of attributes with the help of which the tuples/records of the weak entities can
57
Sreevidya B., Department of CSE, Amrita School of Engineering, Bengaluru
A1 B1
Strong Identifying Weak ---
Entity Relationship Entity B2
A2
B3
58
Sreevidya B., Department of CSE, Amrita School of Engineering, Bengaluru
CampusCode RollNo
Relati --- --
Campus
ship
onship
Has Stud
Name
CampusLoc
ship
Sem
Roll No Name Sem
01 A 5 CampusCode CampusLoc
02 A 3 CBR Ettimadai
01 B 5 BL Bengaluru
03 C 3
59
Sreevidya B., Department of CSE, Amrita School of Engineering, Bengaluru
CampusCode SlNo
Relati --- --
Campus
ship
onship
Has Stud
Name
CampusLoc
ship
Sem
Sl No Name Sem
01 Adi 5 CampusCode CampusLoc
02 A 3 CBR Ettimadai
01 Abhi 5 BL Bengaluru
02 C 3
Discriminator
CBR01 (campuscode,Slno)
BLR01
60
Sreevidya B., Department of CSE, Amrita School of Engineering, Bengaluru
In a city, there are multiple univ, each of which has multiple library
• Assume in a (Amrita) University, there are multiple libraries and each library has
many student members. A student may be a member in multiple libraries.
• Each library has its own set of books. Within the library, these books are identified by
a unique number.
• Students can borrow multiple books from subscribed library.
• Students can also borrow books through inter library loan ie., in case the book is
unavailable in his/her subscribed library, the subscribed library avails from the other
library in which the student is not a member.
Draw the ERD
Strong Entity BRANCH Weak Entity PK? Discriminator?
Strong Entity GENRE Weak Entity ? PK? Discriminator?
Strong Entity LIBRARY Weak Entity BOOK PK LibCode Discriminator BookNo
61
Library : LibCode,LibLoc
Sreevidya B., Department of CSE, Amrita School of Engineering, Bengaluru
L1, GndFlr
L2 , FirstFlr
B1, DB
B2, OS
B1, PR
B2, OS
Case Study 2:
• University X
• Library (LibNo, LibLoc)
• Student(RegNo,Name,Sem)
• Books (ISBN, Title, Author, price)
Draw the ERD
63
Loans
book
Sreevidya B., Department of CSE, Amrita School of Engineering, Bengaluru
from
1 m L1: B1, B2, B3, B4 Srini(B5)
LibNo
Library L2: B1, B2, B5 ---sree(B3), raj(B4)
LibLoc
1 1 m
loans Is a
has member
1 n
bNo m
--- Name
Book borrows Stud
bname m RegNo
1
AuthorName
Ph No
64
Sreevidya B., Department of CSE, Amrita School of Engineering, Bengaluru
Case Study 3:
• Assume in a city, there are multiple banks and each bank has many branches.
Each branch has multiple customers.
• Customers have various types of accounts.
• Customers can also avail different types of loans from the respective
branches.
• One customer can have multiple accounts and can avail multiple loans.
Draw the ERD DLB---B1, B2
SBI----B1, B2, B3
65
Sreevidya B., Department of CSE, Amrita School of Engineering, Bengaluru
Case Study 4:
• Given below are entities, connect them appropriately with necessary
relationship and relationship constraints including mapping cardinalities and
participation constraints.
Complete the ERD with Attributes for each entity and key attributes if any.
66
Sreevidya B., Department of CSE, Amrita School of Engineering, Bengaluru
67
Sreevidya B., Department of CSE, Amrita School of Engineering, Bengaluru
Credit Ph No
grade DoB
Age
68
Mapping Strong Entity
Sreevidya B., Department of CSE, Amrita School of Engineering, Bengaluru
Dname
Ccode
course
Cname Course(Ccode, Cname, Credit)
Credit
69
Name
Sreevidya B., Department of CSE, Amrita School of Engineering, Bengaluru
Ph No
DoB Stud( Regno, Name, DoB, Phno1, phno2)
Age 1, A, 22 sept, 100, null
2, B, 22 Aug, 101,102
Stud_ph(Regno, phno)
1, 100
2, 101
2, 102
Sreevidya B., Department of CSE, Amrita School of Engineering, Bengaluru
Staff
Sid
Name
Ph No
Lname
Fname
Staff_ph(Sid, phno)
Staff (Sid, Fname, Lname)
Mapping 1:1 relationship
Sreevidya B., Department of CSE, Amrita School of Engineering, Bengaluru
1
1
Dept Headed Staff
by
Dept (dname, dloc, sid )……. Dname is the pk and sid is the fk
Cse, GF, S1
ECE, FF, S2
1 m
Dept Has Staff
1 m
Dept Has Staff
Dept(dname,dloc) dname is the pk Staff(sid, fname,lname, dname ) sid is the pk and dname is the fk
Dept o Course
offers
1 m
Dept(dname,dloc) dname is the pk Course(ccode,cname,credit, dname) code is the pk and dname is the fk
m 1
staff o
teaches Course
Staff(sid, fname,lname, code ) sid is the pk and code is the fk Course(ccode,cname,credit) code is the pk
74
Descriptive Attribute
Sreevidya B., Department of CSE, Amrita School of Engineering, Bengaluru
How about attaching datedriven attribute with driver, this is also incorrect, becasr the driver drives can drive different
trucks at different times , as in the case above you cannot have mulriple rows with same LicNo(violation of pk) andalso
one cell cannot accommodate more than one date. So you cannot attach date driven to Driver.
Therefore the attribute is better attached as an attribute describing the relationship between the two entities and they
are called descriptive attributes.
75
Mapping m:n relationship ( a new table is created with only key attributes of
Sreevidya B., Department of CSE, Amrita School of Engineering, Bengaluru
m n
stud o
registers Course
grade
C1,S1, A
C2,S1, B
vaishak O DB 76
Mapping m:n relationship ( a new table is created with only key attributes of
Sreevidya B., Department of CSE, Amrita School of Engineering, Bengaluru
1 n
stud o
registers Course
Stud(regno,name,dob)
Pk=regno
Course(ccode,cname, credit,regno)
Pk=ccode
Fk=regno
Stud_ph(regno,phno)
Fk=regno
Pk=(regno,phno)
77
Mapping m:n relationship ( a new table is created with only key attributes of
Sreevidya B., Department of CSE, Amrita School of Engineering, Bengaluru
m n
stud o
registers Course
grade
Stud_Reg( RegNo, ccode, grade) this table depicts the relationship between stud and course
RegNo is a fk, ccode is a fk, grade is a descriptive attribute, (Regno,ccode) is a pk
78
Mapping Weak entity
Sreevidya B., Department of CSE, Amrita School of Engineering, Bengaluru
m 1
A or B
A(a1,a2)
Discriminator =a1 Mapping:
Pk of the weak entity= pk of the strong
entity + discriminator of the w.e
Pk of A=(b1,a1) A(b1,a1,a2)
Discriminator=a1
FK=b1
B(b1,b2) PK=a1,b1
Pk=b1
B(b1,b2)
PK=b1
79
Mapping Weak entity
Sreevidya B., Department of CSE, Amrita School of Engineering, Bengaluru
m 1
A or B
B (b1, b2,b3) b1 is pk
A( b1, a1, a2, a3) b1 is fk, a1 is discriminator, (a1,b1 ) is the pk
80
Tables after mapping
Sreevidya B., Department of CSE, Amrita School of Engineering, Bengaluru
1. Dept(dname, dloc)
2. Course(Ccode, Cname, Credit)
3. Stud(RegNo, Name, DoB)
4. Stud_phNo (RegNo, PhNo)
5. staff( Sid, Fname, Lname )
6. Staff_Ph (Sid, PhNo)
7. Dept(dname,dloc, sid)
8. Staff(sid, fname,lname, dname )
9. Course(ccode,cname,credit, dname)
10.Staff(sid, fname,lname, ccode )
11.Stud_Reg( RegNo, ccode, grade)
81
Tables after mapping
Sreevidya B., Department of CSE, Amrita School of Engineering, Bengaluru
1. Dept(dname, dloc)
2. Course(Ccode, Cname, Credit)
3. Stud(RegNo, Name, DoB)
4. Stud_phNo (RegNo, PhNo)
5. staff( Sid, Fname, Lname )
6. Staff_Ph (Sid, PhNo)
7. Dept(dname,dloc, sid)
8. Staff(sid, fname,lname, dname )
9. Course(ccode,cname,credit, dname)
10.Staff(sid, fname,lname, ccode )
11.Stud_Reg( RegNo, ccode, grade)
82
Tables after mapping Tables after filtering
Sreevidya B., Department of CSE, Amrita School of Engineering, Bengaluru
Dept-1,7
1. Dept(dname, dloc)
Course -2,9
2. Course(Ccode, Cname, Credit)
Staff-5,6,8,10
3. Stud(RegNo, Name, DoB) Stud -3,4,11
4. Stud_phNo (RegNo, PhNo)
5. staff( Sid, Fname, Lname )
6. Staff_phNo(Sid, PhNo) 1. Dept(dname, dloc)
7. Dept(dname,dloc, sid) 9 Course(ccode,cname,credit, dname)
Combine 8 and 10 =12 Staff(sid, fname,lname, dname,ccode )
8. Staff(sid, fname,lname, dname )
6 Staff_phNo(Sid, PhNo)
9. Course(ccode,cname,credit, dname) 3 Stud(RegNo, Name, DoB)………………….
10. Staff(sid, fname,lname, ccode ) 4 Stud_phNo (RegNo, PhNo)
11. Stud_Reg( RegNo, ccode, grade)
11 Stud_Reg( RegNo, ccode, grade)
83
Tables after mapping Tables after filtering
Sreevidya B., Department of CSE, Amrita School of Engineering, Bengaluru
Dept-1,7
Course -2,9
Staff-5,6,8,10
Stud -3,4,11
1 Dept(dname, dloc)…………..(3)
9 Course(ccode,cname,credit, dname) ………………(4)
Combine 8 and 10 =12 Staff(sid, fname,lname, dname,ccode ) ……………(6)
6 Staff_phNo(Sid, PhNo)………………..(7)
3 Stud(RegNo, Name, DoB)……………………no FK…….(1)
4 Stud_phNo (RegNo, PhNo)…………..(2)
11 Stud_Reg( RegNo, ccode, grade) ……………….(5)
Modify table 1 using ALTER ADD command and add a column sid , accordingly 1
becomes Dept(dname, dloc, sid)
84
Tables after mapping Tables after filtering
Sreevidya B., Department of CSE, Amrita School of Engineering, Bengaluru
1. Dept(dname, dloc)
1. Dept(dname, dloc)
2. Course(Ccode, Cname, Credit)
2. Stud(RegNo, Name, DoB)
3. Stud(RegNo, Name, DoB)
3. Stud_phNo (RegNo, PhNo)
4. Stud_phNo (RegNo, PhNo)
4. Staff (Sid, PhNo)
5. staff( Sid, Fname, Lname )
5. Course(ccode,cname,credit,dname)
6. Staff (Sid, PhNo)
6. Staff(sid, fname,lname, dname,ccode) //combining 8 & 10
7. Dept(dname,dloc, sid)
7. Stud_Reg( RegNo, ccode, grade)
8. Staff(sid, fname,lname, dname )
9. Course(ccode,cname,credit, dname)
10. Staff(sid, fname,lname, ccode )
11. Stud_Reg( RegNo, ccode, grade)
85
Ternary Relationship :
Sreevidya B., Department of CSE, Amrita School of Engineering, Bengaluru
3. Emp_ph (Employeeid,phone)
88
4. Cust_ph(Customer_id,phone)
Sreevidya B., Department of CSE, Amrita School of Engineering, Bengaluru
Aggregation
In aggregation, the relationship between two entities is treated as a single
entity. In aggregation, relationship with its corresponding entities is
aggregated into a higher level entity.
Manages
Manager
For example: Center entity offers the Course entity act as a single entity in
the relationship which is in a relationship with another entity visitor. In the
real world, if a visitor visits a coaching center then he will never enquire
about the Course only or just about the Center instead he will enquiry about
both.
Center(only attributes of Center)
Course (Only attributes of course)
Visitor(Only attributes of visitor)
Center_course(pks of center and course)
Enquire(pks of center, course, visitor,date)
Date
For example: Center entity offers the Course entity act as a single entity in
the relationship which is in a relationship with another entity visitor. In the
real world, if a visitor visits a coaching center then he will never enquire
about the Course only or just about the Center instead he will enquiry about
both.
Center(only attributes of Center)
Course (Only attributes of course)
Vsitor(Only attributes of visitor)
Center_course(pks of center and course)
Enquire(pks of center, course, visitor,date)
Date
92