schedule - courses · example winter 2002 arthur keller – cs 180 5–12 sells = bars = barinfo =...

43
Winter 2002 Arthur Keller – CS 180 5–1 Schedule Today: Jan. 17 (TH) Relational Algebra. Read Chapter 5. Project Part 1 due. Jan. 22 (T) SQL Queries. Read Sections 6.1-6.2. Assignment 2 due. Jan. 24 (TH) Subqueries, Grouping and Aggregation. Read Sections 6.3-6.4. Project Part 2 due. Jan. 29 (T) Modifications, Schemas, Views. Read Sections 6.5-6.7. Assignment 3 due.

Upload: buinhi

Post on 30-Apr-2018

217 views

Category:

Documents


4 download

TRANSCRIPT

Win

ter

2002

Art

hur

Kel

ler

– C

S 18

05–

1

Sche

dule

•T

oday

: Jan

. 17

(TH

)◆

Rel

atio

nal A

lgeb

ra.

◆R

ead

Cha

pter

5. P

roje

ct P

art 1

due

.

•Ja

n. 2

2 (T

)◆

SQL

Que

ries

.◆

Rea

d Se

ctio

ns 6

.1-6

.2. A

ssig

nmen

t 2 d

ue.

•Ja

n. 2

4 (T

H)

◆Su

bque

ries

, Gro

upin

g an

d A

ggre

gatio

n.◆

Rea

d Se

ctio

ns 6

.3-6

.4. P

roje

ct P

art 2

due

.

•Ja

n. 2

9 (T

)◆

Mod

ific

atio

ns, S

chem

as, V

iew

s.◆

Rea

d Se

ctio

ns 6

.5-6

.7. A

ssig

nmen

t 3 d

ue.

Win

ter

2002

Art

hur

Kel

ler

– C

S 18

05–

2

“Cor

e” R

elat

iona

l Alg

ebra

A s

mal

l set

of

oper

ator

s th

at a

llow

us

to m

anip

ulat

ere

latio

ns in

lim

ited

but u

sefu

l way

s. T

he o

pera

tors

are

:

1.U

nion

, int

erse

ctio

n, a

nd d

iffe

renc

e: th

e us

ual s

etop

erat

ors.

◆B

ut th

e re

latio

n sc

hem

as m

ust b

e th

e sa

me.

2.Se

lect

ion:

Pic

king

cer

tain

row

s fr

om a

rel

atio

n.

3.P

roje

ctio

n: P

icki

ng c

erta

in c

olum

ns.

4.P

rodu

cts

and

join

s: C

ompo

sing

rel

atio

ns in

use

ful w

ays.

5.R

enam

ing

of r

elat

ions

and

thei

r at

trib

utes

.

Win

ter

2002

Art

hur

Kel

ler

– C

S 18

05–

3

Rel

atio

nal A

lgeb

ra

•lim

ited

expr

essi

ve p

ower

(su

bset

of p

ossi

ble

quer

ies)

•go

od o

ptim

izer

pos

sibl

e

•ric

h en

ough

lang

uage

to e

xpre

ss e

noug

h us

eful

thin

gs

Fin

itene

ss

σ S

ELE

CT

π P

RO

JEC

T

X C

AR

TE

SIA

N P

RO

DU

CT

FU

ND

AM

EN

TA

L

U U

NIO

N

BIN

AR

Y

– S

ET

-DIF

FE

RE

NC

E

∩ S

ET

-IN

TE

RS

EC

TIO

N

θ T

HE

TA

-JO

IN

C

AN

BE

DE

FIN

ED

N

AT

UR

AL

JOIN

IN

TE

RM

S O

F

÷ D

IVIS

ION

or

QU

OT

IEN

T

F

UN

DA

ME

NT

AL

OP

S

UN

AR

Y

Win

ter

2002

Art

hur

Kel

ler

– C

S 18

05–

4

Ext

ra E

xam

ple

Rel

atio

ns

DE

POSI

T(b

ranc

h-na

me,

acc

t-no

,cus

t-na

me,

bala

nce)

CU

STO

ME

R(c

ust -

nam

e,st

reet

,cus

t-ci

ty)

BO

RR

OW

(bra

nch-

nam

e,lo

an-n

o,cu

st-n

ame,

amou

nt)

BR

AN

CH

(bra

nch-

nam

e,as

sets

, bra

nch-

city

)

CL

IEN

T(c

ust-

nam

e,em

pl-n

ame)

Bo

rro

w

B-N

L

-#

C-N

A

MT

T1

Mid

tow

n

123

F

red

6

00 T

2

M

idto

wn

23

4

Sal

ly 1

200

T3

Mid

tow

n

235

S

ally

150

0 T

4

D

ow

nto

wn

612

T

om

20

00

Win

ter

2002

Art

hur

Kel

ler

– C

S 18

05–

5

Sele

ctio

nR

1 =

σC(R

2)

whe

re C

is a

con

ditio

n in

volv

ing

the

attr

ibut

es o

f re

latio

n R

2.

Exa

mpl

eR

elat

ion Sells

:

JoeMenu

= σ

bar=Joe's(Sells

)

bar

beer

pric

eJo

e's

Bud

2.50

Joe'

sM

iller

2.75

Sue

'sB

ud2.

50S

ue's

Coo

rs3.

00

bar

beer

pric

eJo

e's

Bud

2.50

Joe'

sM

iller

2.75

Win

ter

2002

Art

hur

Kel

ler

– C

S 18

05–

6

S

ELE

CT

(σσσσ

)

ar

ity(σσσσ

(R))

= a

rity

(R)

0 ≤≤≤≤

car

d(σσσσ

(R))

≤≤≤≤ c

ard

(R)

σσσσ c

(R

)

σσσσ

c (

R) ⊆⊆⊆⊆

(R

)

c is

sel

ecti

on

co

nd

itio

n:

term

s o

f fo

rm:

attr

op

val

ue

att

r o

p a

ttr

op

is o

ne

of

< =

> ≤≤≤≤ ≠≠≠≠ ≥≥≥≥

e

xam

ple

of

term

: b

ran

ch-n

ame

= "M

idto

wn

"

ter

ms

are

con

nec

ted

by

∧∧∧∧ ∨∨∨∨

¬¬¬¬

σσσσ b

ran

ch-n

ame

= "M

idto

wn

" ∧∧∧∧

am

ou

nt

> 10

00 (

Bo

rro

w)

σσσσ c

ust

-nam

e =

em

p-n

ame

(clie

nt)

Win

ter

2002

Art

hur

Kel

ler

– C

S 18

05–

7

Proj

ecti

onR

1 =

π L

(R2)

whe

re L

is a

list

of

attr

ibut

es f

rom

the

sche

ma

of R

2.

Exa

mpl

eπ b

eer,price(Sells

)

•N

otic

e el

imin

atio

n of

dup

licat

e tu

ples

.

beer

pric

eB

ud2.

50M

iller

2.75

Coo

rs3.

00

Win

ter

2002

Art

hur

Kel

ler

– C

S 18

05–

8

Pro

ject

ion

(ππππ

)

0 ≤≤≤≤

car

d (ππππ

A (

R))

≤≤≤≤ c

ard

(R

)

ari

ty (ππππ

A (

R))

= m

≤≤≤≤ a

rity

(R)

= k

ππππ

i 1,..

.,im

(R)

1 ≤≤≤≤

i j ≤≤≤≤

k d

isti

nct

pro

du

ces

set

of

m-t

up

les ⟨⟨⟨⟨ a

1 ,.

..,a

m ⟩⟩⟩⟩

su

ch t

hat

∃∃∃∃ k

-tu

ple

⟨⟨⟨⟨ b

1,...

,bk

⟩⟩⟩⟩ in

R w

her

e a j

= b

i j f

or

j = 1

,...,m

ππππ b

ran

ch-n

ame,

cu

st-n

ame

(

Bo

rro

w)

Mid

tow

n

F

red

Mid

tow

n

S

ally

Do

wn

tow

n T

om

Win

ter

2002

Art

hur

Kel

ler

– C

S 18

05–

9

Prod

uct

R =

R1

× R

2

pair

s ea

ch tu

ple

t 1 o

f R

1 w

ith e

ach

tupl

e t 2

of

R2

and

puts

in R

a tu

ple

t 1t 2

.

Win

ter

2002

Art

hur

Kel

ler

– C

S 18

05–

10

Car

tesi

an P

rod

uct

(×××× )

a

rity

(R)

= k1

a

rity

(R ××××

S)

= k1

+ k

2

a

rity

(S)

= k2

c

ard

(R ××××

S)

= ca

rd(R

) ××××

card

(S)

R ××××

S is

th

e se

t al

l po

ssib

le (

k1 +

k2)

-tu

ple

s

wh

ose

fir

st k

1 at

trib

ute

s ar

e a

tup

le in

R

last

k2

attr

ibu

tes

are

a tu

ple

in S

R

S

R ××××

S

A B

C

D

D E

F

A

B

C D

D'

E F

Win

ter

2002

Art

hur

Kel

ler

– C

S 18

05–

11

The

ta-J

oin

R =

R1

C R

2is

equ

ival

ent t

o R

= σ

C(R

1 ×

R2)

.

Win

ter

2002

Art

hur

Kel

ler

– C

S 18

05–

12

Exa

mpl

eSells

=

Bars

=

BarInfo = Sells Sells.Bar=Bars.Name Bars

bar

beer

pric

eJo

e's

Bud

2.50

Joe'

sM

iller

2.75

Sue

'sB

ud2.

50S

ue's

Coo

rs3.

00

nam

ead

drJo

e's

Map

le S

t.S

ue's

Riv

er R

d.

bar

beer

pric

ena

me

addr

Joe'

sB

ud2.

50Jo

e's

Map

le S

t.Jo

e's

Mill

er2.

75Jo

e's

Map

le S

t.S

ue's

Bud

2.50

Sue

'sR

iver

Rd.

Sue

'sC

oors

3.00

Sue

'sR

iver

Rd.

Win

ter

2002

Art

hur

Kel

ler

– C

S 18

05–

13

The

ta-J

oin

Rar

ity(R

) =

r

arity

(S)

= s

arity

(R

S)

= r

+ s

0 ≤

card

(R

S

) ≤

card

(R)

× ca

rd(S

)

S

i θ j

σ$i

θ $

(r+

j)

(R

× S

)

R

S

1 . .

. r

1

. . .

s

θ

θ

θ ca

n be

< >

= ≠

≤ ≥

If eq

ual (

=),

then

it is

an E

QU

IJO

IN

RS

(R

× S

)c

c

R(A

B C

)

S

(C D

E)

resu

lt ha

s sc

hem

a T

(A B

C C

' D E

)

R.A

<S

.D

ij

R(A

BC

) S

(CD

E)

T(A

BC

C’D

E)

1

3 5

2 1

1

1

3 5

1 2

2

2 4

6

1

2 2

1 3

5 3

3 4

3

5 7

3 3

4

1

3 5

4 4

3

4 6

8

4

4 3

2 4

6 3

3 4

2

4 6

4 4

3

3 5

7 4

4 3

Win

ter

2002

Art

hur

Kel

ler

– C

S 18

05–

14

Nat

ural

Joi

nR

= R

1

R2

calls

for

the

thet

a-jo

in o

f R

1 an

d R

2 w

ith th

e co

nditi

on th

atal

l attr

ibut

es o

f th

e sa

me

nam

e be

equ

ated

. The

n, o

neco

lum

n fo

r ea

ch p

air

of e

quat

ed a

ttrib

utes

is p

roje

cted

out

.

Exa

mpl

eSu

ppos

e th

e at

trib

ute name

in r

elat

ion Bars

was

cha

nged

to bar

, to

mat

ch th

e ba

r na

me

in Sells

.BarInfo = Sells Bars

bar

beer

pric

ead

drJo

e's

Bud

2.50

Map

le S

t.Jo

e's

Mill

er2.

75M

aple

St.

Sue

'sB

ud2.

50R

iver

Rd.

Sue

'sC

oors

3.00

Riv

er R

d.

Win

ter

2002

Art

hur

Kel

ler

– C

S 18

05–

15

Ren

amin

gρ S

(A1,

…,A

n) (R

) p

rodu

ces

a re

latio

n id

entic

al to

R b

utna

med

S a

nd w

ith a

ttrib

utes

, in

orde

r, n

amed

A1,

…,A

n.

Exa

mpl

eBars

=

ρ R(bar,addr

) (Bars

) =

•T

he n

ame

of th

e se

cond

rel

atio

n is

R.

nam

ead

drJo

e's

Map

le S

t.S

ue's

Riv

er R

d.

bar

addr

Joe'

sM

aple

St.

Sue

'sR

iver

Rd.

Win

ter

2002

Art

hur

Kel

ler

– C

S 18

05–

16

Un

ion

(R

∪∪∪∪ S

) a

rity

(R)

= ar

ity(

S)

= a

rity

(R ∪∪∪∪

S)

m

ax(c

ard

(R),

card

(S))

≤≤≤≤ c

ard

(R ∪∪∪∪

S)

≤≤≤≤ c

ard

(R)

+ ca

rd(S

)

set

of

tup

les

in R

or

S o

r b

oth

R ⊆⊆⊆⊆

R ∪∪∪∪

S

S

⊆⊆⊆⊆ R

∪∪∪∪ S

Fin

d c

ust

om

ers

of

Per

ryri

dg

e B

ran

ch

ππππ Cu

st-N

ame

(σσσσ

Bra

nch

-Nam

e =

"Per

ryri

dg

e" (

BO

RR

OW

∪∪∪∪ D

EP

OS

IT)

)

Win

ter

2002

Art

hur

Kel

ler

– C

S 18

05–

17

Dif

fere

nce

(R −−−−

S)

ari

ty(R

) =

arit

y(S

) =

arit

y(R

– S

)

0

≤≤≤≤ c

ard

(R –

S)

≤≤≤≤ c

ard

(R)

∅∅∅∅

⊆⊆⊆⊆ R

– S

⊆⊆⊆⊆ R

is t

he

tup

les

in R

no

t in

S

Dep

osi

tors

of

Per

ryri

dg

e w

ho

are

n't

bo

rro

wer

s o

f P

erry

rid

ge

ππππC

ust

-Nam

e (σσσσ

Bra

nch

-Nam

e =

"Per

ryri

dg

e" (D

EP

OS

IT –

BO

RR

OW

) )

Dep

osi

t <

Per

ryri

dg

e, 3

6, P

at, 5

00 >

Bo

rro

w

< P

erry

rid

ge,

72,

Pat

, 100

00 >

π C

ust-

Nam

e (σ

Bra

nch-

Nam

e =

"P

erry

ridge

" (

DE

PO

SIT

) )

—π C

ust-

Nam

e (σ

Bra

nch-

Nam

e =

"P

erry

ridge

" (

BO

RR

OW

) )

Doe

s σ

(D) − π

(B)

) w

ork?

Win

ter

2002

Art

hur

Kel

ler

– C

S 18

05–

18

Com

bini

ng O

pera

tions

Alg

ebra

=1.

Bas

is a

rgum

ents

+2.

Way

s of

con

stru

ctin

g ex

pres

sion

s.Fo

r re

latio

nal a

lgeb

ra:

1.A

rgum

ents

= v

aria

bles

sta

ndin

g fo

rre

latio

ns +

fin

ite, c

onst

ant r

elat

ions

.2.

Exp

ress

ions

con

stru

cted

by

appl

ying

one

of th

e op

erat

ors

+ p

aren

thes

es.

•Q

uery

= e

xpre

ssio

n of

rel

atio

nal a

lgeb

ra.

Win

ter

2002

Art

hur

Kel

ler

– C

S 18

05–

19

ππππ Cu

st-N

ame,

Cu

st-C

ity

(σσσσC

LIE

NT

.Ban

ker-

Nam

e =

"Jo

hn

son

"

(C

LIE

NT

×××× C

US

TO

ME

R)

)

=

ππππ C

ust

-Nam

e,C

ust

-Cit

y (

CU

ST

OM

ER

)

•Is

th

is a

lway

s tr

ue?

ππππC

LIE

NT

.Cu

st-N

ame,

CU

ST

OM

ER

.Cu

st-C

ity

(σσσσ

CL

IEN

T.B

anke

r-N

ame

= "J

oh

nso

n"

∧∧∧∧ C

LIE

NT

.Cu

st-N

ame

= C

US

TO

ME

R.C

ust

-Nam

e

(C

LIE

NT

×××× C

US

TO

ME

R)

)

ππππC

LIE

NT

.Cu

st-N

ame,

CU

ST

OM

ER

.... Cu

st-C

ity

(σσσσ

CL

IEN

T.C

ust

-Nam

e =C

US

TO

ME

R.C

ust

-Nam

e

(CU

ST

OM

ER

×××× ππππ

Cu

st-N

ame

(((( σσσσ C

LIE

NT

.Ban

ker-

Nam

e="J

oh

nso

n"

(C

LIE

NT

) )

) )

Win

ter

2002

Art

hur

Kel

ler

– C

S 18

05–

20

SE

T IN

TE

RS

EC

TIO

N

ari

ty(R

) =

arit

y(S

) =

arit

y (R

∩∩∩∩ S

)

(R

∩∩∩∩ S

)

0 ≤≤≤≤

car

d (

R ∩∩∩∩

S) ≤≤≤≤

min

(ca

rd(R

), c

ard

(S))

tu

ple

s b

oth

in R

an

d in

S

R −−−−

(R −−−−

S)

= R

∩∩∩∩ S

SR

∅ ⊆

R ∩

S ⊆

R

∅ ⊆

R ∩

S ⊆

S

Win

ter

2002

Art

hur

Kel

ler

– C

S 18

05–

21

Ope

rato

r Pr

eced

ence

The

nor

mal

way

to g

roup

ope

rato

rs is

:

1.U

nary

ope

rato

rs σ

, π, a

nd ρ

hav

e hi

ghes

t pre

cede

nce.

2.N

ext h

ighe

st a

re th

e “m

ultip

licat

ive”

ope

rato

rs,

,

C ,

and

×.

3.L

owes

t are

the

“add

itive

” op

erat

ors,

∪, ∩

, and

—.

•B

ut th

ere

is n

o un

iver

sal a

gree

men

t, so

we

alw

ays

put

pare

nthe

ses

arou

nd th

e ar

gum

ent o

f a

unar

y op

erat

or, a

nd it

is a

good

idea

to g

roup

all

bina

ry o

pera

tors

with

par

enth

eses

encl

osin

g th

eir

argu

men

ts.

Exa

mpl

eG

roup

R ∪

σS

T

as

R ∪

(S )

T ).

Win

ter

2002

Art

hur

Kel

ler

– C

S 18

05–

22

Eac

h E

xpre

ssio

n N

eeds

a S

chem

a

•If

∪, ∩

, — a

pplie

d, s

chem

as a

re th

e sa

me,

so

use

this

sche

ma.

•Pr

ojec

tion:

use

the

attr

ibut

es li

sted

in th

e pr

ojec

tion.

•Se

lect

ion:

no

chan

ge in

sch

ema.

•Pr

oduc

t R ×

S: u

se a

ttrib

utes

of

R a

nd S

.◆

But

if th

ey s

hare

an

attr

ibut

e A

, pre

fix

it w

ith th

e re

latio

nna

me,

as

R.A

, S.A

.

•T

heta

-joi

n: s

ame

as p

rodu

ct.

•N

atur

al jo

in: u

se a

ttrib

utes

fro

m e

ach

rela

tion;

com

mon

attr

ibut

es a

re m

erge

d an

yway

.

•R

enam

ing:

wha

teve

r it

says

.

Win

ter

2002

Art

hur

Kel

ler

– C

S 18

05–

23

Exa

mpl

e•

Find

the

bars

that

are

eith

er o

n M

aple

Str

eet

or s

ell B

ud f

or le

ss th

an $

3.Sells(bar, beer, price)

Bars(name, addr)

Win

ter

2002

Art

hur

Kel

ler

– C

S 18

05–

24

Exa

mpl

eFi

nd th

e ba

rs th

at s

ell t

wo

diff

eren

t bee

rs a

t the

sam

e pr

ice.

Sells(bar, beer, price)

Win

ter

2002

Art

hur

Kel

ler

– C

S 18

05–

25

Lin

ear

Not

atio

n fo

r E

xpre

ssio

ns•

Inve

nt n

ew n

ames

for

inte

rmed

iate

rel

atio

ns, a

nd a

ssig

nth

em v

alue

s th

at a

re a

lgeb

raic

exp

ress

ions

.

•R

enam

ing

of a

ttrib

utes

impl

icit

in s

chem

a of

new

rel

atio

n.

Exa

mpl

eFi

nd th

e ba

rs th

at a

re e

ither

on

Map

le S

tree

t or

sell

Bud

for

less

than

$3.

Sells(bar, beer, price)

Bars(name, addr)

R1(name) := π n

ame(σ

addr = Maple St.(Bars))

R2(name) := π b

ar(σ

beer=Bud AND price<$3(Sells))

R3(name) := R1 ∪ R2

Win

ter

2002

Art

hur

Kel

ler

– C

S 18

05–

26

Why

Dec

ompo

sitio

n “W

orks

”?

Wha

t doe

s it

mea

n to

“w

ork”

? W

hy c

an’t

we

just

tear

set

sof

attr

ibut

es a

part

as

we

like?

•A

nsw

er: t

he d

ecom

pose

d re

latio

ns n

eed

to r

epre

sent

the

sam

e in

form

atio

n as

the

orig

inal

.◆

We

mus

t be

able

to r

econ

stru

ct th

e or

igin

al f

rom

the

deco

mpo

sed

rela

tions

.

Proj

ectio

n an

d Jo

in C

onne

ct th

eO

rigi

nal a

nd D

ecom

pose

d R

elat

ions

•Su

ppos

e R

is d

ecom

pose

d in

to S

and

T.

We

proj

ect R

ont

o S

and

onto

T.

Win

ter

2002

Art

hur

Kel

ler

– C

S 18

05–

27

Exa

mpl

e

R =

•R

ecal

l we

deco

mpo

sed

this

rel

atio

n as

:

nam

ead

drbe

ersL

iked

man

ffa

vori

teB

eer

Jane

way

Voy

ager

Bud

A.B

.W

icke

dAle

Jane

way

Voy

ager

Wic

kedA

leP

ete'

sW

icke

dAle

Spoc

kE

nter

pris

eB

udA

.B.

Bud

Win

ter

2002

Art

hur

Kel

ler

– C

S 18

05–

28

Proj

ect o

nto Drinkers1(name,

addr,

favoriteBeer

):

Proj

ect o

nto Drinkers3(beersLiked, manf):

Proj

ect o

nto Drinkers4(name, beersLiked):

beer

sLik

edm

anf

Bud

A.B

.W

icke

dAle

Pet

e's

Bud

A.B

.

nam

ead

drbe

ersL

iked

Jane

way

Voy

ager

Bud

Jane

way

Voy

ager

Wic

kedA

leSp

ock

Ent

erpr

ise

Bud

nam

ead

drfa

vori

teB

eer

Jane

way

Voy

ager

Wic

kedA

leSp

ock

Ent

erpr

ise

Bud

Win

ter

2002

Art

hur

Kel

ler

– C

S 18

05–

29

Rec

onst

ruct

ion

of O

rigi

nal

Can

we

figu

re o

ut th

e or

igin

al r

elat

ion

from

the

deco

mpo

sed

rela

tions

?

•So

met

imes

, if

we

natu

ral j

oin

the

rela

tions

.

Exa

mpl

eDrinkers3 Drinkers4

=

•Jo

in o

f ab

ove

with

Drinkers1

= o

rigi

nal R

.

nam

ebe

ersL

iked

man

fJa

new

ayB

udA

.B.

Jane

way

Wic

kedA

leP

ete'

sSp

ock

Bud

A.B

.

Win

ter

2002

Art

hur

Kel

ler

– C

S 18

05–

30

The

orem

Supp

ose

we

deco

mpo

se a

rel

atio

n w

ith s

chem

a X

YZ

into

XY

and

XZ

and

pro

ject

the

rela

tion

for

XYZ

ont

o X

Y a

nd X

Z.

The

n X

Y

X

Z is

gua

rant

eed

to r

econ

stru

ct X

YZ

if a

nd o

nly

if X

→→

Y (

or e

quiv

alen

tly, X

→→

Z).

•U

sual

ly, t

he M

VD

is r

eally

a F

D, X

→ Y

or

X →

Z.

•B

CN

F: W

hen

we

deco

mpo

se X

YZ in

to X

Y a

nd X

Z, i

t is

beca

use

ther

e is

a F

D X

→ Y

or

X →

Z th

at v

iola

tes

BC

NF.

◆T

hus,

we

can

alw

ays

reco

nstr

uct X

YZ

fro

m it

s pr

ojec

tions

ont

o X

Yan

d X

Z.

•4N

F: w

hen

we

deco

mpo

se X

YZ in

to X

Y a

nd X

Z, i

t is

beca

use

ther

e is

an

MV

D X

→→

Y o

r X

→→

Z th

at v

iola

tes

4NF.

◆A

gain

, we

can

reco

nstr

uct X

YZ f

rom

its

proj

ectio

ns o

nto

XY

and

XZ

.

Win

ter

2002

Art

hur

Kel

ler

– C

S 18

05–

31

Bag

Sem

antic

s

A r

elat

ion

(in

SQL

, at l

east

) is

rea

lly a

bag

or

mul

tise

t.•

It m

ay c

onta

in th

e sa

me

tupl

e m

ore

than

once

, alth

ough

ther

e is

no

spec

ifie

d or

der

(unl

ike

a lis

t).

•E

xam

ple:

{1,

2,1,

3} is

a b

ag a

nd n

ot a

set

.•

Sele

ct, p

roje

ct, a

nd jo

in w

ork

for

bags

as

wel

l as

sets

.◆

Just

wor

k on

a tu

ple-

by-t

uple

bas

is, a

nd d

on't

elim

inat

e du

plic

ates

.

Win

ter

2002

Art

hur

Kel

ler

– C

S 18

05–

32

Bag

Uni

on

Sum

the

times

an

elem

ent a

ppea

rs in

the

two

bags

.•

Exa

mpl

e: {

1,2,

1} ∪

{1,

2,3,

3} =

{1,

1,1,

2,2,

3,3}

.

Bag

Int

erse

ctio

nT

ake

the

min

imum

of

the

num

ber

of o

ccur

renc

es in

eac

hba

g.•

Exa

mpl

e: {

1,2,

1} ∩

{1,

2,3,

3} =

{1,

2}.

Bag

Dif

fere

nce

Prop

er-s

ubtr

act t

he n

umbe

r of

occ

urre

nces

in th

e tw

o ba

gs.

•E

xam

ple:

{1,

2,1}

– {

1,2,

3,3}

= {

1}.

Win

ter

2002

Art

hur

Kel

ler

– C

S 18

05–

33

Law

s fo

r B

ags

Dif

fer

From

Law

s fo

r Se

ts

•So

me

fam

iliar

law

s co

ntin

ue to

hol

d fo

r ba

gs.

◆E

xam

ples

: uni

on a

nd in

ters

ectio

n ar

e st

ill c

omm

utat

ive

and

asso

ciat

ive.

•B

ut o

ther

law

s th

at h

old

for

sets

do

not h

old

for

bags

.

Exa

mpl

eR

∩ (

S ∪

T) ≡

(R ∩

S) ∪

(R

∩ T

) ho

lds

for

sets

.•

Let

R, S

, and

T e

ach

be th

e ba

g {1

}.•

Lef

t sid

e: S

∪ T

= {

1,1}

; R ∩

(S ∪

T)

= {

1}.

•R

ight

sid

e: R

∩ S

= R

∩ T

= {

1};

(R ∩

S) ∪

(R

∩ T

) =

{1}

∪ {

1} =

{1,

1} ≠

{1}

.

Win

ter

2002

Art

hur

Kel

ler

– C

S 18

05–

34

Ext

ende

d (“

Non

clas

sica

l”)

Rel

atio

nal A

lgeb

raA

dds

feat

ures

nee

ded

for

SQL

, bag

s.

1.D

uplic

ate-

elim

inat

ion

oper

ator

δ.

2.E

xten

ded

proj

ectio

n.

3.So

rtin

g op

erat

or τ

.

4.G

roup

ing-

and-

aggr

egat

ion

oper

ator

γ.

5.O

uter

join

ope

rato

r o

.

Win

ter

2002

Art

hur

Kel

ler

– C

S 18

05–

35

Dup

licat

e E

limin

atio

nδ(

R)

= r

elat

ion

with

one

cop

y of

eac

h tu

ple

that

app

ears

one

or m

ore

times

in R

.

Exa

mpl

eR

=A

B1

23

41

2

δ(R

) =

AB

12

34

Win

ter

2002

Art

hur

Kel

ler

– C

S 18

05–

36

Sort

ing

•τ L

(R)

= li

st o

f tu

ples

of

R, o

rder

ed a

ccor

ding

toat

trib

utes

on

list L

.•

Not

e th

at r

esul

t typ

e is

out

side

the

norm

al ty

pes

(set

or

bag)

for

rel

atio

nal a

lgeb

ra.

◆C

onse

quen

ce: τ

can

not b

e fo

llow

ed b

y ot

her

rela

tiona

lop

erat

ors.

Exa

mpl

eR

=A

B1

33

45

2τ B

(R)

= [

(5,2

), (

1,3)

, (3,

4)].

Win

ter

2002

Art

hur

Kel

ler

– C

S 18

05–

37

Ext

ende

d Pr

ojec

tion

Allo

w th

e co

lum

ns in

the

proj

ectio

n to

be

func

tions

of o

ne o

r m

ore

colu

mns

in th

e ar

gum

ent r

elat

ion.

Exa

mpl

eR

=A

B1

23

4π A

+B,A

,A(R

) =

A+B

A1

A2

31

17

33

Win

ter

2002

Art

hur

Kel

ler

– C

S 18

05–

38

Agg

rega

tion

Ope

rato

rs

•T

hese

are

not

rel

atio

nal o

pera

tors

; rat

her

they

sum

mar

ize

a co

lum

n in

som

e w

ay.

•Fi

ve s

tand

ard

oper

ator

s: S

um, A

vera

ge,

Cou

nt, M

in, a

nd M

ax.

Win

ter

2002

Art

hur

Kel

ler

– C

S 18

05–

39

Gro

upin

g O

pera

tor

γ L(R

), w

here

L is

a li

st o

f el

emen

ts th

at a

re e

ither

a)In

divi

dual

(gr

oupi

ng)

attr

ibut

es o

rb)

Of

the

form

θ(A

), w

here

θ is

an

aggr

egat

ion

oper

ator

and

A th

e at

trib

ute

to w

hich

it is

app

lied,

is c

ompu

ted

by:

1.G

roup

R a

ccor

ding

to a

ll th

e gr

oupi

ng a

ttrib

utes

on

list L

.2.

With

in e

ach

grou

p, c

ompu

te θ

(A),

for

eac

h el

emen

t θ(A

)on

list

L.

3.R

esul

t is

the

rela

tion

who

se c

olum

ns c

onsi

st o

f on

e tu

ple

for

each

gro

up. T

he c

ompo

nent

s of

that

tupl

e ar

e th

eva

lues

ass

ocia

ted

with

eac

h el

emen

t of

L f

or th

at g

roup

.

Win

ter

2002

Art

hur

Kel

ler

– C

S 18

05–

40

Exa

mpl

eL

et R

=ba

rbe

erpr

ice

Joe'

sB

ud2.

00Jo

e's

Mill

er2.

75Su

e's

Bud

2.50

Sue'

sC

oors

3.00

Mel

'sM

iller

3.25

Com

pute

γbe

er,A

VG

(pri

ce)(R

).

1.G

roup

by

the

grou

ping

attr

ibut

e(s)

, beer

in th

is c

ase:

bar

beer

pric

eJo

e's

Bud

2.00

Sue'

sB

ud2.

50Jo

e's

Mill

er2.

75M

el's

Mill

er3.

25Su

e's

Coo

rs3.

00

Win

ter

2002

Art

hur

Kel

ler

– C

S 18

05–

41

2.C

ompu

te a

vera

ge o

f price

with

in g

roup

s:

beer

AV

G(p

rice

)

Bud

2.25

Mill

er3.

00

Coo

rs3.

00

Win

ter

2002

Art

hur

Kel

ler

– C

S 18

05–

42

Out

erjo

in

The

nor

mal

join

can

“lo

se”

info

rmat

ion,

beca

use

a tu

ple

that

doe

sn’t

join

with

any

from

the

othe

r re

latio

n (d

angl

es)

has

nove

stag

e in

the

join

res

ult.

•T

he n

ull v

alue

⊥ c

an b

e us

ed to

“pa

d”da

nglin

g tu

ples

so

they

app

ear

in th

e jo

in.

•G

ives

us

the

oute

rjoi

n op

erat

or

o .

•V

aria

tions

: the

ta-o

uter

join

, lef

t- a

nd r

ight

-ou

terj

oin

(pad

onl

y da

nglin

g tu

ples

fro

m th

ele

ft (

resp

ectiv

ely,

rig

ht).

Win

ter

2002

Art

hur

Kel

ler

– C

S 18

05–

43

Exa

mpl

e

R =

AB

12

34

S =

BC

45

67

R

o S

=A

BC

34

5pa

rt o

f nat

ural

join

12

⊥pa

rt o

f rig

ht-o

uter

join

⊥6

7pa

rt o

f lef

t-ou

terj

oin