SQL Server自动增加没有主键的列 [英] SQL Server auto increment a column without primary key

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

问题描述

是否可以在 SQL Server 中自动增加一列而不是主键?

Is it possible to auto increment a column in SQL Server WITHOUT it being a primary key?

如果是,怎么做.

谢谢

推荐答案

是的.不要求将 IDENTITY 列设为主键.

Yes. There is no requirement that IDENTITY columns be made a primary key.

CREATE TABLE T
(
X INT PRIMARY KEY,
Y INT IDENTITY(1,1)
)

虽然我不确定这什么时候有用.如果您有一个想要用作 PK 的自然键,那么您可能无论如何都希望对代理备用键施加唯一约束.

Though I'm not sure when this would be useful. If you have a natural key that you want to use as the PK then you would probably want to put a unique constraint on the surrogate alternate key anyway.

为了建立 FK 关系,SQL Server 不关心列是否是 PK,它只需要一个唯一的索引.

For purposes of setting up FK relationships SQL Server doesn't care if the column(s) is the PK or not it just requires a unique index on it/them.

这篇关于SQL Server自动增加没有主键的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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