本文翻譯自:Auto increment primary key in SQL Server Management Studio 2012
How do I
auto increment
the
primary key
in a
SQL Server
database table, I've had a look through the forum but can't see how.
我如何在SQL Server
資料庫表中 auto increment
primary key
,我已經浏覽了論壇,但看不到如何。 I've looked the the properties but can't see an option, I have seen an answer where you go to the
Identity
specification property and set it to yes and set the
Identity increment
to 1, but that section is grayed out and I can't change the no to yes.
我看過屬性,但看不到選項,我看到了一個答案,您可以轉到Identity
規範屬性,并将其設定為是,并将 Identity increment
設定為1,但是該部分顯示為灰色,我可以不要将“否”更改為“是”。 There must be a simple way to do this but I can't find it.
必須有一種簡單的方法來執行此操作,但我找不到它。#1樓
參考:https://stackoom.com/question/k7Uc/SQL-Server-Management-Studio-中的自動遞增主鍵
#2樓
You have to expand the Identity section to expose increment and seed.
您必須展開“身份”部分以顯示增量和種子。
Edit: I assumed that you'd have an integer datatype, not char(10).
編輯:我假設您将有一個整數資料類型,而不是char(10)。Which is reasonable I'd say and valid when I posted this answer
釋出此答案時,我會說合理且有效#3樓
Make sure that the Key column's datatype is
int
and then setting identity manually, as image shows
確定Key列的資料類型為int
,然後手動設定身份,如圖所示 Or just run this code
或者隻是運作此代碼-- ID is the name of the [to be] identity column
ALTER TABLE [yourTable] DROP COLUMN ID
ALTER TABLE [yourTable] ADD ID INT IDENTITY(1,1)
the code will run, if
ID
is not the only column in the table
如果ID
不是表中的唯一列,則代碼将運作 image reference fifo's
圖像參考fifo#4樓
When you're creating the table, you can create an
IDENTITY
column as follows:
建立表時,可以建立一個IDENTITY
列,如下所示: CREATE TABLE (
ID_column INT NOT NULL IDENTITY(1,1) PRIMARY KEY,
...
);
The
IDENTITY
property will auto-increment the column up from number 1. (Note that the data type of the column has to be an integer.) If you want to add this to an existing column, use an
ALTER TABLE
command.
IDENTITY
屬性将使列從1開始自動遞增。(請注意,列的資料類型必須為整數。)如果要将其添加到現有列,請使用 ALTER TABLE
指令。 Edit:
編輯:Tested a bit, and I can't find a way to change the Identity properties via the Column Properties window for various tables.
測試了一下,我找不到通過各種表的“列屬性”視窗更改辨別屬性的方法。I guess if you want to make a column an identity column, you HAVE to use an
ALTER TABLE
command.
我想如果您想将一列設定為辨別列,則必須使用ALTER TABLE
指令。 #5樓
When you're using Data Type: int you can select the row which you want to get autoincremented and go to the column properties tag.
使用資料類型:int時,可以選擇要自動遞增的行,然後轉到列屬性标簽。There you can set the identity to 'yes'.
在那裡,您可以将身份設定為“是”。The starting value for autoincrement can also be edited there.
自動增量的起始值也可以在此處進行編輯。Hope I could help ;)
希望我能幫忙;)#6樓
Expand your database, expand your table right click on your table and select design from dropdown.
展開資料庫,展開表,右鍵單擊表,然後從下拉清單中選擇設計 。Now go Column properties below of it scroll down and find Identity Specification , expand it and you will find Is Identity make it Yes.
現在,轉到其下面的“ 列”屬性 ,向下滾動并找到“ 身份規範” ,将其展開,您将發現“身份是否為Yes”。Now choose Identity Increment right below of it give the value you want to increment in it.
現在,在它下面的“ 身份增量 ”中選擇要增加的值。