"The DELETE statement conflicted with the REFERENCE constraint" 而引用 table 中没有数据

"The DELETE statement conflicted with the REFERENCE constraint" while there is no data in referenced table

我有两个相关表:

[GameDataGroup] with PK

[Arena_GameData] with FK

我尝试执行查询:

 DELETE FROM [ACP_MAIN_STABLE_DB_content].[dbo].[GameDataGroup] 
 WHERE [key] LIKE '%' + '_test_group' + '%'

并留言:

The DELETE statement conflicted with the REFERENCE constraint "FK__Arena_GameData__GameDataGroup". The conflict occurred in database "ACP_MAIN_STABLE_DB_content", table "dbo.Arena_GameData", column 'gameDataGroupId'.

"dbo.Arena_GameData", column 'gameDataGroupId'中没有相关数据。为什么阻止我删除那条记录?

'FK__Arena_GameData__GameDataGroup'定义:

您的 DELETE 查询得到的结果比您预期的多,因为 _LIKE 而言是一个通配符。

所以 LIKE '%_test_group%' 将匹配至少有一个字符的任何文本,然后是字符 test,然后是任何字符,然后是字符 group,可选地后跟任何数字字符 - 与您可能期望的不同,它需要准确找到序列 _test_group.

如果您需要进行此类匹配,您可以使用转义 - LIKE '%!_test!_group%' ESCAPE '!' 应该可以满足您的要求。

如果任何记录已被任何其他 table 删除 table 中的记录,您将收到一条错误消息,因此您无法删除这些记录来自 table,其他人有 referenced,或者您也应该使用 cascade 选项删除那些引用。