Relational algebra is a formal system developed by Edgar F. Codd in 1970 as a theoretical framework for manipulating relational data. It serves as the foundation of modern query languages like SQL and provides a mathematical basis for database operations. This page introduces the concepts, operators, and applications of relational algebra.
In relational algebra, a relation represents a table in a relational database. It consists of a set of tuples (rows) that all share the same attributes (columns). Each relation has a name that identifies it within the database schema.
An attribute is a named column in a relation. Each attribute has an associated domain, which is the set of allowable values for that attribute. For example, a "Department" attribute might have a domain of string values representing department names.
A tuple is a single row in a relation, representing a specific set of attribute values. Each tuple must have the same structure as the relation's schema and contains exactly one value for each attribute.
Relational algebra consists of a set of operations that manipulate relations. These operations can be categorized into basic (primitive) operators and derived operators.
The selection operator filters tuples from a relation based on a specific condition. It is denoted by the Greek letter sigma (). The syntax is condition(relation).
Example: age > 25(Person) returns all tuples from the Person relation where the age attribute is greater than 25.
The projection operator extracts specific attributes from a relation, discarding the others. It is denoted by the Greek letter pi (). The syntax is attribute list(relation).
Example: name, salary(Employee) returns a new relation containing only the name and salary attributes from the Employee relation.
The Cartesian product combines every tuple from one relation with every tuple from another relation. It is denoted by the multiplication sign (). The resulting relation has a number of tuples equal to the product of the numbers of tuples in the input relations.
Example: Employee Department creates a new relation where each Employee tuple is combined with each Department tuple.
The union operator combines all tuples from two relations that are union-compatible (having the same structure and attribute domains). It is denoted by the union symbol () and eliminates duplicate tuples.
Example: Manager Supervisor returns a relation containing all tuples that are in either Manager or Supervisor relations, or in both.
The set difference operator returns tuples from the first relation that are not present in the second relation, assuming they are union-compatible. It is denoted by the minus sign (-).
Example: Employee - Contractor returns all tuples in Employee that are not in Contractor.
The join operator combines tuples from two relations based on a related attribute. It is essentially a Cartesian product followed by a selection. It is denoted by the bow tie symbol ().
Example: Employee Employee.dept_id = Department.id Department returns tuples from Employee and Department that have matching department IDs.
A natural join is a special case of the join operation where the joining condition is based on all attributes with the same name in both relations. It also removes the duplicate attributes in the result.
Example: Employee Department automatically joins on the common attributes (typically dept_id).
The intersection operator returns tuples that are present in both relations, assuming they are union-compatible. It is denoted by the intersection symbol ().
Example: Manager Supervisor returns a relation containing tuples that are present in both Manager and Supervisor relations.
The rename operator changes the name of a relation or its attributes. It is denoted by the Greek letter rho (). The syntax is new relation name(old relation name) or new attribute name(s)(relation).
Example: Staff(Employee) renames the Employee relation to Staff.
The division operator is useful for queries that involve "all instances of" relationships. It is denoted by the division symbol (). If we have relations R and S, then R S returns all tuples from R that are associated with every tuple in S.
Example: To find students who have taken all courses from a set of required courses, we could use: StudentCourse RequiredCourse
Extended relational algebra includes aggregate functions like SUM, AVG, COUNT, MIN, and MAX, which perform calculations on groups of tuples.
Example: To find the average salary by department: department, AVG(salary)(Employee)
Let's consider a simple database with the following relations:
| student_id | name | age | department |
|---|---|---|---|
| 001 | John | 20 | Computer Science |
| 002 | Sarah | 21 | Mathematics |
| 003 | Michael | 22 | Computer Science |
| course_id | name | credits | department |
|---|---|---|---|
| C101 | Database Systems | 3 | Computer Science |
| M201 | Calculus | 4 | Mathematics |
| C102 | Programming | 3 | Computer Science |
| student_id | course_id | grade |
|---|---|---|
| 001 | C101 | A |
| 001 | C102 | B+ |
| 002 | M201 | A- |
| 003 | C101 | B |
department = "Computer Science" AND age < 22(Student)
This returns a relation containing only John (student_id 001).
name(student Enrollment name = "Database Systems"(Course))
This returns a relation containing John and Michael.
course_id(department = "Computer Science"(Student) Enrollment)
This returns courses C101 and C102.
While relational algebra is primarily a theoretical foundation, its principles are implemented in practical database systems through query languages like SQL. Understanding relational algebra provides several benefits:
Relational calculus is another theoretical query language for the relational model. It is a non-procedural language that describes what to retrieve rather than how to retrieve it. There are two variants of relational calculus: tuple relational calculus and domain relational calculus.
The key difference between relational algebra and relational calculus is that:
Despite this difference, both have equivalent expressive power, as shown by Codd's theorem. This means that any query that can be expressed in one can also be expressed in the other.
Relational algebra serves as the mathematical foundation for relational database systems. Its set of operators provides a formal way to express queries and transformations on relational data. While most database professionals work with higher-level languages like SQL in practice, understanding relational algebra offers valuable insights into how database systems work and how queries are processed and optimized.
The concepts of relational algebra continue to influence modern database systems, including distributed databases, NoSQL systems that support relational operations, and even newer data processing frameworks. As data management technologies evolve, the fundamental principles of relational algebra remain relevant in how we think about querying and transforming data.
```
