11
Database Systems {week 02} Rensselaer Polytechnic Institute CSCI-4380 – Database Systems David Goldschmidt, Ph.D.

Rensselaer Polytechnic Institute CSCI-4380 – Database Systems David Goldschmidt, Ph.D

Embed Size (px)

Citation preview

Page 1: Rensselaer Polytechnic Institute CSCI-4380 – Database Systems David Goldschmidt, Ph.D

Database Systems{week 02}

Rensselaer Polytechnic InstituteCSCI-4380 – Database SystemsDavid Goldschmidt, Ph.D.

Page 2: Rensselaer Polytechnic Institute CSCI-4380 – Database Systems David Goldschmidt, Ph.D

Selection (review)

Find and select all tuples from relation Rthat satisfy some set of conditions Forms the basis of querying a database The selection C (R) is based on Boolean

condition C over attributes of relation R Example conditions include:▪ A = e, A > e, A >= e, A < e, A <= e, A <> e▪ A1 = A2, A1 <> A2

▪ Any combination of conditions using AND, OR, NOT

A, A1, and A

2 are attributese is a constant or expression

Page 3: Rensselaer Polytechnic Institute CSCI-4380 – Database Systems David Goldschmidt, Ph.D

Selection (review)

Selection selects a subset of tuples in relation R (with the schema unchanged)

C (R) = { t | t is a tuple in R and t satisfies the condition C on relation R }

Selection conditions can only refer toattributes in the given relation R

For conditions spanning multiple relations, we first must combine those relations (i.e. join)

Page 4: Rensselaer Polytechnic Institute CSCI-4380 – Database Systems David Goldschmidt, Ph.D

Cartesian product

The Cartesian product combinestwo relations to form a new relation The new relation has all of the attributes

of the original two relations It’s often a good idea to rename

attributesin the original relations such thatthere’s no ambiguity

Page 5: Rensselaer Polytechnic Institute CSCI-4380 – Database Systems David Goldschmidt, Ph.D

Cartesian product

Given two relations R and S withschemas R(A1,A2,...,An) and S(B1,B2,...,Bm) The Cartesian product RxS produces

relation T with schema T(A1,A2,...,An,B1,B2,...,Bm)

We can disambiguate attributesby using R.Ax and S.By ▪ i.e. attributes Ax and By have the same name

Page 6: Rensselaer Polytechnic Institute CSCI-4380 – Database Systems David Goldschmidt, Ph.D

Cartesian product

The Cartesian product of relations R and S:

RxS = { (r,s) | r is a tuple in R and s is a tuple in S }▪ Note that (r,s) has all values in r and s

The Cartesian product is like multiplicationin that it produces a tuple for every pair oftuples from R and S▪ e.g. if R has 10 tuples and S has 5 tuples,

then RxS will have 50 tuples

So why do we want to do this?

Page 7: Rensselaer Polytechnic Institute CSCI-4380 – Database Systems David Goldschmidt, Ph.D

Join

Cartesian product RxS is often followed by a selection condition that specifies how tuples in R should be matched to tuples in S

name=student (Undergraduate x Advising)

Translation: Join the Undergraduate and Advising relations by selecting tuples in which the name attribute equals the student attribute

Page 8: Rensselaer Polytechnic Institute CSCI-4380 – Database Systems David Goldschmidt, Ph.D

Natural joins

When joining two relations, we often want to join on common (same-named) attributes This is a natural join on relations R and S

and is denoted R⋈S The selection condition selects tuples that

have the same values for same-named attributes

Note that the schema does not repeat thesame-named attributes

Page 9: Rensselaer Polytechnic Institute CSCI-4380 – Database Systems David Goldschmidt, Ph.D

Theta joins

Joins based on specific conditions arecalled theta joins and are denoted R ⋈C S To perform a theta join, first take the

product of relations R and S Next, select tuples that satisfy condition C The resulting schema is the union of the

schemas of R and S with R.Ax and S.By prefixes,if necessaryIn a theta join, no deduplication

of attributes is performed!

Page 10: Rensselaer Polytechnic Institute CSCI-4380 – Database Systems David Goldschmidt, Ph.D

Rename

The rename operator changes thename of the attributes of relation Rwith schema R(A1,A2,...,An)

S(B1,B2,...,Bn) (R) = relation S(B1,B2,...,Bn)

in which R.A1 is renamed S.B1, R.A2 is renamed S.B2, and so on

To only change some attribute names,simply specify Bi = Ai

Page 11: Rensselaer Polytechnic Institute CSCI-4380 – Database Systems David Goldschmidt, Ph.D

Exercises

Find all faculty who advise a student

Find all faculty who do not adviseany students

Find faculty who advise at least two students

Find faculty who advise a student that is not in their department (e.g. dual majors)