Integrity Constraints

Lecture 5 · integrity constraints guard against accidental damage to the database, by ensuring that authorized changes do not result in a loss of data consistency.

Examples of rules a constraint expresses

  • A checking account must have a balance greater than $10,000.00
  • The salary of a bank employee must be at least $4.00 an hour
  • A customer must have a (non-null) phone number
Two types of integrity constraint:
  1. Integrity constraints on a single relationnot null, primary key, unique, check (P).
  2. Referential integrity — a value appearing in one relation must also appear in another (foreign keys).

1 · Constraints on a single relation

not null

not null
name    varchar(20)   not null
budget  numeric(12,2) not null

unique (A1, A2, …, Am)

The unique specification states that the attributes A₁, A₂, … Aₘ form a candidate key.

Candidate keys are permitted to be null — in contrast to primary keys, which are automatically not null. This is exactly why the “one Active salary per employee” trick (a generated column that is NULL for inactive rows) works.

check (P) — where P is a predicate

check clause
create table section (
    course_id     varchar(8),
    sec_id        varchar(8),
    semester      varchar(6),
    year          numeric(4,0),
    building      varchar(15),
    room_number   varchar(7),
    time_slot_id  varchar(4),
    primary key (course_id, sec_id, semester, year),
    check (semester in ('Fall','Winter','Spring','Summer')
        or semester in ('F','W','Sp','S'))
);

Complex check clauses

  • check (time_slot_id in (select time_slot_id from time_slot)) — why not use a foreign key here instead?
  • “Every section has at least one instructor teaching the section.” — how would you write that?
Unfortunately: a subquery inside a check clause is not supported by pretty much any database. The alternative is a trigger. create assertion <name> check <predicate>; is also not supported by anyone. Triggers →

2 · Referential integrity

Ensures that a value that appears in one relation for a given set of attributes also appears for a certain set of attributes in another relation.

Formally: let A be a set of attributes, and R and S two relations containing A, where A is the primary key of S. Then A is a foreign key of R if for any value of A appearing in R, that value also appears in S.

instructor

IDnamedept_namesalary
1AbdurCSE10000
2amanCSE12500
3bilalEEE17000
4morshedEEE12000
5rashidBBA10000
6irshadBBA7800
7IrfanBiology15400
8MobassirBiology20100

department

IDdept_namedept_full_namebuilding
1CSEComputer Science and EngineeringB101
2EEEElectrical and Electronic EngineeringB200
3BBABachelor of Business AdministrationB201
The violation: instructors 7 and 8 have dept_name = 'Biology', but there is no Biology tuple in department. Referential integrity is broken — either insert (4, Biology, Department of Biology, B300) into department, or reject those two instructor rows.
declaring the foreign key
create table instructor (
    ID         char(5),
    name       varchar(20) not null,
    dept_name  varchar(20),
    salary     numeric(8,2),
    primary key (ID),
    foreign key (dept_name) references department
);

Cascading actions in referential integrity

Cascading actions are rules on a foreign key that tell the DB what to do in the child table when the referenced parent row is updated or deleted, so that referential integrity stays valid.
cascading
create table instructor (
    ID         char(5),
    name       varchar(20) not null,
    dept_name  varchar(20),
    salary     numeric(8,2),
    primary key (ID),
    foreign key (dept_name) references department
        on delete cascade
        on update cascade
);

Delete or update of the shared attribute in the department table will now automatically delete or update the value in the instructor table. Alternative actions to cascade: set null, set default.

ActionEffect on the childTypical use
ON DELETE CASCADEDelete the parent row → the child rows are deleted too.Attendance rows of a deleted employee.
ON UPDATE CASCADEChange the parent key → the child FK values follow automatically.Renaming a department code.
SET NULLThe child FK becomes NULL (the column must be nullable).An approver who leaves the university.
SET DEFAULTThe child FK reverts to its declared DEFAULT value.Moving orphans to a “General” department.
RESTRICT / NO ACTIONRefuse the delete/update while children still reference the row.A department that still has instructors.
Summary of the four classic integrity types:Domain (data type / check) · Entity (primary key = unique + not null) · Referential (foreign key) · User-defined (unique, business checks). Enforcing them in the DBMS rather than in application code means no application can ever bypass them.

Applied in the project: Task 2 — MySQL schema →