Académique Documents
Professionnel Documents
Culture Documents
Processing
Fundamentals, Design, and Implementa7on
Chapter Three:
The Rela-onal Model
3-1
Chapter Objectives
• To understand basic relational terminology
• To understand the characteristics of relations
• To understand alternative terminology used in describing
the relational model and integrity rules (entity and
referential integrity)
• To identify primary, candidate, and composite keys
• To be able to identify possible insertion, deletion, and
update anomalies in a relation
• To introduce the Relational Algebra
3-2
First
• We need to understand:
– The relational model
– Relational model terminology
3-3
The Relational Model
• Introduced in 1970
• Created by E.F. Codd
– He was an IBM engineer
– The model used mathematics known as
“relational algebra”
• Now the standard model for commercial
DBMS products.
3-4
Important Relational Model Terms
• Entity
• Relation
• Functional dependency
• Determinant
• Candidate key
• Composite key
• Primary key
• Surrogate key
• Foreign key
• Referential integrity constraint
• Normal form
• Multivalued dependency
3-5
Entity
• An entity is some identifiable thing that
users want to track:
– Customers
– Computers
– Sales
3-6
Relation
• Relational DBMS products store data about entities in relations,
which are a special type of table.
• A relation is a two-dimensional table that has the following
characteristics:
– Rows contain data about an entity.
– Columns contain data about attributes of the entity.
– All entries in a column are of the same kind.
– Each column has a unique name.
– Cells of the table hold a single value.
– The order of the columns is unimportant.
– The order of the rows is unimportant.
– No two rows may be identical.
3-7
A Relation
3-8
Tables That Are Not Relations:
Multiple Entries per Cell
3-9
Tables That Are Not Relations:
Table with Required Row Order
3-10
A Relation with Values
of Varying Length
3-11
Database Tables (Relations)
Student
StdNo StdFirstName StdLastName StdCity StdState StdZip StdMajor StdClass StdGPA
123-45-6789 HOMER WELLS SEATTLE WA 98121-1111 IS FR 3.00
124-56-7890 BOB NORBERT BOTHELL WA 98011-2121 FIN JR 2.70
234-56-7890 CANDY KENDALL TACOMA WA 99042-3321 ACCT JR 3.50
CREATE TABLE Statement
CREATE TABLE Student
( StdNo CHAR(11),
StdFirstName VARCHAR(50),
StdLastName VARCHAR(50),
StdCity VARCHAR(50),
StdState CHAR(2),
StdZip CHAR(10),
StdMajor CHAR(6),
StdClass CHAR(6),
StdGPA DECIMAL(3,2) )
Common Data Types
• CHAR(L)
• VARCHAR(L)
• INTEGER
• FLOAT(P)
• Date/Time: DATE, TIME, TIMESTAMP
• DECIMAL(W, R)
• BOOLEAN
Relationships
Student Offering
StdNo StdLastName OfferNo CourseNo
123-45-6789 WELLS 1234 IS320
124-56-7890 KENDALL 4321 IS320
234-56-7890 NORBERT
Enrollment
StdNo OfferNo
123-45-6789 1234
234-56-7890 1234
123-45-6789 4321
124-56-7890 4321
Alternative Terminology
3-18
Functional Dependencies Are Not Equations
ObjectColor ! Weight
ObjectColor ! Shape
ObjectColor ! (Weight, Shape)
3-19
Composite Determinants
• Composite determinant = a determinant
of a functional dependency that consists of
more than one attribute
(StudentName, ClassName) ! (Grade)
• Entity integrity
– No two rows with the same primary key value
– No null values in any part of a primary key
3-20
Functional Dependency Rules
• If A à (B, C), then A à B and Aà C.
• If (A,B) à C, then neither A nor B
determines C by itself.
3-21
Functional Dependencies in the
ORDER_ITEM Table
3-22
Functional Dependencies in the
ORDER_ITEM Table
(OrderNumber, SKU) !
(Quantity, Price, ExtendedPrice)
3-23
Identifying Functional
Dependencies
Do it for the following relation!
What Makes Determinant Values Unique?
3-25
Keys
• A key is a combination of one or more
columns that is used to identify rows in a
relation.
• A composite key is a key that consists of
two or more columns.
3-26
Candidate and Primary Keys
• A candidate key is a key that determines
all of the other columns in a relation.
• A primary key is a candidate key selected
as the primary means of identifying rows in
a relation.
– There is only one primary key per relation.
– The primary key may be a composite key.
– The ideal primary key is short, numeric, and
never changes.
3-27
Surrogate Keys
• A surrogate key is an artificial column
added to a relation to serve as a primary
key.
– DBMS supplied
– Short, numeric, and never changes—an ideal
primary key
– Has artificial values that are meaningless to
users
– Normally hidden in forms and reports
3-28
Surrogate Keys
NOTE: The primary key of the relation is underlined
below:
• RENTAL_PROPERTY without surrogate
key:
RENTAL_PROPERTY (Street, City,
State/Province, Zip/PostalCode, Country, Rental_Rate)
3-29
Foreign Keys
• A foreign key is the primary key of one
relation that is placed in another relation to
form a link between the relations.
– A foreign key can be a single column or a
composite key.
– The term refers to the fact that key values are
foreign to the relation in which they appear as
foreign key values.
3-30
Foreign Keys
NOTE: The primary keys of the relations are underlined
and any foreign keys are in italics in the relations below:
3-31
The Referential Integrity Constraint
• A referential integrity constraint is a
statement that limits the values of the
foreign key to those already existing as
primary key values in the corresponding
relation.
– Foreign keys must match candidate key of
source table
– Foreign keys can be null in some cases
– In SQL, foreign keys associated with primary
keys 3-32
Foreign Key with a
Referential Integrity Constraint
NOTE: The primary key of the relation is underlined and
any foreign keys are in italics in the relations below:
3-33
Course Table Example
543-21-0987
Victoria Emmanuel
654-32-1098
Leonard Fibon
098-78-5432 876-54-3210
Leonard Vince Cristopher Colan
Faculty Table Definition
CREATE TABLE Faculty
( FacNo CHAR(11),
FacFirstName VARCHAR(50) NOT NULL,
FacLastName VARCHAR(50) NOT NULL,
FacCity VARCHAR(50) NOT NULL,
FacState CHAR(2) NOT NULL,
FacZipCode CHAR(10)NOT NULL,
FacHireDate DATE,
FacDept CHAR(6),
FacSupervisor CHAR(11),
CONSTRAINT PKFaculty PRIMARY KEY (FacNo),
CONSTRAINT FKFacSupervisor FOREIGN KEY
(FacSupervisor) REFERENCES Faculty )
Relationship Window with
1-M Relationships
M-N Relationships
• Rows of each table are related to multiple
rows of the other table
• Not directly represented in the relational
model
• Use two 1-M relationships and an
associative table
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
Possible Actions
Restrict Project
Subset Operator Notes
• Restrict
– Logical expression as input
– Example: OffDays = 'MW' AND OffTerm =
'SPRING' AND OffYear = 2010
• Project
– List of columns is input
– Duplicate rows eliminated if present
• Often used together
Extended Cross Product
• Building block for join operator
• Builds a table consisting of all
combinations of rows from each of the two
input tables
• Produces excessive data
• Subset of cross product is useful (join)
Extended Cross Product
Example
Faculty
FacNo FacName
111-11-1111 joe Natural Join of Offering and Faculty
222-22-2222 sue
333-33-3333 sara FacNo FacName OfferNo
111-11-1111 joe 1111
Faculty
FacNo FacName
111-11-1111 joe Outer Join of Offering and Faculty
222-22-2222 sue
333-33-3333 sara FacNo FacName OfferNo
111-11-1111 joe 1111
A UNION B
A INTERSECT B
A MINUS B
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
Summarize Operator
• Decision-making operator
• Compresses groups of rows into
calculated values
• Simple statistical (aggregate) functions
• Not part of original relational algebra
Summarize Example
SUMMARIZE Enrollment
Enrollment ADD AVG(EnrGrade)
StdNo OfferNo EnrGrade GROUP BY StdNo
111-11-1111 1111 3.8
111-11-1111 2222 3.0 StdNo AVG(EnrGrade)
111-11-1111 3333 3.4 111-11-1111 3.4
222-22-2222 1111 3.5 222-22-2222 3.3
222-22-2222 3333 3.1 333-33-3333 3.0
333-33-3333 1111 3.0
Divide Operator
RELATION_NAME(Attr01,Attr02,……,Attr0n)
RELATION_NAME(Column01,Column02,…..LastColumn)
RELATION_NAME(PrimaryKey, Attr02,…….,Attr0n)
3-67