2. Unit – 2: Relational Model 2
Topics to be covered
• Structure of relational databases
• Domains
• Relations
• Relational algebra
• Fundamental operators and syntax
• Relational algebra queries
• Tuple relational calculus
3. Unit – 2: Relational Model 3
Student Table
RollNo Name Branch Semester SPI
101 Raju CE 3 8
102 Mitesh CI 3 7
103 Mayur CE 3 6
104 Nilesh EE 3 9
105 Hitesh CI 3 7
106 Tarun ME 3 8
107 Suresh CE 3 9
Rows or
Tuples or
Records (7)
Columns (5)
Degree = No of columns (5)
Cardinality
=
No
of
tuples
(7)
• Domain is a set of all possible unique values for a specific column.
• Domain of Branch attribute is (CE, CI, ME, EE)
Attributes:
Title of column
4. Unit – 2: Relational Model 4
Structure of relational databases
Table (Relation): A database object that holds a collection of data
for a specific topic. Table consist of rows and columns.
Column (Attribute): The vertical component of a table. A column
has a name and a particular data type; e.g. varchar, decimal,
integer, datetime etc.
Record (Tuple): The horizontal component of a table, consisting of
a sequence of values, one for each column of the table. It is also
known as row.
A database consists of a collection of tables (relations), each
having a unique name.
5. Unit – 2: Relational Model 5
1. Find out following for the given employee table:
i. No of columns
ii. No of records
iii. Different attributes
iv. Degree
v. Cardinality
Exercise
EmpID Name Dept Salary Exp JoinDate
E01 Jatin Sales 30000 8 01-05-05
E02 Meet HR 30000 7 01-03-05
E03 Ram HR 15000 4 01-04-10
E04 Niraj Sales 25000 6 01-03-08
E05 Priya Adm 20000 8 01-11-07
Employee
6. Unit – 2: Relational Model 6
Super Key
A super key is a set of one or more attributes whose values
uniquely identifies each record within a relation (table).
EnrollNo RollNo Branch Sem SPI Name BL
140540107001 101 CE 3 8 Jatin 0
140540107002 102 CE 3 7 Meet 0
140540106001 101 CI 3 8 Ram 0
140540106002 102 CI 3 6 Jatin 1
Super Key
EnrollNo
Super Key
(RollNo, Branch, Sem)
Super Key
(SPI, Name, BL)
7. Unit – 2: Relational Model 7
Candidate Key
A candidate key is a subset of a super key.
A candidate key is a single attribute or the least combination of
attributes that uniquely identifies each record in the table.
A candidate key is a super key for which no proper subset is a
super key.
Every candidate key is a super key but every super key is not a
candidate key.
EnrollNo RollNo Branch Sem SPI Name BL
140540107001 101 CE 3 8 Jatin 0
140540107002 102 CE 3 7 Meet 0
140540106001 101 CI 3 8 Ram 0
140540106002 102 CI 3 6 Jatin 1
Candidate Key
EnrollNo
Candidate Key
(RollNo, Branch, Sem)
8. Unit – 2: Relational Model 8
Primary Key
A primary key is a candidate key that is chosen by database
designer to identify tuples uniquely in a relation (table).
EnrollNo RollNo Branch Sem SPI Name BL
140540107001 101 CE 3 8 Jatin 0
140540107002 102 CE 3 7 Meet 0
140540106001 101 CI 3 8 Ram 0
140540106002 102 CI 3 6 Jatin 1
Candidate Key
EnrollNo
Candidate Key
(RollNo, Branch, Sem)
Primary Key Primary Key
9. Unit – 2: Relational Model 9
Alternate Key
An alternate key is a candidate key that is not chosen by database
designer to identify tuples uniquely in a relation.
EnrollNo RollNo Branch Sem SPI Name BL
140540107001 101 CE 3 8 Jatin 0
140540107002 102 CE 3 7 Meet 0
140540106001 101 CI 3 8 Ram 0
140540106002 102 CI 3 6 Jatin 1
Candidate Key
EnrollNo
Candidate Key
(RollNo, Branch, Sem)
Primary Key Alternate Key
10. Unit – 2: Relational Model 10
Primary Key rules
A primary key may have one or more attributes.
There is only one primary key in the relation (table).
A primary key attribute value cannot be null.
Generally, the value of a primary key attribute does not change.
11. Unit – 2: Relational Model 11
Foreign Key
A foreign key is used to link two relations (tables).
A foreign key is an attribute or collection of attributes in one table
that refers to the primary key in another table.
A table containing the foreign key is called the child table, and the
table containing the primary key is called the parent table.
EnrollNo Name Branch Sem
170540107001 Jatin CE 3
170540107002 Meet CE 3
170540107003 Ram CE 3
ProjectID Title EnrollNo
P01 Bank 170540107001
P02 College 170540107002
P03 School 170540107002
Student Project
Parent
Table
Child
Table
12. Unit – 2: Relational Model 12
Basic Relational Algebra Operations
Selection Display particular rows/records/tuples from a relation
Projection Display particular columns from a relation
Cross Product Multiply each tuples of both relations
Division Divides one relation by another
Rename Rename a column or a table
Joins Combine data or records from two or more tables
1. Natural Join / Inner Join
2. Outer Join
Set Operators Combine the results of two queries into a single result
1. Left Outer Join 2. Right Outer Join 3. Full Outer Join
1. Union 2. Intersection 3. Minus / Set-difference
Operator Description
13. Unit – 2: Relational Model 13
Selection Operator
Symbol: (Sigma)
Notation: condition (Relation)
Operation: Selects tuples from a relation that satisfy a given
condition.
Operators: =, <>, <, >, <=, >=, Λ (AND), V (OR)
14. Unit – 2: Relational Model 14
Selection Operator example [ condition(Relation)]
Display the detail of students belongs to “CE” branch.
RollNo Name Branch SPI
101 Raj CE 8
102 Meet ME 9
103 Harsh EE 8
104 Punit CE 9
RollNo Name Branch SPI
101 Raj CE 8
104 Punit CE 9
Branch=‘CE’ (Student)
Student
Output
15. Unit – 2: Relational Model 15
Selection Operator example [ condition(Relation)]
Display the detail of students belongs to “CE” branch and having
SPI more than 8.
Branch=‘CE’ Λ SPI>8 (Student)
RollNo Name Branch SPI
101 Raj CE 8
102 Meet ME 9
103 Harsh EE 8
104 Punit CE 9
Student
RollNo Name Branch SPI
104 Punit CE 9
Output
16. Unit – 2: Relational Model 16
Selection Operator example [ condition(Relation)]
Display the detail of students belongs to either “EE” or “ME”
branch.
Branch=‘EE’ V Branch=‘ME’ (Student)
RollNo Name Branch SPI
101 Raj CE 8
102 Meet ME 9
103 Harsh EE 8
104 Punit CE 9
Student
RollNo Name Branch SPI
102 Meet ME 9
103 Harsh EE 8
Output
17. Unit – 2: Relational Model 17
Selection Operator example [ condition(Relation)]
Display the detail of students whose SPI between 7 and 9.
SPI>7 Λ SPI<9 (Student)
RollNo Name Branch SPI
101 Raj CE 8
102 Meet ME 9
103 Harsh EE 8
104 Punit CE 9
Student
RollNo Name Branch SPI
101 Raj CE 8
103 Harshan EE 8
Output
18. Unit – 2: Relational Model 18
1. Display the detail of students whose Rollno is less than 104.
2. Display the detail of students having SPI more than 8.
3. Display the detail of students belongs to “CE” Branch having
SPI less than 8.
4. Display the detail of students belongs to either “CE” or “ME”
Branch.
5. Display the detail of students whose SPI between 6 and 9.
Exercise
RollNo Name Branch SPI
101 Raj CE 6
102 Meet ME 8
103 Harsh EE 7
104 Punit CE 9
Student
Write down relational algebra for the following table.
19. Unit – 2: Relational Model 19
1. Display the detail of all employee.
2. Display the detail of employee whose salary more than 10000.
3. Display the detail of employee belongs to “HR” Dept having Salary
more than 20000.
4. Display the detail of employee belongs to either “HR” or “Admin”
Dept.
5. Display the detail of employee whose Salary between 1000 and
25000 and belongs to “HR” Dept .
Exercise
EmpID Name Dept Salary
101 Nilesh Sales 10000
102 Mayur HR 25000
103 Hardik HR 15000
104 Ajay Admin 20000
Employee
Write down relational algebra for the following table.
20. Unit – 2: Relational Model 20
Projection Operator
Symbol: (Pi)
Notation: attribute set (Relation)
Operation: Selects specified attributes of a relation.
It removes duplicate tuples (records) from the result.
21. Unit – 2: Relational Model 21
Projection Operator example [ attribute set (Relation)]
Display rollno, name and branch of all students.
RollNo, Name, Branch (Student)
RollNo Name Branch SPI
101 Raj CE 8
102 Meet ME 9
103 Harsh EE 8
104 Punit CE 9
Student
RollNo Name Branch
101 Raj CE
102 Meet ME
103 Harsh EE
104 Punit CE
Output
22. Unit – 2: Relational Model 22
1. Display rollno, name and SPI of all students.
2. Display name and SPI of all students.
3. Display the name of all students.
4. Display the name of all branches.
Exercise
RollNo Name Branch SPI
101 Raj CE 6
102 Meet ME 8
103 Harsh EE 7
104 Punit CE 9
Student
Write down relational algebra for the following table.
23. Unit – 2: Relational Model 23
Combined Projection & Selection Operation
Example: Display rollno, name & branch of “ME” branch students.
RollNo Name Branch SPI
101 Raj CE 8
102 Meet ME 9
104 Punit CE 9
RollNo Name Branch SPI
102 Meet ME 9
Branch=‘ME’ (Student)
RollNo Name Branch
102 Meet ME
RollNo, Name, Branch ( Branch=‘ME’ (Student) )
Output-1
Student
Output-2
24. Unit – 2: Relational Model 24
Combined Projection & Selection Operation
Example: Display name, branch and SPI of students whose SPI is
more than 8.
RollNo Name Branch SPI
101 Raj CE 8
102 Meet ME 9
104 Punit CE 9
Name Branch SPI
Meet ME 9
Punit CE 9
Name, Branch, SPI
Student
Output
( SPI>8 (Student) )
25. Unit – 2: Relational Model 25
Combined Projection & Selection Operation
Example: Display name, branch and SPI of students who belongs
to “CE” Branch and SPI is more than 8.
RollNo Name Branch SPI
101 Raj CE 8
102 Meet ME 9
104 Punit CE 9
Name Branch SPI
Punit CE 9
Name, Branch, SPI
Student
Output
( Branch=‘CE’ Λ SPI>8 (Student) )
26. Unit – 2: Relational Model 26
Combined Projection & Selection Operation
Example: Display name of students along with their branch who
belong to either “ME” Branch or “EE” Branch.
RollNo Name Branch SPI
101 Raj CE 8
102 Meet ME 9
104 Punit CE 9
103 Jatin EE 7
Name Branch
Meet ME
Jatin EE
Name, Branch
Student
Output
( Branch=‘ME’ V Branch=‘EE’ (Student) )
27. Unit – 2: Relational Model 27
1. Display rollno, name and SPI of all students belongs to “CE” branch.
2. List the name of students with their branch whose SPI is more than
8 and belongs to “CE” branch.
3. List the name of students along with their branch and SPI who
belongs to either “CE” or “ME” branch and having SPI more than 8.
4. Display the name of students with their branch name whose SPI
between 7 and 9.
Exercise
RollNo Name Branch SPI
101 Raj CE 6
102 Meet ME 8
103 Harsh EE 7
104 Punit CE 9
Write down relational algebra for the following table.
Student
28. Unit – 2: Relational Model 28
1. Display the name of faculties belong to “CE” branch and having
salary more than 25000.
2. Display the name of all “CE” and “ME” branch’s faulty.
3. List the name of faculty with their salary who belongs to “CE” or
“ME” branch having salary more than 35000.
4. Display the name of faculty along with their branch name whose
salary between 25000 and 50000.
Exercise
FacultyID Name Branch Salary
101 B. M. Patel CE 25000
102 A. M. Shah ME 35000
103 J. B. Katara EE 18000
104 H. N. Shukla CE 50000
105 V. K. Vyas ME 45000
Write down relational algebra for the following table.
Faculty
29. Unit – 2: Relational Model 29
Cartesian Product / Cross Product
Symbol: ×(Cross)
Notation: Relation-1 (R1) × Relation-2 (R2)
Operation: It will multiply each tuples of Relation-1 to each tuples
of Relation-2.
Attributes of Resultant Relation = Attributes of R1 + Attributes of R2
Tuples of Resultant Relation = Tuples of R1 *Tuples of R2
30. Unit – 2: Relational Model 30
Cartesian Product / Cross Product
Example:
RollNo Name Branch
101 Raj CE
102 Meet ME
RollNo SPI
101 8
103 9
Student.RollNo Name Branch Result.RollNo SPI
101 Raj CE 101 8
101 Raj CE 103 9
102 Meet ME 101 8
102 Meet ME 103 9
If both relations have some attribute with the same name, it can
be distinguished by combing relation-name.attribute-name.
Student Result
Student× Result
31. Unit – 2: Relational Model 31
Natural Join (Inner Join)
Symbol:
Notation: Relation-1 (R1) Relation-2 (R2)
Operation: Natural join will retrieve consistent data from multiple
relations.
It combines records from different relations that satisfy a given
condition.
Step – 1 It performs Cartesian Product
Step – 2 Then it deletes inconsistent tuples
Step – 3 Then it removes an attribute from
duplicate attributes
Steps
32. Unit – 2: Relational Model 32
Natural Join (Inner Join) example
RollNo Name Branch
101 Raj CE
102 Meet ME
RollNo SPI
101 8
103 9
Student.RollNo Name Branch Result.RollNo SPI
101 Raj CE 101 8
101 Raj CE 103 9
102 Meet ME 101 8
102 Meet ME 103 9
Student Result
Student× Result
Student.RollNo Name Branch Result.RollNo SPI
101 Raj CE 101 8
Student Result
RollNo Name Branch SPI
101 Raj CE 8
Student Result
Step 1 : Performs Cartesian Product
Step 2 : Removes inconsistent tuples
Step 3 : Removes an attribute
33. Unit – 2: Relational Model 33
Natural Join (Inner Join) example
BID BName Head
1 CE Patel
2 ME Shah
FID FName BID
1 Raj 1
2 Meet 2
Branch.BID BName Head FID FName Faculty.BID
1 CE Patel 1 Raj 1
1 CE Patel 2 Meet 2
2 ME Shah 1 Raj 1
2 ME Shah 2 Meet 2
Branch
Faculty
B𝐫𝐚𝐧𝐜𝐡 × Faculty
Branch.BID BName Head FID FName Faculty.BID
1 CE Patel 1 Raj 1
2 ME Shah 2 Meet 2
B𝐫𝐚𝐧𝐜𝐡 Faculty
BID BName Head FID FName
1 CE Patel 1 Raj
2 ME Shah 2 Meet
B𝐫𝐚𝐧𝐜𝐡 Faculty
Step 1 : Performs Cartesian Product
Step 2 : Removes inconsistent tuples
Step 3 : Removes an attribute
To perform a natural join there must
be one common attribute (column)
between two relations.
34. Unit – 2: Relational Model 34
Write down relational algebras for the following tables
• Relations
• Student (Rno, Sname, Address, City, Mobile)
• Department (Did, Dname)
• Academic (Rno, Did, SPI, CPI, Backlog)
• Guide (Rno, PName, Fid)
• Faculty (Fid, Fname, Subject, Did, Salary)
• List the name of students with their department name and SPI of
all student belong to “CE” department.
Sname, Dname, SPI ( Dname=‘CE’ (Student (Department Academic)))
35. Unit – 2: Relational Model 35
Write down relational algebras for the following tables
• Relations
• Student (Rno, Sname, Address, City, Mobile)
• Department (Did, Dname)
• Academic (Rno, Did, SPI, CPI, Backlog)
• Guide (Rno, PName, Fid)
• Faculty (Fid, Fname, Subject, Did, Salary)
• Display the name of students with their project name whose
guide is “A. J. Shah”.
Sname, Pname ( Fname=‘A.J.Shah’ (Student (Guide Faculty)))
36. Unit – 2: Relational Model 36
1. Write down relational algebras for the following tables:
Student (Rno, Sname, Address, City, Mobile)
Department (Did, Dname)
Academic (Rno, Did, SPI, CPI, Backlog)
Guide (Rno, ProjectName, Fid)
Faculty (Fid, Fname, Subject, Did, Salary)
i. List the name of students with their department name having
backlog 0.
ii. List the name of faculties with their department name and
salary having salary more than 25000 and belongs to “CE”
department.
iii. List the name of all faculties of “CE” and “ME” department
whose salary is more than 50000.
Exercise
37. Unit – 2: Relational Model 37
1. Write down relational algebras for the following tables:
Student (Rno, Sname, Address, City, Mobile)
Department (Did, Dname)
Academic (Rno, Did, SPI, CPI, Backlog)
Guide (Rno, ProjectName, Fid)
Faculty (Fid, Fname, Subject, Did, Salary)
iv. Display the name of students with their project name of all
“CE” department’s students whose guide is “Z.Z. Patel”.
v. Display the name of faculties with their department name who
belongs to “CE” department and tough “CPU” subject having
salary more than 25000.
vi. List the name of students with their department name doing
project “Hackathon” under guide “A. V. Pujara”.
Exercise
38. Unit – 2: Relational Model 38
Outer Join
Operation: In natural join some records are missing, if we want
that missing records than we have to use outer join.
Types: Three types of Outer Join
1. Left Outer Join
2. Right Outer Join
3. Full Outer Join
39. Unit – 2: Relational Model 39
Left Outer Join
Display all the tuples of the left relation even through there is no
matching tuple in the right relation.
For such kind of tuples having no matching, the attributes of right
relation will be padded with null in resultant relation.
Example:
RollNo Name Branch
101 Raj CE
102 Meet ME
RollNo SPI
101 8
103 9
RollNo Name Branch SPI
101 Raj CE 8
102 Meet ME null
Student Result
Student Result
40. Unit – 2: Relational Model 40
Left Outer Join example
Display Name, Salary and Balance of the of all employees.
ID Name Balance
1 Raj 5000
2 Meet 8000
Name Salary Balance
Meet 15000 8000
Jay 20000 null
Customer
Output
ID Name Salary
2 Meet 15000
3 Jay 20000
Employee
Name, Salary, Balance (Employee Customer)
41. Unit – 2: Relational Model 41
Right Outer Join
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:
RollNo Name Branch
101 Raj CE
102 Meet ME
RollNo SPI
101 8
103 9
RollNo Name Branch SPI
101 Raj CE 8
103 null null 9
Student Result
Student Result
42. Unit – 2: Relational Model 42
Right Outer Join example
Display Name, Salary and Balance of the of all customers.
ID Name Balance
1 Raj 5000
2 Meet 8000
Name Salary Balance
Raj null 5000
Meet 15000 8000
Customer
Output
ID Name Salary
2 Meet 15000
3 Jay 20000
Employee
Name, Salary, Balance (Employee Customer)
43. Unit – 2: Relational Model 43
Full Outer Join
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:
RollNo Name Branch
101 Raj CE
102 Meet ME
RollNo SPI
101 8
103 9
RollNo Name Branch SPI
101 Raj CE 8
102 Meet ME null
103 null null 9
Student Result
Student Result
44. Unit – 2: Relational Model 44
Full Outer Join example
Display Name, Salary and Balance of the of all employee as well
as customers.
ID Name Balance
1 Raj 5000
2 Meet 8000
Name Salary Balance
Meet 15000 8000
Jay 20000 null
Raj null 5000
Customer
Output
ID Name Salary
2 Meet 15000
3 Jay 20000
Employee
Name, Salary, Balance (Employee Customer)
45. Unit – 2: Relational Model 45
Set Operators
Set operators combine the results of two or more queries into a
single result.
Types of set operators
1. Union
2. Intersect (Intersection)
3. Minus (Set Difference)
46. Unit – 2: Relational Model 46
Union Operator
Symbol: U
Notation: Relation1 U Relation2
Operation: Combine the records from two or more queries in to a
single result.
Name
Raj
Suresh
Meet
Name
Meet
Suresh
Manoj
Name
Manoj
Meet
Raj
Suresh
Customer Employee Customer U Employee
Union removes duplicate records.
47. Unit – 2: Relational Model 47
Union example
Display Name of person who are either employee or customer.
ID Name Balance
1 Raj 5000
2 Meet 8000
Name
Meet
Jay
Raj
Customer
Output
ID Name Salary
2 Meet 15000
3 Jay 20000
Employee
Name (Employee) Name (Customer)
U
48. Unit – 2: Relational Model 48
Intersect Operator
Symbol: ∩
Notation: Relation1 ∩ Relation2
Operation: Returns the records which are common from both
queries.
Name
Raj
Suresh
Meet
Name
Meet
Suresh
Manoj
Name
Meet
Suresh
Customer Employee Customer ∩ Employee
49. Unit – 2: Relational Model 49
Intersect example
Display Name of person who are employee as well as customer.
ID Name Balance
1 Raj 5000
2 Meet 8000
Name
Meet
Customer
Output
ID Name Salary
2 Meet 15000
3 Jay 20000
Employee
Name (Employee) Name (Customer)
∩
50. Unit – 2: Relational Model 50
Minus (Set Difference) Operator
Symbol: −
Notation: Relation1 − Relation2
Operation: Returns all the records from first (left) query that are
not contained in the second (right) query.
Name
Raj
Suresh
Meet
Name
Meet
Suresh
Manoj
Name
Raj
Customer Employee Customer − Employee
Name
Manoj
Employee − Customer
51. Unit – 2: Relational Model 51
Minus example
Display Name of person who are employee but not customer.
ID Name Balance
1 Raj 5000
2 Meet 8000
Name
Jay
Customer
Output
ID Name Salary
2 Meet 15000
3 Jay 20000
Employee
Name (Employee) Name (Customer)
-
52. Unit – 2: Relational Model 52
Minus example
Display Name of person who are customer but not employee.
ID Name Balance
1 Raj 5000
2 Meet 8000
Name
Raj
Customer
Output
ID Name Salary
2 Meet 15000
3 Jay 20000
Employee
Name (Customer) Name (Employee)
-
53. Unit – 2: Relational Model 53
Conditions to perform Set Operators
Set operators will take two or more queries as input, which must
be union - compatible :
RollNo Name Branch SPI
101 Raj CE 8
102 Meet CE 7
103 Neel ME 9
EmpNo Name Branch
101 Patel CE
102 Shah CE
103 Ghosh EE
RollNo Name Branch SPI
101 Raj CE 8
102 Meet CE 7
103 Neel ME 9
EmpNo Name Branch Exp
101 Raj CE 8
102 Meet CE 1
103 Ghosh EE 9
1. Both queries should have same (equal) number of columns
54. Unit – 2: Relational Model 54
Conditions to perform Set Operators
Set operators will take two or more queries as input, which must
be union - compatible:
RollNo Name Branch SPI
101 Raj CE 8
102 Meet CE 7
103 Neel ME 9
EmpNo Name Branch Exp
101 Raj CE 8
102 Meet CE 1
103 Ghosh EE 9
RollNo Name Branch SPI
101 Raj CE 8
102 Meet CE 7
103 Neel ME 9
EmpNo Name Branch Subject
101 Raj CE DBMS
102 Meet CE DS
103 Ghosh EE EEM
2. Corresponding attributes should have the same data type
55. Unit – 2: Relational Model 55
1. Check whether following tables are compatible or not:
• A: (First_name(char), Last_name(char), Date_of_Birth(date))
• B: (FName(char), LName(char), PhoneNumber(number))
(Not compatible) Both tables have 3 attributes but third attributes
datatype is different.
• A: (First_name(char), Last_name(char), Date_of_Birth(date))
• B: (FName(char), LName(char), DOB(date))
(Compatible) Both tables have 3 attributes and of same data type.
Exercise
56. Unit – 2: Relational Model 56
Set Operators
Person (PersonID, Name, Address, Hobby)
Professor (ProfessorID, Name, Office, Salary)
• are not union compatible.
But
Name (Person) & Name (Professor)
• are union compatible.
57. Unit – 2: Relational Model 57
Division Operator
Symbol: ÷
Notation: Relation1 ÷ Relation2
Condition to perform operation:
• Attributes of relation2 is proper subset of attributes of relation1.
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.
58. Unit – 2: Relational Model 58
Division Operator example
Name Subject
Raj DBMS
Raj DS
Meet DBMS
Meet DS
Meet DE
Suresh DBMS
Suresh DS
Suresh DE
Rohit DS
Rohit DE
Subject
DBMS
DS
DE
Name
Meet
Suresh
Student Subject Student ÷ Subject
59. Unit – 2: Relational Model 59
Division Operator example
Sno Pno
S1 P1
S1 P2
S1 P3
S1 P4
S2 P1
S2 P2
S3 P2
S4 P2
S4 P4
S5 P4
Pno
P2
Pno
P2
P4
Pno
P1
P2
P4
Sno
S1
S2
S3
S4
Sno
S1
S4
Sno
S1
A B1 B2 B3
A ÷ B1 A ÷ B2 A ÷ B3
60. Unit – 2: Relational Model 60
List the name of students doing a project in all technologies
Rno Name Technology
101 Raj .NET
101 Raj iPhone
102 Meet .NET
102 Meet PHP
102 Meet iPhone
102 Meet Android
103 Vijay PHP
104 Jay .NET
104 Jay PHP
104 Jay iPhone
104 Jay Android
TID Technology
1 .NET
2 PHP
3 iPhone
4 Android
Name, Technology (Student) ÷ Technology(Project)
Name
Meet
Jay
Student Project
Output
61. Unit – 2: Relational Model 61
Rename Operator
Symbol: ρ (Rho)
Notation: ρ A (X1,X2….Xn) (Relation)
Operation: The rename operator returns an existing relation
under a new name.
How to use:
• ρ x (E)
• Returns a relation E under a new name X.
• ρ A1, A2. …,An (E)
• Returns a relation E with the attributes renamed to A1, A2, …., An.
• ρ x(A1, A2. …,An) (E)
• Returns a relation E under a new name X with the attributes renamed to A1, A2,
…., An.
62. Unit – 2: Relational Model 62
Rename Operator example (Relation)
Rno Name CPI
101 Raj 8
102 Meet 9
103 Suresh 7
ρPerson (Student)
Student
Rno Name CPI
101 Raj 8
102 Meet 9
103 Suresh 7
Person
70. Unit – 2: Relational Model 70
Rename Operator example
A.CPI
8
7
∏A.CPI ( A.CPI<B.CPI (ρA (Student) X ρB (Student)))
71. Unit – 2: Relational Model 71
Rename Operator example
CPI
8
9
7
— =
CPI
9
∏CPI (Student) ─ ∏A.CPI ( A.CPI<B.CPI (ρA (Student) X ρB (Student)))
A.CPI
8
7
=
72. Unit – 2: Relational Model 72
Aggregate Functions
Symbol: g or G
Notation: g function-name (column) (Relation)
Operation: It takes a more than one value as input and returns a
single value as output (result).
• Aggregate functions are:
1. Sum (It returns the sum (addition) of the values of a column.)
2. Max (It returns the maximum value for a column.)
3. Min (It returns the minimum value for a column.)
4. Avg (It returns the average of the values for a column.)
5. Count (It returns total number of values in a given column.)
73. Unit – 2: Relational Model 73
Aggregate Functions example
Example: Find out sum of CPI of all students.
• G sum(CPI) (Student)
Rno Name Branch Semester CPI
101 Ramesh CE 3 9
102 Mahesh EC 3 8
103 Suresh ME 4 7
104 Amit EE 4 8
105 Anita CE 4 8
106 Reeta ME 3 7
107 Rohit EE 4 9
108 Chetan CE 3 8
109 Rakesh CE 4 9
sum
73
Student
74. Unit – 2: Relational Model 74
Aggregate Functions example
Example: Find out maximum & minimum CPI.
• G max(CPI), min(CPI) (Student)
Rno Name Branch Semester CPI
101 Ramesh CE 3 9
102 Mahesh EC 3 8
103 Suresh ME 4 7
104 Amit EE 4 8
105 Anita CE 4 8
106 Reeta ME 3 7
107 Rohit EE 4 9
108 Chetan CE 3 8
109 Rakesh CE 4 9
max min
9 7
Student
75. Unit – 2: Relational Model 75
Aggregate Functions example
Example: Count the number of students.
• G count(Rno) (Student)
Rno Name Branch Semester CPI
101 Ramesh CE 3 9
102 Mahesh EC 3 8
103 Suresh ME 4 7
104 Amit EE 4 8
105 Anita CE 4 8
106 Reeta ME 3 7
107 Rohit EE 4 9
108 Chetan CE 3 8
109 Rakesh CE 4 9
count
9
Student
76. Unit – 2: Relational Model 76
1. Write down relational algebras for the following table:
Employee (person-name, street, city)
Works (person-name, company-name, salary)
Company (company-name, city)
Managers (person-name, manager-name)
i. Find the names of all employees who work for “TCS”.
ii. Find the names and cities of residence of all employees who
work for “Infosys”.
iii. Find the names, street and city of residence of all employees
who work for “ITC” and earn more than $10,000 per annum.
iv. Find the names of all employees in this database who live in
the same city as the company for which they work.
Exercise
77. Unit – 2: Relational Model 77
1. Write down relational algebras for the following table:
Employee (person-name, street, city)
Works (person-name, company-name, salary)
Company (company-name, city)
Managers (person-name, manager-name)
v. Find the names of all employees working in “TCS” who earn
more than 25000 and less than 40000.
vi. Find the name of employee whose manager is “Ajay Patel” and
salary is more than 50000.
vii. Display the name of employee with street, city, company
name, salary and manager name staying in “Rajkot” and
working in “Ahmedabad”.
viii. Find maximum, minimum and average salary of all employee.
ix. Find out the total number of employee.
Exercise
78. Unit – 2: Relational Model 78
Questions
1. Define Super key, Primary key, Candidate key and Alternate key.
2. Explain following Relational Algebra Operation with example.
1. Selection
2. Projection
3. Cross Product
4. Joins (Inner Join, Outer Joins)
5. Rename
6. Division
3. Explain different aggregate functions with example.
79. Unit – 2: Relational Model 79
Questions
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)
1. List all pairs of employee names and the project numbers they work on.
2. List out department number, department name and department budget.
3. List all projects that Raj Yadav works on by project name.
4. List the names of employees who supervise themselves.
80. Unit – 2: Relational Model 80
Questions
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)
1. Find the name of all instructors in the physics department.
2. Find all the courses taught in the fall 2009 semester but not in Spring
semester.
3. Find the names of all instructors in the Comp. Sci. department together
with the course titles of all the courses that the instructors teach.
4. Find the average salary in each department.