0% found this document useful (0 votes)
13 views42 pages

Relational Algebra & Calculus

Uploaded by

Pradeep Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views42 pages

Relational Algebra & Calculus

Uploaded by

Pradeep Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 42

Relational Algebra & Calculus

Introduction
• Relational algebra
– Basic set of operations for the relational model
– More operational (procedural), very useful for
representing execution plans.

• Relational calculus
– Higher-level declarative language for specifying
relational queries
– Lets users describe what they want, rather than
how to compute it: Non-operational, declarative.
Relational Algebra
• Relational algebra operations work on one or more relations to define
another relation without changing the original relations.

• Both operands and results are relations, so output from one


operation can become input to another operation.

• Allows expressions to be nested, just as in arithmetic. This property is


called closure.

• Five basic operations in relational algebra: Selection, Projection,


Cartesian product, Union and Set Difference.

• These perform most of the data retrieval operations needed.

• Also have Join, Intersection, and Division operations, which can be


expressed in terms of 5 basic operations.
Relational Algebra

• Basic operations:
– Selection (  ) Selects a subset of rows from relation.
– Projection ( 
) Deletes unwanted columns from relation.
– Cross-product ( ) Allows us to combine two relations.
– Set-difference (
– Union (

) Tuples in reln. 1, but not in reln. 2.
 ) Tuples in reln. 1 and in reln. 2.
• Additional operations:
– Intersection, join, division, renaming: Not essential, but (very!) useful.
Relational Algebra Operations
Relational Algebra Operations
Selection (or Restriction)
• predicate (R)
– Works on a single relation R and defines a relation that contains only
those tuples (rows) of R that satisfy the specified condition (predicate).
– Selects rows that satisfy selection condition.
– No duplicates in result!
– Schema of result identical to schema of input relation.
– Selection is distributive over binary operators
– Selection is commutative

7
Example - Selection Restriction)
• List all staff with a salary greater than £10,000.

salary > 10000 (Staff)


Projection
• col1, . . . , coln(R)
– Works on a single relation R and defines a relation that
contains a vertical subset of R, extracting the values of
specified attributes and eliminating duplicates.
– 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
input relation.
– Projection operator has to eliminate duplicates!

Note: real systems typically don’t do duplicate elimination unless the user
explicitly asks for it (by DISTINCT). Why not?
Example - Projection
• Produce a list of salaries for all staff, showing only staffNo, fName,
lName, and salary details.

staffNo, fName, lName, salary(Staff)


Union
• RS
– Union of two relations R and S defines a relation
that contains all the tuples of R, or S, or both R
and S, duplicate tuples being eliminated.
– R and S must be union-compatible.

• If R and S have I and J tuples, respectively, union is obtained by


concatenating them into one relation with a maximum of (I + J) tuples.
Example - UNION
• List all cities where there is either a branch office or a property for rent.

city(Branch)  city(PropertyForRent)
Set Difference
• R–S
– Defines a relation consisting of the tuples that are
in relation R, but not in S.
– R and S must be union-compatible.
Example - Set Difference
• List all cities where there is a branch office but no properties for rent.

city(Branch) – city(PropertyForRent)
Intersection
• RS
– Defines a relation consisting of the set of all tuples
that are in both R and S.
– R and S must be union-compatible.

• Expressed using basic operations:


R  S = R – (R – S)
Example - Intersection
• List all cities where there is both a branch office and at least one property
for rent.

city(Branch)  city(PropertyForRent)
Cartesian product
• RXS
– Defines a relation that is the concatenation of
every tuple of relation “R with every tuple of
relation S”
Example - Cartesian product
• List the names and comments of all clients who have viewed a property for
rent.
(clientNo, fName, lName(Client)) X (clientNo, propertyNo, comment
(Viewing))

18
Pearson Education © 2009
Join Operations
• JOINs can be used to combine tables
• Join is a derivative of Cartesian product.

• Equivalent to performing a Selection, using join predicate as selection


formula, over Cartesian product of the two operand relations.

• One of the most difficult operations to implement efficiently in an


RDBMS and one reason why RDBMSs have intrinsic performance
problems.

19
Pearson Education © 2009
Join Operations
• Various forms of join operation
– JOIN (Inner Join) : Return rows when there is at least
one match in both tables
– LEFT JOIN(Left Outer Join): Return all rows from the
left table, even if there are no matches in the right table
– RIGHT JOIN(Right Outer Join): Return all rows from the
right table, even if there are no matches in the left table
– FULL JOIN: Return rows when there is a match in one
of the tables

20
Pearson Education © 2009
SQL INNER JOIN Syntax
SELECT column_name(s) FROM table_name1 INNER JOIN table_name2
ON
table_name1.column_name=table_name2.column_name

INNER JOIN is the same as JOIN


The word "INNER" is optional
Example Inner Join
Left Join
Example - Left Join
Right Join
Example – Right Join
Full Join
Example – Full Join
Relational Calculus
Relational Calculus

•Relational Algebra is a PROCEDURAL


LANGUAGE
 we must explicitly provide a sequence of operations to
generate a desired output result

•Relational Calculus is a DECLARATIVE


LANGUAGE
 we specify what to retrieve, not how to retrieve it
What is Relational Calculus?

• It is a formal language based upon predicate


calculus
• It allows you to express the set of tuples you
want from a database using a formula
• Expressions in the calculus are called formula
• An answer tuple is essentially an assignment
of constants to variables that make the formula
evaluate to true.
Declarative ~ Non-Procedural
Two variants of relational calculus are
• TUPLE Relational Calculus
• Domain Relational Calculus
– Calculus has variables, constants, comparison
ops, logical connectives and quantifiers.
– TRC: Variables range over (i.e., get bound to)
tuples.
– DRC: Variables range over domain elements (=
field values)
TUPLE RELATIONAL CALCULUS
• Query: {T|P(T)}
– T is tuple variable
– P(T) is a formula that describes T

Result: the set of all tuples t for which P(t)


evaluates True.
 Find all sailors with a rating above 7.
OPERATIONS in Tuple Relational
Calculus
• ∧ - AND
• ∨ - OR
• ¬ - NOT
• ∃x – there exists x
• ∀x – for all x

Note: General power of 1st order predicate calculus – allow more flexible expression
formats
Tuple Relational Calculus
• A logical language with variables ranging over tuples:
{ T | Cond }
– Return all tuples T that satisfy the condition Cond.
• { T | R(T) }: returns all tuples T such that T is a tuple in relation R.
• { T.name | F ACULT Y (T) AND T.DeptId = ‘CS’ }.
returns the values of name field of all faculty tuples with the value
’CS’ in their department id field.
– The variable T is said to be free since it is not bound by a quantifier
(for all, exists).
– The result of this statement is a relation (or a set of tuples) that
correspond to all possible ways to satisfy this statement.
– Find all possible instances of T that make this statement true.
TUPLE RELATIONAL CALCULUS
TUPLE RELATIONAL CALCULUS
JOINS IN THE TUPLE RELATIONAL
CALCULUS
Domain Relational Calculus
• Query has the form:
 



x1, x2,..., xn | p x1, x2,..., xn




 
   


x1, x2,..., xn
Answer includes all tuples that
make the formula p x1, x2,..., xn 
 be true.
 
 

Formula is recursively defined, starting with simple atomic formulas


(getting tuples from relations or making comparisons of values), and
building bigger and better formulas using the logical connectives.
Domain Relational Calculus
Domain Relational Calculus
• Query has the form:
{ <x1,x2….xn>| p(<x1,x2….xn>)}
• Domain Variable – ranges over the values in the
domain of some attribute or is a constant
• Example: If the domain variable x1 maps to
attribute - Name char(20) then x1 ranges over all
strings that are 20 characters long
Not just the strings values in the relation’s attribute
• Answer includes all tuples that make the formula
p() true.
DOMAIN RELATIONAL CALCULUS

You might also like