บทที่ 3 ฐานข้อมูลเชิงสัมพันธ์
TRANSCRIPT
1
บทท 3ฐานขอมลเชงสมพนธ(Relational Database)
2/90
คณสมบตของ Relation
ประเภทของ Relation
การควบคมความถกตองใหกบขอมล
กฎ Referential Integrity
3/90
ฐานขอมลแบบ Relational
โครงสรางของฐานขอมลแบบ Relational ไดถกนยามไวในแบบจ าลองดานฐานขอมลทชอวา Relational Model
แบบจ าลองน คดคนโดย Dr.E.F. Codd นกคณตศาสตรในชวงป ค.ศ. 1968 ดวยการนยามทฤษฎตางๆ ใหกบโครงสรางขอมลในรปแบบคณตศาสตร เพอแกไขขอบกพรอง ของฐานขอมลแบบเดมๆ
4/90
Dr.E.F.Codd กลาวถงฐานขอมลแบบ Relation ไว 3 สวนดงน
1. Data Structure สวนทเกยวกบโครงสรางของขอมล ภายในฐานขอมลแบบ Relational Model
2. Data Integrity สวนทเกยวกบกฎเกณฑตางๆ ทใชควบคมความถกตองของขอมลภายในฐานขอมล
3. Data Manipulation สวนทกลาวเกยวกบการกระท าตางๆ ทจะเกดขนกบขอมล
ฐานขอมลแบบ Relational
5/90
นยามค าศพททใชในฐานขอมลแบบ Relational
Relation
เปนหนวยทใชจดเกบขอมลทอยในรปแบบของตารางขนาด 2 มต ทประกอบดวยแถว (Row) และสดมภ (Column) มกจะถกเรยกวา “Table”
ฐานขอมลแบบ Relational
6/90
Relation
ชอของแตละแถวของ Relation จะเรยกวา “Tuple” ชอของแตละสดมภ จะเรยกวา “Attribute”
7/90
Domain
คอการนยามขอบเขตของขอมลทเปนไปไดใหกบขอมลในแตAttribute เพอปองกนไมใหเกดการปอนขอมลทเกนขอบเขตทก าหนด
Domain
8/90
คณสมบตของ Relation
1. ภายใน Relation ใดๆ ตองม Attribute ใด Attribute หนง ทท าใหแตละ Tuple ใน Relation มขอมลไมซ ากน
9/90
2. ภายใน Relation จะไมมการก าหนดล าดบทใหกบแตละ Tuple
3. ภายใน Relation จะไมมการก าหนดล าดบทใหกบแตละ Attribute
4. คาในทก Attribute จะตองมความหมายใดความหมายหนงเทานน(คณสมบต Atomicity)
คณสมบตของ Relation
10/90
5. ชอของแตละ Attribute ใน Relation เดยวกน จะตองมชอทไมซ ากน
6. คาทปรากฏในแตละ Attribute ใน Relation เดยวกน จะตองใชแทนขอมลทมความหมายเดยวกน
คณสมบตของ Relation
11/90
ประเภทของ Relation
Relation สามารถแบงออกเปนประเภทตางๆ ไดดงน
1. Base Relation เปน Relation จรงในฐานขอมล ซงใชเกบขอมลในหนวยความจ าส ารอง
2. View เปน Relation เสมอน ทไดจากการใชค าสง Query กบ BaseRelation
12/90
วว
13/90
Degree
คอ จ านวนของ Attribute ในแตละ Relation
Degree = 4
14/90
Cardinality
คอ จ านวนของ Tuple ใน Relation หนง ซงจะน าไปใชก าหนด ประเภทของความสมพนธระหวางขอมลใน Relation
15/90
Catalog ของ Relational Model
Catalog มลกษณะเปน Relation จดเกบขอมลแบบ MetadataTABNAME COLCOUNT ROWCOUNT ………..
DEPARTMENT 2 3 ……….
EMPLOYEE 4 4 ………..
………………… ……………… ………………… ………..
TABLES
TABNAME COLNAME ………..
DEPARTMENT DeptID ……….
DEPARTMENT DeptNAME ……….
EMPLOYEE EmpID ………..
EMPLOYEE name ……….
EMPLOYEE surname ……….
EMPLOYEE DeptID ……….
………………… ……………… ………..
COLUMNS
16/90
สรปโครงสรางของ Relational Model
Data Structure ไดก าหนดโครงสรางทใชในการจดเกบขอมล ไวในรปแบบตาราง 2 มต ทเรยกวา “Relation”
แตละแถวของ Relation จะถกเรยกวา “Tuple” และแตละสดมภจะถกเรยกวา “Attribute”
การก าหนดขอบเขตของขอมลในแอตทรบวสเรยกวา Domain
จ านวนแอตทรบวสใน Relation เรยกวา “Degree” จ านวน ทเพลในรเลชนเรยกวา “Cardinality”
17/90
เหตใดจงไมก าหนดล าดบทใหกบ แอตทรบวสและทเพล
ขอแตกตางระหวาง view กบ base Relation คอ
สรปโครงสรางของ Relational Model
18/90
การควบคมความถกตองใหกบขอมล (Data Integrity)
“Integrity Rule Integrity Rule” เปน กฎเกณฑและขอจ ากดตางๆ ทก าหนดขนเพอใชควบคมใหขอมลในฐานขอมล มความถกตองตามความเปนจรงอยเสมอ
19/90
Integrity Rule
แบงได 2 ประเภทคอ 1. Database Specific Integrity Rule เปน กฎเกณฑทก าหนดขนเพอใช
ควบคมความถกตองใหกบฐานขอมลใดฐานขอมลหนงโดยเฉพาะ รหสพนกงานแตละคน ตองเปนตวเลขจ านวน 5 หลก พนกงานแตละคนสงกดไดเพยงแผนกเดยว จ านวนเงนเดอนของพนกงานจะตองมคามากกวา 0
20/90
Integrity Rule
2. General Integrity Rule เปนกฎเกณฑและขอจ ากดทก าหนดขนเพอควบคม และรกษาความสมพนธระหวางขอมลในสวนตางๆ ภายในฐานขอมล ใหมความสมพนธทถกตอง สามารถน าไปใชไดกบทกฐานขอมล
ม 2 กฎ คอ
Entity Integrity
Referential Integrity
21/90
Key
Key คอ Attribute หรอชดของ Attribute ทระบแลวท าใหทราบคาของแอตทรบวสอน
Super Key คอ Attribute หรอชดของ Attribute ทท าใหขอมลแตละ Tuple ใน Relation มคาของขอมลทไมซ ากน (Unique)
22/90
Candidate Key
Candidate Key คอ Super Key ขนาดเลกทสดทท าใหขอมลในแตละ Tuple ของ Relation มคาของขอมลทไมซ ากน
23/90
Candidate Key
คณสมบตของ Candidate Key ประกอบดวย
1. Relation สามารถม Candidate Key ไดมากกวา 1 ถา Relation นนปรากฏ Attribute ทสามารถท าใหแตละ Tuple ใน Relation มคาของขอมลไมซ ากน
2. Candidate Key สามารถก าหนดขนจากหลาย Attribute ได
24/90
Candidate Key
3. คาของ Candidate Key จะไมสามารถแยกออกเปน Key ยอยๆ ไดอก หรออกนยหนงคอ Candidate Key ตองเปน Super Key ทเลกทสดทท าใหขอมลในแตละ Tuple ของ Relation มคาขอมลไมซ ากน
25/90
Primary Key และ Alternate Key
Primary Key คอการเลอก Candidate Key ทมขนาดเลกทสด และถกเรยกใชโดยการกระท าตางๆ มากทสดมาเปน Key หลกเพอใชตรวจสอบการซ ากนของขอมล
Candidate Key ทเหลอจะท าหนาทเปน Alternate Key
Primary Key เปน Key ทใชก าหนดใหกบ Base Relation แตไมจ าเปนวาทก Base Relation จะตองม Primary Key
26/90
Foreign Key
Foreign Key คอ Attribute ใน Relation ทใชอางองไปยงAttribute ทท าหนาทเปน Candidate Key ของอก Relation หนงทมความสมพนธกน
27/90
Foreign Key
คณสมบตของ Foreign Key จะประกอบดวย1. Foreign Key สามารถก าหนดขนจาก Attribute เดยว หรอ
มากกวากได เชนเดยวกบ Primary Key2. คาของ Foreign Key ใน Relation หนง จะตองปรากฏ อยใน
Candidate Key ของ Relation ทสมพนธกนนน แต ทกคาของCandidate Key ใน Relation หนง ไมจ าเปนตองปรากฏอยในForeign Key ของ อก Relation หนง ทสมพนธกน
28/90
Foreign Key
3. จ านวนของ Attribute ทจะมาประกอบเปน Foreign Key จะขนอยกบจ านวนของ Attribute ทก าหนดเปน Candidate Keyของ Relation ทมความสมพนธกบ Relation นน ทถกน ามาใชเปรยบเทยบกบคาของ Foreign Key
4. Attribute ทท าหนาทเปน Foreign Key ไมจ าเปนตองเปนCandidate Key ของ Relation นน
29/90
5. Relation ทเปนเจาของ Foreign Key จะถกเรยกวา “ReferencingRelation” สวน Relation ทเปนเจาของ Candidate Key ทสมพนธกบForeign Key นนจะถกเรยกวา “Referenced Relation” หรอ“Target Relation”
Foreign Key
Referencing Relation Referenced Relation6. Relation ใดๆ สามารถเปนไดทง Referenced Relation และ
Referencing Relation
30/90
Foreign Key
7. Relation ใดๆ สามารถมความสมพนธดวย Foreign Key กบตวมนเองได ซงความสมพนธแบบนจะเรยกวา “Self-Referencing”
ขอสงเกต Candidate Key ทถก Foreign Key อางถงโดยทวไป มกจะท าหนาทเปน Primary Key ของอก Relation
31/90
Relational Database Keys
32/90
กฎ Referential Integrity
“ฐานขอมลใดๆ จะตองไมปรากฏ Relation ทมขอมลทมคาของ Foreign Key ทไมขนกบ Candidate Key ของ Relation อน”
การท าใหขอมลใน Foreign Key สามารถอางองไปยงCandidate Key ของ Relation อนได จะตองก าหนดกฎเพอควบคมการลบ และแกไขขอมลระหวาง Relation ตางๆ ทสมพนธกนดวยForeign Key เรยกวา กฎ Foreign Key
33/90
กฎ Referential Integrity
กฎ Foreign Key
1. กรณทมการลบขอมลใน Target Relation ตองเลอกท าอยางใดอยางหนงกบขอมลใน Referencing Relation ดงน
- ใหยกเลกการลบขอมลใน Target Relation
- ใหลบขอมลทงของ Referencing และ Target Relation ทมForeign Key อางองอยท งหมด
34/90
กฎ Referential Integrity
2. กรณมการแกไขเปลยนแปลงขอมลในสวน Candidate Key ของTarget Relation ทมความสมพนธกบ Foreign Key ของอกRelation หนงทสมพนธกน ตองเลอกท าอยางใดอยางหนงกบขอมลในสวน Foreign Key ของ Referencing Relation ดงน
- ยกเลกการแกไขขอมลในสวน Candidate Key ของ Target Relation
- แกไขขอมลในสวน Foreign Key ของ Referencing Relation และในสวน Candidate Key ของ Target Relation ท Foreign Key นนอางองอยท งหมด
35/90
Null
Null คอการก าหนดคาส าหรบขอมลทไมสามารถระบคาได
Null จะมคาแตกตางจากชองวาง หรอจ านวนศนย เนองจากNull เปนคาทไมปรากฏ และเปนคาทแทนขอมลทไมสามารถระบคาได
36/90
กฎ Entity Integrity
กฎ Entity Integrity คอ กฎทระบไววา “Primary Key ของBase Relation ใดๆ หามมคาเปน Null” มขอนาสงเกตคอ
1. ใชส าหรบ Base Relation เทานน
2. ใชกบเฉพาะ Primary Key เทานน ไมรวมถง Candidate Key
3. สามารถก าหนดใหกบทกๆ Attribute ของ Base Relation ได
37/90
Foreign Key กบคา Null
ในบางครง Foreign Key ไมสามารถระบคาได
ดงนนค าสง SQL ทใชก าหนด Foreign Key จงปรากฏค าสงทใชก าหนดให Foreign Key มคาเปน Null ไดหรอไมเพมขนมา
38/90
Foreign Key กบคา Null
กฎเพมเตมทก าหนดให Foreign Key มคาเปน Null ได
1. กรณท Foreign Key เปน Composite Key และมคาเปน Null แลวทก Attribute ทประกอบกนเปน Foreign Key นน จะตองมคาเปนNull ดวย
2. การลบขอมลใน Target Relation เพมเตมรายละเอยดคอ ใหลบขอมลของ Target Relation ได แตตองก าหนดใหคาของ ForeignKey ของ Referencing Relation ทอางถง มคาเปน Null แทน
39/90
3. การแกไขเปลยนแปลงขอมลในสวน Candidate Key ของTarget Relation ทมความสมพนธกบ Foreign Key ของอกRelation หนง ทสมพนธกน ใหแกไขขอมลในสวน CandidateKey ของ Target Relation ได แตตองก าหนด ใหคาของForeign Key ของ Referencing Relation ทอางถงนนมคาเปนNull แทน
Foreign Key กบคา Null
40/90
Super Key คอ Attribute หรอชดของ Attribute ทท าใหขอมลแตละ Tuple ใน Relation
มคาของขอมลทไมซ ากน (Unique)
Candidate Key คอ Super Key ขนาดเลกทสด
Primary Key คอ Candidate Key ทถกเลอกมาเปนคยหลก
Alternate Key คอ ถกตองนะคราบ
Foreign Key คอ ถกตองนะคราบ
สรปกฎในการรกษาความถกตอง
41/90
สรปกฎในการรกษาความถกตอง
การควบคมความถกตองใหกบขอมล (Data Integrity) ในฐานขอมล มการก าหนดกฎทใชควบคมความสมพนธระหวางขอมลใน Relation ตางๆ ใหมความถกตองอยเสมอ ไดแก Entity Integrity
Referential Integrity
42/90
กฎ Entity Integrity กลาววา
Primary Key ของ Base Relation ใดๆ หามมคาเปน Null
กฎ Referential Integrity กลาววา
ฐานขอมลใดๆ จะตองไมปรากฏ Relation ทมขอมลทมคาของ Foreign Key ทไมขนกบ Candidate Key ของ Relation อน
สรปกฎในการรกษาความถกตอง
43/90
การจดการขอมล (Data Manipulation)
Dr. E. F. Codd ไดก าหนดแบบจ าลองของการกระท าตางๆ ทสามารถเกดขนกบฐานขอมลแบบ Relational ไวใน RelationalModel 2 แบบดวยกนคอ
Relational Algebra
Relational Calculus
44/90
Relational Algebra
Relational Algebra แบงตามประเภทการใชงาน แยกไดเปน 2 ประเภท คอ การใชงานขนพนฐาน ไดแก
Selection (s ). เลอกแถวจากความสมพนธ
Projection (p ) เลอกเฉพาะคอลมนทตองการจากความสมพนธ Cross- product (X) สามารถรวมความสมพนธได Set difference (- ) หาความแตกตางระหวาง 2 ความสมพนธ
Union () เชอมความสมพนธ 2 อนเขาดวยกน การใชงานขนสง ไดแก Intersection, join , division
45/90
Selection Operator (Restriction)
ตวกระท าในการ Select คอ s (sigma) เพอแสดงขอมลของ Tuple ใน Relationทมคาตรงตามเงอนไขทระบ
เครองหมายเปรยบเทยบ ไดแก = (เทากบ), (ไมเทากบ), < (นอยกวา), (นอยกวาหรอเทากบ), > (มากกวา), > (มากกวาหรอเทากบ)
ตวกระท าทางตรรกะ ไดแก (and) , (or) และ (not)
s height > 178 Player
s age > 25 position = กองหนา Player บางครงอธบายในรปแบบ a WHERE x operator y
46/90
Select
47/90
Projection operator
Projection คอ การเลอกเฉพาะบาง Attribute ของ Relation ทตองการมาแสดง โดยใชสญลกษณ p (pi)
เทยบไดกบการใชค าสง SELECT … FROM ในภาษา SQL
p weight, height Player SELECT weight, height FROM Player
หรอใชรปแบบ a [x,y,….z]
48/90
Project
49/90
การใช Selection operator รวมกบ Projection operator
p name, position (s height > 178 Player) SELECT name, position FROM Player WHERE height > 178
Position age name height weight
กองหนา 26 สเมธ 183 82
กองหลง 30 ชาตชาย 169 71
กองกลาง 24 กองเกยรต 180 78
ปกซาย 21 ผลด 175 78
Player
name Position
สเมธ กองหนา
กองเกยรต กองกลาง
50/90
Product (Cross Product)
เปนการกระท าเพอแสดงขอมลของทก Tuple ทสามารถเปนไปได ทเกดจากการจบคกนของ ขอมล 2 Relation แบบผลคณ Cartesian ตามทฤษฎของเซต
a
b
c
x
ya
a
b
b
c
c
x
y
x
y
x
y
Product
51/90
Product (Cross Product)
S#
S1
S2
S3
A P#
P1
P2
P3
B S#
S1
S1
S1
A TIMES B
S2
S2
S2
S3
S3
S3
P#
P1
P2
P3
P1
P2
P3
P1
P2
P3
A TIMES BA X B
52/90
Product (Cross Product)
53/90
Union
R1 R2
R1 UNION R2
คอการแสดงขอมลของทก Tuple ทเกดจากการรวมกนของขอมล 2 Relation ในรปแบบของการ Union
54/90
Union
ตวอยางการ UNION ระหวาง Relation A และ Relation B
S# SNAME STATUS CITY
S1 ก.การชาง 20 สมทรปราการ
S4 เอส.พ.คอนสตรคชน 20 สมทรปราการ
S# SNAME STATUS CITY
S1 ก.การชาง 20 สมทรปราการ
S2 ส.ซพพลาย 10 สพรรณบร
A B
S# SNAME STATUS CITY
S1 ก.การชาง 20 สมทรปราการ
S4 เอส.พ.คอนสตรคชน 20 สมทรปราการ
S2 ส.ซพพลาย 10 สพรรณบร
A UNION B
55/90
Intersection
เปนการกระท าเพอแสดงขอมลของ Tuple ทปรากฏอยในทง 2 Relation
R1 INTERSECT R2
R1 R2
56/90
Intersect
57/90
Difference
เปนการกระท าเพอแสดงขอมลของ Tuple ทปรากฏอยใน Relation หนง แตไมปรากฏอยในอก Relation หนง ในรปแบบ A-B
R1 MINUS R2
R1 - R2
58/90
Difference
59/90
Divide
aaabc
X
ya
Divide
xyzxy
แสดงขอมลทเปนไปไดทงหมด ซงเกดจากการน าเอา Relation R1 เปรยบเทยบกบ Relation R2 โดยท Relation R1 ตองม Degree ทสงกวา Relation R2 และทก Attribute ของ Relation R2 ตองปรากฏอยใน Relation R1
60/90
DIVIDE
61/90
Join Operation
เปนการกระท าเพอแสดงขอมลทเปนไปไดทงหมด ซงเกดจากการเชอมโยงขอมลของ 2 Relation ทมคาของขอมลในAttribute ทก าหนดเหมอนกน
a1
a2
a3
Join
b1
b1
b2
c1
c1
c2
b1
b2
b3
c1
c2
c3
a1
a2
a3
b1
b1
b2
62/90
Join Operation
Join Types Inner join Left outer join Right outer join Full outer join
Join Conditions Natural On <predicate> Using (A1, A2, …, An)
63/90
Natural Join
Links tables by selecting only rows with common values in their common attribute(s)
Result of a three-stage process: PRODUCT of the tables is created SELECT is performed on Step 1 output to yield only the rows for
which the AGENT_CODE values are equal Common column(s) are called join column(s)
PROJECT is performed on Step 2 results to yield a single copy of each attribute, thereby eliminating duplicate columns
64/90
Two Tables That Will Be Used in Join Illustrations
65/90
Natural Join, Step 1: PRODUCT
66/90
Natural Join, Step 2: SELECT
67/90
Natural Join, Step 3: PROJECT
68/90
Other Forms of Join
Equijoin Links tables on the basis of an equality condition that compares
specified columns of each table Outcome does not eliminate duplicate columns Condition or criterion to join tables must be explicitly defined Takes its name from the equality comparison operator (=) used in
the condition
Theta join If any other comparison operator is used
69/90
Inner Join – ExampleRelation loan Relation borrower
customer-name loan-number
Jones
Smith
Hayes
L-170
L-230
L-155
loan-number amount
L-170
L-230
L-260
3000
4000
1700
branch-name
Downtown
Redwood
Perryridge
Inner Joinloan Borrower
loan-number amount
L-170
L-230
3000
4000
customer-name
Jones
Smith
branch-name
Downtown
Redwood
loan-number
L-170
L-230
Select * from (loan inner join borrower on loan.loan-number = borrower.loan-number)
(p loan-number, branch-name, amount (loan)) (p customer-name, loan-number (borrower))
70/90
Natural Inner Join – Example
loan-number amount
L-170
L-230
3000
4000
customer-name
Jones
Smith
branch-name
Downtown
Redwood
loan-number
L-170
L-230
Select * from (loan inner join borrower onloan.loan-number = borrower.loan-number)
loan-number amount
L-170
L-230
3000
4000
customer-name
Jones
Smith
branch-name
Downtown
Redwood
Select * from (loan natural inner join borrower onloan.loan-number = borrower.loan-number)
71/90
Outer Join
เปนสวนเพมเตมของ join operation ทหลกเลยงการสญหายของขอมล โดยการแทนขอมลใหเปนคา null ใน tuple ทขอมลของอกrelation ทน ามา join ไมตรงกบ relation หลกในการ join
72/90
Left Outer Join – Example
Relation loan Relation borrowercustomer-name loan-number
Jones
Smith
Hayes
L-170
L-230
L-155
loan-number amount
L-170
L-230
L-260
3000
4000
1700
branch-name
Downtown
Redwood
Perryridge
Select * from (loan left outer join borrower onloan.loan-number = borrower.loan-number)
Left Outer Joinloan borrower
loan-number amount
L-170
L-230
L-260
3000
4000
1700
customer-name
Jones
Smith
null
branch-name
Downtown
Redwood
Perryridge
loan-number
L-170
L-230
null
73/90
Right Outer Join – Example
Relation loan Relation borrowercustomer-name loan-number
Jones
Smith
Hayes
L-170
L-230
L-155
loan-number amount
L-170
L-230
L-260
3000
4000
1700
branch-name
Downtown
Redwood
Perryridge
Select * from (loan natural right outer join borroweron loan.loan-number = borrower.loan-number)
Right Outer Joinloan borrower
loan-number amount
L-170
L-230
L-155
3000
4000
null
customer-name
Jones
Smith
Hayes
branch-name
Downtown
Redwood
null
74/90
Full Outer Join – ExampleRelation loan Relation borrower
customer-name loan-number
Jones
Smith
Hayes
L-170
L-230
L-155
loan-number amount
L-170
L-230
L-260
3000
4000
1700
branch-name
Downtown
Redwood
Perryridge
Select * from (loan Full outer join borrowerusing (loan-number))
Full Outer Joinloan borrower
loan-number amount
L-170
L-230
L-260
L-155
3000
4000
1700
null
customer-name
Jones
Smith
null
Hayes
branch-name
Downtown
Redwood
Perryridge
null
75/90
ก าหนดรปแบบการคนหาในลกษณะของนพจนหรอสมการทางคณตศาสตรทมตวแปร คาคงท ตวกระท า ตวเชอม และอนๆ
เปนการใชคณตศาสตรในรปของตรรกะเขามาชวยในการคนหาขอมล
ค าตอบทไดจากการใช Relational calculus คอแถวของขอมลจากความสมพนธทท าใหคาของสมการคณตศาสตรนนมคาเปน จรง
Relational Calculus
76/90
Relational Calculus
รปแบบของควร
(A1, …,An) | p[(A1,…,An)] }
ค าตอบทไดจากควร ประกอบไปดวยแถวของขอมล (A1,…,An) ทท าใหสมการ p [(A1,…,An)] มคาเปนจรง
ตวกระท ากบสมการมดงน : (not) , (intersection), (union),
(for all) , (there exists)
77/90
Relational Calculus
ตวอยาง หาขอมล t1 ทกแถวในความสมพนธ EmpDept ทท าใหมบางแถวใน t2 ซง t2 อยในความสมพนธ Emp และทง t1 และ t2 มคาในคอลมนDept เหมอนกน
{t1 | ( t2 )((t1 EmpDept) & (t2 Emp) & (t1 .Dept = t2.Dept)}
78/90
Indexes
Arrangement used to logically access rows in a table
Index key Index’s reference point
Points to data location identified by the key
Unique index Index in which the index key can only have one pointer value (row)
associated with it
Each index is associated with only one table
79/90
Components of an Index
80/90
ศพท
attribute domain candidate key
composit key data dictionary
domain entity integrity
equijoin foreign key (FK)
index join column(s)
key left outer join
natural join null
81/90
ศพท
primary key referential integrity
relational algebra right outer join
secondary key super key
synonym theta join
unique index
82/90
????
ตองการขอมลเฉพาะบางคอลมนของตารางตองใชการกระท าใด Projection
ตองการขอมลเฉพาะบางทเพลของตารางตองใชการกระท าใด Selection
ผลของการกระท า Cartesian Product ระหวาง 2 รเลชนคอ ไดตารางผลลพธเปนการจบคทกกรณทเปนไปไดของขอมล
83/90
????
ขอแตกตางระหวางการ join กบการท า Product คอ
ขอแตกตางระหวาง Inner join กบ Outer join คอ
left outer join , right outer join และ full outer join ตางกนอยางไร
84/90
????
จากตาราง ตองการ join ขอมลจาก 2 ตารางตองเขยนค าสงอยางไร
Select * from (Product inner join Supplier on Product.SP# = Supplier.SP#)
P# PNAME SP#
P1 Part1 S1
P2 Part2 S2
SP# SNAME CITY
S1 Supplier1 กทม.
S2 Supplier1 สมทรปราการ
ตาราง Product ตาราง Supplier
85/90
สรปโครงสรางของ Relational Model
Data Structure ไดก าหนดโครงสรางทใชในการจดเกบขอมล ไวในรปแบบตาราง 2 มต ทเรยกวา “Relation”
แตละแถวของ Relation จะถกเรยกวา “Tuple” และแตละสดมภจะถกเรยกวา “Attribute”
การก าหนดขอบเขตของขอมลในแอตทรบวสเรยกวา Domain
จ านวนแอตทรบวสใน Relation เรยกวา “Degree” จ านวน ทเพลในรเลชนเรยกวา “Cardinality”
86/90
Super Key คอ Attribute หรอชดของ Attribute ทท าใหขอมลแตละ Tuple ใน Relation ม
คาของขอมลทไมซ ากน (Unique)
Candidate Key คอ Super Key ขนาดเลกทสด
Primary Key คอ Candidate Key ทถกเลอกมาเปนคยหลก
Alternate Key คอ Candidate Key อนๆ ทไมถกเลอก
Foreign Key คอ คยทใชส าหรบเชอมโยงความสมพนธระหวางรเลชน
สรปกฎในการรกษาความถกตอง
87/90
กฎ Entity Integrity กลาววา
Primary Key ของ Base Relation ใดๆ หามมคาเปน Null
กฎ Referential Integrity กลาววา
ฐานขอมลใดๆ จะตองไมปรากฏ Relation ทมขอมลทมคาของ Foreign Key ทไมขนกบ Candidate Key ของ Relation อน
สรปกฎในการรกษาความถกตอง
88/90
WORKSHOP
สรางตารางดงรป
89/90
WORKSHOP
เชอมโยงความสมพนธของทงสองตาราง ทดสอบ Entity Integrity และ Referential Integrity
90/90
WORKSHOP