The ALTER TABLE statement conflicted with the FOREIGN KEY constraint

Error Message:
Msg 547, Level 16, State 0, Line 1
The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_BookingHistory_BookingRef". The conflict occurred in database "DB_Name", table "dbo.Depended_Table/s", column 'bookingRef'. 

This means that existing data in those tables are causing the referential integrity to break which is causing the FK to fail
You have two options
1. To clear and correct the data in your parent and child tables so that reference column values correspond to that in your parent table and then create the FK s. THIS IS THE RECOMMENDED APPROACH
2. To go ahead and create FK constraint with NO CHECK option which will bypass the referential integrity check whilst creating the FK and will only enforce for the future data. But this is not recommended as it means data integrity of the table has already been compromised so ideally you should try to implement 1
ALTER TABLE <Table Name> WITH NOCHECK
ADD CONSTRAINT <FK_Table Name_IDFK FOREIGN KEY(IDFK)
REFERENCES <Ref Table Name>(ID)

Comments

Popular Posts

Failed to execute the package or element. Build errors were encountered

Exception deserializing the package "The process cannot access the file because it is being used by another process."

Temporary enable and disable SSRS subscriptions