5. Relational Model (1)
5. Relational Model (1)
Database Systems
Lecture 5
Slide 2-2
Chapter Outline
⚫ Relational Model Concepts
⚫ Relational Model Constraints and Relational Database
Schemas
⚫ Update Operations and Dealing with Constraint
Violations
Slide 2-3
Relational Model Concepts
• The relational Model of Data is based on the
concept of a Relation.
• A Relation is a mathematical concept based on the
ideas of sets.
• The strength of the relational approach to data
management comes from the formal foundation
provided by the theory of relations.
• We review the essentials of the relational approach
in this lecture.
Slide 2-4
Relational Model Concepts
⚫ The model was first proposed by Dr. E.F. Codd of
IBM in 1970 in the following paper:
"A Relational Model for Large Shared Data
Banks," Communications of the ACM, June 1970.
Slide 2-5
INFORMAL DEFINITIONS
⚫ A relation is a two-dimensional table:
– Relation ≈ table.
– Attribute ≈ column name.
– Tuple ≈ row (not the header row).
⚫ Database ≈ collection of relations.
⚫ A relation has two parts:
Schema defines column heads of the table (attributes).
Instance contains the data rows (tuples, rows, or records) of the table.
Slide 2-6
FORMAL DEFINITIONS
⚫ A Relation may be defined in multiple ways.
⚫ The Schema of a Relation: R (A1, A2, .....An)
Relation schema R is defined over attributes A1, A2, .....An
For Example -
CUSTOMER (Cust-id, Cust-name, Address, Phone#)
Slide 2-7
FORMAL DEFINITIONS
⚫ A tuple is an ordered set of values
⚫ <632895, "John Smith", "101 Main St. Atlanta, GA 30332", "(404) 894-2000">
Slide 2-8
DEFINITION SUMMARY
Table Relation
Column Attribute/Domain
Row Tuple
Values in a column Domain
Table Definition Schema of a Relation
Slide 2-9
Example - Figure 5.1
Slide 2-10
CHARACTERISTICS OF
RELATIONS
⚫ Ordering of tuples in a relation r(R): The tuples are not
considered to be ordered, even though they appear to be in
the tabular form.
⚫ Ordering of attributes in a relation schema R (and of
values within each tuple): We will consider the attributes
in R(A1, A2, ..., An) and the values in t=<v1, v2, ..., vn> to
be ordered .
(However, a more general alternative definition of relation
does not require this ordering).
⚫ Values in a tuple: All values are considered atomic
(indivisible). A special null value is used to represent
values that are unknown or inapplicable to certain tuples.
Slide 2-11
CHARACTERISTICS OF
RELATIONS
⚫ Each relation in the same relational database schema has a
distinct name
Slide 2-12
Degree and Cardinality
Student Course Grade
Hermione Grainger Database A-
Draco Malfoy Database B
Harry Potter Database A
Ron Weasley Database C
Slide 2-13
Relational Model Integrity
Constraints
⚫ Constraints are conditions that must hold
on all valid relation instances. There are
three main types of constraints:
1. Key constraints
2. Entity integrity constraints
3. Referential integrity constraints
• ICs are specified when schema is defined.
• ICs are checked when relations are modified.
Slide 2-14
Key Constraint
⚫ Supper key
⚫ Candidate Key
⚫ Primary Key
⚫ Composite Keys
⚫ Foreign Key
Slide 2-15
Supper Key
⚫ A Supper key (SK) is an attribute or a set of attributes that uniquely
identify a tuple within a relation.
– Example:
• SS#, Name, Age, License#, Salary
• SS#, Name, Age, License#
• SS#, Name, Age
• SS#, Name
• SS#
➢ Name, Age, Salary (not a super key) ???
➢ Name, Age, Salary, License# (A supper key) ???
Employee
SS# Name Age License# Salary
Slide 2-16
Primary Key
⚫ A Primary key (PK) is a minimal supper key that is used to
identify tuples uniquely. This means that no subset of the primary
key is sufficient to provide unique identification of tuples.
⚫ Exercise:
Slide 2-17
Foreign Key
⚫ A Foreign key (FK) is an attribute(s) in one relation
that matches the primary key of another relation.
Department
Dept.# Dept. Name Foreign Key
Employee
SS# Name Age License# Salary Dept.#
Slide 2-18
Entity Integrity
⚫ Relational Database Schema: A set S of relation schemas
that belong to the same database. S is the name of the
database.
S = {R1, R2, ..., Rn}
⚫ Entity Integrity: The primary key attributes PK of each
relation schema R in S cannot have null values in any tuple
of r(R). This is because primary key values are used to
identify the individual tuples.
t[PK] null for any tuple t in r(R)
⚫ Note: Other attributes of R may be similarly constrained
to disallow null values, even though they are not members
of the primary key.
Slide 2-19
Referential Integrity
⚫ A constraint involving two relations (the previous constraints
involve a single relation).
⚫ Used to specify a relationship among tuples in two relations:
the referencing relation and the referenced relation.
⚫ Tuples in the referencing relation R1 have attributes FK that
reference the primary key attributes PK of the referenced
relation R2.
⚫ A tuple t1 in R1 is said to reference a tuple t2 in R2 if
t1[FK] = t2[PK].
⚫ A referential integrity constraint can be displayed in a
relational database schema as a directed arc from R1.FK to R2.
Slide 2-20
Referential Integrity
Constraint
Statement of the constraint
The value in the foreign key column(s) FK of
the referencing relation R1 can be either:
(1) a value of an existing primary key value of
the corresponding primary key PK in the
referenced relation R2,, or..
(2) a null.
In case (2), the FK in R1 should not be a part of its
own primary key. WHY ??????????
Slide 2-21
Other Types of Constraints
Semantic Integrity Constraints:
- based on application semantics and cannot
be expressed by the model
- E.g., “the max. no. of hours per employee
for all projects he or she works on is 56 hrs
per week”
- A constraint specification language may
have to be used to express these
- SQL-99 allows triggers and ASSERTIONS
to allow for some of these
Slide 2-22
Slide 2-23
Slide 2-24
Slide 2-25
Relational Data Model
Operations
⚫There are two categories of relational data
model operations:
– Retrieval operations extract information from the
relational database.
– Update operations causes the relation (and the
relational database) state changes. They Include:
⚫ Insert a tuple
⚫ Delete a tuple
⚫ Modify a value
Slide 2-26
Relational Data Model
Operations
⚫ Integrity constraints should not be violated by the update
operations.
Slide 2-27
Violating Integrity Constraints
Slide 2-28