Cascade only the complete selected parent identity
In fixed disposable database cxdbmslabfkpair, define persistent InnoDB departments with PRIMARY KEY(campusid,departmentid) and employees with required matching roles plus employeeid primary key. Declare a composite FOREIGN KEY with ON DELETE CASCADE. Reset only these disposable child then parent rows, copy compatible inputs, delete parent (1,10), and return surviving employeeid,campusid,departmentid. Never disable checks or touch application tables.
MySQL dialect.
sqlCREATE TABLE department_input (campus_id INT NOT NULL,department_id INT NOT NULL,PRIMARY KEY(campus_id,department_id)); CREATE TABLE employee_input (employee_id INT PRIMARY KEY,campus_id INT NOT NULL,department_id INT NOT NULL); INSERT INTO department_input VALUES (1,10),(2,10),(1,20); INSERT INTO employee_input VALUES (1,1,10),(2,2,10),(3,1,20);
Required ordering: Ascending employee_id.
Edge cases:
- Same local number on another campus survives.
- Absent selected parent makes the targeted delete a no-op.
INSERT INTO department_input VALUES (1,10),(2,10),(1,20); INSERT INTO employee_input VALUES (1,1,10),(2,2,10),(3,1,20);
employee_id campus_id department_id 2 2 10 3 1 20
Explanation: The native composite reference ties children to the correct campus-scoped department, so the cascade preserves other parent identities.
- Only the complete selected parent identity is deleted.
- The native child action is enforced by InnoDB.
Hint 1
List both child and parent roles in the same order.
Hint 2
Let the declared action handle matching children.
The native composite reference ties children to the correct campus-scoped department, so the cascade preserves other parent identities.
The native composite reference ties children to the correct campus-scoped department, so the cascade preserves other parent identities.
Related Problems
- Declare and exercise a native optional InnoDB referenceEasy · sqlSolve Problem
- Duplicate EnrollmentsMedium · sqlSolve Problem
- Employees With No Real DepartmentMedium · sqlSolve Problem