identity是sql server中的“标识”列,也是access中的自动编号
identity(函数)只用在带有 into table 子句的 select 语句中,以将标识列插入到新表中。
尽管类似,但是 identity 函数不是与 create table 和 alter table 一起使用的 identity 属性。
语法identity ( data_type [ , seed , increment ] ) as column_name
参数data_type
标识列的数据类型。标识列的有效数据类型可以是任何整数数据类型分类的数据类型(bit 数据类型除外),也可以是 decimal 数据类型。
seed
要指派给表中第一行的值。给每一个后续行指派下一个标识值,该值等于上一个 identity 值加上 increment 值。如果既没有指定 seed,也没有指定 increment,那么它们都默认为 1。
increment
用来添加到 seed 值以获得表中连续行的增量。
column_name
将插入到新表中的列的名称。
返回类型返回与 data_type 相同的类型。
注释因为该函数在表中创建一个列,所以必须用下列方式中的一种在选择列表中指定该列的名称:
--(1)select identity(int, 1,1) as id_numinto newtablefrom oldtable--(2)select id_num = identity(int, 1, 1)into newtablefrom oldtable示例下面的示例将来自 pubs 数据库中 employee 表的所有行都插入到名为 employees 的新表。使用 identity 函数在 employees 表中从 100 而不是 1 开始编标识号。
定义