Relational Algebra
Relational Algebra
Data integrity means reliability and accuracy of data. Integrity rules are designed to keep the
data consistent and correct. It is very important that a database maintains the quality of the data stored
in it. Data integrity is enforced under the following categories
Entity Integrity
Domain integrity
Referential Integrity
Entity Integrity
The entity integrity rule ensures that the primary key cannot contain null data. It is also called
row integrity. If primary key is allowed to have null value, it is not possible to uniquely identify a row in
the table. So entity integrity defines a tuple as unique entity for a particular relation.
Domain integrity
A set of values that can be stored in a column is called a domain. For example, the marks of a
student in a subject can be from 0 to 100 or CGPA from 0.0-4.0. Domain integrity enforces restrictions
on the values entered in a column. It specifies the validity of a specific data entry in a column. The data
type of a column enforces domain integrity. For example, if the data type of age column is numeric, it
cannot store a value like ‘Twenty Three’ instead of 23.
Referential Integrity
Referential integrity ensures the validity or integrity of relationship between tables when
records are added or deleted. It ensures that key values are consistent across the tables. A value cannot
be inserted in foreign key if it has no corresponding value in primary key field of the relation table. Such
consistency requires that if a key value changes then all references to it should also be changed
accordingly. The value in foreign key column in the referencing table must be same as the value in the
corresponding primary key column in referenced /parent table.
Relational Algebra
Relational algebra is a query language that processes one or more relations to define another
relation without changing original relations.
Unary Operations
The operations which involve only one relation are called unary operation. The following
operations are the unary operations
Selection
Projection
Binary Operations
The operations which involve pairs of relations are called binary operations. A binary operation
uses two relations as input and produces a new relation as output. Binary operations are
Union
Set Difference
Cartesian Product
Selection Operation
The selection operation is a unary operation. The selection operator is sigma σ. It acts like a
filter on a relation. It returns only a certain number of tuples. It selects the tuples using a condition.
σ CONDITION (R)
Comparison Operation = < > ≤ ≥ /=
For example.
σ Name=’zeeshan’ (Student)
Select a student where name is Zeeshan and AGE is 24 from Student table.
Select a student where name is Zeeshan and AGE is not 24 from Student table.
Select a student where name is not Zeeshan and AGE is not 24 from Student table.
Projection is also a unary operation. The projection operation is pi π. It limits the attributes
returned from the original relation.
π ATTRIBUTE (Student)
Select name and AGE from Student table.
π Name,Age (Student)
Select name Where AGE is 24 from Student table.
Union.
The Union operation of two relations combines the tuples of both relations to produce a third
relation. If two relations contain identical tuples, the duplicate tuples are eliminated. Union operation is
commutative. The resulting relation has the same degree as the original relation.
AUB=BUA
The relation on which the union operation is done must contains equal no of attributes that is
they must be union compatible relation.
Example
Table A
A B C
1 X 10
2 Y 20
3 Z 30
Table B
A B C
1 X 10
4 M 40
5 N 50
Result(A U B)
A B C
1 X 10
2 Y 20
3 Z 30
4 M 40
5 N 50
Intersection.
The Intersection operation works on two relations and combines the tuples of both relations to
produce a third relation. Unlike union it produces a relation that only contains common tuples. The
degree of the resulting relation may be equal to or less than the original relation.
A∩B=B∩A
Example
Table A
A B C
1 X 10
2 Y 20
3 Z 30
Table B
A B C
1 X 10
4 M 40
5 N 50
Result(B ∩A)
A B C
1 X 10
Difference.
The difference operation works on two relations. It produces a relation that contains tuples that
occur in the first relation but not in second. Difference operation is not commutative.
A - B /= B - A
Example
Table A
A B C
1 X 10
2 Y 20
3 Z 30
Table B
A B C
1 X 10
4 M 40
5 N 50
Result (A-B)
A B C
2 X 20
3 Z 30
Result (B-A)
A B C
4 M 40
5 N 50
Product.
The product works on two relations. It concatenates every tuple in one relation with every tuple
in second relation in such a way that the columns of new table is the sum of the columns of two tables
and rows in new table is the product of rows of two tables. It is also called Cartesian product or cross
product. The product needs not to be union compatible. It means that they can be of different degree.
A XB
Example
Table A
A B C
1 X 10
2 Y 20
3 Z 30
Table B
A B C
1 X 10
4 M 40
5 N 50
TABLE (AXB)
A1 A2 A3 A4 A5 A6
1 X 10 1 X 10
1 X 10 4 M 40
1 X 10 5 N 50
2 Y 20 1 X 10
2 Y 20 4 M 40
2 Y 20 5 N 50
3 Z 30 1 X 10
3 Z 30 4 M 40
3 Z 30 5 N 50
Division.
The division operator results in columns values in one table for which there are other matching
values corresponding to every row in the another table.
Table A
A B C
1 X 10
1 M 40
1 N 50
2 M 20
3 X 10
3 M 40
3 N 50
Table B (Divisor)
B C
X 10
M 40
N 50
Result A/B Table
A
1
3
Join.
The join operation combines data from one tuple of a relation with tuple from another relation or
same relation.
Types of Join
Theta Join. ɵ
Theta Join is the result of performing a select operation using a comparison operator theta on the
product. Theta is denoted by ɵ. In normal cross product all rows of one relation is merged with all rows
of second relation. In theta join only selected rows of first relation are merged with all rows of second
relation. It is denoted as R [X] S.
ID NAME AGE
1 A 10
2 B 40
3 C 50
B
(σ Name=’B’ (A))XB
Equi join is a join in which tuples are joined on the basis of vales of a common attribute between
two relations. It is the most commonly used type of join. The common attributes of both relations
appear twice in the output.
ID NAME AGE
1 A 10
2 B 40
3 C 50
B
A ⋈ A.ID=B.FID B
Natural join is the same as equi join. The difference is that the common attributes appear only once.
A natural join removes the duplicate attributes.
ID NAME AGE
1 A 10
2 B 40
3 C 50
B
A ⋈ ID, FID B
ID NAME AGE SubjectID Sub_Name
1 A 10 01 DBA
2 B 40 09 ENGLISH
Outer Join
In outer join all tuples of left and right relation are part of output. All tuples of left relation which
are not matched with right relation are left as Null. Similarly all tuples of right relation which are not
matched with left relation are left as Null. There are three types of outer join,
Left Outer Join. It is denoted by ⟕. It includes all tuples of left hand relation and includes only matching
tuples from right hand relation. The unmatched rows are represented as Null.
Right Outer Join. It is denoted by ⟖. It includes all tuples of right hand relation and includes only
matching tuples from left hand relation. The unmatched rows are represented as Null.
Full Outer Join. It is denoted by ⟗ . It includes all tuples of left hand relation and right hand relation.
ID NAME AGE
1 A 10
2 B 40
3 C 50
B
A⟕B
Semi join take the natural join of two relation then projects the attributes of first relation only. After
join and matching the common attributes of both relations only attributes of first relation are projected.
ID NAME AGE
1 A 10
2 B 40
3 C 50
B
A⋉B
ID NAME AGE
1 A 10
2 B 40