How do you add a NOT NULL FOREIGN KEY column to an existing (populated) table in MS SQL?

后端 未结 2 2159
误落风尘
误落风尘 2021-02-20 14:36

I need to add a NOT NULL column to an existing (populated) table that will be a foreign key to another table. This brings about two problems:

  1. When you add the c

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-20 14:57

    Create the column, but allow NULL. Populate the column with the correct data from the foreign key table. Alter the column add not null. Add the foreign key constraint.

提交回复
热议问题