Formal relational query languages form the theoretical foundation for database query languages that we use in practice. These formal languages provide a way to precisely query and manipulate data stored in relational databases. The two primary categories of formal relational query languages are Relational Algebra and Relational Calculus.
Relational Algebra is a procedural query language where we describe how to retrieve data, while Relational Calculus is a non-procedural query language where we describe what data to retrieve without specifying how to get it.
Understanding these formal languages is essential for database professionals as they help in understanding the theoretical underpinnings of SQL and other practical query languages.
Relational Algebra is a procedural query language that uses a set of operations to take one or two relations as input and produce a new relation as output. Let's explore its fundamental operations:
The selection operation selects tuples that satisfy a given predicate.
_predicate(R) For example, to select all employees with salary greater than 50000:
_salary>50000(Employee) The projection operation selects certain attributes from a relation.
_attribute_list(R) For example, to get only the names and departments of employees:
_name,department(Employee) The union operation combines tuples from two relations that share the same schema.
R S For example, to find all employees who are either in department A or department B:
Employee_A Employee_B The set difference operation returns tuples that are in the first relation but not in the second.
R S For example, to find students who have taken a course but not the prerequisite:
Student Prerequisite_Student The Cartesian product combines every tuple from the first relation with every tuple from the second relation.
R S For example, to combine all students with all courses:
Student Course The rename operation changes the name of a relation or its attributes.
_new_name(R) For example, to rename the Employee relation to EMP:
_EMP(Employee) The join operation combines related tuples from different relations.
R _condition S For example, to join Employee and Department relations based on department ID:
Employee _Employee.dept_id = Department.dept_id Department The intersection operation returns tuples that are in both relations.
R S For example, to find courses that are both required and elective:
Required_Course Elective_Course The division operation is useful for queries that involve "all" or "every" conditions.
R S For example, to find students who have taken all courses:
Student_Course Course Relational Calculus is a non-procedural query language that describes what to retrieve rather than how to retrieve it. There are two types of Relational Calculus:
Tuple Relational Calculus uses tuple variables that range over a single relation. The syntax is:
{t | P(t)} where t is a tuple variable and P(t) is a formula that describes t.
For example, to find employees with salary greater than 50000:
{t | t Employee t.salary > 50000} For more complex queries, we often use existential () and universal () quantifiers.
To find students who have taken a specific course (CS101):
{t | t Student s (s Enrollment s.student_id = t.student_id s.course_id = 'CS101')} Domain Relational Calculus uses domain variables that range over the domains of attributes, rather than entire tuples. The syntax is:
{(x, x, ..., x) | P(x, x, ..., x)} For example, to find names of employees with salary greater than 50000:
{name | id,dept_no,salary (id,name,dept_no,salary Employee salary > 50000)} Both Relational Algebra and Relational Calculus have their strengths and are theoretically equivalent - any query expressible in one can be expressed in the other. Here are some differences:
Relational Algebra is procedural, specifying how to retrieve data, while Relational Calculus is declarative, specifying what data to retrieve.
Relational Algebra provides a foundation for query optimization, while Relational Calculus is closer to the user's natural way of expressing queries.
Relational Algebra can be more verbose for complex queries, while Relational Calculus can express certain queries more concisely.
Both have strong mathematical foundations, with Relational Algebra based on set theory and Relational Calculus based on predicate calculus.
Both Relational Algebra and Relational Calculus have the same expressive power, known as relational completeness. They can express any query that is safe and domain-independent.
However, there are limitations:
Basic relational algebra and calculus do not support aggregation functions like SUM, AVG, MAX, MIN. These are typically added as extensions.
Neither language natively supports operations that group tuples and apply functions to each group.
Neither language naturally supports recursive queries necessary for hierarchical data.
Queries that require transitive closure (e.g., finding all ancestors in a family tree) are not expressible in basic relational algebra or calculus.
SQL, the practical query language used in most relational database management systems, extends these formal languages to address these limitations by incorporating features from both.
Formal relational query languages provide a solid theoretical framework for database querying. Both Relational Algebra and Relational Calculus offer complementary approaches to querying relational data - one procedural, the other declarative.
These formal languages not only help us understand the theoretical foundations of querying but also aid in the design and optimization of practical query languages like SQL. Understanding these formal concepts allows database professionals to write more efficient queries and better understand how database systems process those queries.
While practical query languages like SQL have evolved to include features beyond basic relational algebra and calculus, the fundamental concepts of these formal languages remain at the heart of database query design and implementation.
```
