截断SQL Server的表数据 [英] Truncate table data of SQL Server

查看:146
本文介绍了截断SQL Server的表数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

我想从表中删除数据。但是表已经通过外键与其他表链接了。因此我无法从表中删除数据。所以我不想删除表的约束..是否有其他方法截断表数据而不删除约束



它抛出异常

不能截断表因为它被FOREIGN KEY约束引用。



需要你的帮助



谢谢

解决方案

阅读以下内容: http://stackoverflow.com/questions/253849/cannot-truncate-table-because-it-is-being-referenced-by-a-foreign-key-constraint [ ^ ]

有很多方法可以管理参照完整性。



您可以禁用约束:



http:// stackoverflow.com/questions/159038/can-foreign-key-constraints-be-temporarily-disabled-using-t-sql [ ^ ]



但是在数据修复之前你将无法重新启用它。



您可以配置级联删除。这意味着通过外键链接的任何记录也将被删除。



http://www.mssqltips.com/sqlservertip/2743/using-delete-cascade-option-for-foreign-keys/ [<一个href =http://www.mssqltips.com/sqlservertip/2743/using-delete-cascade-option-for-foreign-keys/target =_ blanktitle =New Window> ^ ]



但这会改变数据库行为,并允许删除父记录,您可能希望孩子出现,以创建阻止操作。



因此,简而言之,不能在不删除约束的情况下截断表,但SQL确实具有帮助引用完整性的机制。


请参阅这个:



d是数据库中的所有外键约束


Hi guys
I want to delete data from table .but the table is already linked with other via Foreign key..So i am not able to delete data from table..So i dont wanna delete constraints of the table ..Is there other way to Truncate table data without dropping constraint

Its throws exception
Cannot truncate table because it is being referenced by a FOREIGN KEY constraint.

Need your help

Thank you

解决方案

Read the following : http://stackoverflow.com/questions/253849/cannot-truncate-table-because-it-is-being-referenced-by-a-foreign-key-constraint[^]


There are a number of ways to manage referential integrity.

You could disable the contraint:

http://stackoverflow.com/questions/159038/can-foreign-key-constraints-be-temporarily-disabled-using-t-sql[^]

But you wouldn't be able to re-enable it until the data was fixed.

You can configure cascading deletes. This means any record linked through a foreign key would also be deleted.

http://www.mssqltips.com/sqlservertip/2743/using-delete-cascade-option-for-foreign-keys/[^]

But this will change database behaviour and allow parent records to be deleted where you may want the presence of a child to create a blocking action.

So in short, no you can't truncate a table without dropping the constraint, but SQL does have mechanisms for helping with referential integrity.


See this:

Disable All the Foreign Key Constraint in Database


这篇关于截断SQL Server的表数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆