0% found this document useful (0 votes)
15 views23 pages

Database Systems Lec 5

Relational algebra and calculus provide formal query languages for relational databases. Relational algebra uses operators like selection, projection, union, and set difference to compose queries as step-by-step procedures, while relational calculus allows users to declaratively describe what data is wanted. Both help form the basis for SQL and optimize query processing.

Uploaded by

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

Database Systems Lec 5

Relational algebra and calculus provide formal query languages for relational databases. Relational algebra uses operators like selection, projection, union, and set difference to compose queries as step-by-step procedures, while relational calculus allows users to declaratively describe what data is wanted. Both help form the basis for SQL and optimize query processing.

Uploaded by

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

Chapter 4

Relational Algebra
and Calculus
Query Languages Are specialized
languages for asking
(e.g. SQL) questions.

Relational Algebra and


Calculus

Procedural: Declarative: Calculus


Algebra
Instance Instances
s of
Relations
Query of
A Relation
Relational Algebra
• Queries are composed using a collection
of operators.
• Every operator:
– Accepts one or two relation instances
– Returns a relation instance.
• Compose relational algebra expression
• Each query describes a step-by-step
procedure for computing the
desired answer.
Relational Query Languages
• Query languages: Allow manipulation and
retrieval of data from a database.
• Relational model supports simple, powerful QLs:
– Strong formal foundation based on logic.
– Allows for much optimization.
• Query Languages != programming languages!
– QLs not expected to be “Turing complete”.
– QLs not intended to be used for complex calculations.
– QLs support easy, efficient access to large data sets.
Formal Relational Query Languages

Two mathematical Query Languages form the


basis for “real” languages (e.g. SQL), and for
implementation:
Relational Algebra: More operational, very
useful for representing execution plans.
 Relational Calculus: Lets users describe
what they want, rather than how to
compute it. (Non-operational, declarative.)
 Understanding Algebra & Calculus is key to
 understanding SQL, query processing!
Relational Algebra
• Five basic operators
– Selection
– Projection
– Union
– Cross-product
– Difference
Relational Algebra Operations

Pearson Education © 2014 8


Selection
 (Input)
Selection_Criteria
A relation instance
Manipulates data in a single relation

The selection operator specifies the tuples to retain through selection


criteria.

A boolean combination (i.e. using V and ᴧ) of terms

Attribute op or attribute1 op attribute2


constant
< , <=, =, ≠, >=, or >
Selection
S2
sid sname rating age
28 yuppy 9 35.0  rating (S2)
31 lubber 8 55.5
44 guppy 5 35.0 8
58 rusty 10 35.0

sid sname rating age


28 yuppy 9 35.0
58 rusty 10 35.0
Projection
 (Input)
fields

Allows us to extract columns from a relation


Example:
sid sname rating age
28 yuppy 9 35.0 age
31 lubber 8 55.5  age 35.0
44 guppy 5 35.0 (S2) 55.5
58 rusty 10 35.0
sid sname rating age
28 yuppy 9 35.0
31 lubber 8 55.5
44 guppy 5 35.0
58 rusty 10 35.0

 sname,rating ( rating  8 (S 2))

sname rating
yuppy 9
rusty 10
Set Operations
• Takes as input two relation instances
• Four standard operations
– Union
– Intersection
– Set-difference
– Cross-product
• Union, intersection, and difference
require the two input set to be union
compatible
– They have the same number of fields
– corresponding fields, taken in order from left
to right, have the same domains
Set Operation: Union
• R U S returns relation instance
containing all tuples that occur in
either relation instance R or S, or both.
• R and S must be union compatible.
• Schema of the result is defined to be
that of R.
S1
Set Operation: Union S2
sid sname rating age sid sname rating age
22 dustin 7 45.0 28 yuppy 9 35.0
31 lubber 8 55.5
31 lubber 8 55.5
44 guppy 5 35.0
58 rusty 10 35.0 58 rusty 10 35.0

sid sname rating age


22 dustin 7 45.0
31 lubber 8 55.5 S1 U S2
58 rusty 10 35.0
44 guppy 5 35.0
28 yuppy 9 35.0
Set Operation: Intersection
• R ⋂ S: returns a relation instance
containing all tuples that occur in both R
and S.
• R and S must be union compatible.
• Schema of the result is that of R.
Set Operation: Intersection
S1 S2
sid sname rating age sid sname rating age
22 dustin 7 45.0 28 yuppy 9 35.0
31 lubber 8 55.5
31 lubber 8 55.5
44 guppy 5 35.0
58 rusty 10 35.0 58 rusty 10 35.0

sid sname rating age


31 lubber 8 55.5 S1 ⋂ S2
58 rusty 10 35.0
Set Operation: Set-Difference
• R – S: returns a relation instance
containing all tuples that occur in R
but not in S.
• R and S must be union-compatible.
• Scheme of the result is the schema of
R.
Set Operation: Set-Difference
S1 S2
sid sname rating age sid sname rating age
22 dustin 7 45.0 28 yuppy 9 35.0
31 lubber 8 55.5
31 lubber 8 55.5
44 guppy 5 35.0
58 rusty 10 35.0 58 rusty 10 35.0

S1 – S2
sid sname rating age
22 dustin 7 45.0
Set Operation: Cross-Product
• R x S: Returns a relation instance whose
scheme contains:
– All the fields of R (in the same order as
they appear in R)
– All the fields os S (in the same order as
they
appear in S)
• The result contains one tuple <r,s> for each
pair with r ⋳ R and s ⋳ S
• Basically, it is the Cartesian product.
• Fields of the same name are unnamed.
Set Operation: Cross-Product
S1 R1
sid sname rating age
sid bid day
22 dustin 7 45.0
22 101 10/10/96
31 lubber 8 55.5 58 103 11/12/96
58 rusty 10 35.0
S1 x R1
(sid) sname rating age (sid) bid day
22 dustin 7 45.0 22 101 10/10/96
22 dustin 7 45.0 58 103 11/12/96
31 lubber 8 55.5 22 101 10/10/96
31 lubber 8 55.5 58 103 11/12/96
58 rusty 10 35.0 22 101 10/10/96
58 rusty 10 35.0 58 103 11/12/96
Renaming
• Name conflict can arise in some
situations
• It is convenient to be able to give names
to the fields of a relation instance
defined by a relational algebra
expression.
(R(F),E)
•Take arbitrary relational expression E
• Returns an instance of a new relation R
• R is the result of E except that some fields are renamed
• Renaming list has the form (oldname  newname or position 
newname)
Renaming
(C(1sid1,5sid2),S1
R1)sname rating age (sid) bid day
(sid)
22 dustin 7 45.0 22 101 10/10/96
22 dustin 7 45.0 58 103 11/12/96
31 lubber 8 55.5 22 101 10/10/96
31 lubber 8 55.5 58 103 11/12/96
58 rusty 10 35.0 22 101 10/10/96
58 rusty 10 35.0 58 103 11/12/96

sid1 sname rating age sid2 bid day


22 dustin 7 45.0 22 101 10/10/96
22 dustin 7 45.0 58 103 11/12/96
31 lubber 8 55.5 22 101 10/10/96
31 lubber 8 55.5 58 103 11/12/96
58 rusty 10 35.0 22 101 10/10/96
58 rusty 10 35.0 58 103 11/12/96

You might also like