Vous êtes sur la page 1sur 67

Database

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)

•  Relational database is a collection of tables


•  Heading: table name and column names
•  Body: rows, occurrences of data

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

Table-Oriented Set-Oriented Record-Oriented


Table Relation Record type, file

Row Tuple Record

Column Attribute Field


Integrity Rules
•  Entity integrity: primary keys
–  Each table has column(s) with unique values
–  Ensures entities are traceable
•  Referential integrity: foreign keys
–  Values of a column in one table match values
in a source table
–  Ensures valid references among tables
Functional Dependency
•  A functional dependency occurs when the value of one
(set of) attribute(s) determines the value of a second (set
of) attribute(s):
StudentID ! StudentName
StudentID ! (DormName, DormRoom, Fee)
•  The attribute on the left side of the functional
dependency is called the determinant.
•  Functional dependencies may be based on equations:
ExtendedPrice = Quantity X UnitPrice
(Quantity, UnitPrice) ! ExtendedPrice
•  Function dependencies are not equations!

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)

(Quantity, Price) ! (ExtendedPrice)

3-23
Identifying Functional
Dependencies
Do it for the following relation!
What Makes Determinant Values Unique?

•  A determinant is unique in a relation if and


only if, it determines every other column in
the relation.
•  You cannot find the determinants of all
functional dependencies simply by looking
for unique values in one column:
–  Data set limitations
–  Must be logically a determinant

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)

•  RENTAL_PROPERTY with surrogate key:


RENTAL_PROPERTY (PropertyID, 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:

DEPARTMENT (DepartmentName, BudgetCode, ManagerName)


EMPLOYEE (EmployeeNumber, EmployeeLastName,
EmployeeFirstName, DepartmentName)

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:

SKU_DATA (SKU, SKU_Description, Department, Buyer)


ORDER_ITEM (OrderNumber, SKU, Quantity, Price,
ExtendedPrice)

Where ORDER_ITEM.SKU must exist in SKU_DATA.SKU

3-33
Course Table Example

CREATE TABLE Course


( CourseNo CHAR(6),
CrsDesc VARCHAR(250),
CrsUnits SMALLINT,
CONSTRAINT PKCourse PRIMARY KEY(CourseNo),
CONSTRAINT UniqueCrsDesc UNIQUE
(CrsDesc) )
Enrollment Table Example
CREATE TABLE Enrollment
( OfferNo INTEGER,
StdNo CHAR(11),
EnrGrade DECIMAL(3,2),
CONSTRAINT PKEnrollment PRIMARY KEY
(OfferNo, StdNo),
CONSTRAINT FKOfferNo FOREIGN KEY (OfferNo)
REFERENCES Offering,
CONSTRAINT FKStdNo FOREIGN KEY (StdNo)
REFERENCES Student )
Offering Table Example
CREATE TABLE Offering
( OfferNo INTEGER,
CourseNo CHAR(6) CONSTRAINT OffCourseNoRequired
NOT NULL,
OffLocation VARCHAR(50),
OffDays CHAR(6),
OffTerm CHAR(6) CONSTRAINT OffTermRequired NOT NULL,
OffYear INTEGER CONSTRAINT OffYearRequired NOT NULL,
FacNo CHAR(11),
OffTime DATE,
CONSTRAINT PKOffering PRIMARY KEY (OfferNo),
CONSTRAINT FKCourseNo FOREIGN KEY (CourseNo)
REFERENCES Course,
CONSTRAINT FKFacNo FOREIGN KEY (FacNo)
REFERENCES Faculty )
Self-Referencing Relationships

•  Foreign key that references the same


table
•  Represents relationships among members
of the same set
•  Not common but important in specialized
situations
Faculty Data

FacNo 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
Hierarchical Data Display

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: do not permit action on the


referenced row
•  Cascade: perform action on related rows
•  Nullify: only valid if foreign keys accept null
values
•  Default: set foreign keys to a default value
SQL Syntax for Actions
CREATE TABLE Enrollment
( OfferNo INTEGER,
StdNo CHAR(11),
EnrGrade DECIMAL(3,2),
CONSTRAINT PKEnrollment PRIMARY KEY(OfferNo, StdNo),
CONSTRAINT FKOfferNo FOREIGN KEY (OfferNo)
REFERENCES Offering
ON UPDATE CASCADE,
CONSTRAINT FKStdNo FOREIGN KEY (StdNo)
REFERENCES Student
ON UPDATE CASCADE )
Relational Algebra Overview
•  Collection of table operators
•  Transform one or two tables into a new
table
•  Understand operators in isolation
•  Classification
–  Table specific operators
–  Traditional set operators
–  Advanced operators
Subset Operators

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 Faculty PRODUCT Student


FacNo
FacNo StdNo
111-11-1111 111-11-1111 111-11-1111
222-22-2222 111-11-1111 444-44-4444
333-33-3333 111-11-1111 555-55-5555
222-22-2222 111-11-1111
222-22-2222 444-44-4444
Student 222-22-2222 555-55-5555
StdNo 333-33-3333 111-11-1111
111-11-1111 333-33-3333 444-44-4444
444-44-4444 333-33-3333 555-55-5555
555-55-5555
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
–  Relationship diagram useful when combining
tables
Natural Join Operator
•  Most common join operator
•  Requirements
–  Equality matching condition
–  Matching columns with the same unqualified
names
–  Remove one join column in the result
•  Usually performed on PK-FK join columns
Natural Join 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

222-22-2222 sue 2222


Offering
OfferNo FacNo 111-11-1111 joe 3333
1111 111-11-1111
2222 222-22-2222
3333 111-11-1111
Visual Formulation of Join
Outer Join Overview

•  Join excludes non matching rows


•  Preserving non matching rows is important
in some business situations
•  Outer join variations
–  Full outer join
–  One-sided outer join
Outer Join Operators
Full outer join

Left Outer Join Join Right Outer Join

Unmatched rows Matched rows Unmatched rows


of the left table using the join of the right table
condition
Full Outer Join Example

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

222-22-2222 sue 2222


Offering
Offerno FacNo 111-11-1111 joe 3333
1111 111-11-1111
2222 222-22-2222 333-33-3333 sara
3333 111-11-1111
4444 4444
Visual Formulation of Outer Join
Traditional Set Operators

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

•  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
Division Example

SuppPart Part SuppPart DIVIDEBY Part


SuppNo PartNo PartNo SuppNo
s3 p1 p1 s3
s3 p2 p2
s3 p3
s0 p1
s3 {p1, p2, p3}
s1 p2 contains {p1, p2}
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.
Summary
•  Relational model is commercially dominant
•  Learn primary keys, data types, and
foreign keys
•  Visualize relationships
•  Understanding existing databases is
crucial to query formulation
Representation

RELATION_NAME(Attr01,Attr02,……,Attr0n)

RELATION_NAME(Column01,Column02,…..LastColumn)

RELATION_NAME(PrimaryKey, Attr02,…….,Attr0n)

RELATION_NAME(PrimaryKey, Attr02,…….,ForeignKey01,…., Attr0n)

RELATION_NAME(CompositeKey01, CompositeKey02, Atrr03, ……, Atrr0n)

3-67

Vous aimerez peut-être aussi