简体   繁体   中英

SQL Server : add foreign key error

I have a Microsoft SQL Server database of about 8 tables that I am trying to update. 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". The conflict occurred in database "vehicle_data", table "MAINTENANCE_INTERVAL", column '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.

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.

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. 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.

I take it these are new FKs?

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. At least it gives you proff as to why the FKS are needed. 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. 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. If the parent has some valid values that have never ben used in the child table this woudl casue things to fail.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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