RELATIONAL ALGEBRA-DBMS
RELATIONAL ALGEBRA-DBMS
It is also a conceptual language. By this we mean that the queries made in it are not
run on the computer so it is not used as a business language. However, this
knowledge allows us to understand the optimization and query execution of RDBMS.
o UNION
o INTERSECTION
o DIFFERENCE
o CARTERSION PRODUCT
Following conditions are satisfied is both the relations are union
o JOIN
o SELCTION
o Projection
o Division
Let's explain each one by one in detail.
1. Select Operation:
Input:
1. σ BRANCH_NAME="perryride" (LOAN)
Output:
2. Project Operation:
o This operation shows the list of those attributes that we wish to appear in the
result. Rest of the attributes are eliminated from the table.
o It is denoted by ∏ and the attributes to be retrieved appear as subscripts
separated by commas and relations name is given in parenthesis following the
PI.
1. Notation: ∏ A1, A2, An (r)
Where
Input:
NAME CITY
Jones Harrison
Smith Rye
Hays Harrison
Curry Rye
Johnson Brooklyn
Brooks Brooklyn
3. Union Operation:
o The Union operation of two relations results in a new relation containing rows
from both relations with duplicates removed.
o Suppose there are two tuples R and S. The union operation contains all the
tuples that are either in R or S or both in R & S.
o It eliminates the duplicate tuples. It is denoted by ∪.
1. Notation: R ∪ S
A union operation must hold the following condition:
Example:
DEPOSITOR RELATION
CUSTOMER_NAME ACCOUNT_NO
Johnson A-101
Smith A-121
Mayes A-321
Turner A-176
Johnson A-273
Jones A-472
Lindsay A-284
BORROW RELATION
CUSTOMER_NAME LOAN_NO
Jones L-17
Smith L-23
Hayes L-15
Jackson L-14
Curry L-93
Smith L-11
Williams L-17
Input:
CUSTOMER_NAME
Johnson
Smith
Hayes
Turner
Jones
Lindsay
Jackson
Curry
Williams
Mayes
4. Intersection:
o Suppose there are two tuples R and S. The set intersection operation contains
all tuples that are in both R & S.
o It is denoted by intersection ∩.
1. Notation: R ∩ S
Features of Intersection operation:
Input:
CUSTOMER_NAME
Smith
Jones
5. Difference:
o The difference of two relations results in a new relation that contains tuples
that occur in the first relation but not in the second relation.
o Suppose there are two tuples R and S. The set difference operation contains
all tuples that are in R but not in S.
o It is denoted by intersection minus (-).
1. Notation: R - S
Example: Using the above DEPOSITOR table and BORROW table
Input:
CUSTOMER_NAME
Jackson
Hayes
Willians
Curry
But writing the equation with a single intersection operation is more convenient than
involving a pair of difference operations. Here R and S unions are favorable.
6. Cartesian product
o The Cartesian product is used to combine each row in one table with each row
in the other table. It is also known as a cross product.
o It is denoted by X.
o It is a binary relation which means that it always operates on two relations.
1. Notation: E X D
Example:
EMPLOYEE
EMP_ID EMP_NAME EMP_DEPT
1 Smith A
2 Harry C
3 John B
DEPARTMENT
DEPT_NO DEPT_NAME
A Marketing
B Sales
C Legal
Input:
1. EMPLOYEE X DEPARTMENT
Output:
1 Smith A A Marketing
1 Smith A B Sales
1 Smith A C Legal
2 Harry C A Marketing
2 Harry C B Sales
2 Harry C C Legal
3 John B A Marketing
3 John B B Sales
3 John B C Legal
o The resulting relation may have duplicate properties if some properties of the
two relations are defined on common domains.
o The resulting degree of action is equal to the sum of the degrees of all
relations
Degree of E = Degree of E + Degree of D
7. Rename Operation:
The rename operation is used to rename the output relation. It is denoted by rho (ρ).
If no rename operation is applied then the names of the attributes in a resulting
relation are the same as those in the original relation and in the same order.
Example: We can use the rename operator to rename STUDENT relation to
STUDENT1.
1. ρ(STUDENT1, STUDENT)
Note: Apart from these common operations Relational algebra can be used in
Join operations.
8. Join Operation:
A join operation combines two or more relations to form a new relation such that
new relation contains only those tuples from different relations that satisfy the
specified criteria. It forms a new relation which contains all the attributes from both
the joined relations whose tuples are those defined by the restrictions applies i.e. the
join condition applied on two participating relations. The join is performed on two
relations, who have one or more attributes in common. These attributes must be
domain compatible i.e. they have same data type. It is a binary operation. The Join
operation is denoted by a Join symbol. The general from of representing a join
operation on two relations P and Q is
1. P⋈ <join_condion> Q
When we use equality operator in the join condition then such a join is called EQUI
Join. It is mostly commonly used Join.
EMP Table
Dept_No Dname
1 Marketing
2 Purchase
3 Finance
4 Packing
5 Marketing
In the above both table, suppose we want to know the employee information with
department name in which each employee is working. Now the employee
information is in the Emp relation and Department name information is in dept
relation. So to retrieve the columns from both the tables at same time, we need to
join the EMP and DEPT relations. The relations can be joined over the column
Dept_ID that exist in EMP relation and the Dept_no that exist in the DEPT relation and
are domain compatible. Thus the result of EQUI Join where the condition is that
Dept_Id attributes values the EMP relation should be equal to the Dept_No attribute
values in the DEPT relation, the result is shown below.
Another is known as natural join in which there is no need to explicitly name the
columns. A join is performed by joining all columns from the first relation of any
column to another relation with the same name. The result of natural join is as
follows:
The natural join may also been referred to as INNER JOIN. Another type of JOIN in
which a relation is joined to itself by comparing values with a column of the relation
is called a self-join.
101 Raj -
104 Ravi -
105 Abhi -
Resultant Relation
ENAME Mang_Id
Lavi Raj
Chandan Ravi
In the EMP relation, the attribute EMP_ID shows employee'code, ENAME and
Mang_Id under which employee is working. In this, some employee are not having
Mang_Id i.e. their value is null because they act as a manager itself.
o Like the cartersion product, join operations are commutative. Using this
property, we can choose which relation can be the inner and which one the
outer while joining two relations. If P and Q are two relations then,
P⋈ Q = Q ⋈ P
o Rows whose join attribute is null do not appear in the resulting relation.
o We can also join more than two relations by increasing the complexity.
o Joins are generally used when a relationship exists between relations such as
where the join condition is based on the primary key and foreign key columns.
o Join is a very powerful operator and together with projection form a base for
normalization i.e. theoretical support for designing database relations.
o If the attributes on which the join is performed have the same name in both
the relations then renaming is necessary for the EQUIJOIN operation and
unnecessary for the NATURAL JOIN operation because the former i.e. EQUI
JOIN both exist as a result of the common attribute relation but the latter In
i.e., natural additive consequent relations have only one common property.
Division Operation:
The division operation results in a new relation such that every tuple appearing in the
resulting relation must exist in the dividend relation. In the combination of each tuple
in the denominator relation. It is a binary operation that operates on two relations.
The division is represented by a symbol "÷".
Subject Relation
Subject_Name Cousres
DBMS Btech CS
DBMS Btech IT
DBMS BCA
Course Relation
Courses Name
BCA
Btech CS
Btech IT
BSC GWD
MCA
BCA
Subject_Name
DBMS