截断外键引用的表
我们在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果无法删除约束,则无法使用 TRUNCATE TABLE,因此必须使用 DELETE。
注意:禁用FK也不起作用
If you can't drop the constraint, you can't use TRUNCATE TABLE, so you have to use DELETE.
Note: Disabling the FK will not work either
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).如果要截断外键约束中引用的表
If you want to truncate a table referenced in a foreign key constraint