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.
Similar Reads
Set Theory Operations in Relational Algebra
Relational Algebra in DBMS These Set Theory operations are the standard mathematical operations on set. These operations are Binary operations that are, operated on 2 relations unlike PROJECT, SELECT and RENAME operations. These operations are used to merge 2 sets in various ways. The set operation
3 min read
RENAME (ρ) Operation in Relational Algebra
The rename operator in relational databases is used to change the name of a relation (table) or its attributes (columns). It is denoted by rho (Ï). It helps in making the database schema more meaningful or clearer by providing more descriptive names. This operation doesn't change the actual data in
3 min read
SELECT Operation in Relational Algebra
Prerequisite - Relational Algebra Select operation chooses the subset of tuples from the relation that satisfies the given condition mentioned in the syntax of selection. The selection operation is also known as horizontal partitioning since it partitions the table or relation horizontally. Notation
2 min read
Basic Operators in Relational Algebra
The Relational Model is a way of structuring data using relations, which are a collection of tuples that have the same attributes. Relational Algebra is a procedural query language that takes relations as input and returns relations as output. It uses a set of operators to manipulate and retrieve da
4 min read
Set Difference Operator in Relational Algebra
Relational Algebra is used to play with the data stored in relational databases. Relational Algebra has many operations to play with tables. One of the fundamental operations is set difference. This article will discuss Set Difference, its condition, and its examples. Key Terms Used in Set Differenc
4 min read
Extended Operators in Relational Algebra
Extended operators in relational algebra are operators that go beyond the basic set of relational algebra operations. They are also known as derived operators because they can be constructed from combinations of the fundamental operators. There are mainly three types of extended operators in Relatio
7 min read
Cartesian Product Operation in Relational Algebra
Prerequisite â Relational AlgebraWe already are aware of the fact that relations are nothing but a set of tuples, and here we will have 2 sets of tuples.On applying CARTESIAN PRODUCT on two relations that is on two sets of tuples, it will take every tuple one by one from the left set(relation) and w
3 min read
PROJECT Operation in Relational Algebra
Prerequisite - Relational Algebra Project operation selects (or chooses) certain attributes discarding other attributes. The Project operation is also known as vertical partitioning since it partitions the relation or table vertically discarding other columns or attributes. Notation: πA(R) where
2 min read
What are the Properties of Operations in Arithmetic?
Arithmetic probably has the longest history during the time. It is a method of calculation that is been in use from ancient times for normal calculations like measurements, labeling, and all sorts of day-to-day calculations to obtain definite values. The term got originated from the Greek word "arit
6 min read
Relational operators in SAP ABAP
Relational operators in SAP ABAP are symbols or combinations of symbols that compare values and return a Boolean result (either true or false). These operators allow developers to establish relationships between variables, constants, or expressions, facilitating decision-making processes in the prog
6 min read