简体   繁体   English

没有任何外键约束,无法删除表中的行

[英]Unable to delete a row in a table without any foreign key constraints

I am trying to delete rows from a table in SQL Server 2008 that has neither any foreign key constraints nor any triggers . 我试图从既没有任何外键约束也没有任何触发器的 SQL Server 2008中的表中删除行。 When I check the message section, it is showing the rows affected but when I check the table, rows are still present. 当我检查消息部分时,它显示受影响的行,但是当我检查表时,行仍然存在。 Attached is the image for reference. 随附的图片仅供参考。 I am very much sure that there no constraints to this table. 我非常确定此表没有任何限制。 What could be the issue? 可能是什么问题?

在此处输入图片说明

You can use sys tables to query constrints, keys, etc. Based upon these results, you can delete from your table. 您可以使用sys表查询约束,键等。根据这些结果,可以从表中删除。 Please see below for a sample query which returns table constraints. 请参阅下面的示例查询,该查询返回表约束。

    SELECT 
    TableName = t.Name,
    ColumnName = c.Name,
    dc.type,
    dc.type_desc,
    dc.name,
    dc.definition
FROM sys.tables t
INNER JOIN sys.default_constraints dc ON t.object_id = dc.parent_object_id
INNER JOIN sys.columns c ON dc.parent_object_id = c.object_id AND c.column_id = dc.parent_column_id
where t.name = 'yourtablename'
ORDER BY t.Name

If there truly is no trigger on the table you're deleting from, then examine all the linked tables that have FKs to the table you're deleting from (and, in turn, all FKs to those tables). 如果您要从中删除的表上确实没有触发器,请检查所有链接到您要从中删除的表的FK(以及所有到这些表的FK)的链接表。 The FKs would have ON DELETE CASCADE set so that the delete actually works (instead of throwing an error), only, a trigger in any of those FK-bearing tables could be immediately re-adding the rows. FK会设置ON DELETE CASCADE以便删除实际上起作用(而不是引发错误),只是,那些FK轴承表中的任何一个触发器都可以立即重新添加行。

You might not necessarily see a rows affected message for all of these potential trigger operations, because triggers can set SET NOCOUNT ON before doing any DML... 您可能不一定会针对所有这些潜在的触发器操作看到rows affected消息,因为触发器可以在执行任何DML之前将SET NOCOUNT ON

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM