Top Banner
RELATIONAL ALGEBRA Prof. Sin-Min LEE Department of Computer Science
57
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: RELATIONAL ALGEBRA Prof. Sin-Min LEE Department of Computer Science.

RELATIONAL ALGEBRA

Prof. Sin-Min LEE

Department of Computer Science

Page 2: RELATIONAL ALGEBRA Prof. Sin-Min LEE Department of Computer Science.

Terminology:table (relation)row (tuple)

formally, a relation is a set of ordered n-tuples.

Domain : set of data values from which an attribute value can be drawn.

Eg. set of department names set of legal salaries set of possible employee birth dates

Page 3: RELATIONAL ALGEBRA Prof. Sin-Min LEE Department of Computer Science.

If D1, D2, D3……. Dn are the domains of a relation R.

Then, R D1 x D2x D3 x ………..x Dn.

Page 4: RELATIONAL ALGEBRA Prof. Sin-Min LEE Department of Computer Science.

What are the query languages?

It is an abstract language. We use it to express the set of operations that any relational query language must perform.

Two types of operations: 1.set-theoretic operations: tables are

essentially sets of rows 2.native relational operations: focus on the

structure of the rows Query languages are specialized languages for asking questions,or queries,that involve the data in database.

Page 5: RELATIONAL ALGEBRA Prof. Sin-Min LEE Department of Computer Science.

Database Scheme

A relational database scheme, or schema, corresponds to a set of table definitions.

Eg: product(p_id, name, category, description) supply(p_id, s_id, qnty_per_month) supplier(s_id, name, address, ph#)

* remember the difference between a DB instance and a DB scheme.

Page 6: RELATIONAL ALGEBRA Prof. Sin-Min LEE Department of Computer Science.

Keys

The super key, candidate key and primary key notations presented for ER model apply for relational model also.

Page 7: RELATIONAL ALGEBRA Prof. Sin-Min LEE Department of Computer Science.

Query languages

procedural vs. non-proceduralcommercial languages have some

of bothwe will study:

relational algebra (which is procedural, i.e. tells you how to process a query)

relational calculus (which is non-procedural i.e. tells what you want)

Page 8: RELATIONAL ALGEBRA Prof. Sin-Min LEE Department of Computer Science.
Page 9: RELATIONAL ALGEBRA Prof. Sin-Min LEE Department of Computer Science.

Relational AlgebraFundamental operators select project cartesian product union set difference -

Other operators natural join JOIN (butterfly symbol) set intersection division

Page 10: RELATIONAL ALGEBRA Prof. Sin-Min LEE Department of Computer Science.

A Simple DB

account ac# owner ss# balance 1 bob 123 1000 2 sue 456 2000 3 jane 789 3000

transaction t# ac# type amount outcome date 1 1 W 1500 bounced 5/1/98 2 2 D 1000 ok 5/2/98 3 1 W 100 ok 5/4/98 4 3 D 500 ok 5/7/98 5 2 W 200 ok 5/9/98

account had transaction

Page 11: RELATIONAL ALGEBRA Prof. Sin-Min LEE Department of Computer Science.

Selecteg: balance>=1500 account

result : ac# owner ss# balance 2 sue 456 2000 3 jane 789 3000

Projecteg: π owner, ss# account

result: owner ss# bob 123 sue 456 jane 789

Page 12: RELATIONAL ALGEBRA Prof. Sin-Min LEE Department of Computer Science.
Page 13: RELATIONAL ALGEBRA Prof. Sin-Min LEE Department of Computer Science.

Cartesian product

eg: account transaction this will have 15 rows like the ones shown below:ac# owner ss# balance t# type amount outcome

date1 bob 123 1000 1 W 1500 bounced 5/1/982 sue 456 2000 2 D 1000 ok 5/2/98……………

Composing operationseg: “show all transactions done by account owner

Bob”.

σ account.ano= transaction.ano(( owner=“Bob” account) transaction)

Page 14: RELATIONAL ALGEBRA Prof. Sin-Min LEE Department of Computer Science.

Natural Join- combines σ, π, - very commonly usedNatural Join forms the cross product of its

two arguments, does a selection to enforce equality of columns with the same name and removes duplicate columns.

Eg: “show all transactions done by account owner Bob”

σ owner=“Bob” (account JOIN transaction)

Page 15: RELATIONAL ALGEBRA Prof. Sin-Min LEE Department of Computer Science.

Rename operation

What if you need to access the same relation twice in a query?

eg. person(ss#, name, mother_ss#, father_ss#)

“Find the name of Bob’s mother” needs the “person” table to be accessed twice.

The operation ρ x (r) evaluates to a second logical copy of relation r renamed to x.

Page 16: RELATIONAL ALGEBRA Prof. Sin-Min LEE Department of Computer Science.

Rename operation (contd)

eg:

π mother.name (

(ρ mother (person))

JOIN mother.ss# = person.mother_ss#

( name=“Bob” (person)))

Page 17: RELATIONAL ALGEBRA Prof. Sin-Min LEE Department of Computer Science.
Page 18: RELATIONAL ALGEBRA Prof. Sin-Min LEE Department of Computer Science.
Page 19: RELATIONAL ALGEBRA Prof. Sin-Min LEE Department of Computer Science.
Page 20: RELATIONAL ALGEBRA Prof. Sin-Min LEE Department of Computer Science.
Page 21: RELATIONAL ALGEBRA Prof. Sin-Min LEE Department of Computer Science.

Additional OperationsAdditional Operations are those that can be

expressed in terms of other operations. Set Intersection r s = r-(r-s)eg.: r a b s a b r s = a

b 1 a 1 a 1

a 2 b 2 c 3

d 3 d 3 d

r s

Page 22: RELATIONAL ALGEBRA Prof. Sin-Min LEE Department of Computer Science.

Additional Operations(cntd.) Division

useful for “for all” queriesDefinition: Let r(R) and s(S), where R & S are sets

of attributes, be relations, where S is a subset of R. The relation r s has scheme R-S. The tuples in r s consist of the R-S part of the tuples of r such that some tuple tr in r with the those R-S attribute values matches every tuple in s.

Can also be defined in terms of relational algebra.

Page 23: RELATIONAL ALGEBRA Prof. Sin-Min LEE Department of Computer Science.
Page 24: RELATIONAL ALGEBRA Prof. Sin-Min LEE Department of Computer Science.
Page 25: RELATIONAL ALGEBRA Prof. Sin-Min LEE Department of Computer Science.
Page 26: RELATIONAL ALGEBRA Prof. Sin-Min LEE Department of Computer Science.
Page 27: RELATIONAL ALGEBRA Prof. Sin-Min LEE Department of Computer Science.
Page 28: RELATIONAL ALGEBRA Prof. Sin-Min LEE Department of Computer Science.
Page 29: RELATIONAL ALGEBRA Prof. Sin-Min LEE Department of Computer Science.
Page 30: RELATIONAL ALGEBRA Prof. Sin-Min LEE Department of Computer Science.
Page 31: RELATIONAL ALGEBRA Prof. Sin-Min LEE Department of Computer Science.
Page 32: RELATIONAL ALGEBRA Prof. Sin-Min LEE Department of Computer Science.
Page 33: RELATIONAL ALGEBRA Prof. Sin-Min LEE Department of Computer Science.
Page 34: RELATIONAL ALGEBRA Prof. Sin-Min LEE Department of Computer Science.
Page 35: RELATIONAL ALGEBRA Prof. Sin-Min LEE Department of Computer Science.
Page 36: RELATIONAL ALGEBRA Prof. Sin-Min LEE Department of Computer Science.
Page 37: RELATIONAL ALGEBRA Prof. Sin-Min LEE Department of Computer Science.
Page 38: RELATIONAL ALGEBRA Prof. Sin-Min LEE Department of Computer Science.
Page 39: RELATIONAL ALGEBRA Prof. Sin-Min LEE Department of Computer Science.
Page 40: RELATIONAL ALGEBRA Prof. Sin-Min LEE Department of Computer Science.
Page 41: RELATIONAL ALGEBRA Prof. Sin-Min LEE Department of Computer Science.
Page 42: RELATIONAL ALGEBRA Prof. Sin-Min LEE Department of Computer Science.
Page 43: RELATIONAL ALGEBRA Prof. Sin-Min LEE Department of Computer Science.
Page 44: RELATIONAL ALGEBRA Prof. Sin-Min LEE Department of Computer Science.
Page 45: RELATIONAL ALGEBRA Prof. Sin-Min LEE Department of Computer Science.
Page 46: RELATIONAL ALGEBRA Prof. Sin-Min LEE Department of Computer Science.
Page 47: RELATIONAL ALGEBRA Prof. Sin-Min LEE Department of Computer Science.
Page 48: RELATIONAL ALGEBRA Prof. Sin-Min LEE Department of Computer Science.
Page 49: RELATIONAL ALGEBRA Prof. Sin-Min LEE Department of Computer Science.
Page 50: RELATIONAL ALGEBRA Prof. Sin-Min LEE Department of Computer Science.
Page 51: RELATIONAL ALGEBRA Prof. Sin-Min LEE Department of Computer Science.
Page 52: RELATIONAL ALGEBRA Prof. Sin-Min LEE Department of Computer Science.
Page 53: RELATIONAL ALGEBRA Prof. Sin-Min LEE Department of Computer Science.
Page 54: RELATIONAL ALGEBRA Prof. Sin-Min LEE Department of Computer Science.
Page 55: RELATIONAL ALGEBRA Prof. Sin-Min LEE Department of Computer Science.
Page 56: RELATIONAL ALGEBRA Prof. Sin-Min LEE Department of Computer Science.
Page 57: RELATIONAL ALGEBRA Prof. Sin-Min LEE Department of Computer Science.

Additional Operations(cntd.) Assignment operationSometimes it is convenient to write a

relational algebra expression as a sequence of steps rather than one large expression. To do this, you can use assignment:

relname expressioneg.: temp π pno (part)

bigsuppliers supply temp