简体   繁体   English

SQL Server:添加外键错误

[英]SQL Server : add foreign key error

I have a Microsoft SQL Server database of about 8 tables that I am trying to update. 我有一个要更新的大约8个表的Microsoft SQL Server数据库。 I create temporary tables, drop the existing tables, rename the temporary tables to their final names, then create indexes and foreign key constraints to speed up look ups. 我创建临时表,删除现有表,将临时表重命名为其最终名称,然后创建索引和外键约束以加快查找速度。

The problem is when I try to create the foreign key constraints on the renamed tables I receive the following error. 问题是,当我尝试在重命名的表上创建外键约束时,收到以下错误。

The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK__maintenance_interval_id". ALTER TABLE语句与FOREIGN KEY约束“ FK__maintenance_interval_id”冲突。 The conflict occurred in database "vehicle_data", table "MAINTENANCE_INTERVAL", column 'maintenance_interval_id'. 数据库“ vehicle_data”的表“ MAINTENANCE_INTERVAL”的列“ maintenance_interval_id”中发生了冲突。

Here is the statement that is causing the problem 这是引起问题的语句

ALTER TABLE VEH_ENG_MAINTENANCE_INTERVAL
ADD CONSTRAINT FK_maintenance_interval_id FOREIGN KEY (maintenance_interval_id) 
REFERENCES MAINTENANCE_INTERVAL(maintenance_interval_id)

People have pointed out that it is likely caused by a mismatch of data in the columns of each table. 人们指出,这很可能是由于每个表的列中的数据不匹配所致。 Is there an easy way to check this? 有一个简单的方法来检查吗? Both tables have thousands of entries. 两个表都有数千个条目。

Create table statement : 创建表语句:

CREATE TABLE [vehicle_data].[dbo].[MAINTENANCE_INTERVAL]
[maintenance_interval_id] int,
[interval_type] varchar(32),
[value] decimal(18,2),
[units] varchar(32),
[initial_value] decimal(18,2),
PRIMARY KEY CLUSTERED ([maintenance_interval_id] ASC))

The FK constraint error is claiming that there's a violation of the constraint, so it can't be applied. FK约束错误声称存在违反约束的情况,因此无法应用。

The FK itself is saying that every value in VEH_ENG_MAINTENEANCE_INTERVAL.maintenance_interval_id should be defined in the MAINTENANCE_INTERVAL.maintenance_interval_id table/column. FK本身说VEH_ENG_MAINTENEANCE_INTERVAL.maintenance_interval_id中的每个值都应在MAINTENANCE_INTERVAL.maintenance_interval_id表/列中定义。

So this query will show you all rows in your table that have values that are NOT in the foreign key table. 因此,此查询将向您显示表中具有不在外键表中的值的所有行。

SELECT * 
  FROM VEH_ENG_MAINTENANCE_INTERVAL 
 WHERE maintenance_interval_id NOT IN (SELECT maintenance_interval_id FROM MAINTENANCE_INTERVAL)

This will show you all the rows that are causing issues. 这将向您显示所有导致问题的行。 Look at the maintenance_interval_id values and compare them to what is in the MAINTENANCE_INTERVAL table. 查看maintenance_interval_id值,并将它们与MAINTENANCE_INTERVAL表中的值进行比较。 You'll either need to add rows to the latter table, or delete the "bad data" from the table you're trying to apply the FK Constraint to. 您将需要在后一个表中添加行,或者从要对其应用FK约束的表中删除“不良数据”。

I take it these are new FKs? 我认为这些是新的FK?

YOu have one of two problems. 您有两个问题之一。 First and most likely is that if you have not had a constraint before, then you have values in the child table that don't exist in the parent table. 首先,最有可能的是,如果之前没有约束,那么子表中的值将不存在于父表中。 You should fix the data before attempting to add the constraint. 您应在尝试添加约束之前修复数据。 You can also do it with a NOCHECK but that is a particularly poor idea since if the that record is ever updated, then it will fail unless the fk field is changed. 您也可以使用NOCHECK来执行此操作,但这是一个特别糟糕的主意,因为如果该记录曾经被更新,那么除非fk字段被更改,否则它将失败。 At least it gives you proff as to why the FKS are needed. 至少它使您了解为什么需要FKS。 Unfortunately it is soewhat hard to know what to put inth eparent table is the key value is a number. 不幸的是,很难知道在表中放入什么是键值是数字。 WHo knows waht orderID 927 was now that it no longer exists but you still have teh order detaials for it. 谁知道orderID 927现在已经不存在,但您仍然需要订单详细信息。 YOU amy need to crete some sort of fake values (Like an Unknnown record to attach all the bad data to) or you may need to drop the records, it rather depends on your business rules and what the dat means. 您艾米需要创建某种假值(例如将未知数据附加到未知记录),或者您可能需要删除记录,这取决于您的业务规则和数据的含义。 We can't answeer that of course, only your company can. 当然,只有您的公司才能做到这一点。 But in general, if the orphaned records have financial or regulatory or legal data in them, they are generally not deleted. 但是通常,如果孤立记录中包含财务,法规或法律数据,则通常不会删除它们。

The other time this can occur is if you have the PK-FK relationship reversed and are trying to make the parent table into the child. 另一个可能发生的情况是,如果您的PK-FK关系被逆转并且正在尝试使父表成为子表。 If the parent has some valid values that have never ben used in the child table this woudl casue things to fail. 如果父级具有一些从未在子级表中使用的有效值,则可能会导致失败。

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

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