(Reprint) MySQL foreign key constraint to delete all tables, disable the foreign key constraint

In fact, if you want to delete all the tables directly as follows:

Select all tables directly in navicat, then right to delete the table, there will be prompt, the way to determine, it will not delete the first table and the foreign key word table, as long as the way to determine, delete several groups put the table are deleted over, It is not too much trouble.

 

turn:

MySQL foreign key constraint delete all tables, disable the foreign key constraint

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/junlovejava/article/details/78360253

 Although the foreign key of the database to ensure data consistency and data integrity, but also to some extent the data update performance degree. In development, we use PowerDesigner establishing physical data model, for clarity of structure, increase readability, creates relationships between tables and tables.
 In the actual development, database generally does not exist outside the key development manual Ali is also not to force foreign key with a cascade operation, all foreign key concepts that must be addressed in the application layer. If the foreign key in the database already exists, and how to do it?

Delete all the foreign key table

 Discover schemaall the foreign key name and then stitching generated delete statement, and then execute.

SELECT CONCAT('ALTER TABLE ',TABLE_SCHEMA,'.',TABLE_NAME,' DROP FOREIGN KEY ',CONSTRAINT_NAME,' ;') FROM information_schema.TABLE_CONSTRAINTS c WHERE c.TABLE_SCHEMA='库名' AND c.CONSTRAINT_TYPE='FOREIGN KEY';
  • 1
  • 2
  • 3

Modify MySQL variablesFOREIGN_KEY_CHECKS

-- 禁用外键约束
SET FOREIGN_KEY_CHECKS = 0 -- 启用外键约束 SET FOREIGN_KEY_CHECKS = 1;
  • 1
  • 2
  • 3
  • 4

Because FOREIGN_KEY_CHECKSbased on the session, closed session when the connection is reestablished, this variable will restore the default value, which is to open foreign key constraint, of course, we can also global FOREIGN_KEY_CHECKSvariables.

SET GLOBAL FOREIGN_KEY_CHECKS = 0;
  • 1

or:

SET @@GLOBAL.FOREIGN_KEY_CHECKS = 0;
  • 1

After editing, we can see the results of the modified

SELECT @@FOREIGN_KEY_CHECKS;

Guess you like

Origin www.cnblogs.com/XingXiaoMeng/p/11706484.html