Relational Algebra in Database Management System.

Relation Algebra:

It is  a procedure language used to query different relations in a Relational Database. It takes instances of relation as input and gives instances of relation as output, which means the input to every relational algebraic expression will be a relational and output of expression will also be a relation.

Relation algebra provides a mathematical background using different mathematical operators along with relation from a database .

#Relation: Table
#Relationship: Natural Association 

It consists of following basic operators used to query over the relation:

1. SELECTION  (σ)
2. PROJECTION  (∏)
3. UNION  (∪)
4. SET DIFFERENCE  (−)
5. CARTESIAN PRODUCT  (Χ)
6. RENAME  (ρ)

SELECTION  (σ):
 It is a unary operators used to select tuples from a relation. It is define as follow :
σ     (r)
    ρ 

Where σ is operator 
 is relation 
ρ is predicate which specify different condition using relational  operators and joining predicate such as AND ,OR.

Example:
An expression in σ is same as a query in SQL with select *; 
σ(Table_name)
or
SELECT * FROM Table_name.

PROJECTION  (∏)
Projection is a unary operators used to project columns over a relations which means projection is used to select columns from the specify relations.
It is define as follows:

∏                     (r);
    C1, C2 ,.......

is a operator
is relation
C1, C2 and so on are the columns of relation r.

Example:

 ∏                                               (employee_table);
    employee_id, employee_name
or

SELECT employee_id , employee_name 
FROM employee_table;

UNION  (∪):
It is a binary operator that is always takes two relations say r and s where that  r and s must specify the following properties:  

1. Arity (Degree) of r and s must be same i.e number of columns on r and s must be same.

2. Domain of first column in r must be same to domain of first column in s i.e corresponding columns in R and S should have the same domain .

 It is denoted as :

  ∪ s
Where r and s are relation.

Example:
Write an expression to find out employee number if they have either sales more then quota or order amount more than 2500/-.

               (σ  sales > quota(salesrep))     <----- r
      emp_id

 ∏    σ amount > 2500   (orders))   <------ s
     rep 

SET DIFFERENCE  (−):

It is a binary operator which takes two relations r and s and it is denoted as r - s ,where r and s must satisfy the following property:

1. Arity (degree ) of r and s must be same.

2. Domain of the first column of r must be same as first column of s.

3. When r - s is executed , then the output will have all tuples which are there in r but not in s.

Example:

Find out the employee id having sales more then quota and not accepted an order for more than 2500.

              (σ sales > quota (salsrep))
      emp_id 
_

             (σ amount > quota (orders)) ;
      rep


For Cartesian Product and Rename👇                                                                                                              https://languagecopy.blogspot.com/2020/07/cartesian-product-and-rename-concept-in.html

Comments

Popular posts from this blog

Introduction to Python.

Decision Making Statement.