By Kato Mivule
Database Systems
Outline
In this article we take a look at the differences between SQL, Relational Algebra, and Relational Calculus. I this article, we focus on the main differences between Relational Algebra and Relational Calculus. Since SQL is mainly an implementation language, we take note of some major differences between Relational Algebra and Relational Calculus.
Introduction
Relational Algebra (RA) and Relational Calculus (RC) are formal languages for the database relational model while SQL is the practical language in the database relational model. [1] In these formal languages a conceptual database model is expressed in mathematical terms and notations while in the practical language – SQL, the mathematical expressions of the functionality and transaction of the database operations are implemented physically. Formal languages provide a medium through which to optimize and implement queries in database transactions. [1]
Of course the first notable differences between these languages in the syntax and notation used in the expressions. Each language, that RA, RC, and SQL have their own notations to express their notations.
RA Notations – Procedural Expression Language
We begin with the notations used in RA and that are unique to RA and are procedural, requiring a sequence of operations to express a query transaction as described by Elmasri et al: [1]
SELECT
The SELECT opeartor is σ (sigma) symbol and used as an expression to choose tuples that meet the selection condition…
σ<Selection condition>(R)
PROJECT
The PROJECT operator in RA is ∏ (pi) symbol used to choose attributes from a relation.
∏<attribute list>(R )
RENAME
The RENAME operator is symbolized by ρ (rho) and is used to express a naming of query results, attributes, and relations.
ρ s(B1, B2, B3,….Bn)(R )
UNION
RA notation for UNION is symbolized by ∪ , and includes all tuples that are in A or in B, eliminating duplicate tuples, therefore set A UNION set B would be expressed as:
RESULT ← A ∪ B
INTERSECTION
The INTERSECTION operation on a relation A INTERSECTION relation B, is symbolized by A ∩ B, includes tuples that are only in A and B.
RESULT ← A ∩ B
MINUS
the MINUS operation includes tuples from one Relation that are not in another Relation and symbolized by the – (minus) symbol. Therefore A – B would be expressed as…
RESULT ← A – B
CARTERSIAN PRODUCT
Creates a relation that has all the attributes of A and B, allowing all the attainable combinations of tuples from A and B in the result. The notation used is X.
C = A X B
JOIN
The JOIN operation is denoted by the ⟗ symbol and is used to compound similar tuples from two Relations into single longer tuples.
A ⟗ <join condition> B
NATURAL JOIN
The NATURAL JOIN operation returns results that does not include the JOIN attributes of the second Relation B and is symbolized by the * symbol.
- A * ⟗ <join condition> B,
OR A * ⟗ (<join attributes 1>),
(<join attributes 2>) B
OR A * B
DIVISION
The DIVISION operation will return a Relation R(X) that includes all tuples t[X] in R(Z) that appear in R1 in combination with every tuple from R2(Y), where Z = X ∪ Y. The DIVISION operator is symbolized by ∻ symbol
R1(Z) ∻ R2(Y)
RC Notation – Non Procedural Expression Language
As Elmasri et al note, RC is a non procedural expression language that does not require any sequence to be followed in the expression of query transactions. One declarative statement could be used express a query transaction. [1]
The calculus query syntax as noted by Elmasri et al is:
{t | COND(t)}
Where t is the tuple variable.
COND(t) is the conditional expression.
The AND, OR, and NOT logical operators are also used in the expressions. An example of a RC expression is:
{t | STUDENT(t) AND t. GPA > 3.0}
This query would return all students whose GPA is greater than 3.0.
Expressions and Formulas in RC as noted by Elmasri et al are in the form:
{t1.Aj, t2.Ak,…, tn.Am | COND(t1, t2, …, tn, tn+1, tn+2, …, tn+m)}
Where COND is the condition or formula.
Existential and Universal Quantifiers
Elmasri et al further note that in RC quantifies that are largely unique to RC can appear in fomulas. The quantifiers include:
Universal quantifier (∀): Is true if all tuples make the formula true.
Existential quantifier (∃): Is true if there exists some tuple that makes the formula true
Example would include:
List the first name, last name and address of Students whose Major is Computer Science.
Q1: {s.Fname, s.Lname, s.Address | STUDENTS(s) AND (∃ m) (MAJOR(m)
AND m.major=’Computer Science’ AND m.studentnumb=t.studentnumb)}
SQL Notation
SQL is a structured query language, a comprehensive database language with data definition, queries, and updates.[1] It is with SQL that RA and RC are implemented physically in a database management program. Therefore this is the major difference between RA, RC and SQL, in that SQL will take the conceptual expressions of RA and RC and implement them at the computer level or physical level. SQL includes many declarative statements that it would take another article to write all of them but below are some few included here:
- Create: used to create schema and tables
- Insert: used to insert data to the tables
- Select: used to select tuples that satisfy a condition, also used to select attributes.
- Select fname From STUDENTS Where major = ‘computer science’: selects first name of students from the students table whose major is computer science.
It is noted that RC is much easier to implement in SQL as the RC notation is similar to SQL syntax.
Conclusion
Therefore RA and RC major differences are that RC is non procedural while RA is procedural and rigid as such. However, another notable difference is that it is easy to implement RC expressions directly to SQL language.
Sources
[1] Ramez Elmasri, Shamkant B. Navathe, Fundamentals of Database Systems, Edition 6, Addison Wesley Pub Co Inc, 2010, ISBN 0136086209, 9780136086208, Page 145 – 186