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?
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?
6条答案
按热度按时间jrcvhitl1#
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:
bqf10yzr2#
The easiest way would be:
SQL Server Management Studio
.Column Properties
Window browse toIdentity Specification
>Is Identity
And set toNo
.Table Designer
> SelectGenerate Change Script...
I Like using this method for getting scripts, since it allows me to generate scripts I'm not sure how to compose from scratch and thus learning and improving my skills...
lvjbypge3#
If it's a primary key column, then you have to drop the PK first. If there's any tables referencing it, then you'll have to drop these FKs to be able to drop the PK. After that, add another column of the same type, update it with values from identity column, drop the identity column, rename the new column to whatever the name of identity column was (with
sp_rename
procedure), recreate the PK, recreate the FKs, check if everything went right.I'd be very careful doing it on a production database. Ensure that noone can access the data while you're doing this.
hfyxw5xn4#
I searched a lot to find a simple solution to remove the auto increment because i should do a lot of work if i drop the column and the primary key which was a foreign key on another table where I should remove the data that are using my foreign ... finally I ended up with a very simple solution that made my life easy:
6pp0gazn5#
SET IDENTITY_INSERT [TABLE] OFF
.. this allows to remove the auto increment to off state.., so that we have to enter the value in thatcolumnqyswt5oh6#
You can remove 'auto_increment' by using following query...
For exmample,
table_name = STUDENT,
auto_incremented_column_name = ID,
column_data_type = INT
To add 'auto_increment'