Reputation: 3
I'm performing CRUD in Spring MVC on two tables.My two table employeedetail [master table] and login[child table] with employeeid as foreign key. When i'm running my Query
Query 1.
DELETE e.* ,l.* FROM employeedetail e JOIN login l ON l.employeeid=e.employeeid WHERE l.employeeid=10;
Error:
Error Code: 1451. Cannot delete or update a parent row: a foreign key constraint fails (
springmvcmaven
.login
, CONSTRAINTemployee_login_id_fk
FOREIGN KEY (employeeid
) REFERENCESemployeedetail
(employeeid
) ON DELETE NO ACTION ON UPDATE NO ACTION)
Query 2:
DELETE e.* ,l.* FROM employeedetail e JOIN login l ON l.employeeid=e.employeeid WHERE employeeid=10;
ERROR :
Error Code: 1052. Column 'employeeid' in where clause is ambiguous
can any help me with this.
Upvotes: 0
Views: 1995
Reputation: 39196
Solution 1:-
In the foreign key constraints, you can declare it as ON DELETE CASCADE
. So that when the record in master table is deleted, all the records in the child table will be deleted automatically.
Solution 2:-
Delete the child table records i.e. login
table first
Delete the master table record i.e. employeedetail
table
DELETE FROM login l WHERE l.employeeid=10;
DELETE FROM employeedetail e WHERE e.employeeid=10;
Upvotes: 0