这是我的做法:
foo
、foo_bar
等tablename_id
(例如 foo_id
、foo_bar_id
等).foo_bar
具有 FK foo_id
(其中 foo_id
是 foo
的 PK).莉>tablename_fk_columnname
(例如进一步示例 3,它将是 foo_bar_foo_id
).由于这是一个表名/列名组合,所以它保证在数据库中是唯一的.foo
, foo_bar
, etc.tablename_id
(e.g. foo_id
, foo_bar_id
, etc.).foo_bar
has the FK foo_id
(where foo_id
is the PK of foo
).tablename_fk_columnname
(e.g. furthering example 3, it would be foo_bar_foo_id
). Since this is a table name/column name combination, it is guaranteed to be unique within the database.有没有更好、更标准的方法来做到这一点?
Is there a better, more standard way to do this?
我要说的第一点是:保持一致.
I would say that first and foremost: be consistent.
我认为您已经了解了您在问题中概述的约定.不过有几点意见:
I reckon you are almost there with the conventions that you have outlined in your question. A couple of comments though:
我认为第 1 点和第 2 点很好.
Points 1 and 2 are good I reckon.
第 3 点 - 遗憾的是这并不总是可能的.想一想您将如何处理具有列 foo_id
和 another_foo_id
的单个表 foo_bar
,它们都引用了 foo
> 表 foo_id
列.您可能需要考虑如何处理此问题.不过,这有点极端!
Point 3 - sadly this is not always possible. Think about how you would cope with a single table foo_bar
that has columns foo_id
and another_foo_id
both of which reference the foo
table foo_id
column. You might want to consider how to deal with this. This is a bit of a corner case though!
第 4 点 - 与第 3 点类似.您可能需要在外键名称的末尾引入一个数字,以适应有多个引用列.
Point 4 - Similar to Point 3. You may want to introduce a number at the end of the foreign key name to cater for having more than one referencing column.
第 5 点 - 我会避免这种情况.当您想在以后从表中添加或删除列时,它为您提供的帮助很少,并且会变得很头疼.
Point 5 - I would avoid this. It provides you with little and will become a headache when you want to add or remove columns from a table at a later date.
其他一些要点是:
索引命名约定
您可能希望为索引引入命名约定——这对您可能想要执行的任何数据库元数据工作都有很大帮助.例如,您可能只想调用索引 foo_bar_idx1
或 foo_idx1
- 完全取决于您,但值得考虑.
You may wish to introduce a naming convention for indexes - this will be a great help for any database metadata work that you might want to carry out. For example you might just want to call an index foo_bar_idx1
or foo_idx1
- totally up to you but worth considering.
单列名与复数列名
解决列名和表名中复数与单数的棘手问题可能是个好主意.这个主题经常在数据库社区中引起大辩论.对于表名和列,我会坚持使用单数形式.那里.我说过了
It might be a good idea to address the thorny issue of plural vs single in your column names as well as your table name(s). This subject often causes big debates in the DB community. I would stick with singular forms for both table names and columns. There. I've said it.
这里最重要的当然是一致性!
The main thing here is of course consistency!
这篇关于MySQL 是否有命名约定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!