What are the Unary Operations in Relational Algebra?
Last Updated :
26 Mar, 2024
The unary operations in Relational algebra are selection, projection, and rename. The select, project, and rename operators in relational algebra work on one relation only so they are called unary operators. In this article, we will discuss the unary operations in relational algebra. We will also discuss each unary operator in detail along with examples. Let’s start learning on the topic “Unary Operations in Relational Algebra”.
What is Relational Algebra?
Relational algebra is a procedural query processing language that provides the base of relational databases and SQL. Relational algebra has various operators like select, project, rename, cartesian product, union, intersection, set difference, joins, etc. These operators are used to form queries in relational algebra.
What are Unary Operations in Relational Algebra?
The operations that operate on only one relation are called unary operations in relational algebra. The three unary operations in relational algebra are:
Selection Operation in Relational Algebra
The selection operation is a unary operation that is performed on one relation. The selection operation is used to retrieve tuples from the relation that satisfies the given condition. It is denoted by σ. Selection operation in relational algebra is written as:
σcondition (Relationname)
We can also add multiple conditions if required using the operators ∧ (AND), ⋁and (OR). These operators are used to combine multiple conditions as required in the problem. Below is the selection operator representation with multiple conditions.
σcondition1 operator condition2 … condition n (Relationname)
To select all the tuples of a relation we write the selection operation without any condition.
σ (Relationname)
Projection Operation in Relational Algebra
The projection operation is a unary operation that is performed on a relation. A projection operation is used to retrieve all the values of one or more attributes. It is denoted by π. Projection operation in relational algebra is written as:
πcolumnname(Relationname)
We can add multiple column names in projection operation using the comma operator if required. The comma operator is used when we have to retrieve multiple column values. Below is the projection operation representation to output multiple columns.
πcolumnname1, columnname2, …,columnnamen (Relationname)
Rename Operation in Relational Algebra
The rename operation is operation applied on one relation. Rename operation as the name suggests is used to rename the relation, attributes or both. It is denoted by ρ.
Rename Operation for Renaming Relation
Rename operation for renaming relation is written as:
ρ New_relation_name (Old_relation_name)
For example: To rename relation R to S: ρ S(R)
Rename Operation for Renaming Columns of Relation
Rename operation for renaming columns of relation is written as:
ρ (New_columnnames) (Relation_name)
For example: To rename columns of relation R (a, b, c) to (x, y, z): ρ (x, y, z) (R)
Rename Operation for Renaming Both Relation and Columns of Given Relation
Rename operation for renaming both relation and columns of relation R (a, b, c) is written as:
ρ New_relation_name(New_columnnames) (Old_relation_name)
For example: To rename both relation and columns of R (a, b, c) to S (x, y, z): ρ S (x, y, z) (R)
Example on Unary Operations in Relational Algebra
Example 1: Consider the below given table and write the queries in relational algebra for the questions given below.
Student
|
Rollno
|
Studentname
|
Marks
|
1
|
A
|
80
|
2
|
B
|
50
|
3
|
C
|
95
|
4
|
D
|
62
|
5
|
A
|
70
|
Q.1: Output all the tuples of the relation.
σ (Student)
Q.2: Retrieve only those tuples where Rollno is greater than 2.
σRollno > 2 (Student)
Q.3: Retrieve only those tuples where either Marks less than 70 or Studentname is A.
σMarks < 70 ∨ Studentname = A (Student)
Q.4: Retrieve the tuples with Student Name A and Marks greater than 75.
σMarks > 75 ∧ Studentname = A (Student)
Q.5: Output all the values in the column Rollno.
πRollno(Student)
Q.6: Retrieve all the values of columns Rollno and Marks
πRollno, Marks (Student)
Q.7: Retrieve the Rollno of students whose Marks is greater than or equal to 80.
πRollno [σMarks >=80 (Student)]
Q.8: Rename the relation as S and attributes of relation Rollno as r, Studentname as Sname and Marks as m.
ρ S (r, Sname, m) (Student)
Example 2: Predict the output of given queries. Consider the below relation.
Employee
|
Eid
|
Ename
|
Salary
|
201
|
P
|
20000
|
202
|
Q
|
5000
|
203
|
R
|
10000
|
204
|
P
|
7500
|
Q.1: Query- σ (Employee)
Output:
Employee
|
Eid
|
Ename
|
Salary
|
201
|
P
|
20000
|
202
|
Q
|
5000
|
203
|
R
|
10000
|
204
|
P
|
7500
|
Q.2: σSalary > 7000 ∧ Ename = P (Student)
Output:
Employee
|
Eid
|
Ename
|
Salary
|
201
|
P
|
20000
|
204
|
P
|
7500
|
Q.3: πEname(Employee)
Output:
Q.4: ρ E (id, name, S) πEid, Ename, Salary [σSalary <10000 (Employee)]
Output:
Conclusion
In this article, we have discussed the unary operators that are widely used in SQL in full detail. These operators are Selection, Projection, Rename. We have also discussed the examples on all these operators for your better understanding.
Share your thoughts in the comments
Please Login to comment...