DBMS Unit 2 (1)
DBMS Unit 2 (1)
1
Preliminaries
{ A query is applied to relation instances,
and the result of a query is also a relation Relational Algebra
instance
z Schemas of input relations for a query are fixed { Selection
z The schema for the result of a given query is also
{ Projection
fixed
{ Set operations
{ Positional vs. named-field notation
z Positional notation is easier for formal definitions; { Renaming
named-field notation is more readable { Joins
z Both are used in SQL { Division
7 8
2
Example Instances Projection π
Boat ( bid: integer, bname: string, color: string ) { To delete attributes that are not in projection list
Sailors ( sid: integer, sname: string, rating: integer, { Schema of result contains exactly the fields in the
age: real ) projection list, with the same names that they had
Reserves ( sid: integer, bid: integer, day: date ) in the single input relation
sid sname rating age sid sname rating age { Projection operator has to eliminate duplicates!
22 Dustin 7 45.0 28 Yuppy 9 35.0
31 Lubber 8 55.5 31 Lubber 8 55.5 sname rating age
58 Rusty 10 35.0 44 guppy 5 35.0 Yuppy 9 55.5
Instance S1 of Sailors 58 Rusty 10 35.0 Lubber 8 35.0
Instance S2 of Sailors π
sid bid day guppy 5 a g e(S 2 )
22 101 10/10/96 Rusty 10
58 103 11/12/96 π s n a m e, r a tin g
(S 2 )
Instance R1 of Reserves
9 10
3
Union, Intersection, Set-Difference Cross-Product ×
{ These 3 operators { R × S = {<r, s> | r ∈ R, s ∈ S}
take 2 input relations, sid sname rating age z Each row of R is paired with each row of S
which must be union- 22 Dustin 7 45.0
compatible: z Result schema has one field per field of R and
31 Lubber 8 55.5 S, with field names inherited if possible
z Have the same
number of fields 58 Rusty 10 35.0 z The result fields have the same domains as
z Corresponding fields 44 guppy 5 35.0 the corresponding fields in R and S
have the same types 28 Yuppy 9 35.0 z Naming conflict: R and S contain field(s) with
{ Result schema S1∪ S 2 the same name
z The first relation { The corresponding fields in R × S are
sid sname rating age unnamed and referred to only by position
sid sname rating age 31 Lubber 8 55.5 { E.g., both S1 and R1 have a field sid
22 Dustin 7 45.0 58 Rusty 10 35.0
S1 − S 2 S1∩ S 2
13 14
15 16
4
Joins Joins (Cont.)
{ One of the most useful operations in { Condition Join
relational algebra R >< c S = σ c ( R × S)
{ The most common way to combine z C: join condition
information from two or more relations may refer to the attributes of both R and S
{ Defined as a cross-product followed by z Result schema is same as that of cross-product
selections and projections z Result has fewer tuples than cross-product;
{ Has a smaller result than a cross-product might be able to compute more efficiently
5
Division (Cont.) Division (Cont.)
B1 B2 B3 Division is not an essential operation; just
A {
P# P# P# a useful shorthand
S# P#
P2 P2 P1 z (Also true of joins, but joins are so common
S1 P1 that systems implement joins specially)
P4 P2
S1 P2 { Expressing division using basic operators
P4
S1 P3 z Idea: for A/B, compute all x values that are
S1 P4 A/B1 A/B2 A/B3 not “disqualified” by some y value in B
S2 P1 S# S# S# z x value is disqualified if: by attaching y value
from B, we obtain an xy tuple that is not in A
S2 P2 S1 S1 S1
S3 P2 S2 S4 Disqualified x values: π x ((π x ( A ) × B ) − A )
S4 P2 S3 A / B: π x ( A) − all disqualified tuples
S4 P4 S4
21 22
6
Examples (Cont.) Examples (Cont.)
{ Find the names of sailors who have { Find the names of sailors who have
reserved a red or a green boat reserved a red and a green boat
z Identify all red or green boats, then find sailors z Identify sailors who have reserved red boats,
who have reserved one of these boats sailors who have reserved green boats, and
then, find the intersection
ρ (Tempboats, (σ Boats)) z Note that sid is a key for Sailors
color =' red ' ∨ color = ' green '
π s n a m e ( T e m p b o a ts >< R e s e r v e s >< S a ilo r s ) ρ ( T em p red , π ( (σ B o a ts ) >< R e serves ))
sid co lo r =' red '
ρ (Tempgreen, π ((σ Boats) >< Re serves))
• Tempboats can also be defined using union sid color =' green'
• What if “∨” is replaced by “∧” in this query? π sn a m e (( T em p red ∩ T em p g reen ) >< S a ilo rs )
25 26
27 28
7
Relational Calculus
{ Two variants of relational calculus
z Tuple relational calculus (TRC): SQL
Relational Calculus z Domain relational calculus (DRC): QBE
{ Calculus has variables, constants, comparison
operators, logical connectives and quantifiers
{ Domain relational calculus
z TRC: variables range over tuples
{ Tuple relational calculus z DRC: variables range over domain elements (= field
values)
z Both TRC and DRC are simple subsets of first-order
logic
{ Calculus expressions are called formulas
{ An answer tuple is an assignment of constants
to variables that make the formula evaluate to
true
30
31 32
8
Domain Relational Calculus (Cont.) DRC Query Examples
{ Free and bound variables { Find all sailors with a rating above 7
z ∃ and ∀ are quantifiers
z The use of ∃X and ∀X is said to bind X { 〈 I, N, T, A〉 | 〈 I, N, T, A〉 ∈ Sailors ∧ T > 7}
z A variable that is not bound is free
{ An important restriction on the definition z The condition 〈I, N, T, A〉 ∈ Sailors ensures
of a DRC query that the domain variables I, N, T and A are
{ 〈x1, x2, …, xn〉 | p ( 〈x1, x2, …, xn〉 ) } bound to fields of the same Sailors tuple
z The variables x1, x2, …, xn that appear to the left z “|” should be read as “such that”
of `|’ must be the only free variables in the z The term 〈I, N, T, A〉 to the left of “|” says
formula p ( …) that every tuple 〈I, N, T, A〉 that satisfies T>7
is in the answer
34
9
DRC Query Examples (Cont.) DRC Query Examples (Cont.)
{ Find the names of sailors who have { Find the names of sailors who have
reserved all boats (solution 1) reserved all boats (solution 2)
z Find all sailors 〈I, N, T, A〉 such that: for each z Simpler notation, same query (much clearer!)
3-tuple 〈B, BN, C〉, either it is not a tuple in
Boats, or there is a tuple in Reserves showing
that sailor I has reserved B
{ 〈 N〉 | ∃ I, T, A ( 〈 I, N, T, A〉 ∈ Sailors ∧
∀ 〈 B, BN, C〉 ∈ Boats
( ∃ 〈 Ir, Br, D〉 ∈ Reserves ( Ir = I ∧ Br = B ) ) ) }
{ 〈 N〉 | ∃ I, T, A ( 〈 I, N, T, A〉 ∈ Sailors ∧
∀ B, BN, C ( ¬ (〈 B, BN, C〉 ∈ Boats ) ∨ { To find the names of sailors who jave
( ∃ 〈 Ir, Br, D〉 ∈ Reserves ( Ir = I ∧ Br = B ) ) ) ) } reserved all red boats
{ 〈 N〉 | ∃ I, T, A ( 〈 I, N, T, A〉 ∈ Sailors ∧
∀ 〈 B, BN, C〉 ∈ Boats ( C ≠ ‘red’ ∨
37
∃ 〈 Ir, Br, D〉 ∈ Reserves ( Ir = I ∧ Br = B ) ) ) } 38
39 40
10
TRC Query Examples TRC Query Examples (Cont.)
{ Find the names and ages of sailors with a { Find the names of sailors who have
rating above 7 reserved all boats
{ P | ∃ S∈ Sailors ( S.rating>7 ∧ P.name = S.sname { P | ∃ S ∈ Sailors ∀ B ∈ Boats
∧ P.age = S.age ) } ( ∃ R ∈ Reserves ( S.sid = R.sid ∧ R.bid = B.bid
∧ P.sname = S.sname ) ) }
z P is a tuple variable with two fields: name
and age { Find sailors who have reserved all red
z P.name=S.sname and P.age=S.age gives values boats
to the fields of an answer tuple P
* If a variable R does not appear in an atomic { S | S ∈ Sailors ∧ ∀ B ∈ Boats
formula of the form R ∈ Rname, the type of R ( B.color ≠ ’red’ ∨ (∃ R ∈ Reserves
is a tuple whose fields include all (and only) ( S.sid = R.sid ∧ R.bid = B.bid ) ) ) }
fields of R that appear in the formula
41 42
43 44
11