DBMS Unit-3
DBMS Unit-3
(DBMS)
GTU # 3130703
Unit-3
Relational
Query
Languages
Looping
Outline
• 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 Table (Relation): A database object
(5) that holds a collection of data for a
specific topic.
Stud Table consist of rows and columns.
Attributes:
ent
Roll Nam Bran Semes SP
Title of Column (Attribute): The vertical
column No e ch ter I
component of a table. A column has a
101 Raju CE 3 8 name and a particular data type; e.g.
102 Mites CI 3 7 varchar, decimal, integer, datetime etc.
Cardinality = No of
Rows or h Record (Tuple): The horizontal
Tuples or component of a table, consisting of a
103 Mayu CE 3 6
Records sequence of values, one for each
r
tuples (7)
(7) column of the table. It is also known as
104 Niles EE 3 9 row.
h
A database consists of a collection of
105 Hites CI 3 7 tables (relations), each having a unique
h name.
Degree = No of
Domain 106is a setTarun ME
columns
of all 3 unique 8
(5)
possible values for
a specific107
column.Sures CE 3 9
Domain of Branch h attribute is (CE, CI, ME, EE)
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).
Super Key Super Key
Super Key
(RollNo, Branch, (SPI, Name,
EnrollNo
Sem) BL)
Primary
Key
Candidate Candidate Key
Key (RollNo, Branch,
EnrollNo Sem)
Primary Alternate
Key Key
Candidate Candidate Key
Key (RollNo, Branch,
EnrollNo Sem)
Stud Proje
ent Nam Bran Se ct
EnrollNo Projec
e ch m Title EnrollNo
tID
190540107 Raju CE 3 101 Bank 190540107
001 001
190540107 Mites CE 3 102 College 190540107
002 h 002
190540107 Niles CE 3 103 School 190540107
003 h 003
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
er (Student)
Outp
ut
Roll Nam Bran SP
No e ch I
104 Meet CE 9
Selection Operator [σ condition (Relation)]
Exam Display the detail of students belongs to either “CI”
ple or “ME” Branch.
Stud
ent
Roll Nam Bran SP
No e ch I
101 Raju CE 8
102 Mites ME 9
h
103 Niles CI 9
h
104 σBranch=‘CI’
Answ Meet CE 9
V Branch=‘ME’
er (Student)
Outp
ut
Roll Nam Bran SP
No e ch I
102 Mites ME 9
h
103 Niles CI 9
Selection Operator [σ condition (Relation)]
Exam Display the detail of students whose SPI
ple between 7 and 9.
Stud
ent
Roll Nam Bran SP
No e ch I
101 Raju CE 8
102 Mites ME 9
h
103 Niles CI 9
h
104 σSPI>7CEΛ SPI<99
Answ Meet
er (Student)
Outp
ut
Roll Nam Bran SP
No e ch I
101 Raju CE 8
Exercise
Write down the relational algebra for the student Stud
ent
table. Roll Nam Bran SP
No e ch I
Display the detail of students whose RollNo is less than
104. 101 Raj CE 6
Display the detail of students having SPI more than 8. 102 Meet ME 8
Display the detail of students belongs to “CE” Branch 103 Hars EE 7
having SPI less than 8. h
Display the detail of students belongs to either “CE” or 104 Punit CE 9
“ME”down
Write Branch. the relational algebra for the Emplo
Display the detail of students whose SPI between 6 and yee
EmpI Nam Sala
employee table. Dept
9. D e ry
Display the detail of all employee.
101 Niles Sales 1000
Display the detail of employee whose Salary more than
h 0
10000.
Display the detail of employee belongs to “HR” Dept 102 Mayu HR 2500
r 0
having Salary more than 20000.
Display the detail of employee belongs to either “HR” 103 Hardi HR 1500
k 0
or “Admin” Dept.
Display the detail of employee whose Salary between 104 Ajay Admi 200
n 00
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.
Exam Display RollNo, Name and Branch of Answ ∏ RollNo, Name, Branch
ple all students. er (Student)
Stud Outp
ent
Roll Nam Bran SP ut
Roll Nam Bran
No e ch I No e ch
101 Raju CE 8 101 Raju CE
102 Mites ME 9 102 Mites ME
h h
103 Niles CI 9 103 Niles CI
h h
104 Meet CE 9 104 Meet CE
Exercise
Write down the relational algebra for the student Stud
ent
table. Roll Nam Bran SP
No e ch I
Display RollNo, Name and SPI of all students.
101 Raj CE 6
Display Name and SPI of all students.
Display the Name of all students. 102 Meet ME 8
Display the Name of all branches. 103 Hars EE 7
h
104 Punit CE 9
Write down the Emplo
relational algebra for the yee
employee table. EmpI Nam Sala
Dept
D e ry
Display EmpID with Name of all employee.
101 Niles Sales 1000
Display Name and Salary of all employee. h 0
Display the Name of all employee. 102 Mayu HR 2500
Display the Name of all departments. r 0
103 Hardi HR 1500
k 0
104 Ajay Admi 200
n 00
Combined Projection & Selection Operation
Exam Display RollNo, Name & Branch of “ME”
ple Branch students.
Stud
ent
Roll Nam Bran SP
No e ch I
101 Raju CE 8
102 Mites ME 9
h
103 Niles CI 9
h
104
Step- Meet σ CE
Branch=‘ME’ 7 Answ ∏ RollNo, Name, Branch (σBranch=‘ME’
1 (Student) er (Student))
Outpu Outpu
t-1
Roll Nam Bran SP t-2
Roll Nam Bran
No e ch I No e ch
102 Mites ME 9 102 Mites ME
h h
Combined Projection & Selection Operation
Exam Display Name, Branch and SPI of students whose SPI
ple is more than 8.
Stud
ent
Roll Nam Bran SP
No e ch I
101 Raju CE 8
102 Mites ME 9
h
103 Niles CI 9
h
104
Step- Meet σSPI>8CE 7 Answ ∏ Name, Branch, SPI (σSPI>8
1 (Student) er (Student))
Outpu Outpu
t-1
Roll Nam Bran SP t-2
Nam Bran SP
No e ch I e ch I
102 Mites ME 9 Mites ME 9
h h
103 Niles CI 9 Niles CI 9
Combined Projection & Selection Operation
Exam Display Name, Branch and SPI of students who belongs to “CE” Branch
ple and SPI is more than 7.
Stud
ent
Roll Nam Bran SP
No e ch I
101 Raju CE 8
102 Mites ME 9
h
103 Niles CI 9
h
104
Step- Meet σBranch=‘CE’
CE 7
Λ SPI>7 Answ ∏ Name, Branch, SPI (σBranch=‘CE’ Λ SPI>7
1 (Student) er (Student))
Outpu Outpu
t-1
Roll Nam Bran SP t-2
Nam Bran SP
No e ch I e ch I
101 Raju CE 8 Raju CE 8
Combined Projection & Selection Operation
Exam Display Name of students along with their Branch who belong to either “ME”
ple Branch or “CI” Branch.
Stud
ent
Roll Nam Bran SP
No e ch I
101 Raju CE 8
102 Mites ME 9
h
103 Niles CI 9
h
104
Step- Meet σBranch=‘ME’
CE 7
V Branch=‘CI’ Answ ∏ Name, Branch (σBranch=‘ME’ V Branch=‘CI’
1 (Student) er (Student))
Outpu Outpu
t-1
Roll Nam Bran SP t-2
Nam Bran
No e ch I e ch
102 Mites ME 9 Mites ME
h h
103 Niles CI 9 Niles CI
Exercise
Write down the relational algebra for the student Stud
ent
table. Roll Nam Bran SP
No e ch I
Display Rollno, Name and SPI of all students belongs to
“CE” Branch. 101 Raj CE 6
List the Name of students with their Branch whose SPI 102 Meet ME 8
is more than 8 and belongs to “CE” Branch.
103 Hars EE 7
List the Name of students along with their Branch and h
SPI who belongs to either “CE” or “ME” Branch and
104 Punit CE 9
having SPI more than 8.
Write Emplo
down the relational algebra for the
Display the Name of students with their Branch name yee
employee
whose SPItable.
between 7 and 9.
EmpI Nam
Dept
Sala
Display the Name of employee belong to “HR” Dept D e ry
and having salary more than 20000. 101 Niles Sales 1000
Display the Name of all “Admin” and “HR” Dept’s h 0
employee. 102 Mayu HR 2500
List the Name of employee with their Salary who r 0
belongs to “HR” or “Admin” Dept having salary more 103 Hardi HR 1500
than 15000. k 0
Display the Name of employee along with their Dept 104 Ajay Admi 200
name whose salary between 15000 and 30000. n 00
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
Exam Perform Cross Product between Student Answ (Student) X
ple and Result. er (Result)
Steps performed in
Natural Description
Steps Join
Step – 1 It performs Cartesian Product
Step – 2 Then it deletes inconsistent tuples
Then it removes an attribute from duplicate
Step – 3
attributes
Natural Join / Inner Join Example
Exam Perform Natural Join between Student Answ (Student)
ple and Result. er (Result)
Stud Resu Outp
ent
RN Nam Bran lt
RN SP ut
RN Nam Bran SP
o e ch o I o e ch I
10 Raju CE 10 8 10 Raju CE 8
1 1 To perform a Natural
1 Join there must be one
10 Mites ME 10 9 common attribute (column) between two
Steps
2 h performed in 3 relations.
Natural Join
Step:1 Perform Cross Step:2 Removes
Product
Student. Nam Bran Result. SP inconsistent
Student. Nam tuples
Bran Result. SP
RNo e ch RNo I RNo e ch RNo I
101 Raju CE 101 8 101 Raju CE 101 8
Step:3 Removes an attribute
101 Raju CE 103 9
from duplicate
RN Nam Bran SP
102 Mites ME 101 8 o e ch I
h
10 Raju CE 8
102 Mites ME 103 9 1
h
Natural Join / Inner Join Example
Exam Perform Natural Join between Branch Answ (Branch)
ple and Faculty. er (Faculty)
Bran Facul Outp
ch
BI BNa ty
FI FNa BI ut
BI Bna HO FI FNa
HOD
D me D me D D me D D me
1 CE Shah 10 Raj 1 1 CE Sha 10 Raj
1 h 1
2 ME Patel
10 Meet 2 2 ME Pate 10 Meet
3 l 3
To perform a Natural Join there must be one common
attribute (column) between two relations.
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)
Exam List the name of students with their department name and SPI of all student
ple belong to “CE” department.
Answ ∏ Sname, Dname, (σDname=‘CE’ (Student (Department
er SPI Academic)))
Exam Display the name of students with their project name whose
ple guide is “A. J. Shah”.
Answ ∏ Sname, (σFname=‘A.J.Shah’ (Student (Guide
er Pname Faculty)))
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”
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 Sym
Sr. Outer Join
1 Left Outer Join bol
2 Right Outer Join
3 Full Outer Join
Exerci Is there any difference in the output if we swap the tables in Intersection.
se (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).
Exerci Is there any difference in the output if we swap the tables in Set difference.
se (Employee) − (Customer).
Union Operators Example
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
Alge (A) ÷ Alge (A) ÷ Alge (A) ÷ Alge (A) ÷
S1 P4
bra (B1) bra (B2) bra (B3) bra (B4)
S2 P1 Outp Outp Outp Outp
ut ut ut ut
S2 P2 SNo SNo SNo SNo
S3 P2 S1 S1 S1
S4 P2 S2 S4
S4 P4 S3
S5 P4 S4
Division Operator Example
Exam List the name of students doing a project in all
ple technologies.
Stude Proje
nt
RN Nam Technol ct
TI Technol
o e ogy D ogy
10 Raj .NET 1 .NET
1
2 PHP
10 Raj PHP
1 3 Android
10 Meet .NET 4 iPhone
2
Answ ∏ Name, Technology (Student) ÷ ∏ Technology
10 Meet PHP er (Project)
2 Outp
10 Meet iPhone ut
Name
2
Meet
10 Meet Android
2
10 Rohit Android
3
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.
Rename Operator Example
Exam Rename Exam Rename
ple table ple attributes
Stud Stud
ent ent
Rn Na CP
RN Na CP
o me I o me I
10 Raj 8 10 Raj 8
1 1
10 Meet 9 10 Meet 9
2 2
AlgeJay ρPerson
10 7 AlgeJay ρ(RollNo,
10 7 StudentName, SPI)
3
bra (Student) 3
bra (Student)
Perso Stud
n ent
Roll StudentN SP
RN Na CP
o me I No ame I
10 Raj 8 101 Raj 8
1
102 Meet 9
10 Meet 9
2 103 Jay 7
10 Jay 7
Rename Operator Example
Exam Rename table and Exam Rename particular
ple attributes both ple attributes
Stud Stud
ent
Rn Na CP ent
Rn Na CP
o me I o me I
10 Raj 8 10 Raj 8
1 1
10 Meet 9 10 Meet 9
2 2
AlgeJay ρPerson
10 7 (RollNo, StudentName) (∏ RNo, Name AlgeJay ρStudentName
10 7 / Name
3
bra (Student)) 3
bra (Student)
Perso Stud
n
Roll StudentN ent
Rn StudentN CP
No ame o ame I
101 Raj 10 Raj 8
102 Meet 1
10 Meet 9
103 Jay 2
Relational Algebra
Operations
Aggregate Functions
Section - 3.9
Aggregate Functions
Symbol: g or G
Notation: g (Relation)
function-name(column), function-name(column), …, function-name(column)
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
Stud
ent Exam Find out sum of CPI of all Outpu
Rn Bran Semes CP ple students. t
Name sum
o ch ter I
Answ g sum(CPI)
10 Rames CE 3 9 73
er (Student)
1 h
10 Mahes EC 3 8 Exam Find out maximum & Outpu
2 h ple minimum CPI. tma mi
10 Suresh ME 4 7 Answ g max(CPI), min(CPI) x n
3 er (Student) 9 7
10 Amit EE 4 8 Exam Count the number of Outpu
4 ple students. t
count
10 Anita CE 4 8 Answ g count(Rno)
5 9
er (Student)
10 Reeta ME 3 7
6
Exam Find out average of CPI of all Outpu
ple students. t
avg
Answ g avg(CPI)
10 Rohit EE 4 9
7 8.11
er (Student)
10 Chetan CE 3 8
8
Relational Algebra [Exercise]
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)
Find the names of all employees who work for “TCS”.
Find the names and cities of residence of all employees who work for “Infosys”.
Find the names, street and city of residence of all employees who work for “ITC” and
earn more than $10,000 per annum.
Find the names of all employees in this database who live in the same city as the
company for which they work.
Find the names of all employees working in “TCS” who earn more than 25000 and
less than 40000.
Find the name of employee whose manager is “Ajay Patel” and salary is more than
50000.
Display the name of employee with street, city, company name, salary and manager
name staying in “Rajkot” and working in “Ahmedabad”.
Find maximum, minimum and average salary of all employee.
Find out the total number of employee.
Open Source and
Commercial DBMS
Section - 4
Open Source and Commercial DBMS
Open Source Commercial DBMS
DBMS, which is available in the DBMS, which is available in the
market at free of cost. market at a certain price.
The code of open source DBMS The code of commercial DBMS
product can be viewed, shared or product can not be view, share or
modified
There arebychances
the community.
of malfunctioning modify by theiscommunity.
The security high and code is not
with code as source code is open. accessible to unauthorized person.
Examples: MySQL, MongoDB, SQLite Examples: Microsoft SQL Server, IBM
etc Db2 etc