Week 04 - Relational Model
Week 04 - Relational Model
Week 04
Relational Model
Engr. Rashid Farid Chishti
http://youtube.com/rfchishti
http://sites.google.com/site/chis
hti International Islamic University H-10, Islamabad, Pakistan
Learning Objectives
Understand the relational model and its importance in DBMS
Learn key concepts like relational schema, keys, and constraints
What is a Relational Model ?
The Relational Model is a way to structure and manage data in a database by
organizing it into tables (relations).
Converts the ER Diagram into tables (relations) with attributes.
It was introduced by E.F. Codd in 1970 and is the foundation of most modern
database systems.
Relational Model is the most widely used model.
In this model, the data is maintained in the form of a two-dimensional table.
All the information is stored in the form of row and columns.
The basic structure of a relational model is tables.
So, the tables are also called relations in the relational model.
Relational Model
Purpose: Organizes data into tables (relations) with rows and columns.
Focus: Relationships between tables using keys.
Example: A university database with tables like Students, Courses, and Enrollments.
Key Features:
Uses tables, primary keys, foreign keys, and normalization.
3. Relation Instance
The set of tuples of a relation at a particular instance of time is called as relation
6. Relationships:
One-to-One (1:1): One entity is related to one entity in another table.
One-to-Many (1:M)
Many-to-Many (M:N)
each tuple (row) in a relation. It ensures that no two rows have the same value for this
attribute. Example: StudentID in the STUDENT table.
8. Foreign Key (FK)
An attribute in one table that references the primary key of another table, establishing
10. Cardinality
The number of tuples (rows) in a table.
blank space. Example: A student record having no phone number in a STUDENT table.
12. Relational Schema
The structure of the database, defining tables, attributes, and relationships.
Example: STUDENT (ROLL_NO, NAME, ADDRESS, PHONE and AGE) is relation schema for
STUDENT.
Example of a Relational Model
Students Table Courses Table
Student_ID Name Age Major Course_ID Course_Name Credits
101 Alice 22 CS CS101 Databases 3
102 Bob 24 Math MT202 Algebra 3 Enrollments Table
103 John 23 CS (Relationship Table)
Student_ID Course_ID Grade
101 CS101 A
102 MT202 B+
Relationships:
Many-to-Many (M:N)
Many students can enroll in many courses
Properties of Relations
Name of relation is distinct from all other relations.
Each relation cell contains exactly one atomic (single) value.
Each attribute contains a distinct name.
Tuple has no duplicate value.
Order of tuple can have a different sequence.
Operations in Relational Model
Insert Operation
The insert operation gives values of the attribute for a new tuple which should be
Update Operation
You can see that in the below-given relation table CustomerName= 'Apple' is updated
Select Operation
Select a specific values. Example: CustomerName= ‘Amazon' is selected.
Relational Schema
A Relational Schema is the blueprint of a relational database. It defines how
data is organized and how relationships between data are maintained.
It describes the structure of tables (relations), the attributes (columns) in each
table, and the constraints applied to the data.
Key Components of a Relational Schema:
Tables (Relations): Represents entities or relationships.
Attributes (Columns): Represents the properties or fields of an entity.
Tuples (Rows): Represents records or data entries in a table.
Primary Key: Uniquely identifies each tuple in a table.
Foreign Key: Establishes relationships between tables by referring to the primary key of
another table.
Constraints: Rules like NOT NULL, UNIQUE, CHECK, and DEFAULT that maintain data
integrity.
Relational Schema: Example
Imagine a university database with two entities: Students and Courses.
Customer Order Product
PK ID Customer_ID PK ID
Name Product_ID Quantity
Phone Order_Date Product_Type
Address Order_Status
Relational Schema:
Customer(ID, Name, Phone, Address)
Product(ID, Quantity, Product_Type)
Order(Student_ID, Product_ID, Order_Date, Order_Status)
Relational Schema: Example
Primary Keys:
ID for Customer
ID for Product
Composite key (Student_ID, Course_ID) for Order
Foreign Keys:
Student_ID in Order references ID in Customer
Product_ID in Order references ID in Product
Relational Schema: How it Works
The schema defines the structure but does not store data directly.
SQL is used to implement the schema, create tables, and manage data.
The schema ensures data consistency by enforcing relationships between
tables. CREATE TABLE COURSE (
CourseID INT PRIMARY KEY,
Title VARCHAR(100) NOT NULL,
Credits INT CHECK (Credits > CREATE TABLE STUDENT (
0) StudentID INT PRIMARY
); KEY,
CREATE TABLE ENROLLMENT ( Name VARCHAR(100) NOT
StudentID INT, NULL,
CourseID INT, Age INT CHECK (Age >=
Grade CHAR(1), 18),
PRIMARY KEY (StudentID, CourseID), Major VARCHAR(50)
FOREIGN KEY (StudentID) REFERENCES STUDENT(StudentID) ON DELETE
);
CASCADE,
FOREIGN KEY (CourseID) REFERENCES COURSE(CourseID) ON DELETE
CASCADE
Relational Schema: How it Works
Example Data Insertion:
INSERT INTO STUDENT VALUES (101, 'Alice Johnson', 20, 'Computer
Science');
INSERT INTO COURSE VALUES (301, 'Database Systems', 3);
INSERT INTO ENROLLMENT VALUES (101, 301, 'A');
Query Example:
SELECT s.Name, c.Title, e.Grade
FROM STUDENT s
JOIN ENROLLMENT e ON s.StudentID = e.StudentID
JOIN COURSE c ON e.CourseID = c.CourseID;
Comparison of three Models
Feature ER Model Relational Model Relational Schema
Physical blueprint for database
Purpose Conceptual design of data Logical structure of data
implementation
Entities, attributes, and Tables (relations), rows (tuples), Table structures, columns, and
Focus constraints
relationships and columns (attributes)
ER Diagrams (Entities,
Representation Relations (Tables) Table definitions with attributes
Relationships)
Abstraction
High-level Mid-level Low-level
Level
Early stage (Conceptual Intermediate stage (Logical Final stage (Physical
Design Stage implementation)
design) design)
Database developers (SQL
Used By Database designers Database architects
coders)
Use Case Planning and early-stage Conceptualizing and structuring Defining tables for the DBMS.
design. data.
Levels of Abstraction
Feature ER Model Relational Model Relational Schema
Entity Names ✔ ✔
Attributes ✔ ✔
Entity Relationships ✔ ✔
Primary Keys ✔ ✔ ✔
Foreign Keys ✔ ✔
Tables Names ✔
Column Names ✔
manipulate.
Structural Independence
Changes in database schema (such as adding or modifying columns) do not affect
maintain data consistency. Ensures referential integrity, preventing orphan records and
maintaining valid relationships.
Advantages of using Relational Model
Reduced Data Redundancy
Normalization techniques eliminate data duplication, improving storage efficiency.
Data Security
Provides access control mechanisms, ensuring only authorized users can access or
data manipulation. SQL makes it easier to retrieve and update data efficiently.
Multi-User Support & Concurrency Control
Allows multiple users to access and manipulate the database simultaneously.
Ensures that changes made by one user do not negatively impact others.
different platforms.
Popular RDBMSs like MySQL, PostgreSQL, Oracle, and SQL Server support the relational
operations.
3. Scalability Challenges
Vertical Scaling (adding more resources to a single server) is common but has limits.
NoSQL databases.
Disadvantages of using Relational Model
4. Rigid Schema Structure
Changes in the schema (e.g., adding/removing columns) can be difficult and impact
existing applications.
Not ideal for dynamic or semi-structured data (e.g., JSON, XML).
better here).
6. High Development & Maintenance Cost
Requires specialized expertise for database design, optimization, and administration.
Regular backups, indexing, query tuning, and security management add to maintenance
costs.
Disadvantages of using Relational Model
7. ACID Compliance Overhead
While ACID properties ensure consistency, they can introduce latency in distributed
systems.
Eventual consistency (used in NoSQL) is sometimes preferred for high-speed applications.
NoSQL databases are often preferred for real-time analytics and IoT applications.
ER Diagram
Relational Diagram
Attributes
Relationships
Cardinality
ER Diagram Example Using Draw.IO
ER Diagram Example Using Draw.IO
Roles in Relationships
Symbol Meaning
Non Identifying
Relationship
Identifying Relationship
One Identifying
Many Identifying
One Identifying
Many Identifying
Roles in Relationships
Roles in Relationships
Business Rule 1
An employee can have 0,1 or several phone numbers
They may or may not have a telephone number
Employee Phone
ID Phone_Number
First_Name Employee_ID
Last_Name Phone_Type
City Primary_OR_Secondry
Street
House
Email
Business Rule 2
There are finite number of phone number combinations that exist.
Over time different employee can have same phone number, each a different
time.
Phone
Phone_Number
Employee_ID
Phone_Type
Primary_OR_Secondry
Business Rule 3
An employee can be paid either hourly or by a yearly salary. Depending on
how they are paid, we need to collect specific information that applies only to
that type of employee.
Employee Hourly_Employee
ID Employee_ID
First_Name Hourly_Rate
Last_Name Full_Time_OR_Part_Time
City
Salary_Employee
Street
Employee_ID
House
Monthly_Salary
Email
Business Rule 4
An employee can be assigned to many projects, however a single project can
have multiple employees assigned to it.
Employee Project
Assignment
ID ID
Employee_ID
First_Name Name
Project_ID
Last_Name Description
Assigned_On
City Start_Date
Street End_Date
House
Email
MySQL Sample Database
MySQL Sample Database
MySQL Sample Database
Converting ER Model to Relational Schema
Steps:
1. Convert entities to tables
2. Convert attributes to columns
3. Define primary and foreign keys
4. Represent relationships through foreign keys
Example:
Entity: Student (StudentID, Name) → Table: Student
The primary key of the table will be the key attribute of the entity set.
relational model.
During conversion, simple attributes of the composite attributes are taken into account
relational model.
One table will contain all the simple attributes with the primary key.
Other table will contain the primary key and all the multi valued attributes.
1 1 Way 2
3. A (a1, a2)
4. BR (a1, b1, b2)
Converting ER Diagram to Table
Relational Diagram for University Database