Explain fundamental operations of relational algebra with example.
Database Management System
Computer Science Engineering
539
Seema
Relational algebra is a set of operations that can be used to manipulate relations, which are tables in database theory. The fundamental operations of relational algebra are:
Selection: This operation selects tuples that satisfy a given condition. For example, the following query selects all tuples from the Customer
table where the Country
is USA
:
SQL
SELECT *
FROM Customer
WHERE Country = 'USA';
Projection: This operation projects a set of attributes from a relation. For example, the following query projects the CustomerID
and Country
attributes from the code>Customer table:
SQL
SELECT CustomerID, Country
FROM Customer;
Cartesian product: This operation creates a new relation that contains all possible combinations of tuples from two or more relations. For example, the following query creates a new relation that contains all possible combinations of customers and orders:
SQL
SELECT *
FROM Customer
CROSS JOIN Order;
Union: This operation combines two relations into a single relation. The resulting relation will contain all of the tuples from both relations, with duplicates removed. For example, the following query combines theCustomer
and Order
tables into a single table:
SQL
SELECT *
FROM Customer
UNION
SELECT *
FROM Order;
Intersection: This operation combines two relations into a single relation. The resulting relation will contain only the tuples that are in both relations. For example, the following query finds all customers who have placed an order:
SQL
SELECT *
FROM Customer
INTERSECT
SELECT *
FROM Order;
Difference: This operation creates a new relation that contains all of the tuples from the first relation that are not in the second relation. For example, the following query finds all customers who have not placed an order:
SQL
SELECT *
FROM Customer
EXCEPT
SELECT *
FROM Order;
These are just a few of the fundamental operations of relational algebra. There are many other operations that can be used to manipulate relations. Relational algebra is a powerful tool that can be used to perform complex queries on databases.