Relational Algebra Maybe - SQL
Relational Algebra Maybe - SQL
Relational Algebra Maybe - SQL
Maybe -- SQL
Confused by Normal Forms ?
3NF
BCNF
4NF
Dependents
SSN Dname
999999999 Emily
777777777 Joe
Employee Dependents =
Name, SSN, Dname( SSN=SSN2(Employee x SSN2, Dname(Dependents))
Name SSN Dname
John 999999999 Emily
Tony 777777777 Joe
Natural Join
• R= A B S= B C
X Y Z U
X Z V W
Y Z Z V
Z V
A B C
• R S= X Z U
X Z V
Y Z U
Y Z V
Z V W
Natural Join
• Given the schemas R(A, B, C, D), S(A, C, E),
what is the schema of R S ?
R1 R2
• Natural join is a particular case of equi-join
• A lot of research on how to do it efficiently
Semi-join
• R S = A1,…,An (R S)
• Where the schemas are:
– Input: R(A1,…An), S(B1,…,Bm)
– Output: T(A1,…,An)
Semi-join
Applications in distributed databases:
• Product(pid, cid, pname, ...) at site 1
• Company(cid, cname, ...) at site 2
• Query: price>1000(Product) cid=cid Company
• Compute as follows:
T1 = price>1000(Product) site 1
T2 = Pcid(T1) site 1
send T2 to site 2 (T2 smaller than T1)
T3 = T2 Company site 2 (semijoin)
send T3 to site 1 (T3 smaller than Company)
Answer = T3 T1 site 1 (semijoin)
Relational Algebra
• Five basic operators, many derived
• Combine operators in order to construct
queries: relational algebra expressions,
usually shown as trees
Complex Queries
Product ( pid, name, price, category, maker-cid)
Purchase (buyer-ssn, seller-ssn, store, pid)
Company (cid, name, stock price, country)
Person(ssn, name, phone number, city)
Note:
•in Purchase: buyer-ssn, seller-ssn are foreign keys in Person, pid is foreign key in
Product;
•in Product maker-cid is a foreign key in Company
• Which is better:
price>100(Product) (Purchase city=seaPerson)
price>100(Product) Purchase) city=seaPerson
• Depends ! This is the optimizer’s job…
Finally: RA has Limitations !
• Cannot compute “transitive closure”
Name1 Name2 Relationship
Select
Select attributes
attributes
From
From relations
relations(possibly
(possiblymultiple,
multiple,joined)
joined)
Where
Where conditions
conditions(selections)
(selections)
Selections
Company(sticker, name, country, stockPrice)
SELECT
SELECT **
FROM
FROM Company
Company
WHERE
WHERE country=“USA”
country=“USA”AND
ANDstockPrice
stockPrice>>50
50
SELECT
SELECT **
FROM
FROM Company
Company
WHERE
WHERE country=“USA”
country=“USA”AND
AND
address
addressLIKE
LIKE“%Mountain%”
“%Mountain%”
Projections
Select only a subset of the attributes
SELECT
SELECT name,
name,stockPrice
stockPrice
FROM
FROM Company
Company
WHERE
WHERE country=“USA”
country=“USA”AND ANDstockPrice
stockPrice>>50
50
SELECT
SELECT name
nameAS
AScompany,
company,stockprice
stockpriceAS
ASprice
price
FROM
FROM Company
Company
WHERE
WHERE country=“USA”
country=“USA”AND
ANDstockPrice
stockPrice>>50
50
Ties are broken by the second attribute on the ORDERBY list, etc.
Joins
Product (pname, price, category, maker)
Purchase (buyer, seller, store, product)
Company (cname, stockPrice, country)
Person(pname, phoneNumber, city)
SELECT
SELECT pname,
pname,store
store
FROM
FROM Person,
Person,Purchase
Purchase
WHERE
WHERE pname=buyer
pname=buyerAND
ANDcity=“Seattle”
city=“Seattle”
AND
ANDproduct=“gizmo”
product=“gizmo”
Disambiguating Attributes
Find names of people buying telephony products:
SELECT
SELECT Person.name
Person.name
FROM
FROM Person,
Person,Purchase,
Purchase,Product
Product
WHERE
WHERE Person.name=Purchase.buyer
Person.name=Purchase.buyer
AND
AND Product=Product.name
Product=Product.name
AND
AND Product.category=“telephony”
Product.category=“telephony”
Tuple Variables
Find pairs of companies making products in the same category
SELECT
SELECT product1.maker,
product1.maker,product2.maker
product2.maker
FROM
FROM Product
ProductAS
ASproduct1,
product1,Product
ProductAS
ASproduct2
product2
WHERE
WHERE product1.category=product2.category
product1.category=product2.category
AND
AND product1.maker
product1.maker<>
<>product2.maker
product2.maker
SELECT
SELECTProduct.name
Product.name
FROM
FROM Product
ProductAS
ASProduct
Product
WHERE
WHEREProduct.price
Product.price>>100
100
1. Nested loops:
Answer
Answer=={} {}
for
forx1
x1in
inR1
R1dodo
for
forx2
x2in
inR2
R2do do
…..
…..
for
forxn
xnin inRnRndo
do
ififConditions
Conditions
then
thenAnswer
Answer==Answer
AnswerUU{(a1,…,ak)
{(a1,…,ak)
return
returnAnswer
Answer
Meaning (Semantics) of SQL
Queries
SELECT a1, a2, …, ak
FROM R1 AS x1, R2 AS x2, …, Rn AS xn
WHERE Conditions
2. Parallel assignment
Answer
Answer=={} {}
for
forall
allassignments
assignmentsx1
x1in
inR1,
R1,…,
…,xn
xnin
inRn
Rndo
do
ififConditions
Conditionsthen
thenAnswer
Answer==Answer
AnswerUU{(a1,…,ak)}
{(a1,…,ak)}
return
returnAnswer
Answer
Answer(a ,…,akk))
Answer(a11,…,a RR11(x
(x1111,…,x
,…,x1p1p),…,R
),…,Rnn(x
(xn1n1,…,x
,…,xnpnp),),Conditions
Conditions
Meaning (Semantics) of SQL
Queries
SELECT a1, a2, …, ak
FROM R1 AS x1, R2 AS x2, …, Rn AS xn
WHERE Conditions
Looking for R (S T)
UNION
(SELECT name
FROM Person, Purchase
WHERE buyer=name AND store=“The Bon”)