Lecture 04 The Relational Data Model
Lecture 04 The Relational Data Model
Lecture 4 – Chapter 5
The Relational Data Model
Edgar F. Codd
He received
the ACM Turing Award
in 1981.
● schema
○ defined by the DB designer
○ generally fixed once defined *
● database state
○ changes over time due to user updates
relation
specifications Conceptual Schema
application
application
application
program(s)
application
program(s) users of
program(s)
program(s) the data
query processor
security manager
database designer concurrency manager
enters the index manager
definition of
relation schemas
data
SQL DDL = relation definition relation
definition language processor relations
schemas
(CREATE TABLE)
Relation Schemas
and
Relation Instances
Relation Schemas
● A relation is defined by
a name and
a set of attributes
relation
name StockItem
Attribute Domain
ItemID string(4)
set of Description string(50)
attributes
Price currency/dollars
Taxable boolean
attribute attribute
names domains
Definition: Relation Schema
● Relation Schema
R(A1, A2, … , An)
○ R is the relation name
dom(Ai)
● degree = the number of attributes
Example Relation Schema
dom(ItemId) = string(4)
dom(Description) = string(50)
dom(Price) = currency/dollars
dom(Taxable) = boolean
degree of STOCKITEM = 4
Definition: Relation
● A relation is denoted by
r(R)
○ R is the name of the relation schema for the relation
○
t = < v 1, v 2 , … , v n >
n is the degree of R
vi є dom(Ai)
● Alternate notations:
ith value of tuple t is also referred to as
vi = t[Ai] or vi = t.Ai
Example Relation
r(STOCKITEM) =
{ < I119, "Monopoly", $29.95, true >,
< I007, "Risk", $25.45, true >,
< I801, "Bazooka Gum", $0.25, false > }
○ implication:
no nested relations or other complex data structures
● Domain constraints
vi є dom(Ai)
○ values for an attribute must be from
the domain associated with the attribute
● Non-null constraints
○ the domain of some attributes may not include null,
implying that a value for that attribute
is required for all tuples
Key Constraints
● By definition, all tuples in a relation are unique
● Example:
key key
FK
STORESTOCK( StoreId, Item, Quantity )
FK
PK
STORE( StoreID, Manager, Address, Phone
)
Referential Integrity:
Textual Representation
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
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.
Schema with FKs
State Change and
Constraint
Enforcement
Causes of Constraint Violations
● What can cause a
referential integrity constraint violation?
○ inserting a tuple in R1 with an illegal FK
violation correction
● Relation schema
○ list of attributes: name and domain constraint
○ superkeys: key constraints
○ primary key: entity integrity constraint