Relational algebra is a theoretical framework that describes a set of mathematical operators used to manipulate and query relational databases. Here are five common relational algebra operators and examples of their use:
-
Selection (σ): This operator selects tuples that meet a given condition. It is used to filter rows from a table that match a certain criteria. The syntax for selection is σ(condition)(relation). For example, let's say we have a table called "Students" with columns "Name", "Age", and "GPA". We can use the selection operator to select all the students who are older than 20:
σ(Age > 20)(Students)
-
Projection (π): This operator selects certain columns from a table. It is used to reduce the number of columns returned by a query. The syntax for projection is π(column1, column2, ...)(relation). For example, if we want to select only the "Name" and "GPA" columns from the "Students" table, we can use the projection operator:
π(Name, GPA)(Students)
-
Union (∪): This operator combines two tables into a single table with all the distinct rows from both tables. The syntax for union is relation1 ∪ relation2. For example, if we have two tables "A" and "B" with the same columns, we can combine them into a single table "C" with all the distinct rows:
C = A ∪ B
-
Intersection (∩): This operator returns only the rows that are common between two tables. The syntax for intersection is relation1 ∩ relation2. For example, if we have two tables "A" and "B" with the same columns, we can select only the rows that are common to both tables:
A ∩ B
-
Join (⋈): This operator combines two tables based on a common column. The syntax for join is relation1 ⋈ condition relation2, where the condition specifies how the two tables should be matched. For example, if we have two tables "Students" and "Courses" with a common column "ID", we can join them on that column:
Students ⋈ ID = CourseID Courses
These are just a few examples of the many relational algebra operators available. Other common operators include difference, division, and rename.