Introduction of Relational Algebra in DBMS - GeeksforGeeks
Introduction of Relational Algebra in DBMS
- Difficulty Level : Easy
- Last Updated : 28 Jun, 2021
Relational Algebra is procedural query language, which takes Relation as input and generate relation as output. Relational algebra mainly provides theoretical foundation for relational databases and SQL.
Operators in Relational Algebra
Projection (π)
Projection is used to project required column data from a relation.
Example :
R (A B C) ----------1 2 42 2 33 2 34 3 4
π (BC)B C-----2 42 33 4
Note: By Default projection removes duplicate data.
Selection (σ)
Selection is used to select required tuples of the relations.
for the above relation
σ (c>3)R
will select the tuples which have c more than 3.
Note: selection operator only selects the required tuples but does not display them. For displaying, data projection operator is used.
For the above selected tuples, to display we need to use projection also.
π (σ (c>3)R ) will show following tuples.A B C-------1 2 44 3 4
Union (U)
Union operation in relational algebra is same as union operation in set theory, only constraint is for union of two relation both relation must have same set of Attributes.
Set Difference (-)
Set Difference in relational algebra is same set difference operation as in set theory with the constraint that both relation should have same set of attributes.
Rename (ρ)
Rename is a unary operation used for renaming attributes of a relation.
ρ (a/b)R will rename the attribute ‘b’ of relation by ‘a’.
Cross Product (X)
Cross product between two relations let say A and B, so cross product between A X B will results all the attributes of A followed by each attribute of B. Each record of A will pairs with every record of B.
below is the example
A B(Name Age Sex ) (Id Course) ------------------ -------------Ram 14 M 1 DSSona 15 F 2 DBMSkim 20 MA X BName Age Sex Id Course---------------------------------Ram 14 M 1 DSRam 14 M 2 DBMSSona 15 F 1 DSSona 15 F 2 DBMSKim 20 M 1 DSKim 20 M 2 DBMS
Note: if A has ‘n’ tuples and B has ‘m’ tuples then A X B will have ‘n*m’ tuples.
Natural Join ()
Natural join is a binary operator. Natural join between two or more relations will result set of all combination of tuples where they have equal common attribute.
Let us see below example
Emp Dep(Name Id Dept_name ) (Dept_name Manager)------------------------ --------------------- A 120 IT Sale YB 125 HR Prod ZC 110 Sale IT AD 111 ITEmp DepName Id Dept_name Manager-------------------------------A 120 IT AC 110 Sale YD 111 IT A
Conditional Join
Conditional join works similar to natural join. In natural join, by default condition is equal between common attribute while in conditional join we can specify the any condition such as greater than, less than, not equal
Let us see below example
R S(ID Sex Marks) (ID Sex Marks)------------------ -------------------- 1 F 45 10 M 202 F 55 11 M 223 F 60 12 M 59Join between R And S with condition R.marks >= S.marksR.ID R.Sex R.Marks S.ID S.Sex S.Marks-----------------------------------------------1 F 45 10 M 201 F 45 11 M 222 F 55 10 M 202 F 55 11 M 223 F 60 10 M 203 F 60 11 M 223 F 60 12 M 59