DBMS Unit -2 - Part-1
DBMS Unit -2 - Part-1
Unit -2 Part-1
DATABASE MANAGEMENT
SYSTEMS
By B. Lokesh Joel
Unit-2:
• Introduction to the Relational Model: • Relational Algebra,
• Integrity constraint over relations,
• Tuple relational Calculus,
• enforcing integrity constraints,
• introduction to views,
▪ User-Friendly: Even novice users can understand and query databases easily.
▪ Formal Definition
– Let R(f1:D1, ..., fn:Dn) be a relation schema.
– Each fi (field) has a domain Di with values Domi.
– An instance of R is a set of tuples satisfying:
Domain Constraints
▪ Atomic Values:
– Each attribute value must be atomic (indivisible).
– Example (Valid Atomic Value):
• age = 21 (Single integer value)
• phone_numbers = {9876543210, 9123456789} (Multiple values in one attribute)
– A relational database should not store lists or nested data in a single column.
▪ Key Constraints
▪ Foreign Key Constraints
▪ General Constraints
Key Constraints
▪ A key constraint ensures that each
row in a table is uniquely identifiable.
▪ A candidate key is a minimal set of
fields that uniquely identifies a tuple. • Key Constraints in this table:
• sid is a candidate key (no two students
▪ A superkey is any set of attributes have the same sid).
• {login, age} is another candidate key
that contains a candidate key. (students may share a login or age, but
not both).
▪ Every relation must have at least one • {sid, name} is a superkey, but not a
key. candidate key because sid alone is
sufficient.
https://www.geeksforgeeks.org/super-key-in-dbms/
Super Key
▪ A super key is a set of attributes that uniquely identifies a tuple (row) in a relation.
▪ It may contain extra attributes that are not necessary for unique identification.
▪ Any Superset of a Super Key is Also a Super Key
▪ Example:
– Consider the Students table:
• Super Keys:
• {sid}
• {sid, name}
• {sid, name, login}
• {login, age} (assuming login and age together
uniquely identify a student)
Super Key – Includes Primary Key and Candidate Keys:
• Every primary key and candidate key is a super
▪ Key Features of Super Key
key, but not all super keys are primary keys or
– Uniqueness: candidate key.
• A super key ensures that every row in a – Foundation for Database Design:
table is uniquely identified and prevents
duplicate records. • Super keys are essential for determining
candidate keys and ensuring data integrity in a
– One or More Attributes:
database.
• It can consist of a single column or a
combination of multiple columns.
– Not Necessarily Minimal:
• A super key may include extra attributes
that are not required for uniqueness,
unlike a candidate key.
https://www.geeksforgeeks.org/super-key-in-dbms/
Super Key
{A1} ➔ {A1, A2}, {A1, A3}, {A1, A4}
A1 A2 A3 A4 {A1, A2}➔{A1, A2, A3}, {A1, A2, A4}, {A1, A2, A3, A4}
1 2 5 4 {A1,A3}➔{A1, A3, A4}
2 2 6 4 {A2, A3}➔{A2, A3, A4}
3 3 5 5
{A3, A4}
4 4 7 5
Key (Minimal Super Key)
▪ A key is a minimal super key, meaning:
– It uniquely identifies a tuple.
– No extra attributes can be removed while still maintaining uniqueness.
– Example:
• From the super keys above, minimal keys are:
• {sid} (Minimal and unique)
• {login, age} (If login and age together uniquely identify a student)
• But {sid, name} is not a key because name is unnecessary (sid alone is enough).
Candidate Key
▪ If a relation has more than one key, each of them is called a candidate
key.
– Example:
• In the Students table, we identified:
• {sid}
• {login, age}
• Since both uniquely identify a tuple, they are candidate keys.
https://mycareerwise.com/content/calculate-super-key/content/exam/nta-net/computer-science
Note: Number of super keys = 𝟐𝒏−𝒌
where “n” is the number of attributes in the
Relation R and “k” is the number of
attributes in the Candidate Key
• Let a Relation R have attributes {a1,a2,a3} and a1 is the candidate key. Then
how many super keys are possible?
• Let a Relation R have attributes {a1, a2, a3,…,an}. Find Super key of R.
Maximum Super keys = 2n – 1.
• Let a Relation R have attributes {a1, a2, a3,…, an} and the candidate key is “a1 a2 a3”
then the possible number of super keys?
Number of possible super keys is 2(N-3).
https://www.geeksforgeeks.org/number-of-possible-superkeys-in-dbms/
• Let a Relation R have attributes {a1, a2, a3,…, an} and the candidate keys are “a1”, “a2”
then the possible number of super keys?
Let a Relation R have attributes {a1, a2, a3,…,an} and the candidate keys are “a1”, “a2”,
“a3” then the possible number of super keys?
Let a Relation R have attributes {a1, a2, a3,…,an} and the candidate keys are “a1”, and
{a1,a2} then the possible number of super keys?
Primary Key
▪ Among the candidate keys, one key is chosen as the primary key for database
implementation.
▪ It is the main key used for referencing records.
▪ Example:
– If {sid} and {login, age} are candidate keys, we choose {sid} as the primary key because it is:
• Simple (only one attribute)
• Efficient (indexed in most databases)
Foreign Key Constraints
• A foreign key (FK) is an attribute in one relation that refers to the primary
key (PK) in another relation.
• Ensures referential integrity, meaning:
• A value in the foreign key column must exist in the referenced table.
• Prevents orphaned records (references to non-existent entities).
Foreign Key Constraints
Referential Integrity Enforcement
▪ Violations of Foreign Key Constraints
– Invalid Insertion
• Error: "Cannot insert or update because sid=55555 does not exist in Students.“
– Invalid Deletion
▪ These constraints ensure data consistency and correctness for specific requirements.
▪ Example :
– Students must be at least 16 years old.
• If we enforce this rule, students younger than 16 should not be inserted.
• This modifies the Students table and removes underage students.
General Constraints
▪ Extended Domain Constraints
– Example: The rule that students must be older than 16 is an extended domain constraint.
– Why? It defines acceptable values beyond the standard integer type.
– It restricts input values more strictly than a standard data type.
– Another Example: Every student older than 18 must have a GPA greater than 3.
• Common ICs:
• Domain Constraints → Ensures valid data types.
• Primary Key Constraints → Ensures uniqueness & non-null values.
• Unique Constraints → Ensures unique values.
• Foreign Key Constraints → Ensures valid references.
Handling Violations in SQL
▪ INSERT Violations
– INSERT operations fail if they violate constraints like primary key, unique, foreign key,
or domain constraints.
Violation Type Example Error Message Solution
INSERT INTO Students (sid,
"Duplicate entry for primary key Ensure sid is unique before
Primary Key Violation name) VALUES (53688, 'Mike');
sid." insertion.
(Duplicate sid)
Updating Data Type UPDATE Students SET age = "Data type mismatch: age Ensure correct data type
Violation 'twenty' WHERE sid = 1001; must be an INTEGER." before update.
UPDATE Violations
▪ Using ON UPDATE CASCADE
▪ Query Language:
– SQL (Structured Query Language) is commonly used for querying relational databases.
– Example Query (Students younger than 18):
– Order of Operations:
• Selection is applied first to filter rows.
• Projection is applied next to remove unnecessary fields.
Querying Relational Data
▪ Joining Multiple Relations
– Example Query (Students who got an ‘A’ in a course):
or
Emp_id (PK) Emp_name Emp_age Dept_id (PK) Dept_name Emp_id (PK) Dept_id (PK)
101 John 25 10 HR 101 10
102 Alice 30 20 IT 102 20
103 Bob 28 30 Finance 103 30
• A one-to-many relationship occurs
when one entity is associated with
multiple instances of another entity.
• In this case:
• One Faculty can teach multiple
Courses.
• Each Course is taught by only one
Faculty.
Option 3 is Incorrect
Explanation of Each Option:
1. An attribute of an entity can have more than one value ( Correct)
• In ER models, an entity can have multivalued attributes.
• Example: An Employee entity having multiple Phone Numbers.
2. An attribute of an entity can be composite ( Correct)
• A composite attribute is an attribute that can be divided into sub-parts.
• Example: Full Name can be divided into First Name and Last Name.
3. In a row of a relational table, an attribute can have more than one value ( Incorrect)
• In the Relational Model, an attribute must be atomic (it cannot have multiple values).
• This follows First Normal Form (1NF), which states that each attribute in a row must contain
a single, atomic value.
• Multivalued attributes are not allowed in relational tables.
4. In a row of a relational table, an attribute can have exactly one value or a NULL value (
Correct)
• In the Relational Model, an attribute must contain either:
• A single value (Atomic Value).
• A NULL value (if no data is available).
• Find the minimum number of tables required to represent the given ER diagram in
relational model-
1 N N M
1 N N M
Entity-1 Entity-2
Entity-1
A31 A33
A32
N-ary relationships (>2)
[ER diagram in relational model]
A11 A12 A13 A21 A22 A23
1 1 Department
Department
Head
Entities:
1. Department → Attributes: (Dept_id, Dept_Name)
2. Department Head → Attributes: (Emp_id, Emp_Name)
Minimum Number of
Relationship: Tables Required = 1.
• One-to-One (1:1) Relationship
• Total Participation on Both Sides:
• Each Department must have exactly one Department Head.
• Each Department Head must be assigned to exactly one Department.
This means every record in Department must have a matching record in Department Head, and vice versa.
• Find the minimum number of tables required to represent the given ER diagram in
relational model-
1 1 Department
Department
Head
Minimum Number of
Tables Required = 1.
• Find the Maximum number of tables required to represent the given ER diagram in
relational model-
1 1 Department
Department
Head
• Maximum Number of Tables for One-to-One (1:1) Relationship with Total Participation on Both Sides
• Since each department must have exactly one department head, and each department head must belong
to exactly one department, the total participation constraint must be enforced.
• To maximize normalization and maintain entity integrity, we use three tables:
• Department Table (Stores department details) Maximum Number of
• Department_Head Table (Stores department head details) Tables Required = 3.
• Department_Head_Assignment Table (Stores the relationship between department and department
head with total participation)
• Find the Maximum number of tables required to represent the given ER diagram in
relational model-
1 1 Department
Department
Head
• Find the minimum number of tables required to represent the given ER diagram in
relational model
A C A B X Y A
https://testbook.com/question-answer/find-minimum-number-of-tables-required-for-convert--5ed8d2cef60d5d2aa46d83a1
Attributes to relationship : one to many
relationship
Attributes to relationship : one to many
relationship
• The given ER diagram represents the "Work for"
relationship between Employee and Department,
where:
• Each employee belongs to only one department.
• One department can have multiple employees.
• The given ER diagram represents the "Work for" relationship between Employee and Department,
where:
• Each employee belongs to exactly one department.
• Each department has exactly one employee.
• Since this is a one-to-one (1:1) relationship, we can implement it in two ways.
Attributes to relationship : one to one
relationship
• Relational Schema
• We need to create two tables (instead of three, as in one-to-many relationships):
• Approach 1: Add Foreign Key to One Table (Recommended)
• Since each department has one employee, we can add Emp_id as a foreign key in the Department
table.
Attributes to relationship : one to one
relationship
• Alternative Approach: Create a Separate Relationship Table
• If we want a strict separation of entities and relationships, we can use a separate Works_For table.
Attributes to relationship : many to many
relationship
Attributes to relationship : many to many
relationship
• The given ER diagram represents the "Work for" relationship between Employee and Department,
where:
• An employee can work for multiple departments.
• A department can have multiple employees.
• The attribute "since" represents when the employee started working for the department.
• Since this is a many-to-many (M:N) relationship, we need an additional table to capture this
relationship.
Attributes to relationship : many to many
relationship
Emp_id → Employee,
Works_For Emp_id, Dept_id, since (Emp_id, Dept_id)
Dept_id → Department
Relationship Sets (without Constraints) to
Tables
▪ A relationship set, like an entity set, is mapped to a relation (table) in the
relational model.
▪ When there are no key or participation constraints, each participating entity
must be uniquely identifiable.
▪ The relation includes:
– Primary key attributes of each participating entity (stored as foreign keys).
– Descriptive attributes of the relationship set.
– In such cases, the primary key of one of the – This means that did (Department ID) alone is
participating entities can be used as the sufficient as a primary key for the relationship table.
primary key for the relationship table. – ssn (Employee ID) is not part of the primary key
because it does not uniquely determine the
relationship.
Translating Relationship Sets with Key
Constraints
▪ First Approach: Using a Separate Relationship Table
– The Manages relationship is stored as a separate table with:
• did (Department ID) as Primary Key (because each department has at most one manager).
• ssn (Manager's Employee ID) as a Foreign Key.
• since (Start date of management role).
Translating Relationship Sets with Key
Constraints
▪ Second Approach: Merging Relationship Information into One Table
– Instead of creating a separate Manages table, we add manager details directly to the
Departments table.
– Since each department has at most one manager, we store:
– ssn (Manager’s Employee ID) as a foreign key in Departments.
– since (Start date of management role) as an attribute of Departments.
Translating Relationship Sets with Key
Constraints
▪ Comparison of the Two Approaches
Approach Pros Cons
Requires JOINs to retrieve
Separate Table for
Avoids NULL values department-manager
Manages
details
May waste space if
Merging into Dept_Mgr Simplifies queries, avoids
departments have no
Table JOINs
managers (NULL values)
▪ Challenge
– The Works_In table must ensure every department
appears in the relationship.
– Solution: Use an assertion or table constraints, which
SQL-92 does not support efficiently.
Relative Employee
of an
Emp_id Name Age Type_of_Relative
Emp_id Emp_name
E101 John 45 Father E101 Michael
E102 Alice 38 Spouse E102 Sarah
E103 David 50 Brother E103 David
E104 Emma
E104 Sophia 30 Sister
Translating Weak Entity Sets
▪ What is a Weak Entity Set?
– A weak entity set is an entity set that
does not have a sufficient primary key
on its own.
– It always participates in a one-to-many
relationship with a strong entity set
(the owner).
– It has a partial key (also called a
discriminator) that identifies unique
instances of the weak entity within the
owner entity.
Translating Weak Entity Sets
▪ Key Characteristics of Weak Entities
– No Primary Key: It cannot be uniquely
identified without including the primary key of
the owner entity.
– Total Participation: Every weak entity must
be associated with an owner entity.
– Key Constraint: Each weak entity is
associated with only one owner entity.
– Deletion Dependency: If the owner entity is
deleted, all related weak entities must also be
deleted (CASCADE DELETE is required).
Translating Weak Entity Sets
▪ Example: Dependents (Weak
Entity)
– Consider the Dependents weak entity,
where:
• Employees (Owner Entity) have dependents (Weak
Entity).
• Each Dependent is uniquely identified by:
• The name of the dependent (pname)
• The SSN of the employee (ssn)
• If an employee is deleted, all their dependents must
be deleted.
Translating Weak Entity Sets
Explanation of Constraints
Constraint Purpose
Ensures that each dependent is uniquely identified within an
PRIMARY KEY (pname, ssn)
employee (because pname alone is not unique).
FOREIGN KEY (ssn) REFERENCES Employees Links each dependent to an existing employee.
▪ Disadvantages:
– Queries involving subclass-specific attributes (e.g., hourly_wages) require joins
with Employees.
– Cascade delete is required to remove subclass entries when an employee is deleted.
Translating Class Hierarchies (ISA Hierarchies)
▪ Two Approaches to Mapping ISA Hierarchies to
Tables
– Approach 2: Merge Subclasses into Two Tables
(Flattening the Hierarchy)
– Instead of having a separate Employees table,
each subclass contains all employee attributes.
Translating Class Hierarchies (ISA Hierarchies)
Translating Class Hierarchies (ISA Hierarchies)
▪ Advantages:
– Queries for specific subclass employees (e.g., hourly employees) are simpler and
do not require joins.
– Employees are directly classified into their roles without referring to a superclass
table.
▪ Disadvantages:
– Duplication of common attributes (name, lot) if an employee belongs to both
subclasses.
– If there are employees who do not belong to either subclass, they cannot be
stored.
Translating ER Diagrams with Aggregation
▪ What is Aggregation in ER Diagrams?
– Aggregation is used when a relationship itself participates in
another relationship.
– Relationships:
▪ Aggregation:
• Sponsors (pid, did, since) → Represents which department sponsors
which project.
– The Sponsors relationship is aggregated as it
• Monitors (ssn, pid, did, until) → Represents an employee monitoring a
sponsorship. participates in the Monitors relationship.
Translating ER Diagrams with Aggregation
▪ Translating Aggregation to the Relational Model
– To map aggregation into relations, we create a table for
Monitors that includes:
• Primary key attributes of Employees (ssn).
131