• Tidak ada hasil yang ditemukan

COP 5725 Fall 2012 Database Management Systems

N/A
N/A
Protected

Academic year: 2019

Membagikan "COP 5725 Fall 2012 Database Management Systems"

Copied!
68
0
0

Teks penuh

(1)

COP 5725 Fall 2012

Database Management

Systems

University of Florida, CISE Department Prof. Daisy Zhe Wang

Adapted Slides from Prof. Jeff Ullman

(2)

Relational Algebra

(3)

Relational Query Languages

Query languages:

Allow manipulation and

retrieval of data from a database.

• Relational model supports simple, powerful, declarative QLs with precise semantics:

– Strong formal foundation based on logic.

– Allows for much optimization.

(4)

Preliminaries

• A query is applied to

relation instances

, and the result of a query is also a relation instance.

– Schemas of input and output relations for a

query are fixed

• Since each operation returns a relation,

(5)

Formal Relational Query

Languages

• Two mathematical Query Languages

and one “real” QL language (i.e., SQL)

– Relational Algebra: More operational, very useful for representing execution plans.

– Relational Calculus/SQL: Lets users

describe what they want, rather than how

to compute it. (Non-operational,

(6)

What is an “Algebra”

• Mathematical system consisting of:

– Operands --- variables or values from which new values can be constructed.

– Operators --- symbols denoting procedures that construct new values from given

(7)

What is Relational Algebra?

• An algebra whose operands are

relations or variables that represent relations.

• Operators are designed to do the most

common things that we need to do with relations in a database.

– The result is an algebra that can be used

(8)

Roadmap

• core relational algebra

(9)

Core Relational Algebra

• Union, intersection, and difference.

– Usual set operations, but require both

operands have the same relation schema.

• Selection: picking certain rows.

• Projection: picking certain columns.

• Products and joins: compositions of relations.

(10)

Selection

• R1 := SELECTC (R2)

– C is a condition (as in “if” statements) that refers to attributes of R2.

(11)

Example

JoeMenu := SELECTbar=“Joe’s”(Sells):

bar beer price

Joe’s Bud 2.50

(12)

Projection

• R1 := PROJL (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.

(13)

Example

(14)

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.

(15)
(16)

Theta-Join

• R3 := R1 JOINC R2

– Take the product R1 R2.

– Then apply SELECTC to the result.

C

can be any boolean-valued condition.

– Historic versions of this operator allowed

only A  B, where  is =, <, etc.; hence

the name “theta-join.”

(17)
(18)

Natural Join

• A frequent type of join connects two relations by:

– Equating attributes of the same name, and

– Projecting out one copy of each pair of

equated attributes.

• Called

natural

join.

(19)

Example

Note Bars.name has become Bars.bar to make the natural

(20)

Renaming

• The RENAME operator gives a new

schema to a relation.

• R1 := RENAMER1(A1,…,An)(R2) makes R1 be a relation with attributes A1,…,A

n

and

the same tuples as R2.

(21)

Example

Bars( name, addr ) Joe’s Maple St.

Sue’s River Rd.

R( bar, addr ) Joe’s Maple St.

Sue’s River Rd.

(22)

Building Complex Expressions

• Combine operators with parentheses

and precedence rules.

• Three notations, just as in arithmetic:

(23)

Sequences of Assignments

• Example: R3 := R1 JOINC R2 can be written:

R4 := R1 R2

R3 := SELECTC (R4)

• Create temporary relation names. • Renaming can be implied by giving

relations a list of attributes.

(24)

Expressions in a Single Assignment

• Example: the theta-join R3 := R1 JOINC R2 can be written: R3 := SELECTC (R1 R2)

• Precedence of relational operators:

1. [SELECT, PROJECT, RENAME] (highest). 2. [PRODUCT, JOIN].

3. INTERSECTION.

(25)

Expression Trees

• Leaves are operands --- either variables standing for relations or particular,

constant relations.

(26)

Example

• Using the relations Bars(name, addr)

and Sells(bar, beer, price), find the

(27)

As a Tree:

Bars Sells

SELECTaddr = “Maple St.” SELECTprice<3 AND beer=“Bud” PROJECTname

RENAMER(name)

(28)

Example

• Using Sells(bar, beer, price), find the bars that sell two different beers at the same price.

(29)

The Tree

Sells Sells

RENAMES(bar, beer1, price)

JOIN

PROJECTbar

(30)

Schemas for Results

• Union, intersection, and difference: the schemas of the two operands must be the same, so use that schema for the result.

• Selection: schema of the result is the same as the schema of the operand.

(31)

Schemas for Results --- (2)

• Product: schema is the attributes of both relations.

– Use R.A, etc., to distinguish two attributes

named A.

• Theta-join: same as product.

• Natural join: union of the attributes of the two relations.

(32)

Relational Algebra on Bags

• A

bag

(or

multiset

) is like a set, but an element may appear more than once. • Example: {1,2,1,3} is a bag.

(33)

Why Bags?

• SQL, the most important query

language for relational databases, is actually a bag language.

(34)

Operations on Bags

• Selection applies to each tuple, so its effect on bags is like its effect on sets.

• Projection also applies to each tuple, but as a bag operator, we do not

eliminate duplicates.

(35)

Example: Bag Selection

R( A, B )

1 2 5 6 1 2

(36)

Example: Bag Projection

R( A, B ) 1 2 5 6 1 2

(37)
(38)

Example: Bag Theta-Join

R( A, B ) S( B, C )

1 2 3 4

5 6 7 8

1 2

R JOIN R.B<S.B S = A R.B S.B C

1 2 3 4

(39)

Bag Union

• An element appears in the union of two bags the sum of the number of times it appears in each bag.

(40)

Bag Intersection

• An element appears in the intersection of two bags the minimum of the

number of times it appears in either. • Example: {1,2,1,1} INTER {1,2,1,3} =

(41)

Bag Difference

• An element appears in the difference

A

B

of bags as many times as it appears in

A

, minus the number of times it appears in

B

.

– But never less than 0 times.

(42)

Beware: Bag Laws != Set Laws

• Some, but

not

all

algebraic laws that hold for sets also hold for bags.

• Example: the commutative law for union (

R

UNION

S

=

S

UNION

R

)

does

hold for bags.

– Since addition is commutative, adding the

(43)

Example of the Difference

• Set union is

idempotent

, meaning that

S

UNION

S

=

S

.

• However, for bags, if

x

appears

n

times in

S

, then it appears 2

n

times in

S

UNION

S

.

(44)

The Extended Algebra

1. DELTA = eliminate duplicates from bags. 2. TAU = sort tuples.

3. Extended

projection

: arithmetic, duplication of columns.

4. GAMMA = grouping and aggregation.

(45)

Duplicate Elimination

• R1 := DELTA(R2).

(46)

Example: Duplicate Elimination

R = ( A B )

1 2 3 4 1 2

(47)

Sorting

– Break ties arbitrarily.

(48)

Example: Sorting

R = ( A B )

1 2 3 4 5 2

(49)

Extended Projection

• Using the same PROJL operator, we allow the list

L

to contain arbitrary expressions involving attributes, for example:

1. Arithmetic on attributes, e.g., A+B.

(50)

Example: Extended Projection

R = ( A B )

1 2 3 4

(51)

Aggregation Operators

• Aggregation operators are not operators of relational algebra.

• Rather, they apply to entire columns of a table and produce a single result.

• The most important examples: SUM,

(52)

Example: Aggregation

R = ( A B )

1 3 3 4 3 2

(53)

Grouping Operator

• R1 := GAMMAL (R2).

L

is a list of elements that are either:

1. Individual (grouping ) attributes.

2. AGG(A ), where AGG is one of the

aggregation operators and A is an

(54)

Applying GAMMA

L

(R)

• Group

R

according to all the grouping attributes on list

L

.

– That is: form one group for each distinct list

of values for those attributes in R.

• Within each group, compute AGG(

A

) for each aggregation on list

L

.

(55)
(56)

Outerjoin

• Suppose we join

R

JOINC

S

.

• A tuple of

R

that has no tuple of

S

with which it joins is said to be

dangling

.

– Similarly for a tuple of S.

• Outerjoin preserves dangling tuples by

(57)

Example: Outerjoin

R = ( A B ) S = ( B C )

1 2 2 3

4 5 6 7

(1,2) joins with (2,3), but the other two tuples are dangling.

R OUTERJOIN S = A B C 1 2 3

(58)

Division

• Not supported as a primitive operator, but

useful for expressing queries like:

Find sailors who have reserved all

boats

. • Let

A

have 2 fields,

x

and

y

;

B

have only field

y

:

A/B =

– i.e., A/B contains all x tuples (sailors) such

(59)

Example Instances

sid sname rating age

22 dustin 7 45.0

31 lubber 8 55.5

58 rusty 10 35.0

Sailors

sid bid day

22 101 10/10/96 58 103 11/12/96

(60)
(61)

Expressing A/B Using Basic Operators

• Division is not essential op; just a useful shorthand.

Idea

: For

A/B

, compute all

x

values that are not `disqualified’ by some

y

value in

B

.

– x value is disqualified if by attaching y value from

B, we obtain an xy tuple that is not in A.

Disqualified x values:

A/B:

x (( x A( ) B) A)

(62)
(63)

Find names of sailors who’ve reserved boat #103

(64)

Find names of sailors who’ve reserved a red boat

• Information about boat color only available in Boats; so need an extra join:

sname

color red Boats serves Sailors

((

' ' ) Re )

  

A more efficient solution:

(65)

Find sailors who’ve reserved a red

Can also define Tempboats using union! (How?)

(66)

Find sailors who’ve reserved a red and a green boat

 ( ,  ((

' ' ) Re ))

Tempred

sid colorred Boats  serves

sname Tempred((  Tempgreen) Sailors)

 ( ,  ((

' ' ) Re ))

Tempgreen

(67)

Find the names of sailors who’ve reserved all boats

• Uses division; schemas of the input

relations to / must be carefully chosen:

 ( , ( 

, Re ) / ( ))

Tempsids

sid bid serves bid Boats

sname Tempsids(  Sailors)

 To find sailors who’ve reserved all ‘Interlake’ boats:

/ (

' ' )

 

bid bnameInterlake Boats

(68)

Summary

• The relational model has rigorously

defined query languages that are simple and powerful.

• Relational algebra is more operational; useful as internal representation for

query evaluation plans.

• Several ways of expressing a given

Referensi

Dokumen terkait

Rangkaian PSA yang dibuat terdiri dari dua keluaran, yaitu 5 volt dan 12 volt, keluaran 5 volt digunakan untuk mensupplay tegangan ke seluruh rangkaian, sedangkan keluaran 12

Tirto, Siwalan dan Wonokerto 1 Paket Peningkatan Kualitas Permukiman Kumuh Kec Sragi 1 Paket Stimulan Perumahan Swadaya bagi RTM Desa Wonokerto Wetan, Kec. Wonokerto

Bahasa C adalah bahasa pemrograman yang dapat dikatakan berada antara bahasa tingkat rendah (bahasa yang berorientasi pada mesin) dan bahasa tingkat tinggi (bahasa yang

Data rata-rata dari hasil minat siswa antara kelas kontrol yaitu 8D dan kelas perlakuan yaitu 8E menunjukkan dari setiap indikator bahwa kelompok perlakuan

Adapun topik bahasan yang dipilih dalam Laporan Tugas Akhir ini adalah mengenai rangkaian mikrokontroler dan pemrograman bahasa C yang input kendalinya berasal dari water flow

ASUHAN KEPERAWATAN LANJUT USIA Pada Ny.P DENGAN DIAGNOSA MEDIS RHEUMATOID ARTHTRITIS DI UPT PELAYANAN.. SOSIAL LANJUT USIA PASURUAN BABAT

Rencana Terpadu dan Program Investasi Infrastruktur Jangka Menengah (RPI2-JM) Kabupaten Batu Bara merupakan dokumen rencana dan program pembangunan infrastruktur

untuk memenuhi persyaratan memperoleh gelar sarjana kependidikan Program Studi Pendidikan Matematika di Fakultas Keguruan dan Ilmu Pendidikan, Universitas