主键/外键命名约定 [英] Primary key/foreign Key naming convention

查看:161
本文介绍了主键/外键命名约定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的开发团队中,我们对小键盘和外键的命名约定进行了激烈的辩论。我们组中基本上有两个思路:

In our dev group we have a raging debate regarding the naming convention for Primary and Foreign Keys. There's basically two schools of thought in our group:

1:

Primary Table (Employee)   
Primary Key is called ID

Foreign table (Event)  
Foreign key is called EmployeeID

2:

Primary Table (Employee)  
Primary Key is called EmployeeID

Foreign table (Event)  
Foreign key is called EmployeeID

我不想在任何列中复制表的名称(所以我更喜欢上面的选项1)。在概念上,它与其他语言中的许多推荐做法是一致的,您不会在其属性名称中使用对象的名称。我认为命名外键 EmployeeID (或 Employee_ID 可能会更好)告诉读者,它是<$ Employee 表中的c $ c> ID 列。

I prefer not to duplicate the name of the table in any of the columns (So I prefer option 1 above). Conceptually, it is consistent with a lot of the recommended practices in other languages, where you don't use the name of the object in its property names. I think that naming the foreign key EmployeeID (or Employee_ID might be better) tells the reader that it is the ID column of the Employee Table.

其他一些人喜欢选项2,其中您将主键命名为表名,以便列名在整个数据库中相同。我看到这一点,但是现在你不能在视觉上区分主键和外键。

Some others prefer option 2 where you name the primary key prefixed with the table name so that the column name is the same throughout the database. I see that point, but you now can not visually distinguish a primary key from a foreign key.

此外,我认为在列名中具有表名称是多余的,因为如果将表视为实体和列作为属性或属性的属性该实体,您将其视为员工的 Employee 的I​​D属性,而不是员工的 EmployeeID 属性。我不会问我的同事他的 PersonAge PersonGender 是。我问他他的年龄是什么。

Also, I think it's redundant to have the table name in the column name, because if you think of the table as an entity and a column as a property or attribute of that entity, you think of it as the ID attribute of the Employee, not the EmployeeID attribute of an employee. I don't go an ask my coworker what his PersonAge or PersonGender is. I ask him what his Age is.

所以就像我说的,这是一场激烈的辩论,我们继续下去。我有兴趣得到一些新的观点。

So like I said, it's a raging debate and we go on and on and on about it. I'm interested to get some new perspectives.

推荐答案

没关系。我从来没有遇到过选择1和选择2之间有真正差异的系统。

It doesn't really matter. I've never run into a system where there is a real difference between choice 1 and choice 2.

Jeff Atwood在这个话题上有一篇很棒的文章。基本上人们争论最激烈地争论那些不能被证明是错误的话题。或者从不同的角度来看,那些只能通过基于最后一个人的观点的阻止风格耐力获得的话题。

Jeff Atwood had a great article a while back on this topic. Basically people debate and argue the most furiously those topics which they cannot be proven wrong on. Or from a different angle, those topics which can only be won through filibuster style endurance based last-man-standing arguments.

选择一个,并告诉他们专注于实际上会影响你的代码。

Pick one and tell them to focus on issues that actually impact your code.

编辑:如果你想玩得开心,请给他们详细说明为什么他们的方法对于递归表引用是优越的。

If you want to have fun, have them specify at length why their method is superior for recursive table references.

这篇关于主键/外键命名约定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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