You are on page 1of 79

CS F212:Database

Systems
BITS Pilani Dr.Debasis Das
Department of Computer Science and Information Systems
K K Birla Goa Campus

Lecture Session – 3
24 - 01 - 2018
Data Modeling
BITS Pilani
K K Birla Goa Campus
Outline

 Overview of Data Modeling


 Entity-Relationship Model

BITS Pilani, K K Birla Goa Campus


Data Modeling

BITS Pilani, K K Birla Goa Campus


An Overview of Data Models

 The notion of a “data model” is one of the most


fundamental in the study of database systems.
 In this brief summary of the concept, we define
some basic terminology and mention the most
important data models.

BITS Pilani, K K Birla Goa Campus


What is a Data Model?

A data model is a notation for describing data or


information. The description generally consists of
three parts:
 Structure of the data
 Operations on the data
 Constraints on the data

BITS Pilani, K K Birla Goa Campus


Structure of the data
 You may be familiar with tools in programming
languages such as C/C++/Java for describing the
structure of the data used by a program: arrays and
structures (“structs”) or objects, for example.
 The data structures used to implement data in the
computer are sometimes referred to, in discussions of
database systems, as a physical data model, although in
fact they are far removed from the gates and electrons
that truly serve as the physical implementation of the
data.

BITS Pilani, K K Birla Goa Campus


Structure of the data

• In the database world, data models are at a


somewhat higher level than data structures, and
are sometimes referred to as a conceptual model to
emphasize the difference in level.

BITS Pilani, K K Birla Goa Campus


Operations on the data
 In programming languages, operations on the data are
generally anything that can be programmed.
 In database data models, there is usually a limited set
of operations that can be performed. We are generally
allowed to perform a limited set of queries (operations
that retrieve information) and modifications
(operations that change the database).
 By limiting operations, it is possible for programmers
to describe database operations at a very high level, yet
have the database management system implement the
operations efficiently.
BITS Pilani, K K Birla Goa Campus
Constraints on the data
 Database data models usually have a way to describe
limitations on what the data can be.
 These constraints can range from the simple (e.g., “a
day of the week is an integer between 1 and 7”) to
some very complex limitations.

BITS Pilani, K K Birla Goa Campus


The database modeling and
Implementation process

BITS Pilani, K K Birla Goa Campus


Entity-Relationship Model

 However, in practice it is often easier to start with a


higher-level model(i.e., Entity Relationship Model) and
then convert the design to the relational model.
 In the entity-relationship model (or E /R model), the
structure of data is represented graphically, as an
“entity-relationship diagram,” using three principal
element types:
1. Entity sets,
2. Attributes, and
3. Relationships.

BITS Pilani, K K Birla Goa Campus


Entity-Relationship Diagrams

 An E /R diagram is a graph representing entity sets,


attributes, and relationships. Elements of each of these
kinds are represented by nodes of the graph, and we use
a special shape of node to indicate the kind, as follows:

 Entity sets are represented by rectangles.


 Attributes are represented by ovals/ellipses.
 Relationships are represented by diamonds.

 Edges connect an entity set to its attributes and also


connect a relationship to its entity sets.

BITS Pilani, K K Birla Goa Campus


Entity Sets
 An entity is an abstract object of some sort, and a
collection of similar entities forms an entity set. An
entity in some ways resembles an “object” in the sense
of object-oriented programming.
 However, the E /R model is a static concept, involving
the structure of data and not the operations on data.
 Let us consider the design of our running movie
database example. Each movie is an entity, and the set
of all movies constitutes an entity set. Likewise, the
stars are entities, and the set of stars is an entity set.

BITS Pilani, K K Birla Goa Campus


Entity Sets

BITS Pilani, K K Birla Goa Campus


Entity Sets customer and loan

BITS Pilani, K K Birla Goa Campus


Attributes
• Entity sets have associated attributes, which are
properties of the entities in that set. For instance, the
entity set Movies might be given attributes such as title
and length.
• In our version of the E /R model, we shall assume that
attributes are of primitive types, such as strings,
integers, or real's.

BITS Pilani, K K Birla Goa Campus


Attributes

BITS Pilani, K K Birla Goa Campus


Composite Attributes

 For example, the attribute “customer address” can


have the attributes number, street, city, and state. These
are called composite attributes.

BITS Pilani, K K Birla Goa Campus


Composite Attributes

BITS Pilani, K K Birla Goa Campus


Multivalued Attribute
 If an attribute can have more than one value it is called an
multivalued attribute. For example a teacher entity can have
multiple subject values.

BITS Pilani, K K Birla Goa Campus


Derived Attribute

 An attribute based on another attribute. This is found


rarely in ER diagrams. For example for a circle the area
can be derived from the radius.

BITS Pilani, K K Birla Goa Campus


Movies: Relation, Entity Set, and
Attributes

BITS Pilani, K K Birla Goa Campus


Relationships
 Relationships are connections among two or more
entity sets.
 For instance, if Movies and Stars are two entity sets,
we could have a relationship Stars-in that connects
movies and stars.
 The intent is that a movie entity m is related to a star
entity s by the relationship Stars-in if s appears in
movie m.

BITS Pilani, K K Birla Goa Campus


Entity-Relationship Diagrams
 An E /R diagram is a graph representing entity sets,
attributes, and relationships. Elements of each of these
kinds are represented by nodes of the graph, and we use
a special shape of node to indicate the kind, as follows:

 Entity sets are represented by rectangles.


 Attributes are represented by ovals/ellipses.
 Relationships are represented by diamonds.

 Edges connect an entity set to its attributes and also


connect a relationship to its entity sets.

BITS Pilani, K K Birla Goa Campus


An entity-relationship diagram for the
movie database

BITS Pilani, K K Birla Goa Campus


Relationships And Relationship Sets

 In Figure we show the relationship set Works In, in


which each relationship indicates a department in which
an employee works. Note that several relationship sets
might involve the same entity sets. For example, we
could also have a Manages relationship set involving
Employees and Departments.

BITS Pilani, K K Birla Goa Campus


Relationships And Relationship Sets

 A relationship is an association among two or more


entities. For example, we may have the relationship
that Attishoo works in the pharmacy department. As
with entities, we may wish to collect a set of similar
relationships into a relationship set

BITS Pilani, K K Birla Goa Campus


ERD Example

BITS Pilani, K K Birla Goa Campus


ERD Example
 A relationship can also have descriptive attributes. Descriptive
attributes are used to record information about the relationship,
rather than about any one of the participating entities; for example,
we may wish to record that Attishoo works in the
pharmacy department as of January 1991.
 This information is captured in Figure 2.2 by adding an attribute,
since, to Works In. A relationship must be uniquely identified by
the participating entities, without reference to the descriptive
attributes. In the Works In relationship set, for example, each
Works In relationship must be uniquely identified by the
combination of employee ssn and department did. Thus, for a given
employee-department pair, we cannot have more than one
associated since value.
BITS Pilani, K K Birla Goa Campus
Relationship Set borrower

BITS Pilani, K K Birla Goa Campus


Relationship Sets

BITS Pilani, K K Birla Goa Campus


Degree of a Relationship Set

BITS Pilani, K K Birla Goa Campus


Mapping Cardinalities
(Cardinality Constraints)

BITS Pilani, K K Birla Goa Campus


Mapping Cardinalities

BITS Pilani, K K Birla Goa Campus


Mapping Cardinalities

BITS Pilani, K K Birla Goa Campus


E-R Diagrams

BITS Pilani, K K Birla Goa Campus


E-R Diagrams

BITS Pilani, K K Birla Goa Campus


ERD With Composite, Multivalued, and
Derived Attributes

BITS Pilani, K K Birla Goa Campus


Relationship Sets with Attributes

BITS Pilani, K K Birla Goa Campus


BITS Pilani, K K Birla Goa Campus
BITS Pilani, K K Birla Goa Campus
BITS Pilani, K K Birla Goa Campus
BITS Pilani, K K Birla Goa Campus
Key, Candidate Key, Primary key
 A key is a minimal set of attributes whose values
uniquely identify an entity in the set.
 There could be more than one candidate key; if so,
we designate one of them as the primary key.
 For now we will assume that each entity set contains
at least one set of attributes that uniquely identifies
an entity in the entity set; that is, the set of attributes
contains a key.

BITS Pilani, K K Birla Goa Campus


BITS Pilani, K K Birla Goa Campus
ERD Example
 The Employees entity set with attributes ssn, name, and lot is
shown in Figure 2.1. An entity set is represented by a
rectangle, and an attribute is represented by an oval. Each
attribute in the primary key is underlined. The domain
information could be listed along with the attribute name, but
we omit this to keep the figures compact. The key is ssn.

BITS Pilani, K K Birla Goa Campus


An entity-relationship diagram for the
movie database

BITS Pilani, K K Birla Goa Campus


E-R Diagram with a Ternary
Relationship

BITS Pilani, K K Birla Goa Campus


Multiway Relationships
 The E /R model makes it convenient to define
relationships involving more than two entity sets. In
practice, ternary (three-way) or higher-degree
relationships are rare, but they occasionally are
necessary to reflect the true state of affairs.
 A multiway relationship in an E /R diagram is
represented by lines from the relationship diamond
to each of the involved entity sets.

BITS Pilani, K K Birla Goa Campus


A three-way relationship

BITS Pilani, K K Birla Goa Campus


A relationship with an attribute

BITS Pilani, K K Birla Goa Campus


Weak Entities
 Some entity sets in real world naturally depend on
some other entity set. They can be uniquely identified
only if combined with another entity set.
 Example:
 section1, section2, … become unique only if you put
them into a context, e.g. csce4350

BITS Pilani, K K Birla Goa Campus


Weak Entity Set Notations
 Double rectangles for weak entity set
 Double diamond for weak entity relationship
 Dashed underscore for discriminator

BITS Pilani, K K Birla Goa Campus


Weak Entity Set Notations

 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)

BITS Pilani, K K Birla Goa Campus


Specialization
 Top-down design process; we designate
subgroupings within an entity set that are distinctive
from other entities in the set.
 These subgroupings become lower-level entity sets
that have attributes or participate in relationships that
do not apply to the higher-level entity set.
 Depicted by a triangle component labeled ISA (E.g.
customer “is a” person).
 Attribute inheritance – a lower-level entity set
inherits all the attributes and relationship participation
of the higher-level entity set to which it is linked.

BITS Pilani, K K Birla Goa Campus


Specialization Example

BITS Pilani, K K Birla Goa Campus


Generalization
 A bottom-up design process – combine a number of
entity sets that share the same features into a higher-
level entity set.
 Specialization and generalization are simple
inversions of each other; they are represented in an E-
R diagram in the same way.
 The terms specialization and generalization are used
interchangeably.

BITS Pilani, K K Birla Goa Campus


Specialization and Generalization
 Can have multiple specializations of an entity set
based on different features.
 E.g. permanent-employee vs. temporary-employee,
in addition to officer vs. secretary vs. teller
 Each particular employee would be
 a member of one of permanent-employee or
temporary-employee,
 and also a member of one of officer, secretary, or
teller
 The ISA relationship also referred to as superclass -
subclass relationship
BITS Pilani, K K Birla Goa Campus
Design Constraints on a
Specialization/Generalization
 Constraint on which entities can be members of a given lower-
level entity set.
 condition-defined
 E.g. 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
 Noted in E-R diagram by writing disjoint next to the
ISA triangle
 Overlapping
 an entity can belong to more than one lower-level entity
set

BITS Pilani, K K Birla Goa Campus


Design Constraints on a
Specialization/Generalization (Contd.)
 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

BITS Pilani, K K Birla Goa Campus


Aggregation

 The E-R model cannot express relationships among


relationships.
 Consider a DB with information about employees
who work on a particular project and use a number of
machines doing that work. We get the E-R diagram
shown in Figure.

BITS Pilani, K K Birla Goa Campus


Example

BITS Pilani, K K Birla Goa Campus


Aggregation.

 Relationship sets work and uses could be combined into


a single set. However, they shouldn't be, as this would
obscure the logical structure of this scheme.
 The solution is to use aggregation.
 An abstraction through which relationships are
treated as higher-level entities.
 For our example, we treat the relationship set work and
the entity sets employee and project as a higher-level
entity set called work.
 Figure shows the E-R diagram with aggregation.

BITS Pilani, K K Birla Goa Campus


E-R diagram with aggregation

BITS Pilani, K K Birla Goa Campus


Aggregation

 Transforming an E-R diagram with aggregation into


tabular form is easy. We create a table for each entity
and relationship set as before.
 The table for relationship set uses contains a column for
each attribute in the primary key of machinery and
work.

BITS Pilani, K K Birla Goa Campus


E-R Diagram With Aggregation

BITS Pilani, K K Birla Goa Campus


E-R Design Decisions
 The use of an attribute or entity set to represent an object.
 Whether a real-world concept is best expressed by an
entity set or a relationship set.
 The use of a ternary relationship versus a pair of binary
relationships.
 The use of a strong or weak entity set.
 The use of specialization/generalization – contributes to
modularity in the design.
 The use of aggregation – can treat the aggregate entity set
as a single unit without concern for the details of its
internal structure
BITS Pilani, K K Birla Goa Campus
E-R Diagram for a Banking Enterprise

BITS Pilani, K K Birla Goa Campus


Summary of Symbols Used in E-R
Notation

BITS Pilani, K K Birla Goa Campus


Summary of Symbols (Contd.)

BITS Pilani, K K Birla Goa Campus


Example 1
 Construct an E-R diagram for a car insurance company
whose customers own one or more cars each.
 Each car has associated with it zero to any number of
recorded accidents.
 Each insurance policy covers one or more cars, and has
one or more premium payments associated with it.
 Each payment is for a particular period of time, and
has an associated due date, and the date when the
payment was received.

BITS Pilani, K K Birla Goa Campus


E-R diagram for a car insurance
company

BITS Pilani, K K Birla Goa Campus


Car insurance tables:

The relational database schema is given below.

person (driver-id, name, address)


car (license, year, model)
accident (report-number, location, date)
owns (driver-id, license)
participated (report-number driver-id, license, damage-amount)

BITS Pilani, K K Birla Goa Campus


Example 2
Consider a database used to record the marks that students
get in different exams of different course offerings.
1. Construct an E-R diagram that models exams as entities,
and uses a ternary relationship, for the above database.
2. Construct an alternative E-R diagram that uses only a
binary relationship between students and course-
offerings. Make sure that only one relationship
exists between a particular student and course-offering
pair, yet you can represent the marks that a student gets
in different exams of a course offering

BITS Pilani, K K Birla Goa Campus


E-R diagram for marks database

BITS Pilani, K K Birla Goa Campus


Example 3
A university registrar’s office maintains data about the following
entities:
1. courses, including number, title, credits, syllabus, and prerequisites;
2. course offerings, including course number, year, semester, section
number, instructor(s), timings, and classroom;
3. students, including student-id, name, and program;
4. instructors, including identification number, name, department, and
title.
 Further, the enrollment of students in courses and grades awarded to
students in each course they are enrolled for must be appropriately
modeled. Construct an E-R diagram for the registrar’s office.
Document all assumptions that you make about the mapping
constraints
BITS Pilani, K K Birla Goa Campus
E-R diagram for a university.

BITS Pilani, K K Birla Goa Campus


Thanks!!!
Queries?

BITS Pilani, K K Birla Goa Campus

You might also like