如何从sql server 2012中的表中删除自动增量 [英] How to remove auto increment from table in sql server 2012
本文介绍了如何从sql server 2012中的表中删除自动增量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在 SQL Server 2012 中创建了一个表,主键作为自动增量.但是如何使用 SQL 查询从表中删除该自动增量属性?
I have created a table in SQL Server 2012 with primary key as auto increment. But how can I remove that auto increment property from the table using a SQL query?
推荐答案
如果您需要保留该列中的数据,则在表上创建一个相同类型(但名称不同)的新列,复制将要删除的列中的数据删除到新列,删除旧列并重命名新列.完整示例:
If you need to keep the data in that column then create a new column on the table which is of the same type (but a different name), copy the data from the column you want to get rid of to the new one, drop the old column and rename the new. Complete example:
CREATE TABLE test(col1 INT IDENTITY (1,1) NOT NULL, col2 VARCHAR(10) NULL);
ALTER TABLE test ADD col3 INT NULL;
UPDATE test SET col3 = col1;
ALTER TABLE test DROP COLUMN col1;
EXEC sp_rename 'dbo.test.col3', 'col1', 'COLUMN';
这篇关于如何从sql server 2012中的表中删除自动增量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文