2.5 - Relational Model 2 - Integrity Constraints-1
2.5 - Relational Model 2 - Integrity Constraints-1
constraints
ISYS3414 – Practical Database Concepts
Database Design Process
Hardware and
Operating System
Information about the Problem
Characteristics
• Types of constraints:
oColumn; Key and Domain; Referential; User-defined 3
4
Domain and Column Constraints
• Domain Constraints
oEach column of a table has a data domain
oEvery value of a column has to belong to the respective data domain
oEx.: Student’s sid and age are Integers.
CREATE TABLE students (
• Column Constraints sid INTEGER,
oMore restrictive than Domain Constraints gname VARCHAR(30),
fname VARCHAR(30),
oEx.: Student’s sid is a positive Integer age INTEGER,
oEx.: Student’s gname is a string hold up to gpa REAL,
CHECK (sid > 0),
30 characters CHECK (gpa > 0.0)
);
CHECK constraints does
not work on MySQL 5
Key Constraints
6
Key Constraints
• Two ways to
specify
oWhen declaring
the column
oAs constraint
9
Referential Constraints
• Foreign Key
oColumns whose values are from a primary key of another table.
oEx.: working hours are only registered for existing employees
CREATE TABLE employee( CREATE TABLE work_hours_per_day(
id INTEGER PRIMARY KEY, id_employee INTEGER,
... day DATE,
); hours INTEGER,
CONSTRAINT pk_work_hours_per_day
The column id_employee of the table PRIMARY KEY (id_employee, day),
work_hours_per_day references the column CONSTRAINT fk_work_hours_per_day
id of the table employees. FOREIGN KEY (id_employee)
Each value of id_employee is a value that REFERENCES employee(id)
exists in the column id of the table employee. );
10
Referential Constraints
11
Referential Constraints
• Allows you to
delete data from
child tables
automatically when
you delete the
data from the
parent table.
• Example:
13
User-Defined Constraints
•Triggers
oProcedural code that is automatically executed when a
(particular) change is made in the database.
•Assertions
oDeclarative constraints that may involve many tables. 14
Integrity Constraints Violations - Running Example
15
Q&A
16