SQL表中主键自增

发布于 2024-09-11 17:15:44 字数 139 浏览 5 评论 0原文

使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

梦言归人 2024-09-18 17:15:44
  1. 想必您正在设计表格。如果没有:右键单击表名称 - “设计”。
  2. 单击所需的列。
  3. 在“列属性”(底部)中,滚动到“身份规范”部分,将其展开,然后切换“(是身份)”改为“”。

在此处输入图像描述

  1. Presumably you are in the design of the table. If not: right click the table name - "Design".
  2. Click the required column.
  3. In "Column properties" (at the bottom), scroll to the "Identity Specification" section, expand it, then toggle "(Is Identity)" to "Yes".

enter image description here

小ぇ时光︴ 2024-09-18 17:15:44

虽然以下不是在 GUI 中执行此操作的方法,但您可以简单地使用 IDENTITY 数据类型(start,increment)来获得自动增量:

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

插入语句应列出除 id 列之外的所有列(它将填充自动增量值):

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

并且的结果

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

将是

id    name
--------------------------
1     alpha
2     beta

Although the following is not way to do it in GUI but you can get autoincrementing simply using the IDENTITY datatype(start, increment):

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

the insert statement should list all columns except the id column (it will be filled with autoincremented value):

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

and the result of

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

will be

id    name
--------------------------
1     alpha
2     beta
绾颜 2024-09-18 17:15:44

右键单击 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.

深居我梦 2024-09-18 17:15:44

对于那些遇到问题的人,一旦根据下面的答案更改后仍然不允许您保存,请执行以下操作:

工具 ->选项->设计师->表和数据库设计器 ->取消选中“防止保存需要重新创建表的更改”框 ->确定

并尝试保存,因为它现在应该可以工作

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

对你再特殊 2024-09-18 17:15:44

我这台机器上没有 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.

习惯成性 2024-09-18 17:15:44

我认为有一种方法可以在定义阶段做到这一点,例如

创建表员工(
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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文