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'.
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)
ADD CONSTRAINT <FK_Table Name_IDFK FOREIGN KEY(IDFK)
REFERENCES <Ref Table Name>(ID)
Comments
Post a Comment
Hi User,
Thanks for visiting My Blog and please provide your valuable feedback and subscribe for more updates. Please don't post any spam content or comments.
Thank You