如何在MySQL中强制截断数据库中的所有表(都是innodb) ?

时间:2022-08-29 19:13:05

I think I get foreign key constraint error when I try to truncate innodb tables. I was not having problems with this when using MyISAM.

我想当我试图截断innodb表时,会得到外键约束错误。我在使用MyISAM时没有遇到问题。

Is there an easy way to force truncate all tables? Or should I just make a script to drop the database, create new one and then create the tables from scratch?

是否有一种简单的方法强制截断所有表?或者我应该编写一个脚本来删除数据库,创建新的数据库,然后从头创建表?

2 个解决方案

#1


54  

About the FK constraints, you could disable them with next statements -

关于FK约束,您可以使用下面的语句禁用它们

SET FOREIGN_KEY_CHECKS = 0;
...DML statements
SET FOREIGN_KEY_CHECKS = 1; -- enable checking

#2


8  

If you have foreign key problems during your operation you can:

如果您在操作过程中遇到外键问题,您可以:

ALTER TABLE tablename DISABLE KEYS

then do your business, and afterwards re-enable keys with:

然后做你的生意,然后重新启用钥匙:

ALTER TABLE tablename ENABLE KEYS

This techinique is used in MySQL dumps.

这个techinique用于MySQL转储。

#1


54  

About the FK constraints, you could disable them with next statements -

关于FK约束,您可以使用下面的语句禁用它们

SET FOREIGN_KEY_CHECKS = 0;
...DML statements
SET FOREIGN_KEY_CHECKS = 1; -- enable checking

#2


8  

If you have foreign key problems during your operation you can:

如果您在操作过程中遇到外键问题,您可以:

ALTER TABLE tablename DISABLE KEYS

then do your business, and afterwards re-enable keys with:

然后做你的生意,然后重新启用钥匙:

ALTER TABLE tablename ENABLE KEYS

This techinique is used in MySQL dumps.

这个techinique用于MySQL转储。