Search

Thursday, January 24, 2019

Relational Algebra in DBMS with Examples

Posted By Manisha Gupta

What is Relational Algebra?

Relational algebra is a widely used procedural query language. It collects instances of relations as input and gives occurrences of relations as output. It uses various operation to perform this action.
Relational algebra operations are performed recursively on a relation. The output of these operations is a new relation, which might be formed from one or more input relations.
In this tutorial, you will learn:
  • What is Relational Algebra?
  • SELECT(σ)
  • Projection(π)
  • Union operation (υ)
  • Set Difference (-)
  • Intersection
  • Cartesian product(X)
  • Join Operations
  • Inner Join:
  • Theta Join:
  • EQUI join:
  • NATURAL JOIN (⋈)
  • OUTER JOIN
  • Left Outer Join(A  B)
  • Right Outer Join: ( A  B )
  • Full Outer Join: ( A  B)

Basic Relational Algebra Operations:

Relational Algebra devided in various groups

Unary Relational Operations

  • SELECT (symbol: σ)
  • PROJECT (symbol: π)
  • RENAME (symbol: )

Relational Algebra Operations From Set Theory

  • UNION (υ)
  • INTERSECTION ( ),
  • DIFFERENCE (-)
  • CARTESIAN PRODUCT ( x )

Binary Relational Operations

  • JOIN
  • DIVISION
Let's study them in detail:

SELECT (σ)

The SELECT operation is used for selecting a subset of the tuples according to a given selection condition. Sigma(σ)Symbol denotes it. It is used as an expression to choose tuples which meet the selection condition. Select operation selects tuples that satisfy a given predicate.
σp(r)
σ is the predicate
r stands for relation which is the name of the table
p is prepositional logic
Example 1
σ topic = "Database" (Tutorials)
Output - Selects tuples from Tutorials where topic = 'Database'.
Example 2
σ topic = "Database" and author = "guru99"( Tutorials)
Output - Selects tuples from Tutorials where the topic is 'Database' and 'author' is guru99.
Example 3
σ sales > 50000 (Customers)
Output - Selects tuples from Customers where sales is greater than 50000

Projection(π)

The projection eliminates all attributes of the input relation but those mentioned in the projection list. The projection method defines a relation that contains a vertical subset of Relation.
This helps to extract the values of specified attributes to eliminates duplicate values. (pi) The symbol used to choose attributes from a relation. This operation helps you to keep specific columns from a relation and discards the other columns.
Example of Projection:
Consider the following table
CustomerIDCustomerNameStatus
1GoogleActive
2AmazonActive
3AppleInactive
4AlibabaActive
Here, the projection of CustomerName and status will give
Π CustomerName, Status (Customers)
CustomerNameStatus
GoogleActive
AmazonActive
AppleInactive
AlibabaActive

Union operation (υ)

UNION is symbolized by ∪ symbol. It includes all tuples that are in tables A or in B. It also eliminates duplicate tuples. So, set A UNION set B would be expressed as:
The result <- A ∪ B
For a union operation to be valid, the following conditions must hold -
  • R and S must be the same number of attributes.
  • Attribute domains need to be compatible.
  • Duplicate tuples should be automatically removed.
Example
Consider the following tables.
Table A
Table B
column 1column 2column 1column 2
1111
1213
A ∪ B gives
Table A ∪ B
column 1column 2
11
12
13

Set Difference (-)

- Symbol denotes it. The result of A - B, is a relation which includes all tuples that are in A but not in B.
  • The attribute name of A has to match with the attribute name in B.
  • The two-operand relations A and B should be either compatible or Union compatible.
  • It should be defined relation consisting of the tuples that are in relation A, but not in B.
Example
A-B
Table A - B
column 1column 2
12

Intersection

An intersection is defined by the symbol ∩
A ∩ B
Defines a relation consisting of a set of all tuple that are in both A and B. However, A and B must be union-compatible.
Example:
A ∩ B
Table A ∩ B
column 1column 2
11

Cartesian product(X)

This type of operation is helpful to merge columns from two relations. Generally, a Cartesian product is never a meaningful operation when it performs alone. However, it becomes meaningful when it is followed by other operations.
Example – Cartesian product
σ column 2 '1' (A X B)
Output – The above example shows all rows from relation A and B whose column 2 has value 1
σ column 2 = '1' (A X B)
column 1column 2
11
11

Join Operations

Join operation is essentially a cartesian product followed by a selection criterion.

Join operation denoted by ⋈.
JOIN operation also allows joining variously related tuples from different relations.
Types of JOIN:
Various forms of join operation are:
Inner Joins:
  • Theta join
  • EQUI join
  • Natural join
Outer join:
  • Left Outer Join
  • Right Outer Join
  • Full Outer Join

Inner Join:

In an inner join, only those tuples that satisfy the matching criteria are included, while the rest are excluded. Let's study various types of Inner Joins:

Theta Join:

The general case of JOIN operation is called a Theta join. It is denoted by symbol θ
Example
A ⋈θ B
Theta join can use any conditions in the selection criteria.
For example:
A ⋈ A.column 2 >  B.column 2 (B)
A ⋈ A.column 2 > B.column 2 (B)
column 1column 2
12

EQUI join:

When a theta join uses only equivalence condition, it becomes a equi join.
For example:
A ⋈ A.column 2 =  B.column 2 (B)
A ⋈ A.column 2 = B.column 2 (B)
column 1column 2
11
EQUI join is the most difficult operations to implement efficiently in an RDBMS and one reason why RDBMS have essential performance problems.

NATURAL JOIN (⋈)

Natural join can only be performed if there is a common attribute (column) between the relations. The name and type of the attribute must be same.
Example
Consider the following two tables
C
NumSquare
24
39
D
NumCube
28
318
C ⋈ D
C ⋈ D
NumSquareCube
244
399

OUTER JOIN

In an outer join, along with tuples that satisfy the matching criteria, we also include some or all tuples that do not match the criteria.

Left Outer Join(A  B)

In the left outer join, operation allows keeping all tuple in the left relation. However, if there is no matching tuple is found in right relation, then the attributes of right relation in the join result are filled with null values.
Consider the following 2 Tables
A
NumSquare
24
39
416
B
NumCube
28
318
575
A  B
A ⋈ B
NumSquareCube
244
399
416-

Right Outer Join: ( A  B )

In the right outer join, operation allows keeping all tuple in the right relation. However, if there is no matching tuple is found in the left relation, then the attributes of the left relation in the join result are filled with null values.
A  B
A ⋈ B
NumCubeSquare
284
3189
575-

Full Outer Join: ( A  B)

In a full outer join, all tuples from both relations are included in the result, irrespective of the matching condition.
A  B
A ⋈ B
NumCubeSquare
248
3918
416-
5-75

Summary

Operation
Purpose
Select(𝛔)
The SELECT operation is used for selecting a subset of the tuples according to a given selection condition
Projection(π)
The projection eliminates all attributes of the input relation but those mentioned in the projection list.
Union Operation(∪)
UNION is symbolized by symbol. It includes all tuples that are in tables A or in B.
Set Difference(-)
- Symbol denotes it. The result of A - B, is a relation which includes all tuples that are in A but not in B.
Intersection(∩)
Intersection defines a relation consisting of a set of all tuple that are in both A and B.
Cartesian Product(X)
Cartesian operation is helpful to merge columns from two relations.
Inner Join
Inner join, includes only those tuples that satisfy the matching criteria.
Theta Join(θ)
The general case of JOIN operation is called a Theta join. It is denoted by symbol θ.
EQUI Join
When a theta join uses only equivalence condition, it becomes a equi join.
Natural Join(⋈)
Natural join can only be performed if there is a common attribute (column) between the relations.
Outer Join
In an outer join, along with tuples that satisfy the matching criteria.
Left Outer Join( )
In the left outer join, operation allows keeping all tuple in the left relation.
Right Outer join()
In the right outer join, operation allows keeping all tuple in the right relation.
Full Outer Join()
In a full outer join, all tuples from both relations are included in the result irrespective of the matching condition.

1 comments:

Follow on Facebook

ManishaTech . 2017 Copyright. All rights reserved. Designed by Manisha Gupta | Manisha Gupta