MySQL 是否有命名约定?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7899200/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 21:26:51  来源:igfitidea点击:

Is there a naming convention for MySQL?

mysqlnaming-conventionsmysql-workbench

提问by StackOverflowNewbie

Here's how I do it:

这是我的方法:

  1. Table names are lower case, uses underscores to separate words, and are singular (e.g. foo, foo_bar, etc.
  2. I generally (not always) have a auto increment PK. I use the following convention: tablename_id(e.g. foo_id, foo_bar_id, etc.).
  3. When a table contains a column that is a foreign key, I just copy the column name of that key from whatever table it came from. For example, say table foo_barhas the FK foo_id(where foo_idis the PK of foo).
  4. When defining FKs to enforce referential integrity, I use the following: 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.
  5. I order the columns like this: PKs, FKs, then the rest of columns alphabetically
  1. 表名是小写,下划线的用途来分隔词语,并且是单数(例如foofoo_bar
  2. 我通常(并非总是)有一个自动增量 PK。我用下面的约定:tablename_id(例如foo_idfoo_bar_id等)。
  3. 当一个表包含一个作为外键的列时,我只是从它来自的任何表中复制该键的列名。例如,假设表foo_bar具有 FK foo_id(其中foo_id是 的 PK foo)。
  4. 在定义 FK 以强制执行参照完整性时,我使用以下内容:(tablename_fk_columnname例如进一步示例 3,它将是foo_bar_foo_id)。由于这是一个表名/列名组合,它保证在数据库中是唯一的。
  5. 我对列进行排序:PK、FK,然后按字母顺序排列其余列

Is there a better, more standard way to do this?

有没有更好、更标准的方法来做到这一点?

采纳答案by Tom Mac

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:

我认为您几乎已经完成了您在问题中概述的约定。不过有几点意见:

Points 1 and 2 are good I reckon.

我认为第 1 点和第 2 点很好。

Point 3 - sadly this is not always possible. Think about how you would cope with a single table foo_barthat has columns foo_idand another_foo_idboth of which reference the footable foo_idcolumn. You might want to consider how to deal with this. This is a bit of a corner case though!

第 3 点 - 遗憾的是,这并不总是可能的。想一想您将如何处理foo_bar具有列foo_idanother_foo_id都引用foofoo_id列的单个表。您可能需要考虑如何处理此问题。不过,这有点极端!

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.

第 4 点 - 与第 3 点类似。您可能希望在外键名称的末尾引入一个数字,以适应具有多个引用列的情况。

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.

第 5 点 - 我会避免这种情况。当您想在以后从表中添加或删除列时,它为您提供的帮助很少,并且会变得很头疼。

Some other points are:

其他一些要点是:

Index Naming Conventions

索引命名约定

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_idx1or foo_idx1- totally up to you but worth considering.

您可能希望为索引引入命名约定——这对您可能想要执行的任何数据库元数据工作都有很大帮助。例如,您可能只想调用索引foo_bar_idx1foo_idx1- 完全取决于您,但值得考虑。

Singular vs Plural Column Names

单数与复数列名

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 debatesin the DB community. I would stick with singular forms for both table names and columns. There. I've said it.

解决列名和表名中复数与单数的棘手问题可能是个好主意。这个主题经常在 DB 社区引起大讨论。对于表名和列,我会坚持使用单数形式。那里。我说过了。

The main thing here is of course consistency!

这里最重要的当然是一致性!

回答by mal-wan

Consistency is the key to any naming standard. As long as it's logical and consistent, you're 99% there.

一致性是任何命名标准的关键。只要它是合乎逻辑的和一致的,你就成功了 99%。

The standard itself is very much personal preference - so if you like your standard, then run with it.

标准本身在很大程度上是个人喜好 - 所以如果你喜欢你的标准,那就用它吧。

To answer your question outright - no, MySQL doesn't have a preferred naming convention/standard, so rolling your own is fine (and yours seems logical).

直接回答你的问题 - 不,MySQL 没有首选的命名约定/标准,所以滚动你自己的就好了(你的似乎合乎逻辑)。

回答by Daniel W.

MySQL has a short description of their more or less strict rules:

MySQL对其或多或少严格的规则有一个简短的描述:

https://dev.mysql.com/doc/internals/en/coding-style.html

https://dev.mysql.com/doc/internals/en/coding-style.html

Most common codingstyle for MySQL by Simon Holywell:

Simon Holywell 最常见的 MySQL 编码风格:

http://www.sqlstyle.guide/

http://www.sqlstyle.guide/

See also this question: Are there any published coding style guidelines for SQL?

另请参阅此问题: 是否有任何已发布的 SQL 编码风格指南?

回答by paulsm4

Thankfully, PHP developers aren't "Camel case bigots" like some development communities I know.

值得庆幸的是,PHP 开发人员不像我所知道的某些开发社区那样是“骆驼案例偏执狂”。

Your conventions sound fine.

你的约定听起来不错。

Just so long as they're a) simple, and b) consistent - I don't see any problems :)

只要它们 a) 简单,并且 b) 一致 - 我看不出任何问题:)

PS: Personally, I think 5) is overkill...

PS:就我个人而言,我认为 5) 是矫枉过正...