gpt4 book ai didi

sql - 为什么我不能更新这个表?

转载 作者:行者123 更新时间:2023-12-04 21:26:35 25 4
gpt4 key购买 nike

我在 SQL Server 2008 R2 中有三个表:Country AddressCountry_AddressCountry有列:CountryID Country1、阿富汗
2、澳大利亚
Address有列:AddressID Address1, 5 史密斯路
Country_Address有列:CountryID AddressID1, 1
编辑 : 所以在这个例子中,5 smith way 是阿富汗的地址。
我清空了表格,然后添加了两个国家和一个地址,然后添加了 Country_Address 中的链接。在 1 和 1 之间。当我尝试更新 CountryID 时从 1 到 2,我不能因为 FK 约束。我不明白这是为什么,因为 CountryID我正在尝试分配确实存在。那么我如何设法更新它?

编辑1 :这是我尝试在 SQL Management Studio 中进行更新时遇到的错误,我收到 InvalidOperationException - “无法更新‘...’类型的模型。”在 Visual Studio 中。

No row was updated.

The data in row 1 was not committed.Error Source: .Net SqlClient DataProvider. Error Message: The UPDATEstatement conflicted with the FOREIGNKEY constraint"FK_Country_Address_Address". Theconflict occurred in database"...", table"dbo.Address", column 'AddressID'.

The statement has been terminated.

最佳答案

问题是当您尝试更新 Country 表中的主键值时,Country_Address 表将包含对 Country 表的无效引用。

如果您想像这样更新 key ,最简单的方法是启用所谓的级联更新。您在自己创建外键时执行此操作。

看这里:http://msdn.microsoft.com/en-us/library/aa933119%28SQL.80%29.aspx

编辑:如果我最终正确理解了这一点,我认为 Country_Address 中的外键在 CountryID 上是相反的,或者至少是不正确的柱子。请验证外键定义。

UPDATE Country_Address SET CountryID = 2如果一切设置正确,应该可以工作。您在尝试更新时收到的错误消息不应导致 AddressID 中的值无效。列 - 这向我表明外键设置不正确。

关于sql - 为什么我不能更新这个表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4342517/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com