×

Relational Algebra in DBMS

Relational Algebra is a widely used procedural query language, which takes instances of one or more relation as an input and generates a new relation as an output. It uses a different set of operators (like unary or binary operators) and operands to perform queries. EF Codd created the relational algebra for relational database. 
The relational algebra provides a query language in which all tuples or rows of data are accessed in one statement without the use of loops, etc.

The operators that are used to perform relational algebra are:

  1. Basic Operators
  2. Derived Operators

Basic Operators

  • SELECT (?)
  • PROJECT (?)
  • RENAME ( ? )
  • UNION (?)
  • DIFFERENCE (-)
  • CARTESIAN PRODUCT ( x )

Project Operator(?)

The project operator is represented by the pie(?) symbol, which is used to access the values of specified columns or attributes from a table or a relation.

Syntax: ? (R)

In the above syntax, R is the name of a relation or table and C1, C2 C3…. CN are the attributes or columns name of the relation.

Example:

Table Student:

Rollno Name Age
101 A 20
102 B 21
103 C 20
104 D 20


Query: Retrieve the Rollno from the above table Student

  • ?Rollno(Student)

Output:

Rollno
101
102
103
104

Select Operator(?)

The select operator is represented by the sigma(?) symbol, which is used to fetch the tuples (rows) from the relation that satisfies the selection condition. We cannot fetch the attributes of a relation using this command.

Syntax: ?(R)

In the above syntax, R is a relation or name of a table, and the condition is a propositional logic which uses the relational operators like ?, <,=,>, ?.

Example:

Table Student:

Rollno Name Age
101 A 20
102 B 21
103 C 20
104 D 20


Query: Retrieve the name of Rollno 102 from the above table Student

  • ?Name(? Rollno=102(Student))

Output:

Name
B

Rename Operator( ? )

The rename operator is denoted by the rho() symbol. It is used to rename a relation or an attribute of the relation.

Syntax:  ? (new_Relation_name,old_Relation_name)

Example:

Table Student:

Rollno Name Age
101 A 20
102 B 21
103 C 20
104 D 20


Query: Rename the name of Student relation to student1.

  • (student1,Student)

Union Operator (?)

The union operator is represented by the (?)symbol, which is used to display all the tuples (rows) from two relations. It is a binary operation because to apply this operation user must require a minimum of two relations.

For a valid union operation, both the table should have same number of attributes to combine them. Duplicate data are eliminated during the union operation.

Syntax: R1 U R2

In the above syntax, R1 is the name of the first relation or table, and R2 is the name of the second relation or table.

Example:

Table Student:                                                                    

Rollno Name
 101 A
102 B
 103 C
104 D

Table Employee:

Employee_id Name
 201 P
202 Q
 203 R
204 S


Query: Combine the table Student with another table Employee

  • (Student)U(Employee)

Output:

Rollno Name
 101 A
102 B
 103 C
104 D
 201 P
202 Q
 203 R
204 S

Set Difference (-)

The set difference operation is represented by the minus (-) sign, which is used to display all the tuples (rows) that are in the first relation but not in the second relation.

Syntax: R1 – R2

In the above syntax, R1 is the name of the first relation or table, and R2 is the name of the second relation or table.

Example:

Table Student:                                                                    

Rollno Name
 101 A
102 B
 103 C
104 D

Table Employee:

Employee_id Name
 101 A
202 Q
 103 C
204 S
  • (Student)-(Employee)

Output:

Rollno Name
102 B
104 D

Cartesian Product (x)

It is represented by the cross (x) symbol, which is used to combine each tuple in one relation with each tuple in another relation. This operation is also known as a cross product.

Syntax: R1 x R2

In the above syntax, R1 is the name of the first relation or table, and R2 is the name of the second relation or table. If the relation R1 contains x columns and m rows, and relation R2 contains y columns and n rows, the resultant table will contain x+y columns and m*n rows.

Example:

Table T1:                                                                    

Column1 Column2 Column3
 101 A 12
102 B 21
 103 C 20
104 D 31

Table T2:

columnA ColumnB
 101 A
202 Q
  • (T1)X(T2)

Output:

Column1 Column2 Column3 columnA ColumnB
 101 A 12  101 A
 101 A 12 202 Q
102 B 21  101 A
102 B 21 202 Q
 103 C 20  101 A
 103 C 20 202 Q
104 D 31  101 A
104 D 31 202 Q

Derived Operators

Derived operators are the operators which can be obtained from the basic operators and consists of following three types:

  • INTERSECTION ( ? )
  • JOINS (?)
  • DIVISION(/)

Intersection Operator (?)

The intersection operator is represented by the (?) symbol, which is used to display all the tuples or rows of common attributes from two relations. It is also a binary operator.

Syntax: R1 ?  R2

In the above syntax, R1 is the name of the fist relation or table, and R2 is the name of the second relation or table.

Example:

Table Student:                                                                    

Rollno Name
 101 A
102 B
 103 C
104 D

Table Employee:

Employee_id Emp_Name
 101 A
202 Q
 103 C
204 S
  • ? Name (Student) ? ? Emp_Name (Employee) 

Output

Name
A
C

Joins

A join operation is represented by ?(Bowtie) Symbol. Joins are the combination of related tuples from two different relations or tables into a single type. It is similar to a Cartesian product. In Cartesian product operation, a user gets all the possible combinations of relations. But in join operation, a user gets only those combinations that satisfy some matching conditions between two relations. A relation can also join to itself, which is called as a self-join.

Division Operator ( / )

The division operator is represented by the division (/) symbol, which can be applied if and only if:

  • Attributes of a relation B is a proper subset of attributes of a relation A.
  • The resultant relation will have the attributes = (All attributes of relation A – All attributes of relation B).
  • The relation or table returned by the division operator will display those tuples from relation A, which are associated with every B’s relation tuple.

Example:
Table: Enrolled

Sid Cid
S1 C1
S2 C1
S1 C2
S3 C2

Table: Course

Cid
C1
C2

Query: Retrieve the student_id of students who enrolled in every course.

  • ?Sid (Enrolled) – (?Sid ( (?Sid(Enrolled)) x ( ?Cid (Course) ) ) – (Enrolled) )

Output:

Sid
S1


Related Topics

ER Diagram for Student Management System in DBMS

Entity Relationship diagrams Entity Relationship Diagrams, or ER Diagrams for short, are diagrams that show the relationships among entity sets that are stored in databases. Alternatively said, ER diagrams assist in...

6 minutes read.

BCNF in DBMS

BCNF stands for Boyce–Codd Normal Form. What is the Normal form? The normal form is mainly used to reduce the redundancy of the database tables. Or we can say that the normal form...

4 minutes read.

B+ (Plus) Tree in DBMS

What is a B+ tree? The B+ tree is known as a balanced binary search tree. in this tree, we can store the data in the form of nodes. B+ tree...

9 minutes read.

Red-Black Tree Deletion in DBMS

Introduction To Red Black Tree Each node's extra bit in a red-black tree, a type of self-balancing binary search tree, is frequently understood as the color (red or black). The balance...

3 minutes read.

Relational DBMS Concepts

What is RDBMS? RDBMS stands for Relational Database Management System.The relational database management system is a type of DBMS that stores information in the form of related tables and uses a...

3 minutes read.

Generalization in DBMS

Generalization in DBMS In the database management system, generalization is a concept combining the common attributes of two or more lower-level entity and form a new higher level with the common...

3 minutes read.

Difference between Relational and Non-Relational Databases

Databases have become an integral part of our lives, from powering big websites like Amazon and Netflix to everyday tasks like remembering our passwords. With such an indispensable role in...

7 minutes read.

What is a Database

A database is a structured collection of data that is often kept electronically on a computer system. Typically, a database is managed by a database management system (DBMS). Together, the...

9 minutes read.

ACID Properties in DBMS

A transaction in a database has the following four properties, known as ACID properties. These properties are used to maintain the consistency of the database in the case of system failure and concurrent...

4 minutes read.

Levels of Abstraction in DBMS

Data abstraction is a way to hide unwanted or irrelevant information from the end user in DBMS. It helps in enhancing the security of data, and simplifies database design using...

4 minutes read.

Normalization in DBMS: 1NF, 2NF, 3NF, BCNF & 4NF with Examples

Normalization is a technique of organizing the data in the database. It is a systematic approach which is used to remove or reduce data redundancy in the tables and remove the...

7 minutes read.

DBMS Schedule

A schedule is a process of combining the multiple transactions into one and executing the operations of these transactions in a predefined order. A schedule can have multiple transactions in it, each transaction...

2 minutes read.

Conversion of ER Diagram into Relational Model

After designing the Entity-Relationship diagram, you need to convert it into tables in the relational model. Because the relational model can be easily implemented by the Relational DBMS like Oracle, MySQL, etc....

2 minutes read.

DBMS Generalization, Specialization, and Aggregation

DBMS Generalization Generalization is a bottom-up approach in which the common attributes of two or more lower-level entities combines to form a new higher-level entity. In generalization, the generalized entity of higher level can also...

3 minutes read.

Difference between Dropbox and Amazon Drive

What is Dropbox? It is a file organising company that is owned by the American Company Dropbox Inc. Itsheadquarters is situated in San Francisco, California, U.S. Dropbox offers personal cloud, client...

3 minutes read.

DBMS Examples

Introduction to DBMS A database management system (DBMS) is a software application that is designed to manage and organize data stored in a database. It is responsible for storing, retrieving, and...

4 minutes read.

Database for library management system

The database can be considered the container where the data or information can be stored electronically in a computer system. Most mobile applications and websites run our day-to-day activities by...

5 minutes read.

Super Key in DBMS

Super Key in DBMS: The super key is a column or a set of columns in the database table, which uniquely identifies the tuple or row of the same table....

2 minutes read.

DBMS Data Independence: Logical and Physical

Data Independence in DBMS: Data independence is a concept of DBMS which alters the schema of the database at one level of the database system without altering the schema definition...

2 minutes read.

Redundancy in DBMS

Data redundancy is a situation that is created in the database in which the same amount of data is stored in two different places. The different places are found in a...

3 minutes read.