唯一约束的命名约定 [英] Naming convention for unique constraint
问题描述
命名约定很重要,主键和外键是常用且显而易见的约定(分别为PK_Table
和FK_Table_ReferencedTable
).索引的IX_Table_Column
命名也很标准.
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.
那UNIQUE约束呢?此约束是否有公认的命名约定?我见过UK_TableName_Column
,UQ_TableName_Column
,并且有人推荐AX_TableName_Column
-我不知道那是哪里来的.
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.
我通常使用UQ
,但是我并不特别喜欢它,而且我不喜欢不得不针对UK
拥护者来捍卫自己使用它的选择.
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.
推荐答案
我的想法不是关键:这是一个约束.
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.
一个例子是该密钥是"ThingID",它是代替ThingName作为自然密钥的替代 key .您仍然需要 constraint ThingName:虽然它不会用作键.
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.
我还将使用UQ和UQC(如果已群集).
I'd also use UQ and UQC (if clustered).
您可以改为使用唯一索引,然后使用"IXU".根据所采用的逻辑,索引也是键,但仅在唯一时才是键.否则,它是一个索引.因此,我们将从IK_columnname
表示唯一索引,而从IX_columnname
表示非唯一索引.很棒.
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 and IX_columnname
for non-unique indexes. Marvellous.
唯一约束和唯一索引之间的唯一区别是INCLUDE列.
And the only difference between a unique constraint and a unique index is INCLUDE columns.
2013年2月.自SQL Server 2008起,索引也可以具有过滤器.约束不能
Feb 2013. Since SQL Server 2008, indexes can have filters too. Constraints can not
所以,它归结为以下其中之一
So, it comes down to one of
- 按照使用SQL的其余部分坚持使用UQ
- 将IK用于唯一索引(也将IKC用于集群索引)以保持一致...
这篇关于唯一约束的命名约定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!