CSE2004: Database Management System
Module 2
Data Modeling – ER Model and
Relational Model
Topics to be Covered
Entity-Relationship Model
Types of Attributes
Relationship
Structural Constraints
Relational Model
Relational Model Constraints
Mapping E-R Model to Relational Schema
Integrity Constraints
Entity-Relationship(ER) Model
The ER model is a high-level conceptual data model. It has not been
implemented in any commercial DBMS , but is a powerful short hand often
used in database design for a first rendition of the miniworld.
The ER model was introduced by Peter Chen in 1976, and is now the most
widely used conceptual data model.
Definitions
An entity is an object in the mini world.
An attribute is the properties of an entity which can have a value from a value set (domain)
An entity set is the set of entities of same type that same same properties
A relationship is an association among several entities
A relationship set is a mathematical relation among n 2 entities, each taken from entity sets
A key attribute of an entity type is one whose value uniquely identifies an entity of that type.
A combination of attributes may form a composite key.
If there is no applicable value for an attribute that attribute is set to a null value.
Attribute & Attribute Value
Student Attribute
ID Name Faculty Major
475020126-3 Kamonphon Science IT
465020100-0 PiyathiDa Science Comp SC.
Attribute Value
Entity Sets
instructor_ID instructor_name student-ID student_name
Database System Concepts, Silberschatz, Korth and Sudharsan 7th Edition
Relationship Set advisor
(student-instructor)
Database System Concepts, Silberschatz, Korth and Sudharsan 7th Edition
E-R Diagram - Symbols
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 (will study later)
Database System Concepts, Silberschatz, Korth and Sudharsan 4th Edition
E-R Diagram - Symbols
Database System Concepts, Silberschatz, Korth and Sudharsan 4th Edition
Summary of Symbols Used in E-R Notation
Summary of Symbols Used in E-R Notation
Types of Attributes
Attributes can be
composite / simple (atomic)
single-valued / multivalued
stored / derived
descriptive
key / nonkey.
Composite Attributes
Database System Concepts, Silberschatz, Korth and Sudharsan 7th Edition
Composite Attributes
Composite
Attribute
Address
ZipCode
District Street name
Single valued Vs. Multi valued
Attribute
Age Degree
A : 24 Years A : B.Sc.
B : 27 Years B : B.Sc, M.Sc., M.Phil., Ph.D.
Single Value Multi Value
Stored Vs. Derived
Attribute
Age Birth Day Stored Attribute
A : 24 Years A : 13 July 1980
B : 40 Years B : 23 Dec 1964
Derived Attribute
Entity with a multivalued attribute (Skill) and derived
attribute (Years_Employed)
ER Diagram with Composite, Multi-valued and
Derived Attributes
Database System Concepts, Silberschatz, Korth and Sudharsan 4th Edition
ER Diagram with Descriptive Attributes (Relationship
Sets with attributes)
Database System Concepts, Silberschatz, Korth and Sudharsan 4th Edition
EXample
The company stores the information about the currently working
employees. The information includes employee number, name gender
,salary, date of birth , date of joining, address, phone no ,department no ,
department name, email id. The name of the employee contains first
name, middle name and last name. The address includes house number,
street, city, state and zip code. Every employee is uniquely identified by
the employee no. The employee may have multiple phone numbers such
as home, office or mobile. The year of experience of each employee can
be calculated by year of joining entities associated with that employee.
Design an E-R diagram for a company database
Roles
Database System Concepts, Silberschatz, Korth and Sudharsan 4th Edition
Structural Constraints
Mapping Cardinalities
One-to-One
One-to-Many
Many-to-One
Many-to-Many
Participation Constraints
Total
Partial
Mapping Cardinalities – One-to-One
Example: One student can register for one
faculty
Database System Concepts, Silberschatz, Korth and Sudharsan 7th Edition
Mapping Cardinalities – One-to-Many
For example, one class is consisting of multiple
students.
Database System Concepts, Silberschatz, Korth and Sudharsan 7 th Edition
Mapping Cardinalities – Many-to-One
For example, many students belong to the
same class.
Database System Concepts, Silberschatz, Korth and Sudharsan 7 th Edition
Mapping Cardinalities – Many-to-Many
For example, Students as a group are associated with multiple
faculty members, and faculty members can be associated with
multiple students.
Database System Concepts, Silberschatz, Korth and Sudharsan 7 th Edition
Notation for Expressing More Complex Constraints
Database System Concepts, Silberschatz, Korth and Sudharsan 7 th Edition
Alternative: Crow’s Foot Notation
Total Participation
Total participation (indicated by double line): every entity in the entity set
participates in at least one relationship in the relationship set
Example: every student must have an associated instructor
Partial participation: some entities may not participate in any relationship in
the relationship set
Example: participation of instructor in advisor is partial
Database System Concepts, Silberschatz, Korth and Sudharsan 7 th Edition
ER Diagram with Ternary Relationship
Database System Concepts, Silberschatz, Korth and Sudharsan 4 th Edition
Weak Entity Set
Entity with No primary key
Weak Entity is depicted with Double
Rectangle
Weak Relationship is depicted with
Double Diamonds
Participation is Total for all weak
entity sets.
Mapping Cardinality is One-to-
Many
Weak entity depends on the
identifying entity (Strong entity) for
its unique representation.
Database System Concepts, Silberschatz, Korth and Sudharsan 4 th Edition
simple ER Diagram
Student entity has attributes such as Stu_Id, Stu_Name &
Stu_Addr and College entity has attributes such as Col_ID &
Col_Name. college can have many students however a student
cannot study in multiple colleges at the same time.Each student
must be study any one college.
Dr. M. Premalatha, VIT Chennai
Components of a ER Diagram
Dr. M. Premalatha, VIT Chennai
How to Create an Entity Relationship Diagram (ERD)
Dr. M. Premalatha, VIT Chennai
The music database stores details of a artists, albums, and tracks. Artists, albums, and
tracks each have a name. Artist, which has an artist_id attribute that uniquely identifies it.
Each Album entity is uniquely identified by its album_id combined with the artist_id of
the corresponding Artist entity. A Track entity is similarly uniquely identified by its
track_id combined with the related album_id and artist_id attributes. The Played entity is
uniquely identified by a combination of its played time, and the related track_id,
album_id, and artist_id attributes. Each track has a time length. An album is made by
exactly one artist. · An artist makes one or more albums. An album contains one or more
tracks. When a track is played, the date and time the playback began (to the nearest
second) should be recorded; this is used for reporting when a track was last played, as
well as the number of times music by an artist, from an album, or a track has been played.
Dr. M. Premalatha, VIT Chennai
Dr. M. Premalatha, VIT Chennai
A salesperson may manage many other salespeople. A salesperson is
managed by only one salespeople. A salesperson can be an agent for
many customers. A customer is managed by one salespeople. A
customer can place many orders. An order can be placed by one
customer. An order lists many inventory items. An inventory item may
be listed on many orders. An inventory item is assembled from many
parts. A part may be assembled into many inventory items. Many
employees assemble an inventory item from many parts. A supplier
supplies many parts. A part may be supplied by many suppliers.
Dr. M. Premalatha, VIT Chennai
Dr. M. Premalatha, VIT Chennai
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
Dr. M. Premalatha, VIT Chennai
Dr. M. Premalatha, VIT Chennai
Extended E-R Model
Specialization – Subgrouping
Generalization – Top-Down Design Process
Design Constraint
Disjoint
Overlapping
Completeness Constraint
Total
Partial
Attribute Inheritance
Database System Concepts, Silberschatz, Korth and Sudharsan 4 th Edition
Example
Database System Concepts, Silberschatz, Korth and Sudharsan 4 th Edition
Disjoint (d)
Disjointness Constraints
The subclasses must have disjoint sets of entities
Overlap (o)
The subclasses may have overlapping sets of entities
An attribute-defined specialization on the JobType attribute of EMPLOYEE.
Figure 4.5 Notation for specialization with overlapping (nondisjoint) subclasses.
Figure 4.7 A specialization lattice (with multiple inheritance) for a UNIVERSITY database.
Aggregation
Database System Concepts, Silberschatz, Korth and Sudharsan 4 th Edition
Dr. M. Premalatha, VIT Chennai
Aggregation
Aggregation represents a ‘has-a’ relationship between entity types, where
one represents the ‘whole’ and the other the ‘part’
An example of aggregation is the Car and Engine entities. A car is
made up of an engine. The car is the whole and the engine is the
part. Aggregation does not represent strong ownership. This
means, a part can exist on its own without the whole. There is no
stronger ownership between a car and the engine. An engine of a
car can be moved to another car.
Dr. M. Premalatha, VIT Chennai
Dr. M. Premalatha, VIT Chennai
Composition
Composition is a form of aggregation that represents an association between
entities, where there is a strong ownership between the ‘whole’ and the ‘part’.
For example, a tree and a branch have a composition relationship. A branch is
‘part’ of a ‘whole’ tree - we cannot cut the branch and add it to another tree
Summary of Symbols
Database System Concepts, Silberschatz, Korth and Sudharsan 4 th Edition
Example
Fundamentals Database Systems, Elmasri, Navathe, 7 th Edition
Dr. M. Premalatha, VIT Chennai
Dr. M. Premalatha, VIT Chennai
Relational Schema
Fundamentals Database Systems, Elmasri, Navathe, 7 th Edition
ER to Relational Mapping
ER-to-Relational Mapping Algorithm
Step 1: Mapping of Regular Entity Types
Step 2: Mapping of Weak Entity Types
Step 3: Mapping of Binary 1:1 Relation Types
Step 4: Mapping of Binary 1:N Relationship Types.
Step 5: Mapping of Binary M:N Relationship Types.
Step 6: Mapping of Multivalued attributes.
Step 7: Mapping of N-ary Relationship Types.
Mapping EER Model Constructs to Relations
Step 8: Options for Mapping Specialization or Generalization.
Step 9: Mapping of Union Types (Categories).
Fundamentals Database Systems, Elmasri, Navathe, 7 th Edition
ER to Relational Mapping
Fundamentals Database Systems, Elmasri, Navathe, 7 th Edition
ER to Relational Mapping
Fundamentals Database Systems, Elmasri, Navathe, 7 th Edition
EER to Relational Mapping
Fundamentals Database Systems, Elmasri, Navathe, 7 th Edition
EER to Relational Mapping
Fundamentals Database Systems, Elmasri, Navathe, 7 th Edition
EER to Relational Mapping
Fundamentals Database Systems, Elmasri, Navathe, 7 th Edition
EER to Relational Mapping
Fundamentals Database Systems, Elmasri, Navathe, 7 th Edition
EER to Relational Mapping - Exercise
Fundamentals Database Systems, Elmasri, Navathe, 7 th Edition
Relational Model
• The relational Model of Data is based on the concept of a
Relation
• Informally, a relation looks like a table of values.
• A relation typically contains a set of rows.
• The data elements in each row represent certain facts that
correspond to a real-world entity or relationship
• In the formal model, rows are called tuples
• Each column has a column header that gives an indication of the
meaning of the data items in that column
• In the formal model, the column header is called an attribute name (or
just attribute)
Fundamentals Database Systems, Elmasri, Navathe, 7 th Edition
Relational Model
Fundamentals Database Systems, Elmasri, Navathe, 7 th Edition
Relational Model
Informal Terms Formal Terms
Table Relation
Column Header Attribute
All possible Column Values Domain
Row Tuple
Table Definition Schema of a Relation
Populated Table State of the Relation
Fundamentals Database Systems, Elmasri, Navathe, 7 th Edition
Types of Keys in Database Management System
Key Constraints
Primary Key (1 Key)
Foreign Key (Referential Integrity)
Candidate key (Minimal Super Key)
Super Key (1 or more Keys)
Fundamentals Database Systems, Elmasri, Navathe, 7 th Edition
Dr. M. Premalatha, VIT Chennai
A superkey is a group of single or multiple keys which identifies rows in a table.
A Super key may have additional attributes that are not needed for unique identification
Example:
In the below -given example, EmpSSN and EmpNum name are superkeys.
EmpSSN EmpNum Empname
9812345098 AB05 Shown
9876512345 AB06 Roslyn
199937890 AB07 James
Dr. M. Premalatha, VIT Chennai
Relational Model Constraints – Candidate Keys
Fundamentals Database Systems, Elmasri, Navathe, 7 th Edition
Dr. M. Premalatha, VIT Chennai
Compound key
COMPOUND KEY has two or more attributes that allow you to uniquely
recognize a specific record. It is possible that each column may not be
unique by itself within the database. However, when combined with the
other column or columns the combination of composite keys become
unique.
Dr. M. Premalatha, VIT Chennai
Composite key
COMPOSITE KEY is a combination of two or more columns that
uniquely identify rows in a table. The combination of columns
guarantees uniqueness, though individually uniqueness is not
guaranteed. Hence, they are combined to uniquely identify
records in a table.
The difference between compound and the composite key is that
any part of the compound key can be a foreign key, but the
composite key may or maybe not a part of the foreign key.
Dr. M. Premalatha, VIT Chennai
Surrogate key
SURROGATE KEYS is An artificial key which aims to uniquely
identify each record is called a surrogate key. This kind of partial
key in dbms is unique because it is created when you don't have
any natural primary key.
Dr. M. Premalatha, VIT Chennai
Integrity Constraints
NOT NULL Constraint: Ensures that a column cannot have NULL
value.
DEFAULT Constraint: Provides a default value for a column when
none is specified.
UNIQUE Constraint: Ensures that all values in a column are different.
PRIMARY Key: Uniquely identified each rows/records in a database
table.
FOREIGN Key: Uniquely identified a rows/records in any another
database table.
CHECK Constraint: The CHECK constraint ensures that all values in a
column satisfy certain conditions.
constraints
Types of integrity constraints:
Domain integrity constraints
Entity Integrity constraints
Referential integrity constraints
How to Create and Maintain Integrity Constraints
Two methods for creating integrity constraints:
Code them in the CREATE TABLE command
Add them later with the ALTER TABLE command
Domain integrity constraints
These constraints set a range and any violations that take
place will prevent the user from performing the manipulation that
caused the breach. There are basically two types of domain
integrity constraints.
Not Null constraints
Check constraint
Not Null Constraints
When a ‘Not null’ constraints is enforced through either on a column or a set of columns in
a table, it will not allow Null values. The user has to provide a value for the column.
Syntax:
Create table table_name (columnname datatype constraint constraint_name not null);
Check constraints:
Check constraints specify conditions that each row must satisfy. These are rules governed by
logical expressions or Boolean expressions. Check conditions cannot contain subqueries.
Syntax:
Create table table_name (columnname datatype constraint constraint_name check
(condition));
Entity Integrity constraints
Each entity represents a table and each row of a table represents
an instance of that entity. Each row in a table can be uniquely
identified using the entity constraint. There are basically two types
of Entity Integrity constraints.
Unique constraints
Primary key constraints
Unique constraints
Usage of the unique key constraint is to prevent the duplication of values within the rows of a
specified columns or a set of columns in a table. Columns defined with this constraint can also allow null
values.
Syntax:
Create table table_name (columnname datatype… constraint constraint_name unique (colname)) ;
Primary key constraints
The primary key constraint avoids duplication of rows and does not allow Null values, when enforced
in a column or set of columns. A table can have only one primary key.
Syntax:
Create table table_name (columnname datatype constraint constraint_name primary key);
Referential integrity constraints
To establish a ‘parent-child’ or a ‘master-detail’ relationship between two tables
having a common column, we make used of referential integrity constraints. To
implement this, we should define the column in the parent table as a primary key and
the same column in the child table as a foreign key referring to the corresponding
parent entry.
Syntax:
Create table table_name (columnname datatype constraint constraint_name
references table_name (columnname));
Creating Tables With Constraints
(Not Null)
SQL> CREATE TABLE STUD(ROLLNO NUMBER(6) NOT NULL,NAME VARCHAR2(10),BRANCH VARCHAR2(6));
Table created.
SQL> DESC STUD;
Name Null? Type
----------------------------------------- -------- ----------------------------
ROLLNO NOT NULL NUMBER(6)
NAME VARCHAR2(10)
BRANCH VARCHAR2(6)
(UNIQUE)& primary key)
SQL> CREATE TABLE STUD(ROLLNO NUMBER(6) UNIQUE ,NAME
VARCHAR2(10),BRANCH VARCHAR2(6));
> CREATE TABLE STUD(ROLLNO NUMBER(6) PRIMARY KEY ,NAME
VARCHAR2(10),BRANCH VARCHAR2(6));
Create table stu( roll no number(4), gender vharchar(1) check
(gender in (‘m’,’f’),
mark number(3) check (marks>=0 and marks<=100)
(check)
SQL> create table stud(rno number(5),name varchar2(10),sal number(10) constraint no_ck check(sal between 10000 and
30000));
Example
SQL> /
Enter value for rno: 565
Enter value for name: rohit
Enter value for sal: 35000
old 1: insert into stud values(&rno,'&name',&sal)
new 1: insert into stud values(565,'rohit',35000)
insert into stud values(565,'rohit',35000)
*
ERROR at line 1:
ORA-02290: check constraint (SCOTT.NO_CK) violated
Foreign Key
SOL>create table adm(stuid number(6) constraint stuid_pk
primary key,sname varchar2(15),per number(5));
SQL> create table course(stuid number(6) constraint sid_fk
references adm(stuid),branch varchar2(5),sec varchar2(10));
Operations in Relational Model
Insert, update, delete and select.
Insert is used to insert data into the relation
Delete is used to delete tuples from the table.
Modify allows you to change the values of some attributes in
existing tuples.
Select allows you to choose a specific range of data.
Dr. M. Premalatha, VIT Chennai
Advantages of using Relational Model
implicity: A Relational data model in DBMS is simpler than the hierarchical and network model.
Structural Independence: The relational database is only concerned with data and not with a
structure. This can improve the performance of the model.
Easy to use: The Relational model in DBMS is easy as tables consisting of rows and columns are
quite natural and simple to understand
Query capability: It makes possible for a high-level query language like SQL to avoid complex
database navigation.
Data independence: The Structure of Relational database can be changed without having to
change any application.
Scalable: Regarding a number of records, or rows, and the number of fields, a database should
be enlarged to enhance its usability.
Dr. M. Premalatha, VIT Chennai
Disadvantages of using Relational Model
Few relational databases have limits on field lengths which can’t
be exceeded.
Relational databases can sometimes become complex as the
amount of data grows, and the relations between pieces of data
become more complicated.
Complex relational database systems may lead to isolated
databases where the information cannot be shared from one
system to another.
Dr. M. Premalatha, VIT Chennai
References
1. Fundamentals Database Systems, Elmasri, Navathe, 7 th Edition,
Pearson Publication
2. Database System Concepts, Silberschatz, Korth and Sudharsan, 7 th
Edition
3. Database System Concepts, Silberschatz, Korth and Sudharsan, 4 th
Edition
Thank You…