当前位置:首页 > PHP教程 > PHP总结归纳

SQLServer中自动获取编号字段

①像access中的自动编号字段 右键你的表--设计表--找到你的id字段(类int型)--标识--是--标识种子(初始值)--标识递增量--ok ②用identity (seed,increment)参数 seed -启始值 increment -增量 create table 表名( 你的id identity (1, 1) not null ,你的其他

  ①像access中的自动编号字段

  右键你的表-->设计表-->找到你的id字段(类int型)-->标识-->是-->标识种子(初始值)-->标识递增量-->ok

  ②用identity (seed,increment)参数

  seed -启始值

  increment -增量

  create table 表名(

  你的id identity (1, 1) not null ,你的其他字段... )

  create table 表名(

  你的字段id autoincrement(1000,10),其他字段... )

  ③修改起始值和步进值

  alter table 表名 alter column 你的字段id counter(2000,50)

  ④让一个删空的表自动增加字段的开始值重新从1开始

  alter table 表名 alter column 你的字段id counter(1,1)

  上述3 4只适用与access,counter为其一种数据类型,可以在access中指定一不是自动编号的字段为自动编号字段,,也可以让一自动编号字段重新从指定值按指定步长自动编号。但是如果表中有数据,用户不能用该语句来将该列的数据类型改变为counter 数据类型。对于sql server并不支持。

  对于sql server我们或许总希望用alter table 表名 alter column 你的字段 identity(1,1)

  来指定字段重新从1开始计数,但是这句话本身是错误的,好长时间我也疑惑为什么这句话不能执行。如果我们看看ms 对alter table语句的定义就清楚了,这句话根本是错误的。下面是ms对alter table语句的定义。

  alter table table

  { [ alter column column_name

  { new_data_type [ ( precision [ , scale ] ) ]

  [ collate < collation_name > ]

  [ null | not null ]

  | {add | drop } rowguidcol }

  ]

  | add

  { [ < column_definition > ]

  | column_name as computed_column_expression

  } [ ,...n ]

  | [ with check | with nocheck ] add

  { < table_constraint > } [ ,...n ]

  | drop

  { [ constraint ] constraint_name

  | column column } [ ,...n ]

  | { check | nocheck } constraint

  { all | constraint_name [ ,...n ] }

  | { enable | disable } trigger

  { all | trigger_name [ ,...n ] }

  }

  < column_definition > ::=

  { column_name data_type }

  [ [ default constant_expression ] [ with values ]

  | [ identity [ ( seed , increment ) [ not for replication ] ] ]

  ]

  [ rowguidcol ]

  [ collate < collation_name > ]

  [ < column_constraint > ] [ ...n ]

  < 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 [ ,...n ] ) }

  [ with fillfactor = fillfactor ]

  [ on { filegroup | default } ]

  ]

  | foreign key

  [ ( column [ ,...n ] ) ]

  references ref_table [ ( ref_column [ ,...n ] ) ]

  [ 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 )

  }

  可以看到,identity只是在< column_definition >中,也就是说,我们可以这样使用

  alter table 表名 add 字段名 int identity(1,1)

  即,我们可以增加一个字段并指定它为自动编号字段。但是不能更改一个字段为自动编号字段(也或许我没找到方法)。即,如果我们想给表增加自动编号字段,只能使用添加字段的方法,而不能更改一个已有的字段为自动编号字段。

  至于如果需要更改自动编号字段计数起始值可以使用dbcc命令:

  dbcc checkident (表名,reseed,100)

  自动编号字段下一个从101开始计。


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!