[英]Index autoincrement for Microsoft SQL Server 2008 R2
I created a new table in SQL Server 2008 R2, and i would like that the index is on autoincrement. 我在SQL Server 2008 R2中创建了一个新表,我希望索引是自动增量的。 How to do that? 怎么做? There is no identity data type; 没有身份数据类型; i selected int 我选择了int
In SQL Server, it's not a separate datatype ("autoincrement") - but you can define an INT
column to be an IDENTITY
. 在SQL Server中,它不是单独的数据类型(“自动增量”) - 但您可以将INT
列定义为IDENTITY
。
How are you creating your table - visual designer or T-SQL script?? 你是如何创建表的 - 可视化设计器或T-SQL脚本?
In T-SQL, you would use: 在T-SQL中,您将使用:
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. 它是INT类型列的一个选项 - 您可以定义种子(起始值)和增量 - 通常都设置为1。
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(ie step) as each record in the table is inserted. 这将创建一个标识列,该标识列以1开始,并在插入表中的每个记录时将其增加1(即步骤)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.