From Relational Algebra to the Structured Query Language Rose-Hulman Institute of Technology Curt...
-
date post
15-Jan-2016 -
Category
Documents
-
view
223 -
download
0
Transcript of From Relational Algebra to the Structured Query Language Rose-Hulman Institute of Technology Curt...
![Page 1: From Relational Algebra to the Structured Query Language Rose-Hulman Institute of Technology Curt Clifton.](https://reader036.fdocuments.in/reader036/viewer/2022062309/56649d395503460f94a13c09/html5/thumbnails/1.jpg)
From Relational Algebra to the Structured Query Language
Rose-Hulman Institute of Technology
Curt Clifton
![Page 2: From Relational Algebra to the Structured Query Language Rose-Hulman Institute of Technology Curt Clifton.](https://reader036.fdocuments.in/reader036/viewer/2022062309/56649d395503460f94a13c09/html5/thumbnails/2.jpg)
Review – Relational Integrity Entity Integrity Constraints:
Primary key values cannot be null Referential Integrity Constraints:
Foreign key values must either: Match the primary key values of some tuple, or Be null
![Page 3: From Relational Algebra to the Structured Query Language Rose-Hulman Institute of Technology Curt Clifton.](https://reader036.fdocuments.in/reader036/viewer/2022062309/56649d395503460f94a13c09/html5/thumbnails/3.jpg)
Review – Relational Algebra Intersection: R1 R2 Union: R1 R2 Difference: R1 - R2 Selection: BDATE < 1970-1-1 (EMPLOYEE)
Projection: FNAME, BDATE(EMPLOYEE) Theta-Join: Natural Join: R1 * R2
![Page 4: From Relational Algebra to the Structured Query Language Rose-Hulman Institute of Technology Curt Clifton.](https://reader036.fdocuments.in/reader036/viewer/2022062309/56649d395503460f94a13c09/html5/thumbnails/4.jpg)
Why Relational Algebra? Foundational knowledge Used by query optimizers
Finer grained than SQL Can be formally reasoned about
Formal basis for semantics of SQL
![Page 5: From Relational Algebra to the Structured Query Language Rose-Hulman Institute of Technology Curt Clifton.](https://reader036.fdocuments.in/reader036/viewer/2022062309/56649d395503460f94a13c09/html5/thumbnails/5.jpg)
Homework Problem 6.18 Parts a–d and g Begin in class, may work in groups of 2–3
Please note your partners on the sheet
![Page 6: From Relational Algebra to the Structured Query Language Rose-Hulman Institute of Technology Curt Clifton.](https://reader036.fdocuments.in/reader036/viewer/2022062309/56649d395503460f94a13c09/html5/thumbnails/6.jpg)
Sets versus Bags Sets
Order doesn’t matter No duplicates
Examples {1,2,3} = {2,1,3} {1,2} {2} = {1,2}
Bags (or multi-sets) Order doesn’t matter Duplicates allowed
Examples {1,2,3} = {2,1,3} {1,2} {2} = {1,2,2} {1,2,3} ≠ {1,2,2,3}
![Page 7: From Relational Algebra to the Structured Query Language Rose-Hulman Institute of Technology Curt Clifton.](https://reader036.fdocuments.in/reader036/viewer/2022062309/56649d395503460f94a13c09/html5/thumbnails/7.jpg)
Why Bags? Efficiency! Eliminating duplicates can be expensive By default SQL uses bags
![Page 8: From Relational Algebra to the Structured Query Language Rose-Hulman Institute of Technology Curt Clifton.](https://reader036.fdocuments.in/reader036/viewer/2022062309/56649d395503460f94a13c09/html5/thumbnails/8.jpg)
Bag Union “Just dump all the elements into a single bag” An element appears in the union of two bags
the sum of the number of times it appears in each bag
![Page 9: From Relational Algebra to the Structured Query Language Rose-Hulman Institute of Technology Curt Clifton.](https://reader036.fdocuments.in/reader036/viewer/2022062309/56649d395503460f94a13c09/html5/thumbnails/9.jpg)
Bag Intersection “Whichever bag has the fewest, has the
answer” An element appears in the intersection of two
bags the minimum number of times it appears in either.
![Page 10: From Relational Algebra to the Structured Query Language Rose-Hulman Institute of Technology Curt Clifton.](https://reader036.fdocuments.in/reader036/viewer/2022062309/56649d395503460f94a13c09/html5/thumbnails/10.jpg)
Bag Difference “Take elements out of the first if they’re in
the second” An element appears in the difference of two
bags as many times as it appears in the first, minus the number of times it appears in the second, but no less than 0 times
![Page 11: From Relational Algebra to the Structured Query Language Rose-Hulman Institute of Technology Curt Clifton.](https://reader036.fdocuments.in/reader036/viewer/2022062309/56649d395503460f94a13c09/html5/thumbnails/11.jpg)
Bag Selection
A B
1 2
5 6
1 2
R
Like set selection But input and output
can be bags
![Page 12: From Relational Algebra to the Structured Query Language Rose-Hulman Institute of Technology Curt Clifton.](https://reader036.fdocuments.in/reader036/viewer/2022062309/56649d395503460f94a13c09/html5/thumbnails/12.jpg)
Bag Projection
A B
1 2
5 6
1 2
R
Unlike set project, can turn a set into a bag
![Page 13: From Relational Algebra to the Structured Query Language Rose-Hulman Institute of Technology Curt Clifton.](https://reader036.fdocuments.in/reader036/viewer/2022062309/56649d395503460f94a13c09/html5/thumbnails/13.jpg)
Bag Theta-Join
A B
1 2
5 6
1 2
R
Pair each tuple of first table with each tuple of second
Check condition Don’t eliminate
duplicates
B C
3 4
7 8
S
![Page 14: From Relational Algebra to the Structured Query Language Rose-Hulman Institute of Technology Curt Clifton.](https://reader036.fdocuments.in/reader036/viewer/2022062309/56649d395503460f94a13c09/html5/thumbnails/14.jpg)
Introducing SQL Pronounced:
“ess queue ell” Or “sequel”
Benefits: Designed for the relational model Easily optimized by DBMS Standard (well, sort of, a little bit, sometimes)
http://sqlfairy.sourceforge.net
![Page 15: From Relational Algebra to the Structured Query Language Rose-Hulman Institute of Technology Curt Clifton.](https://reader036.fdocuments.in/reader036/viewer/2022062309/56649d395503460f94a13c09/html5/thumbnails/15.jpg)
Running Example – The SodaBase Soda(name, manf) Rest(name, addr, contract) Customer(name, addr, phone) Likes(customer, soda) Sells(rest, soda, price) Frequents(customer, rest)
![Page 16: From Relational Algebra to the Structured Query Language Rose-Hulman Institute of Technology Curt Clifton.](https://reader036.fdocuments.in/reader036/viewer/2022062309/56649d395503460f94a13c09/html5/thumbnails/16.jpg)
The Basic SQL Query SELECT attributes
FROM tableWHERE condition
Semantics: attributes(condition(table))
![Page 17: From Relational Algebra to the Structured Query Language Rose-Hulman Institute of Technology Curt Clifton.](https://reader036.fdocuments.in/reader036/viewer/2022062309/56649d395503460f94a13c09/html5/thumbnails/17.jpg)
Example Find all the names of all sodas made by
PepsiCo
![Page 18: From Relational Algebra to the Structured Query Language Rose-Hulman Institute of Technology Curt Clifton.](https://reader036.fdocuments.in/reader036/viewer/2022062309/56649d395503460f94a13c09/html5/thumbnails/18.jpg)
Example Find all the names of all sodas made by
PepsiCo SELECT name
FROM Soda WHERE manf = 'PepsiCo'
(note single quotes)
![Page 19: From Relational Algebra to the Structured Query Language Rose-Hulman Institute of Technology Curt Clifton.](https://reader036.fdocuments.in/reader036/viewer/2022062309/56649d395503460f94a13c09/html5/thumbnails/19.jpg)
Select * For getting all attributes… SELECT *
FROM tableWHERE condition
Semantics: condition(table)
![Page 20: From Relational Algebra to the Structured Query Language Rose-Hulman Institute of Technology Curt Clifton.](https://reader036.fdocuments.in/reader036/viewer/2022062309/56649d395503460f94a13c09/html5/thumbnails/20.jpg)
Example SELECT *
FROM Soda WHERE manf = 'PepsiCo'
![Page 21: From Relational Algebra to the Structured Query Language Rose-Hulman Institute of Technology Curt Clifton.](https://reader036.fdocuments.in/reader036/viewer/2022062309/56649d395503460f94a13c09/html5/thumbnails/21.jpg)
Renaming Attributes SELECT attribute1 AS newName1, …
FROM tableWHERE condition
Semantics: newName1, …(attribute1, …(condition(table)))
![Page 22: From Relational Algebra to the Structured Query Language Rose-Hulman Institute of Technology Curt Clifton.](https://reader036.fdocuments.in/reader036/viewer/2022062309/56649d395503460f94a13c09/html5/thumbnails/22.jpg)
Example SELECT name AS soda, manf AS maker
FROM SodaWHERE manf = 'PepsiCo'
![Page 23: From Relational Algebra to the Structured Query Language Rose-Hulman Institute of Technology Curt Clifton.](https://reader036.fdocuments.in/reader036/viewer/2022062309/56649d395503460f94a13c09/html5/thumbnails/23.jpg)
Expressions in SELECT Clauses Can use expressions on attributes in SELECT SELECT f(attributes), …
FROM tableWHERE condition
More powerful than the relational algebra we’ve seen Would need functions on tuples
![Page 24: From Relational Algebra to the Structured Query Language Rose-Hulman Institute of Technology Curt Clifton.](https://reader036.fdocuments.in/reader036/viewer/2022062309/56649d395503460f94a13c09/html5/thumbnails/24.jpg)
Example Show selling prices in Yen
![Page 25: From Relational Algebra to the Structured Query Language Rose-Hulman Institute of Technology Curt Clifton.](https://reader036.fdocuments.in/reader036/viewer/2022062309/56649d395503460f94a13c09/html5/thumbnails/25.jpg)
Example Show selling prices in Yen SELECT rest, soda,
price * 115 AS priceInYen FROM Sells
![Page 26: From Relational Algebra to the Structured Query Language Rose-Hulman Institute of Technology Curt Clifton.](https://reader036.fdocuments.in/reader036/viewer/2022062309/56649d395503460f94a13c09/html5/thumbnails/26.jpg)
Another Example: Constants SELECT customer,
'likes Pepsi' AS promotionFROM LikesWHERE soda = 'Pepsi'
![Page 27: From Relational Algebra to the Structured Query Language Rose-Hulman Institute of Technology Curt Clifton.](https://reader036.fdocuments.in/reader036/viewer/2022062309/56649d395503460f94a13c09/html5/thumbnails/27.jpg)
Example: Complex Conditions Find the price that Joe’s Sushi charges for
Pepsi
![Page 28: From Relational Algebra to the Structured Query Language Rose-Hulman Institute of Technology Curt Clifton.](https://reader036.fdocuments.in/reader036/viewer/2022062309/56649d395503460f94a13c09/html5/thumbnails/28.jpg)
Example: Complex Conditions Find the price that Joe’s Sushi charges for Pepsi SELECT price
FROM Sells WHERE rest = 'Joe''s Sushi'
AND soda = 'Pepsi' Note:
Double apostrophe inside string AND, OR, NOT Case insensitive
![Page 29: From Relational Algebra to the Structured Query Language Rose-Hulman Institute of Technology Curt Clifton.](https://reader036.fdocuments.in/reader036/viewer/2022062309/56649d395503460f94a13c09/html5/thumbnails/29.jpg)
Pattern Matching WHERE clauses can compare string to
pattern Attribute LIKE pattern Attribute NOT LIKE pattern
Pattern syntax: Pattern is a string % in string represents any number of characters _ in string represent any single character
![Page 30: From Relational Algebra to the Structured Query Language Rose-Hulman Institute of Technology Curt Clifton.](https://reader036.fdocuments.in/reader036/viewer/2022062309/56649d395503460f94a13c09/html5/thumbnails/30.jpg)
Example Find the customers with exchange 555,
regardless of area code
![Page 31: From Relational Algebra to the Structured Query Language Rose-Hulman Institute of Technology Curt Clifton.](https://reader036.fdocuments.in/reader036/viewer/2022062309/56649d395503460f94a13c09/html5/thumbnails/31.jpg)
Example Find the customers with exchange 555,
regardless of area code SELECT name
FROM CustomerWHERE phone LIKE '%555-____'
-- That’s four underscores
![Page 32: From Relational Algebra to the Structured Query Language Rose-Hulman Institute of Technology Curt Clifton.](https://reader036.fdocuments.in/reader036/viewer/2022062309/56649d395503460f94a13c09/html5/thumbnails/32.jpg)
Dealing with Null Why might a tuple have a null value? SQL uses three-valued logic to handle null
A boolean expression can be true, false, or unknown
Comparison with null yields unknown instead of error
WHERE clause must be true to match
![Page 33: From Relational Algebra to the Structured Query Language Rose-Hulman Institute of Technology Curt Clifton.](https://reader036.fdocuments.in/reader036/viewer/2022062309/56649d395503460f94a13c09/html5/thumbnails/33.jpg)
Three-Valued Logic True = 1 False = 0 Unknown = 1/2 x AND y = min(x, y) x OR y = max(x, y) NOT x = 1 - x
![Page 34: From Relational Algebra to the Structured Query Language Rose-Hulman Institute of Technology Curt Clifton.](https://reader036.fdocuments.in/reader036/viewer/2022062309/56649d395503460f94a13c09/html5/thumbnails/34.jpg)
Consider SELECT rest
FROM SellsWHERE price < 2.00 OR price >= 2.00
If the Sells relation has the value:
Beware of nulls!
rest soda price
Joe's Pepsi null
![Page 35: From Relational Algebra to the Structured Query Language Rose-Hulman Institute of Technology Curt Clifton.](https://reader036.fdocuments.in/reader036/viewer/2022062309/56649d395503460f94a13c09/html5/thumbnails/35.jpg)
Combining Relations List multiple tables in FROM Use Relation.Attribute to distinguish SELECT soda
FROM Likes, FrequentsWHERE Frequents.customer = Likes.customer
AND rest = 'Joe''s' Semantics:
![Page 36: From Relational Algebra to the Structured Query Language Rose-Hulman Institute of Technology Curt Clifton.](https://reader036.fdocuments.in/reader036/viewer/2022062309/56649d395503460f94a13c09/html5/thumbnails/36.jpg)
Tuple Variables Can distinguish two copies of same relation Example: Find all pairs of sodas by the same
manufacturer… Omitting trivial pairs like (Pepsi, Pepsi) Omitting permutations of same sodas by listing
members of pair alphabetically
![Page 37: From Relational Algebra to the Structured Query Language Rose-Hulman Institute of Technology Curt Clifton.](https://reader036.fdocuments.in/reader036/viewer/2022062309/56649d395503460f94a13c09/html5/thumbnails/37.jpg)
Solution SELECT s1.name, s2.name
FROM Soda s1, Soda s2WHERE s1.manf = s2.manf
AND s1.name < s2.name
![Page 38: From Relational Algebra to the Structured Query Language Rose-Hulman Institute of Technology Curt Clifton.](https://reader036.fdocuments.in/reader036/viewer/2022062309/56649d395503460f94a13c09/html5/thumbnails/38.jpg)
The Story Thus Far SELECT … FROM … WHERE SELECT * … SELECT Foo AS Bar … SELECT expression … SELECT … FROM … WHERE … LIKE … SELECT … FROM Foo, Bar … SELECT … FROM Foo f1, Foo f2 …