唯一约束的命名约定
命名约定很重要,主键和外键有常用且明显的约定(分别为PK_Table
和FK_Table_ReferencedTable
)。索引的 IX_Table_Column
命名也相当标准。
那么 UNIQUE 约束呢?此约束是否有普遍接受的命名约定?我见过 UK_TableName_Column
、UQ_TableName_Column
,还有人推荐 AX_TableName_Column
- 我不知道它来自哪里。
我通常使用 UQ
,但我不是特别喜欢它,而且我不喜欢必须针对 UK
的拥护者来捍卫我使用它的选择。
我只是想看看是否就最流行的命名达成了共识,或者是否有一个很好的理由来解释为什么一个命名比其他命名更有意义。
Naming conventions are important, and primary key and foreign key have commonly used and obvious conventions (PK_Table
and FK_Table_ReferencedTable
, respectively). The IX_Table_Column
naming for indexes is also fairly standard.
What about the UNIQUE constraint? Is there a commonly accepted naming convention for this constraint? I've seen UK_TableName_Column
, UQ_TableName_Column
, and someone recommending AX_TableName_Column
- I don't know where that comes from.
I've typically used UQ
but I don't particularly like it, and I do not enjoy having to defend my choice of using it against a UK
advocate.
I would simply like to see if there is a consensus on the most prevalent naming, or a good reasoning as to why one makes more sense than the others.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我的索引和约束的命名约定:
_PK
_AK{xx}
_IX{xx}
_CK{xx}
_DF{xx}
_FK{xx}
其中
{xx}
是 2 位序列号,每个表的每种约束类型从 01 开始。主键没有序列号,因为只能有一个。 2 个字符的字母后缀含义为:PK
AK
FK
IX
CK
DF
我通常希望按控制对象而不是按对象类型对元数据/系统目录数据进行分组。
My naming convention for indices and constraints:
_PK
_AK{xx}
_IX{xx}
_CK{xx}
_DF{xx}
_FK{xx}
Where
{xx}
is a 2-digit sequence number, starting at 01 for each constraint type per table. Primary key doesn't get a sequence number since there can be only one. The 2-char alpha suffix meanings are:PK
AK
FK
IX
CK
DF
I generally want to group metadata/system catalog data by the controlling object rather than by object type.
我的想法是这不是关键:它是一个约束。
当然,它可以用作键,并且唯一标识一行,但它不是键。
例如,密钥是“ThingID”,它是用于代替自然密钥 ThingName 的代理密钥。您仍然需要约束 ThingName:但它不会用作密钥。
我还会使用 UQ 和 UQC(如果是集群的话)。
您可以使用唯一索引来代替“IXU”。根据所采用的逻辑,索引也是键,但前提是唯一。否则它就是一个索引。因此,我们首先使用
IK_columnname
表示唯一索引,使用IX_columnname
表示非唯一索引。奇妙。唯一约束和唯一索引之间的唯一区别是 INCLUDE 列。
编辑:2013 年 2 月。从 SQL Server 2008 开始,索引也可以有过滤器。约束不能
所以,归结为
My thinking is it isn't a key: it's a constraint.
It could be used as a key of course, and uniquely identifies a row, but it isn't the key.
An example would be that the key is "ThingID", a surrogate key used in place of ThingName the natural key. You still need to constrain ThingName: it won't be used as a key though.
I'd also use UQ and UQC (if clustered).
You could use a unique index instead and go for "IXU". By the logic employed, an index is also a key but only when unique. Otherwise it's an index. So then we'd start with
IK_columnname
for unique indexes andIX_columnname
for non-unique indexes. Marvellous.And the only difference between a unique constraint and a unique index is INCLUDE columns.
Edit: Feb 2013. Since SQL Server 2008, indexes can have filters too. Constraints can not
So, it comes down to one of
我用的是昆士兰大学。英国的 K 让我想起 K,因为它用于 PK 和 FK。好吧,无论如何,在我想到英国之后;具有讽刺意味的是,当英国提出了如此多的其他协会时,这应该是 UNIQUE 的前缀 =)
I use UQ. The K in UK makes me think of K as it's used in PK and FK. Well, after I think of United Kingdom anyways; ironic that this should be a prefix for UNIQUE when UK brings up so many other associations =)