0% found this document useful (0 votes)
41 views

3 - Chapter 2 - Relational Database Modeling - P2

RA provides a less powerful but more optimized query language for databases. It is an algebraic language whose atomic operands are relations and constants. The six primitive operators are selection, projection, product, union, difference, and rename, which can be combined to form queries through expression trees.

Uploaded by

Trương Nhu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

3 - Chapter 2 - Relational Database Modeling - P2

RA provides a less powerful but more optimized query language for databases. It is an algebraic language whose atomic operands are relations and constants. The six primitive operators are selection, projection, product, union, difference, and rename, which can be combined to form queries through expression trees.

Uploaded by

Trương Nhu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Database Systems

Session 3
Chapter 2 - Relational
Database Modeling - Part 2
Objectives

1 Understand why do we need Algebraic Query Language

2 Understand how Algebraic Query Language work


Contents

1 Why do we need Algebraic Query Language

2 An Algebraic Query Language


2.4. An Algebraic Query
Language
2.4.1. Why do we need a special Query Language?

One should ask why we need a new kind of


programming languages for databases?
Won’t conventional languages like C or Java
suffice to ask and answer any computable
question about relations? Because we can
represent a tuple of a relation by struct (in C) or
an object (in Java) and we can represent
relations by arrays of these elements.
The surprising answer is that RA is useful
because it is less powerful than C or Java
2.4.2. What is an Algebra?

An algebra, in general, consist of


operators and atomic operands
RA is another example of an algebra: its
atomic operands are:
 Variables that stand for relations
 Constants, which are finite relations
2.4.2. Relational algebra definition

Relational algebra, an offshoot of algebra


of sets via operators
Operators operate on one or more
relations to create a new relation
2.4.3 Overview of relational algebra

In any algebra, some operators are primitive and


the others, being definable in terms of the
primitive ones, are derived
The six primitive operators of relational algebra
are:
 the SELECTION,
 the PROJECTION,
 the CARTESIAN PRODUCT (also called the cross
product or cross join),
 the SET UNION
 the SET DIFFERENCE, and
 the RENAME
2.4.4. Set Operations on Relations - Set Union

Sells1 Sells2
bar beer price bar beer price
Joe's Bud 2.50 Joe's Bud 2.50
Joe's Miller 2.75 Joe's Miller 2.75
Sue's Bud 2.50 Sue's Miller 3.00

Sells1 U Sells2
bar beer price
Joe's Bud 2.50
Joe's Miller 2.75
Sue's Bud 2.50
Sue's Miller 3.00
2.4.4. Set Operations on Relations - Set Except

Sells1 Sells2
bar beer price bar beer price
Joe's Bud 2.50 Joe's Bud 2.50
Joe's Miller 2.75 Joe's Miller 2.75
Sue's Bud 2.50 Sue's Miller 3.00

Sells1 \ Sells2
bar beer price
Sue's Bud 2.50
2.4.4. Set Operations on Relations - Set Intersection

This

Sells1 Sells2
bar beer price bar beer price
Joe's Bud 2.50 Joe's Bud 2.50
Joe's Miller 2.75 Joe's Miller 2.75
Sue's Bud 2.50 Sue's Miller 3.00

Sells1 ∩ Sells2
bar beer price
Joe's Bud 2.50
Joe's Miller 2.75
2.4.5. Projection

R1 := πL (R2)
 L is a list of attributes from the schema of R2.
 R1 is constructed by looking at each tuple of
R2, extracting the attributes on list L, in the
order specified, and creating from those
components a tuple for R1.
 Eliminate duplicate tuples, if any.
Example: Projection

Relation Sells:
bar beer price
Joe’s Bud 2.50
Joe’s Miller 2.75
Sue’s Bud 2.50
Sue’s Miller 3.00

Prices := πbeer, price(Sells):


beer price
Bud 2.50
Miller 2.75
Miller 3.00
Extended Projection

 Using the same πL operator, we allow


the list L to contain arbitrary expressions
involving attributes:
1. Arithmetic on attributes, e.g., A+B->C.
2. Duplicate occurrences of the same
attribute.
Example: Extended Projection

R= (A B)
1 2
3 4

πA+B->C, A, A (R) = C A1 A2
3 1 1
7 3 3
2.4.6. Selection

R1 := σC (R2)
 C is a condition (as in “if” statements) that
refers to attributes of R2.
 R1 is all those tuples of R2 that satisfy C.
Example: Selection

Relation Sells:
bar beer price
Joe’s Bud 2.50
Joe’s Miller 2.75
Sue’s Bud 2.50
Sue’s Miller 3.00

JoeMenu := σbar=“Joe’s”(Sells):
bar beer price
Joe’s Bud 2.50
Joe’s Miller 2.75
2.4.7. Product

R3 := R1 Χ R2
 Pair each tuple t1 of R1 with each tuple t2
of R2.
 Concatenation t1t2 is a tuple of R3.
 Schema of R3 is the attributes of R1 and
then R2, in order.
 But beware attribute A of the same name in
R1 and R2: use R1.A and R2.A.
Example: R3 := R1 Χ R2

R1( A, B) R3( A, R1.B, R2.B, C )


1 2 1 2 5 6
3 4 1 2 7 8
1 2 9 10
R2( B, C) 3 4 5 6
5 6 3 4 7 8
7 8 3 4 9 10
9 10
2.4.8. Natural Join

A useful join variant (natural join)


connects two relations by:
 Equating attributes of the same name, and
 Projecting out one copy of each pair of
equated attributes.
Denoted R3 := R1 ⋈ R2.
Example: Natural Join

Sells( bar, beer, price ) Bars( bar, addr )


Joe’s Bud 2.50 Joe’s Maple St.
Joe’s Miller 2.75 Sue’s River Rd.
Sue’s Bud 2.50
Sue’s Coors 3.00

BarInfo := Sells ⋈ Bars


Note: Bars.name has become Bars.bar to make the natural
join “work.”
BarInfo( bar, beer, price, addr )
Joe’s Bud 2.50 Maple St.
Joe’s Milller 2.75 Maple St.
Sue’s Bud 2.50 River Rd.
Sue’s Coors 3.00 River Rd.
2.4.9 Theta Join

R ⋈ θ S

The result of theta join consists of all


combinations of tuples in two relations
R and S that satisfy θ condition
Example: Theta Join

R S
A B C D
1 1 2 2
1 2 3 2
2 3 4 1

R⋈B>=CS
A B C D
1 2 2 2
2 3 2 2
2 3 3 2
2.4.11. Renaming

The ρ operator gives a new schema to


a relation.
R1 := ρR1(A1,…,An)(R2) makes R1 be a
relation with attributes A1,…,An and
the same tuples as R2.
Simplified notation: R1(A1,…,An) := R2.
Example: Renaming

Bars( name, addr )


Joe’s Maple St.
Sue’s River Rd.

R(bar, addr) := Bars

R( bar, addr )
Joe’s Maple St.
Sue’s River Rd.
2.4.11 Combining Operations to Form Queries

Movies (title, year, length, genre, studioName)


What are titles and years of movies made by
Fox that are at least 100 minutes long?
To answer above question, see the steps
represented as an expression tree:
πtitle, year

σlength >= 100 σstudioName = ‘Fox’
Movies Movies
Summary

RA is more useful than C or Java because it is


less powerful.
RA is an algebra: its atomic operands are:
 Variables that stand for relations
 Constants, which are finite relations
The six primitive operators of RA are: Selection,
Projection, Product, Union, Difference and
Rename
Other operators of RA are: Natural Join, Theta
Join, …

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