IntermediateBasic SELECT

Add a FOREIGN KEY constraint

The query

SQL
ALTER TABLE employees 
ADD CONSTRAINT fk_dept 
FOREIGN KEY (dept_id) 
REFERENCES departments(dept_id)
ON DELETE SET NULL;
Tested against PostgreSQL 16

Note

Enforces referential integrity. ON DELETE SET NULL means if the department is deleted, the employee\'s dept_id becomes NULL instead of deleting the employee.

Tables referenced