The Relational Data Model
The Relational Data Model
schema
o defined by the DB designer
o generally fixed once defined *
database state
o changes over time due to user updates
* schema modifications are possible once the database
is populated, but this generally causes difficulties
relation schema
RDM Schemas
External View
relation
specifications
mapping from
relations to
storage layout (files)
External View
Conceptual Schema
Internal Schema
External View
query processor
security manager
concurrency manager
index manager
database designer
enters the
definition of
relation schemas
SQL DDL = relation
definition language
(CREATE TABLE)
users of
the data
data
definition
processor
relation
schemas
relations
RELATION SCHEMAS
AND
RELATION INSTANCES
Relation Schemas
A relation is defined by
a name and
a set of attributes
Each attribute has a name and a domain
o a domain is a set of possible values
o types are domain names
o all domains are sets of atomic values
RDM does not allow complex data types
o domains may contain a special null value
set of
attributes
StockItem
Attribute
ItemID
Description
Price
Taxable
attribute
names
Domain
string(4)
string(50)
currency/dollars
boolean
attribute
domains
Definition: Relation
A relation is denoted by
r(R)
o R is the name of the relation schema for the
relation
Definition: Relation
Each tuple is an ordered list of n values
t = < v1, v2, , vn >
o n is the degree of R
dom(Ai)
Alternate notations:
ith value of tupleort is also referred to as
vi
t[Ai]
vi
t.Ai
Example Relation
r(STOCKITEM) =
{ < I119, "Monopoly", $29.95, true >,
< I007, "Risk", $25.45, true >,
< I801, "Bazooka Gum", $0.25, false > }
t2 = < I007, "Risk", $25.45, true >
t2[Price] = t2.Price = $25.45
Characteristics of Relations
A relation is a set
o tuples are unordered
o no duplicate tuples
Characteristics of Relations
Values in tuples are atomic
o atomic = non-structured
(similar to primitive types in C++)
o implication:
no nested relations or other complex data
structures
Description
Price
Taxable
I119
Monopoly
$29.95
True
I007
Risk
$25.45
True
I801
Bazooka Gum
$0.25
False
Example Relation
Example Schema
Example
State
CONSTRAINTS
Constraints
Constraints are restrictions on legal relation
states
o they add further semantics to the schema
Domain constraints
Non-null constraints
Key Constraints
By definition, all tuples in a relation are
unique
Often, we want to restrict tuples further such
that some subset of the attributes is unique
for all tuples
Example: in the StockItem relation,
no ItemID should appear in more than one
tuple
o ItemID is called a key attribute
key
key
candidate key
Example Keys
STOCKITEM( ItemId, Description, Price, Taxable )
superkeys:
(ItemId), (Description), (ItemId, Description)
keys:
(ItemId), (Description)
candidate keys:
(ItemId), (Description)
primary key:
(ItemId)
(assumes that
Description is
unique for all items)
Integrity Constraints
Entity integrity constraint
o no primary key value can be null
o the primary key is the tuple identifier
Referential Integrity
PK = primary key in R2
FK = foreign key in R1
dom(R1[FK]) = dom(R2[PK])
Referential Integrity:
Diagrammatic Representation
STOCKITEM( ItemId, Description, Price, Taxable )
PK
FK
STORESTOCK( StoreId, Item, Quantity )
FK
PK
STORE( StoreID, Manager, Address, Phone )
Referential Integrity:
Textual Representation
STOCKITEM( ItemId, Description, Price, Taxable )
STORESTOCK( StoreId, Item, Quantity )
STORE( StoreID, Manager, Address, Phone )
constraints:
STORESTOCK[StoreId] refers to STORE[StoreID]
STORESTOCK[Item] refers to STOCKITEM[ItemId]
Referential Integrity:
Example State
r(STORESTOCK) =
< "S002", "I065", 120 >,
< "S047", "I954", 300 >,
< "S002", "I954", 198 >
StoreId is a foreign key but not a key
all values in FK exist in PK
r(STORE) =
< "S002", "Tom", "112 Main", "999-8888" >,
< "S047", "Sasha", "13 Pine", "777-6543" >
Referential Integrity:
Constraint Violation
r(STORESTOCK) =
< "S002", "I065", 120 >,
< "S047", "I954", 300 >,
< "S333", "I954", 198 >
StoreId S333 does not exist in PK:
this is an illegal database state
r(STORE) =
< "S002", "Tom", "112 Main", "999-8888" >,
< "S047", "Sasha", "13 Pine", "777-6543" >
Both relation states are legal, but the database state is illegal.
Inserting Tuples
r1(STORESTOCK) =
r2(STORESTOCK) =
Deleting Tuples
r2(STORESTOCK) =
r3(STORESTOCK) =
Updating Tuples
r3(STORESTOCK) =
r3(STORESTOCK) =
Enforcing Constraints
constraint enforcement:
ensuring that no invalid database states can
exist
invalid state: a state in which a constraint is
violated
Possible ways to enforce constraints:
o reject any operation that causes a violation, or
o allow the violating operation and then attempt
to correct the database
delete
update
domain, non-null
yes
no
yes
key
yes
no
yes
entity integrity
yes
no
yes
referential integrity
yes/FK
yes/PK
yes/FK/PK
correction
domain, non-null
key
entity integrity
referential integrity
FK insertion
referential integrity
PK deletion
propagate delete to FK
(may cascade)
Schema for
Airline
Database
NEXT UP
skip ahead to Chapter 7:
Translating ER Schemas to Relational
Schemas
then back to Chapter 6:
The Relational Algebra: operations on
relations
PREVIEW: ER to Relational