Module-2 Relational Algebra - Presentation

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 31

Department of Information Science & Engineering

Acharya Institute of Technology

- Nurturing Aspirations & Supporting Growth

Relational
Algebra

C K Marigowda | Associate Professor & HOD | Information Science & Engineering | Acharya Institute of Technology |
marigowda@acharya.ac.in
Outline
▪ Unary Relational Operations
- SELECT operation
- PROJECT operation
- Sequences of unary operations
-RENAME operation
▪ Binary Relational Operations
- UNION operation
- INTERSECTION operation
- SET DIFFERENCE operation
Confidentiality
- CARTESIAN PRODUCT operation
- JOIN operation
Integrity
- DIVISION operation
▪ AGGREGATE
AvailabilityFunction
Unary Relational
Operations

C K Marigowda | Associate Professor & HOD | Information Science & Engineering | Acharya Institute of Technology |
marigowda@acharya.ac.in
COMPANY Database
Unary Relational Operations
Unary operations take one relation, and return one
relation:

– SELECT operation

– PROJECT operation

– Sequences of unary operations

– RENAME operation
The SELECT Operation
▪ It selects a subset of tuples from a relation that
satisfy a SELECT condition

▪ The SELECT operation is denoted by:


 <Selection condition> (R)

▪ The selection condition is a Boolean expression


specified on the attributes of relation R

▪ Query: Select the details of the Ashok


 Name=‘Ashok’ (EMPLOYEE)
Queries on SELECT Operation
▪ Query: Select the employees who works for
department number 2
 Dno=2 (EMPLOYEE)
▪ Query: Select the employees whose salary is greater
than 30000
 Salary>30000 (EMPLOYEE)
▪ Query: Select the tuples of all employee who either
work in department 1 and salary greater than 28000
or work in department 2 and salary greater than
31000
 (Dno=1 AND Salary>28000 OR (Dno=2) AND
Salary>31000 ) (EMPLOYEE)
The PROJECT Operation
• The PROJECT operation , Selects certain columns
from a relation and discards the columns that are
not in the PROJECT list

• The PROJECT operation is denoted by:


 <attribute list> ( R )

Where attribute-list is a list of attributes from the


relation R

Query: Retrieve the Name and Salaries of all the


employees
 Name, Salary ( EMPLOYEE )
Results of SELECT and PROJECT operations

Query: Retrieve the Name of the of the employee who work


for department number 1 and whose salary is greater than
27000
R1 (DNO=1 AND SALARY>27000) (EMPLOYEE)
Result   NAME (R1)

OR
Result  NAME ( (DNO=1 AND SALARY>27000) (EMPLOYEE))
Binary Relational
Operations

C K Marigowda | Associate Professor & HOD | Information Science & Engineering | Acharya Institute of Technology |
marigowda@acharya.ac.in
Binary Relational Operations

- UNION operation

- INTERSECTION operation

- SET DIFFERENCE operation

- CARTESIAN PRODUCT operation

- JOIN operation

- DIVISION operation
Confidentiality

Integrity

Availability
Binary Relational Operations
Two relations R1 and R2 are said to be union compatible
if they have the same degree and all their attributes
(correspondingly) have the same domain

▪ The UNION, INTERSECTION, and SET DIFFERENCE


operations are applicable on union compatible
relations

▪ The resulting relation has the same attribute names as


the first relation
The UNION operation
▪ The result of UNION operation on two relations, R1
and R2, is a relation, R3 , that includes all tuples that
are either in R1, or in R2 , or in both R1 and R2.

• The UNION operation is denoted by:

R3 = R1  R2

• The UNION operation eliminates duplicate tuples


Example
Results of the UNION operation
RESULT = RESULT1  RESULT2
The INTERSECTION operation

• The result of INTERSECTION operation on two


relations, R1 and R2, is a relation, R3, that includes all
tuples that are in both R1 and R2

• The INTERSECTION operation is denoted by:

R3 = R1  R2

• The both UNION and INTERSECTION operations are


commutative and associative operations
The SET DIFFERENCE Operation

• The result of SET DIFFERENCE operation on two


relations, R1 and R2, is a relation, R3, that includes all
tuples that are in R1 but not in R2

• The SET DIFFERENCE operation is denoted by:

R3 = R1 – R2

• The SET DIFFERENCE (or MINUS) operation is not


commutative
Example
(a) Two relations STUDENT, INSTRUCTOR
(b) STUDENT  INSTRUCTOR
(c) STUDENT  INSTRUCTOR
(d) STUDENT – INSTRUCTOR
(e) INSTRUCTOR – STUDENT
The CARTESIAN PRODUCT Operation

• This operation also known as CROSS PRODUCT or CROSS


JOIN denoted by:

R3 = R1 X R2

• The resulting relation, R3, includes all combined tuples


from two relations R1 and R2

• Degree (R3) = Degree (R1) + Degree (R2)

|R3| = |R1|  |R2|


Example
The CARTESIAN PRODUCT (CROSS PRODUCT)

R1 R2
FN LN FNAME LNAME
ABC A XYZ X
BCD D ACH M

R = R1 X R2
R
FN LN FNAME LNAME
ABC A XYX X
ABC A ACH M
BCD D XYZ X
BCD D ACH M
JOIN Operations
• An important operation for any relational database is
the JOIN operation, because it enables us to combine
related tuples from two relations into single tuple

• The JOIN operation is denoted by:

R = R1 ⋈ <join condition> R2

• The degree of resulting relation is

degree(R1) + degree(R2)
Example
Result of the JOIN operation

DEPT_MGR  DEPARTMENT ⋈ MGRSSN=SSN EMPLOYEE

Name SSN DOB Salary Address SuperSSN DNo DName DNumber MGRSSN
Ashok 123456789 20-May-70 25000 JP Nagar, 103456789 1 ES 1 123456789
Bangalore
Raju 103456789 10-Oct-80 30000 Vijayanagar, 103456789 2 DD 2 103456789
Bangalore
The JOIN Operation
• The difference between CARTESIAN PRODUCT and
JOIN is that the resulting relation from JOIN consists
only those tuples that satisfy the join condition

• The JOIN operation is equivalent to CARTESIAN


PRODUCT and then SELECT operation on the result
of CARTESIAN PRODUCT operation, if the
select-condition is the same as the join condition
CARTESIAN PRODUCT and JOIN

DEPT_MGR_1  DEPARTMENT X EMPLOYEE


DEPT_MGR   MGRSSN=SSN DEPT_MGR_1
Name SSN DOB Salary Address SuperSSN DNo DName DNumber MGRSSN
Ashok 123456789 20-May-70 25000 JP Nagar, 103456789 1 ES 1 123456789
Bangalore
Ashok 123456789 20-May-70 25000 JP Nagar, 103456789 1 DD 2 103456789
Bangalore
Raju 103456789 10-Oct-80 30000 Vijayanagar, 103456789 2 ES 1 123456789
Bangalore
Raju 103456789 10-Oct-80 30000 Vijayanagar, 103456789 2 DD 2 103456789
Bangalore
Smith 120456789 1-Jan-80 30000 Soldevanahalli, 120456789 1 ES 1 123456789
Bangalore
Smith 120456789 1-Jan-80 30000 Soldevanahalli, 120456789 1 DD 2 103456789
Bangalore
The EQUIJOIN Operation

• If the JOIN operation has equality comparison only


(that is, = operation), then it is called an EQUIJOIN
operation

• In the resulting relation on an EQUIJOIN operation, we


always have one or more pairs of attributes that have
identical values in every tuples
The NATURAL JOIN Operation

• In EQUIJOIN operation, if the two attributes in the join


condition have the same name, then in the resulting
relation we will have two identical columns. In order
to avoid this problem, we define the NATURAL JOIN
operation

• The NATURAL JOIN operation is denoted by:

R3 = R1 * R2

• In R3 only one of the duplicate attributes from the list


are kept
Example
Result of the NATURAL JOIN operation

DEPARTMENT DName DNumber SSN


ES 1 123456789
DD 2 103456789

DEPT_MGR  DEPARTMENT * EMPLOYEE

Name SSN DOB Salary Address SuperSSN DNo DName DNumber


Ashok 123456789 20-May-70 25000 JP Nagar, 103456789 1 ES 1
Bangalore
Raju 103456789 10-Oct-80 30000 Vijayanagar, 103456789 2 DD 2
Bangalore
The DIVISION Operation

• The DIVISION operation is useful for some queries. For


example, “Retrieve the name of employees who
work on all the projects that ‘Smith’ works on.”

• The Division operation is denoted by:

R3 = R1 ÷ R 2

• In general, attributes in R2 are a subset of attributes in


R1
Example

SSN_PNOS ÷ SMITH_PNOS
Aggregate
functions

C K Marigowda | Associate Professor & HOD | Information Science & Engineering | Acharya Institute of Technology |
marigowda@acharya.ac.in
Example
The AGGREGATE FUNCTION operation
Thank you
marigowda@acharya.ac.in
www.acharya.ac.in

You might also like