需要一个没有主键的自动增量字段

需要一个没有主键的自动增量字段

问题描述:

我正在使用SQL-server2008.
对于我的表,我需要一个auto-Increment(int)字段.
但这不应该是主键字段.
我该如何实现..Plz提供步骤..

I am working with SQL-server2008.
For my table I need an auto-Increment(int) field.
But it should not be a Primary-key field.
How can I achieve this..Plz provide the steps..

除非您具有另一个Identity字段且必需的非Identity字段是从中得出.为什么首先要使其成为非标识?
There is no way to get it unless you have another Identity field and the required non identity field is derived from it. Why do you want to make it a non identity in the first place?


例如,在使用T-SQL创建表时,将IDENTITY 关键字用于适当的列.像这样的东西:
For example when creating the table with T-SQL use the IDENTITY keyword for the proper column. Something like:
CREATE TABLE MyTable (
   Col1 INT NOT NULL PRIMARY KEY,
   Col2 VARCHAR(100),
   Col3 BIGINT IDENTITY(1,1)
)


有关更多详细信息,请参见:创建表 [


For more details, see: CREATE TABLE[^]

[Addition]
Just saw you other post and based on that I got the impression you''re using a primary key with several other columns and this identity on alongside it. I would suggest to do it vice versa. Use this identity field as your primary key (both in the database and in the application) and have a unique key on the other columns.