Auto increment primary key in SQL tables

Using the Sql Express Management Studio 2008 GUI (not with encoding), how to make a primary key with auto-increment?

Let me explain: there is a table in which there is a column called "id", and the elements of this column are primary. I want this column to grow automatically, but how?

Greetings

+53
sql-server-2008 ssms
Jul 29 '10 at 2:53 on
source share
6 answers
  • Presumably you are in a table design. If not: right-click on the table name - Design .
  • Select the desired column.
  • In the " Column Properties " (bottom), highlight the " Identifier Specification " section, expand it, then switch " (Is Identity) " to " Yes ."

enter image description here

+92
Jul 29 '10 at 2:57
source share

Right click on the table in SSMS, "Design" and click on the id column. In the properties, specify the identifier for sowing @, for example. 1 and have an increment of 1 - save, and you're done.

+10
Jul 29 '10 at 2:57
source share

Although this cannot be done in the GUI, you can get auto-increment simply by using the IDENTITY (start, increment) data type:

CREATE TABLE "dbo"."TableName" ( id int IDENTITY(1,1) PRIMARY KEY NOT NULL, name varchar(20), ); 

the insert statement should display all columns except the id column (it will be populated using a value with auto-increments):

 INSERT INTO "dbo"."TableName" (name) VALUES ('alpha'); INSERT INTO "dbo"."TableName" (name) VALUES ('beta'); 

and result

 SELECT id, name FROM "dbo"."TableName"; 

will be

 id name -------------------------- 1 alpha 2 beta 
+9
Sep 18 '14 at 13:44 on
source share

I do not have Express Management Studio on this computer, so I'm going to be based on memory. I think you need to set the column as "IDENTIFICATION", and there should be [+] in the properties where you can expand, and set auto-increment to true.

+6
Jul 29 '10 at 2:58
source share

for those who have a problem with this, still does not allow you to save, as soon as it is changed in accordance with the answer below, follow these steps:

tools -> options -> designer -> Table and database designers -> uncheck "prevent the saving of changes requiring a table restore" → OK

and try to save, as it should work now

+4
Nov 05 '13 at 18:30
source share

I think there is a way to do this at the stage of determining how it

create a table employee (id int identity, varchar name (50), primary key (id)) .. I am trying to see if there is a way to modify an existing table and make the column as Identity, which does not look theoretically (as existing values ​​may need to be changed)

0
Dec 17 '13 at 21:44
source share



All Articles