Can a sql server table have two identity columns?

前端 未结 9 1858
孤城傲影
孤城傲影 2020-11-27 06:23

I need to have one column as the primary key and another to auto increment an order number field. Is this possible?

EDIT: I think I\'ll just use a composite number a

9条回答
  •  佛祖请我去吃肉
    2020-11-27 06:59

    CREATE TABLE [dbo].[Foo](
        [FooId] [int] IDENTITY(1,1) NOT NULL,
        [BarId] [int] IDENTITY(1,1) NOT NULL
    )
    

    returns

    Msg 2744, Level 16, State 2, Line 1
    Multiple identity columns specified for table 'Foo'. Only one identity column per table is allowed.
    

    So, no, you can't have two identity columns. You can of course make the primary key not auto increment (identity).

    Edit: msdn:CREATE TABLE (Transact-SQL) and CREATE TABLE (SQL Server 2000):

    Only one identity column can be created per table.

提交回复
热议问题