Beruflich Dokumente
Kultur Dokumente
Lecture 2
Review of the
Relational Model and SQL
Properties of a Relation
More Definitions
Example
Relation schema
Relation instance
sno
sname
scity
sstate
s1
city 1
WA
s2
city 1
WA
s3
city 2
MA
s4
city 2
MA
Integrity Constraints
Key Constraints
Key constraint: certain minimal subset of fields
is a unique identifier for a tuple
Integrity constraint
Condition specified on a database schema
Restricts data that can be stored in db instance
Candidate key
Minimal set of fields
That uniquely identify each tuple in a relation
Foreign key
Field that refers to tuples in another relation
Typically, this field refers to the primary key of other
relation
Can pick another field as well
10
Primary key
11
12
13
General Constraints
14
Relational Queries
Query evaluation
Input: instances of input relations
Output: instance of output relation
15
Relational Algebra
Relational Operators
Selection: condition(S)
Condition is Boolean combination (,) of terms
Term is: attr. op constant, attr. op attr.
Op is: <, <=, =, , >=, or >
Relational operators
Take one or two relation instances as argument
Return one relation instance as result
Easy to compose into relational algebra expressions
Magda Balazinska - CSE 444, Spring 2011
16
17
Projection: list-of-attributes(S)
Union (), Intersection (), Set difference (),
Cross-product or cartesian product ()
Join: R S = (R S)
Division: R/S, Rename (R(F),E)
Magda Balazinska - CSE 444, Spring 2011
18
Relational Operators
zip,disease(Patient)
no
name
zip
disease
zip
disease
p1
98125
flu
98125
flu
p2
98125
heart
98125
heart
p3
98120
lung
98120
lung
p4
98120
heart
98120
heart
disease=heart(Patient)
Selection: condition(S)
Condition is Boolean combination (,) of terms
Term is: attr. op constant, attr. op attr.
Op is: <, <=, =, , >=, or >
zip (disease=heart(Patient))
no
name
zip
disease
zip
p2
98125
heart
98120
p4
98120
heart
98125
Projection: list-of-attributes(S)
Union (), Intersection (), Set difference (),
Cross-product or cartesian product ()
Join: R S = (R S)
Division: R/S, Rename (R(F),E)
Magda Balazinska - CSE 444, Spring 2011
19
Cross-Product Example
AnonPatient P
Voters V
age
zip
disease
name
age
zip
54
98125
heart
p1
54
98125
20
98120
flu
p2
20
98120
Theta-join: R
P.zip
disease
name
V.age
V.zip
54
98125
heart
p1
54
98125
54
98125
heart
p2
20
98120
20
98120
flu
p1
54
98125
20
98120
flu
p2
20
98120
Equijoin: R
= (R x S)
= A ((R x S))
Natural join: R
S = A ((R x S))
Equijoin
Equality on all fields with same name in R and in S
Magda Balazinska - CSE 444, Spring 2011
21
Theta-Join Example
AnonPatient P
P V
P.age
20
22
Equijoin Example
AnonPatient P
Voters V
Voters V
age
zip
disease
name
age
zip
age
zip
disease
name
age
zip
54
98125
heart
p1
54
98125
54
98125
heart
p1
54
98125
20
98120
flu
p2
20
98120
20
98120
flu
p2
20
98120
P.age
P.zip
disease
name
V.age
V.zip
20
98120
flu
p2
20
98120
23
P.age=V.age
age
P.zip
disease
name
V.zip
54
98125
heart
p1
98125
20
98120
flu
p2
98120
24
Voters V
age
zip
disease
name
age
zip
54
98125
heart
p1
54
98125
20
98120
flu
p2
20
98120
More Joins
Outer join
Include tuples with no matches in the output
Use NULL values for missing attributes
age
zip
disease
name
54
98125
heart
p1
20
98120
flu
p2
Variants
Left outer join
Right outer join
Full outer join
25
disease
name
age
zip
54
98125
heart
p1
54
98125
20
98120
flu
p2
20
98120
33
98120
lung
age
zip
disease
name
54
98125
heart
p1
20
98120
flu
p2
33
98120
lung
null
26
Voters V
age
27
28
Extended Operators
of Relational Algebra
More Examples
Duplicate elimination ()
Relations
Supplier(sno,sname,scity,sstate)!
!Part(pno,pname,psize,pcolor)!
!Supply(sno,pno,qty,price)!
Aggregate operators ()
Min, max, sum, average, count
Grouping operators ()
Q3: Name of supplier of red parts or parts with size greater than 10
sname(Supplier Supply (psize>10 (Part) pcolor=red (Part) ) )
Sort operator ()
29
30
SQL Query
Basic form: (plus many many more bells and whistles)
SELECT <attributes>
FROM <one or more relations>
WHERE <conditions>
More
32
PName
Price
Category
Manufacturer
PName
Price
Category
Manufacturer
Gizmo
$19.99
Gadgets
GizmoWorks
Gizmo
$19.99
Gadgets
GizmoWorks
Powergizmo
$29.99
Gadgets
GizmoWorks
Powergizmo
$29.99
Gadgets
GizmoWorks
SingleTouch
$149.99
Photography
Canon
SingleTouch
$149.99
Photography
Canon
MultiTouch
$203.99
Household
Hitachi
MultiTouch
$203.99
Household
Hitachi
SELECT *
FROM
Product
WHERE category=Gadgets
selection
31
Product
PName
Price
Category
Manufacturer
Gizmo
$19.99
Gadgets
GizmoWorks
Powergizmo
$29.99
Gadgets
GizmoWorks
33
Details
selection and
projection
PName
Price
SingleTouch
$149.99
Manufacturer
Canon
MultiTouch
$203.99
Hitachi
34
Eliminating Duplicates
Category
Case insensitive:
Gadgets
Photography
Household
Compare to:
Constants:
Category
abc - yes
abc - no
SELECT category
FROM Product
Gadgets
Gadgets
Photography
Household
35
36
Joins
Product (pname, price, category, manufacturer)
Company (cname, stockPrice, country)
Ties are broken by the second attribute on the ORDER BY list, etc.
Ordering is ascending, unless you specify the DESC keyword.
37
Tuple Variables
Person(pname, address, worksfor)
Company(cname, address)
SELECT DISTINCT pname, address
FROM
Person, Company
WHERE worksfor = cname
38
Nested Queries
Nested query
Which
address ?
39
40
Correlated Queries
Aggregation
SELECT avg(price)
FROM
Product
WHERE maker=Toyota
SELECT count(*)
FROM Product
WHERE year > 1995
Note (1) scope of variables (2) this can still be expressed as single SFW
Magda Balazinska - CSE 444, Spring 2011
43
44
45