截断外键引用的表

发布于 2024-08-25 02:08:16 字数 283 浏览 3 评论 0原文

我们在 SQL Server 2005 数据库中有两个表,A 和 B。有一项服务每天都会截断表 A。

最近,表 B 中添加了外键约束,引用表 A。因此,即使表 B 为空,也无法再截断表 A。是否有任何解决方法可以获得与截断表 A 相同的结果?

我已经尝试过下面的方法,但身份没有重置。

DBCC CHECKIDENT (TABLENAME, RESEED, 0)

附言。在有人指出这是重复之前,这里的不同之处在于我不允许删除约束,也不允许创建任何约束。

We have two tables in a SQL Server 2005 database, A and B.There is a service which truncates table A every day.

Recently, a foreign key constraint was added to table B, referencing table A. As a result, it isn't possible truncating table A anymore, even if table B is empty. Is there any workaround to get the same result as truncating table A?

I've already tried the approach below but the identity wasn't reset.

DBCC CHECKIDENT (TABLENAME, RESEED, 0)

PS. before anyone points this as a duplicate, the different thing here is that I'm not allowed to drop constraints, nor creating any.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

白馒头 2024-09-01 02:08:16

如果无法删除约束,则无法使用 TRUNCATE TABLE,因此必须使用 DELETE。

DELETE TABLEA
DBCC CHECKIDENT (TABLENAME, RESEED, 0)

注意:禁用FK也不起作用

If you can't drop the constraint, you can't use TRUNCATE TABLE, so you have to use DELETE.

DELETE TABLEA
DBCC CHECKIDENT (TABLENAME, RESEED, 0)

Note: Disabling the FK will not work either

看透却不说透 2024-09-01 02:08:16

SET FOREIGN_KEY_CHECKS = 0;# MySQL 返回空结果集(即零行)。 -- 禁用外键检查。 TRUNCATE TABLE table_name;# MySQL 返回一个空结果集(即零行)。 SET FOREIGN_KEY_CHECKS = 1;# MySQL 返回一个空结果集(即零行)。 -- 启用外键检查。# MySQL 返回一个空结果集(即零行)。

SET FOREIGN_KEY_CHECKS = 0;# MySQL returned an empty result set (i.e. zero rows). -- Disable foreign key checking. TRUNCATE TABLE table_name;# MySQL returned an empty result set (i.e. zero rows). SET FOREIGN_KEY_CHECKS = 1;# MySQL returned an empty result set (i.e. zero rows). -- Enable foreign key checking.# MySQL returned an empty result set (i.e. zero rows).

じее 2024-09-01 02:08:16

如果要截断外键约束中引用的表

1-DISABLE related FK
2-TRUNCATE all related tables.
3-ENABLE the previously disabled FKs

SET FOREIGN_KEY_CHECKS = 0; -- Disable foreign key checking.
TRUNCATE TABLE Table1;
SET FOREIGN_KEY_CHECKS = 1; -- Enable foreign key checking.

If you want to truncate a table referenced in a foreign key constraint

1-DISABLE related FK
2-TRUNCATE all related tables.
3-ENABLE the previously disabled FKs

SET FOREIGN_KEY_CHECKS = 0; -- Disable foreign key checking.
TRUNCATE TABLE Table1;
SET FOREIGN_KEY_CHECKS = 1; -- Enable foreign key checking.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文