ER Diagram
Where We Are?
Proposal Analysis
Requirements
Modeling
Schema design Design
Tables
Indexes Implementation
Queries
Optimization
Testing
Installation
ER Diagram
• Widely used conceptual level data model
• proposed by Peter P Chen in 1970s
• Data model to describe the database system at the requirements stage
• provide high level description
• easy to understanding
• The E-R (entity-relationship) data model views the real world as a set of basic objects (entities)
and relationships among these objects
• It is intended primarily for the DB design process by allowing the specification of an enterprise
scheme. This represents the overall logical structure of the DB
Entity, Entity Type, Entity Set
• Entity – a thing/ object in the real world with independent existence and distinguishable. For instance,
Richa Sharma with ad-har no. 890123456 is an entity, as she can be uniquely identified as one
particular person in the universe
•An entity may be physical (a person or a book or passport or car or employee for example) or
conceptual (like a holiday or a disease or a course, or university, account)
• Entity type - a collection or set of entities that have same properties and is described by its name and
attributes
o Employee is an entity set with list of properties (Emp_ID, Emp_name, Emp_dept, Emp_salary)
o Company is another entity set with properties (Company_name, registration_no, location, owner)
• Entity Set- The collection of entities of a particular entity type in database at any point in time
o all persons having an account at a bank
o all employees working in CSE department
o all students in NIT
Example of Entity, Entity Set
Student Entity type
Roll_number Stu_name Address Age Branch
A001 Ravi Mumbai 17 BT Entity
Entity set A004 Ramesh Bihar 18 CSE
B005 Richa Delhi 17 CSE
B006 Disha Delhi 19 MME
C001 Mira Kolkata 18 BT
{A001,B005} = entity set
{A001, A004, B005, C001}= entity set
{A001, A004, B005, B006, C001}= entity set
Attribute
Attributes - Each entity is described by a set of attributes/properties
Student entity
Stud_name–name of the student
Roll_number–the roll number of the student
Age–the age of the student etc.
Domain - Each attribute takes the values from a set called its domain. Domain of an attribute is the set of
permitted values
Phone _number – ten positive integers
Marks ={0 to 100}
Minimum_balance > 1000
Student_age–{17,18, …, 45} Home_address–character strings of length 35
• Domain should be atomic or indivisible ( set of set of integers is not atomic, name ( Ravi Sharma)
• All entities in an Entity set/type have the same set of attributes
• Chosen set of attributes –amount of detail in modeling
Domain
Types of Attributes
Types of Attributes
• Simple Attribute - having atomic or indivisible values
Example : age , marital status, Roll_number, Branch
• Composite Attributes – can be divided into smaller sub parts
Example: address ( street address ( number, street , flat_no ), city, state, zip )
name ( first_name, middle_name, sur_name)
qualification(Degree_Name, Year, University_Name), ph (extension, number)
• Derived Attributes - attribute value is dependent on some other attribute (stored attributes)
- is not physically stored within the database, instead it is derived by using an
algorithm
Example: Age depends on DOB
Age is a derived attribute and DOB is stored attribute
Types of Attributes
• Single valued - having only one value rather than a set of values
Example: place_of_birth – single string value
age – an integer value
Branch – single string value
• Multi valued -having a set of values rather than a single value
Example: Courses_Enrolled for student Email _address for student
College_Degree for student Phone _number for a person
Color of a car
• Attributes can be:
Simple single-valued
Simple multi-valued
Composite single-valued (Part No: CA-08-02-189935 ,Location: KOL, Factory#:08, shift#: 02, part#: 189935)
Composite multi-valued
Null Values
• In some cases, a particular entity may not have an applicable value for an attribute
• Ex: apartment number attribute of address applies only to those addresses that are apartment type but not to the
addresses which are residence or own house type. Another case, college degree of a person. Person may have college
degree or may not have. In such situations, NULL value is used. NULL represents missing value
Meaning of NULL (i) not applicable
(ii) unknown missing ( value exits but missed to enter, eg height
of student)
not known (don’t know whether exists or not .
Eg. phone number of a person)
• Null value ≠ zero value
• A field with null value is a field with no value. It appears to be blank
• Roll_No of a student entity cannot be NULL whereas Phone_number can be NULL
Symbols Used
We can express the overall logical structure of a database graphically with an E-R diagram
Its components are:
• rectangles representing entity sets
• ellipses representing attributes.
• double ellipse representing multi-valued attribute
• ellipse connected to ellipse representing composite attribute
• dashed ellipse representing derived attribute
• diamonds representing relationship sets
• lines linking attributes to entity sets and entity sets to relationship sets
Example
Middle
name Last
name
Fname
email
student
name
Student
DOB
SID
age branch Ph_no
Example
Middle
name Last
name
Fname
email since
student
name
name FID
Student advised
DOB Faculty
by
age branch Ph_no The “since” attribute in relationship
is called a descriptive attribute, since
SID it describes the mapping from A to B
Attributes for Relationships
Joining
date
Professor Join department
grade
Student enrolls course
Relationship
• Relationship establishes an association between two or more entities
Example: student Richa enrolls in CSE course
Relationship enrolls has Student and Course as the participating entity sets
• Degree of a relationship
Degree: the number of participating entities
Degree 2: binary
Degree 3: ternary
Degree n: n-ary
Cardinality Ratio
• Cardinality ratio: Maximum number of relationships in which an entity can participate
• Four possibilities are usually specified:
➢ one-to-one (1:1)
➢ one-to-many (1:N)
➢ many-to-one (N:1)
➢ many-to-many (M:N)
Cardinality Ratio
• For binary relationship sets between entity sets E1 and E2, the mapping cardinality must be one of:
One-to-one: An entity in E1 is associated with at most one entity in E2, and an entity in E2 is
associated with at most one entity in E1
One-to-many: An entity in E1 is associated with any number in E2. An entity in E2 is associated
with at most one entity in E1
Many-to-one: An entity in E1 is associated with at most one entity in E2. An entity in E2 is
associated with any number in E1
Many-to-many: Entities in E1 and E2 are associated with any number from each other
Participation Constraint
Participation - Minimum number of relationships in which an entity can participate
An entity set may participate in a relation either totally or partially
Total participation: Every entity in the set is involved in some association (or tuple) of the relationship
Partial participation: Not all entities in the set are involved in association (or tuples) of the relationship
Partial Total
Employee manages Department
1 : 1 Relationship
since
name name
Men Marri
Women
ed to
1 1
name P_no
1 1
person has passport
Min- Max Constraint / Structural Constraint
• Min: Minimum number of times an entity can participate in the relationship
• Max: Maximum number of times an entity can participate in the relationship
Dept
EMP Manages
manages Dept E1 D1
EMP .
E2 . D2
1 1
.
Equivalent to E3 . D3
.
E4 . D4
EMP manages Dept E5 . ……
……
(0,1) (1,1) 1 1
1 : N Relationship
FID F_name SID S_name
N
1
supervis
Faculty student
es
Sex address Sex
Age
N : 1 Relationship
EID E_name DID D_name
1
N
EMP Works Department
Sex address HOD
Location
Min- Max Constraint for N: 1 Relationship
• An employee can work for only one department. A department can have many employees but
newly established department has no employee
Works Dept
EMP
E1 D1
.
Works E2 D2
EMP Dept .
for
E3 . D3
(1,1) .
(0,N) E4 . D4
E5 ……
……
1 N
M : N Relationship
SID S_name CID C_name
N
M
student Enrolls Course
Sex address
Credit Type
M : N Relationship
FID F_name PID P_name
N
M
Works
Faculty Project
for
Sex address
Type Fund
Min- Max Constraint for M:N Relationship
• An employee can work for more than one project. A project can have many employees but some
employees might not be associated with a project
Works Project
Emp
E1 P1
.
Works E2 P2
EMP Project .
for
E3 . P3
(0,M) .
(1,N) E4 . P4
E5 ……
……
M N
ER Diagram # Example
DeptID Deptname
EmpID Name 1
N Works
on Department
Employee
1
1 1
Address manages controls
Ph_no
startdate N
M N
City State
Project
Works
for
PID Pname
Recursive Relationship
Sometimes entities sets can be related to themselves
Recursive relationship: An entity set relating to itself
Example: Supervision is a relationship which relates an employee to a supervisor where both
employee and supervisor are entities of same type (employee). Here, employee entity type
participates twice in supervision relationship, once in the role of supervisor and another is
supervisee (subordinate)
Recursive Relationship
Emp (sub
Emp supervise ordinate)
(boss)
E5
Supervise E1 o E1
E4 E2 o E2
Boss Subordinate
1 N E3 E3
E1 E4 o E4
Emp
E5 o E5
E2 E3
(0,N) (0,1)
Ternary Relationship
So far, we have only considered binary relationships, however it is possible to have higher order
relationships, including ternary relationships ( number of participating entities =3)
Consider the following example that describes the fact that employees at a bank work in one or more bank
branches, and have one or more job descriptions
Ternary Relationship
In general, unless you really need a ternary
relationship, use binary relationships.
Name SSN
FACT: Every ternary (and higher order)
relationship can be converted into a set of Males
binary relationships.
Name SSN Name SSN
Females Parent Students
of
Sample Example of ER Diagram
Key
• Differences between entities within a given entity set must be expressed in terms of attributes
• Key–an attribute or a collection of attributes whose value(s) uniquely identify an entity in the
entity set
Examples:
SID-Key for Student entity set
FID -Key for faculty entity set
(name, SID) – key for student
(name, address) – key to identify a student
Note that name alone is not, as two students could have the same name.
• A key for an entity set may have more than one attribute
• An entity set may have more than one key
• Determined by the designers
Key
• A super key is a set of one or more attributes which, taken collectively, allow us to identify uniquely an entity
in the entity set
• A super key may contain extraneous attributes, and we are often interested in the smallest super key. A super
key for which no subset is a super key is called a candidate key
• A primary key is a candidate key (there may be more than one) chosen by the DB designer to identify entities
in an entity set
We can see that {Name, SID} is a
Name SID
super key
Note that a good choice of primary Lisa 1272
key is very important!
Bharati 5592 In this example, SID is a candidate
For example, it is usually much faster key, as it is minimal, and uniquely
to search a database by the primary Lisa 7552 identifies a student entity
key, than by any other key
Simran 5582
Weak Entity
In relational database, weak entity is an entity that can not be identified by its attributes alone. It can be
identified uniquely only by considering the primary key of another (owner/ strong) entity
o Weak entities are not of independent existence
o Weak entity set may not have a key attribute
o Owner entity set and weak entity set must participate in a one-to-many relationship set (one
owner, many weak entities). It has total participation in relationship with strong entity
o Weak entity set must have total participation in this identifying relationship set
Emp has Dependent
Identifying Total
Strong entity weak entity
relationship participation
Weak Entity Examples
Loan has payment
Payment Pay_date
Loan no Loan no
amount amount
Keys in DBMS
SID SNAME DEPT MARKS SUB
1 RINA CS 70 C1
2 DIP IT 67 C2
3 RINA CS 70 C1
4 SUBHO IT 45 C2
5 ANISH CS 80 C1
Find SKs, CKs and PKs
R (A,B,C,D)
A B C D
1 1 5 1
2 1 7 1
3 1 7 1
4 2 6 1
5 2 5 1
6 2 5 2
R (A,B,C)
A B C
1 1 1
2 1 2
3 2 1
4 2 2
Keys in DBMS
SID SNAME DEPT MARKS SUB
1 RINA CS 70 C1
2 DIP IT 67 C2
3 RINA CS 70 C1
4 SUBHO IT 45 C2
5 ANISH CS 80 C1
Find SKs, CKs and PKs
R (A,B,C,D)
A B C D
1 1 5 1
2 1 7 1
3 1 7 1
4 2 6 1
5 2 5 1
6 2 5 2
R (A,B,C)
A B C
1 1 1
2 1 2
3 2 1
4 2 2