Microsoft SQL Server 2008 R2 的索引自动增量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4228073/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Index autoincrement for Microsoft SQL Server 2008 R2
提问by Magnetic_dud
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
我在 SQL Server 2008 R2 中创建了一个新表,我希望索引处于自动增量状态。怎么做?没有身份数据类型;我选择了int
回答by marc_s
In SQL Server, it's not a separate datatype ("autoincrement") - but you candefine 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。
回答by Srinivas Reddy Thatiparthy
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.
这将创建一个标识列,该列以 1 开头 id 并在插入表中的每条记录时将其增加一个(即步长)。