View
224
Download
0
Category
Tags:
Preview:
Citation preview
Fall 2001 Arthur Keller – CS 180 6–1
Schedule• Today (TH) Bags and SQL Queries.
Read Sections 6.1-6.3. Project Part 2 due.
• Oct. 16 (T) Duplicates, Aggregation, Modifications. Read Sections 5.3-5.4, 6.4-6.5. Assignment 3
due.
• Oct. 18 (TH) Schemas, Views. Read Sections 6.6-6.7. Project Part 3 due.
Fall 2001 Arthur Keller – CS 180 6–2
Review: Weak Entities• Consider a relationship, Ordered, between two
entity sets, Buyer and Product
• How can we add Shipments to the mix?
is wrong. Why?
Buyer ProductOrdered
Qty
Buyer ProductOrdered
QtyShipment
Name
UPC
Name
UPC
ID
Fall 2001 Arthur Keller – CS 180 6–3
• Solution: make Ordered into a weak entity set.
• And then add Shipment.
Buyer Product
Qty
Buyer Product
Shipment
Name
UPC
Name
UPC
ID
OrderedOB OP
QtyOrdered
OrderedOB OP
Part ofQty
Shipped
Part-of ismany-many and not a weak relationship!
Fall 2001 Arthur Keller – CS 180 6–4
Bag Semantics
A relation (in SQL, at least) is really a bag or multiset.
• It may contain the same tuple more than once, although there is no specified order (unlike a list).
• Example: {1,2,1,3} is a bag and not a set.• Select, project, and join work for bags as
well as sets. Just work on a tuple-by-tuple basis, and don't
eliminate duplicates.
Fall 2001 Arthur Keller – CS 180 6–5
Bag Union
Sum the times an element appears in the two bags.• Example: {1,2,1} {1,2,3,3} = {1,1,1,2,2,3,3}.
Bag IntersectionTake the minimum of the number of occurrences in each
bag.• Example: {1,2,1} {1,2,3,3} = {1,2}.
Bag DifferenceProper-subtract the number of occurrences in the two bags.• Example: {1,2,1} – {1,2,3,3} = {1}.
Fall 2001 Arthur Keller – CS 180 6–6
Laws for Bags Differ From Laws for Sets
• Some familiar laws continue to hold for bags. Examples: union and intersection are still commutative and
associative.
• But other laws that hold for sets do not hold for bags.
ExampleR (S T) (R S) (R T) holds for sets.• Let R, S, and T each be the bag {1}.• Left side: S T = {1,1}; R (S T) = {1}.• Right side: R S = R T = {1};
(R S) (R T) = {1} {1} = {1,1} {1}.
Fall 2001 Arthur Keller – CS 180 6–7
Extended (“Nonclassical”)Relational Algebra
Adds features needed for SQL, bags.
1. Duplicate-elimination operator .
2. Extended projection.
3. Sorting operator .
4. Grouping-and-aggregation operator .
5. Outerjoin operator o .
Fall 2001 Arthur Keller – CS 180 6–8
Duplicate Elimination(R) = relation with one copy of each tuple that appears one
or more times in R.
ExampleR = A B
1 23 41 2
(R) = A B1 23 4
Fall 2001 Arthur Keller – CS 180 6–9
Sorting L(R) = list of tuples of R, ordered according to
attributes on list L.• Note that result type is outside the normal types
(set or bag) for relational algebra. Consequence: cannot be followed by other relational
operators.
ExampleR = A B
1 33 45 2
B(R) = [(5,2), (1,3), (3,4)].
Fall 2001 Arthur Keller – CS 180 6–10
Extended Projection
Allow the columns in the projection to be functions of one or more columns in the argument relation.
ExampleR = A B
1 23 4
A+B,A,A(R) =A+B A1 A23 1 17 3 3
Fall 2001 Arthur Keller – CS 180 6–11
Aggregation Operators
• These are not relational operators; rather they summarize a column in some way.
• Five standard operators: Sum, Average, Count, Min, and Max.
Fall 2001 Arthur Keller – CS 180 6–12
Grouping Operator
L(R), where L is a list of elements that are either
a) Individual (grouping) attributes orb) Of the form (A), where is an aggregation operator
and A the attribute to which it is applied,is computed by:1. Group R according to all the grouping attributes on list L.2. Within each group, compute (A), for each element (A)
on list L.3. Result is the relation whose columns consist of one tuple
for each group. The components of that tuple are the values associated with each element of L for that group.
Fall 2001 Arthur Keller – CS 180 6–13
ExampleLet R = bar beer price
Joe's Bud 2.00Joe's Miller 2.75Sue's Bud 2.50Sue's Coors 3.00Mel's Miller 3.25
Compute beer,AVG(price)(R).
1. Group by the grouping attribute(s), beer in this case:bar beer priceJoe's Bud 2.00Sue's Bud 2.50Joe's Miller 2.75Mel's Miller 3.25Sue's Coors 3.00
Fall 2001 Arthur Keller – CS 180 6–14
2.Compute average of price within groups:
beer AVG(price)
Bud 2.25
Miller 3.00
Coors 3.00
Fall 2001 Arthur Keller – CS 180 6–15
Outerjoin
The normal join can “lose” information, because a tuple that doesn’t join with any from the other relation (dangles) has no vestage in the join result.
• The null value can be used to “pad” dangling tuples so they appear in the join.
• Gives us the outerjoin operator o .• Variations: theta-outerjoin, left- and right-
outerjoin (pad only dangling tuples from the left (resp., right).
Fall 2001 Arthur Keller – CS 180 6–16
Example
R = A B1 23 4
S = B C4 56 7
R o S = A B C3 4 51 2 6 7
Fall 2001 Arthur Keller – CS 180 6–17
SQL Queries
• Principal form:SELECT desired attributesFROM tuple variables –– range over relationsWHERE condition about tuple variables;
Running example relation schema:Beers(name, manf)Bars(name, addr, license)Drinkers(name, addr, phone)Likes(drinker, beer)Sells(bar, beer, price)Frequents(drinker, bar)
Fall 2001 Arthur Keller – CS 180 6–18
Example
What beers are made by Anheuser-Busch?Beers(name, manf)
SELECT nameFROM BeersWHERE manf = 'Anheuser-Busch';
• Note single quotes for strings.nameBudBud LiteMichelob
Fall 2001 Arthur Keller – CS 180 6–19
Formal Semantics of Single-Relation SQL Query
1. Start with the relation in the FROM clause.2. Apply (bag) , using condition in WHERE clause.3. Apply (extended, bag) using attributes in
SELECT clause.
Equivalent Operational SemanticsImagine a tuple variable ranging over all tuples of
the relation. For each tuple:• Check if it satisfies the WHERE clause.• Print the values of terms in SELECT, if so.
Fall 2001 Arthur Keller – CS 180 6–20
Star as List of All Attributes
Beers(name, manf)
SELECT *
FROM Beers
WHERE manf = 'Anheuser-Busch';
name manf
Bud Anheuser-Busch
Bud Lite Anheuser-Busch
Michelob Anheuser-Busch
Fall 2001 Arthur Keller – CS 180 6–21
Renaming columns
Beers(name, manf)
SELECT name AS beer
FROM Beers
WHERE manf = 'Anheuser-Busch';
beer
Bud
Bud Lite
Michelob
Fall 2001 Arthur Keller – CS 180 6–22
Expressions as Values in Columns
Sells(bar, beer, price)
SELECT bar, beer,
price*120 AS priceInYen
FROM Sells;
bar beer priceInYen
Joe’s Bud 300
Sue’s Miller 360… … …
• Note: no WHERE clause is OK.
Fall 2001 Arthur Keller – CS 180 6–23
• Trick: If you want an answer with a particular string in each row, use that constant as an expression.Likes(drinker, beer)
SELECT drinker,'likes Bud' AS whoLikesBud
FROM LikesWHERE beer = 'Bud';
drinker whoLikesBudSally likes BudFred likes Bud… …
Fall 2001 Arthur Keller – CS 180 6–24
Example• Find the price Joe's Bar charges for Bud.
Sells(bar, beer, price)
SELECT priceFROM SellsWHERE bar = 'Joe''s Bar' AND
beer = 'Bud';
• Note: two single-quotes in a character string represent one single quote.
• Conditions in WHERE clause can use logical operators AND, OR, NOT and parentheses in the usual way.
• Remember: SQL is case insensitive. Keywords like SELECT or AND can be written upper/lower case as you like. Only inside quoted strings does case matter.
Fall 2001 Arthur Keller – CS 180 6–25
Patterns• % stands for any string.• _ stands for any one character.• “Attribute LIKE pattern” is a condition that is true
if the string value of the attribute matches the pattern. Also NOT LIKE for negation.
ExampleFind drinkers whose phone has exchange 555.
Drinkers(name, addr, phone)
SELECT nameFROM DrinkersWHERE phone LIKE '%555-_ _ _ _’;
• Note patterns must be quoted, like strings.
Fall 2001 Arthur Keller – CS 180 6–26
Nulls
In place of a value in a tuple's component.• Interpretation is not exactly “missing value.”• There could be many reasons why no value is
present, e.g., “value inappropriate.”
Comparing Nulls to Values• 3rd truth value UNKNOWN.• A query only produces tuples if the WHERE-
condition evaluates to TRUE(UNKNOWN is not sufficient).
Fall 2001 Arthur Keller – CS 180 6–27
Example
bar beer price
Joe's bar Bud NULLSELECT barFROM SellsWHERE price < 2.00 OR price >= 2.00;
UNKNOWN UNKNOWN
UNKNOWN
• Joe's Bar is not produced, even though the WHERE condition is a tautology.
Fall 2001 Arthur Keller – CS 180 6–28
3-Valued LogicThink of true = 1; false = 0, and unknown = 1/2. Then:• AND = min.• OR = max.• NOT(x) = 1 – x.
Some Key Laws Fail to HoldExample: Law of the excluded middle, i.e.,
p OR NOT p = TRUE
• For 3-valued logic: if p = unknown, then left side = max(1/2,(1-1/2)) = 1/2 ≠ 1.
• Like bag algebra, there is no way known to make 3-valued logic conform to all the laws we expect for sets/2-valued logic, respectively.
Fall 2001 Arthur Keller – CS 180 6–29
Multirelation Queries• List of relations in FROM clause.• Relation-dot-attribute disambiguates attributes from
several relations.
ExampleFind the beers that the frequenters of Joe's Bar like.
Likes(drinker, beer)Frequents(drinker, bar)
SELECT beerFROM Frequents, LikesWHERE bar = 'Joe''s Bar' AND
Frequents.drinker = Likes.drinker;
Fall 2001 Arthur Keller – CS 180 6–30
Formal Semantics of Multirelation Queries
Same as for single relation, but start with the product of all
the relations mentioned in the FROM clause.
Operational SemanticsConsider a tuple variable for each relation in the FROM.
• Imagine these tuple variables each pointing to a tuple of
their relation, in all combinations (e.g., nested loops).
• If the current assignment of tuple-variables to tuples
makes the WHERE true, then output the attributes of the
SELECT.
Fall 2001 Arthur Keller – CS 180 6–31
bar
Frequents
drinker beerdrinker
Likes
flSally
SallyJoe’s
Fall 2001 Arthur Keller – CS 180 6–32
Explicit Tuple VariablesSometimes we need to refer to two or more copies of a relation.• Use tuple variables as aliases of the relations.
ExampleFind pairs of beers by the same manufacturer.
Beers(name, manf)
SELECT b1.name, b2.nameFROM Beers b1, Beers b2WHERE b1.manf = b2.manf AND
b1.name < b2.name;
• SQL permits AS between relation and its tuple variable;Oracle does not.
• Note that b1.name < b2.name is needed to avoid producing (Bud, Bud) and to avoid producing a pair in both orders.
Fall 2001 Arthur Keller – CS 180 6–33
SubqueriesResult of a select-from-where query can be used in the where-clause of
another query.
Simplest Case: Subquery Returns a Single, Unary Tuple
Find bars that serve Miller at the same price Joe charges for Bud.Sells(bar, beer, price)
SELECT barFROM SellsWHERE beer = 'Miller' AND price =
(SELECT priceFROM Sells}WHERE bar = 'Joe''s Bar' AND
beer = 'Bud');• Notice the scoping rule: an attribute refers to the most closely nested
relation with that attribute.• Parentheses around subquery are essential.
Fall 2001 Arthur Keller – CS 180 6–34
The IN Operator“Tuple IN relation” is true iff the tuple is in the relation.
ExampleFind the name and manufacturer of beers that Fred likes.
Beers(name, manf)Likes(drinker, beer)
SELECT *FROM BeersWHERE name IN
(SELECT beerFROM LikesWHERE drinker = 'Fred’);
• Also: NOT IN.
Fall 2001 Arthur Keller – CS 180 6–35
EXISTS“ EXISTS(relation)” is true iff the relation is nonempty.
ExampleFind the beers that are the unique beer by their manufacturer.
Beers(name, manf)
SELECT nameFROM Beers b1WHERE NOT EXISTS
(SELECT *FROM BeersWHERE manf = b1.manf AND
name <> b1.name);
• Note scoping rule: to refer to outer Beers in the inner subquery, we need to give the outer a tuple variable, b1 in this example.
• A subquery that refers to values from a surrounding query is called a correlated subquery.
Fall 2001 Arthur Keller – CS 180 6–36
QuantifiersANY and ALL behave as existential and universal quantifiers,
respectively.• Beware: in common parlance, “any” and “all” seem to be
synonyms, e.g., “I am fatter than any of you” vs. “I am fatter than all of you.” But in SQL:
ExampleFind the beer(s) sold for the highest price.
Sells(bar, beer, price)
SELECT beerFROM SellsWHERE price >= ALL(
SELECT priceFROM Sells);
Class ProblemFind the beer(s) not sold for the lowest price.
Recommended