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