一个 sql server 表可以有两个标识列吗?

问题描述:

我需要将一列作为主键,另一列用于自动增加订单号字段.这可能吗?

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

我想我会使用一个复合数作为订单号.还是谢谢.

I think I'll just use a composite number as the order number. Thanks anyways.

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

返回

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

msdn:CREATE TABLE (Transact-SQL)创建表 (SQL Server 2000):

每个表只能创建一个标识列.

Only one identity column can be created per table.