You are on page 1of 41

Topics to be covered

▪ Structure of Relational Database


▪ Domain
▪ Keys of relations
▪ Relational Algebra
▪ Implementing Relational Algebra using SQL
▪ Set Operators
▪ Sub queries
Definitions
Arity = No. of • Attribute = Column Name
Columns = 3 • 2 attribute in same relation
(table) can't have same name

Total No. of
Tuples = Set of
Cardinality permitted
of relation values for that
attribute =
Row =
Domain
Record =
Tuple
• Predefined row/column
format for storing
information.
• Relation = Table
Keys
▪ Super Key
• A super key is a set of one or more attributes whose values
uniquely identifies each record within a relation.

Super Key (EnrollNo) Super Key (RollNo, Branch, Semester)

EnrollNo RollNo Name Branch Semester SPI BackLog


6662030701 101 Pritesh CE 3 8 0
6662030702 102 Vaibhav CE 3 7 0
6662030601 101 Mihir CI 3 8 0
6662030602 102 Darshan CI 3 6 1
5662030701 101 Priya CE 5 8 0
5662030702 102 Shreya CE 5 7 1
5662030601 101 Niddhi CI 5 7 1
Keys
▪ Candidate Key
• A candidate is a subset of a super key.
• A candidate key is the least combination of attribute that uniquely
identifies each record in the table.
• The least combination of fields distinguishes a candidate key from
a super key.
Keys
▪ Candidate Key What is the difference between
super key and candidate key??

Candidate Key
(RollNo, Branch, Semester)

EnrollNo RollNo Name Branch Semester SPI BackLog


6662030701 101 Pritesh CE 3 8 0
6662030702 102 Vaibhav CE 3 7 0
6662030601 101 Mihir CI 3 8 0
6662030602 102 Darshan CI 3 6 1
5662030701 101 Priya CE 5 8 0
5662030702 102 Shreya CE 5 7 1
5662030601 101 Niddhi CI 5 7 1
Keys
▪ Super Key V/S Candidate Key
EnrollNo alone works as a
super key??
Answer is Yes
Super Key So (EnrollNo, Branch) is super
(EnrollNo, Branch) key but not candidate key.

EnrollNo RollNo Name Branch Semester SPI BackLog


6662030701 101 Pritesh CE 3 8 0
6662030702 102 Vaibhav CE 3 7 0
6662030601 101 Mihir CI 3 8 0
6662030602 102 Darshan CI 3 6 1
5662030701 101 Priya CE 5 8 0
5662030702 102 Shreya CE 5 7 1
5662030601 101 Niddhi CI 5 7 1
Keys
▪ Super Key V/S Candidate Key
RollNo and Branch alone works
Super Key
as a super key??
(RollNo, Branch, Semester)
Answer is No

EnrollNo RollNo Name Branch Semester SPI BackLog


6662030701 101 Pritesh CE 3 8 0
6662030702 102 Vaibhav CE 3 7 0
6662030601 101 Mihir CI 3 8 0
6662030602 102 Darshan CI 3 6 1
5662030701 101 Priya CE 5 8 0
5662030702 102 Shreya CE 5 7 1
5662030601 101 Niddhi CI 5 7 1
Keys
▪ Super Key V/S Candidate Key
RollNo and Semester alone
Super Key
works as a super key??
(RollNo, Branch, Semester)
Answer is No

EnrollNo RollNo Name Branch Semester SPI BackLog


6662030701 101 Pritesh CE 3 8 0
6662030702 102 Vaibhav CE 3 7 0
6662030601 101 Mihir CI 3 8 0
6662030602 102 Darshan CI 3 6 1
5662030701 101 Priya CE 5 8 0
5662030702 102 Shreya CE 5 7 1
5662030601 101 Niddhi CI 5 7 1
Keys
▪ Super Key V/S Candidate Key
Branch and Semester alone
Super Key
works as a super key??
(RollNo, Branch, Semester)
Answer is No

EnrollNo RollNo Name Branch Semester SPI BackLog


6662030701 101 Pritesh CE 3 8 0
6662030702 102 Vaibhav CE 3 7 0
6662030601 101 Mihir CI 3 8 0
6662030602 102 Darshan CI 3 6 1
5662030701 101 Priya CE 5 8 0
5662030702 102 Shreya CE 5 7 1
5662030601 101 Niddhi CI 5 7 1
Keys
▪ Super Key V/S Candidate Key
(RollNo, Branch, Semester) is
Super Key super key as well as candidate
(RollNo, Branch, Semester) key.

EnrollNo RollNo Name Branch Semester SPI BackLog


6662030701 101 Pritesh CE 3 8 0
6662030702 102 Vaibhav CE 3 7 0
6662030601 101 Mihir CI 3 8 0
6662030602 102 Darshan CI 3 6 1
5662030701 101 Priya CE 5 8 0
5662030702 102 Shreya CE 5 7 1
5662030601 101 Niddhi CI 5 7 1
Keys
▪ Primary key
• A Primary key is chosen by database designer to identify tuples
uniquely in a relation.
▪ Alternate key
• If any table have more than one candidate key, then after choosing
primary key from those candidate key, rest of candidate keys are
known as an alternate key of that table.
Keys
▪ Primary Key V/S Alternate Key

Candidate
Primary KeyKey Candidate Key
Alternate Key
EnrollNo (RollNo, Branch, Semester)

EnrollNo RollNo Name Branch Semester SPI BackLog


6662030701 101 Pritesh CE 3 8 0
6662030702 102 Vaibhav CE 3 7 0
6662030601 101 Mihir CI 3 8 0
6662030602 102 Darshan CI 3 6 1
5662030701 101 Priya CE 5 8 0
5662030702 102 Shreya CE 5 7 1
5662030601 101 Niddhi CI 5 7 1
Keys
▪ Foreign key
• A foreign key is a set of one or more attributes whose values are
derived from the primary key attribute of another relation.
Stu_ID RollNo Name Branch Semester SPI BackLog
1 101 Pritesh CE 3 9 0
2 102 Vaibhav CI 3 8 0
3 101 Nidhi CE 5 7 1
4 105 Shyam CI 5 8 2

Fac_ID Stu_ID Fac_SN Fac_Name Branch

1 1 501 Akash CE
2 2 502 Mohit CI
3 4 504 Nitin CE
4 1 515 Harnish CI
Relational algebra operation
▪ It is a language for expressing relational database queries.
▪ It is a procedural query language.
▪ Operations are as below;
1. Selection
2. Projection
3. Division
4. Cartesian product / Cross product
5. Rename
6. Set Operations
• Union
• Intersection
• Difference / Minus
Selection operation
▪ Retrieve one or more than one row/s (tuples) that satisfies the
condition/s with all columns (attribute)
▪ Symbol : σ (Sigma)
▪ Notation : σ(condition) <Table name>
▪ Operators:
• =,!=, <,>,<=,>=, Λ(AND), ∨(OR)
▪ Example: Student

Stu_I Stu_RollNo Name Departmen


D t
1 101 Ram Computer
2 105 Param IT
3 208 Krishti Computer
4 106 Balram Electrical
Selection operation example
▪ Find out the list of students from the following table who have
department = “Computer”
Stu_I Stu_RollNo Name Departmen
D t
1 101 Ram Computer
2 105 Param IT
3 208 Krishti Computer
▪ σDepartment=“Computer” 4(Student)
106 Balram Electrical

▪ Output:

Stu_I Stu_RollNo Name Departmen


D t
1 101 Ram Computer
3 208 Krishti Computer
Projection operation
▪ Retrieve one or more than one column/s (attribute) that satisfies
the condition/s with all rows (tuples)
▪ Symbol : ∏ (Pi)
▪ Notation : ∏ (column name) <Table Name>
▪ Example: Student

Stu_I Stu_RollNo Name Departmen


D t
1 101 Ram Computer
2 105 Param IT
3 208 Krishti Computer
4 106 Balram Electrical
Projection operation example
▪ List all the student’s name and department

Stu_I Stu_RollNo Name Departmen


D t
1 101 Ram Computer
2 105 Param IT
3 208 Krishti Computer

▪ ∏Name, Department (Student)


4 106 Balram Electrical

▪ Output:
Name Departmen
t
Ram Computer
Param IT
Krishti Computer
Balram Electrical
Combined Projection & Selection Operation
▪ Example: Display rollno, name & department of “CE” department
students. RollNo Name Department SPI
101 Raj CE 8
Student 102 Meet ME 9
103 Harsh EE 8
104 Punit CE 9
∏ RollNo, Name, Department (σ Department=‘CE’ (Student) )
1
2
RollNo Name Department SPI
101 Raj CE 8
Output-1
104 Punit CE 9

RollNo Name Branch


Output-2 101 Raj CE
104 Punit CE
Combined Projection & Selection Operation
▪ Example: Display rollno and name of “CE” department students.
RollNo Name Department SPI
101 Raj CE 8
Student 102 Meet ME 9
103 Harsh EE 8
104 Punit CE 9
σ Department=‘CE’ (∏ RollNo, Name(Student) )
1
2
RollNo Name
Where is branch column to
101 Raj check condition??
Output-1
102 Meet
103 Harsh So we can’t use selection left
104 Punit side and projection right side.
Division Operator
▪ Symbol: ÷
▪ Notation: Relation1 ÷ Relation2
▪ Operation: Produce the tuples in Relation1, that match all tuples
in Relation2.
Division Operator Examples

B
1 B
2 B
3

A A/ A/ A/
B1 B2 B3
Cartesian product / Cross product
▪ Combines information of two tables
▪ It will multiply each row(tuples) of first table(relation) to each
row(tuples) of second table(relation).
▪ Symbol : X (Cross)
▪ Notation : Relation1 X Relation2
▪ Resultant Relation:
1. Relation1 have n1 attribute and Relation2 have n2 attribute then
resultant relation will have n1+n2 attribute.
2. Relation1 have n1 tuples and Relation2 have n2 tuples then
resultant relation will have n1*n2 tuples.
3. If both relations have some attribute with same name, then it
will be distinguished by relation1.attribute and
relation2.attribute
Cartesian product example
Student Faculty
Stu_I Stu_RollN Name Department Fac_I Fac_Nam Department
D o D e
1 101 Ram Computer 1 Nitin Computer
2 105 Param IT 2 Chintan IT
Student X Faculty
Stu_I Stu_RollN Name Departmen Fac_I Fac_Nam Faculty.Deparmtn
D o t D e et
1 101 Ram Computer 1 Nitin Computer
1 101 Ram Computer 2 Chintan IT
2 105 Param IT 1 Nitin Computer
2 105 Param IT 2 Chintan IT
Set Operators
▪ Operation: All of these operations take two input relations, which
must be union - compatible:
1. Both relations have same (equal) number of columns

RollN Name Branch SPI EmpN Name Branc


o o h
101 Raj CE 8 101 Patel CE
102 Meet CE 7 102 Shah CE
103 Neel ME 9 103 Ghos EE
h
RollN Name Branch SPI EmpN Name Branch Exp
o o
101 Raj CE 8 101 Raj CE 8
102 Meet CE 7 102 Meet CE 1
103 Neel ME 9 103 Ghos EE 9
h
Set Operators
▪ Operation: All of these operations take two input relations, which
must be union - compatible:
2. Attributes domain must be compatible

RollN Name Branch SPI EmpN Name Branch Subjec


o o t
101 Raj CE 8 101 Raj CE DBMS
102 Meet CE 7 102 Meet CE DS
103 Neel ME 9 103 Ghos EE EEM
h
RollN Name Branch SPI EmpN Name Branch Exp
o o
101 Raj CE 8 101 Raj CE 8
102 Meet CE 7 102 Meet CE 1
103 Neel ME 9 103 Ghos EE 9
h
Union operator
▪ Symbol: U
▪ Notation: Relation1 U Relation2
▪ Operation: Combine the records from two or more tables (sets) in
to a single table (set), without duplicates.

Student U Faculty
Raj
Student Faculty Suresh
Raj Nitin Meet
Suresh Raj Nitin
Meet Akash Akash
Intersect operator
▪ Symbol: ∩ (Intersection)
▪ Notation: Relation1 ∩ Relation2
▪ Operation: Returns the records which are common from both
relations.

Student Faculty Student ∩ Faculty


Raj Nitin Raj
Suresh Raj
Meet Akash
Minus/Difference Operator
▪ Symbol: −
▪ Notation: Relation1 − Relation2
▪ Operation: Returns all the records from first (left) relation that are
not contained in the second relation.

Student Faculty Student – Faculty –


Raj Nitin Faculty Student
Raj Suresh Nitin
Suresh
Meet Akash
Meet Akash
Rename Operator
▪ Symbol: ρ (Rho)
▪ Notation: ρA (X1,X2….Xn) (Relation)
▪ Operation: It is used to rename a relation or attributes.
Rn Name CP
Student o I
101 Raj 8
102 Meet 9
▪ Find out maximum CPI from student 103 Sures 7
table.
∏CPI (Student) — ∏A.CPI (σ A.CPI<B.CPI (ρA h(Student) X ρB (Student)))
Rename Operator
▪ Example: Student Rn Name CP
o I

ρA (Student) X ρB (Student) 101 Raj 8


102 Meet 9
103 Sures 7
A.Rno A.Name A.CPI B.Rno B.Nameh B.CPI
101 Raj 8 101 Raj 8
101 Raj 8 102 Meet 9
101 Raj 8 103 Suresh 7
102 Meet 9 101 Raj 8
102 Meet 9 102 Meet 9
102 Meet 9 103 Suresh 7
103 Suresh 7 101 Raj 8
103 Suresh 7 102 Meet 9
103 Suresh 7 103 Suresh 7
Rename Operator
▪ Example: Student

σ A.CPI<B.CPI (ρ (Student) X ρ (Student))


A B

A.Rno A.Name A.CPI B.Rno B.Name B.CPI


101 Raj 8 101 Raj 8
101 Raj 8 102 Meet 9
101 Raj 8 103 Suresh 7
102 Meet 9 101 Raj 8
102 Meet 9 102 Meet 9
102 Meet 9 103 Suresh 7
103 Suresh 7 101 Raj 8
103 Suresh 7 102 Meet 9
103 Suresh 7 103 Suresh 7
Rename Operator
▪ Example: Student

σ A.CPI<B.CPI (ρ (Student) X ρ (Student))


A B

A.Rno A.Name A.CPI B.Rno B.Name B.CPI


101 Raj 8 101 Raj 8
101 Raj 8 102 Meet 9
101 Raj 8 103 Suresh 7
102 Meet 9 101 Raj 8
102 Meet 9 102 Meet 9
102 Meet 9 103 Suresh 7
103 Suresh 7 101 Raj 8
103 Suresh 7 102 Meet 9
103 Suresh 7 103 Suresh 7
Rename Operator
▪ Example: Student

σ A.CPI<B.CPI(ρA (Student) X ρB (Student))


A.Rno A.Name A.CPI B.Rno B.Name B.CPI
101 Raj 8 102 Meet 9
103 Suresh 7 101 Raj 8
103 Suresh 7 102 Meet 9
Rename Operator
▪ Example: Student

∏A.CPI (σ A.CPI<B.CPI (ρ (Student) X ρ (Student)))


A B

A.Rno A.Name A.CPI B.Rno B.Name B.CPI


101 Raj 8 102 Meet 9
103 Suresh 7 101 Raj 8
103 Suresh 7 102 Meet 9
Rename Operator
▪ Example: Student

∏A.CPI (σ A.CPI<B.CPI (ρ (Student) X ρ (Student)))


A B

A.CPI
8
7
Rename Operator
▪ Example: Student

∏CPI (Student) ─ ∏A.CPI (σ A.CPI<B.CPI (ρA (Student) X ρB (Student)))

CPI A.CPI CPI


8 8 9
— =
9 7
7
Sub Query
▪ Query in a query.
▪ Sub query is usually added in a where clause.
▪ We will use sub query when we want to search some data using
select query but we don’t know the exact value from data.
▪ Types of sub query,
1. Single row sub query
2. Multiple row sub query
3. Correlated sub query
Single row sub query
▪ Returns 0 or 1 row
▪ Can be used with <,>,<=,>= etc operators.
▪ Example: Find out the name of staff whose salary is maximum.

select staff_name from


staff
where staff_salary=(select max(staff_salary) from staff);

Single row sub query


Multiple row sub query
▪ Returns one or more rows
▪ Can be used with IN, NOT IN, ANY, ALL etc operators.
▪ Example: Find out the name of staff who are from “Computer”
department using sub query.

SELECT * FROM
staff
WHERE dep_id IN
(SELECT dep_id FROM department where dep_id='2');

Multiple row sub query


Correlated sub query
▪ If a sub query references columns in the parent query.
▪ This makes it impossible to evaluate the sub query before
evaluating the parent query.
▪ Example: Find out the name of staff who earn less salary then
average salary.

SELECT staff_name FROM


staff
WHERE staff_salary <
(select avg(staff_salary) from staff);

Correlated sub query

You might also like