0% found this document useful (0 votes)
6 views26 pages

Relational Algebra

Relational algebra is a procedural query language used for querying databases, utilizing a collection of operators to describe step-by-step procedures for obtaining results. It contrasts with relational calculus, which is non-procedural and declarative. Key operations in relational algebra include selection, projection, set operations, joins, and division, allowing for complex queries on relational data.
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)
6 views26 pages

Relational Algebra

Relational algebra is a procedural query language used for querying databases, utilizing a collection of operators to describe step-by-step procedures for obtaining results. It contrasts with relational calculus, which is non-procedural and declarative. Key operations in relational algebra include selection, projection, set operations, joins, and division, allowing for complex queries on relational data.
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/ 26

Relational Algebra

• Query languages are specialized languages for asking questions, or queries,


that involve the data in a database.
• Queries in relational algebra are composed using a collection of operators,
and each query describes a step-by-step procedure for computing the
desired answer; that is, queries are specified in an operational manner.
• Relational algebra is procedural.
• In contrast to Relational Algebra, Relational Calculus is a non-procedural
query language, that is, it tells what to do but never explains how to do it.
This nonprocedural style of querying is called declarative.
• We present a number of sample queries using the following schema:
• Sailors(sid: integer, sname: string, rating: integer, age: real)
• Boats(bid: integer, bname: string, color: string)
• Reserves(sid: integer, bid: integer, day: date)
• Relational algebra is one of the two formal query languages
associated with the relational model.
• Queries in algebra are composed using a collection of operators.
• A fundamental property is that every operator in the algebra accepts
(one or two) relation instances as arguments and returns a relation
instance as the result.
• This property makes it easy to compose operators to form a complex
query.
• We describe the basic operators of the algebra (selection, projection,
union, cross-product, and difference),
• Each relational query describes a step-by-step procedure for
computing the desired answer, based on the order in which operators
are applied in the query.
• Relational systems use algebra expressions to represent query
evaluation plans.
SELECTION,PROJECTION
• Relational algebra includes operators to select rows from a relation
(σ) and to project columns (π).
• These operations allow us to manipulate data in a single relation.
• Sailors relation shown in Figure 4.2, denoted as S2.
• We can retrieve rows corresponding to expert sailors by using the σ
operator. The expression σ rating>8(S2) evaluates to the relation
• The projection operator π allows us to extract columns from a
relation;
• for example, we can find out all sailor names and ratings by using π.
The expression πsname, rating(S2)
• The schema of the result of a selection is the schema of the input
relation instance.
• The projection operator π allows us to extract columns from a
relation; for example, we can find out all sailor names and ratings by
using π.
• The expression πsname, rating(S2)
• The subscript sname, rating specifies the fields to be retained; the
other fields are ‘projected out.’
• πsname,rating(σrating>8(S2))
Set Operations
The following standard operations on sets are also available in
relational algebra:
union(Ս),
intersection (Ո),
set-difference (−),
cross-product (X).
Union: RՍS returns a relation instance containing all tuples
that occur in either relation instance R or relation instance S
(or both). R and S must be union-compatible, and the schema
of the result is defined to be identical to the schema of R.
• Two relation instances are said to be union-compatible if
the following conditions hold:
- they have the same number of the fields, and
- corresponding fields, taken in order from left to right, have
the same domains.
Intersection: RՈS returns a relation instance
containing all tuples that occur in both R and S.
The relations R and S must be union-compatible,
and the schema of the result is defined to be
identical to the schema of R.
Set-difference: R−S returns a relation instance
containing all tuples that occur in R but not in S.
The relations R and S must be union-compatible,
and the schema of the result is defined to be
identical to the schema of R.
Queries
• Find the names of sailors who have reserved boat 103. This query can
be written as follows:
• πsname((σbid=103Reserves) ⊲⊳ Sailors)
• Find the names of sailors who have reserved a red boat.
πsname((σcolor=′red′Boats) ⊲⊳ Reserves ⊲⊳ Sailors
• Find the colors of boats reserved by Lubber.
• πcolor((σsname=′Lubber′Sailors) ⊲⊳ Reserves ⊲⊳ Boats)
• Find the names of sailors who have reserved at least one boat.
• πsname(Sailors ⊲⊳ Reserves)
• Consider the Sailors-Boats-Reserves DB described in the text. s (sid, sname,
rating, age) b (bid, bname, color) r (sid, bid, date) Write each of the following
queries in RA
• 1. Find the colors of boats reserved by Albert.
• 2. Find all sailor id’s of sailors who have a rating of at least 8 or reserved boat
103.
• 3. Find the names of sailors who have not reserved a red boat.
• 4. Find the sailor id’s of sailors with age over 20 who have not reserved a red
boat.
• 5. Find the names of sailors who have reserved at least two boats.
• 6. Find the names of sailors who have reserved all boats.
Cross-product: RxS returns a relation instance
whose schema contains all the fields of R (in the
same order as they appear in R) followed by all the
fields of S(in the same order as they appear in S).
The result of RxS contains one tuple ‹r, s› (the
concatenation of tuples r and s) for each pair of
tuples rεR; sεS. The cross-product operation is
sometimes called Cartesian product.
• Joins
-The join operation is one of the most useful operations in
relational algebra and the most commonly used way to
combine information from two or more relations.

Following are several variants of Join operation


• Conditional join
• Equijoin
• Natural join
Condition Joins
• The most general version of the join operation accepts a
join condition c and a pair of relation instances as
arguments, and returns a relation instance.
• The join condition is identical to a selection condition in
form.
• The operation is defined as follows:
R c S = σc(RxS)

0
• Thus is defined to be a cross-product followed by a
0

selection.
• Note that the condition c can (and typically does) refer to
attributes of both R and S.
Equijoin
•When join uses only equality comparison operator,
it is said to be equijoin.
•when the join condition consists solely of equalities
(connected by ^) of the form R.name1 = S.name2, that
is, equalities between two fields in R and S.
•For join conditions that contain only such equalities,
the join operation is refined by doing an additional
projection.
•The join operation with this refinement is called
equijoin.
Sql Query for EquiJoin
SELECT column_list
FROM table1, table2....
WHERE table1.column_name =
table2.column_name;

or

SELECT *
FROM table1
JOIN table2
[ON (join_condition)]
Natural Join
• a natural join, and it has the nice property that the result is
guaranteed not to have two fields with the same name.
• The equijoin expression S1 ⊲⊳R.sid=S.sid R1 is actually a natural join
and can simply be denoted as S1 ⊲⊳ R1, since the only common field
is sid.
• If the two relations have no attributes in common, S1 ⊲⊳ R1 is simply
the cross-product.
• Select * from table1 naturaljoin table2
• Division
• The division operator is useful for expressing certain kinds of queries,
for example:
• “Find the names of sailors who have reserved all boats.”
• the division operator does not have the same importance as the other
operators—it is not needed as often, and database systems do not try
to exploit the semantics of division by implementing it as a distinct
operator
• We can compute disqualified tuples using the algebra expression
• πx((πx(A) × B) − A)
• Thus we can define A/B as
• πx(A) − πx((πx(A) × B) − A

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy