0% found this document useful (0 votes)
19 views73 pages

DBMS Unit-3

Uploaded by

22f3000849
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views73 pages

DBMS Unit-3

Uploaded by

22f3000849
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 73

Database Management Systems

(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)

Roll Bran Se SP Nam B


EnrollNo
No ch m I e L
190540107 101 CE 3 8 Raju 0
001
190540107 102 CE 3 7 Mites 1
002 h
190540106 101 CI 3 6 Mayu 2
001 r
190540106 102 CI 3 9 Niles 0
002 h
180540107 101 CE 5 7 Hites 1
001 h
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 Candidate Key
candidate key.
Key (RollNo, Branch,
EnrollNo Sem)

Roll Bran Se SP Nam B


EnrollNo
No ch m I e L
190540107 101 CE 3 8 Raju 0
001
180540107 101 CE 5 7 Mites 1
002 h
190540106 101 CI 3 6 Mayu 2
001 r
Primary Key
 A primary key is a candidate key that is chosen by database
designer to identify tuples uniquely in a relation (table).

Primary
Key
Candidate Candidate Key
Key (RollNo, Branch,
EnrollNo Sem)

Roll Bran Se SP Nam B


EnrollNo
No ch m I e L
190540107 101 CE 3 8 Raju 0
001
180540107 101 CE 5 7 Mites 1
002 h
190540106 101 CI 3 6 Mayu 2
001 r
Alternate Key
 An alternate key is a candidate key that is not chosen by database
designer to identify tuples uniquely in a relation.

Primary Alternate
Key Key
Candidate Candidate Key
Key (RollNo, Branch,
EnrollNo Sem)

Roll Bran Se SP Nam B


EnrollNo
No ch m I e L
190540107 101 CE 3 8 Raju 0
001
180540107 101 CE 5 7 Mites 1
002 h
190540106 101 CI 3 6 Mayu 2
001 r
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.
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

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

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
Relational Algebra
Operations
Selection Operator
Section - 3.1
Selection Operator
 Symbol: σ (Sigma)
 Notation: σ (Relation)
condition
 Operation: Selects tuples from a relation that satisfy a given
condition.
 Operators: =, <>, <, >, <=, >=, Λ (AND), V (OR)
Exam σ
Display the detail of students belongs to Answ Branch=‘CE’
ple “CE” Branch. er (Student)
Stud Outp
ent
Roll Nam Bran SP ut
Roll Nam Bran SP
No e ch I No e ch I
101 Raju CE 8 101 Raju CE 8
102 Mites ME 9 104 Meet CE 9
h
103 Niles CI 9
h
104 Meet CE 9
Selection Operator [σ condition (Relation)]

Exam Display the detail of students belongs to “CE” Branch and


ple having SPI 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

Answ σBranch=‘CE’ Λ SPI>8


104 Meet CE 9

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)

Stud Resu Outp


ent
RN Nam Bran lt
RN SP ut
Student. Nam Bran Result. SP
o e ch o I RNo e ch RNo I
10 Raju CE 10 8 101 Raju CE 101 8
1 1
101 Raju CE 102 9
10 Mites ME 10 9
If both relations have some attribute with the same 102 Mites ME 101 8
2 h 2
name, it can be distinguished by combing relation- h
name.attribute-name.
102 Mites ME 102 9
Cartesian Product / Cross Product Example
Consider only selected
Exam Perform Cross Product between Student attributes
ple and Result. • Student – RNo, Name and
Stud Resu Branch
ent
RN Nam Bran Se lt
RN SP B Ran • Result – RNo, SPI and BL
o e ch m o I L k
10 Raju CE 3 10 8 1 2
1 1
10 Mites ME 5 10 9 0 1
2 h
Answ ∏ RNo, Name, Branch (Student)
3 X ∏ RNo, SPI, BL
er (Result)
Outp
ut
Student. Nam Bran Result. SP B
RNo e ch RNo I L
101 Raju CE 101 8 1
101 Raju CE 103 9 0
102 Mites ME 101 8 1
h
102 Mites ME 103 9 0
Cartesian Product / Cross Product Example
Exam Perform Cross Product between Student Consider only selected tuples
• Student – Branch=‘CE’ and
ple and Result.
Stud Resu Sem=3
ent lt • Result – SPI>7 and BL<1
RN Nam Bran Se RN SP B Ran
o e ch m o I L k
10 Raju CE 3 10 8 1 2
1 1
10 Mites ME 5 10 9 0 1
2 h 3
10 Om CE 3 10 7 2 3
Answ σBranch=‘CE’ Λ Sem=3 X σSPI>7 Λ
3 5
(Student)
10
er Dhar CE 5
BL<1 (Result)
4
Outpa
ut
Student. Na Bran Se Result. SP B Ran
RNo me ch m RNo I L k
101 Raju CE 3 103 9 0 1
103 OM CE 3 103 9 0 1
Relational Algebra
Operations
Natural Join / Inner Join
Section - 3.4
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.

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

To perform a Outer Join there must be one common


attribute (column) between two relations.
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.
Exam Perform Left Outer Join between Student Answ (Student)
ple and Result. er (Result)
Stud Resu Outp
ent
Roll Na Bran lt
Roll SP ut
Roll Nam Bran
SPI
No me ch No I No e ch
101 Raj CE 101 8 101 Raj CE 8
102 Meet ME 103 9 102 Meet ME NUL
L
Exerci What is the output of (Result)
se (Student).
Left Outer Join Example
Exam Perform Left Outer Join between Student and Result. (Display
ple RollNo, Name and SPI)
Stud Resu
ent
Roll Na Bran lt
Roll SP B
No me ch No I L
101 Raj CE 101 8 1
102 Meet ME 103 9 0

Answ ∏ RollNo, Name, ((Student)


er SPI
(Result))
Outp
ut
Roll Nam
SPI
No e
101 Raj 8
102 Meet NUL
L
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.
Exam Perform Right Outer Join between Student Answ (Student)
ple and Result. er (Result)
Stud Resu Outp
ent
Roll Na Bran lt
Roll SP ut
Roll Nam Bran SP
No me ch No I No e ch I
101 Raj CE 101 8 101 Raj CE 8
102 Meet ME 103 9 103 NUL NULL 9
L
Exerci What is the output of (Result)
se (Student).
Right Outer Join Example
Exam Perform Right Outer Join between Student and Result. (Display
ple RollNo, Name and SPI)
Stud Resu
ent
Roll Na Bran lt
Roll SP B
No me ch No I L
101 Raj CE 101 8 1
102 Meet ME 103 9 0

Answ ∏ RollNo, Name, ((Student)


er SPI (Result))
Outp
ut
Roll Nam SP
No e I
101 Raj 8
103 NUL 9
L
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 kindFull
Perform of Outer
tuples having
Join noStudent
between matching, it willAnsw
be padded with NULL in
(Student)
Exam
pleresultant
and relation.
Result. er (Result)
Stud Resu Outp
ent
Roll Na Bran lt
Roll SP ut
Roll Nam Bran
SPI
No me ch No I No e ch
101 Raj CE 101 8 101 Raj CE 8
102 Meet ME 103 9 102 Meet ME NUL
L
Exerci What is the output of (Result) 103 NUL NULL 9
se (Student). L
Full Outer Join Example
Exam Perform Full Outer Join between Student and Result. (Display
ple RollNo, Name and SPI)
Stud Resu
ent
Roll Na Bran lt
Roll SP B
No me ch No I L
101 Raj CE 101 8 1
102 Meet ME 103 9 0

Answ ∏ RollNo, Name, ((Student)


er SPI (Result))
Outp
ut
Roll Nam
SPI
No e
101 Raj 8
102 Meet NUL
L
103 NUL 9
Relational Algebra
Operations
Set Operators
Section - 3.6
Set Operators
 Set operators combine the results of two or more queries into a
single result.

Three types of Set


Operators Sym
Sr. Set Operator
1 Union bol
U
Intersect /
2 ∩
Intersection
Minus / Set
3 −
difference
Conditi Set operators will take two or more queries as input, which must be
• Bothunion-compatible.
ons queries should have same (equal) number of
columns
• Corresponding attributes should have the same
data type or domain
Conditions to perform Set Operators
Conditio Both queries should have same (equal)
ns-1 number of columns.
Stud Facul Stud Facul
ent
RN Na De SP ty
FI Na De ent
RN Na De ty Na De
FId
o me pt I d me pt o me pt me pt
10 Raj CE 8 10 Patel CE 10 Raj CE 101 Patel CE
1 1 1
102 Shah ME
10 Meet ME 9 10 Shah ME 10 Meet ME
2 2 2 103 Dav ME
e
10 Jay
Conditio CE 9 10 Dav
Corresponding ME should have
attributes 10theJay CE
3
ns-2 same data3type.
e 3
Stud Facul Stud Facul
ent
RN Na De SP ty
FI Na De ent
RN Na De SP ty
FI Na De Ex
Sub
o me pt I d me pt o me pt I d me pt p
10 Raj CE 8 10 Patel CE DS 10 Raj CE 8 10 Patel CE 5
1 1 1 1
10 Meet ME 9 10 Shah ME DBM 10 Meet ME 9 10 Shah ME 3
2 2 S 2 2
10 Jay CE 9 10 Dav ME DF 10 Jay CE 9 10 Dav ME 4
Set Operators [Exercise]

Exerci Check whether following tables are


se 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.

• Person (PersonID, Name, Address, Hobby)


• Professor (ProfessorID, Name, OfficeAddress, Salary)
• (Not compatible) Both tables have 4 attributes but forth attributes datatype is
∏ Name, Address
different.
& ∏ Name, OfficeAddress
(Person) (Professor)

• (Compatible) Both tables have 2 attributes and of same data type.


Union Operator
 Symbol: U
 Notation: Relation-1 (R1) U Relation-2 (R2) OR Algebra-1 U Algebra-2
 Operation:
 It displays all the tuples/records belonging to the first relation (left relation) or the
second relation (right relation) or both.
 It also eliminates duplicate tuples (tuples present in both relations appear once).
Exam Perform Union between Customer and Answ (Customer) U
ple Employee. er (Employee)
Custo Emplo Outp
mer yee ut
Name Name Name
Raju Meet Manoj
Suresh Suresh Meet
Meet Manoj Raju
Suresh
Exerci Is there any difference in the output if we swap the tables in Union operator.
se (Employee) U (Customer).
Intersect/ Intersection Operator
 Symbol: ∩
 Notation: Relation-1 (R1) ∩ Relation-2 (R2) OR Algebra-1 ∩ Algebra-2
 Operation:
 It displays all the tuples/records belonging to both relations. OR
 It displays all the tuples/records which are common from both relations.

Exam Perform Intersection between Customer Answ (Customer) ∩


ple and Employee. er (Employee)
Custo Emplo Outp
mer yee ut
Name Name Name
Raju Meet Meet
Suresh Suresh Suresh
Meet Manoj

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).

Exam Perform Set difference between Customer Answ (Customer) −


ple and Employee. er (Employee)
Custo Emplo Outp
mer yee ut
Name Name Name
Raju Meet Raju
Suresh Suresh
Meet Manoj

Exerci Is there any difference in the output if we swap the tables in Set difference.
se (Employee) − (Customer).
Union Operators Example

Exam Display Name of person who are either


ple employee or customer.
Custo Emplo
mer
I Nam Balan yee
I Nam De Sala
D e ce D e pt ry
1 Raju 10000 2 Sures CE 8000
h
2 Sures 2000
h 0 3 Mano ME 900
j 0
Answ ∏ Name (Customer) U ∏ Name
er (Employee)
Outp
ut
Name
Manoj
Raju
Suresh
Intersect/ Intersection Operators Example

Exam Display Name of person who are employee as well


ple as customer.
Custo Emplo
mer
I Nam Balan yee
I Nam De Sala
D e ce D e pt ry
1 Raju 10000 2 Sures CE 8000
h
2 Sures 2000
h 0 3 Mano ME 900
j 0
Answ ∏ Name (Customer) ∩ ∏ Name
er (Employee)
Outp
ut
Name
Suresh
Minus/ Set difference Operators Example

Exam Display Name of person who are employee but


ple not customer.
Custo Emplo
mer
I Nam Balan yee
I Nam De Sala
D e ce D e pt ry
1 Raju 10000 2 Sures CE 8000
h
2 Sures 2000
h 0 3 Mano ME 900
j 0
Answ ∏ Name (Employee) − ∏ Name
er (Customer)
Outp
ut
Name
Manoj
Minus/ Set difference Operators Example

Exam Display Name of person who are customer but


ple not employee.
Custo Emplo
mer
I Nam Balan yee
I Nam De Sala
D e ce D e pt ry
1 Raju 10000 2 Sures CE 8000
h
2 Sures 2000
h 0 3 Mano ME 900
j 0
Answ ∏ Name (Customer) − ∏ Name
er (Employee)
Outp
ut
Name
Raju
Set Operators [Exercise]
Exerci What is the output of following relational algebra for the below
se mentioned tables:
Custo Emplo
mer
I Nam Balan yee
I Nam De Sala
D e ce D e pt ry
1 Raju 10000 2 Sures CE 8000
h
2 Sures 2000
h 0 3 Mano ME 900
j 0
Algebr ∏ ID, Name(Customer) U ∏ ID, Name
a-1 (Employee)
Algebr ∏ ID, Name, Balance (Customer) U ∏ ID, Name, Salary
a-2 (Employee)
Algebr ∏ ID, Name(Customer) ∩ ∏ ID, Name
a-3 (Employee)
Algebr ∏ ID, Name, Balance (Customer) ∩ ∏ ID, Name, Salary
a-4 (Employee)
Set Operators [Exercise]
Exerci What is the output of following relational algebra for the below
se mentioned tables:
Custo Emplo
mer
I Nam Balan yee
I Nam De Sala
D e ce D e pt ry
1 Raju 10000 2 Sures CE 8000
h
2 Sures 2000
h 0 3 Mano ME 900
j 0
Algebr ∏ ID, Name(Customer) − ∏ ID, Name
a-1 (Employee)
Algebr ∏ ID, Name, Balance (Customer) − ∏ ID, Name, Salary
a-2 (Employee)
Algebr ∏ ID, Name(Employee) − ∏ ID, Name
a-3 (Customer)
Algebr ∏ ID, Name, Balance (Employee) − ∏ ID, Name, Salary
a-4 (Customer)
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.
Division Operator Example
Exam Perform Division operation between Student Answ (Student) ÷
ple and Subject. er (Subject)
Stude Subje Outp
nt ct ut
Nam Subje Subjec Name
e ct t Rohit
Raj DBMS DBMS
Suresh
Raj DS DS
Meet DS DF
Meet DF
Rohit DBMS
Rohit DS
Rohit DF
Sures DBMS
h
Sures DF
h
Sure DS
Division Operator 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

You might also like