Friday, July 24, 2009

Cannot define PRIMARY KEY constraint on nullable column in table

This error occurs if you attempt to create a Primary Key with a column that is defined as accepting NULL values.

For Example:

CREATE TABLE [dbo].[FactLocateOrdersSKUAggYear](
[dw_order_year_key] [int] NOT NULL,
[dw_brand_id] [smallint] NULL
)

This error will occur if a PK is created that includes dw_brand_id. To resolve, drop any indexes that reference this column and then execute:

ALTER TABLE [dbo].[FactLocateOrdersSKUAggYear] ALTER COLUMN dw_brand_id smallint NOT NULL

No comments:

Post a Comment