By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
CREATE TABLE
CREATE TABLE
CREATE FUNCTION
CREATE TRIGGER
CREATE FUNCTION
CREATE TRIGGER
| users_id | employee_nr | is_employee |
|---|---|---|
| 1 | null | f |
| 2 | null | f |
| 3 | 123 | t |
INSERT 0 3
| user_roles_id | users_id | role_name | is_employee |
|---|---|---|---|
| 1 | 1 | killer | null |
| 2 | 2 | burgler | null |
| 3 | 3 | thief | null |
| 4 | 3 | employee | t |
INSERT 0 4
ERROR: insert or update on table "user_roles" violates foreign key constraint "role_employee_requires_employee_nr_fk" DETAIL: Key (is_employee, users_id)=(t, 2) is not present in table "users".
DELETE 1
UPDATE 1
UPDATE 1
INSERT 0 1
| users_id | employee_nr | is_employee |
|---|---|---|
| 1 | null | f |
| 3 | null | f |
| 2 | 436 | t |
SELECT 3
| user_roles_id | users_id | role_name | is_employee |
|---|---|---|---|
| 1 | 1 | killer | null |
| 2 | 2 | burgler | null |
| 3 | 3 | thief | null |
| 6 | 2 | employee | t |
SELECT 4
ERROR: update or delete on table "users" violates foreign key constraint "role_employee_requires_employee_nr_fk" on table "user_roles" DETAIL: Key (is_employee, users_id)=(t, 2) is still referenced from table "user_roles".