Joy1979 Joy1979 - 6 months ago 11
SQL Question

Adding column with primary key in existing table

I am trying to add primary key to newly added column in existing table name

Product_Details
.

New Column added:
Product_Detail_ID
(
int
and
not null
)

I am trying add primary key to
Product_Detail_ID
(please note: there are no other primary or foreign key assigned to this table)

I am trying with this query but getting error.

ALTER TABLE Product_Details
ADD CONSTRAINT pk_Product_Detils_Product_Detail_ID PRIMARY KEY(Product_Detail_ID)
GO


Error:


The
CREATE UNIQUE INDEX
statement terminated because a duplicate key was found for the object name
'dbo.Product\_Details'
and the index name
'pk\_Product\_Detils'
. The duplicate key value is (0).



Am I missing something here? I am using SQL Server 2008 R2. I would appreciate any help.

Answer

I thank all of you for your prompt and helpful reply:

With reference of Timo Hahn's comment I insert the data into column and then execute with following query and it worked.

ALTER TABLE Product_Details
ADD CONSTRAINT pk_Product_Detils_Product_Detail_ID primary key(Product_Detail_ID)
GO

Thank you all