RT````SQl有没有类似ACCESS里自动编号的功能?还是要自己编?SQL2008里怎么弄?

解决方案 »

  1.   

    create table A
    (
     id int identity(0,1) primary key not null
    )
    那么这个表每添加记录这个id就自动增加1,从0开始
      

  2.   

    自增列.
    IDENTITY(属性)
    在表中创建一个标识列。该属性与 CREATE TABLE 及 ALTER TABLE Transact-SQL 语句一起使用。说明  IDENTITY 属性与 SQL-DMO Identity 属性不同,后者表现列的行标识属性。 
    语法
    IDENTITY [ ( seed , increment ) ]参数
    seed装载到表中的第一个行所使用的值。increment增量值,该值被添加到前一个已装载的行的标识值上。必须同时指定种子和增量,或者二者都不指定。如果二者都未指定,则取默认值 (1,1)。注释
    如果在经常进行删除操作的表中存在着标识列,那么在标识值之间可能会产生差距。如果这构成了问题,那么请不要使用 IDENTITY 属性。但是,为了确保未产生差距,或者为了弥补现有的差距,在用 SET IDENTITY_INSERT ON 显式地输入标识值之前,请先对现有的标识值进行计算。如果重新使用已删除的标识值,那么请使用示例 B 中的示例代码进行检查,以获得下一个可用的标识值。请用您的表名、标识列数据类型以及(该数据类型的)最大可允许值的数值 –1 替换 tablename、column_type 和 max(column_type) – 1。使用 DBCC CHECKIDENT 检查当前的标识值,并将其与标识列中的最大值进行比较。 当将 IDENTITY 属性与 CREATE TABLE 一起使用时,Microsoft® SQL Server™ 使用 CREATE TABLE 的 NOT FOR REPLICATION 选项替代标识列的自动增加。通常,SQL Server 给插入表中的每个新行指派一个值,该值比前面的最高值要大出某些增量。但是,如果新行是由另一个数据源复制过来的,那么标识值必须保持与其在数据源中完全相同。示例
    A. 将 IDENTITY 属性与 CREATE TABLE 一起使用
    下面的示例创建一个新表,该表将 IDENTITY 属性用于获得自动增加的标识号。USE pubs
    IF EXISTS(SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES
          WHERE TABLE_NAME = 'new_employees')
       DROP TABLE new_employees
    GO
    CREATE TABLE new_employees
    (
     id_num int IDENTITY(1,1),
     fname varchar (20),
     minit char(1),
     lname varchar(30)
    )INSERT new_employees
       (fname, minit, lname)
    VALUES
       ('Karin', 'F', 'Josephs')INSERT new_employees
       (fname, minit, lname)
    VALUES
       ('Pirkko', 'O', 'Koskitalo')B. 使用一般语法查找标识值中的差距
    下面的示例显示一般的语法,当删除数据时,可以使用该语法查找标识值中的差距。说明  下面的 Transact-SQL 脚本中的第一部分只用作示范说明。可以运行以下面的注释开始的 Transact-SQL 脚本:- - Create the img table.
    -- Here is the generic syntax for finding identity value gaps in data.
    -- This is the beginning of the illustrative example.
    SET IDENTITY_INSERT tablename ONDECLARE @minidentval column_type
    DECLARE @nextidentval column_type
    SELECT @minidentval = MIN(IDENTITYCOL) FROM tablename
    IF @minidentval = IDENT_SEED('tablename')
       SELECT @nextidentval = MIN(IDENTITYCOL) + IDENT_INCR('tablename')
       FROM tablename t1
       WHERE IDENTITYCOL BETWEEN IDENT_SEED('tablename') AND 
          MAX(column_type) AND
          NOT EXISTS (SELECT * FROM tablename t2
             WHERE t2.IDENTITYCOL = t1.IDENTITYCOL + 
                IDENT_INCR('tablename'))
    ELSE
       SELECT @nextidentval = IDENT_SEED('tablename')
    SET IDENTITY_INSERT tablename OFF
    -- Here is an example to find gaps in the actual data.
    -- The table is called img and has two columns: the first column 
    -- called id_num, which is an increasing identification number, and the 
    -- second column called company_name.
    -- This is the end of the illustration example.-- Create the img table.
    -- If the img table already exists, drop it.
    -- Create the img table.
    IF EXISTS(SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES 
          WHERE TABLE_NAME = 'img')
       DROP TABLE img
    GO
    CREATE TABLE img (id_num int IDENTITY(1,1), company_name sysname)
    INSERT img(company_name) VALUES ('New Moon Books')
    INSERT img(company_name) VALUES ('Lucerne Publishing')
    -- SET IDENTITY_INSERT ON and use in img table.
    SET IDENTITY_INSERT img ONDECLARE @minidentval smallint
    DECLARE @nextidentval smallint
    SELECT @minidentval = MIN(IDENTITYCOL) FROM img
     IF @minidentval = IDENT_SEED('img')
        SELECT @nextidentval = MIN(IDENTITYCOL) + IDENT_INCR('img')
        FROM img t1
        WHERE IDENTITYCOL BETWEEN IDENT_SEED('img') AND 32766 AND
          NOT    EXISTS (SELECT * FROM img t2
              WHERE t2.IDENTITYCOL = t1.IDENTITYCOL + IDENT_INCR('img'))
     ELSE
        SELECT @nextidentval = IDENT_SEED('img')
    SET IDENTITY_INSERT img OFF
      

  3.   

    identity(1,1) --1开始,自增1
      

  4.   

    identity(seed,increment)以种子seed为编号的开始,increment为每次增加的量。
      

  5.   

    access 都有 sql 怎么能没有呢 ?identity
      

  6.   

    Create table table1
    (
    a varchar(20),
    b float,
    c datetime
    )insert into table1
    select '192.168.0.1',4,GETDATE()
    union all
    select '192.168.0.1',4,GETDATE()
    union all
    select '192.168.0.1',4,GETDATE()select id = row_number() over (order by a desc),*
    from table1 
    order by a desc
      

  7.   

    当然有啊!
    如果是2005,2008,在设计器,先选择int,再选择下面的 "是否自动标识",选择是,起始值 为1 ,步长为1
    如果SQL,则用int identity(1,1)