You are on page 1of 70

UNIT II.

   RELATIONAL MODEL
ER and EER diagrams:
Components of ER model, Conventions, Converting ER
diagrams into tables
Relational Model: Basic concepts, Attributes and Domains,
Codd’s rules.
Relational Integrity : Nulls, Entity, Referential integrities,
Enterprise constraints, Views, Schema Diagram.

Case Study
Student / Timetable / Reservation / any data
Management System
E-R Diagrams

 Rectangles represent entity sets.


 Diamonds represent relationship sets.
 Lines link attributes to entity sets and entity sets to relationship sets.
 Ellipses represent attributes
 Double ellipses represent multivalued attributes.
 Dashed ellipses denote derived attributes.
 Underline indicates primary key attributes.
E-R Diagram With Composite, Multivalued, and
Derived Attributes
Relationship Sets with Attributes
Roles
The labels “manager” and “worker” are called roles.
They specify how employee entities interact via the
works-for relationship set.
Roles are indicated in E-R diagrams by labeling the lines
that connect diamonds to rectangles.
Role labels are optional, and are used to clarify semantics
of the relationship
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.
E.g.: One-to-one relationship:
 A customer is associated with at most one loan via the

relationship borrower
 A loan is associated with at most one customer via

borrower
One-To-Many Relationship
In the one-to-many relationship a loan is associated with at
most one customer via borrower, a customer is associated
with several (including 0) loans via borrower
Many-To-Many Relationship

A customer is associated with several (possibly 0)


loans via borrower
A loan is associated with several (possibly 0)
customers via borrower
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
 E.g. participation of loan in borrower is total
 every loan must have a customer associated to it via borrower
 Partial participation: some entities may not participate in any relationship in
the relationship set
 E.g. participation of customer in borrower is partial
Alternative Notation for Cardinality Limits

 Cardinality limits can also express participation constraints


Weak Entity Sets
An entity set that does not have a primary key is referred to as a
weak 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.
Weak Entity Sets (Cont.)
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)
More Weak Entity Set Examples
In a university, a course is a strong entity and a
course-offering can be modeled as a weak entity

The discriminator of course-offering would be


semester (including year) and section-number (if
there is more than one section)
Extended Entity-Relationship Model
• Since 1980s there has been an increase in emergence of
new database applications with more demanding
requirements.

• Basic concepts of ER modelling are not sufficient to


represent requirements of newer, more complex
applications.

• Response is development of additional ‘semantic’


modelling concepts.
The enhanced entity-relationship (EER) model is a high-level
or conceptual data model incorporating extensions to the original
entity-relationship (ER) model, used in the design of databases.

 It was developed to reflect more precisely the properties and


constraints that are found in more complex databases.

The EER model includes all of the concepts introduced by the


ER model.
Additionally it includes the concepts of a subclass and super
class, along with the concepts of specialization and
generalization.
Subclasses and Super classes
An entity type may have additional meaningful sub groupings
of its entities.

Example: EMPLOYEE may be further grouped into


SECRETARY,
ENGINEER,
MANAGER,
TECHNICIAN,
SALARIED_EMPLOYEE,
HOURLY_EMPLOYEE,…
 Each of these groupings is a subset of EMPLOYEE entities
 Each is called a subclass of EMPLOYEE
 EMPLOYEE is the super class for each of these subclasses

These are called super class/subclass relationships.

Example: EMPLOYEE/SECRETARY,
EMPLOYEE/TECHNICIAN
Another example of super class and sub class:-

Super class
vehicle

truck scooter
Bus car

Sub class
Specialization
Is the process of defining a set of subclasses of a super class.
The set of subclasses is based upon some distinguishing
characteristics of the entities in the super class.
Example: {SECRETARY, ENGINEER, TECHNICIAN} is a
specialization of EMPLOYEE based upon job type.
 May have several specializations of the same super class.
Example: Another specialization of EMPLOYEE based in
method of pay is {SALARIED_EMPLOYEE,
HOURLY_EMPLOYEE}.
Super class/subclass relationships and specialization can be
diagrammatically represented in EER diagrams.

student sid
name
is A

Undergrad graduate

Attributes of a subclass are called specific attributes.


For example, Typing Speed of SECRETARY
The subclass can participate in specific relationship types.
For example, BELONGS_TO of HOURLY_EMPLOYEE
Example of a Specialization
Generalization
The reverse of the specialization process.

Several classes with common features are generalized into a


super class; original classes become its subclasses.

Example: CAR, TRUCK generalized into VEHICLE.

Both CAR, TRUCK become subclasses of the super class


VEHICLE.

 We can view {CAR, TRUCK} as a specialization of


VEHICLE
 Alternatively, we can view VEHICLE as a generalization
of CAR and TRUCK
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.
Generalization and Specialization
 Arrow pointing to the generalized super class represents a
generalization .
 Arrows pointing to the specialized subclasses represent a
specialization.
 We advocate not drawing any arrows in these situations.
 A super class or subclass represents a set of entities.
 Shown in rectangles in EER diagrams (as are entity types).
 Sometimes, all entity sets are simply called classes,
whether they are entity types, super classes, or subclasses
GENERALIZATION AND SPECIALIZATION

student sid
name
is A
Generalization Specialization

Undergrad graduate
Generalization Specialization

Generalization Specialization
Aggregation
The E-R model cannot express relationships among relationships.
When would we need such a thing?
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
Hours Member
Name Id

Project
Employee Work

Users

Figure E-R diagram with


Machinery redundant relationships

Id
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.

  
Hours Member
Name Id

Work Project
Employee Work

Users

Machinery

Id

Figure shows the E-R diagram with aggregation.


Summary of Symbols Used in
E-R Notation
E-R Diagram for a Banking Enterprise
Converting E-R diagram into tab
les
The Relational Model
Data and relationships are represented by a collection of tables
Each table has a number of columns with unique names, e.g.
customer, account
Figure shows a sample relational database

Fig:-A sample relational database.


Example of a Relation
attributes
(or columns)

tuples
(or rows)
Codd's Rules
Codd's twelve rules are a set of
thirteen rules (numbered zero to
twelve) proposed by Edgar F. Codd, a
pioneer of the relational model.
For databases, designed to define what
is required from a database
management system in order for it to
be considered relational, i.e., a
relational database management
system (RDBMS).
0.Foundation Rule
 A relational database management system must manage its
stored data using only its relational capabilities.

1. Information Rule
 All information in the database should be represented in one
and only one way - as values in a table.

2.Guaranteed Access Rule


 Each and every datum (atomic value) is guaranteed to be
logically accessible by resorting to a combination of table
name, primary key value and column name.
3. Systematic Treatment of Null Values

 Null values (distinct from empty character string or a string of


blank characters and distinct from zero or any other number)
are supported in the fully relational DBMS for representing
missing information in a systematic way, independent of data
type.

4. Dynamic On-line Catalog Based on the Relational Model

 The database description is represented at the logical level in


the same way as ordinary data,
 so authorized users can apply the same relational language to
its interrogation as they apply to regular data.
5. Comprehensive Data Sublanguage Rule
 A relational system may support several languages and
various modes of terminal use.
 However, there must be at least one language whose
statements are expressible, per some well-defined syntax, as
character strings and whose ability to support all of the
following is comprehensible:
view definition
data definition
data manipulation (interactive and by program)
integrity constraints
authorization
transaction boundaries (begin, commit, and rollback).
6. View Updating Rule
 All views that are theoretically updateable are also updateable
by the system
7. High-level Insert, Update, and Delete

 The capability of handling a base relation or a derived


relation as a single operand applies nor only to the retrieval of
data but also to the insertion, update, and deletion of data.

8. Physical Data Independence


 Application programs and terminal activities remain logically
unimpaired whenever any changes are made in either storage
representation or access methods.

9.Logical Data Independence


 Application programs and terminal activities remain logically
unimpaired when information preserving changes of any kind
that theoretically permit unimpairment are made to the base
tables.
10. Integrity Independence
 Integrity constraints specific to a particular relational
database must be definable in the relational data sublanguage
and storable in the catalog, not in the application programs.

11. Distribution Independence


 The data manipulation sublanguage of a relational DBMS
must enable application programs and terminal activities to
remain logically unimpaired whether and whenever data are
physically centralized or distributed.

12.Nonsubversion Rule
 If a relational system has or supports a low-level (single-
record-at-a-time) language, that low-level language cannot be
used to subvert or bypass the integrity rules or constraints
expressed in the higher-level (multiple-records-at-a-time)
relational language
The Network Model

Data are represented by collections of records

Relationships among data are represented by links

Organization is that of an arbitrary graph.

The network model is a database model conceived as a


flexible way of representing objects and their relationships
Figure shows a sample network database that is the equivalent
of the relational database

Relational Model

Network Model
The Hierarchical Model
Similar to the network model
A hierarchical data model is a data model in which the data
is organized into a tree-like structure.
Organization of the records is as a collection of trees, rather
than arbitrary graphs
The structure allows representing information using
parent/child relationships: each parent can have many children
but each child only has one parent (also known as a 1:many
ratio ).
In a database, an entity type is the equivalent of a table
each individual record is represented as a row and
an attribute as a column.
A sample hierarchical database
Schema Diagram
A database schema ,along with primary key and foreign key
dependencies, can be depicted pictorially by schema diagram.

In schema diagram each relation appears as a box, with


attributes listed inside it and relation name above it.

Foreign key dependencies appear as arrow from the foreign


key attributes of the referencing relation to the primary key of
the referenced relation.
Schema Diagram for the Banking Enterprise
Relation name

Primary key
Foreign key
Relation

Attribute
Schema Diagram for University Database
Integrity
Integrity constraints are used to ensure accuracy and
consistency of data in a relational database.
Data integrity is handled in a relational database through the
concept of referential integrity.

There are many types of integrity constraints that play a role in


referential integrity-
Entity integrity :-
The entity integrity constraint states that no primary key value
can be null.
This is because the primary key value is used to identify
individual tuples in a relation.
Having null value for the primary key implies that we cannot
identify some tuples.
This also specifies that there may not be any duplicate entries
in primary key column.
 Referential Integrity :-
The referential integrity constraint is specified between two relations
and is used to maintain the consistency among tuples in the two
relations.

The referential integrity constraint states that a tuple in one relation


that refers to another relation must refer to an existing tuple in that
relation.

Referential integrity is with the relationships between the tables of a


database, ie that the data of one table does not contradict the data of
another table.

Specifically, every foreign key value in a table must have a matching


primary key value in the related table.

This is the most common type of integrity constraint.

This is used to manage the relationships between primary and foreign


keys.
Domain Integrity :-

A domain is defined as the set of all unique values permitted for


an attribute.
For example, a domain of Date is the set of all possible valid
dates.
A domain of Integer is all possible whole numbers.
A domain of day-of-week is Monday, Tuesday ... Sunday.

The domain integrity states that every element from a relation


should respect the type and restrictions of its corresponding
attribute.
Restrictions could be the range of values that the element can
have, the default value if none is provided, and if the element
can be NULL.
Column Constraints :-

During the data analysis phase, business rules will identify any
column constraints.
For example, a salary cannot be negative.
an employee number must be in the range 1000 - 2000, etc.

User-Defined Integrity Constraints :-

Business rules may dictate that when a specific action occurs,


further actions should be triggered.
For example, deletion of a record automatically writes that record
to an audit table.
Views
Views in SQL are kind of virtual tables.

A view also has rows and columns as they are in a real table in
the database.

We can create a view by selecting fields from one or more


tables present in the database.

A View can either have all the rows of a table or specific rows
based on certain condition.
Syntax:

CREATE VIEW view_name AS  
SELECT column1, column2.....  
FROM table_name  
WHERE condition;
Example
CREATE VIEW DetailsView AS  
SELECT NAME, ADDRESS  
FROM Student_Details  
WHERE STU_ID < 4; 

SELECT * FROM DetailsView; 
Sample table:
Student_Detail
STU_ID NAME ADDRESS
1 Stephan Delhi
2 Kathrin Noida
3 David Ghaziabad
4 Alina Gurugram

Student_Marks
STU_ID NAME MARKS AGE
1 Stephan 97 19
2 Kathrin 86 21
3 David 74 18
4 Alina 90 20
5 John 96 18
Syntax:

CREATE VIEW view_name AS  
SELECT column1, column2.....  
FROM table_name  
WHERE condition;  
Creating View from a single table
In this example, we create a View named DetailsView
from the table Student_Detail.

CREATE VIEW DetailsView AS  
SELECT NAME, ADDRESS  
FROM Student_Details  
WHERE STU_ID < 4;
  
Just like table query, we can query the view to view the data.
SELECT * FROM DetailsView;  

NAME ADDRESS
Stephan Delhi
Kathrin Noida
David Ghaziabad
Creating View from multiple tables

View from multiple tables can be created by simply include multiple


tables in the SELECT statement.
In the given example, a view is created named MarksView from two
tables Student_Detail and Student_Marks.
CREATE VIEW MarksView AS  
SELECT Student_Detail.NAME, Student_Detail.ADDRESS, Student
_Marks.MARKS  FROM Student_Detail, Student_Mark
WHERE Student_Detail.NAME = Student_Marks.NAME; 
SELECT * FROM MarksView;  

NAME ADDRESS MARKS


Stephan Delhi 97
Kathrin Noida 86
David Ghaziabad 74
Alina Gurugram 90
Case Study

Student / Timetable / Reservation / any


data Management System
E-R Model Case Studies
1: Suppose you are given the following requirements for a simple
database for the National Hockey League (NHL):
 The NHL has many teams,
 Each team has a name, a city, a coach, a captain, and a set of
players,
 Each player belongs to only one team,
 Each player has a name, a position (such as left wing or goalie), a
skill level, and a set of injury records,
 A team captain is also a player,
 A game is played between two teams (referred to as host_team
and guest_team) and has a date (such as May 11th, 1999) and a
score (such as 4 to 2).
Construct a clean and concise ER diagram for the NHL database
2: 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 identi-cation 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.
3: (a) 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.
(b) Construct appropriate tables for the above ER Diagram ?
4: (a) Construct an E-R diagram for a hospital with a set of patients
and a set of medical doctors. Associate with each patient a log of the
various tests and examinations conducted
(b) Construct appropriate tables for the above ER Diagram :

Patient(SS#, name, insurance)

Physician ( name, specialization)

Test-log( SS#, test-name, date, time)

Doctor-patient (physician-name, SS#)

Patient-history (SS#, test-name, date)


Thank You!!!

You might also like