Professional Documents
Culture Documents
Algebra
Key = attribute set that uniquely identifies each
row in the relation/table
Keys:
Customer ID
Coustomer ID, Tax ID
……
Superkey
Candidate key
Primary key
Example of 口罩剩餘數}
• Candidate key:
FaceMask • {代碼, 電話} is not a miminal set. Since {電話} could be
removed from {代碼, 電話} while the remining attribute
dataset {代碼} is still a key
• {代碼}, {名稱}, {電話}, {地址}
Introduction
• Query languages are specialized languages for
asking questions or queries, that involve the
data in a database.
Relation R
s s a=b Ùd>5 ( R )
a b c d
a a 1 7 a b c d
a b 5 7 a a 1 7
b b 12 3 b b 23 10
b b 23 10
Selection ( s )
a b
a b
a b a 1
a 1
R S a 2 RUS a 2
a 2
b 3 b 1
b 1
b 3
• R È S returns a relation instance containing all tuples
that occur in either relation R or relation S (or both).
• The union operation is commutative: R È S = S È R
a b
a b a b
a 1
R S a 2 R-S a 1
a 2
b 3 b 1
b 1
Set Difference
• Set difference R – S
returns a relation instance containing all the tuples that occur in R but
not in S.
a b
a b a b
a 1
R S a 2 RÇS a 2
a 2
b 3
b 1
Intersection
R Ç S = R - (R - S )
Cartesian-Product ( ´ ) R´S
a b c d e
S a 1 a 10 +
R c d e a 1 b 10 +
a b a 10 + a 1 b 20 -
a 1 b 10 + a 1 g 10 -
b 2 b 20 - b 2 a 10 +
g 10 - b 2 b 10 +
b 2 b 20 -
b 2 g 10 -
Cartesian product (Cross product)
EMPLOYEE
DEPENDENT
Empnames
Dependents
Emp_dependents ¬ Empnames x Dependents
Emp_dependents
Query: for each female employee, retrieve employee’s name
and a list of the names of her dependents
Actual_dependents ¬ s id=eid ( Emps_dependents )
Result ¬ P fname,lname,dep_name ( Actual_dependents )
Actual_dependents
Result
Join
Empnames id=eid
Dependents
Actual_dependents
Result
Conditional Join
Employee Employee.salary>F.salary
r ( F, Employee )
Employee Employee.salary<F.salary F
eid lname salary (eid) lname salary
22 Dustin 7 31 Lubber 8
31 Dustin 7 58 Rusty 10
31 Lubber 8 58 Rusty 10
Equi-Join
A condition join where the condition contains only equalities.
The conditions of the join have the form R.name1 = S.name2,
where we want to join R with S.
In the resulting relation, S.name2 will be dropped.
R R.b=S.b S
a b c b e f a b c e f
a 1 a 1 X a
b a 1 a X a
R
b 5 g S
3 X a 1 a X g
g 4 b 1 X g a 1 g X a
a 1 g 2 Y d 1
3 Y
a g X g
d 2 b e d 2 b Y d
Natural Join
An Equijoin on all common fields.
For R S, the resulting schema contains the attributes of R
followed by the attributes in S that are not in R.
If two relations have no attributes in common, the result is
simply the cross-product.
R S
a b c d b d e R S
a 1 a X 1 X a a b c d e
b 2 g X 3 X b a 1 a X a
g 4 b Y 1 X g a 1 a X g
a 1 g Y 2 Y d d 2 b Y d
d 2 b Y 3 Y e
Division
x y y x
a 1 B 1 A/B a
a 2 2 e
a 3
b 1
A g 1 The division operation A/B is the
d 1 set of all x values
(in the form of unary tuples)
d 3
d 4 such that for every y value in a
e 1 tuple of B,
e 2 there is a tuple <x,y> in A.
x y y x
a 1 B 1 A/B a
a 2 2 e
a 3
b 1
Another way to understand division:
A g 1
d 1 For each x value in A, consider the
d 3 set of y values that appear in tuples
d 4 of A with that x value.
e 1
e 2 If this set contains all y values in B,
the x value is in the result of A/B.
Examples of Division A/B
sno pno pno pno pno
s1 p1 p2 p2 p1
s1 p2 p4 p2
s1 p3
B1 p4
s1 p4
B2
s2 p1 sno B3
s2 p2 s1
s3 p2 s2 sno
s4 p2 s3 s1 sno
s4 p4 s4 s4 s1
Works_on id pno
- Solution 1
P PID ( s country=UK (Airport ) s date=2020/03/16 (Inbound ))
- Solution 2:
P PID( s country=UK and date=2020/03/16 ( Airport Inbound ))
Query 2: Find the ID of users who traveled to UK and were
back Taiwan on 2020/03/16
- Solution 1:
P ID (( s country=UK (Airport ) s date=2020/03/16 (Inbound)) User)
- Solution 2:
P ID ( P PID ( ( P LATA code (s country=UK Airport )) s date=2020/03/16
(Inbound)) User )
Query 3: Find the ID of users who were back from UK or Japan.
- Solution 1:
r ( TempAirport, ( s country=UK V country=Japan Airport ) )
P ID ( TempAirport Inbound User )
- Solution 2
P ID (( ( s country=UK Airport ) Inbound ) User ) U
P ID (( ( s country=Japan Airport) Inbound ) User )
Query 4: Find the PID of users who were in the same plant of one
confirmed case with his PID as TW0001 on 2020/02/28
-Solution 1
P PID, flight_no (s Date=2020/02/28 Inbound )/P flight_no(s PID=TW001 and
Date=2020/02/28 Inbound)