RI Constraints Flashcards
Example of defining RI constraint
CREATE TABLE Departments (
id INT PRIMARY KEY,
name VARCHAR(50)
);
CREATE TABLE Employees (
id INT PRIMARY KEY,
name VARCHAR(50),
department_id INT,
–if the PKey in Departments (parent) changes, the FKey here will change
FOREIGN KEY (department_id) REFERENCES Departments(id) ON UPDATE CASCADE
);
ON UPDATE CASCADE
“If the primary key in Table B (parent) changes, update the corresponding value in my foreign key column.”
ON DELETE CASCADE
“If a row in Table B (parent) is deleted, delete my related rows automatically.”
ON UPDATE SET NULL
“If the primary key in Table B (parent) changes, set my foreign key column to NULL”
ON DELETE SET NULL
“If the primary key in Table B (parent) gets deleted, set my foreign key column to NULL”
ON UPDATE SET DEFAULT
“If the primary key in Table B (parent) changes, set my foreign key column to its default value”
ON UPDATE RESTRICT
“If you try to update a primary key in Table B (parent) that I reference, stop the operation if it affects me”
ON UPDATE NO ACTION
“If the primary key in Table B (parent) changes, do nothing immediately, but enforce integrity later if needed”