Cannot delete or update a parent row: a foreign key constraint fails solution, constraintfails

Source: Internet
Author: User

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 );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.