DBMS NOTES v23
DBMS NOTES v23
3. Contact Hours:
o Lecture (L): 3
o Tutorial (T): 0
o Practical (P): 2
o Theory: 3 hours
o Practical: 0
5. Relative Weightage:
o Practical (PR): 0%
6. Credits: 4
7. Semester: IV
10. Objective: To provide knowledge about the principles, concepts, and applications of
Database Management Systems.
Contact
S. No. Content
Hours
2 Relational Data Model and Language: Relational data model concepts, integrity 7
Contact
S. No. Content
Hours
Total
Contact 42
Hours
Suggested Books
Textbooks
Reference Books
This lesson covers Entity-Relationship (ER) Modeling, which is a conceptual database design
technique used to visually represent entities, attributes, and relationships. Students will learn how to
create ER diagrams, apply keys and constraints, and convert ER models into relational tables.
Learning Objectives
3. Apply keys (super key, candidate key, primary key) to uniquely identify records.
It serves as a blueprint for designing databases and ensures data consistency, integrity, and
efficiency.
o Without proper data modeling, customer orders may not link properly to products,
leading to data integrity issues.
Explanation of Database Design Process (Figure 7.1) – Including Stakeholders at Each Level
This diagram represents the main phases of database design, showing how a real-world system
(Miniworld) is translated into a structured database. The design process is divided into several
phases, from requirements gathering to implementation, involving different stakeholders at each
stage.
What Happens?
• The miniworld represents the real-world domain that the database is designed for.
• This is where the system operates, such as a university, hospital, or e-commerce platform.
Who is Involved?
What Happens?
• This is the first phase where database designers interact with stakeholders to understand:
Who is Involved?
3. Functional Analysis
What Happens?
Who is Involved?
Example:
What Happens?
Who is Involved?
Example:
What Happens?
• Maps the Conceptual Schema into a specific DBMS data model (e.g., Relational, NoSQL).
Who is Involved?
Example:
6. Physical Design
What Happens?
• Defines:
Example:
What Happens?
• Includes:
o User Interfaces
o Database Queries
o Stored Procedures
Who is Involved?
Example:
8. Transaction Implementation
What Happens?
• The final step where the database is implemented and runs real transactions.
• Application programs interact with the database to perform CRUD operations (Create, Read,
Update, Delete).
Who is Involved?
Example:
Conclusion
• What it is:
• Example:
o A University Database:
Reference:
Elmasri & Navathe, Chapter 7: Conceptual Database Design
• What it is:
o Converts the conceptual model into a logical structure suitable for implementation
in a specific database (e.g., MySQL, PostgreSQL).
Name VARCHAR(50),
Age INT
);
Course_Name VARCHAR(100)
);
Student_ID INT,
Course_ID INT,
Reference:
Korth & Silberschatz, Chapter 6: Relational Model
• What it is:
• Example:
Reference:
Ramakrishnan & Gehrke, Chapter 9: Storage & File Organization
Abstraction
Definition Users Representation
Level
ER Model Components
1. Entities (Objects or Concepts)
Reference:
Elmasri & Navathe, Chapter 7: ER Model & Conceptual Design
■ The company is organized into departments. Each department has a unique name, a unique
number, and a particular employee who manages the department.We keep track of the start date
when that employee began man aging the department. A department may have several locations.
■ A department controls a number of projects, each of which has a unique name, a unique number,
and a single location.
■ We store each employee’s name, Social Security number,2 address, salary, sex (gender), and birth
date. An employee is assigned to one department, but may work on several projects, which are not
necessarily controlled by the same department. We keep track of the current number of hours per
week that an employee works on each project. We also keep track of the direct supervisor of each
employee (who is another employee).
■ We want to keep track of the dependents of each employee for insurance purposes. We keep each
dependent’s first name, sex, birth date, and relation ship to the employee.
Figure 7.2 shows how the schema for this database application can be displayed by means of the
graphical notation known as ER diagrams
Explanation of the ER Diagram for the COMPANY Database (Figure 7.2)
This Entity-Relationship (ER) diagram represents the structure of a COMPANY Database, including
entities, attributes, relationships, and constraints. It captures essential components of an
organization, such as employees, departments, projects, and dependents.
1. Entities (Rectangles)
2. Attributes (Ovals)
3. Relationships (Diamonds)
• Attributes (Ovals):
o Address
o Salary
o Sex
• Relationships:
o SUPERVISION (self-referential)
• Attributes:
o Name
• Relationships:
• Attributes:
o Name
o Location
• Relationships:
• Attributes:
o Name
o Sex
o Birth_date
o Relationship
• Relationship:
• Meaning: Each employee works for one department, but a department can have many
employees.
• Attributes:
Cardinality:
• Meaning: Each department is managed by one employee, and an employee can manage
only one department.
• Cardinality: 1:1
• Meaning: An employee (supervisor) can supervise multiple employees, but each employee
has only one supervisor.
Cardinality:
• Meaning: Each employee can have multiple dependents, but each dependent is linked to
only one employee.
Cardinality:
• One employee (1) → Many dependents (N)
• Meaning: An employee can work on multiple projects, and a project can have multiple
employees working on it.
• Attributes:
Cardinality:
• Foreign Keys:
Solution: A junction table (WORKS_ON) is created with a composite primary key (Emp_Ssn,
Proj_Num).
• Meaning: A department controls multiple projects, but each project is controlled by one
department.
Cardinality:
3. Cardinality Summary
Fname VARCHAR(50),
Lname VARCHAR(50),
Minit CHAR(1),
Bdate DATE,
Address VARCHAR(100),
Salary DECIMAL(10,2),
Sex CHAR(1),
Dept_ID INT,
);
Emp_Ssn INT,
Proj_Num INT,
Hours DECIMAL(5,2),
);
6. Summary
• EMPLOYEE is the central entity with multiple relationships.
5. Summary
Data modeling helps in structuring and organizing data before database implementation.
Three levels of abstraction (Conceptual, Logical, Physical) ensure scalability and efficiency.
The ER Model is the foundation for relational database design.
ER diagrams help in visualizing real-world entities and their relationships.
Components of ER Model
2 Types of Entities
1. Strong Entities:
o Example:
2. Weak Entities:
o Identified by a partial key and related to a strong entity using a foreign key.
o Example:
3 Entity Set
• Example: The set of all students in a university forms a STUDENT entity set.
2 Types of Attributes
• Example:
o Phone_Number
2. Composite Attributes
• Example:
3. Derived Attributes
• Example:
4. Multi-Valued Attributes
• Example:
Attribute Diagram
Definition Example
Type Representation
attributes
• Example:
2 Relationship Types
1. One-to-One (1:1)
• Example:
2. One-to-Many (1:M)
• Definition: One entity is related to multiple entities, but each related entity is linked to only
one entity.
• Example:
o A department can have multiple employees, but each employee belongs to only
one department.
3. Many-to-Many (M:N)
• Example:
o A student can enroll in multiple courses, and each course can have multiple
students.
4 Attributes in Relationships
• Example:
o WORKS_ON relationship between EMPLOYEE and PROJECT has an attribute Hours
(number of hours worked).
Mapping Constraints
1. What are Mapping Constraints?
• Mapping constraints define the number of entities in one entity set that can be associated
with another entity set in a relationship.
• They describe how entities are related to each other in terms of cardinality.
o One-to-One (1:1)
o One-to-Many (1:M)
o Many-to-Many (M:N)
✔ Definition:
• Example:
o Each department is managed by one employee, and each employee manages only
one department.
Dept_Name VARCHAR(50),
);
✔ Definition:
• A single instance of Entity A can be associated with multiple instances of Entity B, but each
instance of B is related to only one instance of A.
• Example:
o A department can have multiple employees, but each employee belongs to only
one department.
Name VARCHAR(50),
Dept_ID INT,
);
• A company has multiple offices, but each office belongs to one company.
✔ Definition:
• A single instance of Entity A can be associated with multiple instances of Entity B, and vice
versa.
• Example:
o Employees can work on multiple projects, and each project can have multiple
employees.
✔ Solution:
• A junction table (WORKS_ON) is created to handle M:N relationships.
Emp_ID INT,
Proj_ID INT,
Hours DECIMAL(5,2),
);
• The WORKS_ON table resolves the M:N relationship between EMPLOYEE and PROJECT.
• Employees can be assigned to multiple teams, and each team has multiple employees.
✔ Definition:
• Every instance of an entity must be associated with at least one instance of another entity.
• Example:
• Some instances of an entity may not be associated with any instance of another entity.
• Example:
ER Diagram
Constraint Type Definition Example in Company DB
Representation
• A key is an attribute (or set of attributes) that uniquely identifies an entity in an entity set.
o Super Key
o Candidate Key
o Primary Key
o Foreign Key
o Composite Key
o Alternate Key
✔ Definition:
• A super key is a set of one or more attributes that uniquely identifies an entity in an entity
set.
• It may contain extra attributes that are not necessary for uniqueness.
✔ General Rule:
• Super keys may contain extra attributes, but they still uniquely identify an entity.
2.2 Candidate Key
✔ Definition:
• Every candidate key is a super key, but not every super key is a candidate key.
✔ Definition:
• A primary key is one of the candidate keys chosen to uniquely identify records in a table.
✔ SQL Implementation:
Name VARCHAR(50),
);
1. Uniqueness – No two rows can have the same primary key value.
✔ Definition:
• A foreign key is an attribute in one entity that refers to the primary key of another entity.
✔ SQL Implementation:
Dept_Name VARCHAR(50)
);
Name VARCHAR(50),
Dept_ID INT,
);
✔ Definition:
✔ SQL Implementation:
Emp_ID INT,
Proj_ID INT,
Hours DECIMAL(5,2),
);
✔ Definition:
• Example: If Emp_ID is the primary key, then Email and Phone are alternate keys.
✔ Definition:
• It does not have a unique ID, so it uses Dependent_Name along with Emp_ID.
✔ SQL Implementation:
Dependent_Name VARCHAR(50),
Emp_ID INT,
Relationship VARCHAR(50),
);
3. Summary Table
Super Key Set of attributes that uniquely identify an entity {Emp_ID, Email, Phone}
• Specialization: The process of dividing a higher-level entity into two or more lower-level
entities based on unique characteristics.
These concepts are essential for hierarchical modeling, improving database efficiency, minimizing
redundancy, and ensuring better data organization.
2. Generalization in the ER Model
2.1 Definition of Generalization
✔ Generalization is a bottom-up process in which multiple specific entities (subclasses) are merged
into a single higher-level entity (superclass).
✔ It abstracts common attributes and relationships into a generalized entity.
✔ Purpose:
• Reduce redundancy.
• Software Engineers
• HR Managers
• Sales Representatives
Each type of employee has unique attributes, but they also share common attributes such as
Emp_ID, Name, Salary.
✔ Generalization Process:
o Subclasses:
▪ Software_Engineer(Programming_Languages)
▪ HR_Manager(Employee_Benefits)
▪ Sales_Rep(Sales_Region)
Name VARCHAR(50),
Salary DECIMAL(10,2)
);
Programming_Languages VARCHAR(100),
);
Employee_Benefits VARCHAR(100),
);
Sales_Region VARCHAR(50),
);
✔ Benefits of Generalization:
✔ Specialization is a top-down process where a single higher-level entity is divided into multiple
specialized lower-level entities based on unique characteristics.
✔ Purpose:
• Improve data integrity by storing only relevant attributes for each entity.
✔ Specialization Process:
• Subclasses:
o Manager(Department_Managed)
o Engineer(Technical_Skill)
o Intern(Internship_Duration)
Name VARCHAR(50),
Salary DECIMAL(10,2)
);
Department_Managed VARCHAR(100),
);
Technical_Skill VARCHAR(100),
Internship_Duration INT,
);
✔ Benefits of Specialization:
ER Diagram
Subclasses merge into a superclass A superclass splits into subclasses
Representation
Combining Software_Engineer,
Dividing EMPLOYEE into Manager,
Example HR_Manager, and Sales_Rep into
Engineer, and Intern
EMPLOYEE
✔ Definition: Specifies whether all higher-level entities must be included in some subclass.
✔ Types:
6. Summary
• PERSON:
o Relationships:
• STUDENT:
o Relationships:
o Predicate: Class = 5.
o Attributes: Degrees.
o Relationships:
• DEPARTMENT:
o Relationships:
• COLLEGE:
• COURSE:
• SECTION:
• INSTRUCTOR_RESEARCHER:
o Union category of FACULTY and GRAD_STUDENT.
• GRANT:
o Relationships:
▪ PI (Principal Investigator).
• It allows modeling of complex relationships where an association between two entities acts
as a higher-level entity participating in another relationship.
• Consider a company where employees work on projects, and the company keeps track of
the managers supervising each project.
• Entities:
✔ Problem:
• This allows us to store additional attributes (e.g., review score of employee by the manager
for that project).
Salary DECIMAL(10,2)
);
Proj_Name VARCHAR(100),
Budget DECIMAL(10,2)
);
Emp_ID INT,
Proj_ID INT,
Hours_Worked DECIMAL(5,2),
);
Manager_ID INT,
Emp_ID INT,
Proj_ID INT,
Review_Score DECIMAL(3,2),
);
1.2 Process
1.3 Example
Name VARCHAR(50),
Address VARCHAR(100),
Salary DECIMAL(10,2),
Sex CHAR(1),
Birth_date DATE
);
o 1:M → Add the foreign key in the table representing the "many" side.
Each employee works for one department, but a department has many employees (1:N).
Each employee can work on multiple projects, and each project can have multiple employees (M:N).
Emp_Ssn INT,
Proj_ID INT,
Hours_Worked DECIMAL(5,2),
);
• Require a composite primary key (including the owner entity's primary key).
Dependent_Name VARCHAR(50),
Emp_Ssn INT,
Relationship VARCHAR(50),
);
✔ If an employee is deleted, all their dependents are also deleted (ON DELETE CASCADE).
Dept_ID INT,
Location VARCHAR(100),
);
✔ Each department can have multiple locations, stored separately.
Option 1: Separate Tables Each subclass has a table with a STUDENT(Ssn, Class),
(Default) foreign key to the superclass FACULTY(Ssn, Rank, Salary)
Option 2: Single Table for One table holds all attributes (NULL PERSON(Ssn, Name, Address, Sex,
Superclass & Subclasses values for missing attributes) Rank, Class, Salary)
Name VARCHAR(50),
Address VARCHAR(100),
Sex CHAR(1),
Bdate DATE
);
Rank VARCHAR(20),
Salary DECIMAL(10,2),
);
sql
CopyEdit
Manager_ID INT,
Emp_Ssn INT,
Proj_ID INT,
);
Entity Attributes
Each entity and relationship from the ER model must be converted into a table.
Each entity in the ER diagram is converted into a table with its attributes.
1. EMPLOYEE Table
sql
CopyEdit
Fname VARCHAR(30),
Minit CHAR(1),
Lname VARCHAR(30),
Bdate DATE,
Address VARCHAR(100),
Salary DECIMAL(10,2),
Sex CHAR(1)
);
2. DEPARTMENT Table
sql
CopyEdit
Name VARCHAR(50),
Number_of_Employees INT
);
3. PROJECT Table
sql
CopyEdit
Name VARCHAR(50),
Location VARCHAR(100)
);
4. DEPENDENT Table
sql
CopyEdit
Emp_Ssn INT,
Name VARCHAR(50),
Sex CHAR(1),
Birth_date DATE,
Relationship VARCHAR(50),
);
1. WORKS_FOR (N:1)
sql
CopyEdit
2. MANAGES (1:1)
sql
CopyEdit
3. WORKS_ON (M:N)
An employee can work on multiple projects, and a project can have multiple employees.
A junction table is required.
sql
CopyEdit
Proj_ID INT,
Hours_Worked DECIMAL(5,2),
);
4. CONTROLS (1:N)
sql
CopyEdit
5. SUPERVISION (1:N)
An employee supervises multiple employees, but each employee has only one supervisor.
sql
CopyEdit
sql
CopyEdit
Dept_ID INT,
Location VARCHAR(100),
PRIMARY KEY (Dept_ID, Location),
);
sql
CopyEdit
Tables Created
Tuple
A single entry (record) in a relation (101, John, 21, 'CS')
(Row/Record)
Uniquely identify rows and maintain Primary Key: SID, Foreign Key:
Keys (PK, FK)
relationships Dept_ID
3. Understanding Relational Schema
A Relational Schema is the blueprint of a database table.
101 John 21 CS
102 Alice 22 IT
103 Bob 20 EE
✔ Schema Representation:
STUDENT(SID INT PRIMARY KEY, Name VARCHAR(50), Age INT, Major VARCHAR(30))
• A relation must satisfy constraints like uniqueness (Primary Key), referential integrity
(Foreign Key), and domain rules.
5. Properties of Relations
4. Integrity Constraints
1. Introduction to Integrity Constraints
Integrity constraints are rules that ensure data accuracy, consistency, and validity in a relational
database.
• These constraints prevent invalid data entry and maintain relationships between tables.
• Enforced using Primary Keys, Foreign Keys, Domain Constraints, and Referential Integrity.
• They help in avoiding data anomalies like redundancy, inconsistency, and NULL values in
important fields.
Integrity constraints in a relational database can be classified into the following categories:
• A super key is a set of one or more attributes that uniquely identifies a row in a table.
pgsql
CopyEdit
✔ Every super key must contain at least one attribute that uniquely identifies a row.
• A candidate key is the minimal super key (a key that uniquely identifies a row, but contains
no unnecessary attributes).
• A table can have multiple candidate keys, but only one is chosen as the Primary Key.
✔ Emp_ID and Phone uniquely identify rows, but only one will be chosen as the Primary Key.
• A primary key (PK) is the chosen candidate key used to uniquely identify rows.
Name VARCHAR(50),
Dept_ID INT,
);
4. Domain Constraints
);
• A foreign key (FK) enforces referential integrity by linking a column in one table to the
primary key (PK) of another table.
| EMPLOYEE Table |
101 John 10
102 Alice 20
| DEPARTMENT Table |
10 HR
20 IT
Dept_Name VARCHAR(50)
);
Name VARCHAR(50),
Dept_ID INT,
);
✔ ON DELETE CASCADE ensures that if a department is deleted, all employees in that department
are also deleted.
A foreign key (FK) is an attribute (or a set of attributes) in one table that refers to the primary key
(PK) in another table.
• The EMPLOYEE table references the DEPARTMENT table using a foreign key.
2. Relational Schema
Creating the DEPARTMENT Table
Dept_Name VARCHAR(50),
Location VARCHAR(100)
);
Name VARCHAR(50),
Salary DECIMAL(10,2),
Dept_ID INT,
);
3. Referential Integrity
Referential Integrity is a rule that maintains consistency between related tables.
• It ensures that foreign key values always reference existing records in the primary key table.
• If a referenced record is deleted or updated, we must define how the dependent table
should react.
Insert Rule A foreign key value must match a primary key value in the referenced table or be NULL.
Delete Rule Prevents deletion of a referenced record unless handled using cascading actions.
Update If the primary key is updated, the foreign key should update accordingly or prevent the
Rule action.
ON DELETE SET NULL Sets foreign key to NULL when parent is deleted.
ON UPDATE CASCADE Updates child records when parent’s primary key is updated.
ON UPDATE SET NULL Sets foreign key to NULL when parent’s primary key is updated.
Name VARCHAR(50),
Salary DECIMAL(10,2),
Dept_ID INT,
);
✔ If DEPARTMENT (Dept_ID = 10) is deleted, all employees with Dept_ID = 10 are also deleted.
5.2 ON DELETE SET NULL
Name VARCHAR(50),
Salary DECIMAL(10,2),
Dept_ID INT,
);
✔ If DEPARTMENT (Dept_ID = 10) is deleted, employees remain, but their Dept_ID is set to NULL.
Name VARCHAR(50),
Salary DECIMAL(10,2),
Dept_ID INT,
);
Name VARCHAR(50),
Salary DECIMAL(10,2),
Dept_ID INT,
Feature Description
Referential Integrity Ensures foreign key values match existing primary key values.
ON DELETE SET NULL Sets child foreign key to NULL when parent is deleted.
ON UPDATE CASCADE Updates child foreign key when parent’s primary key changes.
SQL
CREATE SCHEMA COMPANY AUTHORIZATION ‘Jsmith’
The SELECT-FROM-WHERE Structure of Basic SQL Queries
Tuple calculus:
VIEWS
A view in SQL terminology is a single table that is derived from other tables. These other tables can
be base tables or previously defined views. A view does not necessarily exist in physical form; it is
considered to be a virtual table, in contrast to base tables, whose tuples are always physically stored
in the database. This limits the possible update operations that can be applied to views, but it does
not provide any limitations on querying a view
A view is supposed to be always up-to-date; if we modify the tuples in the base tables on which the
view is defined, the view must automatically reflect these changes. Hence, the view is not realized or
materialized at the time of view definition but rather at the time when we specify a query on the
view. It is the responsibility of the DBMS and not the user to make sure that the view is kept up-to-
date.
The problem of efficiently implementing a view for querying is complex. Two main approaches have
been suggested.
1. One strategy, called query modification, involves modifying or transforming the view query
(submitted by the user) into a query on the underlying base tables. For example, the query
QV1 would be automatically modified to the following query by the DBMS:
SELECT FROM WHERE Fname, Lname EMPLOYEE, PROJECT, WORKS_ON Ssn=Essn AND
Pno=Pnumber AND Pname=‘ProductX’;
The disadvantage of this approach is that it is inefficient for views defined via complex
Queries that are time-consuming to execute, especially if multiple queries are going to be
applied to the same view within a short period of time.
2. The second strategy, called view materialization, involves physically creating a temporary
view table when the view is first queried and keeping that table on the assumption that
other queries on the view will follow. In this case, an efficient strategy for automatically
updating the view table when the base tables are updated must be developed in order to
keep the view up-to-date.
Techniques using the concept of incremental update have been developed for this purpose,
where the DBMS can determine what new tuples must be inserted, deleted, or modified in a
materialized view table when a database update is applied to one of the defining base tables.
The view is generally kept as a materialized (physically stored) table as long as it is being
queried.
If the view is not queried for a certain period of time, the system may then automatically
remove the physical table and recompute it from scratch when future queries reference the
view.
Updating of views is complicated and can be ambiguous. In general, an update on a view
defined on a single table without any aggregate functions can be mapped to an update on
the underlying base table under certain conditions. For a view involving joins, an update
operation may be mapped to update operations on the underlying base relations in multiple
ways. Hence, it is often not possible for the DBMS to determine which of the updates is
intended. To illustrate potential problems with updating a view defined on multiple tables,
consider the WORKS_ON1 view, and suppose that we issue the command to update the
PNAME attribute of ‘John Smith’ from ‘ProductX’ to ‘ProductY’. This view update is shown in
• It provides a set of operations that take one or more relations (tables) as input and return a
relation as output.
A set of operations, called outer joins, were developed for the case where the user wants to keep all
the tuples in R, or all those in S, or all those in both relations in the result of the JOIN, regardless of
whether or not they have matching tuples in the other relation. This satisfies the need of queries in
which tuples from two tables are to be combined by matching corresponding rows, but without
losing any tuples for lack of matching values. For example, suppose that we want a list of all
employee names as well as the name of the departments they manage if they happen to manage a
department; if they do not manage one, we can indicate it with a NULL value.
Comparison: Tuple Relational Calculus (TRC) vs. Domain Relational
Calculus (DRC) vs. Relational Algebra (RA)
1. Introduction
In the relational model, Tuple Relational Calculus (TRC), Domain Relational Calculus (DRC), and
Relational Algebra (RA) are three different formal query languages used for retrieving data. They
differ in their approach, notation, and expressiveness.
2. Comparison Table
Tuple Relational Calculus Domain Relational Calculus
Feature Relational Algebra (RA)
(TRC) (DRC)
Type of
Non-procedural Non-procedural Procedural
Language
Uses a sequence of
Focuses on individual
Focus Focuses on tuples (rows). operations to manipulate
attribute values (columns).
relations.
Variables Uses tuple variables (t) to Uses domain variables (x, y, Operates directly on
Tuple Relational Calculus Domain Relational Calculus
Feature Relational Algebra (RA)
(TRC) (DRC)
Query Queries specify what to Queries specify what to Queries specify both what
Execution retrieve, but not how. retrieve, but not how. and how to retrieve results.
• Definition: TRC queries specify what to retrieve using tuples (rows) as variables.
• Definition: DRC queries specify what to retrieve using domain variables (column values).
• Uses operations like Selection (σ), Projection (π), Joins (⋈), Union (∪), Set Difference (-),
and Cartesian Product (×).
DRC Query:
SQL Equivalent:
TRC Query:
DRC Query:
SQL Equivalent: