The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_SortId".
2017-03-11 07:31
471 查看
在创建外键时,出错。
USE E_Market
GO
ALTER TABLE CommodityInfo
ADD CONSTRAINT FK_SortId FOREIGN KEY(SortId) references CommoditySort(SortId)
GO
想在E_Market数据库中的表 CommodityInfo 为SortId设置外键约束时出错:
The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_SortId". The conflict occurred in database "E_Market", table "dbo.CommoditySort", column 'SortId'.
原因:因为 CommodityInfo 的SortId已经存在数据,所以添加约束失败
解决方案:
USE E_Market
GO
ALTER TABLE CommodityInfo WITH NOCHECK --(不检查表是否已存在数据,直接添加约束)
ADD CONSTRAINT FK_SortId FOREIGN KEY(SortId) references CommoditySort(SortId)
GO
注意,with nocheck 意味着之前添加的数据不受新添约束的影响,对于要求数据准确性高的情况下不使用这种方法。
USE E_Market
GO
ALTER TABLE CommodityInfo
ADD CONSTRAINT FK_SortId FOREIGN KEY(SortId) references CommoditySort(SortId)
GO
想在E_Market数据库中的表 CommodityInfo 为SortId设置外键约束时出错:
The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_SortId". The conflict occurred in database "E_Market", table "dbo.CommoditySort", column 'SortId'.
原因:因为 CommodityInfo 的SortId已经存在数据,所以添加约束失败
解决方案:
USE E_Market
GO
ALTER TABLE CommodityInfo WITH NOCHECK --(不检查表是否已存在数据,直接添加约束)
ADD CONSTRAINT FK_SortId FOREIGN KEY(SortId) references CommoditySort(SortId)
GO
注意,with nocheck 意味着之前添加的数据不受新添约束的影响,对于要求数据准确性高的情况下不使用这种方法。
相关文章推荐
- Msg 547 The ALTER TABLE statement conflicted with the CHECK constraint "".
- Msg 547 The ALTER TABLE statement conflicted with the CHECK constraint "".
- SQL SERVER 2005删除维护作业报错:The DELETE statement conflicted with the REFERENCE constraint "FK_subplan_job_id"
- The INSERT statement conflicted with the FOREIGN KEY constraint
- SQL SERVER 2005删除维护作业报错:The DELETE statement conflicted with the REFERENCE constraint "FK_subplan_job_id"
- SQL SERVER 2005删除维护作业报错:The DELETE statement conflicted with the REFERENCE constraint "FK_subplan_jo
- sql: Query to Display Foreign Key Relationships and Name of the Constraint for Each Table in Database
- 170.A constraint in a table is defined with the INITIALLY IMMEDIATE clause. You executed the ALTER T
- 使用ABP时报错“UPDATE 语句与 FOREIGN KEY SAME TABLE 约束"FK_dbo.AbpUsers_dbo.AbpUsers_LastModifierUserId"冲突”的解决办法
- INSERT 语句与 FOREIGN KEY SAME TABLE 约束"FK__Course__Cpno__1367E606"冲突。该冲突发生于数据库"student",表"dbo.Course",
- ERROR 1701 (42000): Cannot truncate a table referenced in a foreign key constraint
- two improvement to the "drop all table with T-SQL" solution
- phoenix create table Order of columns in primary key constraint must match the order in which they'r
- 1215-Cannot add the foreign key constraint
- mysql Error : Cannot truncate a table referenced in a foreign key constraint 清空具有外键约束的表时报
- EJB3: Mapping of 1-to-1 relationships when primary key in the source table is also a foreign key for the target entity
- MySQL添加外键时报错:1215 Cannot add the foreign key constraint的解决方法
- MySQL添加外键时报错:1215 Cannot add the foreign key constraint的解决方法
- mysql基础(五) Mysql删除表时,报"Connot delete or update a parent row:a foreign key constraint fails"解决方案
- Mysql删除表提示Cannot truncate a table referenced in a foreign key constraint解决办法