SQL表中主键自增
使用Sql Express Management Studio 2008 GUI(不带编码),如何使主键自动递增?
让我解释一下:有一个表,其中有一列名为“id”,并且该列的项目设置为主键。我想让这个列自动递增,但是怎么做呢?
干杯
Using Sql Express Management Studio 2008 GUI (not with coding), how can I make a primary key auto-incremented?
Let me explain: there is a table which has a column named "id" and the items of this column are set to be primary keys. I want to make this column auto-incremented, but how?
Cheers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
虽然以下不是在 GUI 中执行此操作的方法,但您可以简单地使用 IDENTITY 数据类型(start,increment)来获得自动增量:
插入语句应列出除 id 列之外的所有列(它将填充自动增量值):
并且的结果
将是
Although the following is not way to do it in GUI but you can get autoincrementing simply using the IDENTITY datatype(start, increment):
the insert statement should list all columns except the id column (it will be filled with autoincremented value):
and the result of
will be
右键单击 SSMS 中的表,对其进行“设计”,然后单击 id 列。在属性中,将身份设置为种子 @ 例如 1 并增量为 1 - 保存即可完成。
Right-click on the table in SSMS, 'Design' it, and click on the id column. In the properties, set the identity to be seeded @ e.g. 1 and to have increment of 1 - save and you're done.
对于那些遇到问题的人,一旦根据下面的答案更改后仍然不允许您保存,请执行以下操作:
工具 ->选项->设计师->表和数据库设计器 ->取消选中“防止保存需要重新创建表的更改”框 ->确定
并尝试保存,因为它现在应该可以工作
for those who are having the issue of it still not letting you save once it is changed according to answer below, do the following:
tools -> options -> designers -> Table and Database Designers -> uncheck "prevent saving changes that require table re-creation" box -> OK
and try to save as it should work now
我这台机器上没有 Express Management Studio,所以我将根据记忆进行操作。我认为您需要将该列设置为“IDENTITY”,并且属性下应该有一个[+],您可以在其中展开,并将自动增量设置为true。
I don't have Express Management Studio on this machine, so I'm going based on memory. I think you need to set the column as "IDENTITY", and there should be a [+] under properties where you can expand, and set auto-increment to true.
我认为有一种方法可以在定义阶段做到这一点,例如
创建表员工(
id int 身份,
名称 varchar(50),
主键(id)
)..
我想看看是否有一种方法可以更改现有表并使该列成为 Identity,这在理论上看起来不可能(因为现有值可能需要修改)
I think there is a way to do it at definition stage like this
create table employee(
id int identity,
name varchar(50),
primary key(id)
)..
I am trying to see if there is a way to alter an existing table and make the column as Identity which does not look possible theoretically (as the existing values might need modification)