Lecture 12: Further relational algebra, further SQL
description
Transcript of Lecture 12: Further relational algebra, further SQL
![Page 1: Lecture 12: Further relational algebra, further SQL](https://reader036.fdocuments.us/reader036/viewer/2022062316/56816860550346895ddeaec3/html5/thumbnails/1.jpg)
1
Lecture 12:Further relational algebra,
further SQL
www.cl.cam.ac.uk/Teaching/current/Databases/
![Page 2: Lecture 12: Further relational algebra, further SQL](https://reader036.fdocuments.us/reader036/viewer/2022062316/56816860550346895ddeaec3/html5/thumbnails/2.jpg)
2
Today’s lecture• Where does SQL differ from relational
model?• What are some other features of SQL?• How can we extend the relational algebra
to match more closely SQL?
![Page 3: Lecture 12: Further relational algebra, further SQL](https://reader036.fdocuments.us/reader036/viewer/2022062316/56816860550346895ddeaec3/html5/thumbnails/3.jpg)
3
Duplicate rows• Consider our relation instances from lecture
6, Reserves, Sailors and Boats• Consider SELECT rating,age FROM Sailors;• We get a relation that doesn’t satisfy our
definition of a relation!• RECALL: We have the keyword DISTINCT to
remove duplicates
![Page 4: Lecture 12: Further relational algebra, further SQL](https://reader036.fdocuments.us/reader036/viewer/2022062316/56816860550346895ddeaec3/html5/thumbnails/4.jpg)
4
Multiset semantics• A relation in SQL is really a multiset or
bag, rather than a set as in the relational model– A multiset has no order (unlike a list), but
allows duplicates– E.g. {1,2,1,3} is a bag– select, project and join work for bags as well
as sets• Just work on a tuple-by-tuple basis
![Page 5: Lecture 12: Further relational algebra, further SQL](https://reader036.fdocuments.us/reader036/viewer/2022062316/56816860550346895ddeaec3/html5/thumbnails/5.jpg)
5
Bag operations• Bag union:
– Sum the number of times that an element appears in the two bags, e.g.
• {1,2,1}{1,2,3} = {1,1,1,2,2,3}
• Bag intersection:– Take the minimum of the number of occurrences in
each bag, e.g.• {1,2,1}{1,2,3,3} = {1,2}
• Bag difference:– Proper-subtract the number of occurrences in the two
bags, e.g.• {1,2,1}-{1,2,3,3} = {1}
![Page 6: Lecture 12: Further relational algebra, further SQL](https://reader036.fdocuments.us/reader036/viewer/2022062316/56816860550346895ddeaec3/html5/thumbnails/6.jpg)
6
Laws for bags• Note that whilst some of the familiar (set-
theoretic) laws continue to hold, some of them do not
• Example: R(ST) = (RS)(RT) ??
![Page 7: Lecture 12: Further relational algebra, further SQL](https://reader036.fdocuments.us/reader036/viewer/2022062316/56816860550346895ddeaec3/html5/thumbnails/7.jpg)
7
Extended relational algebraAdd features needed for SQL
1. Bag semantics2. Duplicate elimination operator, 3. Sorting operator, 4. Grouping and aggregation operator, 5. Outerjoin operators, oV, Vo, oVo
![Page 8: Lecture 12: Further relational algebra, further SQL](https://reader036.fdocuments.us/reader036/viewer/2022062316/56816860550346895ddeaec3/html5/thumbnails/8.jpg)
8
Duplicate-elimination operator
(R) = relation R with any duplicated tuples removed
• R= (R)=
• This is used to model the DISTINCT feature of SQL
A B1 23 41 2
A B1 23 4
![Page 9: Lecture 12: Further relational algebra, further SQL](https://reader036.fdocuments.us/reader036/viewer/2022062316/56816860550346895ddeaec3/html5/thumbnails/9.jpg)
9
Sorting L1,… Ln
(R) returns a list of tuples of R, ordered according to the attributes L1, …, Ln
• Note: does not return a relation• R= B(R)= [(5,2),(1,3),(3,4)]
• ORDER BY in SQL, e.g. SELECT * FROM Sailors WHERE rating>7 ORDER BY age, sname;
A B
1 3
3 4
5 2
![Page 10: Lecture 12: Further relational algebra, further SQL](https://reader036.fdocuments.us/reader036/viewer/2022062316/56816860550346895ddeaec3/html5/thumbnails/10.jpg)
10
Extended projection• SQL allows us to use arithmetic operators SELECT age*5 FROM Sailors;• We extend the projection operator to allow the
columns in the projection to be functions of one or more columns in the argument relation, e.g.
• R= A+B,A,A(R)=
A B1 23 4
A+B A.1 A.23 1 17 3 3
![Page 11: Lecture 12: Further relational algebra, further SQL](https://reader036.fdocuments.us/reader036/viewer/2022062316/56816860550346895ddeaec3/html5/thumbnails/11.jpg)
11
Arithmetic• Arithmetic (and other expressions) can not
be used at the top level– i.e. 2+2 is not a valid SQL query
• How would you get SQL to compute 2+2?
![Page 12: Lecture 12: Further relational algebra, further SQL](https://reader036.fdocuments.us/reader036/viewer/2022062316/56816860550346895ddeaec3/html5/thumbnails/12.jpg)
12
Aggregation• SQL provides us with operations to summarise a
column in some way, e.g. SELECT COUNT(rating) FROM Sailors;
SELECT COUNT(DISTINCT rating) FROM Sailors;
SELECT COUNT(*) FROM Sailors WHERE rating>7;• We also have SUM, AVG, MIN and MAX
![Page 13: Lecture 12: Further relational algebra, further SQL](https://reader036.fdocuments.us/reader036/viewer/2022062316/56816860550346895ddeaec3/html5/thumbnails/13.jpg)
13
Grouping• These aggregation operators have been
applied to all qualifying tuples. Sometimes we want to apply them to each of several groups of tuples, e.g.– For each rating, find the average age of the
sailors– For each rating, find the age of the youngest
sailor
![Page 14: Lecture 12: Further relational algebra, further SQL](https://reader036.fdocuments.us/reader036/viewer/2022062316/56816860550346895ddeaec3/html5/thumbnails/14.jpg)
14
GROUP BY in SQL SELECT [DISTINCT] target-list FROM relation-list WHERE qualification GROUP BY grouping-list;• The target-list contains
1. List of column names2. Aggregate terms– NOTE: The variables in target-list must be
contained in grouping-list
![Page 15: Lecture 12: Further relational algebra, further SQL](https://reader036.fdocuments.us/reader036/viewer/2022062316/56816860550346895ddeaec3/html5/thumbnails/15.jpg)
15
GROUP BY cont.For each rating, find the average age of the sailors SELECT rating,AVG(age) FROM Sailors GROUP BY rating;
For each rating find the age of the youngest sailor SELECT rating,MIN(age) FROM Sailors GROUP BY rating;
![Page 16: Lecture 12: Further relational algebra, further SQL](https://reader036.fdocuments.us/reader036/viewer/2022062316/56816860550346895ddeaec3/html5/thumbnails/16.jpg)
16
Grouping and aggregationL(R) where L is a list of elements that are
either– Individual column names (“Grouping
attributes”), or– Of the form (A), where is an aggregation
operator (MIN, SUM, …) and A is the column it is applied to
• For example,rating,AVG(age)(Sailors)
![Page 17: Lecture 12: Further relational algebra, further SQL](https://reader036.fdocuments.us/reader036/viewer/2022062316/56816860550346895ddeaec3/html5/thumbnails/17.jpg)
17
Semantics• Group R according to the grouping
attributes• Within each group, compute (A)• Result is the relation consisting of one
tuple for each group. The components of that tuple are the values associated with each element of L for that group
![Page 18: Lecture 12: Further relational algebra, further SQL](https://reader036.fdocuments.us/reader036/viewer/2022062316/56816860550346895ddeaec3/html5/thumbnails/18.jpg)
18
Example• Let R=
• Compute beer,AVG(price)(R)
bar beer priceAnchor 6X 2.50Anchor Adnam’s 2.40Mill 6X 2.60Mill Fosters 2.80Eagle Fosters 2.90
![Page 19: Lecture 12: Further relational algebra, further SQL](https://reader036.fdocuments.us/reader036/viewer/2022062316/56816860550346895ddeaec3/html5/thumbnails/19.jpg)
19
Example cont.1. Group according to the grouping attribute,
beer:
2. Compute average of price within groups:
bar beer priceAnchor 6X 2.50Mill 6X 2.60Anchor Adnam’s 2.40Mill Fosters 2.80Eagle Fosters 2.90
beer price6X 2.55Adnam’s 2.40Fosters 2.85
![Page 20: Lecture 12: Further relational algebra, further SQL](https://reader036.fdocuments.us/reader036/viewer/2022062316/56816860550346895ddeaec3/html5/thumbnails/20.jpg)
20
NULL values• Sometimes field values are unknown (e.g. rating
not known yet), or inapplicable (e.g. no spouse name)
• SQL provides a special value, NULL, for both these situations
• This complicates several issues– Special operators needed to check for NULL– Is NULL>8? Is (NULL OR TRUE)=TRUE?– We need a three-valued logic– Need to carefully re-define semantics
![Page 21: Lecture 12: Further relational algebra, further SQL](https://reader036.fdocuments.us/reader036/viewer/2022062316/56816860550346895ddeaec3/html5/thumbnails/21.jpg)
21
NULL values• Consider INSERT INTO Sailors (sid,sname) VALUES (101,”Julia”);
SELECT * FROM Sailors;
SELECT rating FROM Sailors;
SELECT sname FROM Sailors WHERE rating>0;
![Page 22: Lecture 12: Further relational algebra, further SQL](https://reader036.fdocuments.us/reader036/viewer/2022062316/56816860550346895ddeaec3/html5/thumbnails/22.jpg)
22
Entity integrity constraint• An entity integrity constraint states that
no primary key value can be NULL
![Page 23: Lecture 12: Further relational algebra, further SQL](https://reader036.fdocuments.us/reader036/viewer/2022062316/56816860550346895ddeaec3/html5/thumbnails/23.jpg)
23
Outer join• Note that with the usual join, a tuple that
doesn’t ‘join’ with any from the other relation is removed from the resulting relation
• Instead, we can ‘pad out’ the columns with NULLs
• This operator is called an full outer join, written oVo
![Page 24: Lecture 12: Further relational algebra, further SQL](https://reader036.fdocuments.us/reader036/viewer/2022062316/56816860550346895ddeaec3/html5/thumbnails/24.jpg)
24
Example of full outer join• Let R= Let S=
• Then RVS =
• But RoVoS =
A B1 23 4
B C4 56 7
A B C3 4 5
A B C1 2 NULL
3 4 5NULL 6 7
![Page 25: Lecture 12: Further relational algebra, further SQL](https://reader036.fdocuments.us/reader036/viewer/2022062316/56816860550346895ddeaec3/html5/thumbnails/25.jpg)
25
Outer joins in SQL• SQL/92 has three variants:
– LEFT OUTER JOIN (algebra: oV)– RIGHT OUTER JOIN (algebra: Vo)– FULL OUTER JOIN (algebra: oVo)
• For example: SELECT * FROM Reserves r LEFT OUTER JOIN Sailors s ON r.sid=s.sid;
![Page 26: Lecture 12: Further relational algebra, further SQL](https://reader036.fdocuments.us/reader036/viewer/2022062316/56816860550346895ddeaec3/html5/thumbnails/26.jpg)
26
Views• A view is a query with a name that can be used in
further SELECT statements, e.g. CREATE VIEW ExpertSailors(sid,sname,age) AS SELECT sid,sname,age FROM Sailors WHERE rating>9;
• Note that ExpertSailors is not a stored relation• (WARNING: mysql does not support views )
![Page 27: Lecture 12: Further relational algebra, further SQL](https://reader036.fdocuments.us/reader036/viewer/2022062316/56816860550346895ddeaec3/html5/thumbnails/27.jpg)
27
Querying views• So an example query SELECT sname FROM ExpertSailors WHERE age>27;• is translated by the system to the following: SELECT sname FROM Sailors WHERE rating>9 AND age>27;
![Page 28: Lecture 12: Further relational algebra, further SQL](https://reader036.fdocuments.us/reader036/viewer/2022062316/56816860550346895ddeaec3/html5/thumbnails/28.jpg)
28
SummaryYou should now understand:• Multi-set semantics• Conditions• Aggregation• GROUP BY• NULLs, entity ICs and outer joins• Views• Extensions to the core relational algebra