Forum : On executing the DELETE statement I keep getting the error about foreign key constraint failing. What do I do?
Brief description  about Online courses   join in Online courses
View prabeen  patra 's Profile

On executing the DELETE statement I keep getting the error about foreign key constraint failing. What do I do?

On executing the DELETE statement I keep getting the error about foreign key constraint failing. What do I do?
Asked by prabeen patra | Mar 18, 2010 |  Reply now
Replies (1)
View teacher siliconindia 's Profile
Hi Prabeen,
What it means is that so of the data that you're trying to delete is still alive in another table. Like if you have a table for universities and a table for students, which contains the ID of the university they go to, running a delete on a university table will fail if the students table still contains people enrolled at that university. Proper way to do it would be to delete the offending data first, and then delete the university in question. Quick way would involve running SET foreign_key_checks=0 before the DELETE command, and setting the parameter back to 1 after the DELETE is done. If your foreign key was formulated with ON DELETE CASCADE, the data in dependent tables will be removed automatically.
Mar 18, 2010