如何截断外键约束表?
为什么 mygroup
上的 TRUNCATE 不起作用? 即使我有 ON DELETE CASCADE SET
我得到:
错误 1701 (42000):无法截断外键约束中引用的表 (
mytest
.instance
、CONSTRAINTinstance_ibfk_1
FOREIGN KEY (GroupID
) 参考mytest
.mygroup
(ID
))
drop database mytest;
create database mytest;
use mytest;
CREATE TABLE mygroup (
ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY
) ENGINE=InnoDB;
CREATE TABLE instance (
ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
GroupID INT NOT NULL,
DateTime DATETIME DEFAULT NULL,
FOREIGN KEY (GroupID) REFERENCES mygroup(ID) ON DELETE CASCADE,
UNIQUE(GroupID)
) ENGINE=InnoDB;
Why doesn't a TRUNCATE on mygroup
work?
Even though I have ON DELETE CASCADE SET
I get:
ERROR 1701 (42000): Cannot truncate a table referenced in a foreign key constraint (
mytest
.instance
, CONSTRAINTinstance_ibfk_1
FOREIGN KEY (GroupID
) REFERENCESmytest
.mygroup
(ID
))
drop database mytest;
create database mytest;
use mytest;
CREATE TABLE mygroup (
ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY
) ENGINE=InnoDB;
CREATE TABLE instance (
ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
GroupID INT NOT NULL,
DateTime DATETIME DEFAULT NULL,
FOREIGN KEY (GroupID) REFERENCES mygroup(ID) ON DELETE CASCADE,
UNIQUE(GroupID)
) ENGINE=InnoDB;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
是的,您可以:
使用这些语句,您可能会冒着将不遵守
FOREIGN KEY
约束的行放入表中的风险。Yes you can:
With these statements, you risk letting in rows into your tables that do not adhere to the
FOREIGN KEY
constraints.您无法
TRUNCATE
应用了FK约束的表(TRUNCATE
与DELETE
不同)。要解决此问题,请使用以下任一解决方案。两者都存在破坏数据完整性的风险。
选项 1:
TRUNCATE
选项 2: 由user447951在他们的答案中建议
You cannot
TRUNCATE
a table that has FK constraints applied on it (TRUNCATE
is not the same asDELETE
).To work around this, use either of these solutions. Both present risks of damaging the data integrity.
Option 1:
TRUNCATE
Option 2: suggested by user447951 in their answer
我会简单地这样做:
I would simply do it with :
如果您使用 phpMyAdmin,则很容易。
只需取消选中
SQL
选项卡下的启用外键检查
选项并运行TRUNCATE
Easy if you are using phpMyAdmin.
Just uncheck
Enable foreign key checks
option underSQL
tab and runTRUNCATE <TABLE_NAME>
在 MYSQL 数据库上进行测试
解决方案 1:
解决方案 2:
这对我有用。我希望,这也会对您有所帮助。感谢您提出这个问题。
Tested on MYSQL Database
Solution 1:
Solution 2:
This works for me. I hope, this will help you also. Thanks for asking this question.
你可以做
you can do
根据 MySQL 文档,TRUNCATE 不能用于具有外键关系的表。据我所知,没有完全的替代方案。
删除约束仍然不会调用 ON DELETE 和 ON UPDATE。
我ATM能想到的唯一解决方案是:
似乎MySQL中的TRUNCATE还不是一个完整的功能(它也不调用触发器)。查看评论
As per MySQL documentation, TRUNCATE cannot be used on tables with foreign key relationships. There is no complete alternative AFAIK.
Dropping the constraint still does not invoke the ON DELETE and ON UPDATE.
The only solution I can ATM think of is to either:
It would seem TRUNCATE in MySQL is not a complete feature yet (it also does not invoke triggers).See comment
虽然有人问这个问题,但我不知道,但现在如果您使用 phpMyAdmin,您可以简单地打开数据库并选择要截断的表。
删除数据或表
标题下的清空
选项。启用外键检查
的选项。只需取消选择它并按Yes
按钮,所选的表就会被截断。也许它在内部运行 user447951 的答案中建议的查询,但从 phpMyAdmin 界面使用非常方便。
While this question was asked I didn't know about it, but now if you use phpMyAdmin you can simply open the database and select the table(s) you want to truncate.
Empty
option under the headingDelete data or table
.Enable foreign key checks
. Just unselect it and press theYes
button and the selected table(s) will be truncated.Maybe it internally runs the query suggested in user447951's answer, but it is very convenient to use from phpMyAdmin interface.
答案确实是由zerkms提供的,如选项 1 中所述:
棘手的部分是删除约束,所以我想告诉你如何做,以防有人需要知道如何做到这一点:
运行
SHOW CREATE TABLE
查询来查看你的国外KEY的名字(下图中红框):
运行
ALTER TABLE <表名称>;删除外键<外键名称>
。这将删除外键约束。删除关联的索引(通过表结构页面),即可完成。
重新创建外键:
Answer is indeed the one provided by zerkms, as stated on Option 1:
The tricky part is Removing constraints, so I want to tell you how, in case someone needs to know how to do that:
Run
SHOW CREATE TABLE <Table Name>
query to see what is your FOREIGN KEY's name (Red frame in below image):Run
ALTER TABLE <Table Name> DROP FOREIGN KEY <Foreign Key Name>
. This will remove the foreign key constraint.Drop the associated Index (through table structure page), and you are done.
to re-create foreign keys:
另一个解决方法是删除表中的所有行,然后重置自动增量列:
然后运行:
Another workaround is delete all rows in the table then reset auto-increment columns:
then Run:
如何截断外键约束表?
此图将演示如何解决使用外键约束截断表时的 mysql 错误。
如果您使用 PHPMYADMIN,则很容易截断具有外键约束的表。
编辑器示例截断表学生;将学生替换为
表的名称。
它会像魔术一样工作。
How to truncate a foreign key constrained table?
This illustration will demonstrate how to solve mysql error when truncating a table with foreign key constraint.
If you are using PHPMYADMIN, it is very easy to truncate a table with foreign key constraint.
Editor example truncate table students; Replace students with the
name of the table.
It will work like magic.
如果您使用 Laravel 迁移,则可以使用 Facades 来完成此操作。
更喜欢使用 Eloquent 对象,请回答“Eloquent”方式
。在 Laravel 7 和 8 中,为了跨 4 个数据库(MySql、Postgres、SQLite 和 SqlServer)兼容,并且没有 Eloquent,你可以使用:
if you are using laravel migrations, you can do this using facades helpers
prefer to use Eloquent objects, answer the "Eloquent" way
In Laravel 7 and 8, for compatibility across 4 databases (MySql, Postgres, SQLite and SqlServer) and no Eloquent, you can use:
获取旧的外键检查状态和 sql 模式是截断/删除表的最佳方法,就像 Mysql Workbench 在将模型同步到数据库时所做的那样。
Getting the old foreign key check state and sql mode are best way to truncate / Drop the table as Mysql Workbench do while synchronizing model to database.
如果表的数据库引擎不同,您将收到此错误,因此将它们更改为 InnoDB
If the database engine for tables differ you will get this error so change them to InnoDB
只需使用 CASCADE
但要准备好级联删除)
Just use CASCADE
But be ready for cascade deletes )
在进行增删改查操作查询和截断表之前,我们此时使用安全查询。
对于像删除查询这样的粗略操作,您可以编写此命令。
之后该查询可以将零重写为一以进行安全查询。
对于截断表,我们可以在截断查询之前编写以下命令。
之后该查询可以将零重写为一以进行安全查询。
Before crud operation query and truncating table, we use a safe query at that point.
For a crude operation like deleting a query, you can write this command.
after this that query can rewrite zero to one for safe query.
For truncate tables, we can write the command below before truncating the query.
after that query can rewrite zero to one for safe query.