

















































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


















































Johannes [email protected]://www.cs.cornell.edu/johannesSlides fromDatabase Management Systems, 3
rd^ Edition, Ramakrishnan and Gehrke.
Relational Query Languages v Query languages:
Allow manipulation and retrieval of data from a database. v Relational model supports simple, powerful QLs:–^ Strong formal foundation based on logic.–^ Allows for much optimization. v Query Languages
!=^ programming languages!
-^ QLs not expected to be “Turing complete”.–^ QLs not intended to be used for complex calculations.–^ QLs support easy, efficient access to large data sets.
Preliminaries v A query is applied to
relation instances
, and the
result of a query is also a relation instance.–^ Schemas^ of input relations for a query are fixed (butquery will run regardless of instance!)–^ The schema for the
result^ of a given query is also fixed! Determined by definition of query languageconstructs. v Positional vs. named-field notation:– Positional notation easier for formal definitions,named-field notation more readable.– Both used in SQL
Database Management Systems, R. Ramakrishnan and J. Gehrke Example Instances
sid^ sname
rating^ age 22 dustin
31 lubber
58 rusty^
sid^ sname
rating^ age 28 yuppy
31 lubber
44 guppy
58 rusty^ sid^ bid^ day 22 101 10/10/96 58 103 11/12/96^10 35. R1 S1 S
v^ “Sailors” and “Reserves”relations for our examples. v^ We’ll use positional ornamed field notation,assume that names of fieldsin query results are`inherited’ from names offields in query inputrelations.
)^ Selects a subset of rows from relation.
-^ Projection^
(^ )^ Deletes unwanted columns from relation.
-^ Cross-product
(^ ) Allows us to combine two relations.
-^ Set-difference
(^ ) Tuples in reln. 1, but not in reln. 2.
-^ Union^ (^
) Tuples in reln. 1 and in reln. 2. v^ Additional operations:–^ Intersection,
join , division, renaming: Not essential, but (very!) useful. v Since each operation returns a relation, operationscan be^ composed
Projection
sname^ ratingyuppy^9 lubber^8 guppy^5 rusty^10 π^ sname rating
π S (^ )^2 age
v^ Deletes attributes that are not in^ projection list
. v^ Schema^ of result contains exactlythe fields in the projection list,with the same names that theyhad in the (only) input relation. v^ Projection operator has toeliminate
duplicates! (Why??)– Note: real systems typicallydon’t do duplicate eliminationunless the user explicitly asksfor it. (Why not?)
Union, Intersection, Set-Difference v All of these operations taketwo input relations, whichmust be^ union-compatible
:
-^ Same number of fields.–^ `Corresponding’ fieldshave the same type. v What is the^ schema
of result?
sid^ sname
rating^ age 22 dustin
7 45. 31 lubber
8 55. 58 rusty^
10 35. 44 guppy
5 35. 28 yuppy
9 35. sid^ sname
rating^ age 31 lubber
8 55. 58 rusty^
sid^ sname
rating^ age 22 dustin
Cross-Product v Each row of S1 is paired with each row of R1. v Result schema
has one field per field of S1 and R1, with field names `inherited’ if possible.–^ Conflict : Both S1 and R1 have a field called
sid.
C^ sid^
sid^ S^ R 1 1 5^
(sid)^ sname^
rating^ age^ (sid)
bid^ day 22 dustin^
7 45.0^22
101 10/10/ 22 dustin^
7 45.0^58
103 11/12/ 31 lubber^
8 55.5^22
101 10/10/ 31 lubber^
8 55.5^58
103 11/12/ 58 rusty^
10 35.0^22
101 10/10/ 58 rusty^
10 35.0^58
103 11/12/ ^ Renaming operator
:
Joins v Equi-Join : A special case of condition join wherethe condition
c^ contains only
equalities
v^ Result schema
similar to cross-product, but only one copy of fields for which equality is specified. v Natural Join
: Equijoin on
all^ common fields. sid^ sname
rating^
age^ bid^
day
22 dustin
7
45.0^101
10/10/
58 rusty
10
35.0^103
11/12/
Database Management Systems, R. Ramakrishnan and J. Gehrke Division v Not supported as a primitive operator, but useful forexpressing queries like:^ Find sailors who have reserved
all^ boats.
v^ 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 that for
every y
tuple (boat) in
B , there is an
xy^ tuple in
A****.
-^ Or : If the set of
y^ values (boats) associated with an
x^ value
(sailor) in^ A
contains all
y^ values in^
B , the^ x^ value is in
A/B.
v^ In general,
x^ and^ y^ can be any lists of fields;
y^ is the
list of fields in
B , and^ x^
y^ is the list of fields of
{^
} Ayx
Expressing A/B Using Basic Operators v Division is not essential op; just a useful shorthand.– (Also true of joins, but joins are so common that systemsimplement joins specially.) v Idea : For^ A/B
, compute all
x^ values that are not `disqualified’ by some
y^ value in
-^ 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:
Find names of sailors who’ve reserved boat #103 v Solution 1:
=^103
^ Solution 2
:^ ρ
σ( ,
π^ Temp (^ sname^
^ Solution 3
Find sailors who’ve reserved a red or a green boat v Can identify all red or green boats, then findsailors who’ve reserved one of these boats:^ ρ
σ (^
, (^ '
'^
'^ '^
))
Tempboats
color^
red^ color
Boats green =^ ∨^
=
π^ Tempboatssname
^ Can also define Tempboats using union! (How?) ^ What happens if
is replaced by
in this query?
Find sailors who’ve reserved a red and a green boat^ v^ Previous approach won’t work! Must identifysailors who’ve reserved red boats, sailorswho’ve reserved green boats, then find theintersection (note that
sid^ is a key for Sailors): ρ
π^ σ
π^ sname^
ρ
π^ σ