CSE 344: Section 5
BCNF, Relational Algebra
October 26th, 2023
Announcements
Announcements
Boyce-Codd Normal Form (BCNF)
Motivation of BCNF
Recall the three anomalies:
If a relation is in BCNF, then it does not have these anomalies
If it is not in BCNF, then it does have these anomalies
BCNF Algorithm
Example: BCNF Decomposition
Restaurant(id,name,rating,popularity,rec)
SET rec? = ‘Y’
WHERE popularity = ‘Respectable’;
id | name | rating | popularity | rec? |
1 | Mee Sum Pastry | 3 | Okay | N |
2 | Café on the Ave | 4 | Respectable | N |
3 | Guanaco’s Tacos | 4 | Respectable | N |
4 | Aladdin Gyro-Cery | 5 | Poppin | Y |
Restaurant
Example: BCNF Decomposition
Restaurant(id,name,rating,popularity,rec)
rating+ = rating, popularity, rec?
id | name | rating | popularity | rec? |
1 | Mee Sum Pastry | 3 | Okay | N |
2 | Café on the Ave | 4 | Respectable | N |
3 | Guanaco’s Tacos | 4 | Respectable | N |
4 | Aladdin Gyro-Cery | 5 | Poppin | Y |
Restaurant
Example: BCNF Decomposition
id | name | rating | popularity | rec? |
1 | Mee Sum Pastry | 3 | Okay | N |
2 | Café on the Ave | 4 | Respectable | N |
3 | Guanaco’s Tacos | 4 | Respectable | N |
4 | Aladdin Gyro-Cery | 5 | Poppin | Y |
Restaurant(id,name,rating,popularity,rec)
Restaurant
rating+ = rating, popularity, rec
R1(rating,popularity,rec?)
R2(rating,id,name)
rating | popularity | rec? |
3 | Okay | N |
4 | Respectable | N |
5 | Poppin | Y |
id | name | rating |
1 | Mee Sum Pastry | 3 |
2 | Café on the Ave | 4 |
3 | Guanaco’s Tacos | 4 |
4 | Aladdin Gyro-Cery | 5 |
R2
R1
Example: BCNF Decomposition
id | name | rating | popularity | rec? |
1 | Mee Sum Pastry | 3 | Okay | N |
2 | Café on the Ave | 4 | Respectable | N |
3 | Guanaco’s Tacos | 4 | Respectable | N |
4 | Aladdin Gyro-Cery | 5 | Poppin | Y |
Restaurant(id,name,rating,popularity,rec)
Restaurant
rating+ = rating, popularity, rec
R1(rating,popularity,rec?)
R2(rating,id,name)
popularity+ = popularity, rec?
id | name | rating |
1 | Mee Sum Pastry | 3 |
2 | Café on the Ave | 4 |
3 | Guanaco’s Tacos | 4 |
4 | Aladdin Gyro-Cery | 5 |
R2
Example: BCNF Decomposition
id | name | rating | popularity | rec? |
1 | Mee Sum Pastry | 3 | Okay | N |
2 | Café on the Ave | 4 | Respectable | N |
3 | Guanaco’s Tacos | 4 | Respectable | N |
4 | Aladdin Gyro-Cery | 5 | Poppin | Y |
Restaurant(id,name,rating,popularity,rec)
Restaurant
rating+ = rating, popularity, rec
R1(rating,popularity,rec?)
R2(rating,id,name)
popularity+ = popularity, rec?
id | name | rating |
1 | Mee Sum Pastry | 3 |
2 | Café on the Ave | 4 |
3 | Guanaco’s Tacos | 4 |
4 | Aladdin Gyro-Cery | 5 |
R2
R4(popularity,rating)
R3(popularity,rec?)
Example: BCNF Decomposition
id | name | rating | popularity | rec? |
1 | Mee Sum Pastry | 3 | Okay | N |
2 | Café on the Ave | 4 | Respectable | N |
3 | Guanaco’s Tacos | 4 | Respectable | N |
4 | Aladdin Gyro-Cery | 5 | Poppin | Y |
Restaurant(id,name,rating,popularity,rec)
Restaurant
rating+ = rating, popularity, rec
R1(rating,popularity,rec?)
R2(rating,id,name)
popularity+ = popularity, rec?
id | name | rating |
1 | Mee Sum Pastry | 3 |
2 | Café on the Ave | 4 |
3 | Guanaco’s Tacos | 4 |
4 | Aladdin Gyro-Cery | 5 |
R2
R4(popularity,rating)
R3(popularity,rec?)
popularity | rec? |
Okay | N |
Respectable | N |
Poppin | Y |
R3
rating | popularity |
3 | Okay |
4 | Respectable |
5 | Poppin |
R4
Example: BCNF Decomposition
id | name | rating | popularity | rec? |
1 | Mee Sum Pastry | 3 | Okay | N |
2 | Café on the Ave | 4 | Respectable | N |
3 | Guanaco’s Tacos | 4 | Respectable | N |
4 | Aladdin Gyro-Cery | 5 | Poppin | Y |
Restaurant(id,name,rating,popularity,rec)
Restaurant
rating+ = rating, popularity, rec
R1(rating,popularity,rec?)
R2(rating,id,name)
popularity+ = popularity, rec?
id | name | rating |
1 | Mee Sum Pastry | 3 |
2 | Café on the Ave | 4 |
3 | Guanaco’s Tacos | 4 |
4 | Aladdin Gyro-Cery | 5 |
R2
R4(popularity,rating)
R3(popularity,rec?)
popularity | rec? |
Okay | N |
Respectable | N |
Poppin | Y |
R3
rating | popularity |
3 | Okay |
4 | Respectable |
5 | Poppin |
R4
R2, R3, R4 is the normalized database schema. No more anomalies!
Example: BCNF Decomposition
Now when we update the rec? so that a popularity = ‘Respectable’ has rec?= ‘Y’
UPDATE R3 SET rec? = ‘Y’ WHERE popularity = ‘Respectable’;
id | name | rating |
1 | Mee Sum Pastry | 3 |
2 | Café on the Ave | 4 |
3 | Guanaco’s Tacos | 4 |
4 | Aladdin Gyro-Cery | 5 |
R2
R3
R4
popularity | rec? |
Okay | N |
Respectable | N |
Poppin | Y |
rating | popularity |
3 | Okay |
4 | Respectable |
5 | Poppin |
Relational Algebra
RA Operators
Standard:
⋂ - Intersect
⋃ - Union
⎼ - Difference
σ - Select
π - Project
⍴ - Rename
Extended:
δ - Duplicate Elim.
ɣ - Group/Agg.
τ - Sorting
Joins:
⨝ - Nat. Join
⟕ - L.O. Join
⟖ - R.O. Join
⟗ - F.O. Join
✕- Cross Product
15
Ɣ Notation
Grouping and aggregation on group:
ɣattr_1, …, attr_k, count/sum/max/min(attr) -> alias
Aggregation on the entire table:
ɣcount/sum/max/min(attr) -> alias
16
Format
17
Query Plans (Example SQL -> RA)
Select-Join-Project structure
Make this SQL query into RA (remember FWGHOS):
SELECT R.b, T.c, max(T.a) AS T_max
FROM Table_R AS R, Table_T AS T
WHERE R.b = T.b
GROUP BY R.b, T.c
HAVING max(T.a) > 99
18
Query Plans (Example SQL -> RA)
Select-Join-Project structure
Make this SQL query into RA (remember FWGHOS):
SELECT R.b, T.c, max(T.a) AS T_max
FROM Table_R AS R, Table_T AS T
WHERE R.b = T.b
GROUP BY R.b, T.c
HAVING max(T.a) > 99
19
Difference Operator
SELECT DISTINCT R.a
FROM Table_R AS R
WHERE NOT EXISTS (
SELECT *
FROM Table_S AS S
WHERE S.b = R.a
AND S.c < 15
);
20
Difference Operator
SELECT DISTINCT R.a
FROM Table_R AS R
WHERE NOT EXISTS (
SELECT *
FROM Table_S AS S
WHERE S.b = R.a
AND S.c < 15);
21
πR2.a
Difference Operator
SELECT DISTINCT R.a
FROM Table_R AS R
WHERE NOT EXISTS (
SELECT *
FROM Table_S AS S
WHERE S.b = R.a
AND S.c < 15);
Equivalent SQL:
SELECT DISTINCT * FROM Table_R R2
EXCEPT
SELECT R1.a FROM Table_R R1, Table_S S
WHERE S.c < 15 AND R1.a = S.b;
22
πR2.a
SQL TO RA
General Approach
24
Subqueries in FROM to RA
Relational Algebra
84
Subqueries in FROM to RA
Relational Algebra
85
Subqueries in FROM to RA
Relational Algebra
86
Subqueries in FROM to RA
Relational Algebra
87
Subqueries in FROM to RA
Relational Algebra
88
Subqueries in WHERE/HAVING
Relational Algebra
99
Subqueries in WHERE/HAVING
Relational Algebra
100
Subqueries in WHERE/HAVING
Relational Algebra
101
Subqueries in WHERE/HAVING
Relational Algebra
100
Subqueries in WHERE/HAVING
Relational Algebra
100
Query Optimizations
Rewrite Rules
RA to RA: WHERE
Query Optimization
37
=
Idempotency
RA to RA: SELECT
Query Optimization
38
=
=
Idempotency
RA to RA: WHERE-AND
Query Optimization
39
=
=
Commutativity
RA to RA: WHERE-OR
Query Optimization
40
=
set
set
RA to RA: SELECT-WHERE
Query Optimization
41
=
“push down”
Conditional* Commutativity�* if c only references attributes in A
RA to RA: WHERE-HAVING
Query Optimization
42
Schema(G,A)
Schema(G,A)
=
“push down”
Conditional* Commutativity�* if c only references attributes in G
Conditional Commutativity
Query Optimization
43
Attribute P.Job not in {P.UserID, P.Name}
RA to RA: WHERE-JOIN
Query Optimization
44
=
Distributive
Example Query Optimization
Query Optimization
45
Supplier x
Supply y
Push Selections Down��σC1 and C2(R ⋈ S)
= σC1(σC2(R ⋈ S))
= σC1(R ⋈ σC2(S))
= σC1(R) ⋈ σC2(S)
Supplier x
Supply y
Supplier(sid, name, city, state)
Supply(sid, pid, quantity)
Worksheet
Cardinality Estimation: Factors
T(R) = # tuples for relation R
V(R, a) = # of unique values of attribute a in relation R
Selectivity Formulas
Cardinality Estimation Example
Cardinality Estimation
Supply
σpno=2
Supplier
σscity=’Seattle’ Λ sstate=’WA’
πsname
SELECT sname
FROM Supply x, Supplier y
WHERE x.sid=y.sid AND x.pno=2 AND y.scity=’Seattle’ AND y.sstate=’WA’;
Supply(sid, pno, quantity)
Supplier(sid, sname, scity, sstate)
⋈sid=sid
Cardinality Estimation
Supply
σpno=2
Supplier
σscity=’Seattle’ Λ sstate=’WA’
πsname
Supply Statistics:
Supplier Statistics:
⋈sid=sid
Cardinality Estimation
Supply Statistics:
Supplier Statistics:
T(Supply) * 1 / V(Supply, pno)
= 4
T(Supplier) * 1 / V(Supplier, scity) * 1 / V(Supplier, sstate)
= 5
Supply
σpno=2
Supplier
σscity=’Seattle’ Λ sstate=’WA’
πsname
⋈sid=sid
Cardinality Estimation
Supply Statistics:
Supplier Statistics:
T1 = 4
T2 = 5
Supply
σpno=2
Supplier
σscity=’Seattle’ Λ sstate=’WA’
πsname
⋈sid=sid
Cardinality Estimation
⋈supplier_id=sid
Supply Statistics:
Supplier Statistics:
T1 = 4
T2 = 50
But wait a second…. Seattle is in Washington!
Supply
σpno=2
Supplier
σscity=’Seattle’ Λ sstate=’WA’
πsname
Cardinality Estimation
⋈supplier_id=sid
Supply Statistics:
Supplier Statistics:
T1 = 4
T2 = 50
(4 * 50) /
max(
V(Supplier, sid),
V(Supply, sid)
)
Supply
σpno=2
Supplier
σscity=’Seattle’ Λ sstate=’WA’
πsname
Cardinality Estimation
⋈supplier_id=sid
Supply Statistics:
Supplier Statistics:
T1 = 4
T2 = 50
(4 * 50) / 1000
Supply
σpno=2
Supplier
σscity=’Seattle’ Λ sstate=’WA’
πsname
Cardinality Estimation
⋈supplier_id=sid
Supply Statistics:
Supplier Statistics:
T1 = 4
T2 = 50
T3 = 0.2
Supply
σpno=2
Supplier
σscity=’Seattle’ Λ sstate=’WA’
πsname
Cardinality Estimation
⋈supplier_id=sid
Supply Statistics:
Supplier Statistics:
T1 = 4
T2 = 50
T3 = 0.2
No filtering at this step
Supply
σpno=2
Supplier
σscity=’Seattle’ Λ sstate=’WA’
πsname
Cardinality Estimation
⋈supplier_id=sid
Supply Statistics:
Supplier Statistics:
T1 = 4
T2 = 50
T3 = 0.2
Total ≅ 0.2
Supply
σpno=2
Supplier
σscity=’Seattle’ Λ sstate=’WA’
πsname