Relational algebra
Relational algebra
Relational algebra
Objectives
• How to form queries in relational algebra
Introduction
• Similar to normal algebra (as in 2+3*x-y), except we use
relations as values instead of numbers, and the
operations and operators are different.
• Not used as a query language in actual DBMSs. (SQL
instead.)
• The inner, lower-level operations of a relational DBMS
are, or are similar to, relational algebra operations. We
need to know about relational algebra to understand
query execution and optimization in a relational DBMS.
• Some advanced SQL queries requires explicit relational
algebra operations, most commonly outer join.
• Relations are seen as sets of tuples, which means that
no duplicates are allowed. SQL behaves differently in
some cases. Remember the SQL keyword distinct.
• SQL is declarative, which means that you tell the
DBMS what you want, but not how it is to be
calculated. A C++ or Java program is procedural, which
means that you have to state, step by step, exactly how
the result should be calculated. Relational algebra is
(more) procedural than SQL. (Actually, relational
algebra is mathematical expressions.)
Relational Algebra
• Relational algebra is high level procedural
language
• Relational algebra operations work on one or
more relations to define another relation with
out changing original relations
• Both operands and results are relations
– Output from one relation can be input to another
• Allows nested expressions
– Closure property
Review of concepts and operations from set theory
• set
• element
• no duplicate elements (but: multiset = bag)
• no order among the elements (but: ordered set)
• subset
• proper subset (with fewer elements)
• superset
• union
• intersection
• set difference
• cartesian product
Relational Algebra
• Basic operations
– Selection, Projection, Union, Set difference,
Cartesian product and Rename
• Perform most required data retrieval
operations
• Additional operations
– join, set intersection, Division and assignment
– Can be expressed in terms of the basic operations.
Basic Operations
• x(E)
• To rename the relation given by an expression
E to the name x:
Example- Rename
• csdeg_students (programme_code=”CSDEG” (student))
– The operation returns the relation given by the expression, and the relation
is named csdeg_students. Can now use that name in further operations.
• To find any students named Solomon who are on the CSDEG
programme:
– stud_firstname=”Solomon” ( csdeg_students (programme_code=”CSDEG” (student)))
• Or, can simply rename a relation, as a relation is itself a trivial
relational algebra expression:
– student2(student)
• We can also rename the attributes in the relation, using syntax like
this: x (A1,A2,….An)(E)
– If the expression E has arity n (n attributes); A1 is a name for the first
attribute, An is a name for the nth attribute.
Example- Rename
find a course with highest credit hours
• course.credit_hours (course) –
• course.credit_hours (course.credit_hours < c.credit_hours (course
x c(course)))
Additional Operations