# Relational Algebra Rohit Khokher. Relational Algebra Set Oriented Operations UnionIntersectionDifference Cartesian Product Relation Oriented Operations.

## Presentation on theme: "Relational Algebra Rohit Khokher. Relational Algebra Set Oriented Operations UnionIntersectionDifference Cartesian Product Relation Oriented Operations."— Presentation transcript:

Relational Algebra Rohit Khokher

Relational Algebra Set Oriented Operations UnionIntersectionDifference Cartesian Product Relation Oriented Operations SelectProjectRenameJoinDivision

 Relational algebra Types of operations Set theoretic operations Union operator Intersection operator Difference operator Cartesian product operator Selection operator Project operator Joins Division operation Some examples

 Union, Intersection and Difference  Operands need to be union compatible for the result to be a valid relation Definition : Two relations R1 and R2 are union compatible iff:  They have the same number of attributes.  Corresponding fields have the same type.

The result R1  R2 is a relation that includes all tuples that are either in R1 or in R2 or in both without duplicate tuples. The resulting relation might have the same attribute names as the first or the second relation.

R1 R2 R1 U R2

 The result R1  R2 is a relation that includes only those tuples : That are in R1 and also appear in R2.  The resulting relation might have the same attribute names as the first or the second relation

R1 R2

 The result R1  R2 is a relation that: includes all tuples that are in R1 and not in R2  The resulting relation might have the same attribute names as the first or the second relation

R1 R2 R1-R2

Definition:  Result schema has one field per field of P and Q, with field names `inherited’ if possible.  The result, denoted by PXQ, is a relation that includes all the possible combinations of tuples from P and Q  Used in conjunction with other operations

X PQ

 Selects rows that satisfy selection condition.  No duplicates in result!  Schema of result identical to schema of (only) input relation.  Result relation can be the input for another relational algebra operation! (Operator composition.)

S

 Deletes attributes that are not in projection list.  Schema of result contains exactly the fields in the projection list, with the same names that they had in the (only) input relation. Result =  (attribute list) (relation name) Resulting relation has only the attributes in the list, in same order as they appear in the list the degree(result) = number of attributes in the attribute list no duplicates in the result attribute list are drawn from the specified relation ;

S

stands for the usual comparison operators ‘ ‘, ‘>=‘, etc The result is a concatenated set but only for those tuples where the condition is true. It does not require union compatibility of R1 and R2 Result =  R1 (  join condition) R2  Fewer tuples than cross-product, might be able to compute more efficiently

R1 S1

 Definition: The division of two relations R1(A 1,A 2,...,A n ) with cardinality i and R2(B 1,B 2,...,B m ) with cardinality j is a relation R3 with degree k=n-m, cardinality i*j and attributes (A 1,A 2,...,A n,B 1,B 2,...,B m ) that satisfy the division condition  The principle algebraic counterpart of queries that involve the universal quantifier   Relational languages do not express relational division

A B1 A/B1 B2 A/B2 B3 A/B3

 Sailors: ( sid, sname, rating, age )  Boats: ( bid, color, bname )  Reserves: ( sid, bid, date )

Solution 1:

 Information about boat color only available in Boats; so need an extra join:  A more efficient solution:

Can identify all red or green boats, then find sailors who’ve reserved one of these boats:  What happens if is replaced by in this query?

 Previous approach won’t work! Must identify sailors who’ve reserved red boats, sailors who’ve reserved green boats, then find the intersection (note that sid is a key for Sailors):

Download ppt "Relational Algebra Rohit Khokher. Relational Algebra Set Oriented Operations UnionIntersectionDifference Cartesian Product Relation Oriented Operations."

Similar presentations