Sei sulla pagina 1di 48

BITS Pilani Dr.

Lavika Goel
Assistant Professor
BITS Pilani Department of CSIS
Lecture 2: 27/7/2019
Pilani Campus
Hyderabad Campus
BITS Pilani
Hyderabad Campus
Pilani Campus

Database Design and Applications (CSI ZG518/


SSZG518)
Entity-Relationship Model

Design Process
Modeling
Constraints
E-R Diagram
Design Issues
Weak Entity Sets
Extended E-R Features
Design of the Bank Database

Database Design and Applications (CSI ZG518/ SSZG518) Pilani Campus


BITS Pilani, Hyderabad Campus
Modeling
A database can be modeled as:
– a collection of entities,
– relationship among entities.
An entity is an object that exists and is distinguishable from other objects.
– Example: specific person, company, event, plant
Entities have attributes
– Example: people have names and addresses
An entity set is a set of entities of the same type that share the same properties.
– Example: set of all persons, companies, trees, holidays

Database Design and Applications (CSI ZG518/ SSZG518) Pilani Campus


BITS Pilani, Hyderabad Campus
Entity Sets instructor and student

ID name ID name

Database Design and Applications (CSI ZG518/ SSZG518) Pilani Campus


BITS Pilani, Hyderabad Campus
Keys
A super key of an entity set is a set of one or more attributes whose values uniquely determine
each entity.
A candidate key of an entity set is a minimal super key
– ID is candidate key of instructor
– course_id is candidate key of course
Although several candidate keys may exist, one of the candidate keys is selected to be the
primary key.

Database Design and Applications (CSI ZG518/ SSZG518) Pilani Campus


BITS Pilani, Hyderabad Campus
Foreign Keys, Referential
Integrity
Foreign key : Set of attributes in one entity set that is used to `refer’ to an
attribute in another entity set.
– Must correspond to the primary key of the other entity set.
– Like a `logical pointer’.

If all foreign key constraints are enforced, referential integrity is achieved (i.e.,
no dangling references.)

Database Design and Applications (CSI ZG518/ SSZG518) Pilani Campus


BITS Pilani, Hyderabad Campus
Foreign Keys
E.g. Only students listed in the Students entity set should be allowed to enroll
for courses.
– sid is a foreign key referring to Students:

Enrolled
sid cid grade Students
sid name login age gpa
53666 Carnatic101 C
53666 Jones jones@cs 18 3.4
53666 Reggae203 B
53688 Smith smith@eecs 18 3.2
53650 Topology112 A
53666 History105 B 53650 Smith smith@math 19 3.8

11111 English102 A
Database Design and Applications (CSI ZG518/ SSZG518) Pilani Campus
BITS Pilani, Hyderabad Campus
Relationship Sets
A relationship is an association among several entities
Example:
44553 (Peltier) advisor 22222 (Einstein)
student entity relationship set instructor entity
A relationship set is a mathematical relation among n  2 entities, each taken from entity sets
{(e1, e2, … en) | e1  E1, e2  E2, …, en  En}

Example:
(44553,22222)  advisor
There can be more than one relationship set involving the same entity sets.

Database Design and Applications (CSI ZG518/ SSZG518) Pilani Campus


BITS Pilani, Hyderabad Campus
Relationship Set advisor

Database Design and Applications (CSI ZG518/ SSZG518) Pilani Campus


BITS Pilani, Hyderabad Campus
Keys for Relationship Sets
Keys provide a mechanism to distinguish between different relationships in a relationship set.
For ex., if there is a many to many relationship between customer and account, then the union
of the primary keys of customer and account will compose the primary key of the
relationship.
If the relationship set has no attributes, then the following set of attributes describe an individual
relationship:

In case the relationship also has attributes, the following set of attributes describe an individual
relationship:

Database Design and Applications (CSI ZG518/ SSZG518) Pilani Campus


BITS Pilani, Hyderabad Campus
Relation advisor with instructor and
student
the relationship set is many-to-many, Then the primary key of advisor consists
of the union of the primary keys of instructor and student
If the relationship is many-to-one from student to instructor—that is, each
student can have have at most one advisor—then the primary key of advisor
is simply the primary key of student.
if an instructor can advise only one student— that is, if the advisor relationship
is many-to-one from instructor to student—then the primary key of advisor is
simply the primary key of instructor.
one-to-one relationships ??
either candidate key can be used as the primary key

Database Design and Applications (CSI ZG518/ SSZG518) Pilani Campus


BITS Pilani, Hyderabad Campus
E-R Diagrams

 Rectangles represent entity sets.


 Diamonds represent relationship sets.
 Attributes listed inside entity rectangle or as ellipses connected to the entity.
 Underline indicates primary key attributes

Database Design and Applications (CSI ZG518/ SSZG518) Pilani Campus


BITS Pilani, Hyderabad Campus
Attributes
An entity is represented by a set of attributes, that is descriptive properties possessed by all
members of an entity set.
– Example:
instructor = (ID, name, street, city, salary )
course= (course_id, title, credits)
Domain – the set of permitted values for each attribute
Attribute types:
– Simple and composite attributes.
– Single-valued and multivalued attributes
• Example: multivalued attribute: phone_numbers
– Derived attributes
• Can be computed from other attributes
• Example: age, given date_of_birth

Database Design and Applications (CSI ZG518/ SSZG518) Pilani Campus


BITS Pilani, Hyderabad Campus
Redundant (Derived) Attributes
Suppose we have entity sets
– instructor, with attributes including dept_name
– department
and a relationship
– inst_dept relating instructor and department
Attribute dept_name in entity instructor is redundant since there is an explicit relationship
inst_dept which relates instructors to departments
– The attribute replicates information present in the relationship, and should be removed
from instructor

Database Design and Applications (CSI ZG518/ SSZG518) Pilani Campus


BITS Pilani, Hyderabad Campus
Composite Attributes

Database Design and Applications (CSI ZG518/ SSZG518) Pilani Campus


BITS Pilani, Hyderabad Campus
Entity With Composite, Multivalued, and Derived
Attributes

composite

multivalued

derived

Database Design and Applications (CSI ZG518/ SSZG518) Pilani Campus


BITS Pilani, Hyderabad Campus
Relationship Sets with Attributes

Database Design and Applications (CSI ZG518/ SSZG518) Pilani Campus


BITS Pilani, Hyderabad Campus
Roles
Entity sets of a relationship need not be distinct
– Each occurrence of an entity set plays a “role” in the relationship

The labels “course_id” and “prereq_id” are called roles.

Database Design and Applications (CSI ZG518/ SSZG518) Pilani Campus


BITS Pilani, Hyderabad Campus
Mapping Cardinality Constraints
Express the number of entities to which another entity can be associated via a relationship set.
Most useful in describing binary relationship sets.
For a binary relationship set the mapping cardinality must be one of the following types:
– One to one
– One to many
– Many to one
– Many to many

Database Design and Applications (CSI ZG518/ SSZG518) Pilani Campus


BITS Pilani, Hyderabad Campus
ER Notation for Cardinality
Constraints
We express cardinality constraints by drawing either
– a directed line (), signifying “one,” or
– an undirected line (—), signifying “many,”
between the relationship set and the entity set.
One-to-one relationship:
– A student is associated with at most one instructor via the relationship advisor
– A student is associated with at most one department via stud_dept

Database Design and Applications (CSI ZG518/ SSZG518) Pilani Campus


BITS Pilani, Hyderabad Campus
One-to-One Relationship

one-to-one relationship between an instructor and a student


– an instructor is associated with at most one student via advisor
– and a student is associated with at most one instructor via advisor

Database Design and Applications (CSI ZG518/ SSZG518) Pilani Campus


BITS Pilani, Hyderabad Campus
One-to-Many Relationship

one-to-many relationship between an instructor and a student


– an instructor is associated with several (including 0) students via advisor
– a student is associated with at most one instructor via advisor,

Database Design and Applications (CSI ZG518/ SSZG518) Pilani Campus


BITS Pilani, Hyderabad Campus
Many-to-One Relationships

In a many-to-one relationship between an instructor and a student,


– an instructor is associated with at most one student via advisor,
– and a student is associated with several (including 0) instructors via advisor

Database Design and Applications (CSI ZG518/ SSZG518) Pilani Campus


BITS Pilani, Hyderabad Campus
Many-to-Many
Relationship
An instructor is associated with several (possibly 0) students via advisor
A student is associated with several (possibly 0) instructors via advisor

Database Design and Applications (CSI ZG518/ SSZG518) Pilani Campus


BITS Pilani, Hyderabad Campus
Degree of a Relationship Set
binary relationship
– involve two entity sets (or degree two).
– most relationship sets in a database system are binary.
Relationships between more than two entity sets are rare. Most relationships are binary. (More
on this later.)
 Example: students work on research projects under the guidance of an instructor.
 relationship proj_guide is a ternary relationship between instructor, student, and project

Database Design and Applications (CSI ZG518/ SSZG518) Pilani Campus


BITS Pilani, Hyderabad Campus
E-R Diagram with a Ternary Relationship

Database Design and Applications (CSI ZG518/ SSZG518) Pilani Campus


BITS Pilani, Hyderabad Campus
Cardinality Constraints on Ternary
Relationship

We allow at most one arrow out of a ternary (or greater degree) relationship to indicate
a cardinality constraint
E.g., an arrow from proj_guide to instructor indicates each student has at most one
guide for a project
If there is more than one arrow, there are two ways of defining the meaning.
– E.g., a ternary relationship R between A, B and C with arrows to B and C could mean
1. each A entity is associated with a unique entity from B and C or
2. each pair of entities from (A, B) is associated with a unique C entity, and each pair (A,
C) is associated with a unique B
– Each alternative has been used in different formalisms
– To avoid confusion we outlaw more than one arrow

Database Design and Applications (CSI ZG518/ SSZG518) Pilani Campus


BITS Pilani, Hyderabad Campus
Participation of an Entity Set in a
Relationship Set
 Total participation (indicated by double line): every entity in the entity set
participates in at least one relationship in the relationship set
 Example: participation of loan in borrower relationship is total.

 Partial participation: some entities may not participate in any


relationship in the relationship set
E.g., participation of customer in borrower relationship is partial

Database Design and Applications (CSI ZG518/ SSZG518) Pilani Campus


BITS Pilani, Hyderabad Campus
Weak Entity Sets

An entity set that does not have a primary key is referred to as a weak entity set.
The existence of a weak entity set depends on the existence of a identifying
entity set
– it must relate to the identifying entity set via a total, many-to-one relationship set
from the weak entity set to the identifying entity set
– Identifying relationship depicted using a double diamond
The discriminator (or partial key) of a weak entity set is the set of attributes that
distinguishes among all the entities of a weak entity set.
The primary key of a weak entity set is formed by the primary key of the strong
entity set on which the weak entity set is existence dependent, plus the weak
entity set’s discriminator.

Database Design and Applications (CSI ZG518/ SSZG518) Pilani Campus


BITS Pilani, Hyderabad Campus
Weak Entity Sets

We depict a weak entity set by double rectangles.


We underline the discriminator of a weak entity set with a dashed line.
payment_number – discriminator of the payment entity set
Primary key for payment – (loan_number, payment_number)

Database Design and Applications (CSI ZG518/ SSZG518) Pilani Campus


BITS Pilani, Hyderabad Campus
Weak Entity Sets

• Identifying relationship does not have any descriptive attributes.


• A weak entity set can be associated with more than one identifying entity sets
too.

Database Design and Applications (CSI ZG518/ SSZG518) Pilani Campus


BITS Pilani, Hyderabad Campus
Extended ER Features
 Specialization/Generalization
 Aggregation

Database Design and Applications (CSI ZG518/ SSZG518) Pilani Campus


BITS Pilani, Hyderabad Campus
Extended ER Features
 Specialization

Entity set person with attributes ID, name, address

Subset of entities within an entity set may have attributes that are not shared by all the entities
in the entity set

Employee ( all attributes of person + salary attribute)


Student ( all attributes of person + cgpa attribute)

Specialization is depicted by a hollow arrow-head pointing from the specialized entity to the
other entity

Database Design and Applications (CSI ZG518/ SSZG518) Pilani Campus


BITS Pilani, Hyderabad Campus
Specialization

Overlapping
specialization: entity
may belong to multiple
specialized entity sets
Disjoint specialization:
entity may belong to
at most one
specialized entity set

Top-down design
process

Database Design and Applications (CSI ZG518/ SSZG518) Pilani Campus


BITS Pilani, Hyderabad Campus
Generalization

Bottom up process
From employee (ID,name, address, salary) and student (ID, name, address
,cgpa) to person(ID, name, address)
Generalization proceeds from the recognition that a number of entity sets share
some common features.
On the basis of their commonalities, generalization synthesizes these entity
sets into a single, higher-level entity set.

Specialization: single entity set, find differences among entities, create distinct
lower level entity sets that may have additional attributes and may participate
in additional relationships

Database Design and Applications (CSI ZG518/ SSZG518) Pilani Campus


BITS Pilani, Hyderabad Campus
Design Constraints on a
Specialization/Generalization

Constraint on which entities can be members of a given lower-level entity set.


– condition-defined
• Example: all customers over 65 years are members of senior-citizen entity set; senior-citizen
ISA person.
– user-defined
Constraint on whether or not entities may belong to more than one lower-level entity set within a
single generalization.
– Disjoint
• an entity can belong to only one lower-level entity set

– Overlapping
• an entity can belong to more than one lower-level entity set

Database Design and Applications (CSI ZG518/ SSZG518) Pilani Campus


BITS Pilani, Hyderabad Campus
Design Constraints on a
Specialization/Generalization (Cont.)
Completeness constraint -- specifies whether or not an entity in the higher-level entity set
must belong to at least one of the lower-level entity sets within a generalization.
– total : an entity must belong to one of the lower-level entity sets
– partial: an entity need not belong to one of the lower-level entity sets

Database Design and Applications (CSI ZG518/ SSZG518) Pilani Campus


BITS Pilani, Hyderabad Campus
Aggregation

ER models seen till now can’t express relationships among relationships.

Proj_guide with instructor, student, and project.

Evaluation report as an entity.

Have a quaternary relationship??


Have a multivalued composite attribute of proj_guide?
What If report has a relation with dean entity?

Database Design and Applications (CSI ZG518/ SSZG518) Pilani Campus


BITS Pilani, Hyderabad Campus
Aggregation

Redundancy??

Database Design and Applications (CSI ZG518/ SSZG518) Pilani Campus


BITS Pilani, Hyderabad Campus
Aggregation
– Treat relationship as an
abstract entity
– Allows relationships between
relationships
– Abstraction of relationship into
new entity

Database Design and Applications (CSI ZG518/ SSZG518) Pilani Campus


BITS Pilani, Hyderabad Campus
Symbols summary

Database Design and Applications (CSI ZG518/ SSZG518) BITS Pilani, Pilani Campus
Used in recursive relationship sets

Database Design and Applications (CSI ZG518/ SSZG518) BITS Pilani, Pilani Campus
Database Design and Applications (CSI ZG518/ SSZG518) BITS Pilani, Pilani Campus
Database Design and Applications (CSI ZG518/ SSZG518) BITS Pilani, Pilani Campus
E-R Design Decisions

o The use of an attribute or entity set to represent an object.


o Whether a real-world concept is best expressed by an entity set or a
relationship set.
o The use of a ternary relationship versus a pair of binary relationships.
o The use of a strong or weak entity set.
o The use of specialization/generalization – contributes to modularity in the
design.
o The use of aggregation – can treat the aggregate entity set as a single unit
without concern for the details of its internal structure.

Database Design and Applications (CSI ZG518/ SSZG518) Pilani Campus


BITS Pilani, Hyderabad Campus
ER diagram for
University
database

Database Design and Applications (CSI ZG518/ SSZG518) BITS BITS PilaniPilani
Pilani,Pilani,
HyderabadCampusCampus
Relational schema

Database Design and Applications (CSI ZG518/ SSZG518) BITS Pilani, Pilani Campus

Potrebbero piacerti anche