Dbms Chapter 3
Dbms Chapter 3
Unit-3
Relational Database Model and
Algebra
Primary Key
Child
Parent
Table
Table
Student Project
EnrollNo Name Branch Sem ProjectID Title EnrollNo
190540107001 Raju CE 3 101 Bank 190540107001
190540107002 Sanjay CE 3 102 College 190540107002
190540107003 Shyam CE 3 103 School 190540107003
190540107004 ram CE 3 104 Hospital 190540107001
Foreign key and referential integrity
Integrity means same all over.
If ram belong to bca then he must belong to bca faculty not to
masters in crisis or MCA
Primary Foreign key
Primary
Student key Course
key
Roll_no Name Address Course_id Course_name Roll_no
R1 R2
fk
a b c x y z
R1 R2
fk
a b c x y z
Foreign Foreign
key Did Dname Block
Eid Ename Age key Eid Did
D1 Electronics A
E1 A 20
E1 D1 D2 Computer A Table Reduction
E2 B 21
E2 D2 D3 Lab B One to one can be merged
E3 C 20
E3 D4 D4 SBCAence and C
E4 A 22 Eid Ename Age Did
Humanities
E4 D3 E1 A 20 D1
E5 B 21
D5 Elective D
E2 B 21 D2
E3 C 20 D4
Primary key = Either Eid or Did
E4 A 22 D3
E5 B 21
One –many relationship O_no
ID Date
Item
1 M name
Name Customer Give Order
city Cost
Foreign
key Foreign
key
ID Name city ID O_no Date O_no Item Cost ID O-no Item Cost
name name
C1 A Ktm C1 O1
O1 Shoes 3000 C1 O1 Shoes 3000
C2 B Pok C1 O2
O2 Shirt 2000 C1 O2 Shirt 2000
C3 C Ktm C2 O3
O3 Jersey 1000 C2 O3 Jersey 1000
C4 D Pok C2 O4
O4 Sweater 1500 C2 O4 Sweater 1500
C5 E dang C2 O5
O5 pant 2500 C2 O5 Pant 2500
Primary key = O_no
Primary key = O_no
Can be merged to the many side
Many to many (M-N)
M N
Student Study Course
Foreign
Foreign
key
key
A A, B B
Example Display the detail of students belongs to “CE” Branch. Answer σBranch=‘CE’ (Student)
Student Output
RollNo Name Branch SPI RollNo Name Branch SPI
101 Raju CE 8 101 Raju CE 8
102 Sanjay ME 9 104 Ram CE 9
103 Shyam BCA 9
104 Ram CE 9
Selection Operator [σ condition (Relation)]
Display the detail of students belongs to “CE” Branch and having SPI more than 8.
Example
Student
RollNo Name Branch SPI
101 Raju CE 8
102 Sanjay ME 9
103 Shyam BCA 9
104 ram CE 9
Answer
σBranch=‘CE’ Λ SPI>8 (Student)
Output
RollNo Name Branch SPI
104 ram CE 9
Selection Operator [σ condition (Relation)]
Display the detail of students belongs to either “BCA” or “ME” Branch.
Example
Student
RollNo Name Branch SPI
101 Raju CE 8
102 Sanjay ME 9
103 Shyam BCA 9
104 ram CE 9
Student
RollNo Name Branch SPI
101 Raju CE 8
102 Sanjay ME 9
103 Shyam BCA 9
104 ram CE 9
Employee
Write down the relational algebra for the employee
table. EmpID Name Dept Salary
Display the detail of all employee. 101 Shyam Sales 10000
Display the detail of employee whose Salary more than 10000. 102 Mayur HR 25000
Display the detail of employee belongs to “HR” Dept having 103 Swopnil HR 15000
Salary more than 20000.
Display the detail of employee belongs to either “HR” or 104 Ajay Admin 20000
“Admin” Dept.
Display the detail of employee whose Salary between 10000
and 25000 and belongs to “HR” Dept.
Projection Operator
Projection Operator
Symbol: ∏ (Pi)
Example Display RollNo, Name and Branch of all students. Answer ∏ RollNo, Name, Branch (Student)
Student Output
RollNo Name Branch SPI RollNo Name Branch
101 Raju CE 8 101 Raju CE
102 Sanjay ME 9 102 Sanjay ME
103 Shyam BCA 9 103 Shyam BCA
104 ram CE 9 104 ram CE
Exercise
Write down the relational algebra for the student table. Student
Display RollNo, Name and SPI of all students. RollNo Name Branch SPI
Display Name and SPI of all students. 101 Raj CE 6
Display the Name of all students. 102 ram ME 8
Display the Name of all branches. 103 Bharat EE 7
104 Sanjay CE 9
Employee
Write down the relational algebra for the employee
table. EmpID Name Dept Salary
Display EmpID with Name of all employee. 101 Shyam Sales 10000
Display Name and Salary of all employee. 102 Mayur HR 25000
Display the Name of all employee. 103 Swopnil HR 15000
Display the Name of all departments.
104 Ajay Admin 20000
Combined Projection & Selection Operation
Example Display RollNo, Name & Branch of “ME” Branch students.
Student
RollNo Name Branch SPI
101 Raju CE 8
102 Sanjay ME 9
103 Shyam BCA 9
104 ram CE 7
Output-1 Output-2
RollNo Name Branch SPI RollNo Name Branch
102 Sanjay ME 9 102 Sanjay ME
Combined Projection & Selection Operation
Example Display Name, Branch and SPI of students whose SPI is more than 8.
Student
RollNo Name Branch SPI
101 Raju CE 8
102 Sanjay ME 9
103 Shyam BCA 9
104 ram CE 7
Output-1 Output-2
RollNo Name Branch SPI Name Branch SPI
102 Sanjay ME 9 Sanjay ME 9
103 Shyam BCA 9 Shyam BCA 9
Combined Projection & Selection Operation
Example Display Name, Branch and SPI of students who belongs to “CE” Branch and SPI is more than 7.
Student
RollNo Name Branch SPI
101 Raju CE 8
102 Sanjay ME 9
103 Shyam BCA 9
104 ram CE 7
Step-1
σBranch=‘CE’ Λ SPI>7 (Student)
Answer
∏ Name, Branch, SPI (σBranch=‘CE’ Λ SPI>7 (Student))
Output-1 Output-2
RollNo Name Branch SPI Name Branch SPI
101 Raju CE 8 Raju CE 8
Combined Projection & Selection Operation
Example Display Name of students along with their Branch who belong to either “ME” Branch or “BCA” Branch.
Student
RollNo Name Branch SPI
101 Raju CE 8
102 Sanjay ME 9
103 Shyam BCA 9
104 ram CE 7
Step-1
σBranch=‘ME’ V Branch=‘BCA’ (Student)
Answer ∏ Name, Branch (σBranch=‘ME’ V Branch=‘BCA’ (Student))
Output-1 Output-2
RollNo Name Branch SPI Name Branch
102 Sanjay ME 9 Sanjay ME
103 Shyam BCA 9 Shyam BCA
Exercise ∏ RollNo, Name, SPI (σ Branch=‘CE’ (Student))
Write down the relational algebra for the student table. Student
Display Rollno, Name and SPI of all students belongs to “CE” Branch. RollNo Name Branch SPI
List the Name of students with their Branch whose SPI is more than 8 and 101 Raj CE 6
belongs to “CE” Branch.
List the Name of students along with their Branch and SPI who belongs to
102 ram ME 8
either “CE” or “ME” Branch and having SPI more than 8. 103 Bharat EE 7
Display the Name of students with their Branch name whose SPI between 7
and 9. 104 Sanjay CE 9
Example Perform Cross Product between Student and Result. Answer (Student) X (Result)
Output
Student.RNo Name Branch Sem Result.RNo SPI BL Rank
101 Raju CE 3 103 9 0 1
103 OM CE 3 103 9 0 1
Natural Join / Inner Join
Natural Join / Inner Join
Symbol:
Notation: Relation-1 (R1) Relation-2 (R2) OR Algebra-1 Algebra-2
Operation: Natural join will retrieve consistent data from multiple relations.
It combines records from different relations that satisfy a given condition.
List the name of students with their department name having backlog 0.
List the name of faculties with their department name and salary having salary more than 25000
and belongs to “CE” department.
List the name of all faculties of “CE” and “ME” department whose salary is more than 50000.
Display the students name with their project name of all “CE” department’s students whose guide
is “sujan kc”.
Display the name of faculties with their department name who belongs to “CE” department and
tough “CPU” subject having salary more than 25000.
List the name of students with their department name doing project “Hackathon” under guide
“Ajay Shah”.
Outer Join
Outer Join
In natural join some records are missing, if we want that missing records then we
have to use outer join.
Example Perform Left Outer Join between Student and Result. Answer (Student) (Result)
Student Result
RollNo Name Branch RollNo SPI BL
101 Raj CE 101 8 1 RollNo Name Branch SPI BL
102 ram ME 103 9 0 101 Raj CE 8 1
102 ram ME NULL NULL
Answer ∏ RollNo, Name, SPI ((Student) (Result))
Output
RollNo Name SPI
101 Raj 8
102 ram NULL
Right Outer Join
Symbol:
Notation: Relation-1 (R1) Relation-2 (R2) OR Algebra-1 Algebra-2
Operation:
Display all the tuples of right relation even through there is no matching tuple in the left relation.
For such kind of tuples having no matching, the attributes of left relation will be padded with NULL in resultant
relation.
Example Perform Right Outer Join between Student and Result. Answer (Student) (Result)
Student Result
RollNo Name Branch RollNo SPI BL
101 Raj CE 101 8 1
102 ram ME 103 9 0
Output
RollNo Name SPI
101 Raj 8
103 NULL 9
Full Outer Join
Symbol:
Notation: Relation-1 (R1) Relation-2 (R2) OR Algebra-1 Algebra-2
Operation:
Display all the tuples of both of the relations. It also pads null values whenever required. (Left outer join + Right
outer join)
For such kind of tuples having no matching, it will be padded with NULL in resultant relation.
Example Perform Full Outer Join between Student and Result. Answer (Student) (Result)
Student Result
RollNo Name Branch RollNo SPI BL
101 Raj CE 101 8 1
102 ram ME 103 9 0
Output
RollNo Name SPI
101 Raj 8
102 ram NULL
103 NULL 9
Self Join Example
Example Find student id who is enrolled in at least two courses
Study
S_id C_id Since Study1 Study2
∏ Study1._id
((Study1) (Study2))study1.S_id = study2.S_id and study1.C_id <> study2.C_id
Study1 Study2
S_id C_id Since S_id C_id Since
101 DBMS 2022 101 DBMS 2022
102 CN 2021 101 DBMS 2022 Study1 Study2
101 OS 2022 101 DBMS 2022 S_id C_id Since S_id C_id Since
101 DBMS 2022 102 CN 2021 101 OS 2022 101 DBMS
102 CN 2021 102 CN 2021 101 DBMS 2022 101 OS 2022
101 OS 2022 102 CN 2022
101 DBMS 2022 101 OS 2022
102 CN 2021 101 OS 2022 Output
1 Raj 1 1 Raj 1
1 Raj 1 3 Jay 3
2 ram 1 3 Jay 3
3 Jay 3 3 Jay 3
Employee Department
D_id Location E_id Employee Department
E_id Ename Address
101 Ram Balaju 01 Kalanki 103 E_id Ename Address D_id Location E_id
∏ ((Employee)
Ename
(Department))employee.E_id = Department.E_id and employee.address = department.location
Employee Department
E_id Ename Address D_id Location E_id
Employee Department
101 Ram Balaju 01 Kalanki 103
E_id Ename Address D_id Location E_id
102 Shyam Kalimati 01 Kalanki 103
103 Bharat kalanki 01 Kalanki 103
103 Bharat kalanki 01 Kalanki 103
Output
Bharat
Consider the relational database given below and give an expression in the relational algebra:
Employee (E_id, E-name, salary), Department (D_id, D-name, budget, E_id)
I. Find the names of all employees in this database who have higher salary than the budget of
department.
Consider the relational database given below and give an expression in the relational algebra:
Employee (person-name, street, city) , Works (person-name, company-name, salary)
Company (company-name, city) , Manages (person-name, manager-name)
I. Find the names of all employees in this database who live in the same city as the company for
which they work.
Set Operators
Set Operators
Set operators combine the results of two or more queries into a single result.
Conditions Set operators will take two or more queries as input, which must be union-compatible.
Example Perform Union between Customer and Employee. Answer (Customer) U (Employee)
exercise Is there any difference in the output if we swap the tables in Intersection. (Employee)
∩ (Customer).
Minus/ Set difference Operator
Symbol: −
Notation: Relation-1 (R1) − Relation-2 (R2) OR Algebra-1 − Algebra-2
Operation:
It displays all the tuples/records belonging to the first relation (left relation) but not in the second relation (right
relation).
Example Perform Set difference between Customer and Employee. Answer (Customer) − (Employee)
Customer Employee Output
Name Name Name
Raju ram Raju
Suresh Suresh
ram Manoj
exercise Is there any difference in the output if we swap the tables in Set difference. (Employee)
− (Customer).
Union Operators Example
Customer Employee
ID Name Balance ID Name Dept Salary
1 Raju 10000 2 Suresh CE 8000
2 Suresh 20000 3 Manoj ME 9000
Output
Name
Manoj
Raju
Suresh
Intersect/ Intersection Operators Example
Customer Employee
ID Name Balance ID Name Dept Salary
1 Raju 10000 2 Suresh CE 8000
2 Suresh 20000 3 Manoj ME 9000
Output
Name
Suresh
Minus/ Set difference Operators Example
Example Display Name of person who are employee but not customer.
Customer Employee
ID Name Balance ID Name Dept Salary
1 Raju 10000 2 Suresh CE 8000
2 Suresh 20000 3 Manoj ME 9000
Output
Name
Manoj
Minus/ Set difference Operators Example
Example Display Name of person who are customer but not employee.
Customer Employee
ID Name Balance ID Name Dept Salary
1 Raju 10000 2 Suresh CE 8000
2 Suresh 20000 3 Manoj ME 9000
Output
Name
Raju
Division Operator
Division Operator
Symbol: ÷ (Division)
Notation: Relation1 (R1) ÷ Relation2 (R2) OR Algebra1 ÷ Algebra2
Condition:
Attributes of relation2/algebra2 must be a proper subset of attributes of relation1/algebra1.
Operation:
The output of the division operator will have attributes =
All attributes of relation1 – All attributes of relation2
The output of the division operator will have tuples =
Tuples in relation1, which are associated with the all tuples of relation2.
Division Operator Example
Example Perform Division operation between Student and Subject. Answer (Student) ÷ (Subject)
A B1 B2 B3 B4
Sno PNo PNo PNo PNo PNo
S1 P1 P2 P2 P1 P2
S1 P2 P4 P2 P5
S1 P3 P4
S1 P4 Algebra (A) ÷ (B1) Algebra (A) ÷ (B2) Algebra (A) ÷ (B3) Algebra (A) ÷ (B4)
S2 P1
Output Output Output Output
S2 P2
SNo SNo SNo SNo
S3 P2
S1 S1 S1
S4 P2
S2 S4
S4 P4
S3
S5 P4
S4
Division Operator Example
Example List the name of students doing a project in all technologies.
Student Project
RNo Name Technology TID Technology
101 Raj .NET 1 .NET
Student Student
Person Student
Student Student
Rno Name CPI Rno Name CPI
101 Raj 8 101 Raj 8
102 ram 9 102 ram 9
103 Jay 7 103 Jay 7
Algebra ρPerson (RollNo, StudentName) (∏ RNo, Name (Student)) Algebra ρStudentName / Name (Student)
Person Student
RollNo StudentName Rno StudentName CPI
101 Raj 101 Raj 8
102 ram 102 ram 9
103 Jay 103 Jay 7
Rename Operator Example
Example Find out maximum CPI from student table. Step-1 ρA (Student) X ρB (Student)
Student Output-1
A.Rno A.Name A.CPI B.Rno B.Name B.CPI 103 Jay 7 101 Raj 8
101 Raj 8 102 ram 9 103 Jay 7 102 ram 9
103 Jay 7 101 Raj 8 103 Jay 7 103 Jay 7
103 Jay 7 102 ram 9
Rename Operator Example
Example Find out maximum CPI from student table.
Student
Step-3
∏ A.CPI (σA.CPI<B.CPI (ρA (Student) X ρB (Student)))
Rno Name CPI
101 Raj 8 Output-3
Output-2
Student
Step-3
∏ A.CPI (σA.CPI<B.CPI (ρA (Student) X ρB (Student)))
Rno Name CPI
Output-3
101 Raj 8
A.CPI
102 ram 9
8
103 Jay 7
7
Step-4
∏ CPI (Student) − ∏ A.CPI (σA.CPI<B.CPI (ρA (Student) X ρB (Student)))
Customer Employee
ID Name Balance ID Name Dept Salary
1 Raju 10000 2 Suresh CE 8000
2 Suresh 20000 3 Manoj ME 9000
Customer Employee
ID Name Balance ID Name Dept Salary
ID Name ID Name
1 Raju 10000 2 Suresh CE 8000
1 Raju 2 Suresh
2 Suresh 20000 3 Manoj ME 9000
2 Suresh 3 Manoj
ID Name
Algebra-1 ∏ ID, Name (Customer) − ∏ ID, Name (Employee) 1 Raju
Operation:
It takes a more than one value as input and returns a single value as output (result).
Aggregate functions are:
Sum (It returns the sum (addition) of the values of a column.)
Max (It returns the maximum value for a column.)
Min (It returns the minimum value for a column.)
Avg (It returns the average of the values for a column.)
Count (It returns total number of values in a given column.)
Aggregate Functions Example
Student
Example Find out sum of CPI of all students. Output
Rno Name Branch Semester CPI
sum
101 Ramesh CE 3 9 Answer g sum(CPI) (Student)
73
102 Mahesh EC 3 8
103 Suresh ME 4 7 Example Find out maximum & minimum CPI. Output
I. List all pairs of employee names and the project numbers they work on.
II. List out department number, department name and department budget.
III. List all projects that Raj Yadav works on by project name.
IV. List the names of employees who supervise themselves.
Questions asked in TU [Relational Algebra]
5. Consider the following relational database, where the primary keys are underlined.
Give an expression in the relational algebra to express each of the following queries
course (course-id, title, dept_name, credits)
instructor (id, name, dept_name, salary)
section (course-id, sec-id, semester, year, building, room_no, time_slot_id)
teaches (id, course-id, sec-id, semester, year)
I. Find the names of all employees who work for First Bank Corporation.
II. Find the names and cities of residence of all employees who work for First Bank Corporation.
III. Find the names, street address, and cities of residence of all employees who work for First Bank Corporation and
earn more than $10,000 per annum.
IV. Find the names of all employees in this database who do not work for First Bank Corporation.
Assignment
1. Define Super key, Primary key, Candidate key and Alternate key.
2. Explain following Relational Algebra Operation with example.
I. Selection
II. Projection
III. Cross Product
IV. Joins (Inner Join, Outer Joins)
V. Set operators
3. Explain different aggregate functions with example.
Questions of [Relational Algebra]
4. Consider the following relational database, where the primary keys are underlined. Give an expression in the
relational algebra to express each of the following queries
employee (ssn, name, dno, salary, hobby, gender)
department (dno, dname, budget, location, mgrssn)
works_on (ssn, pno)
project (pno, pname, budget, location, goal)
I. List all pairs of employee names and the project numbers they work on.
II. List out department number, department name and department budget.
III. List all projects that Raj Yadav works on by project name.
IV. List the names of employees who supervise themselves.
Questions of [Relational Algebra]
5. Consider the following relational database, where the primary keys are underlined. Give an expression in the
relational algebra to express each of the following queries
course (course-id, title, dept_name, credits)
instructor (id, name, dept_name, salary)
section (course-id, sec-id, semester, year, building, room_no, time_slot_id)
teaches (id, course-id, sec-id, semester, year)
I. Find the names of all employees who work for First Bank Corporation.
II. Find the names and cities of residence of all employees who work for First Bank Corporation.
III. Find the names, street address, and cities of residence of all employees who work for First Bank Corporation and
earn more than $10,000 per annum.
IV. Find the names of all employees in this database who do not work for First Bank Corporation.
teacher (tid, tname, managerid)
Display tid tname and manager name from above table
E_id Ename managerid E_id Ename managerid
1 Raj 1 1 Raj 1
3 Jay 2 2 ram 1
1 Raj 1 3 Jay 2
2 ram 1 3 Jay 2
3 Jay 2 3 Jay 2
∏ ((Employee1)
Eemployee1.eidmployee1.name,employee2.ename
(Employee2))employee1.E_id = emloyee2.E_id and employee1.managerid = employee2.managerid
Thank You