Download presentation
Presentation is loading. Please wait.
Published byCalvin Walters Modified over 8 years ago
1
Rensselaer Polytechnic Institute CSCI-4380 – Database Systems David Goldschmidt, Ph.D.
2
Find and select all tuples from relation R that 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 1 = A 2, A 1 <> A 2 ▪ Any combination of conditions using AND, OR, NOT A, A 1, and A 2 are attributes e is a constant or expression
3
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 to attributes in the given relation R For conditions spanning multiple relations, we first must combine those relations (i.e. join)
4
The Cartesian product combines two 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 attributes in the original relations such that there’s no ambiguity
5
Given two relations R and S with schemas R(A 1,A 2,...,A n ) and S(B 1,B 2,...,B m ) The Cartesian product RxS produces relation T with schema T(A 1,A 2,...,A n,B 1,B 2,...,B m ) We can disambiguate attributes by using R.A x and S.B y ▪ i.e. attributes A x and B y have the same name
6
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 multiplication in that it produces a tuple for every pair of tuples 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?
7
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
8
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 the same-named attributes
9
Joins based on specific conditions are called 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.A x and S.B y prefixes, if necessary In a theta join, no deduplication of attributes is performed!
10
The rename operator changes the name of the attributes of relation R with schema R(A 1,A 2,...,A n ) S(B 1,B 2,...,B n ) ( R ) = relation S(B 1,B 2,...,B n ) in which R.A 1 is renamed S.B 1, R.A 2 is renamed S.B 2, and so on To only change some attribute names, simply specify B i = A i
11
Find all faculty who advise a student Find all faculty who do not advise any 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)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.