Professional Documents
Culture Documents
CH 3
CH 3
CH 3
The
Relational
Database
Model Database Systems:
Design, Implementation, and Management
CHAPTER 3
The Relational Database Model
Database
Systems
1
Chapter Objectives
Chapter 3
The
Relational
The logical view of data as organized in the
Database
Model
relational model
Keys
Relational database operators
Database
Systems
2
A Logical View of Data
Chapter 3
The Entities, Attributes, and Entity Sets
Relational
Database An Entity is a person, place, event, or thing for
Model
which we intend to collect data.
Example:
Database
Systems
3
A Logical View of Data
Chapter 3
The Table
Relational
Database A Table contains a group of related entities -- i.e. an
Model
entity set.
It is also called a relation (Term borrowed from Set
theory).
Characteristics of a Relational Table
A table is composed of rows and columns.
Each row (tuple) represents a single entity within the
entity set.
Each column represents an attribute and is identified
by a distinct name.
Database
Systems
4
A Logical View of Data
Chapter 3
The Characteristics of a Relational Table
Relational
Database Each row/column intersection represents a single
Model
data value (atomic).
The number of tuples in a table is called its
cardinality.
The number of columns is known as its degree.
All values in a column must conform to the same
data format (type) and must be within a specified
range, known as the attribute domain.
Changing the order of the rows and/or columns does
not change the table.
Database
Systems
5
Example – Relational Table
Chapter 3
The
Relational
Database
Model
Database
Systems
6
Keys
Chapter 3
The Functional Dependence
Relational
Database Attribute B is functionally dependent on attribute A
Model
(attribute A determines attribute B: A -> B) if all the
rows in a table that agree in value for attribute A
must also agree in value for attribute B.
Example:
Database
Systems
7
Keys
Chapter 3
The A key that is composed of more than one attributes
Relational
Database
Model
is known as a composite key.
Example:
Database
Systems
8
Keys
Chapter 3
The Superkey: An attribute or a combination of
Relational
Database
Model
attributes that uniquely identifies each row in a
table.
Example:
Database
Systems
9
Keys
Chapter 3
The Primary Key
Relational
Database A candidate key selected to uniquely identify an
Model
entity.
Cannot have null values (A null value is no value, it
is NOT equal to a zero or a blank space).
Enforces Entity Integrity (Guarantees that each
entity is uniquely identified by a non-null primary
key value)
A primary key is a superkey as well as a candidate
key.
Example:
Database
Systems
10
Keys
Chapter 3
The Foreign Key
Relational
Database An attribute (or a combination of attributes) in one
Model
table whose values must either match the primary
key values in a designated table or be null.
Used to logically link one table with another
(compare with the physical pointers in Hierarchical
and Network models).
Enforces Referential Integrity (Guarantees valid
references to another table, i.e., cannot delete a tuple
from a table that is referenced by in another table
through a foreign key).
Database
Systems
11
Example – Integrity Constraints
Chapter 3
The
Relational
Database
Model
Database
Systems
12
Example – Integrity Constraints
Chapter 3
The
Relational
Database
Model
Database
Systems
13
Secondary Key
Chapter 3
The Secondary Key
Relational
Database Used for data retrieval purpose.
Model
Database
Systems
14
Relational Database Operators
Chapter 3
The These operators are based on relational algebra
Relational
Database
Model
theory.
They define functions to manipulate data in one or
more tables (relations).
Application of a relational operator to one or more
tables results in another table.
The eight relational operators are: UNION,
INTERSECT, DIFFERENCE, PRODUCT,
SELECT, PROJECT, JOIN, and DIVIDE.
To be considered minimally relational, a DBMS
must support SELECT, PROJECT and JOIN
Database
Systems
15
Relational Database Operators
Chapter 3
The Union Compatibility
Relational
Database Two tables are said to be union-compatible when
Model
they have the same degree (number of attributes),
say n, and the jth attributes (j in the range of 1 to n)
of the two tables are drawn from the same domain
(they need not have the same name).
Example:
Database
Systems
16
Union
Chapter 3
The The tables must be union compatible
Relational
Database
Model
A Union B results in C that contains all tuples from
both A and B with no duplicates.
Example:
A (All Insy) B (All Fina)
Name Major Gpa Name Major Gpa
John Insy 3.4 Jack Fina 4.0
Joe Insy 3.7 Jeb Fina 2.5
C (A Union B)
Name Major Gpa
John Insy 3.4
Joe Insy 3.7
Jack Fina 4.0
Database
Systems Jeb Fina 2.5
17
Intersect
Chapter 3
The The tables must be union compatible
Relational
Database
Model
A Intersect B results in C that contains tuples that
are common to both A and B.
Example:
C (A INTERSECT B)
Name Major Gpa
Database
Systems
Jill Insy 4.0
18
Difference
Chapter 3
The The tables must be union compatible
Relational
Database
Model
A MINUS B results in C that contains the tuples
that appear in A but not in B.
Example:
A (All Insy) B (High Achievers)
Name Major Gpa Name Major Gpa
John Insy 3.4 Jack Fina 4.0
Joe Insy 3.7 Jill Insy 4.0
Jill Insy 4.0 Jeb Mktg 3.98
Bob Insy 2.7
C (A Minus B)
Name Major Gpa
John Insy 3.4
Database
Systems
Joe Insy 3.7
Bob Insy 2.7 19
Product
Chapter 3
The
PRODUCT produces a list of all possible pairs of rows from two tables.
Relational If table A has 5 rows and B has 10, A product B will yield a table with 50 rows.
Database
Example:
Model
A (Item) B (Supplier)
Inumber IName SName SCity
101 Sweat Shirt WalMart Dallas
205 Trousers Kmart Phoenix
C (A Product B)
Inumber IName SName SCity
101 Sweat Shirt WalMart Dallas
101 Sweat Shirt Kmart Phoenix
Database
Systems 205 Trousers WalMart Dallas
20
205 Trousers Kmart Phoenix
Select
Chapter 3
The
Relational
Database SELECT yields all attributes of selected tuples that satisfy a
Model
specified condition.
It produces a horizontal subset of a table.
Database
Systems
21
Project
Chapter 3
The
Relational PROJECT produces a list of all values for selected
Database
Model
attributes.
It yields a vertical subset of a table.
Database
Systems
22
Join
Chapter 3
The
Relational JOIN allows us to combine information from two
Database
Model or more tables.
The tables participating in the join operation must
have attributes defined over a common domain.
EquiJoin: Compares specified columns of two
tables based on equality condition. The result is a
wider table where each row is formed by
concatenating two rows, one from each table, such
that the two rows have the same values in these two
columns.
Performed by a Product followed by a Select.
Database
Systems
23
JOIN - Example
Chapter 3
The
Relational
Database
Model
Database
Systems
24
JOIN - Example
Chapter 3
The
Relational
Database
Model
Database
Systems
25
JOIN - Example
Chapter 3
The
Relational
Database
Model
Database
Systems
26
JOIN - Example
Chapter 3
The
Relational
Database
Model
Database
Systems
27
Join
Chapter 3
The
Relational Natural join: EquiJoin with the duplicate column removed.
Database
Model
Performed by a Project on the result of equijoin. (aka Inner
Join)
When the term Join is mentioned without any prefix, it is
implied to be Natural Join.
Outer Join: Unmatched rows from the participating tables
are retained in the result table with unmatched attributes left
blank or null.
Left Outer join keeps all tuples from the left relation
Right Outer Join keeps all tuples from the right relation
Theta Join: EquiJoin with the equality operator replaced by
any other comparison operator, such as greater than, less
than, etc.
Database
Systems
28
Left Outer Join - Example
Chapter 3
The
Relational
Database
Model
Database
Systems
29
Right Outer Join - Example
Chapter 3
The
Relational
Database
Model
Database
Systems
30
Divide
Chapter 3
The
Relational Consider dividing a relation A with two attributes X
Database
Model and Y by a relation B with a single attribute Y.
Note that attribute Y is common to both A and B.
A can be thought of as a set of pairs of values <x,y>
and B as a set of single values <y>.
The result of dividing A by B is C, a set of values of
x such that the pair <x,y> appears in A for all values
of y appearing in B.
In general relation A can be of degree m+n, and
relation B can be of degree n.
Database
Systems
31
Divide
Chapter 3
The Examples of Divide
Relational
Database
Model A (Supplier-Part) B1 (Part) C1(A Divided by B1)
Sup# Part# Part# Sup#
S1 P1 P1 S1
S1 P2 S2
S1 P3
S1 P4 B2 (Part) C2 (A Divided by B2)
S1 P5 Part# Sup#
S1 P6 P2
S2 P1 P4
S2 P2
S3 P2 B3 (Part) C3 (A Divided by B3)
S4 P2 Part# Sup#
S4 P4 P1
S4 P5 P2
Database
Systems
P3
32
The Data Dictionary
Chapter 3
The Data dictionary contains metadata that describes
Relational
Database
Model
the data stored in the database.
It stores:
the names of the data items in the database
the types and sizes of the data items
the constraints on each data item
the names of authorized users, the data items that
each user can access, and the types of access
allowed.
Database
Systems
33
Example - Data Dictionary
Chapter 3
The
Relational
Database
Model
Database
Systems
34
The System Catalog
Chapter 3
The System catalog is a very detailed system data
Relational
Database
Model
dictionary. It describes all objects within the
database.
System catalog is a system-created database whose
tables store the database characteristics and
contents.
System catalog tables can be queried just like any
other tables.
System catalog automatically produces database
documentation.
All data dictionary information are found in the
system catalog.
Database
Systems
35
Data Redundancy Revisited
Chapter 3
The The relational database model does not completely
Relational
Database
Model
eliminate data redundancy but uses controlled data
redundancy.
Foreign keys create redundant data, but serve the
useful purpose of maintaining referential integrity.
They are also used in Join operations.
Sometimes user requirements demand storing
apparently redundant data, such as storing the sale
price in Invoice Line Item. A deeper analysis
reveals no redundancy (See the Invoice example).
Database
Systems
36
Data Redundancy Revisited
Chapter 3
The
Relational
Database
Model
Database
Systems
37
Data Redundancy Revisited
Chapter 3
The
Relational
Database
Model
In this example Product Price is copied from the PRODUCT table to the LINE table. Does this
example actually carry redundant data?
Database
Systems
38