Relational Algebra in DBMS

Relational Algebra in DBMS

Relational algebra is a formal language used to manipulate and query relational databases. It provides a set of operations that can be applied to relations (tables) to retrieve and transform data.

Selection Operation

The selection operation in relational algebra allows us to retrieve a subset of tuples from a relation based on a given condition.
This condition is specified using logical operators and comparison operators.
Let's consider an example to understand how the selection operation works.
Suppose we have a relation called "Employees" with attributes (columns) such as "EmployeeID," "Name," "Department," and "Salary."
To select all employees who belong to the "Sales" department, we can use the following selection operation:
1Employees_Sales = σ(Department = 'Sales')(Employees)
In this example, the selection condition is Department = 'Sales', and the resulting relation "Employees_Sales" will contain only those tuples that satisfy this condition.

Projection Operation

The projection operation in relational algebra allows us to retrieve specific attributes (columns) from a relation while discarding the others. It helps in creating new relations with a subset of attributes. Let's continue with our "Employees" relation and demonstrate the projection operation.
Suppose we want to create a new relation that includes only the "Name" and "Salary" attributes from the "Employees" relation. We can use the following projection operation:
1Employee_Name_Salary = π(Name, Salary)(Employees)
The resulting relation "Employee_Name_Salary" will contain only the "Name" and "Salary" attributes from the original "Employees" relation.

Union Operation

The union operation in relational algebra combines two relations and returns a new relation that contains all the tuples from both relations while eliminating any duplicates. It can be useful when we want to merge two relations with similar schemas. Let's illustrate the union operation with an example.
Consider two relations: "Students" with attributes (columns) "StudentID," "Name," and "Major," and "Alumni" with attributes "StudentID," "Name," and "Employer." To combine these relations, we can use the union operation as follows:
1All_Records = Students ∪ Alumni
The resulting relation "All_Records" will contain all the tuples from both "Students" and "Alumni," eliminating any duplicate tuples.

Intersection Operation

The intersection operation in relational algebra returns a new relation that contains only the common tuples between two relations. It is useful when we want to find the shared elements between two relations. Let's continue with our "Students" and "Alumni" relations and demonstrate the intersection operation.
1Common_Students = Students ∩ Alumni
The resulting relation "Common_Students" will contain only the tuples that exist in both "Students" and "Alumni."

Set Difference Operation

The set difference operation in relational algebra allows us to retrieve the tuples that exist in one relation but not in another. It helps in finding the elements that are unique to a specific relation. Let's consider an example to understand the set difference operation.
Suppose we have two relations: "All_Students" with attributes (columns) "StudentID" and "Name," and "Graduated_Students" with attributes "StudentID" and "Name." To find the students who have not yet graduated, we can use the set difference operation as follows:
1Not_Graduated_Students = All_Students - Graduated_Students
The resulting relation "Not_Graduated_Students" will contain only the tuples that exist in "All_Students" but not in "Graduated_Students."

Division Operation

The division operation in relational algebra is used to find tuples that satisfy a certain condition across two relations.
It is helpful when we want to determine a subset of tuples that can generate a specific result when combined with another relation.
Let's illustrate the division operation with an example.
Consider two relations: "Courses" with attributes (columns) "CourseID" and "CourseName," and "Prerequisites" with attributes "CourseID" and "PrerequisiteCourseID."
To find the courses that have all their prerequisites satisfied, we can use the division operation as follows:
1Courses_With_Satisfied_Prerequisites = Courses ÷ Prerequisites
The resulting relation "Courses_With_Satisfied_Prerequisites" will contain only those courses whose prerequisites are satisfied by other courses.

Relational Calculus

Relational calculus is another formal language used in database management systems to express queries and define constraints.
It provides a declarative way to specify what data we want to retrieve from a database without specifying how to retrieve it.
There are two types of relational calculus: domain calculus and tuple calculus.

Domain

In relational calculus, a domain represents a set of possible values for an attribute. Each attribute in a relation is associated with a domain that defines the valid values it can hold.
The domain can be numeric, string, date, or any other data type supported by the database system.

Tuple

A tuple in relational calculus refers to a row or record in a relation. It represents a collection of attribute-value pairs that define a specific entity or entry in the database.
Each tuple in a relation must have a unique combination of attribute values, which serves as its identifier.

Well-Formed Formula

A well-formed formula (WFF) in relational calculus is a logical expression that defines a condition or constraint on the tuples in a relation. It consists of logical operators (AND, OR, NOT) and comparison operators (equal, not equal, greater than, less than) used to specify the desired condition. A WFF is considered well-formed if it follows the syntax and rules of the relational calculus language.

Specification

In relational calculus, a specification is an expression that defines a subset of tuples from a relation based on a given condition. It is similar to the selection operation in relational algebra, where the condition is expressed using logical and comparison operators. A specification helps in filtering out tuples that do not satisfy the specified condition.

Quantifiers

Quantifiers in relational calculus are used to express statements about sets of tuples in a relation.
The two commonly used quantifiers are the existential quantifier (∃) and the universal quantifier (∀).
The existential quantifier (∃) specifies the existence of at least one tuple that satisfies a given condition, while the universal quantifier (∀) specifies that all tuples in a relation satisfy a given condition.

Conclusion

We explored the basic operations of relational algebra, including selection and projection.
We also discussed set-theoretic operations like union, intersection, and set difference.