DBMS Module 2
DBMS Module 2
Relational Algebra is a formal language used to query and manipulate relational databases,
consisting of a set of operations like selection, projection, union, and join. It provides a
mathematical framework for querying databases, ensuring efficient data retrieval and
manipulation. Relational algebra serves as the mathematical foundation for query SQL
Relational algebra simplifies the process of querying databases and makes it easier to understand
and optimize query execution for better performance. It is essential for learning SQL because
SQL queries are based on relational algebra operations, enabling users to retrieve data
effectively.
Key Concepts in Relational Algebra
Before explaining relational algebra operations, let's define some fundamental concepts:
1. Relations: In relational algebra, a relation is a table that consists of rows and columns,
representing data in a structured format. Each relation has a unique name and is made up of
tuples.
2. Tuples: A tuple is a single row in a relation, which contains a set of values for each attribute.
It represents a single data entry or record in a relational table.
3. Attributes: Attributes are the columns in a relation, each representing a specific characteristic
or property of the data. For example, in a "Students" relation, attributes could be "Name", "Age",
and "Grade".
4. Domains: A domain is the set of possible values that an attribute can have. It defines the type
of data that can be stored in each column of a relation, such as integers, strings, or dates.
Basic Operators in Relational Algebra
Relational algebra consists of various basic operators that help us to fetch and manipulate data
from relational tables in the database to perform certain operations on relational data. Basic
operators are fundamental operations that include selection (σ), projection (π), union (U), set
difference (−), Cartesian product (×), and rename (ρ).
1. Selection(σ)
The Selection Operation is basically used to filter out rows from a given table based on certain
given condition. It basically allows us to retrieve only those rows that match the condition as per
condition passed during SQL Query.
Example: If we have a relation R with attributes A, B, and C, and we want to select tuples
where C > 3, we write:
A B C
1 2 4
2 2 3
3 2 3
4 3 4
A B C
1 2 4
4 3 4
Explanation: The selection operation only filters rows but does not display or change their
order. The projection operator is used for displaying specific columns.
2. Projection(π)
While Selection operation works on rows, similarly projection operation of relational algebra
works on columns. It basically allows us to pick specific columns from a given relational table
based on the given condition and ignoring all the other remaining columns.
Example: Suppose we want columns B and C from Relation R.
π(B,C)(R) will show following columns.
Output:
B C
2 4
2 3
3 4
3. Union(U)
The Union Operator is basically used to combine the results of two queries into a single result.
The only condition is that both queries must return same number of columns with same data
types. Union operation in relational algebra is the same as union operation in set theory.
Example: Consider the following table of Students having different optional subjects in their
course.
FRENCH
`Student_Name Roll_Number
Ram 01
Mohan 02
Vivek 13
Geeta 17
GERMAN
Student_Name Roll_Number
Vivek 13
Geeta 17
Shyam 21
Rohan 25
If FRENCH and GERMAN relations represent student names in two subjects, we can combine
their student names as follows:
π(Student_Name,Roll_Number)(FRENCH) U π(Student_Name,Roll_Number
)(GERMAN)
Output:
Student_Name
Ram
Mohan
Vivek
Geeta
Shyam
Rohan
Explanation: The only constraint in the union of two relations is that both relations must have
the same set of Attributes.
4. Set Difference(-)
Set difference basically provides the rows that are present in one table, but not in another tables.
Set Difference in relational algebra is the same set difference operation as in set theory.
Example: To find students enrolled only in FRENCH but not in GERMAN, we write:
π(Student_Name)(FRENCH) - π(Student_Name)(GERMAN)
Student_Name
Ram
Mohan
Explanation: The only constraint in the Set Difference between two relations is that both
relations must have the same set of Attributes.
5. Rename(ρ)
Rename operator basically allows you to give a temporary name to a specific relational table or
to its columns. It is very useful when we want to avoid ambiguity, especially in complex Queries.
Rename is a unary operation used for renaming attributes of a relation.
Example: We can rename an attribute B in relation R to D
A B C
1 2 4
2 2 3
3 2 3
4 3 4
A D C
1 2 4
2 2 3
3 2 3
4 3 4
6. Cartesian Product(X)
The Cartesian product combines every row of one table with every row of another table,
producing all the possible combination. It's mostly used as a precursor to more complex
operation like joins. Let’s say A and B, so the cross product between A X B will result in all the
attributes of A followed by each attribute of B. Each record of A will pair with every record of
B.
Relation A:
Ram 14 M
Sona 15 F
Kim 20 M
Relation B:
ID Course
1 DS
2 DBMS
Output: If relation A has 3 rows and relation B has 2 rows, the Cartesian product A × B will
result in 6 rows.
Ram 14 M 1 DS
Ram 14 M 2 DBMS
Sona 15 F 1 DS
Sona 15 F 2 DBMS
Kim 20 M 1 DS
Kim 20 M 2 DBMS
Explanation: If A has 'n' tuples and B has 'm' tuples then A X B will have 'n*m' tuples.
Derived Operators in Relational Algebra
Derived operators are built using basic operators and include operations like join, intersection,
and division. These operators help perform more complex queries by combining basic operations
to meet specific data retrieval needs.
1. Join Operators
Join operations in relational algebra combine data from two or more relations based on a related
attribute, allowing for more complex queries and data retrieval. Different types of joins include:
Inner Join
An inner join combines rows from two relations based on a matching condition and only returns
rows where there is a match in both relations. If a record in one relation doesn't have a
corresponding match in the other, it is excluded from the result. This is the most common type of
join.
StudentCourse Table
CourseID EnrollNo
1 1001
2 1001
3 1001
1 1002
2 1003
Syntax:
ON Condition;
The following is a join query that shows the names of students enrolled in different courses.
Query:
SELECT [Link],[Link]
FROM Student
INNER JOIN StudentCourse
ON [Link] = [Link]
ORDER BY [Link];
Note: Inner is optional above. Simple JOIN is also considered as INNER JOIN The above query
would produce the following result.
CourseID StudentName
1 geek1
1 geek2
2 geek1
2 geek3
3 geek1
Example:
For example, let's say we have two tables, Table1 and Table2, with the following data:
Table 1
ID Name
1 John
2 Sarah
3 David
Table 2
ID Address
If we perform an Inner Join on these tables using the ID column, the result set would only
include the matching rows from both tables, which are the rows with ID values of 1 and 2:
Query:
SELECT [Link],
Table 1. Name, Table [Link]
FROM Table1
INNER JOIN Table2
ON [Link] = [Link]
Output:
ID Name Address
● Conditional Join: A conditional join is an inner join where the matching condition
can involve any comparison operator like equals (=), greater than (>), etc. Example:
Joining Employees and Departments on DepartmentID where Salary > 50000 will
return employees in departments with a salary greater than 50,000
● Equi Join: An equi join is a type of conditional join where the condition is
specifically equality (=) between columns from both relations. Example: Joining
Customers and Orders on CustomerID where both relations have this column,
returning only matching records.
● Natural Join: A natural join automatically combines relations based on columns with
the same name and type, removing duplicate columns in the result. It’s a more
efficient way of joining. Example: Joining Students and Enrollments where
StudentID is common in both, and the result contains only unique columns.
Outer Join
An outer join returns all rows from one relation, and the matching rows from the other relation.
If there is no match, the result will still include all rows from the outer relation with NULL
values in the columns from the unmatched relation.
● Left Outer Join: A left outer join returns all rows from the left relation and the
matching rows from the right relation. If there is no match, the result will include
NULL values for the right relation’s attributes. Example: Joining Employees with
Departments using a left outer join ensures all employees are listed, even those who
aren't assigned to any department, with NULL values for the department columns.
● Right Outer Join: A right outer join returns all rows from the right relation and the
matching rows from the left relation. If no match exists, the left relation's columns
will contain NULL values. Example: Joining Departments with Employees using a
right outer join includes all departments, even those with no employees assigned,
filling unmatched employee columns with NULL.
● Full Outer Join: A full outer join returns all rows when there is a match in either the
left or right relation. If a row from one relation does not have a match in the other,
NULL values are included for the missing side. Example: Joining Customers and
Orders using a full outer join will return all customers and orders, even if there’s no
corresponding order for a customer or no customer for an order.
1. Left Outer join
Left Outer Join returns all rows of a table on the left side of the join. For the
rows for which there is no matching row on the right side, the result contains
NULL on the right side.
Left Outer Join returns all the rows from the left table and matching rows
from the right table. If a row in the left table does not have a matching row in
the right table, the result set will include NULL values for the columns in the
right table.
Syntax:
SELECT T1.C1, T2.C2
FROM TABLE T1
LEFT JOIN TABLE T2
ON T1.C1= T2.C1;
Query:
SELECT [Link],[Link]
FROM Student
LEFT OUTER JOIN StudentCourse
ON [Link] = [Link]
ORDER BY [Link];
Note: OUTER is optional above. Simple LEFT JOIN is also considered as LEFT
OUTER JOIN
StudentName CourseID
geek4 NULL
geek2 1
geek1 1
geek1 2
geek3 2
geek1 3
Syntax:
SELECT T1.C1, T2.C2
FROM TABLE T1
Example:
Table Record
Ag
Roll_Number Name
e
1 Harsh 18
2 Ankesh 19
3 Rupesh 18
4 Vaibhav 15
5 Naveen 13
6 Shubham 15
7 Ankit 19
8 Bhupesh 18
Table Course
Course_ID Roll_Number
1 1
2 2
2 3
3 4
1 5
4 9
5 10
4 11
Query:
SELECT [Link],Course.COURSE_ID
FROM Record
RIGHT JOIN Course
ON Course.Roll_Number = Record.Roll_Number;
Output:
Name Course_ID
Harsh 1
Ankesh 2
Rupesh 2
Vaibha
3
v
Naveen 1
NULL 4
NULL 5
NULL 4
Full Outer Join contains the results of both the Left and Right outer joins. It is
also known as cross-join. It will provide a mixture of two tables.
Full Outer Join returns all the rows from both tables, including matching and
non-matching rows. If a row in one table does not have a matching row in the
other table, the result set will include NULL values for the columns in the
table that do not have a match.
Full Outer Join
Syntax:
SELECT * FROM T1
CROSS-JOIN T2;
Query:
SELECT [Link],Course.COURSE_ID
FROM Record
FULL JOIN Course
ON Course.Roll_Number = Record.Roll_Number;
Output:
Name Course_ID
Harsh 1
Ankesh 2
Rupesh 2
Vaibhav 3
Naveen 1
Shubham NULL
Ankit NULL
Bhupesh NULL
NULL 4
NULL 5
NULL 4
2. Set Intersection(∩)
Set Intersection basically allows to fetches only those rows of data that are common between two
sets of relational tables. Set Intersection in relational algebra is the same set intersection
operation in set theory.
Example: Consider the following table of Students having different optional subjects in their
course.
Relation FRENCH
Student_Name Roll_Number
Ram 01
Mohan 02
Vivek 13
Geeta 17
Relation GERMAN
Student_Name Roll_Number
Vivek 13
Geeta 17
Shyam 21
Rohan 25
From the above table of FRENCH and GERMAN, the Set Intersection is used as follows:
π(Student_Name)(FRENCH ∩ π(Student_Name)(GERMAN)
Output:
Student_Name
Vivek
Geeta
Explanation: The only constraint in the Set Difference between two relations is that both
relations must have the same set of Attributes.
3. Division (÷)
The Division Operator is used to find tuples in one relation that are related to all tuples in another
relation. It’s typically used for "for all" queries.
Student_Course (Dividend Table):
Student_ID Course_ID
101 C1
101 C2
102 C1
103 C1
103 C2
Course_ID
C1
C2
Example: Query is to find students who are enrolled in all courses listed in the Course table. In
this case, students must be enrolled in both C1 and C2.
Student_Course(Student_ID, Course_ID)÷ Course(Course_ID)
Output:
Student_ID
101
103
Relational Calculus
Relational calculus is a non-procedural query language used in the context of relational algebra.
It focuses on what data to retrieve, rather than how to retrieve it, making it different from
relational algebra, which is procedural. In relational calculus, queries are expressed using logical
formulas that describe the desired result, without specifying the exact steps to get there.
There are two types of Relational Calculus
1. Tuple Relational Calculus(TRC)
2. Domain Relational Calculus(DRC)
Tuple Relational Calculus (TRC) in DBMS
Tuple Relational Calculus (TRC) is a non-procedural query language used to
{ t | P(t) }
● ∧: AND
● ∨: OR
● ¬: NOT
Quantifiers:
● ∃ t ∈ r (Q(t)) → There exists a tuple t in relation r satisfying
predicate Q(t)
For example, let's say we have a table called "Employees" with the following
attributes:
Employee ID
Name
Salary
Department
ID
To retrieve the names of all employees who earn more than $50,000 per
● The result is a set of tuples where each employee earns more than
$50,000.
it.
{t| P(t)}
in the result
P(t) may have various conditions logically combined with OR (∨), AND (∧),
NOT(¬).
makes a list of the attributes that are to be chosen from the relations as per
the conditions.
{<a1,a2,a3,.....an> | P(a1,a2,a3,.....an)}
where a1,a2,...an are the attributes of the relation and P is the condition.
Table Customer
Stree
Customer name City
t
Saurabh A7 Patiala
Mehak B6 Jalandhar
Sumiti D9 Ludhiana
Ria A5 Patiala
Table Branch
ABC Patiala
DEF Ludhiana
GHI Jalandhar
Table Account
Table Loan
Table Borrower
Saurabh L33
Mehak L49
Ria L98
Table Depositor
Saurabh 1111
Mehak 1113
Suniti 1114
Example 1: Find the loan number, branch, and amount of loans greater than
Resulting relation:
Loan number Branch name Amount
Example 2: Find the loan number for each loan of an amount greater or equal
to 10000.
Resulting relation:
Loan number
L33
L35
L98
Example 3: Find the names of all customers who have a loan and an account
at the bank.
Resulting relation:
Customer name
Saurabh
Mehak
Example 4: Find the names of all customers having a loan at the "ABC"
branch.
{t | ∃ s ∈ borrower(t[customer-name] = s[customer-name]
∧ ∃ u ∈ loan(u[branch-name] = “ABC” ∧ u[loan-number] =
s[loan-number]))}
Resulting relation:
Customer name
Saurabh
Key Concepts:
● TRC does not specify execution steps, only the condition of result.
● It focuses on what to retrieve, not how.
Relational
Feature TRC
Algebra
How to
Focus What to retrieve
retrieve
Set-based
Expression Style Logical expressions
operators
Directly
Abstract, not directly
Execution convertible
executable
to query
Basis for
Use in DBMS Theoretical foundation query
execution
them.
●
Tuple Relational Calculus (TRC)
In Tuple Relational Calculus (TRC), queries are based on finding tuples that satisfy
specific conditions. Let’s look at two examples and explain how the workflow of the
queries proceeds step by step.
1 Alice 20 A
2 Bob 19 B
3 Charlie 22 A
4 David 18 C
TRC Query:
Workflow:
1 Alice 20 A
3 Charlie 22 A
TRC Query:
Workflow:
● Define Tuple Variable: The tuple variable t represents rows in the “Students”
table.
● Set the Condition: The condition [Link] > 18 filters out students older than 18.
● Filter the Table: Each row is checked, and only those where the “Age” attribute is
more than 18 are selected.
● Output the Result: The query returns rows that satisfy the age condition.
Result:
1 Alice 20 A
2 Bob 19 B
3 Charlie 22 A
1 Alice 20 A
2 Bob 19 B
3 Charlie 22 A
4 David 18 C
DRC Query:
{<Name> | ∃ ID, Age, Grade (Students(ID, Name, Age, Grade) ∧ Grade = ‘A’)}
Workflow:
● Define Domain Variables: The variables represent the attributes of the “Students”
table (ID, Name, Age, Grade).
● Specify the Condition: The condition Grade = ‘A’ is applied to filter students who
have an ‘A’ grade.
● Match the Records: The query checks each row to see if it meets the condition.
● Return Attribute Values: Instead of returning the whole row, only the “Name” of
students with grade ‘A’ is retrieved.
Result:
Name
Alice
Charlie
DRC Query:
{<Name, Age> | ∃ ID, Grade (Students(ID, Name, Age, Grade) ∧ Age > 18)}
Workflow:
● Define Domain Variables: Variables Name and Age represent the attributes that
will be in the output.
● Set the Condition: The predicate Age > 18 checks which students meet this age
requirement.
● Evaluate Each Row: The query scans through the rows in the “Students” table.
● Output Specified Attributes: The “Name” and “Age” values of students older than
18 are returned.
Result:
Name Age
Alice 20
Bob 19
Charlie 22
Ease of Use More natural for querying Better suited for simple
full rows and complex queries involving specific
relationships. fields.
Common Use Often used for retrieving Used to fetch specific columns
Case entire records that match a based on certain conditions.
certain criteria.
1. Query Expression:
● Users write queries using relational calculus expressions. These expressions
consist of logical conditions or predicates that specify the data requirements.
● For Tuple Relational Calculus (TRC), queries are expressed in the form { t | P(t)
}, where t represents a tuple, and P(t) is a condition that the tuple must satisfy.
● For Domain Relational Calculus (DRC), queries are expressed as {<x1, x2, …,
xn> | P(x1, x2, …, xn)}, where <x1, x2, …, xn> represents attribute values, and P
is the condition.
2. Evaluation of Conditions:
● The DBMS evaluates the conditions specified in the query. It checks which
records in the database satisfy the logical predicates.
● For TRC, this involves filtering entire tuples (rows) based on the conditions. For
DRC, it involves filtering attribute values within those tuples.
4. Logical Optimization:
● Before executing the query, the DBMS may optimise the logical expression to
improve performance.
● This step involves rewriting the logical conditions or rearranging operations
without changing the result, making the query execution faster.
5. Result Compilation:
● The data that satisfies the conditions is collected and compiled into a result set.
● For TRC, the result includes tuples that meet the criteria. For DRC, the result
contains specific attribute values that match the condition.
6. Displaying Results:
● Finally, the DBMS displays the output of the query to the user.
● The results can be presented as tables (for TRC) or as a list of values (for DRC),
depending on the nature of the query.
Integrity constraints are a set of rules used in DBMS to ensure that the data
maintaining the quality of data by ensuring that the processes like adding,
Integrity constraints also define how different parts of the database are
connected and ensure that these relationships remain valid. They play an
essential role in making sure the data is meaningful and follows the logical
keep the data in a database accurate, consistent and reliable. They act like a
set of guidelines that ensure all the information stored in the database
Integrity Constraints
Example: Making sure every customer has a valid email address & ensuring
There are Different types of Integrity Constraints used in DBMS, these are:
5. Assertion
6. Triggers
1. Domain Constraints
Domain constraints are a type of integrity constraint that ensure the values
stored in a column (or attribute) of a database are valid and within a specific
range or domain. In simple terms, they define what type of data is allowed in
a column and restrict invalid data entry. The data type of domain include
string, char, time, integer, date, currency etc. The value of the attribute must
Manvendra
21CSE102 8th 22
Sharma
2. Name: Accepts only valid text (no numbers) and cannot be left
3. Semester: Allows specific values like 5th, 6th, etc., and ensures
4. Age: Must be an integer within a reasonable range (e.g., 18-30) and
database.
business rules.
errors.
in the relation/table given below, then the data entry of 4th tuple will violate
contains null value. To make this database instance a legal instance, its entry
Ag
Student_id Name Semester
e
21CSE1001 Sonali Rao 5th 20
Entity integrity constraints state that primary key can never contain null value
uniquely, if primary key contains null value then we cannot identify those
rows. A table can contain null value in it except primary key field.
column.
● NOT NULL: The primary key column cannot contain NULL values, as
● Essential for Table Design: Ensures that every record in the table
as NULL value.
3. Key Constraints
table uniquely identify each row. These rules are essential for maintaining
foreign keys).
It states that the primary key attributes are required to be unique and not
null. That is, primary key attributes of a relation must not have null values
and primary key attributes of two tuples must never be same. This constraint
is specified on database schema to the primary key attributes to ensure that
Example: Here, in the below example the Student_id is the primary key
attribute. The data entry of 4th tuple violates the primary key constraint that
Ag
Student_id Name Semester
e
● Unique Values: Each student_id must be unique. 101, 102, 103 are
The Unique key constraint in DBMS ensures that all values in a specified
column (or group of columns) are distinct across the table. It prevents
duplicate entries, maintaining data integrity, but unlike the primary key, it
Example: Here, in the below example the Email column has NULL value in
2nd record.
error.
● Allows One NULL: The email column can contain one NULL value.
tables remain consistent. They enforce that a foreign key in one table must
relational database.
valid.
● Prevents Orphan Records: Avoids cases where a record in a child
a relational database.
20 Chandigarh
21 Punjab
25 Delhi
5. Assertion
An assertion is a declarative mechanism in a database that ensures a specific
When an assertion is made, the system tests it for validity. This testing may
Example of an Assertion:
Explanation:
The following SQL statement creates an assertion to ensure that the total
loan amount at each branch is always less than the total account balances at
all loan amounts for a branch must always be less than the sum of
calculates the total loan amount for each branch. The second
balance.
database.
6. Triggers
● Trigger Type: The trigger runs after an update on the account table.
update.
zero. A loan is created with the same account number as the loan
number, and the loan amount is the absolute value of the negative
✅ Example:
Suppose we have a table Student:
102 Amit CN
●
Entity Integrity ensures:
● Update anomaly: Prevents two different students from sharing the same ID.
● Deletion anomaly: Ensures that deletion of one student doesn’t confuse identity with
another (since IDs are unique).
Example:
Two tables:
Course (Parent)
C1 DBMS
C2 CN
Enrollment (Child)
EnrollID StudentID CourseID (FK)
E1 101 C1
E2 102 C2
●
Referential Integrity ensures:
● Insertion anomaly: You can’t insert enrollment for a course that doesn’t exist.
● Deletion anomaly: You can’t delete a course (say C1) if students are still enrolled in it →
prevents accidental loss of related data.
● Update anomaly: If course ID changes, it must be updated in both tables, or else it’s
rejected.
(In short)
● Entity Integrity keeps each row unique → prevents duplicate/missing identities →
avoids anomalies.
Together, these constraints maintain database consistency and prevent insertion, deletion,
and update anomalies.
Types of Functional dependencies in DBMS
Last Updated : 23 Jul, 2025
specifies the relationship between two sets of attributes where one attribute
attribute set on the left side of the arrow, X is called Determinant, and Y is
Example:
roll_no name dept_name dept_building
42 abc CO A4
43 pqr IT A3
44 xyz CO A4
45 xyz IT A3
46 mno EC B2
47 jkl ME B2
From the above table we can conclude some valid functional
dependencies:
dept_name also.
functional dependency.
Example 1 :
● ABC -> AB
● ABC -> A
● ABC -> ABC
Example 2:
42 abc 17
43 pqr 18
44 xyz 18
Example 1 :
● Id -> Name
Example 2:
42 abc 17
43 pqr 18
44 xyz 18
not all of it. This is a middle ground between trivial and non-trivial functional
NULL.
Example:
Functional Dependency:
{StudentID,CourseID}→CourseID
dependency.
Example:
bike_model manuf_year color
In this table:
● X: bike_model
● Y: color
● Z: manuf_year
years (manuf_year).
2. The colors do not depend on the manufacturing year, and the
independent.
3. The sets of color and manuf_year are linked only to bike_model.
Example:
dep
enrol_no name building_no
t
42 abc CO 4
43 pqr EC 2
44 xyz IT 1
45 abc EC 2
Here, enrol_no → dept and dept → building_no. Hence, according to the
X, Y, Z with the dependencies X->Y and X->Z which states that those
composite key, rather than the whole key. If a relation R has attributes X, Y, Z
where X and Y are the composite key and Z is non key attribute. Then X->Z is
Conclusion
Functional dependency is very important concept in database management
system for ensuring the data consistency and accuracy. In this article we have
discuss what is the concept behind functional dependencies and why they
are important. The valid and invalid functional dependencies and the types of