2

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))
nastassiar
  • 1,545
  • 2
  • 24
  • 43
  • You're trying to add a foreign key, but the data in the tables doesn't play by the rules - obviously, you have data in `VEH_ENG_MAINTENANCE_INTERVAL` that has a value in `maintenance_interval_id` that doesn't exist as a value in `MAINTENANCE_INTERVAL.maintenance_interval_id` -.... you need to fix your data problem first - *then* establish the FK relationship – marc_s May 08 '15 at 19:44
  • Try running this SQL to see if you can find information about that particular FK constraint: SELECT * FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS WHERE CONSTRAINT_NAME = N'FK__LKP_VEH_E__maint__6B79F03D' .. presuming you don't know where this FK is coming from, because it's not the one you're trying to add (you should properly name your FK constraints if you're not) – pmbAustin May 08 '15 at 19:45
  • Why did you ask the same question again??? – Sean Lange May 08 '15 at 19:45
  • possible duplicate of [SQL Primary Key Exception](http://stackoverflow.com/questions/30129644/sql-primary-key-exception) – Sean Lange May 08 '15 at 19:45
  • 1
    And also: I'd recommend to **always** explicitly **name** the FK constraint! Or do you really like these funky names like `FK__LKP_VEH_E__maint__6B79F03D` that much?!?!?! Use `ALTER TABLE .... ADD CONSTRAINT (fk_constraint_name) FOREIGN KEY .....` to give your constraint an **explicit name** by which you can refer to it later on (e.g. to drop it) – marc_s May 08 '15 at 19:46
  • @SeanLange Seems like a different question to me. I have edited the question to take some of the suggestions into account, but I still haven't been able to add the foreign key constraints – nastassiar May 08 '15 at 20:33

2 Answers2

4

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.

pmbAustin
  • 3,890
  • 1
  • 22
  • 33
0

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.

HLGEM
  • 94,695
  • 15
  • 113
  • 186