具體步驟如下
①像Access中的自動編號字段
右鍵你的表
②用IDENTITY (SEED
seed
increment
CREATE TABLE 表名(
你的ID IDENTITY (
CREATE TABLE 表名(
你的字段ID AUTOINCREMENT(
③修改起始值和步進值
ALTER TABLE 表名 ALTER COLUMN 你的字段ID COUNTER(
④讓一個刪空的表自動增加字段的開始值重新從
ALTER TABLE 表名 ALTER COLUMN 你的字段ID COUNTER(
上述
對於SQL Server我們或許總希望用Alter Table 表名 Alter Column 你的字段 IDENTITY(
來指定字段重新從
ALTER TABLE table
{ [ ALTER COLUMN column_name
{ new_data_type [ ( precision [
[ COLLATE < collation_name > ]
[ NULL │ NOT NULL ]
│ {ADD │ DROP } ROWGUIDCOL }
]
│ ADD
{ [ < column_definition > ]
│ column_name AS computed_column_expression
} [
│ [ WITH CHECK │ WITH NOCHECK ] ADD
{ < table_constraint > } [
│ DROP
{ [ CONSTRAINT ] constraint_name
│ COLUMN column } [
│ { CHECK │ NOCHECK } CONSTRAINT
{ ALL │ constraint_name [
│ { ENABLE │ DISABLE } TRIGGER
{ ALL │ trigger_name [
}
< column_definition > ::=
{ column_name data_type }
[ [ DEFAULT constant_expression ] [ WITH VALUES ]
│ [ IDENTITY [ ( seed
]
[ ROWGUIDCOL ]
[ COLLATE < collation_name > ]
[ < column_constraint > ] [
< column_constraint > ::=
[ CONSTRAINT constraint_name ]
{ [ NULL │ NOT NULL ]
│ [ { PRIMARY KEY │ UNIQUE }
[ CLUSTERED │ NONCLUSTERED ]
[ WITH FILLFACTOR = fillfactor ]
[ ON { filegroup │ DEFAULT } ]
]
│ [ [ FOREIGN KEY ]
REFERENCES ref_table [ ( ref_column ) ]
[ ON DELETE { CASCADE │ NO ACTION } ]
[ ON UPDATE { CASCADE │ NO ACTION } ]
[ NOT FOR REPLICATION ]
]
│ CHECK [ NOT FOR REPLICATION ]
( logical_expression )
}
< table_constraint > ::=
[ CONSTRAINT constraint_name ]
{ [ { PRIMARY KEY │ UNIQUE }
[ CLUSTERED │ NONCLUSTERED ]
{ ( column [
[ WITH FILLFACTOR = fillfactor ]
[ ON { filegroup │ DEFAULT } ]
]
│ FOREIGN KEY
[ ( column [
REFERENCES ref_table [ ( ref_column [
[ ON DELETE { CASCADE │ NO ACTION } ]
[ ON UPDATE { CASCADE │ NO ACTION } ]
[ NOT FOR REPLICATION ]
│ DEFAULT constant_expression
[ FOR column ] [ WITH VALUES ]
│ CHECK [ NOT FOR REPLICATION ]
( search_conditions )
}
可以看到
Alter Table 表名 Add 字段名 Int IDENTITY(
即
至於如果需要更改自動編號字段計數起始值可以使用DBCC命令
DBCC CHECKIDENT (表名
自動編號字段下一個從
From:http://tw.wingwit.com/Article/program/SQLServer/201311/22336.html