简体   繁体   English

在现有表中添加带有主键的列

[英]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 .我正在尝试将主键添加到现有表名Product_Details新添加的列。

New Column added: Product_Detail_ID ( int and not null )添加了新列: Product_Detail_IDintnot 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' . CREATE UNIQUE INDEX语句终止,因为找到了对象名称'dbo.Product\\_Details'和索引名称'pk\\_Product\\_Detils' 'dbo.Product\\_Details'的重复键。 The duplicate key value is (0).重复的键值为 (0)。

Am I missing something here?我在这里错过了什么吗? I am using SQL Server 2008 R2.我正在使用 SQL Server 2008 R2。 I would appreciate any help.我将不胜感激任何帮助。

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

在 mysql 中,我能够通过以下查询实现

ALTER TABLE table_name ADD new_column int NOT NULL AUTO_INCREMENT primary key

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检查现有数据,只会检查新数据
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是我们给出主键关键字

If you want to add a new column say deptId to the existing table say department then you can do it using the below code.如果你想在现有表中添加一个新列,比如 deptId,那么你可以使用下面的代码来完成。

ALTER TABLE department ADD COLUMN deptID INT;

it will create your new column named deptID.它将创建名为 deptID 的新列。

now if you want to add constraint also along with new column while creating it then you can do it using the below code.现在,如果您想在创建新列时也添加约束,那么您可以使用以下代码来完成。 In the below code I am adding primary key as a constraint.在下面的代码中,我将主键添加为约束。 you can add another constraint also instead of primary key like foreign key, default etc.您也可以添加另一个约束来代替外键、默认等主键。

ALTER TABLE department ADD COLUMN deptID INT NOT NULL ADD CONSTRAINT PRIMARY KEY(deptID);

Add Primary Key to First Position将主键添加到第一个位置

ALTER TABLE table_name 
ADD column_name INT PRIMARY KEY AUTO_INCREMENT FIRST;

Reference: Stack Overflow |参考:堆栈溢出| Tech On The Net网络技术

Namaste 🙏合十礼🙏

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM