Cannot delete or update a parent row: a foreign key constraint fails solution, constraintfails
When I use the delete from table statement to clear data in a table, the following error occurs: Cannot delete or update a parent row: a foreign key constraint fails.
Paste my database first.
Tb_device table
Tb_device_status_now table
Question:
At first, I cleared the tb_device directly and reported an error. Check that fields in tb_device_status_now are associated with their foreign keys, so they cannot be cleared. So we should first clear the tb_device_status_now table and then the tb_device. An error is returned! Looking at the database, we can see that the dev_master field and the dev_id field in the tb_device table are also associated with foreign keys ....
Therefore, you must first clear the dev_master field to clear the entire table tb_device.
SQL statement for clearing data of a field: update table set column = null
My deletion order is as follows:
// First clear the old data in the tb_device_status_now table String sqldel = "delete from tb_device_status_now" using jdbctemplate.exe cute (sqldel); // then clear the old data in the tb_device table. First, clear the dev_master Field, the foreign key is associated with dev_id. Otherwise, the error String sqldel1 = "update tb_device set dev_master = null" using jdbctemplate.exe cute (sqldel1) cannot be deleted ); string sqldel2 = "delete from tb_device" using jdbctemplate.exe cute (sqldel2 );