SQL 在现有表中添加带有主键的列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13922360/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding column with primary key in existing table
提问by Joy1979
I am trying to add primary key to newly added column in existing table name Product_Details
.
我正在尝试将主键添加到现有表名中新添加的列Product_Details
。
New Column added: Product_Detail_ID
(int
and not null
)
新列说:Product_Detail_ID
(int
和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)
我正在尝试将主键添加到Product_Detail_ID
(请注意:没有分配给该表的其他主键或外键)
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).
该
CREATE UNIQUE INDEX
语句终止,因为找到了对象名称'dbo.Product\_Details'
和索引名称的重复键'pk\_Product\_Detils'
。重复的键值为 (0)。
Am I missing something here? I am using SQL Server 2008 R2. I would appreciate any help.
我在这里错过了什么吗?我正在使用 SQL Server 2008 R2。我将不胜感激任何帮助。
回答by RichardTheKiwi
If you want SQL Server to automatically provide values for the new column, make it an identity.
如果您希望 SQL Server 自动为新列提供值,请将其设为标识。
ALTER TABLE Product_Details DROP COLUMN Product_Detail_ID
GO
ALTER TABLE Product_Details ADD Product_Detail_ID int identity(1,1) not null
GO
ALTER TABLE Product_Details
add CONSTRAINT pk_Product_Detils_Product_Detail_ID primary key(Product_Detail_ID)
GO
回答by Chokho
In mysql, I was able to achieve with following query
在 mysql 中,我能够通过以下查询实现
ALTER TABLE table_name ADD new_column int NOT NULL AUTO_INCREMENT primary key
ALTER TABLE table_name ADD new_column int NOT NULL AUTO_INCREMENT primary key
回答by Shiraz Bhaiji
You are getting the error because you have existing data that does not fullfill the constraint.
您收到错误是因为您的现有数据未满足约束条件。
There are 2 ways to fix it:
有2种方法可以修复它:
- clean up the existing data before adding the constraint
- add the constraint with the "WITH NOCHECK" option, this will stop sql server checking existing data, only new data will be checked
- 在添加约束之前清理现有数据
- 使用“WITH NOCHECK”选项添加约束,这将停止sql server检查现有数据,只会检查新数据
回答by JayaN
ALTER TABLE Jaya
ADD CONSTRAINT no primary key(No);
here Jaya
is table name,
这Jaya
是表名,
no
is column name,
no
是列名,
ADD CONSTRAINT
is we giving the primary key keyword
ADD CONSTRAINT
我们是否给出了主键关键字
回答by srinivas
k. friend command: sql> alter table tablename add primary key(col_name);
克。好友命令:sql> alter table tablename add primary key(col_name);
ex: alter table pk_Product_Detils add primary key(Product_Detail_ID);
例如:alter table pk_Product_Detils 添加主键(Product_Detail_ID);