Skip to content
CampusEduX

Cascade only the complete selected parent identity

Mediumsql
Learn the Concept: FOREIGN KEY

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.

sql
CREATE 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.

Related Problems

Input (stdin)

Output

Run your code to see output here...