Lecture 2-Relational Model Concepts
Lecture 2-Relational Model Concepts
to that attribute
Example: The domain Date may be used to define two attributes
named “Invoice-date” and “Payment-date” with different meanings
Definition Summary
Informal Terms Formal Terms
Table Relation
Row Tuple
35
Objectives
Learn the types and the uses of constraints
constraints
Work with practical examples of creating,
constraints
36
Introduction to Constraints
Constraints:
Are rules or restrictions that guide database
inserts, updates, and deletions
Keep invalid or erroneous data out of the
database
Can be enforced by:
Declaring integrity constraints The focus
of this
Writing a database trigger chapter
Programming constraints into an application
37
Types of Constraints
Types of constraints:
• A brood group must be ‘A’ or ‘B’ or ‘AB’ or ‘O’ only (can not
be any other values else).
Domain Constraints
Since every attribute has an associated domain, there are
constraints (called domain constraints) in the form of
restrictions on the set of values allowed for the attributes of
relations. (A requirement that the values of an attribute must
come from a specific domain).
45
Key Constraints…1
Superkey: Refer to a set of attributes such that
no two tuples in any valid relation instance will
have the same value. That is, for any distinct
tuples t1 and t2 in r(R), t1[SK] t2[SK].
Key: A "minimal" superkey; that is, a superkey
K such that removal of any attribute from K
results in a set of attributes that is not a
superkey.
46
Key Constraints…2
Example: The CAR relation schema:
CAR(State, Reg#, SerialNo, Make, Model, Year)
has two keys
Key1 = {State, Reg#},
Key2 = {SerialNo}.
{SerialNo, Make} is a superkey but not a key.
If a relation has several candidate keys, one is
chosen arbitrarily to be the primary key. The
primary key attributes are underlined.
47
Entity Integrity:
48
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 (called foreign key attributes) that
reference the primary key attributes PK of the
referenced relation R2.
49
Example of PRIMARY KEY and FOREIGN KEY constraints
50
How to Create and Maintain Integrity
Constraints
51
Chapter Summary
Integrity constraints can be enforced using
declared constraints, triggers, or application
programming
A FOREIGN KEY constraint identifies a
parent/child relationship between two tables and
is defined on the child table
Constraints can be created with the CREATE
TABLE and the ALTER TABLE commands
Use the ALTER TABLE statement to rename,
drop, or change the state of a constraint
52
Chapter Summary
To remove the NOT NULL constraint, use
ALTER TABLE MODIFY (column...) statement
When a PRIMARY KEY constraint is created
(and not disabled), a unique index is created to
help enforce the constraint
Use the NOVALIDATE constraint state when you
do not want existing rows to be checked for
compliance with a constraint
The default states of a constraint are ENABLE,
VALIDATE, INITIALLY IMMEDIATE,
NOT DEFERRABLE, and NORELY
53
Chapter Summary
ENABLE … EXCEPTIONS into … can be used
after creating a table (usually called
EXCEPTIONS) to hold the rowid of rows that
violate a constraint
ON DELETE CASCADE and ON DELETE SET
NULL define the behavior of the database when a
parent row is deleted
The CHECK constraint can look for a specified
list of values or other simple expressions
54
In-Class Exercise
Consider the following relations for a database that keeps track of student
enrollment in courses and the books adopted for each course:
STUDENT(SSN, Name, Major, Bdate)
COURSE(Course#, Cname, Dept)
ENROLL(SSN, Course#, Quarter, Grade)
BOOK_ADOPTION(Course#, Quarter, Book_ISBN)
TEXT(Book_ISBN, Book_Title, Publisher, Author)
Draw a relational schema diagram specifying the foreign keys for this
schema.