0% found this document useful (0 votes)
12 views17 pages

6 Relational Algebra

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)
12 views17 pages

6 Relational Algebra

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/ 17

EE4202 Database Systems

RELATIONAL ALGEBRA

Procedural Query Language


INTRODUCTION
• It is a procedural query language
• The basic set of operations for the relational data model is known as relational algebra
• Relational algebra is used for implementing and optimizing queries (statements to retrieve data from a
database)
• Some of the relational algebraic operations are embedded in SQL.
Relational Algebra

Set Operations Relational Algebra specific operations


• UNION • SELECT
• INTERSECTION • PROJECT
• SET DIFFERENCE • RENAME
• DIVISION • JOIN
• CARTESIAN (CROSS) PRODUCT
SELECT
Name Department Area Designation Gross Salary Awards
TEACHER
Dushmantha Electrical Software Lecturer 146,000 6
relation
Meranda Electrical Communication Senior Lecturer 220,000 1
Sajitha Electrical Software Senior Lecturer 240,000 3
Kaman Electrical Communication Senior Lecturer 220,000 1
Banjan Mechanical Material Lecturer 146,000 3
Paridu Electrical Software Lecturer 146,000 1
Sithum Electrical Electronics Lecturer 146,000 0

• Select operation extract tuples from a relation based on meeting a given condition.
• Represented using the sigma ( ) symbol
•  (Relational algebraic expression whose result is a relation)
<selection condition>

• Selection condition is a condition applied on attributes.


• Ex:  Area=Communication(TEACHER)
• Will retrieve all the tuples from TEACHER relation whose Area is equal to Communication.
Name Department Area Designation Gross Salary Awards
Meranda Electrical Communication Senior Lecturer 220,000 1
Kaman Electrical Communication Senior Lecturer 220,000 1
SELECT
 The degree is the number of attributes of the result of relational algebraic operation.
 The degree of a SELECT operation is always equal to degree of the original relation.
 SELECT operation is commutative. That is two select operations can be interchanged without affecting the final
result.

 Ex:  (  (TEACHER)) =  (
Designation=Lecturer Awards>5 (TEACHER))
Awards>5 Designation=Lecturer

Name Department Area Designation Gross Salary Awards

Name Department Area Designation Gross Awards Dushmantha Electrical Software Lecturer 146,000 6
Salary
Dushmantha Electrical Software Lecturer 146,000 6 Banjan Mechanical Material Lecturer 146,000 3

Paridu Electrical Software Lecturer 146,000 1

Sithum Electrical Electronics Lecturer 146,000 0

Name Department Area Designation Gross Awards Name Department Area Designation Gross Awards
Salary Salary
Dushmantha Electrical Software Lecturer 146,000 6 Dushmantha Electrical Software Lecturer 146,000 6
PROJECT
Name Department Area Designation Gross Salary Awards
TEACHER
relation Dushmantha Electrical Software Lecturer 146,000 6
Meranda Electrical Communication Senior Lecturer 220,000 1
Sajitha Electrical Software Senior Lecturer 240,000 3
Kaman Electrical Communication Senior Lecturer 220,000 1
Banjan Mechanical Material Lecturer 146,000 3
Paridu Electrical Software Lecturer 146,000 1 π Name,Area (TEACHER)
Sithum Electrical Electronics Lecturer 146,000 0

• Project operation extracts specified attributes (columns) of all tuples from a relation
Name Area
• Represented using the pi (π) symbol
Dushmantha Software
• π<Set of Attributes> (Relational algebraic expression whose result is a relation)
Meranda Communication
• Ex: π Name,Area (TEACHER) Sajitha Software
Will retrieve all the tuples from TEACHER relation as a new relation with Kaman Communication
all tuples from the original relation, but having attributes Name and Area Banjan Material
only. Paridu Software
• Here, Degree = No. of Attributes of project operation Sithum Electronics
RENAME
TEACHER Name Department Area Designation Gross Salary Awards
relation Dushmantha Electrical Software Lecturer 146,000 6
Meranda Electrical Communication Senior Lecturer 220,000 1
Sajitha Electrical Software Senior Lecturer 240,000 3
Kaman Electrical Communication Senior Lecturer 220,000 1
Banjan Mechanical Material Lecturer 146,000 3
Paridu Electrical Software Lecturer 146,000 1
Sithum Electrical Electronics Lecturer 146,000 0
NEW_RELATION relation
• Rename operation renames a relation or/and attributes of the relation.
• Represented using the rho (ρ) or arrow ( ) symbol
Teaching_area Title
• Ex: ρNEW_RELATION(Teachning_area,Title) ( π Area, Designation(TEACHER)
) Software Lecturer
Communication Senior Lecturer
(π Area, Designation(TEACHER))
• or NEW_RELATION(Teaching_area, Title) Software Senior Lecturer
Material Lecturer
Electronics Lecturer
CARTESIAN PRODUCT
TEACHER DEPARTMENT
Name Department Area Designation Gross Salary Awards Dept_ID Head_name Location Accreditation
Dushmantha Electrical Software Lecturer 146,000 6 ElE_1 Sajitha Sector B Yes
Meranda Electrical Communication Senior Lecturer 220,000 1 MME_1 Randitha Sector A Yes

(TEACHER) X (DEPARTMENT)

Name Department Area Designation Gross Salary Awards Dept_ID Head_name Location Accreditation

Dushmantha Electrical Software Lecturer 146,000 6 ElE_1 Sajitha Sector B Yes

Meranda Electrical Communication Senior Lecturer 220,000 1 ElE_1 Sajitha Sector B Yes

Dushmantha Electrical Software Lecturer 146,000 6 MME_1 Randitha Sector A Yes

Meranda Electrical Communication Senior Lecturer 220,000 1 MME_1 Randitha Sector A Yes

• Also known as the cross product or cross join operation.


• Represented using the cross (x)
• Ex: (TEACHER) X (DEPARTMENT)
• If R1 and R2 are relations having t1 and t2 tuples respectively and a1 and a2 amount of attributes, resulting
relation consists of t1xt2 tuples and a1+a2 attributes.
Name
TEACHER
Department Area Designation
INNER JOIN
Gross Salary Awards
DEPARTMENT
Dept_ID Head_name Location Accreditation
Dushmantha Electrical Software Lecturer 146,000 6 ElE_1 Sajitha Sector B Yes
Meranda Electrical Communication Senior Lecturer 220,000 1
MME_1 Randitha Sector A Yes
Sajitha Electrical Software Senior Lecturer 240,000 3
CEE_1 Layanthi Sector C Yes
Kaman Electrical Communication Senior Lecturer 220,000 1
Banjan Mechanical Material Lecturer 146,000 3
Paridu Electrical Software Lecturer 146,000 1
Sithum Electrical Electronics Lecturer 146,000 0
TEACHER Name = Head_name DEPARTMENT
Name Department Area Designation Gross Salary Awards Dept_ID Head_name Location Accreditation
Sajitha Electrical Software Senior Lecturer 240,000 3 EIE_1 Sajitha Sector B Yes

• Combines related tuples from two relations based on a common attribute.


• Represented using the symbol.
• Generic form is Relation1 <Join conditions> Relation2
• Join condition can be (Relation1_Attribute) Ɵ (Relation2_Attribute) where Ɵ is a comparison operator ( = , !=,
> , <, <=, >=).
• If only = comparison operator is used in the join conditions, that join operation is know as an EQUIJOIN.
• Equijoin always has two or more attributes which values are identical.
• Ex: TEACHER Name = Head_name DEPARTMENT

INNER JOIN
TEACHER DEPARTMENT
Name Department Area Designation Gross Salary Awards Dept_ID Head_name Location Accreditation
Dushmantha Electrical Software Lecturer 146,000 6 ElE_1 Sajitha Sector B Yes
Meranda Electrical Communication Senior Lecturer 220,000 1 MME_1 Randitha Sector A Yes
Sajitha Electrical Software Senior Lecturer 240,000 3
CEE_1 Layanthi Sector C Yes
Kaman Electrical Communication Senior Lecturer 220,000 1
Banjan Mechanical Material Lecturer 146,000 3
Paridu Electrical Software Lecturer 146,000 1
Sithum Electrical Electronics Lecturer 146,000 0 TEACHER Name = Head_name DEPARTMENT

Name Department Area Designation Gross Salary Awards Dept_ID Head_name Location Accreditation
Sajitha Electrical Software Senior Lecturer 240,000 3 EIE_1 Sajitha Sector B Yes

• A join can be considered as a cartesian product operation followed by a select operation.


• Relation1 (TEACHER) X (DEPARTMENT)
• Relation2  (Relation1)
Name = Head_name

• Relation2 is same as the relation obtained from join operation.


SET OPERATIONS
TEACHER Name Department Area Designation Gross Salary Awards
Dushmantha Electrical Software Lecturer 146,000 6
Meranda Electrical Communication Senior Lecturer 220,000 1
Sajitha Electrical Software Senior Lecturer 240,000 3
Kaman Electrical Communication Senior Lecturer 220,000 1
Banjan Mechanical Material Lecturer 146,000 3
Paridu Electrical Software Lecturer 146,000 1
Sithum Electrical Electronics Lecturer 146,000 0

R2  (TEACHER)
R1  Area=Communication (TEACHER)
Awards<3

Name Department Area Designation Gross Salary Awards


Name Department Area Designation Gross Salary Awards
Meranda Electrical Communication Senior Lecturer 220,000 1
Meranda Electrical Communication Senior Lecturer 220,000 1
Kaman Electrical Communication Senior Lecturer 220,000 1
Kaman Electrical Communication Senior Lecturer 220,000 1
Paridu Electrical Software Lecturer 146,000 1
Sithum Electrical Electronics Lecturer 146,000 0

• Inner UNION, INTERSECTION and MINUS are the basic set operations from the set theory applied in relational
algebra.
• These are binary operators meaning that only two relations are involved.
• Two relations must be type compatible which means they should have same degree (no. of attributes) and
domain of corresponding attributes must be same. Ex: R1 and R2 are type compatible
R1  Area=Communication(TEACHER)
INNER UNION R2  Awards<3(TEACHER)

Name Department Area Designation Gross Salary Awards Name Department Area Designation Gross Salary Awards
Meranda Electrical Communication Senior Lecturer 220,000 1 Meranda Electrical Communication Senior Lecturer 220,000 1
Kaman Electrical Communication Senior Lecturer 220,000 1 Kaman Electrical Communication Senior Lecturer 220,000 1
Paridu Electrical Software Lecturer 146,000 1
Sithum Electrical Electronics Lecturer 146,000 0

R1 U R2
Name Department Area Designation Gross Salary Awards
Meranda Electrical Communication Senior Lecturer 220,000 1
Kaman Electrical Communication Senior Lecturer 220,000 1
Paridu Electrical Software Lecturer 146,000 1
Sithum Electrical Electronics Lecturer 146,000 0

• Inner UNION of type compatible relations A and B is the collection of all tuples that are either in A and B and in
both A and B
• Represent by U operator. Resulting relation does not have duplicate tuples
• Ex: R1 U R2
• Union operation is commutative.
INTERSECTION
R1  (TEACHER)
Area=Communication R2  Awards<3(TEACHER)
Name Department Area Designation Gross Salary Awards Name Department Area Designation Gross Salary Awards
Meranda Electrical Communication Senior Lecturer 220,000 1 Meranda Electrical Communication Senior Lecturer 220,000 1

Kaman Electrical Communication Senior Lecturer 220,000 1 Kaman Electrical Communication Senior Lecturer 220,000 1
Paridu Electrical Software Lecturer 146,000 1
Sithum Electrical Electronics Lecturer 146,000 0

R1 R2
Name Department Area Designation Gross Salary Awards
Meranda Electrical Communication Senior Lecturer 220,000 1

Kaman Electrical Communication Senior Lecturer 220,000 1

• INTERSECTION of type compatible relations A and B is the collection of all tuples that are only in both A and B
• Represent by operator. Resulting relation does not have duplicate tuples
• Ex: R1 R2
• Intersection operation is commutative.
MINUS/SET DIFFERENCE
R1  (TEACHER)
Area=Communication R2  Awards<3(TEACHER)
Name Department Area Designation Gross Salary Awards Name Department Area Designation Gross Salary Awards
Meranda Electrical Communication Senior Lecturer 220,000 1 Meranda Electrical Communication Senior Lecturer 220,000 1
Kaman Electrical Communication Senior Lecturer 220,000 1 Kaman Electrical Communication Senior Lecturer 220,000 1
Paridu Electrical Software Lecturer 146,000 1
Sithum Electrical Electronics Lecturer 146,000 0

R1 - R2
R2 - R1

Name Department Area Designation Gross Salary Awards Name Department Area Designation Gross Salary Awards
Paridu Electrical Software Lecturer 146,000 1
Sithum Electrical Electronics Lecturer 146,000 0

• For type compatible relations A and B, A MINUS B is the set of all tuples in A that are not in B.
• Represent by - operator.
• Set difference is not commutative.
• Ex: (R1 – R2) != (R2 – R1)
DIVISION
R1 (Z)
Name (Y) Module (X)
R2 (Y)
Meranda EE6201 Name (Y)
Kaman EE6201 Meranda
Dushmantha EE4202 Kaman
Sithum EE3301
Meranda EE4201
Dushmantha EE6206 R (X) = R1 (Z) ÷ R2 (Y)
Kaman EE2301
Sajitha EE4301
Module (X)
Meranda EE8201
Sithum EE4102 EE6201
Kaman EE8201
EE8201

• If R1 (Z) and R2 (Y) are relations with attributes Z and Y such that Z = (X U Y), then R1 (Z) ÷ R2 (Y) produces
relation R (X) which contains all tuples t (X) that appear in R1 (Z) in combination with every tuple of R2 (Y).
• Represent by ÷ operator.
• Not commutative
• Ex: R (X) = R1 (Z) ÷ R2 (Y)
RELATIONAL ALGEBRA - Extensions
TEACHER
OUTER JOIN DEPARTMENT
Name Department Area Designation Gross Salary Awards Dept_ID Head_name Location Accreditation
Dushmantha Electrical Software Lecturer 146,000 6 ElE_1 Sajitha Sector B Yes
Meranda Electrical Communication Senior Lecturer 220,000 1 MME_1 Randitha Sector A Yes
Sajitha Electrical Software Senior Lecturer 240,000 3 CEE_1 Layanthi Sector C Yes

TEACHER Name = Head_name DEPARTMENT


Name Department Area Designation Gross Salary Awards Dept_ID Head_name Location Accreditation
Dushmantha Electrical Software Lecturer 146,000 6 NULL NULL NULL NULL
Meranda Electrical Communication Senior Lecturer 220,000 1 NULL NULL NULL NULL
Sajitha Electrical Software Senior Lecturer 240,000 3 ElE_1 Sajitha Sector B Yes

TEACHER Name = Head_name DEPARTMENT


Name Department Area Designation Gross Salary Awards Dept_ID Head_name Location Accreditation
Sajitha Electrical Software Senior Lecturer 240,000 3 ElE_1 Sajitha Sector B Yes
NULL NULL NULL NULL NULL NULL MME_1 Randitha Sector A Yes
NULL NULL NULL NULL NULL NULL CEE_1 Layanthi Sector C Yes

 Natural Join and Ɵ join are all inner joins which only joins tuples with matching condition.
 In outer join, tuples from one or both relations appear in the result which do not meet the join condition.
 Outer join is named according to the relation in which all tuples are kept.
 Left outer join keeps all tuples of the left/first relation regardless of join condition. Unmatched tuples in the second/right
relation is padded with nulls. Same is true for right outer join
 In full outer join, all tuples from left and right relations are kept. If no matching condition is found, the corresponding
entries will be padded with null values.
OUTER JOIN
TEACHER(R1) MANAGER(R2)
Name Department Area Designation Name Department Head_ID Experience
Dushmantha Electrical Software Lecturer Sajitha Electrical H005 16
R1 outer union R2
Meranda Electrical Communication Senior Lecturer Ranjitha Mechanical H002 18
Sajitha Electrical Software Senior Lecturer Layanthi Civil H003 13

Name Department Area Designation Hed_ID Experience


Dushmantha Electrical Software Lecturer NULL NULL
Meranda Electrical Communication Senior Lecturer NULL NULL
Sajitha Electrical Software Senior Lecturer H005 16
Randitha Mechanical NULL NULL H002 18
Layanthi Civil NULL NULL H003 13

• Inner UNION is defined for type compatible relations while outer UNION is defined for relations consisting of
some common attributes but not type compatible. i.e. partially compatible
• Two tuples in partially type compatible relations are said to match if their common attributes are equal and will
have values for all its attributes.
• In outer union result, matching tuples appear only once.

You might also like