Unit 1 Lecture 6,7,8
Unit 1 Lecture 6,7,8
Introduction to
relational database
& relational algebra
✓ Outline
Looping
• Structure of Relational Databases
• Key
• Relational Algebra
• Fundamental Operators and Syntax
▪ Selection
▪ Projection
▪ Cross Product OR Cartesian Product
▪ Joins
▪ Set Operators
▪ Division
▪ Rename
▪ Aggregate Functions
• Open Source and Commercial DBMS
Structure of Relational Databases
Section - 1
Structure of Relational Databases
Columns (5) Table (Relation): A database object that holds a
collection of data for a specific topic.
Stude Table consist of rows and columns.
Attributes: nt
RollN
Title of column Name Branch Semester SPI
o Column (Attribute): The vertical component of a
101 Raju CE 3 8 table. A column has a name and a particular data
4
Key
Section - 2
Super Key
🞂 A super key is a set of one or more attributes whose values uniquely identifies each record
within a relation (table).
Primary Key
10
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.
Parent Child
Table Table
Stude Projec
nt tProjectI
EnrollNo Name Branch Sem
Title EnrollNo
D
19054010700 Raju CE 3
1 101 Bank 19054010700
1
19054010700 Mites CE 3
2 h 102 College 19054010700
2
19054010700 Nilesh CE 3
3 103 School 19054010700
3 11
Relational Algebra Operations
Section - 3
Relational Algebra Operations
Operator Description
Selection Display particular rows/records/tuples from a relation
Projection Display particular columns from a relation
Cross Product Multiply each tuples of both relations
Combine data or records from two or more tables
1. Natural Join / Inner Join
Joins
2. Outer Join
1. Left Outer Join 2. Right Outer Join 3. Full Outer Join
Combine the results of two queries into a single result.
Set Operators
1. Union 2. Intersection 3. Minus / Set-difference
Division Divides one relation by another
Rename Rename a column or a table
13
Relational Algebra Operations
Selection Operator
Section - 3.1
Selection Operator
🞂 Symbol: σ (Sigma)
🞂 Notation: σ
condition (Relation)
🞂 Operation: Selects tuples from a relation that satisfy a given condition.
🞂 Operators: =, <>, <, >, <=, >=, Λ (AND), V (OR)
Exampl Display the detail of students belongs to “CE” Branch. Answ σBranch=‘CE’
e er (Student)
Stude Outpu
nt
RollN tRollN
Name Branch SPI Name Branch SPI
o o
101 Raju CE 8 101 Raju CE 8
102 Mites ME 9 104 Meet CE 9
h
103 Nilesh CI 9
104 Meet CE 9
15
Selection Operator [σ condition (Relation)]
Exampl Display the detail of students belongs to “CE” Branch and having SPI more than 8.
e
Stude
nt
RollN
Name Branch SPI
o
101 Raju CE 8
102 Mites ME 9
h
103 Nilesh CI 9
104 Meet CE 9
Answ σBranch=‘CE’ Λ SPI>8
er (Student)
Outpu
tRollN
Name Branch SPI
o
104 Meet CE 9
16
Selection Operator [σ condition (Relation)]
Exampl Display the detail of students belongs to either “CI” or “ME” Branch.
e
Stude
nt
RollN
Name Branch SPI
o
101 Raju CE 8
102 Mites ME 9
h
103 Nilesh CI 9
104 Meet CE 9
Answ σBranch=‘CI’ V Branch=‘ME’
er (Student)
Outpu
tRollN
Name Branch SPI
o
102 Mites ME 9
h
103 Nilesh CI 9
17
Selection Operator [σ condition (Relation)]
Exampl Display the detail of students whose SPI between 7 and 9.
e
Stude
nt
RollN
Name Branch SPI
o
101 Raju CE 8
102 Mites ME 9
h
103 Nilesh CI 9
104 Meet CE 9
Answ σSPI>7 Λ SPI<9
er (Student)
Outpu
tRollN
Name Branch SPI
o
101 Raju CE 8
18
Exercise
🞂 Write down the relational algebra for the student table. Stude
nt
RollN
⮩ Display the detail of students whose RollNo is less than 104. Name Branch SPI
o
⮩ Display the detail of students having SPI more than 8.
101 Raj CE 6
⮩ Display the detail of students belongs to “CE” Branch having SPI less
than 8. 102 Meet ME 8
⮩ Display the detail of students belongs to either “CE” or “ME” Branch. 103 Harsh EE 7
⮩ Display the detail of students whose SPI between 6 and 9.
104 Punit CE 9
Employ
🞂 Write down the relational algebra for the employee table. ee
⮩ Display the detail of all employee. EmpI
Name Dept Salary
D
⮩ Display the detail of employee whose Salary more than 10000.
101 Nilesh Sales 1000
⮩ Display the detail of employee belongs to “HR” Dept having Salary more 0
than 20000.
102 Mayur HR 2500
⮩ Display the detail of employee belongs to either “HR” or “Admin” Dept. 0
⮩ Display the detail of employee whose Salary between 10000 and 25000 103 Hardik HR 1500
and belongs to “HR” Dept. 0
104 Ajay Admi 2000
n 0
19
Relational Algebra Operations
Projection Operator
Section - 3.2
Projection Operator
🞂 Symbol: ∏ (Pi)
🞂 Notation: ∏
attribute set (Relation)
🞂 Operation: Selects specified attributes of a relation.
🞂 It removes duplicate tuples (records) from the result.
Exampl Display RollNo, Name and Branch of all students. Answ ∏ RollNo, Name, Branch
e er (Student)
Stude Outpu
nt
RollN tRollN Branc
Name Branch SPI Name
o o h
101 Raju CE 8 101 Raju CE
102 Mites ME 9 102 Mites ME
h h
103 Nilesh CI 9 103 Nilesh CI
104 Meet CE 9 104 Meet CE
Unit 1 – Database System Architecture 21
Exercise
🞂 Write down the relational algebra for the student table. Stude
nt
RollN
⮩ Display RollNo, Name and SPI of all students. Name Branch SPI
o
⮩ Display Name and SPI of all students.
101 Raj CE 6
⮩ Display the Name of all students.
⮩ Display the Name of all branches. 102 Meet ME 8
103 Hars EE 7
h
104 Punit CE 9
Employ
🞂 Write down the relational algebra for the employee table. ee
⮩ EmpI
Display EmpID with Name of all employee. Name Dept Salary
D
⮩ Display Name and Salary of all employee.
101 Nilesh Sales 1000
⮩ Display the Name of all employee. 0
⮩ Display the Name of all departments. 102 Mayur HR 2500
0
103 Hardi HR 1500
k 0
104 Ajay Admi 2000
n 0
22
Combined Projection & Selection Operation
Exampl Display RollNo, Name & Branch of “ME” Branch students.
e
Stude
nt
RollN
Name Branch SPI
o
101 Raju CE 8
102 Mites ME 9
h
103 Nilesh CI 9
104 Meet CE 7
Step-1 σBranch=‘ME’ Answ ∏ RollNo, Name, Branch (σBranch=‘ME’
(Student) er (Student))
Output- Output-
1
RollN 2
RollN Branc
Name Branch SPI Name
o o h
102 Mites ME 9 102 Mites ME
h h
Unit 1 – Database System Architecture 23
Combined Projection & Selection Operation
Exampl Display Name, Branch and SPI of students whose SPI is more than 8.
e
Stude
nt
RollN
Name Branch SPI
o
101 Raju CE 8
102 Mites ME 9
h
103 Nilesh CI 9
104 Meet CE 7
Step-1 σSPI>8 Answ ∏ Name, Branch, SPI (σSPI>8
(Student) er (Student))
Output- Output-
1
RollN 2 SP
Name Branch SPI Name Branch
o I
102 Mites ME 9 Mites ME 9
h h
103 Nilesh CI 9 Nilesh CI 9
Unit 1 – Database System Architecture 24
Combined Projection & Selection Operation
Exampl Display Name, Branch and SPI of students who belongs to “CE” Branch and SPI is more than 7.
e
Stude
nt
RollN
Name Branch SPI
o
101 Raju CE 8
102 Mites ME 9
h
103 Nilesh CI 9
104 Meet CE 7
Step-1 σBranch=‘CE’ Λ SPI>7 Answ ∏ Name, Branch, SPI (σBranch=‘CE’ Λ SPI>7
(Student) er (Student))
Output- Output-
1
RollN 2 SP
Name Branch SPI Name Branch
o I
101 Raju CE 8 Raju CE 8
25
Combined Projection & Selection Operation
Exampl Display Name of students along with their Branch who belong to either “ME” Branch or “CI” Branch.
e
Stude
nt
RollN
Name Branch SPI
o
101 Raju CE 8
102 Mites ME 9
h
103 Nilesh CI 9
104 Meet CE 7
Step-1 σBranch=‘ME’ V Branch=‘CI’ Answ ∏ Name, Branch (σBranch=‘ME’ V Branch=‘CI’
(Student) er (Student))
Output- Output-
1
RollN 2 Branc
Name Branch SPI Name
o h
102 Mites ME 9 Mites ME
h h
103 Nilesh CI 9 Nilesh CI
26
Exercise
🞂 Write down the relational algebra for the student table. Stude
nt
RollN
⮩ Display Rollno, Name and SPI of all students belongs to “CE” Branch. Name Branch SPI
o
⮩ List the Name of students with their Branch whose SPI is more than 8
and belongs to “CE” Branch. 101 Raj CE 6
⮩ List the Name of students along with their Branch and SPI who belongs 102 Meet ME 8
to either “CE” or “ME” Branch and having SPI more than 8. 103 Hars EE 7
⮩ Display the Name of students with their Branch name whose SPI h
between 7 and 9. 104 Punit CE 9
🞂 Write down the relational algebra for the employee table. Employ
ee
EmpI
⮩ Display the Name of employee belong to “HR” Dept and having salary Name Dept Salary
more than 20000. D
⮩ Display the Name of all “Admin” and “HR” Dept’s employee. 101 Nilesh Sales 1000
0
⮩ List the Name of employee with their Salary who belongs to “HR” or
“Admin” Dept having salary more than 15000. 102 Mayur HR 2500
0
⮩ Display the Name of employee along with their Dept name whose
salary between 15000 and 30000. 103 Hardi HR 1500
k 0
104 Ajay Admi 2000
n 0
27
Relational Algebra Operations
Cartesian Product / Cross Product
Section - 3.3
Cartesian Product / Cross Product
🞂 Symbol: X (Cross)
🞂 Notation: Relation-1 (R1) X Relation-2 (R2) OR Algebra-1 X Algebra-2
🞂 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
Exampl Perform Cross Product between Student and Result. Answ (Student) X
e er (Result)
35
Write down relational algebra for the following tables/relations
🞂 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)
Exampl List the name of students with their department name and SPI of all student belong to “CE” department.
e
Answ ∏ Sname, Dname, (σDname=‘CE’ (Student (Department
er SPI Academic)))
Exampl Display the name of students with their project name whose guide is “A. J. Shah”.
e
Answ ∏ Sname, (σFname=‘A.J.Shah’ (Student (Guide
er Pname Faculty)))
36
Exercise: Write down relational algebra for the following tables/relations
🞂 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 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 “Z.Z.
Patel”.
🞂 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 “I. I. Shah”.
Unit 1 – Database System Architecture 37
Relational Algebra Operations
Outer Join
Section - 3.5
Outer Join
🞂 In natural join some records are missing, if we want that missing records than we have to use
outer join.
Three types of Outer
Join
Sr. Outer Join Symbol
1 Left Outer Join
2 Right Outer Join
3 Full Outer Join
39
Left Outer Join
🞂 Symbol:
🞂 Notation: Relation-1 (R1) Relation-2 (R2) OR Algebra-1 Algebra-2
🞂 Operation:
⮩ 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.
Exampl Perform Left Outer Join between Student and Result. Answ (Student)
e er (Result)
Stude Resul Outpu
nt
RollN Branc tRollN tRollN
Name SPI Name Branch SPI
o h o o
101 Raj CE 101 8 101 Raj CE 8
102 Meet ME 103 9 102 Meet ME NUL
L
Exercis What is the output of (Result)
e (Student).
Unit 1 – Database System Architecture 40
Left Outer Join Example
Exampl Perform Left Outer Join between Student and Result. (Display RollNo, Name and SPI)
e
Stude Resul
nt
RollN Branc tRollN
Name SPI BL
o h o
101 Raj CE 101 8 1
102 Meet ME 103 9 0
Outpu
tRollN
Name SPI
o
101 Raj 8
103 NULL 9
43
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.
Exampl Perform Full Outer Join between Student and Result. Answ (Student)
e er (Result)
Stude Resul Outpu
nt
RollN Branc tRollN tRollN
Name SPI Name Branch SPI
o h o o
101 Raj CE 101 8 101 Raj CE 8
102 Meet ME 103 9 102 Meet ME NUL
L
Exercis What is the output of (Result) 103 NULL NULL 9
e (Student).
Unit 1 – Database System Architecture 44
Full Outer Join Example
Exampl Perform Full Outer Join between Student and Result. (Display RollNo, Name and SPI)
e
Stude Resul
nt
RollN Branc tRollN
Name SPI BL
o h o
101 Raj CE 101 8 1
102 Meet ME 103 9 0
Conditio Set operators will take two or more queries as input, which must be union-compatible.
ns
• Both queries should have same (equal) number of columns
• Corresponding attributes should have the same data type or domain
47
Conditions to perform Set Operators
Conditions Both queries should have same (equal) number of columns.
-1
Stude Facult Stude Facult
nt
RN y Dep nt
RN Dep y Dep
Name Dept SPI FId Name Name FId Name
o t o t t
101 Raj CE 8 101 Patel CE 101 Raj CE 101 Patel CE
102 Meet ME 9 102 Shah ME 102 Meet ME 102 Shah ME
103 Jay CE 9 103 Dave ME 103 Jay CE 103 Dave ME
Exercis Is there any difference in the output if we swap the tables in Intersection. (Employee) ∩
e (Customer). Unit 1 – Database System Architecture 51
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).
Exampl Perform Set difference between Customer and Employee. Answ (Customer) −
e er (Employee)
Custom Employ Outpu
er ee t
Name Name Name
Raju Meet Raju
Suresh Suresh
Meet Manoj
Exercis Is there any difference in the output if we swap the tables in Set difference. (Employee) −
e (Customer). Unit 1 – Database System Architecture 52
Union Operators Example
53
Intersect/ Intersection Operators Example
54
Minus/ Set difference Operators Example
Exampl Display Name of person who are employee but not customer.
e
Custom Employ
er Balanc ee Salar
ID Name ID Name Dept
e y
1 Raju 10000 2 Sures CE 8000
h
2 Sures 20000
h 3 Manoj ME 9000
55
Minus/ Set difference Operators Example
Exampl Display Name of person who are customer but not employee.
e
Custom Employ
er Balanc ee Salar
ID Name ID Name Dept
e y
1 Raju 10000 2 Sures CE 8000
h
2 Sures 20000
h 3 Manoj ME 9000
56
Set Operators [Exercise]
Exercis What is the output of following relational algebra for the below mentioned tables:
e
Custom Employ
er Balanc ee Salar
ID Name ID Name Dept
e y
1 Raju 10000 2 Sures CE 8000
h
2 Sures 20000
h 3 Manoj ME 9000
57
Set Operators [Exercise]
Exercis What is the output of following relational algebra for the below mentioned tables:
e
Custom Employ
er Balanc ee Salar
ID Name ID Name Dept
e y
1 Raju 10000 2 Sures CE 8000
h
2 Sures 20000
h 3 Manoj ME 9000
58
Relational Algebra Operations
Division Operator
Section - 3.7
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.
A B B B B
Sn 1 2 3 4
PNo PNo PNo PNo PNo
o
P2 P2 P1 P2
S1 P1
P4 P2 P5
S1 P2
P4
S1 P3
S1 P4 Algebr (A) ÷ Algebr (A) ÷ Algebr (A) ÷ Algebr (A) ÷
a (B1) a (B2) a (B3) a (B4)
S2 P1 Outpu Outpu Outpu Outpu
S2 P2 t t t t
SNo SNo SNo SNo
S3 P2 S1 S1 S1
S4 P2 S2 S4
S4 P4 S3
S5 P4 S4
62
Division Operator Example
Exampl List the name of students doing a project in all technologies.
e
Stude Projec
nt t
RN Technolo Technolo
Name TID
o gy gy
101 Raj .NET 1 .NET
101 Raj PHP 2 PHP
102 Meet .NET 3 Android
102 Meet PHP 4 iPhone
102 Meet iPhone Answ ∏ Name, Technology (Student) ÷ ∏ Technology
102 Meet Android er (Project)
Outpu
103 Rohit Android t
Name
104 Sures .NET Meet
h
104 Sures iPhone
h
104 Sures Android 63
Relational Algebra Operations
Rename Operator
Section - 3.8
Rename Operator
🞂 Symbol: ρ (Rho)
🞂 Notation: ρA (X1,X2….Xn) (Relation)
🞂 Operation:
⮩ The rename operation is used to rename the output relation.
⮩ The result of rename operator are also relations with new name.
⮩ The original relation name can not be changed when we perform rename operation on any relation.
🞂 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.
75
Open Source and Commercial DBMS
Section - 4
Open Source and Commercial DBMS
Open Source Commercial DBMS
DBMS, which is available in the market at free DBMS, which is available in the market at a
of cost. certain price.
The code of open source DBMS product can be The code of commercial DBMS product can not
viewed, shared or modified by the community. be view, share or modify by the community.
There are chances of malfunctioning with code The security is high and code is not accessible
as source code is open. to unauthorized person.
Examples: MySQL, MongoDB, SQLite etc Examples: Microsoft SQL Server, IBM Db2 etc
77
Question Bank
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. Rename
VI. Division
VII. Set operators
3. Explain different aggregate functions with example.
78
Question Bank [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.
79
Question Bank [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 name of all instructors in the physics department.
II. Find all the courses taught in the fall 2009 semester but not in Spring semester.
III. Find the names of all instructors in the Comp. Sci. department together with the course titles of all the
courses that the instructors teach.
IV. Find the average salary in each department.
80
Question Bank [Relational Algebra]
6. Consider the following relations and write an relational algebra:
▪ EMP (empno, ename, jobtitle, managerno, hiredate, sal, commission, deptno)
▪ DEPT (deptno, dname, location)
I. Find the Employees working in the department number10, 20, 30 only.
II. Find Employees whose names start with letter A or letter a.
III. Find Employees along with their department name.
IV. Find the Employees who are working in Smith's department
V. Find the Employees who get salary more than Allen’s salary.
VI. Display employees who are getting maximum salary in each department.
VII. Find list of employees whose hire date is on or before 1-April-18.
82
Display employees who are getting maximum salary in each department.
• EMP (empno, ename, jobtitle, managerno, hiredate, sal, commission, deptno)
• DEPT (deptno, dname, location)
83
Question Bank [Relational Algebra]
7. 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.
II. Find the names, street address, and cities of residence of all employees who work for HCL and earn more
than $10,000 per annum.
85
Thank
You