简体   繁体   中英

Delete data from two tables

I wish to delete data from two tables i have: staff , and staff_take_courses . the SQLCommand line keeps giving the error:

ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`Exclusif`.`staff_take_courses`, CONSTRAINT `fk_staff_has_courses_staff1` FOREIGN KEY (`staff_id`) REFERENCES `staff` (`id`) ON DELETE NO ACTION O
N UPDATE NO ACTION)

here is the query:

  delete s, stc from staff s inner join staff_take_courses stc on stc.staff_id = s.id where s.id= '$id';

please help me out

Why can you just delete in two statement:

 DELETE FROM staff_take_courses WHERE staff_take_courses.staff_id= '$id';
 DELETE FROM staff WHERE staff.id= '$id';

There must be a problem with the relationship between the tables. That are not allowing you to delete from the tables

Delete from staff_take_courses where staff_take_courses.staff_id= '$id'; Delete from staff WHERE staff.id= '$id'; OR

Just use INNER JOIN as below

DELETE field1 , field2  FROM staff_take_courses  INNER JOIN staff  
WHERE staff_take_courses.staff_id= staff.id and staff_take_courses.staff_id = '1'

You cannot delete a table from database if that table have relationship with another table it will keep on giving you an error . Why not try to check your relationships?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM