Auto Increment Primary Key In SQL Server Management Studio 2012


Answer :

Make sure that the Key column's datatype is int and then setting identity manually, as image shows

enter image description here

Or just run this code

-- ID is the name of the  [to be] identity column ALTER TABLE [yourTable] DROP COLUMN ID  ALTER TABLE [yourTable] ADD ID INT IDENTITY(1,1) 

the code will run, if ID is not the only column in the table

image reference fifo's


When you're creating the table, you can create an IDENTITY column as follows:

CREATE TABLE (   ID_column INT NOT NULL IDENTITY(1,1) PRIMARY KEY,   ... ); 

The IDENTITY property will auto-increment the column up from number 1. (Note that the data type of the column has to be an integer.) If you want to add this to an existing column, use an ALTER TABLE command.

Edit:
Tested a bit, and I can't find a way to change the Identity properties via the Column Properties window for various tables. I guess if you want to make a column an identity column, you HAVE to use an ALTER TABLE command.


You have to expand the Identity section to expose increment and seed.

enter image description here

Edit: I assumed that you'd have an integer datatype, not char(10). Which is reasonable I'd say and valid when I posted this answer


Comments

Popular posts from this blog

Are Regular VACUUM ANALYZE Still Recommended Under 9.1?

Can Feynman Diagrams Be Used To Represent Any Perturbation Theory?