Lecture
Lecture
Lecture (03)
University of Khartoum - Faculty of Mathematical Sciences and Informatics - Alargam Elrayah - 2024
Chapter 3 Outline
• Relational Model Concepts
• Relational Model Constraints and Relational
Database Schemas
• Update Operations and Dealing with Constraint
Violations
2
Relational Model Concepts
• The relational Model of Data is based on the concept of a
Relation
– Has a formal mathematical foundation provided by set
theory and first order predicate logic
• We review the essentials of the formal relational model in
this chapter
• In practice, there is a standard model based on SQL
(Structured Query Language) – described in Chapters 4
and 5
• Note: There are several important differences between
the formal model and the practical model, as we shall see
4
Informal Definitions
6
Informal Definitions
• Key of a Relation:
– Each row (tuple) in the table is uniquely identified by
the value of a particular attribute (or several
attributes together)
• Called the key of the relation
– In the STUDENT relation, SSN is the key
– If no attributes posses this uniqueness property, a
new attribute can be added to the relation to assign
unique row-id values (e.g. unique sequential
numbers) to identify the rows in a relation
• Called artificial key or surrogate key
10
Formal Definitions – State of a Relation
11
12
Formal Definitions - Example
• Let R(A1, A2) be a relation schema:
– Let dom(A1) = {0,1}
– Let dom(A2) = {a,b,c}
• Then: The Cartesian product dom(A1) X dom(A2)
contains all possible tuples from these domains:
{<0,a> , <0,b> , <0,c>, <1,a>, <1,b>, <1,c> }
• The relation state r(R) dom(A1) X dom(A2)
• For example: One possible state r(R) could be {<0,a> ,
<0,b> , <1,c> }
– This state has three 2-tuples: <0,a> , <0,b> , <1,c>
13
16
17
• Values in a tuple:
– All values are considered atomic (indivisible).
– Each value must be from the domain of the
attribute for that column
• If tuple t = <v1, v2, …, vn> is a tuple (row) in the
relation state r of R(A1, A2, …, An)
• Then each vi must be a value from dom(Ai)
18
Characteristics Of Relations (cont.)
• Notation:
– We refer to component values of a tuple t by:
• t[Ai] or t.Ai
• This is the value vi of attribute Ai for tuple t
– Similarly, t[Au, Av, ..., Aw] refers to the subtuple of
t containing the values of attributes Au, Av, ..., Aw,
respectively in t
19
20
Key Constraints
• Superkey SK of R:
– Is a set of attributes SK of R with the following condition:
• No two tuples in any valid relation state r(R) will have the
same value for SK
• That is, for any distinct tuples t1 and t2 in r(R), t1.SK t2.SK
• This condition must hold in any valid state r(R)
• Key K of R:
– Is a "minimal" superkey
– Formally, a key K is a superkey such that removal of any
attribute from K results in a set of attributes that is not a
superkey (or key) any more (does not possess the superkey
uniqueness property)
– Hence, a superkey with one attribute is always a key
21
22
Key Constraints (cont.)
• If a relation has several keys, they are called candidate
keys; one is chosen to be the primary key; the others
are called unique (or secondary) keys
– The primary key attributes are underlined.
• Example: Consider the CAR relation schema:
– CAR(State, Reg#, SerialNo, Make, Model, Year)
– We choose License_number (which contains (State, Reg#)
together) as the primary key – see Figure 3.4
• The primary key value is used to uniquely identify each
tuple in a relation
– Provides the tuple identity
– Also used to reference the tuple from other tuples
• General rule: Choose the smallest-sized candidate key (in
bytes) as primary key
– Not always applicable – choice is sometimes subjective (as
in Figure 3.4 – see next slide)
23
24
Relational Database Schema
• Relational Database Schema:
– A set S of relation schemas that belong to the
same database.
– S is the name of the whole database schema
– S = {R1, R2, ..., Rn}
– R1, R2, …, Rn are the names of the individual
relation schemas within the database S
• Figure 3.5 shows a COMPANY database schema
with 6 relation schemas
25
26
Relational Database State
• Next two slides show an example of a COMPANY
database state (Figure 3.6)
– Each relation has a set of tuples
27
30
Referential Integrity Constraint
• A constraint involving two relations
– The previous constraints (key, entity integrity)
involve a single relation.
• Used to specify a relationship among tuples in
two relations:
– The referencing relation and the referenced
relation.
31
32
33
34
Other Types of Constraints
• Semantic Integrity Constraints:
– cannot be expressed by the built-in model
constraints
– Example: “the max. no. of hours per employee for
all projects he or she works on is 56 hrs per week”
• A constraint specification language can be
used to express these
• SQL has TRIGGERS and ASSERTIONS to
express some of these constraints (see Section
5.2)
35
Exercise
36
Relational Update Operations
• Each relation will have many tuples in its current relation
state
• The relational database state is a union of all the
individual relation states at a particular time
• Whenever the database is changed, a new state arises
• Basic operations for changing the database:
– INSERT new tuples in a relation
– DELETE existing tuples from a relation
– UPDATE attribute values of existing tuples
37
39
INSERT operation
• INSERT one or more new tuples into a relation
• INSERT may violate any of the constraints:
– Domain constraint:
• if one of the attribute values provided for a new tuple is not of
the specified attribute domain
– Key constraint:
• if the value of a key attribute in a new tuple already exists in
another tuple in the relation
– Referential integrity:
• if a foreign key value in a new tuple references a primary key
value that does not exist in the referenced relation
– Entity integrity:
• if the primary key value is null in a new tuple
40
DELETE operation
• DELETE one or more existing tuples from a relation
• DELETE may violate only referential integrity:
– If the primary key value of the tuple being deleted is
referenced from other tuples in the database
• Can be remedied by several actions: RESTRICT, CASCADE,
SET NULL (see Chapter 4 for more details)
– RESTRICT option: reject the deletion
– CASCADE option: propagate the deletion by automatically
deleting the referencing tuples
– SET NULL option: set the foreign keys of the referencing tuples
to NULL (the foreign keys cannot have NOT NULL constraint)
– One of the above options must be specified during database
design for each referential integrity (foreign key) constraint
41
UPDATE operation
• UPDATE modifies the values of attributes in one or more
existing tuples in a relation
• UPDATE may violate domain constraint and NOT NULL
constraint on an attribute being modified
• Other constraints may also be violated:
– Updating the primary key (PK):
• Similar to a DELETE followed by an INSERT
• Need to specify similar options to DELETE
• The CASCADE option propagates the new value of PK to the
foreign keys of the referencing tuples automatically
– Updating a foreign key (FK) may violate referential integrity
– Updating an ordinary attribute (neither PK nor FK):
• Can only violate domain or NOT NULL constraints
42
Summary
• Presented Relational Model Concepts
– Definitions
– Characteristics of relations
• Discussed Relational Model Constraints and Relational
Database Schemas
– Domain constraints’
– Key constraints
– Entity integrity
– Referential integrity
• Described the Relational Update Operations and Dealing
with Constraint Violations
43