Algebra1 After this lecture, you should be able to: Understand the differences between SQL...
-
Upload
adrian-stevens -
Category
Documents
-
view
213 -
download
0
Transcript of Algebra1 After this lecture, you should be able to: Understand the differences between SQL...
![Page 1: Algebra1 After this lecture, you should be able to: Understand the differences between SQL (Structured Query Language) and Relational Algebra expressions.](https://reader035.fdocuments.us/reader035/viewer/2022070402/56649f275503460f94c3e4f6/html5/thumbnails/1.jpg)
Algebra 1
After this lecture, you should be able to: Understand the differences between SQL
(Structured Query Language) and Relational Algebra expressions.
Build queries in Relational Algebra. Understand how DBMS’s process the SQL
queries. Make simple query optimization. Be ready for Quiz 4 and Complete
Assignment 7 (Part I).
Relational Algebra
![Page 2: Algebra1 After this lecture, you should be able to: Understand the differences between SQL (Structured Query Language) and Relational Algebra expressions.](https://reader035.fdocuments.us/reader035/viewer/2022070402/56649f275503460f94c3e4f6/html5/thumbnails/2.jpg)
Algebra 2
Relational Algebra
Relational algebra deals with a set of relations closed under operators. Operators operate on one or more relations to yield a relation.
Algebraic Expressions(a + b) * c(A B) C
Relational algebra is a basis for database query languages.
![Page 3: Algebra1 After this lecture, you should be able to: Understand the differences between SQL (Structured Query Language) and Relational Algebra expressions.](https://reader035.fdocuments.us/reader035/viewer/2022070402/56649f275503460f94c3e4f6/html5/thumbnails/3.jpg)
Algebra 3
Characteristics of Relational Algebra
Operational The order of operations can be specified.
Set-at-a-time Multiple rows are processed by one
operation. Not too detailed
How to access data is not specified. Good for query optimization.
Who supplied P2? SQL
Select SName from S, SPwhere S.S# = SP.S# and P# = 'P2' ;
Relational Algebra
SName (S ⋈ (p#=‘P2‘ SP ))
![Page 4: Algebra1 After this lecture, you should be able to: Understand the differences between SQL (Structured Query Language) and Relational Algebra expressions.](https://reader035.fdocuments.us/reader035/viewer/2022070402/56649f275503460f94c3e4f6/html5/thumbnails/4.jpg)
Algebra 4
Relational algebra …
Union: A B Intersection: A B Difference: A B B Cartesian Product: S × SP Division: A / B Selection: city=‘Rome‘ S
Projection: SName,Status S Join: S ⋈ SP Renaming ρ(citypcity)
P
![Page 5: Algebra1 After this lecture, you should be able to: Understand the differences between SQL (Structured Query Language) and Relational Algebra expressions.](https://reader035.fdocuments.us/reader035/viewer/2022070402/56649f275503460f94c3e4f6/html5/thumbnails/5.jpg)
Algebra 5
Terms: Relational algebra ~ Database
Relation ~ Table Attribute ~ Column Tuple ~ Row (Record) Relation schema ~ Structure of table Relation Instance ~ Data of table
![Page 6: Algebra1 After this lecture, you should be able to: Understand the differences between SQL (Structured Query Language) and Relational Algebra expressions.](https://reader035.fdocuments.us/reader035/viewer/2022070402/56649f275503460f94c3e4f6/html5/thumbnails/6.jpg)
Algebra 6
Union and Intersection
A B
a 1
b 1
A B
a 2
b 1
c 2
A B
a 1
a 2
b 1
c 2
A B
b 1
P Q
P QP Q
![Page 7: Algebra1 After this lecture, you should be able to: Understand the differences between SQL (Structured Query Language) and Relational Algebra expressions.](https://reader035.fdocuments.us/reader035/viewer/2022070402/56649f275503460f94c3e4f6/html5/thumbnails/7.jpg)
Algebra 7
Union and Intersection: Exercise
A B
a 1
b 1
A B
a 1
c 1
A B
a 1
b 1
c 1
A B
a 1
P Q
P Q = ?
P Q = ?
![Page 8: Algebra1 After this lecture, you should be able to: Understand the differences between SQL (Structured Query Language) and Relational Algebra expressions.](https://reader035.fdocuments.us/reader035/viewer/2022070402/56649f275503460f94c3e4f6/html5/thumbnails/8.jpg)
Algebra 8
Difference (-)
A Ba 1
b 1
A Ba 2
b 1
c 2
A B
a 1
P Q
P B Q
![Page 9: Algebra1 After this lecture, you should be able to: Understand the differences between SQL (Structured Query Language) and Relational Algebra expressions.](https://reader035.fdocuments.us/reader035/viewer/2022070402/56649f275503460f94c3e4f6/html5/thumbnails/9.jpg)
Algebra 9
Difference: Exercise
A B
a 1
b 1
c 1
A B
a 1
c 1
d 1
A B
b 1
P Q
P ̶ Q = ?
![Page 10: Algebra1 After this lecture, you should be able to: Understand the differences between SQL (Structured Query Language) and Relational Algebra expressions.](https://reader035.fdocuments.us/reader035/viewer/2022070402/56649f275503460f94c3e4f6/html5/thumbnails/10.jpg)
Algebra 10
Cartesian Product (×)
A B
a 1
b 1
C D
u 2
v 2
w 2
P Q
A B C D
a 1 u 2
a 1 v 2
a 1 w 2
b 1 u 2
b 1 v 2
b 1 w 2
P × Q = {(p, q)|p P and q Q}
![Page 11: Algebra1 After this lecture, you should be able to: Understand the differences between SQL (Structured Query Language) and Relational Algebra expressions.](https://reader035.fdocuments.us/reader035/viewer/2022070402/56649f275503460f94c3e4f6/html5/thumbnails/11.jpg)
Algebra 11
Cartesian Product: Exercise
A
a
b
C D
u 2
v 2
P Q
P × Q = ?
A C D
a u 2
a v 2
b u 2
b v 2
![Page 12: Algebra1 After this lecture, you should be able to: Understand the differences between SQL (Structured Query Language) and Relational Algebra expressions.](https://reader035.fdocuments.us/reader035/viewer/2022070402/56649f275503460f94c3e4f6/html5/thumbnails/12.jpg)
Algebra 12
Selection ()
A B C
a 1 u
a 2 u
b 2 v
A B C
a 2 u
b 2 v
R
B = 2 R
![Page 13: Algebra1 After this lecture, you should be able to: Understand the differences between SQL (Structured Query Language) and Relational Algebra expressions.](https://reader035.fdocuments.us/reader035/viewer/2022070402/56649f275503460f94c3e4f6/html5/thumbnails/13.jpg)
Algebra 13
Get all the information of the suppliers located in Paris.
select *from swhere city = ‘Paris’;
Table S sno | sname | status | city----------------------------- s1 | Smith | 20 | London s2 | Jones | 10 | Paris s3 | Blake | 30 | Paris s4 | Clark | 20 | London s5 | Adams | 30 | Athens
city = ‘Paris‘ S
![Page 14: Algebra1 After this lecture, you should be able to: Understand the differences between SQL (Structured Query Language) and Relational Algebra expressions.](https://reader035.fdocuments.us/reader035/viewer/2022070402/56649f275503460f94c3e4f6/html5/thumbnails/14.jpg)
Algebra 14
Projection ()
A B C
a 1 u
a 2 u
b 2 v
C A
u a
v b
R
C, A R
![Page 15: Algebra1 After this lecture, you should be able to: Understand the differences between SQL (Structured Query Language) and Relational Algebra expressions.](https://reader035.fdocuments.us/reader035/viewer/2022070402/56649f275503460f94c3e4f6/html5/thumbnails/15.jpg)
Algebra 15
What are the S#’s and statuses of the suppliers?
sno | status ------------ s1 | 20 s2 | 10 s3 | 30
select sno, statusfrom s;
Table S sno | sname | status | city----------------------------- s1 | Smith | 20 | London s2 | Jones | 10 | Paris s3 | Blake | 30 | Paris
sno, status S
![Page 16: Algebra1 After this lecture, you should be able to: Understand the differences between SQL (Structured Query Language) and Relational Algebra expressions.](https://reader035.fdocuments.us/reader035/viewer/2022070402/56649f275503460f94c3e4f6/html5/thumbnails/16.jpg)
Algebra 16
What are the S#’s and statuses of the suppliers located in Paris?
sno | status ------------ s2 | 10 s3 | 30
select sno, statusfrom swhere city = 'Paris;
Table S sno | sname | status | city----------------------------- s1 | Smith | 20 | London s2 | Jones | 10 | Paris s3 | Blake | 30 | Paris s4 | Clark | 20 | London s5 | Adams | 30 | Athens
sno, status (city = ‘Paris‘ S)
![Page 17: Algebra1 After this lecture, you should be able to: Understand the differences between SQL (Structured Query Language) and Relational Algebra expressions.](https://reader035.fdocuments.us/reader035/viewer/2022070402/56649f275503460f94c3e4f6/html5/thumbnails/17.jpg)
Algebra 17
Selection and Projection: Exercise
pno pname color weight city ---- ------ ------- ------ ------ p1 nut red 12 London p2 bolt green 15 Paris p3 screw green 17 Rome
select pname, weightfrom pwhere color = ‘green’;
pname weight ----- ------ bolt 15 screw 17
Get the names and weights of the green parts.
pname, weight (color = ‘green‘ P)
![Page 18: Algebra1 After this lecture, you should be able to: Understand the differences between SQL (Structured Query Language) and Relational Algebra expressions.](https://reader035.fdocuments.us/reader035/viewer/2022070402/56649f275503460f94c3e4f6/html5/thumbnails/18.jpg)
Algebra 18
Join
A1 B1
a 1
b 2
B1 ≥ B2 (P × Q)
A1 B1 A2 B2
a 1 c 1
b 2 a 2
b 2 b 2
b 2 c 1
P
A2 B2
a 2
b 2
c 1
Q
![Page 19: Algebra1 After this lecture, you should be able to: Understand the differences between SQL (Structured Query Language) and Relational Algebra expressions.](https://reader035.fdocuments.us/reader035/viewer/2022070402/56649f275503460f94c3e4f6/html5/thumbnails/19.jpg)
Algebra 19
Equijoin
A1 B1
a 1
b 2
A1 B1 A2 B2
a 1 c 1
b 2 a 2
b 2 b 2
PA2 B2
a 2
b 2
c 1
Q
B1 = B2 (P × Q)
![Page 20: Algebra1 After this lecture, you should be able to: Understand the differences between SQL (Structured Query Language) and Relational Algebra expressions.](https://reader035.fdocuments.us/reader035/viewer/2022070402/56649f275503460f94c3e4f6/html5/thumbnails/20.jpg)
Algebra 20
Natural Join (⋈ )
A1 B
a 1
b 2
P
A2 B
a 2
b 2
c 1
Q
P ⋈ Q
A1 B A2
a 1 c
b 2 a
b 2 b
![Page 21: Algebra1 After this lecture, you should be able to: Understand the differences between SQL (Structured Query Language) and Relational Algebra expressions.](https://reader035.fdocuments.us/reader035/viewer/2022070402/56649f275503460f94c3e4f6/html5/thumbnails/21.jpg)
Algebra 21
Natural Join: Example
S# SName City
S1 A X
S2 B Y
S# P# QTY
S1 P1 10
S1 P2 20
S2 P1 30
S# SName
City P# QTY
S1 A X P1 10
S1 A X P2 20
S2 B Y P1 30
S SP
S ⋈ SP = ?
![Page 22: Algebra1 After this lecture, you should be able to: Understand the differences between SQL (Structured Query Language) and Relational Algebra expressions.](https://reader035.fdocuments.us/reader035/viewer/2022070402/56649f275503460f94c3e4f6/html5/thumbnails/22.jpg)
Algebra 22
Selection, Join, and Projection: Example
What are the part numbers of the parts supplied by thesuppliers located in Taipei?
select SP.pnofrom S, SPwhere S.city = ‘Taipei’ and S.sno = SP.sno;
pno((S.City=‘Taipei’S)⋈ SP)
SQL:
Algebra:
![Page 23: Algebra1 After this lecture, you should be able to: Understand the differences between SQL (Structured Query Language) and Relational Algebra expressions.](https://reader035.fdocuments.us/reader035/viewer/2022070402/56649f275503460f94c3e4f6/html5/thumbnails/23.jpg)
Algebra 23
Selection, Join, and Projection: Exercise
What are the names of the suppliers who supplied greenparts?
select snamefrom P, SP, Swhere P.color = ‘green’ and P.pno = SP.pno and SP.sno = S.sno;
sname((pno(color=‘green‘P))⋈ SP ⋈ S)
SQL:
Algebra:
![Page 24: Algebra1 After this lecture, you should be able to: Understand the differences between SQL (Structured Query Language) and Relational Algebra expressions.](https://reader035.fdocuments.us/reader035/viewer/2022070402/56649f275503460f94c3e4f6/html5/thumbnails/24.jpg)
Algebra 24
Subtraction Examples
Get the supplier numbers of the suppliers who did notsupply red parts.
The supplier numbers of all the suppliers:AS = s# (S)
The supplier numbers of the suppliers who supplied some red parts:
RPS = s# (SP ⋈ ( Color = ‘Red‘ (P))
The answer is:AS - RPS
![Page 25: Algebra1 After this lecture, you should be able to: Understand the differences between SQL (Structured Query Language) and Relational Algebra expressions.](https://reader035.fdocuments.us/reader035/viewer/2022070402/56649f275503460f94c3e4f6/html5/thumbnails/25.jpg)
Algebra 25
Subtraction Examples (cont’d)
Get the names of the suppliers who supplied only red parts.
The supplier numbers of the suppliers who supplied some parts:
SS = s# (SP) The supplier numbers of the suppliers who
supplied at least one non-red parts:NRS = s# (SP ⋈ ( color <> ‘Red‘ (P))
The answer is: SName ((SS – NRS) ⋈ S)
![Page 26: Algebra1 After this lecture, you should be able to: Understand the differences between SQL (Structured Query Language) and Relational Algebra expressions.](https://reader035.fdocuments.us/reader035/viewer/2022070402/56649f275503460f94c3e4f6/html5/thumbnails/26.jpg)
Algebra 26
Rename (ρ)
A B C D
a 1 u 2
b 1 v 2
c 1 w 2
ΡΡ(B->B2, (B->B2, 33C2)C2)
A B2 C2 D
a 1 U 2
b 1 v 2
c 1 w 2
![Page 27: Algebra1 After this lecture, you should be able to: Understand the differences between SQL (Structured Query Language) and Relational Algebra expressions.](https://reader035.fdocuments.us/reader035/viewer/2022070402/56649f275503460f94c3e4f6/html5/thumbnails/27.jpg)
Algebra 27
Join with Renaming: Example I
Get the pairs of the supplier numbers of the supplierswho supply at least one identical part.
select SPX.sno, SPY.snofrom SP SPX, SP SPYwhere SPX.pno = SPY.pno and SPX.sno < SPY.sno
SQL:
Algebra: snox,snoy(snox < snoy
((ρ(snosnox)sno,pnoSP)⋈
(ρ(snosnoy)sno,pnoSP)
))
![Page 28: Algebra1 After this lecture, you should be able to: Understand the differences between SQL (Structured Query Language) and Relational Algebra expressions.](https://reader035.fdocuments.us/reader035/viewer/2022070402/56649f275503460f94c3e4f6/html5/thumbnails/28.jpg)
Algebra 28
Join with Renaming: Example IIWhat are the colors of the parts supplied by the suppliers located in Taipei?
Table S Table Psno | sname | sts | city pno | pname | color | wgt | city-------------------------- ---------------------------------- s1 | Smith | 20 | London p1 | nut | red | 12 | London s2 | Jones | 10 | Taipei p2 | bolt | green | 17 | Taipei p3 | screw | blue | 17 | Rome
Table SP sno | pno | qty --------------- s1 | p1 | 300 s2 | p3 | 200
![Page 29: Algebra1 After this lecture, you should be able to: Understand the differences between SQL (Structured Query Language) and Relational Algebra expressions.](https://reader035.fdocuments.us/reader035/viewer/2022070402/56649f275503460f94c3e4f6/html5/thumbnails/29.jpg)
Algebra 29
Join with Renaming: Example IIWhat are the colors of the parts supplied by the suppliers located in Taipei?
select colorFrom S, SP, Pwhere S.city = ‘Taipei’ and S.sno = SP.sno and SP.pno = P.pno;
color(S.City=‘Taipei‘(S ⋈ SP ⋈ P))
Joined also by S.city = P.city !Joined also by S.city = P.city !
SQL:
Algebra:
![Page 30: Algebra1 After this lecture, you should be able to: Understand the differences between SQL (Structured Query Language) and Relational Algebra expressions.](https://reader035.fdocuments.us/reader035/viewer/2022070402/56649f275503460f94c3e4f6/html5/thumbnails/30.jpg)
Algebra 30
Join with Renaming: Example II (cont’d)
color(City=‘Taipei‘
(S ⋈ SP ⋈ ρ(citypcity)P))
Renamed:
color((((pno((sno(City=‘Taipei‘S)) ⋈ SP))⋈ P)
Optimized:
color(S.City=‘Taipei‘(S ⋈ SP ⋈ P))
Not Correct:
![Page 31: Algebra1 After this lecture, you should be able to: Understand the differences between SQL (Structured Query Language) and Relational Algebra expressions.](https://reader035.fdocuments.us/reader035/viewer/2022070402/56649f275503460f94c3e4f6/html5/thumbnails/31.jpg)
Algebra 31
Join Example: Query Plan
PPSS SPSP
⋈
city=‘Taipei‘S
sno
⋈
color
pno
![Page 32: Algebra1 After this lecture, you should be able to: Understand the differences between SQL (Structured Query Language) and Relational Algebra expressions.](https://reader035.fdocuments.us/reader035/viewer/2022070402/56649f275503460f94c3e4f6/html5/thumbnails/32.jpg)
Algebra 32
SQL Query Processing
An SQL query is transformed into an algebraic form for query optimization.
Query optimization is the major task of an SQL query proccessor.
select A1, A2, … , Am
from T1, T2, … , Tn
where C
is converted to
A1, A2, …, Am C (T1 x T2 x … x Tn)
and then optimized.
![Page 33: Algebra1 After this lecture, you should be able to: Understand the differences between SQL (Structured Query Language) and Relational Algebra expressions.](https://reader035.fdocuments.us/reader035/viewer/2022070402/56649f275503460f94c3e4f6/html5/thumbnails/33.jpg)
Algebra 33
Query Optimization Guidelines
JOIN operations are associative and commutative (R ⋈ S) ⋈ T = R ⋈ ( S ⋈ T) R ⋈ S = S ⋈ R
JOIN and SELECT operations are associative and commutative if the SELECT operations are still applicable.
PROJECTIONs can be performed to remove column values not used.
These properties can be used for query optimization. The order of operations can be changed to
produce smaller intermediate results.
![Page 34: Algebra1 After this lecture, you should be able to: Understand the differences between SQL (Structured Query Language) and Relational Algebra expressions.](https://reader035.fdocuments.us/reader035/viewer/2022070402/56649f275503460f94c3e4f6/html5/thumbnails/34.jpg)
Algebra 34
Optimization: Example What are the part numbers of the parts supplied by thesuppliers located in Taipei?
pno((sno(S.City=‘Taipei‘S))⋈(sno,pnoSP))
Optimized:
pno (S.City=‘Taipei‘(S ⋈ SP))
Not Optimized:
pno ((S.City=‘Taipei‘S)⋈ SP)
Improved: