Simple implementation of ignore foreign key constraints when MySQL deletes a table, mysql Constraints

Source: Internet
Author: User

Simple implementation of ignore foreign key constraints when MySQL deletes a table, mysql Constraints

Deleting a table is not very common. Be careful when deleting a table that has a foreign key Association. However, during the development process, it is common to find a Schema design problem and delete all the tables in the existing database to be re-created. In addition, during the test, you also need to recreate all tables in the database. Of course, many automated tools can also do this.

When deleting a table, you may encounter the following error message:

ERROR 1217 (23000): Cannot delete or update a parent row: a foreign key constraint fails

This is because the fields in the table you are trying to delete are used as foreign keys for other tables. Therefore, you are deleting this table (parent table) you must delete a table (sub-table) with a foreign key ). That is to say, the process of deleting a table must be the same as that of creating a table.

But this is often unacceptable. On the one hand, if there are too many tables, manual sorting is a bit unacceptable; on the other hand, there is no automatic tool to sort (in fact, it is not impossible ). Therefore, MySQL provides a variable FOREIGN_KEY_CHECKS to set whether to check foreign key constraints when necessary.

It is generally recommended to do this:

First, all DROP statements are automatically generated, replace MyDatabaseName with your database name:

SELECT concat('DROP TABLE IF EXISTS ', table_name, ';')FROM information_schema.tablesWHERE table_schema = 'MyDatabaseName';

Then, add the following statement to set the FOREIGN_KEY_CHECKS variable before and after the generated code:

SET FOREIGN_KEY_CHECKS = 0 -- DROP statement SET FOREIGN_KEY_CHECKS = 1;

However, it does not matter if you forget the last sentence. This variable is Session-based. That is to say, when you close the client and re-establish a connection, the variable will restore the default value. If you do not need to check the foreign key constraints globally (this case will be rare), you can do this:

SET GLOBAL FOREIGN_KEY_CHECKS = 0;

Or

set @@global.FOREIGN_KEY_CHECKS = 0;

The simple implementation of ignoring the foreign key constraint during the above MySQL table deletion is all the content shared by the small Editor. I hope you can provide a reference and support for the customer's house.

Related Article

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.