Lecture 02 Relational Model
Lecture 02 Relational Model
Lecture 2
January 9, 2018 1
CS3200 – Database Design・ ・・ Spring 2018・ ・・ Derbinsky
A Quick Reminder
• One of the key features of a DBMS is use of data
models to support “data independence”
– The conceptual representation is independent of
underlying storage and/or operation implementation
ER Diagrams
Relations
January 9, 2018 2
CS3200 – Database Design・ ・・ Spring 2018・ ・・ Derbinsky
Outline
1. Model Concepts
2. Model Constraints
3. Data Modification and Constraint Violation
4. Transactions
January 9, 2018 3
CS3200 – Database Design・ ・・ Spring 2018・ ・・ Derbinsky
January 9, 2018 4
CS3200 – Database Design・ ・・ Spring 2018・ ・・ Derbinsky
Motivation
• A formal mathematical basis for databases
– Set theory and first-order predicate logic
– Allows scientists to advance theoretically
January 9, 2018 5
CS3200 – Database Design・ ・・ Spring 2018・ ・・ Derbinsky
Relational Database
A database consists of… Pop Quiz:
i. a set of relations (tables) What is a set?
ii. a set of integrity constraints
January 9, 2018 6
CS3200 – Database Design・ ・・ Spring 2018・ ・・ Derbinsky
A Relation
A relation consists of…
i. its schema, describing structure
ii. its state, or current populated data
STUDENT
Schema
Name SSN Phone Address Age GPA
Ben Bayer 305-61-2435 555-1234 1 Foo Lane 19 3.21
State Chung-cha Kim 422-11-2320 555-9876 2 Bar Court 25 3.53
Barbara Benson 533-69-1238 555-6758 3 Baz Blvd 19 3.25
January 9, 2018 7
CS3200 – Database Design・ ・・ Spring 2018・ ・・ Derbinsky
Relational Schema
• Relation name
STUDENT
January 9, 2018 8
CS3200 – Database Design・ ・・ Spring 2018・ ・・ Derbinsky
Relation State
• A set of n-tuples (rows)
– Each has a value in the domain of every
corresponding attribute (or NULL)
– Notation: r(NAME)
January 9, 2018 9
CS3200 – Database Design・ ・・ Spring 2018・ ・・ Derbinsky
Exercise
Diagrammatically produce a relation HAT according
to the following schema; the relation state should
have at least three tuples
January 9, 2018 10
CS3200 – Database Design・ ・・ Spring 2018・ ・・ Derbinsky
Answer
HAT
Team Size Color
RedSox M Red
Revolution S White
Bruins XL Yellow
January 9, 2018 11
CS3200 – Database Design・ ・・ Spring 2018・ ・・ Derbinsky
January 9, 2018 12
CS3200 – Database Design・ ・・ Spring 2018・ ・・ Derbinsky
NULL
• NULL is a special value
that may be in the
attribute domain
• Several possible
meanings
– E.g. unknown, not
available, does not
apply, undefined, …
• Best to avoid
– Else deal with caution
January 9, 2018 13
CS3200 – Database Design・ ・・ Spring 2018・ ・・ Derbinsky
January 9, 2018 14
CS3200 – Database Design・ ・・ Spring 2018・ ・・ Derbinsky
January 9, 2018 15
CS3200 – Database Design・ ・・ Spring 2018・ ・・ Derbinsky
CLASS CLASS
SSN Class SSN Class
305-61-2435 COMP355, 305-61-2435 COMP355
MATH 650
vs. 422-11-2320 COMP355
422-11-2320 COMP355,
BIOL110 533-69-1238 MATH650
January 9, 2018 16
CS3200 – Database Design・ ・・ Spring 2018・ ・・ Derbinsky
Model Constraints
Categories of restrictions on data in a
relational database
January 9, 2018 17
CS3200 – Database Design・ ・・ Spring 2018・ ・・ Derbinsky
Schema-Based Constraints
Can be directly expressed in schemas of
the data model, typically by specifying them
in the DDL (Data Definition Language)
• Domain
• Key
• Entity integrity
• Referential integrity
January 9, 2018 18
CS3200 – Database Design・ ・・ Spring 2018・ ・・ Derbinsky
Domain Constraints
Within each tuple, the value of each attribute A
must be an atomic value from the domain
dom(A)
January 9, 2018 19
CS3200 – Database Design・ ・・ Spring 2018・ ・・ Derbinsky
Key Constraints
A key is a set of attribute(s) satisfying two properties:
January 9, 2018 20
CS3200 – Database Design・ ・・ Spring 2018・ ・・ Derbinsky
Exercise
Is the following a valid state of DOCTOR?
DOCTOR
Number First Last Number First Last
1 William Hartnell 9 Christopher Eccleston
2 Patrick Troughton 10 David Tennant
3 Jon Pertwee 11 Matt Smith
4 Tom Baker 12 Peter Capaldi
5 Peter Davison 13 Jodie Whittaker
6 Colin Baker
7 Sylvester McCoy
8 Paul McGann
January 9, 2018 21
CS3200 – Database Design・ ・・ Spring 2018・ ・・ Derbinsky
Answer
Is the following a valid state of DOCTOR?
DOCTOR
Number First Last Number First Last
1 William Hartnell 9 Christopher Eccleston
2 Patrick Troughton 10 David Tennant
3 Jon Pertwee 11 Matt Smith
4 Tom Baker 12 Peter Capaldi
5 Peter Davison 13 Jodie Whittaker
6 Colin Baker
Underline = primary key
7 Sylvester McCoy Req #1: Two distinct tuples cannot have
8 Paul McGann identical values for all the attributes of the
key – NOT TRUE!
The Relational Data Model
January 9, 2018 22
CS3200 – Database Design・ ・・ Spring 2018・ ・・ Derbinsky
Exercise
List all candidate key(s) for the current state of
DOCTOR.
DOCTOR
Number First Last Number First Last
1 William Hartnell 9 Christopher Eccleston
2 Patrick Troughton 10 David Tennant
3 Jon Pertwee 11 Matt Smith
4 Tom Baker 12 Peter Capaldi
5 Peter Davison 13 Jodie Whittaker
6 Colin Baker
7 Sylvester McCoy
8 Paul McGann
January 9, 2018 23
CS3200 – Database Design・ ・・ Spring 2018・ ・・ Derbinsky
Answer
List all candidate key(s) for the current state of
DOCTOR.
DOCTOR
Number First Last Number First Last
1 William Hartnell 9 Christopher Eccleston
2 Patrick Troughton 10 David Tennant
3 Jon Pertwee 11 Matt Smith
4 Tom Baker 12 Peter Capaldi
5 Peter Davison 13 Jodie Whittaker
6 Colin Baker Candidate Key #1: { Number }
7 Sylvester McCoy Candidate Key #2: { First, Last }
8 Paul McGann
Why not { Last }, { Number, Last }?
January 9, 2018 24
CS3200 – Database Design・ ・・ Spring 2018・ ・・ Derbinsky
Entity Integrity
In a tuple, no attribute that is part of the PK
can be NULL
January 9, 2018 25
CS3200 – Database Design・ ・・ Spring 2018・ ・・ Derbinsky
Exercise
List all candidate key(s) for the current state of
DOCTOR.
DOCTOR
Number First Last Number First Last
1 William Hartnell 9 Christopher Eccleston
2 Patrick Troughton 10 David Tennant
3 Jon Pertwee 11 Matt Smith
4 Tom Baker 12 Peter Capaldi
5 Peter Davison 13 Jodie Whittaker
6 Colin Baker 14 NULL NULL
7 Sylvester McCoy
8 Paul McGann
January 9, 2018 26
CS3200 – Database Design・ ・・ Spring 2018・ ・・ Derbinsky
Answer
List all candidate key(s) for the current state of
DOCTOR.
DOCTOR
Number First Last Number First Last
1 William Hartnell 9 Christopher Eccleston
2 Patrick Troughton 10 David Tennant
3 Jon Pertwee 11 Matt Smith
4 Tom Baker 12 Peter Capaldi
5 Peter Davison 13 Jodie Whittaker
6 Colin Baker 14 NULL NULL
7 Sylvester McCoy PK = { Number }
8 Paul McGann
January 9, 2018 27
CS3200 – Database Design・ ・・ Spring 2018・ ・・ Derbinsky
Referential Integrity
All tuples in relation R1 must reference an existing
tuple in relation R2 (R1 may be the same as R2)
January 9, 2018 28
CS3200 – Database Design・ ・・ Spring 2018・ ・・ Derbinsky
Example
STUDENT
Name SSN Phone Address Age GPA BFF
DORM STUDENTOFTHEYEAR
SSN Dorm Year SSN
CLASS
SSN Class
January 9, 2018 29
CS3200 – Database Design・ ・・ Spring 2018・ ・・ Derbinsky
Exercise
January 9, 2018 30
CS3200 – Database Design・ ・・ Spring 2018・ ・・ Derbinsky
Answer
January 9, 2018 31
CS3200 – Database Design・ ・・ Spring 2018・ ・・ Derbinsky
Chinook
January 9, 2018 32
CS3200 – Database Design・ ・・ Spring 2018・ ・・ Derbinsky
January 9, 2018 33
CS3200 – Database Design・ ・・ Spring 2018・ ・・ Derbinsky
Insert
Domain
• An attribute value does not appear in the corresponding domain
(including NULL)
Key
• A key value already exists in another tuple
Entity Integrity
• Any part of the primary key is NULL
Referential Integrity
• Any value of any foreign key refers to a tuple that does not exist in
the referenced relation
January 9, 2018 34
CS3200 – Database Design・ ・・ Spring 2018・ ・・ Derbinsky
Delete
Referential Integrity
• Tuple being deleted is referenced by
foreign keys from other tuples
Possible actions
• Reject deletion
• Cascade (propagate deletion)
• Set default/NULL referencing attribute
values (careful with primary key)
The Relational Data Model
January 9, 2018 35
CS3200 – Database Design・ ・・ Spring 2018・ ・・ Derbinsky
Update
• If modifying neither part of primary key nor foreign
key, need only check…
– Domain
January 9, 2018 36
CS3200 – Database Design・ ・・ Spring 2018・ ・・ Derbinsky
Transactions
A transaction is a sequence of database
operations, including retrieval and update(s)
START
Read or write
Read or write
Read or write
…
COMMIT or ROLLBACK
The Relational Data Model
January 9, 2018 37
CS3200 – Database Design・ ・・ Spring 2018・ ・・ Derbinsky
A
tomicity. A transaction is an atomic unit of processing; it should either
be performed in its entirety or not performed at all.
C
onsistency. A transaction should be consistency preserving, meaning
that if it is completely executed from beginning to end without
interference from other transactions, it should take the database from
one consistent state to another.
I
solation. A transaction should appear as though it is being executed in
isolation from other transactions, even though many transactions are
executing concurrently. That is, the execution of a transaction should not
be interfered with by any other transactions executing concurrently.
D
urability. The changes applied to the database by a committed
transaction must persist in the database. These changes must not be
lost because of any failure.
January 9, 2018 38
CS3200 – Database Design・ ・・ Spring 2018・ ・・ Derbinsky
Exercise
1. For a balanced budget, incoming funds must always equal
outgoing payments at the end of the year
Consistency
January 9, 2018 39
CS3200 – Database Design・ ・・ Spring 2018・ ・・ Derbinsky
Summary
• The relational model dictates that a relational database consists of
(i) a set of relations and (ii) a set of integrity constraints
– All constraints met => database in a valid state
January 9, 2018 40