Chapter4 RelationalAlgebra

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 15

Relational Algebra

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

• The basic set of operations for the relational


model is known as the relational algebra.
• Relational algebra is a theoretical language with
operations that work on one or more relations
to define another relation without changing the
original relation.
• The result of the retrieval is a new relation,
which may have been formed from one or more
relations
Relational Algebra

• A sequence of relational algebra operations


forms a relational algebra expression, whose
result will also be a relation that represents
the result of a database query
• The output from one operation can become
the input to another operation (nesting is
possible)
Unary Operations
• Selection
• Selects subset of tuples/rows in a relation that satisfy selection condition.
• Selection operation is a unary operator (it is applied to a single relation)
• The Selection operation is applied to each tuple individually
• The degree of the resulting relation is the same as the original relation but the
cardinality (no. of tuples) is less than or equal to the original relation.
• The Selection operator is commutative.
• Set of conditions can be combined using Boolean operations (∧(AND), ∨(OR), and
~(NOT))
• No duplicates in result!
• Schema of result identical to schema of (only) input relation.
• It is a filter that keeps only those tuples that satisfy a qualifying condition
Notation:

•ᵟ <Selection Condition>
<Relation Name>
Unary Relational Operations
SELECT Operation Properties
– The SELECT operation  is commutative; i.e.,
 <condition1>( < condition2> ( R)) =  <condition2> ( < condition1> ( R))

– A cascaded SELECT operation may be applied in any order; i.e.,


 <condition1>( < condition2> ( <condition3> ( R))
=  <condition2> ( < condition3> ( < condition1> ( R)))

– A cascaded SELECT operation may be replaced by a single selection with


a conjunction of all the conditions; i.e.,
 <condition1>( < condition2> ( <condition3> ( R))
=  <condition1> AND < condition2> AND < condition3> ( R)))
• Example: Find all Employees with skill type of Database.
• < SkillType =”Database”>
(Employee)
• < SkillType =”Database” AND School=”Unity”>
(Employee)
Chapter 6-6
Projection
• Selects certain attributes while discarding the other from the base relation.
• The PROJECT creates a vertical partitioning – one with the needed columns
(attributes) containing results of the operation and other containing the discarded
Columns.
• Schema of result contains exactly the fields in the projection list, with the same
names that they had in the (only) input relation.
• Projection operator has to eliminate duplicates!
– Note: real systems typically don’t do duplicate elimination unless the user explicitly asks
for it.
• If the Primary Key is in the projection list, then duplication will not occur
• Duplication removal is necessary to insure that the resulting table is also a relation.
• The number of tuples in the result of projection is always less/equal to that found in
R
• If the list of attributes includes a key of R, then the number of tuples is equal
• Notation:
• π <Selected Attributes> <Relation Name>
• Example: To display Name, Skill, and Skill Level of an employee, the query and the resulting
relation will be:
• π <FName, LName, Skill, Skill_Level> (Employee)
• π<list1>(π<list2>(R))= π<list1>(R)
Rename Operation
• We may want to apply several relational algebra operations one after the
other. The query could be written in two different forms:
• 1. Write the operations as a single relational algebra expression by nesting
the operations.
• 2. Apply one operation at a time and create intermediate result relations.
In the latter case, we must give names to the relations that hold the
intermediate results􀃎Rename Operation
• If we want to have the Name, Skill, and Skill Level of an employee with
salary greater than 1500 and working for department 5, we can write the
expression for this query using the two alternatives:
• A single algebraic expression:
– ∏<FN,LN,Skill, Level>(Б <salary 1500 ^ dno=5>(employee))
• Using an intermediate relation by the rename operation:
– Result1 Б <dno=5^salary>1500>(employee)
– Result ∏ <FN, LN, Skill,Level>(resullt1)
• UNION Operation
• The result of this operation, denoted by R U S, is a relation that
includes all tuples that are either in R or in S or in both R and S.
Duplicate tuples are eliminated.
• The two operands must be “type compatible”.
• Type Compatibility
• The operand relations R1(A1, A2, ..., An) and R2(B1, B2, ..., Bn) must
have the same number of attributes, and the domains of
corresponding attributes must be compatible; that is,
Dom(Ai)=Dom(Bi) for i=1, 2, ..., n.
• The resulting relation for;
• • R1 ∪ R2,
• • R1 ∩ R2, or
• • R1-R2 has the same attribute names as the first operand
relation R1 (by convention).
• INTERSECTION Operation
• The result of this operation, denoted by R ∩ S, is a
relation that includes all tuples that are in both R
and S. The two operands must be "type compatible"
• Set Difference (or MINUS) Operation
• The result of this operation, denoted by R - S, is a
relation that includes all tuples that are in R but not
in S. The two operands must be "type compatible”.
CARTESIAN (cross product) Operation

• This operation is used to combine tuples from two relations


in a combinatorial fashion. That means, every tuple in
Relation1(R) one will be related with every other tuple in
Relation2 (S).
• In general, the result of R(A1, A2, . . ., An) x S(B1,B2, . . ., Bm) is
a relation Q with degree n + m attributes Q(A 1, A2, . . ., An,
B1, B2, . . ., Bm), in that order.
• Where R has n attributes and S has m attributes.
• The resulting relation Q has one tuple for each combination
of tuples—one from R and one from S.
• Hence, if R has n tuples, and S has m tuples, then | R x S |
will have n* m tuples.
• The two relations need not be type compatible
JOIN Operation

• 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

You might also like