Transcript Chapter 4

Relational Algebra and
Relational Calculus
© Pearson Education Limited 1995, 2005
Relational Algebra
 คือ
ทฤษฎีทางภาษาสาหรับปฏิบต
ั ก
ิ าร
ั่ หนึง่ หรือ
ระหว่างข ้อมูลต่าง ๆ ในรีเลชน
ั โดยผลของการปฏิบต
หลายๆ รีเลชน
ั ก
ิ ารจะ
ั่ หลัก
ไม่สง่ ผลต่อรีเลชน
2
© Pearson Education Limited 1995, 2005
Relational Algebra
 รูปแบบการสร ้างประโยคภาษาใน
Relational
Algebra จะมี Operation พืน
้ ฐาน 5
Operation คือ
– Selection
– Projection
– Cartesian product
– Union
– Set Difference
3
© Pearson Education Limited 1995, 2005
Relational Algebra Operations
4
© Pearson Education Limited 1995, 2005
Relational Algebra Operations
5
© Pearson Education Limited 1995, 2005
Selection (or Restriction)
 predicate
(R)
ั พันธ์
– เป็ นการเลือกแถวจากความสม
ั่ ทีท
– R คือรีเลชน
่ าการ selection
– Predicate คือ เงือ
่ นไขทีก
่ าหนดขึน
้
6
© Pearson Education Limited 1995, 2005
Example - Selection (or Restriction)
 List
all staff with a salary greater than £10,000.
salary > 10000 (Staff)
7
© Pearson Education Limited 1995, 2005
Projection
 col1, . . . , coln(R)
– เป็ นการเลือกเฉพาะคอลัมน์ทต
ี่ ้องการจาก
ั พันธ์
ความสม
– col1…coln คือ คอลัมน์ทเี่ ลือก
8
© Pearson Education Limited 1995, 2005
Example - Projection
 Produce
a list of salaries for all staff, showing
only staffNo, fName, lName, and salary details.
staffNo, fName, lName, salary(Staff)
9
© Pearson Education Limited 1995, 2005
Union
S
ื่ มความสม
ั พันธ์ด ้วยการนารีเลชน
ั
– เป็ นการเชอ
R และ S มา union กัน
– จะได ้จานวน tuple ทัง้ หมดใน R และ S
รวมกัน
– Tuple ทีซ
่ ้ากันจะถูกขจัดออกไป
R
© Pearson Education Limited 1995, 2005
10
Example - Union

List all cities where there is either a branch office
or a property for rent.
city(Branch)  city(PropertyForRent)
11
© Pearson Education Limited 1995, 2005
Set Difference
R
–S
– เป็ นการหาความแตกต่างระหว่างสอง
ั พันธ์
ความสม
– จะได ้ tuple ทีอ
่ ยูใ่ น R แต่ไม่อยูใ่ น S
– โดยมีกฎว่า R และ S ต ้อง union กันเสมอ
12
© Pearson Education Limited 1995, 2005
Example - Set Difference
 List
all cities where there is a branch office but
no properties for rent.
city(Branch) – city(PropertyForRent)
13
© Pearson Education Limited 1995, 2005
Intersection
S
– จะได ้กลุม
่ tuple ทีอ
่ ยูใ่ น R และ S
– โดยมีกฎว่า R และ S ต ้อง union กันเสมอ
R
 Expressed
using basic operations:
R  S = R – (R – S)
14
© Pearson Education Limited 1995, 2005
Example - Intersection
 List
all cities where there is both a branch
office and at least one property for rent.
city(Branch)  city(PropertyForRent)
15
© Pearson Education Limited 1995, 2005
Cartesian product
R
XS
ั พันธ์โดยเป็ นผลคูณ
– เป็ นการรวมความสม
Cartesian ระหว่าง 2 relations
– จะได ้จานวน tuple ของ R ซงึ่ ทาการ join
กับทุก ๆ tuple ใน S
– R และ S ไม่จาเป็ นต ้อง union กันก็ได ้
16
© Pearson Education Limited 1995, 2005
Example - Cartesian product

List the names and comments of all clients who have viewed a
property for rent.
(clientNo, fName, lName(Client)) X (clientNo, propertyNo, comment (Viewing))
17
© Pearson Education Limited 1995, 2005
Example - Cartesian product and Selection

Use selection operation to extract those tuples where
Client.clientNo = Viewing.clientNo.
sClient.clientNo = Viewing.clientNo((ÕclientNo, fName, lName(Client)) 
(ÕclientNo, propertyNo, comment(Viewing)))
Cartesian product and Selection can be reduced to a single
operation called a Join.

© Pearson Education Limited 1995, 2005
18
Join Operations
 เป็ นการรวม
ั ไปเก็บไว ้ในรีเลชน
ั ใหม่
2 รีเลชน
 เป็ น
operation ทีค
่ อ
่ นข ้างยากสาหรับการ
พัฒนาการออกแบบเพือ
่ จัดการฐานข ้อมูลเพือ
่ ให ้
ิ ธิภาพ
ได ้มาซงึ่ ประสท
© Pearson Education Limited 1995, 2005
19
Join Operations
 มีหลายรูปแบบ
ประกอบด ้วย
– Theta join
– Equijoin
– Natural join
– Outer join
– Semijoin
© Pearson Education Limited 1995, 2005
20
Theta join (-join)
R
S
– เป็ นการหา tuple ทีเ่ กิดจากผลคูณ
ั R และ S ตาม
Cartesian ของรีเลชน
เงือ
่ นไขทีก
่ าหนด
– F คือ เงือ
่ นไขทีก
่ าหนดอยูใ่ นรูป R.ai  S.bi
ั การเปรียบเทียบ
– เมือ
่  คือ โอเปอเรชน
(<, , >, , =, )
F
21
© Pearson Education Limited 1995, 2005
Theta join (-join)
 สามารถเขียน
Theta join ให ้อยูใ่ นรูปของ
ั Selection ร่วมกับผลคูณ Cartesian
โอเปอเรชน
ได ้
R
 Degree
F
S = F (R  S)
ของ Theta join คือผลรวมของ degree ใน R
และ S
ั = หมายถึงเป็ นการใช ้
 ถ ้า predicate F เป็ นโอเปอเรชน
Equijoin
© Pearson Education Limited 1995, 2005
22
Example - Equijoin
 List
the names and comments of all clients who
have viewed a property for rent.
(clientNo, fName, lName(Client))
Client.clientNo =
Viewing.clientNo (clientNo, propertyNo, comment(Viewing))
© Pearson Education Limited 1995, 2005
23
Natural join
R
S
ั ด ้วยการ
– เป็ นการ join ระหว่างรีเลชน
projection แอตตริบวิ ท์ทต
ี่ รงกัน
้
– แอตตริบวิ ท์ทซ
ี่ ้าซอนกั
นจะถูกขจัดออกไป
24
© Pearson Education Limited 1995, 2005
Example - Natural join
 List
the names and comments of all clients who
have viewed a property for rent.
(clientNo, fName, lName(Client))
(clientNo, propertyNo, comment(Viewing))
25
© Pearson Education Limited 1995, 2005
Outer join
ั 2 รีเลชน
ั ซงึ่ มีแอตต
tuple ในรีเลชน
ริบวิ ท์ไม่ตรงกัน
 เป็ นการแสดง
R
S
– (Left) outer join จะแสดงค่าทุก ๆ tuple ในรีเล
ั R ทีม
ั S
ชน
่ ค
ี า่ ในแอตตริบวิ ท์ไม่ตรงกับรีเลชน
26
© Pearson Education Limited 1995, 2005
Example - Left Outer join
 Produce
a status report on property viewings.
propertyNo, street, city(PropertyForRent)
Viewing
27
© Pearson Education Limited 1995, 2005
Semijoin
R
S
ั ทีจ
ั จาก tuple ของ
– เป็ นโอเปอเรชน
่ ัดการรีเลชน
ั R
R ทีจ
่ ะเป็ นบางสว่ นในการ join ของรีเลชน
กับ S
F
้
Semijoin โดยการใชการ
Projection
และ Join แทนได ้
 สามารถเขียน
R
F
S =  A (R
F
S)
28
© Pearson Education Limited 1995, 2005
Example - Semijoin

List complete details of all staff who work at the branch in
Glasgow.
Staff
Staff.branchNo=Branch.branchNo(city=‘Glasgow’(Branch))
29
© Pearson Education Limited 1995, 2005
Division
S
ั ทีน
ั ทัง้ สองมาหารกัน
– เป็ นโอเปอเรชน
่ ารีเลชน
– เป็ นการค ้นหากลุม
่ ของ tuple ทีม
่ ค
ี า่ แอตตริ
บิวท์เหมือนกับแอตตริบวิ ท์ของทุก ๆ tuple
ในอีกกลุม
่ หนึง่
ั ทีเ่ ป็ นตัวหาร ต ้องเป็ นสบ
ั เซต
– มีกฎว่า รีเลชน
ั ทีเ่ ป็ นตัวตัง้
ของรีเลชน
R
30
© Pearson Education Limited 1995, 2005
Division
 Expressed
using basic operations:
T1  C(R)
T2  C((S X T1) – R)
T  T1 – T 2
31
© Pearson Education Limited 1995, 2005
Example - Division
 Identify
all clients who have viewed all properties
with three rooms.
(clientNo, propertyNo(Viewing)) 
(propertyNo(rooms = 3 (PropertyForRent)))
32
© Pearson Education Limited 1995, 2005
Aggregate Operations
 AL(R)
– เป็ นการประยุกต์ใช ้ aggregate function (AL) ใน
ั R
รีเลชน
– AL จะประกอบด ้วย 2 สว่ นคือ
(<aggregate_function>, <attribute>)
 Aggregate
functions หลักได ้แก่ :
COUNT, SUM, AVG, MIN, MAX
33
© Pearson Education Limited 1995, 2005
Example – Aggregate Operations
ั ทีม
 หาจานวนทีอ
่ ยูอ
่ าศย
่ ค
ี า่ เชา่ สูงกว่า
เดือน
R(myCount) COUNT
(PropertyForRent))
propertyNo
£350 ต่อ
(σrent > 350
34
© Pearson Education Limited 1995, 2005
Grouping Operation
 GAAL(R)
ั
– เป็ นการจัดกลุม
่ แอตตริทริบวิ ท์ของทูเพิลในรีเลชน
R (GA) และนาไปใชร่้ วมกับ aggregate function
ั ใหม่
(AL) เพือ
่ กาหนดเป็ นรีเลชน
ั ทีไ่ ด ้จะประกอบด ้วยการจัดกลุม
– รีเลชน
่ ของแอตตริ
บิวท์ทไี่ ด ้กาหนดไว ้ในรูปของ aggregate functions
© Pearson Education Limited 1995, 2005
35
Example – Grouping Operation
 หาจานวนของพนั กงานทีท
่ างานในแต่ละสาขาและ
หาผลรวมของเงินเดือนพนักงานในสาขานัน
้ ด ้วย
R(branchNo, myCount, mySum)
branchNo  COUNT staffNo, SUM salary (Staff)
36
© Pearson Education Limited 1995, 2005
Relational Calculus
 Relational
calculus เป็ นรูปแบบการปฏิบัตก
ิ าร
ในลักษณะ nonprocedural
 มีบทบาทสาคัญในการออกแบบภาษาเรียกดู
ข ้อมูล (query language)
 ยึดหลักเกณฑ์วา
่ ต ้องการเรียกดูข ้อมูลอะไร
(what) มากกว่าเรียกดูอย่างไร (how )
© Pearson Education Limited 1995, 2005
37
Relational Calculus
้
 ผู ้ใชสามารถก
าหนดรูปแบบการค ้นหาใน
ลักษณะของนิพจน์และสมการทางคณิตศาสตร์
 ผลลัพธ์ของคาตอบจะได ้ tuple
จาก
ั พันธ์ทส
ความสม
ี่ ง่ ผลให ้สมการคณิตสาสตร์นัน
้ มี
ค่าเป็ นจริง
© Pearson Education Limited 1995, 2005
38
Relational Calculus
 แบ่งออกเป็ น
2 ประเภทคือ
– Tuple-oriented relational calculus
– Domain-oriented relational calculus
39
© Pearson Education Limited 1995, 2005
Tuple Relational Calculus
 สนใจในการค ้นหา
tuples จากเงือ
่ นไขที่
กาหนดทีเ่ ป็ นจริง โดยตัง้ อยูบ
่ นพืน
้ ฐานของตัว
แปรทูเพิล (tuple variables)
 Tuple
variable คือ ตัวแปรทีท
่ าหน ้าทีแ
่ ทนทู
ั นัน
เพิลของรีเลชน
้ เพือ
่ จะได ้อ ้างถึงชว่ งข ้อมูล
ตามเงือ
่ นไขทีต
่ ้องการ
© Pearson Education Limited 1995, 2005
40
Tuple Relational Calculus
่
 เชน
กาหนดให ้ชว่ ง (range) ของทูเพิลในตัว
ั Staff จึงเขียนได ้ว่า
แปร S คือรีเลชน
Staff(S)
 ให ้ค ้นหากลุม
่ ของทูเพิล
S ทัง้ หมดตาม P(S)
ทีเ่ ป็ นจริง เขียนได ้ว่า
{S | P(S)}
เมือ
่ P คือสูตรหรือเงือ
่ นไขทางตรรกะ
© Pearson Education Limited 1995, 2005
41
Tuple Relational Calculus - Example
ั
 แสดงข ้อมูลทูเพิลทุกแอตตริบว
ิ ท์ของรีเลชน
staff ทีม
่ เี งินเดือนมากกว่า £10,000
{S | Staff(S)  S.salary > 10000}
 แสดงข ้อมูลทูเพิลเฉพาะแอตตริบว
ิ ท์
ั
ของรีเลชน
£10,000
staff
salary
ทีม
่ เี งินเดือนมากกว่า
{S.salary | Staff(S)  S.salary > 10000}
© Pearson Education Limited 1995, 2005
42
Tuple Relational Calculus
้ ญ
ั ลักษณ์
 สามารถใชส
quantifiers มาร่วมกับ
สูตรได ้แก่
– Existential quantifier  (‘there exists’)
– Universal quantifier  (‘for all’)
© Pearson Education Limited 1995, 2005
43
Tuple Relational Calculus

้ ตรทีต
Existential quantifier ถูกใชในสู
่ ้องมีเหตุการณ์ท ี่
่
เกิดขึน
้ จริงอย่างน ้อย 1 เหตุการณ์ เชน
Staff(S) (B) (Branch(B) 
(B.branchNo = S.branchNo)  B.city = ‘London’)

ความหมาย : แสดงทูเพิลของ Branch ทีม
่ ี branchNo
เหมือนกันกับ branchNo ในทูเพิลของ Staff และตัง้ อยู่
ใน London
44
© Pearson Education Limited 1995, 2005
Tuple Relational Calculus

้
Universal quantifier ถูกใชในประโยคที
ก
่ ล่าวถึงทุก
่
เหตุการณ์ เชน
(B) (B.city  ‘Paris’)
 ความหมาย
ใน Paris
: แสดงทุก ๆ ทูเพิลของ Branch ทีไ่ ม่อยู่
้
สามารถใชประโยค
~(B) (B.city = ‘Paris’) แทนได ้
 ความหมาย : แสดงทุกทูเพิลทีไ
่ ม่ม ี Branch ทีอ
่ ยูใ่ น
Paris

45
© Pearson Education Limited 1995, 2005
Tuple Relational Calculus



Formulae should be unambiguous and make sense.
A (well-formed) formula is made out of atoms:
» R(Si), where Si is a tuple variable and R is a relation
» Si.a1  Sj.a2
» Si.a1  c
Can recursively build up formulae from atoms:
» An atom is a formula
» If F1 and F2 are formulae, so are their conjunction,
F1 Ù F2; disjunction, F1 Ú F2; and negation, ~F1
» If F is a formula with free variable X, then (X)(F)
and (X)(F) are also formulae.
46
© Pearson Education Limited 1995, 2005
Example - Tuple Relational Calculus

List the names of all managers who earn more
than £25,000.
{S.fName, S.lName | Staff(S) 
S.position = ‘Manager’  S.salary > 25000}

List the staff who manage properties for rent in
Glasgow.
{S | Staff(S)  (P) (PropertyForRent(P) 
(P.staffNo = S.staffNo) Ù P.city = ‘Glasgow’)}
47
© Pearson Education Limited 1995, 2005
Example - Tuple Relational Calculus

List the names of staff who currently do not
manage any properties.
{S.fName, S.lName | Staff(S)  (~(P)
(PropertyForRent(P)(S.staffNo = P.staffNo)))}
Or
{S.fName, S.lName | Staff(S)  ((P)
(~PropertyForRent(P) 
~(S.staffNo = P.staffNo)))}
48
© Pearson Education Limited 1995, 2005
Example - Tuple Relational Calculus
 List
the names of clients who have viewed a
property for rent in Glasgow.
{C.fName, C.lName | Client(C)  ((V)(P)
(Viewing(V)  PropertyForRent(P) 
(C.clientNo = V.clientNo) 
(V.propertyNo=P.propertyNo) 
P.city =‘Glasgow’))}
49
© Pearson Education Limited 1995, 2005
Tuple Relational Calculus
 Expressions
can generate an infinite set. For
example:
{S | ~Staff(S)}
 To
avoid this, add restriction that all values in
result must be values in the domain of the
expression.
50
© Pearson Education Limited 1995, 2005
Domain Relational Calculus
้ วแปรจากโดเมนของทูเพิลในรีเลชน
ั นัน
 จะใชตั
้
แทน
F(d1, d2, . . . , dn) คือสูตรทีใ่ ช ้
 และ d1, d2, . . . , dn แทนโดเมน จะเขียนได ้ว่า
 ถ ้า
{d1, d2, . . . , dn | F(d1, d2, . . . , dn)}
51
© Pearson Education Limited 1995, 2005
Example - Domain Relational Calculus

ื่ ของ manager ทีม
ค ้นหาชอ
่ เี งินเดือนสูงกว่า
£25,000
{fN, lN | (sN, posn, sex, DOB, sal, bN)
(Staff (sN, fN, lN, posn, sex, DOB, sal, bN) 
posn = ‘Manager’  sal > 25000)}
52
© Pearson Education Limited 1995, 2005
Example - Domain Relational Calculus

แสดง staff ทีด
่ แ
ู ลบ ้านเชา่ ใน Glasgow
{sN, fN, lN, posn, sex, DOB, sal, bN |
(sN1,cty)(Staff(sN,fN,lN,posn,sex,DOB,sal,bN) 
PropertyForRent(pN, st, cty, pc, typ, rms,
rnt, oN, sN1, bN1) 
(sN=sN1)  cty=‘Glasgow’)}
53
© Pearson Education Limited 1995, 2005
Example - Domain Relational Calculus

List the names of staff who currently do not
manage any properties for rent.
{fN, lN | (sN)
(Staff(sN,fN,lN,posn,sex,DOB,sal,bN) 
(~(sN1) (PropertyForRent(pN, st, cty, pc,
typ, rms, rnt, oN, sN1, bN1) 
(sN=sN1))))}
54
© Pearson Education Limited 1995, 2005
Example - Domain Relational Calculus

List the names of clients who have
viewed a property for rent in Glasgow.
{fN, lN | (cN, cN1, pN, pN1, cty)
(Client(cN, fN, lN,tel, pT, mR) 
Viewing(cN1, pN1, dt, cmt) 
PropertyForRent(pN, st, cty, pc, typ, rms, rnt, oN,
sN, bN)  (cN = cN1)  (pN = pN1)  cty =
‘Glasgow’)}
55
© Pearson Education Limited 1995, 2005
Other Languages
 Transform-oriented
languages are nonprocedural languages that use relations to
transform input data into required outputs (e.g.
SQL).
 Graphical
languages provide user with picture of
the structure of the relation. User fills in example
of what is wanted and system returns required
data in that format (e.g. QBE).
© Pearson Education Limited 1995, 2005
56
Other Languages
 4GLs
can create complete customized
application using limited set of commands in a
user-friendly, often menu-driven environment.
systems accept a form of natural
language, sometimes called a 5GL, although
this development is still at an early stage.
 Some
© Pearson Education Limited 1995, 2005
57