在现有表中添加带有主键的列 [英] Adding column with primary key in existing table
问题描述
我尝试向现有表名 Product_Details
中新添加的列添加主键。
添加: Product_Detail_ID
( int
和 not null
)
我尝试向 Product_Detail_ID
添加主键(请注意:没有其他主键或外键分配给此表)
我尝试使用此查询,但遇到错误。
ALTER TABLE Product_Details
ADD CONSTRAINT pk_Product_Detils_Product_Detail_ID PRIMARY KEY(Product_Detail_ID)
GO
错误:
CREATE UNIQUE INDEX
语句已终止,因为找到对象名称的重复键'dbo.Product\_Details'
和索引名称'pk\_Product\_Detils'
。重复的键值为(0)。
我使用SQL Server 2008 R2。
感谢大家的快速和有用的回复:
参考Timo Hahn的注释,我将数据插入列,然后用下面的查询执行,并且它工作。
ALTER TABLE Product_Details
ADD CONSTRAINT pk_Product_Detils_Product_Detail_ID主键(Product_Detail_ID)
GO
谢谢大家
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.
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
这篇关于在现有表中添加带有主键的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!