5KS01 DBMS Unit II Relational Algebra SQL
5KS01 DBMS Unit II Relational Algebra SQL
(DBMS)
5KS01
Unit-II
Relational
Algebra , SQL
Mr. J. M. Patil
Department of Computer Science and Engineering
SSGMCE,Shegaon
[email protected]
9921860806
Looping
Outline
• Structure of Relational Databases
• Key
• Why We Need Keys in DBMS
• Types of 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 Branc Semes SP
Title of Name Column (Attribute): The vertical
column No h ter I
component of a table. A column has a
101 Raju CE 3 8 name and a particular data type; e.g.
Mites varchar, decimal, integer, datetime etc.
Cardinality = No of
Rows or 102 CI 3 7 Record (Tuple): The horizontal
h
Tuples or component of a table, consisting of a
Mayu
Records 103 CE 3 6 sequence of values, one for each
r
tuples (7)
(7) column of the table. It is also known as
Niles row.
104 EE 3 9
h
A database consists of a collection of
Hites tables (relations), each having a unique
105 CI 3 7
h name.
Degree = No of
Domain is106a setTarun ME (5)unique
columns
of all possible 3 8
values for
a specific column.
Sures
107
Domain of Branch CE 3 9
h attribute is (CE, CI, ME, EE)
5KS01 (DBMS) Unit II – Relational Algebra ,
Mr. Jaikumar M.Patil 4
Key
Section - 2
What is Key and Why We Need DBMS Keys?
maintained.
between tables.
Mr. Jaikumar M.Patil
5KS01 (DBMS) Unit II – Relational Algebra ,
6
Types of Key
Super Key
Candidate Key
Primary Key
Alternate Key
Foreign Key
Unique Key
Surrogate Key
Super Key
A super key is a combination of all possible attributes whose values
uniquely identifies each record within a relation (table).
A super key may contain additional attributes that are not necessary for
unique identification.
Super key is a superset of a candidate key.
Every candidate key is a super key, but not every super key is a
Super Key Super Key
candidate key. Super Key
(RollNo, Branch, (SPI, Name,
Table can have many super key.
EnrollNo
Sem) BL)
Roll Branc Se SP B
EnrollNo Name
No h m I 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
5KS01 (DBMS) Unit II – Relational Algebra ,
Mr. Jaikumar M.Patil 190540106 102 CI 3 9 Niles 0 8
Candidate Key
A candidate key is a column, or set of columns, that can uniquely identify a
row in a table. There can be multiple candidate keys in a table.
A candidate key is a minimal subset of a super key.
If any proper subset of a super key is a super key then that key cannot be a
candidate key.
Every candidate key is a super key but every super key is not a candidate
key. Candidate Candidate Key
Candidate keys are Keynot allowed
(RollNo,
toBranch,
have null values.
EnrollNo Sem)
Roll Branc Se SP B
EnrollNo Name
No h m I 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
Mr. Jaikumar M.Patil 001 5KS01 (DBMS) r
Unit II – Relational Algebra ,
9
Primary Key
A primary key is a column or a set of columns that uniquely identifies each row in a table.
Or candidate key that is chosen by database designer to identify tuples uniquely in
a relation (table).
The value of primary key can never be NULL.
There is only one primary key in the relation (table).
The Primary
value of primary key must always be unique and can never be changed. No
Updations
Keyis possible.
Candidate Candidate Key
Key (RollNo, Branch,
EnrollNo Sem)
Roll Branc Se SP B
EnrollNo Name
No h m I 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
Mr. Jaikumar M.Patil 001 5KS01 (DBMS) r
Unit II – Relational Algebra ,
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.
Roll Branc Se SP B
EnrollNo Name
No h m I 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
Mr. Jaikumar M.Patil 001 5KS01 (DBMS) r
Unit II – Relational Algebra ,
12
Foreign Key
A foreign key is an attribute in a table is used to define its relationship with another
table.
A foreign key is an attribute or collection of attributes in one table that refers to the
primary key in another table.
Using foreign key helps in maintaining data integrity for tables in relationship.
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 Branc Se ct
EnrollNo Projec
e h 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
5KS01 (DBMS) Unit II – Relational Algebra ,
003
Mr. Jaikumar M.Patil h 003 13
Foreign Key rules
A foreign key may refer the primary key of the table.
Foreign key take only those value which are present in the primary key of
the referenced relation.
Foreign key may have a name other than that of primary key.
Foreign key can take the null value.
In fact, foreign key is not unique in all the time.
Referenced relation may also be called the Master table or Primary
table.
Referencing relation may also be called the Foreign table.
A unique key is similar to a primary key, but it allows one ‘NULL’ value
in the column..
Ensures that all values in a column are unique.
Can have NULL Value.
er (Student)
Outp
ut
Roll Nam Branc SP
No e h I
104 Meet CE 9
5KS01 (DBMS) Unit II – Relational Algebra ,
Mr. Jaikumar M.Patil 23
Selection Operator [σ condition (Relation)]
Exam Display the detail of students belongs to either “CI”
ple or “ME” Branch.
Stud
ent
Roll Nam Branc SP
No e h I
101 Raju CE 8
102 Mites ME 9
h
103 Niles CI 9
h
104
Answ Meet σBranch=‘CI’
CE 9
V Branch=‘ME’
er (Student)
Outp
ut
Roll Nam Branc SP
No e h I
102 Mites ME 9
h
103Mr. Jaikumar
NilesM.Patil
CI 9 5KS01 (DBMS) Unit II – Relational Algebra ,
24
Selection Operator [σ condition (Relation)]
Exam Display the detail of students whose SPI
ple between 7 and 9.
Stud
ent
Roll Nam Branc SP
No e h I
101 Raju CE 8
102 Mites ME 9
h
103 Niles CI 9
h
104
Answ Meet σSPI>7CEΛ SPI<99
er (Student)
Outp
ut
Roll Nam Branc SP
No e h I
101 Raju CE 8
Stud Outp
ent
Roll Nam Branc SP ut
Roll Nam Branc
No e h I No e h
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
104Prof. Firoz
Meet CE
A Sherasiya 9 104
#3130703 (DBMS) Unit 3 – Relational Query Meet CE 28
Exercise
Write down the relational algebra for the student Stud
ent
table. Roll Nam Branc SP
No e h 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 2000
5KS01 (DBMS) Unit II – Relational Algebra , n 0
Mr. Jaikumar M.Patil 29
Combined Projection & Selection Operation
Exam Display RollNo, Name & Branch of “ME”
ple Branch students.
Stud
ent
Roll Nam Branc SP
No e h 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 Branc SP t-2
Roll Nam Branc
No e h I No e h
102 Mites ME 9 102 Mites ME
h h
#3130703 (DBMS) Unit 3 – Relational Query
Prof. Firoz A Sherasiya 30
Combined Projection & Selection Operation
Exam Display Name, Branch and SPI of students whose SPI
ple is more than 8.
Stud
ent
Roll Nam Branc SP
No e h 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 Branc SP t-2
Nam Branc
SPI
No e h I e h
102 Mites ME 9 Mites ME 9
h h
103Prof. Firoz
Niles CI
A Sherasiya
9 #3130703 (DBMS) UnitNiles CI Query
3 – Relational 9 31
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 Branc SP
No e h 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 Branc SP t-2
Nam Branc
SPI
No e h I e h
101 Raju CE 8 Raju CE 8
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)))
5KS01 (DBMS) Unit II – Relational Algebra ,
Mr. Jaikumar M.Patil 43
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”
#3130703 (DBMS) Unit 3 – Relational Query
Prof. Firoz A Sherasiya 44
Inner Join
Symbol:
Notation: Relation-1 (R1) Relation-2 (R2) OR Algebra-1 Algebra-2
An inner join in Database Management Systems (DBMS) is a type of join
operation that combines rows from two tables based on a specified
condition.
The result set includes only the rows where the join condition is true,
meaning it returns only the matching rows from both tables.
Three types of
Outer Join Sym
Sr. Outer Join
1 Left Outer Join bol
2 Right Outer Join
3 Full Outer Join
Outp
ut
Roll Nam
SPI
No e
101 Raj 8
102 Meet NUL
L
5KS01 (DBMS) Unit II – Relational Algebra ,
103 Mr. Jaikumar
NUL M.Patil
9 54
Relational Algebra
Operations
Set Operators
Section - 3.6
Set Operators
Set operators combine the results of two or more queries into a
single result.
Exerci Is there any difference in the output if we swap the tables in Intersection.
se (Employee) ∩ (Customer).
#3130703 (DBMS) Unit 3 – Relational Query
Prof. Firoz A Sherasiya 60
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).
Rules for Set Difference:
Same Number of Columns: Both tables involved in the operation must have the same
number of columns.
Compatible Data Types: The corresponding columns must have the same or
Order Does Not Matter: The order of the rows does not affect the result.
Exerci Is there any difference in the output if we swap the tables in Set difference.
se (Employee) − (Customer).
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
Operation:
The rename operation is used to rename the output relation.
The result of rename operator are also relations with 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.
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.)
Thank
You
Mr. J. M. Patil
Department of Computer Science and
Engineering
SSGMCE,Shegaon
[email protected]
9921860806