Index autoincrement for Microsoft SQL Server 2008 R2

五迷三道 提交于 2019-12-17 18:35:52

问题


I created a new table in SQL Server 2008 R2, and i would like that the index is on autoincrement. How to do that? There is no identity data type; i selected int


回答1:


In SQL Server, it's not a separate datatype ("autoincrement") - but you can define an INT column to be an IDENTITY.

How are you creating your table - visual designer or T-SQL script??

In T-SQL, you would use:

CREATE TABLE dbo.MyTable(ID INT IDENTITY(1,1) ......

and in the visual table designer, you need to check:

It's an option for a column of type INT - you can define the seed (starting value) and the increment - typically both are set to 1.




回答2:


If your table definition is like this,

....,
@id int,
....

change it to,

....
@id int identity(1,1),
....

This will create an identity column which starts id with 1 and keeps increasing it by one(i.e. step) as each record in the table is inserted.



来源:https://stackoverflow.com/questions/4228073/index-autoincrement-for-microsoft-sql-server-2008-r2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!