Chapter4 RelationalAlgebra
Chapter4 RelationalAlgebra
Chapter4 RelationalAlgebra
Query Languages
• varieties of Query languages for manipulating relations.
• Some of them are procedural : User tells what and how to manipulate the data
• Others are non-procedural : User states what data is needed rather than how it
is to be retrieved.
• Two mathematical Query Languages form the basis for Relational languages
• Relational Algebra:
• Relational Calculus:
• We may describe the relational algebra as procedural language: it can be used to
tell the DBMS how to build a new relation from one or more relations in the
database.
• We may describe relational calculus as a non procedural language: it can be used
to formulate the definition of a relation in terms of one or more database
relations.
• Both are non-user friendly languages. They have been used as the basis for other,
higher-level data manipulation languages for relational databases.
• A query is applied to relation instances, and the result of a query is also a relation
instance.
• Schemas of input relations for a query are fixed
• The schema for the result of a given query is also fixed! Determined by definition of
query language constructs.
Relational Algebra
•ᵟ <Selection Condition>
<Relation Name>
Unary Relational Operations
SELECT Operation Properties
– The SELECT operation is commutative; i.e.,
<condition1>( < condition2> ( R)) = <condition2> ( < condition1> ( R))
• The sequence of Cartesian product followed by select is used quite commonly to identify
and select related tuples from two relations, a special operation, called JOIN. Thus in JOIN
operation, the Cartesian Operation and the Selection Operations are used together.
• JOIN Operation is denoted by a symbol.
• This operation is very important for any relational database with more than a single
relation, because it allows us to process relationships among relations.
• The general form of a join operation on two relations
• R(A1, A2,. . ., An) and S(B1, B2, . . ., Bm) is:
• R <join condition>S Is equivalent to <selection condition>(R X S)
• where <join condition> and <selection condition> are the same
• Where R and S can be any relations that result from general relational algebra
expressions. Since JOIN function in two relation, it is a Binary operation.
• This type of JOIN is called a THETA JOIN (θ - JOIN)
• Where θ is the logical operator used in the join condition.
• θ Could be { <, ≤ , >, ≥, ≠, = }
• Example:
• Thus in the above example we want to extract employee information about managers of
the departments, the algebra query using the JOIN operation will be. Employee join dep’t
(empId=mgrId)
• EQUIJOIN Operation
• The most common use of join involves join
conditions with equality comparisons only ( = ).
Such a join, where the only comparison operator
used is called an EQUIJOIN. In the result of an
EQUIJOIN we always have one or more pairs of
attributes (whose names need not be identical)
that have identical values in every tuple since
we used the equality logical operator.
• NATURAL JOIN Operation
Because one of each pair of attributes with
identical values is superfluous, a new operation
called natural join—denoted by *—was created to
get rid of the second (superfluous) attribute in an
EQUIJOIN condition.
The standard definition of natural join requires
that the two join attributes, or each pair of
corresponding join attributes, have the same
name in both relations. If this is not the case, a
renaming operation is applied first.
• OUTER JOIN Operation
• OUTER JOIN is another version of the JOIN operation where non
matching tuples from the first Relation are also included in the
resulting Relation where attributes of the second Relation for a non
matching tuples from Relation one will have a value of NULL.
• Notation: R <join condition> S
• When two relations are joined by a JOIN operator, there could be
some tuples in the first relation not having a matching tuple from
the second relation, and the query is interested to display these
non matching tuples from the first relation. Such query is
represented by the OUTER JOIN
• R <Join Condition > S