需要一个没有主键的自动增量字段 [英] Need an AutoIncrement Field without Primary-Key

查看:65
本文介绍了需要一个没有主键的自动增量字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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.


这篇关于需要一个没有主键的自动增量字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆