简体   繁体   中英

adding a foreign key constraint to a primary key column of a table with existing data

I'm trying to add in a foreign key constraint on a newly created column of an existing table(table A) and the primary key column of a newly created table (table B). This is on sql server 2008. When I add in the relationship the Alter table script fails. I have also made the new column created on table A to allow nulls. However when I try to save with NO on Check Existing data - I'm able to save the relationship. Is this a good way of creating foreign key constraint on existing data, or should I create a new table (table c) mimicking a many to many relationship instead of adding the new column to table A,which will enable me to create my foreign key constraints without having to specify "No Check" on existing data, although the actual relationship is one to many.

Yes, you can do that by making Checking Existing Data On Creation Or Re-Enabling = No

If you do not want to verify new CHECK or FOREIGN KEY constraints against existing data, use WITH NOCHECK

创建时检查现有数据

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