โมเดลขอ้มูลเชิงสัมพนัธ์
The Relational Data Model
1 Asst.Prof. Intiraporn Mulasastra
Data Modeling
Real World
Data Recordings
Data Modeling Data Representation
Data Items
<e,a,v>
<e,a,v>
<e,a,v>
แบบจ ำลองขอ้มูล ขอ้มูลในเคร่ืองคอมฯ
ER-Model
Relational Data Model
2 Asst.Prof. Intiraporn Mulasastra
Relational Database
is a collection of tables
3 Asst.Prof. Intiraporn Mulasastra
Tables
Heading: table name and column names
Body: rows, occurrences of data
StdSSN StdLastName StdMajor StdClass StdGPA
123-45-6789 WELLS IS FR 3.00
124-56-7890 NORBERT FIN JR 2.70
234-56-7890 KENDALL ACCT JR 3.50
Student Table
รหสั นำมสกุล ภำควิชำ ชั้นปี เกรดเฉล่ีย
ไม่มีการเกบ็ข้อมูลซ า้ซ้อน
4 Asst.Prof. Intiraporn Mulasastra
Physical storage
Row1 Row2 Row3 ….
Data in Table
Column names, type
Table Scheme in data dictionary tables
5 Asst.Prof. Intiraporn Mulasastra
E-R Model Relational Model
E-R Model
Entity type
Entity Instance
Relationship
Attribute
Primary key required
Relational Model
Table
Row
Entity References by
using keys
Column
Some tables may not
have a primary key**
Foreign key*
6 Asst.Prof. Intiraporn Mulasastra
Alternative Terminology
Table-
oriented
Set-oriented Record-
oriented
Table Relation Record-type,
File
Row Tuple Record
Column Attribute Field
A tuple : (‘John’, ‘CPE’, 3.1)
7 Asst.Prof. Intiraporn Mulasastra
Primary keys
Entity integrity
Each table has column(s) with unique values
Ensures entities are traceable
StdSSN StdLastName StdMajor StdClass StdGPA
123-45-6789 WELLS IS FR 3.00
124-56-7890 NORBERT FIN JR 2.70
234-56-7890 KENDALL ACCT JR 3.50
PK
unique 8 Asst.Prof. Intiraporn Mulasastra
Types of Relationship
Do not support many to many
one to one
one to many, many to one
9 Asst.Prof. Intiraporn Mulasastra
Relationships 1:N
CourseNo
CrsDesc
CrsUnits
Course
OfferNo
OffLocation
OffTime
Offering
Has
OfferNo OffLocation OffTime CourseNo
111 CPE 203 1/2014 204351
222 CPE 204 2/2014 204351
333 CPE 204 2/2014 204111
CourseNo CrsDesc CrsUnits
204351 Database 3
204111 Programming 3
Different OfferNo, the same CourseNo
Add PK of Course table in Offering table
10 Asst.Prof. Intiraporn Mulasastra
Foreign Keys (FK)
OfferNo OffLocation OffTime CourseNo
111 CPE 203 1/2014 204351
222 CPE 204 2/2014 204351
333 CPE 204 2/2014 204111
CourseNo CrsDesc CrsUnits
204351 Database 3
204111 Programming 3
foreign key
Referential integrity
Values of FK must match values from a source table
Ensures valid references among tables
Course Offering
11 Asst.Prof. Intiraporn Mulasastra
enroll
Many to Many Relationship
12 Asst.Prof. Intiraporn Mulasastra
M-N Relationships
Rows of each table are related to
multiple rows of the other table
Not directly represented in the
relational model
We need
two 1-M relationships and
an associative table
13 Asst.Prof. Intiraporn Mulasastra
Relationships N:M
StdSSN
StdName
Student
OfferNo
OffLocation
Offering
EnrGrade
EnrollmentRegisters Grants
associative table
14 Asst.Prof. Intiraporn Mulasastra
enroll
Many to Many Relationship
StdSSN
StdName
Student
OfferNo
OffLocation
Offering
EnrGrade
EnrollmentRegisters Grants
transform
1:N N:1
N:M
15 Asst.Prof. Intiraporn Mulasastra
OfferNo CourseNo
1234 IS320
4321 IS320
Offering
StdSSN OfferNo
123-45-6789 1234
234-56-7890 1234
123-45-6789 4321
124-56-7890 4321
Enrollment
StdSSN StdLastName
123-45-6789 WELLS
124-56-7890 KENDALL
234-56-7890 NORBERT
Student
Many to Many Relationship
16 Asst.Prof. Intiraporn Mulasastra
Formal Definitions I
Superkey: column(s) with unique values
Candidate key: minimal superkey
A B C D 1 41 500 m 1 42 600 n 2 42 600 o 3 43 750 p
AB, AC, D
?
BC?
17 Asst.Prof. Intiraporn Mulasastra
Formal Definitions I
Primary key: a designated candidate key; cannot contain null values
Foreign key: column(s) whose values must match the values in a primary key of a referenced table
NULL value: special value meaning value unknown or inapplicable
18 Asst.Prof. Intiraporn Mulasastra
Formal Definitions II
Entity integrity
No two rows with the same primary key value
No null values in a primary key
Referential integrity
Foreign keys must match candidate key of source table
Foreign keys in some cases can be null
19 Asst.Prof. Intiraporn Mulasastra
Enrollment Table Example
OfferNo StdSSN EnrGrade
INTEGER CHAR(11) DECIMAL(3,2)
PRIMARY KEY
foreign key foreign key
OfferNo Student
StdSSN Offering
PRIMARY KEY PRIMARY KEY
20 Asst.Prof. Intiraporn Mulasastra
CourseNo
CrsDesc
CrsUnits
Course
OfferNo
OffLocation
OffTime
Offering
Has
teach
ONE
ONE
MANY
MANY
21 Asst.Prof. Intiraporn Mulasastra
Offering Table Example
OfferNo CourseNo ……… FacSSN
PRIMARY KEY
foreign key1 foreign key2
CourseNo Course
FacSSN Faculty
PRIMARY KEY PRIMARY KEY
22 Asst.Prof. Intiraporn Mulasastra
Self-Referencing Relationships
Foreign key that references the same
table
Represents relationships among
members of the same set
Not common but important when
occurring
23 Asst.Prof. Intiraporn Mulasastra
Hierarchical Data Display
098-76-5432
Leonard Vince
876-54-3210
Christopher Colan
654-32-1098
Leonard Fibon
543-21-0987
Victoria Emmanual
Supervice
Supervice
24 Asst.Prof. Intiraporn Mulasastra
Faculty Data
FacSSN FacFirstName FacLastName FacRank FacSalary FacSupervisor
098-76-5432 LEONARD VINCE ASST $35,000 654-32-1098
543-21-0987 VICTORIA EMMANUEL PROF $120,000
654-32-1098 LEONARD FIBON ASSC $70,000 543-21-0987
765-43-2109 NICKI MACON PROF $65,000
876-54-3210 CRISTOPHER COLAN ASST $40,000 654-32-1098
987-65-4321 JULIA MILLS ASSC $75,000 765-43-2109
FK refers to the same table
25 Asst.Prof. Intiraporn Mulasastra
Generalization Hierarchy Example
CASCADE DELETE for Foreign Keys
Table Attribute PK FK
Employee EmpNo, EmpName, EmpHireDate
EmpNo
-
SalaryEmp EmpNo, EmpSalary EmpNo
EmpNo
Hourly Emp EmpNo, EmpRate EmpNo
EmpNo
26 Asst.Prof. Intiraporn Mulasastra
Relationship Window with 1-M
Relationships
Student
Offering
Enrollment
Faculty
Faculty1
Course
MANY
27 Asst.Prof. Intiraporn Mulasastra
Referenced Rows
Referenced row
Foreign keys reference rows in the associated primary key table
Enrollment rows refer to Student and Offering
Actions on referenced rows
Delete a referenced row
Change the primary key of a referenced row
Referential integrity should not be violated
28 Asst.Prof. Intiraporn Mulasastra
Offering Table
OfferNo CourseNo ……… FacSSN
1 204351
PRIMARY KEY foreign key
CourseNo CourseName CourseUnit
204351 Database Systems 3
Course PRIMARY
KEY
foreign key
1. If 204351 is deleted
2. What will happen with OfferNo 1?
29 Asst.Prof. Intiraporn Mulasastra
Possible Actions on referenced rows
Restrict: do not permit action on the
referenced row
Cascade: perform action on related
rows e.g., deletion or Nullify: set foreign key of related row
to NULL only valid if foreign keys accept null
values
Default: set foreign keys to a default value
Ex. Course
Ex. Offering
30 Asst.Prof. Intiraporn Mulasastra
Data Retrieval
Relational Database
31 Asst.Prof. Intiraporn Mulasastra
Relational Algebra Overview
Table operators
Transform one or two tables into a new
table
Understand operators in isolation
Input, calculation, output
Classification
Table specific operators
Traditional set operators (,,)
Advanced operators
32 Asst.Prof. Intiraporn Mulasastra
1.Subset Operators
Restrict Project
Select columns
Select
rows
33 Asst.Prof. Intiraporn Mulasastra
Subset Operator Notes
Restrict (Select Data)
Logical expression as input
Example: OffDays = 'MW' AND OffTerm = 'SPRING' AND OffYear = 2003
Project
List of columns is input
Duplicate rows eliminated if present
Often used together
34 Asst.Prof. Intiraporn Mulasastra
2.Extended Cross Product
Builds a table consisting of all
combinations of rows from each of the
two input tables
Cross Product Table A, Table B
Table C
A
B
11
22 X
A 11
A 22
B 11
B 22 35 Asst.Prof. Intiraporn Mulasastra
Extended Cross Product Example
FacSSN
111-11-1111
222-22-2222
333-33-3333
FacSSN StdSSN
111-11-1111 111-11-1111
111-11-1111 444-44-4444
111-11-1111 555-55-5555
222-22-2222 111-11-1111
222-22-2222 444-44-4444
222-22-2222 555-55-5555
333-33-3333 111-11-1111
333-33-3333 444-44-4444
333-33-3333 555-55-5555
Faculty
Student
Faculty PRODUCT Student
StdSSN
111-11-1111
444-44-4444
555-55-5555
36 Asst.Prof. Intiraporn Mulasastra
3.Join Operator
Most databases have many tables
Combine tables using the join operator
Specify matching condition
Can be any comparison but usually =
PK = FK most common join condition
37 Asst.Prof. Intiraporn Mulasastra
Natural Join Example
FacSSN FacName
111-11-1111 joe
222-22-2222 sue
333-33-3333 sara
OfferNo FacSSN
1111 111-11-1111
2222 222-22-2222
3333 111-11-1111
FacSSN FacName OfferNo
111-11-1111 joe 1111
222-22-2222 sue 2222
111-11-1111 joe 3333
Faculty
Offering
Natural Join of Offering and
Faculty
Relationship diagram
Join condition:
Faculty.FacSSN = Offering.FacSSN
38 Asst.Prof. Intiraporn Mulasastra
4.Outer Join Overview
Join excludes non matching rows
Preserving non matching rows is
important in some problems
Outer join variations
Full outer join
One-sided outer join
Left
right
39 Asst.Prof. Intiraporn Mulasastra
3.Right Outer Join
4 of Outer Join Operators
1.Left Outer Join 2.Join
Matched rows
using the join
condition
Keep unmatched
rows of the left
table + (2)
Keep unmatched
rows of the right
table + (2)
4.Full outer join
40 Asst.Prof. Intiraporn Mulasastra
Left outer join
Builds a table consisting of all matched
rows of the two input tables
Keep unmatched rows of the left table too
Table A
ID Name
A xx
B yy
No ID
11 A
22 A
33 -
join ID Name No
A xx 11
A xx 22
B yy -
Table B matched
unmatched 41 Asst.Prof. Intiraporn Mulasastra
Left Outer Join
Faculty
FacSSN FacName
111-11-1111 JOE
222-22-2222 SUE
333-33-3333 SARA
Offering
OfferNo FacSSN
1111 111-11-1111
2222 222-22-2222
3333 111-11-1111
4444 -
Offering Left Join Faculty
OfferNo FacSSN FacName
1111 111-11-1111 JOE
2222 222-22-2222 SUE
3333 111-11-1111 JOE
4444 **Non matching row**
Non matching row
42 Asst.Prof. Intiraporn Mulasastra
Full Outer Join Example
FacSSN FacName
111-11-1111 joe
222-22-2222 sue
333-33-3333 sara
Offerno FacSSN
1111 111-11-1111
2222 222-22-2222
3333 111-11-1111
4444
FacSSN FacName OfferNo
111-11-1111 joe 1111
222-22-2222 sue 2222
111-11-1111 joe 3333
333-33-3333 sara
4444
Faculty
Offering
Outer Join of Offering and Faculty
matched
Unmatched L
Unmatched R
43 Asst.Prof. Intiraporn Mulasastra
Traditional Set Operators
A UNION B
A INTERSECT B
A MINUS B
A B 44 Asst.Prof. Intiraporn Mulasastra
Union Compatibility
Requirement for the traditional set
operators
Strong requirement
Same number of columns
Each corresponding column is compatible
Positional correspondence
Apply to similar tables by removing
columns first
45 Asst.Prof. Intiraporn Mulasastra
Summarize Operator
Decision-making operator
Compress groups of rows into calculated
values
Simple statistical (aggregate) functions
Not part of original relational algebra
46 Asst.Prof. Intiraporn Mulasastra
Summarize Example
StdSSN AVG(EnrGrade)
111-11-1111 3.4
222-22-2222 3.3
333-33-3333 3.0
StdSSN OfferNo EnrGrade
111-11-1111 1111 3.8
111-11-1111 2222 3.0
111-11-1111 3333 3.4
222-22-2222 1111 3.5
222-22-2222 3333 3.1
333-33-3333 1111 3.0
EnrollmentSUMMARIZE Enrollment
ADD AVG(EnrGrade)
GROUP BY StdSSN
47 Asst.Prof. Intiraporn Mulasastra
Divide Operator
Match on a subset of values
Suppliers who supply all parts
Faculty who teach every IS course
Specialized operator
Typically applied to associative tables
representing M-N relationships
48 Asst.Prof. Intiraporn Mulasastra
Division Example
SuppNo PartNo
s3 p1
s3 p2
s3 p3
s0 p1
s1 p2
PartNo
p1
p2
SuppNo
s3
SuppPart Part SuppPart DIVIDEBY Part
s3 {p1, p2, p3}
contains {p1, p2}
ตวัอย่างเช่น ลูกค้าคนใดซ้ือ นม และขนมปังด้วยในใบเสร็จเดยีวกนั
49 Asst.Prof. Intiraporn Mulasastra
Relational Algebra Summary Operator Meaning
Restrict (Select) Extracts rows that satisfy a specified condition
Project Extracts specified columns.
Product Builds a table from two tables consisting of all possible combinations
of rows, one from each of the two tables.
Union Builds a table consisting of all rows appearing in either of two tables
Intersect Builds a table consisting of all rows appearing in both of two specified
tables
Difference Builds a table consisting of all rows appearing in the first table but not
in the second table
Join Extracts rows from a product of two tables such that two input rows
contributing to any output row satisfy some specified condition.
Outer Join Extracts the matching rows (the join part) of two tables and the
“unmatched” rows from both tables.
Divide Builds a table consisting of all values of one column of a binary (2
column) table that match (in the other column) all values in a unary (1
column) table.
Summarize Organizes a table on specified grouping columns. Specified aggregate
computations are made on each value of the grouping columns.
50 Asst.Prof. Intiraporn Mulasastra
Summary
Relational model is commercially
dominant
Learn relations, primary keys and
foreign keys,
Learn to represent data relationships
Understand concept of data operations
51 Asst.Prof. Intiraporn Mulasastra