Given the following tables:
create table permissions(id identity);
create table companies(id identity,
permission_id bigint not null,
foreign key(permission_id) references permissions(id) on delete cascade);
create table departments(id identity,
company_id bigint not null,
permission_id bigint not null,
foreign key(company_id) references companies(id),
foreign key(permission_id) references permissions(id) on delete cascade);
When a department is deleted, I want the following statements to execute atomically:
departments
row is deletedpermissions
row associated with the department row is deletedcompanies
row associated with the department row is deletedpermissions
row associated with the company (previous point) is deletedQuestions:
READ_COMMITTED
transaction isolation and ON CASCADE DELETE
on all foreign keys, are the rows deleted as a single atomic statement? Or are they executed as separate delete statements which are vulnerable to READ_COMMITTED
anomalies?CLARIFICATIONS:
permissions
forms a Closure Table (e.g. a user has permission to a company, and a company has permission to a department, therefore a user has permission to the department). Because the hierarchical relationship spans different types (i.e. users, companies, departments) the permissions table cannot point to a specific type. Therefore, company/department must reference a permission and not the other way around.CASCADE
take care of the rest because it'll delete the Department's permission, the Department and Company but it'll neglect to delete the company's Permission.companies
row I am deleting. The second connection (trigger) attempts to delete the permissions
row associated with the company but ON CASCADE DELETE
requires it to lock the company as well.It is atomic in most of the databases except mysql with MyISAM and perhaps MS Access, and generally you could do what you want with triggers, but It is weird because usually is the opposite what we want, I mean when one permission is deleted then the the departments that have that permission are deleted too