I'm trying to alter my table named Vettura to modify field ID and making it both Primary and Auto increment, i have already looked in other answers on other similiar conversation, but i can't find the right answer, can you pls help me?
Asked
Active
Viewed 418 times
1 Answers
0
You might not be able to alter the table this way depending on what data is there and if there are dependencies.
If you are using SSMS, I would recommend scripting the database to an SQL file including both the schema and the data and setting DROP and CREATE
. Then you can edit the table definition(s) and regenerate the database.
By the way, for SQL-Server, the Auto increment is called IDENTITY
.

daShier
- 2,056
- 2
- 8
- 14
-
I can't drop and recreate the table, i need to alter it, i tried by using IDENTITY(1,1) but it doesn't work, i'm using SSMS, and i can't change software (school project) so i need to set the existing field ID primary and autoinc (i know how to make it primary but not autoincrement) – Lorenzo Montini Oct 17 '19 at 15:38
-
@LorenzoMontini - Setting `IDENTITY` for an existing column is simply not allowed. See: https://stackoverflow.com/questions/1049210/adding-an-identity-to-an-existing-column – daShier Oct 17 '19 at 15:42
-
i can't drop a table or a column, i need to alter it in order to make a column primary key and autoincrement, so the question is: how can i do that? – Lorenzo Montini Oct 17 '19 at 16:12
-
@LorenzoMontini Please read the comments: It's _can't_ be done in SQL-Server the way you want. We gave you the only viable options. If those are not workable for you, then you are out of luck. – daShier Oct 17 '19 at 16:14
-
Also, it doesn't make sense that you have authority to alter the column and table primary key, but you don't have the ability to script the table to a file (including the data) then recreate it with the configuration you need. If by "school project" you mean that the task is to answer how to do it, then the correct answer is "this can't be done in SQL-Server (without using one of the methods indicated)". – daShier Oct 17 '19 at 16:18