MySQL 有命名约定吗?

发布于 2024-12-12 03:08:35 字数 551 浏览 0 评论 0原文

我是这样做的:

  1. 表名是小写的,使用下划线分隔单词,并且是单数(例如 foofoo_bar 等。
  2. 我通常(并不总是)有自动增量 PK。我使用以下约定:tablename_id(例如 foo_idfoo_bar_id 等)
  3. 。是外键,我只是从该键来自的任何表中复制该键的列名称,例如,假设表 foo_bar 具有 FK foo_id (其中 foo_id)。是 foo 的 PK)。
  4. 在定义 FK 来强制引用完整性时,我使用以下内容:tablename_fk_columnname(例如,进一步示例 3,它将是) foo_bar_foo_id)。由于这是一个表名/列名组合,因此它在数据库中保证是唯一的
  5. :PKs、FKs,然后按字母顺序排列其余列

。有更好、更标准的方法吗?

Here's how I do it:

  1. Table names are lower case, use underscores to separate words, and are singular (e.g. foo, foo_bar, etc.
  2. I generally (not always) have an 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_bar has the FK foo_id (where foo_id is 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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

尐偏执 2024-12-19 03:08:35

我首先要说的是:保持一致。

我认为您已经差不多了解了您在问题中概述的约定。不过有几点评论:

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

第 3 点 - 遗憾的是这并不总是可能的。考虑一下您将如何处理包含列 foo_idanother_foo_id 的单个表 foo_bar,这两个列都引用 foofoo_id 列。您可能需要考虑如何处理这个问题。不过,这有点特殊!

第 4 点 - 与第 3 点类似。您可能需要在外键名称末尾引入一个数字,以满足多个引用列的需要。

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

其他一些要点是:

索引命名约定

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

单数与复数列名

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

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

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.

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!

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.

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.

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

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 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!

爱要勇敢去追 2024-12-19 03:08:35

一致性是任何命名标准的关键。只要逻辑一致且一致,就 99% 都成功了。

标准本身很大程度上取决于个人喜好——因此,如果您喜欢您的标准,那么就使用它。

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

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

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).

落墨 2024-12-19 03:08:35

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

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

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

https://www.sqlstyle.guide/

另请参阅此问题:

是否有任何已发布的 SQL 编码风格指南?

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

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

The most common codingstyle for MySQL by Simon Holywell:

https://www.sqlstyle.guide/

See also this question:

Are there any published coding style guidelines for SQL?

冷默言语 2024-12-19 03:08:35

值得庆幸的是,PHP 开发人员并不像我所知道的一些开发社区那样“偏执驼峰式”。

你的惯例听起来不错。

只要它们 a) 简单,b) 一致 - 我没有看到任何问题:)

PS:
就我个人而言,我认为5)太过分了......

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

Your conventions sound fine.

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

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

初与友歌 2024-12-19 03:08:35

简单答案:

,至少是 Oracle 或社区鼓励的命名约定,不,但是,基本上您必须注意遵循标识符的规则和限制,例如 MySQL 中所示的规则和限制文档: https://dev.mysql.com/doc/refman/8.0/en/identifiers.html

关于你遵循的命名约定,我认为还可以,只是数字5是一个有点不必要,我认为大多数用于管理数据库的可视化工具都提供了对列名称进行排序的选项(我使用 DBeaver,它有它),所以如果目的是对表进行良好的视觉呈现,您可以使用我提到的这个选项。

根据个人经验,我建议这样做:

  • 使用小写。当您将数据库从一台服务器迁移到另一台服务器时,这几乎可以确保互操作性。有时,lower_case_table_names 配置不正确,您的服务器仅仅因为无法识别您的驼峰命名法或帕斯卡命名法标准(区分大小写问题)而开始抛出错误。
  • 简称。简单明了。最简单、最快速的是识别您的表或列,效果越好。相信我,当您在短时间内进行大量不同的查询时,最好让所有查询都易于编写(和读取)。
  • 避免前缀。除非您对不同应用程序的表使用相同的数据库,否则不要使用前缀。这只会让您的查询变得更加冗长。在某些情况下,这可能很有用,例如,当您想要标识主键和外键时,通常使用表名作为 id 列的前缀。
  • 使用下划线分隔单词。如果您仍然想使用多个单词来命名表、列等,请使用下划线来分隔单词,这有助于提高可读性(您的眼睛和紧张的大脑会感谢您的) 。
  • 保持一致。一旦你有了自己的标准,就遵循它。不要成为制定规则的人,而第一个打破规则的人,这是可耻的。

那么“复数与单数”命名又如何呢?嗯,这主要是个人喜好的情况。就我而言,我尝试对表使用复数名称,因为我认为表是元素的集合或包含元素的包,因此复数名称对我来说是有意义的;列的名称为单数,因为我将列视为对这些表元素进行单数描述的属性。

Simple Answer: NO

Well, at least a naming convention as such encouraged by Oracle or community, no, however, basically you have to be aware of following the rules and limits for identifiers, such as indicated in MySQL documentation: https://dev.mysql.com/doc/refman/8.0/en/identifiers.html

About the naming convention you follow, I think it is ok, just the number 5 is a little bit unnecesary, I think most visual tools for managing databases offer a option for sorting column names (I use DBeaver, and it have it), so if the purpouse is having a nice visual presentation of your table you can use this option I mention.

By personal experience, I would recommed this:

  • Use lower case. This almost ensures interoperability when you migrate your databases from one server to another. Sometimes the lower_case_table_names is not correctly configured and your server start throwing errors just by simply unrecognizing your camelCase or PascalCase standard (case sensitivity problem).
  • Short names. Simple and clear. The most easy and fast is identify your table or columns, the better. Trust me, when you make a lot of different queries in a short amount of time is better having all simple to write (and read).
  • Avoid prefixes. Unless you are using the same database for tables of different applications, don't use prefixes. This only add more verbosity to your queries. There are situations when this could be useful, for example, when you want to indentify primary keys and foreign keys, that usually table names are used as prefix for id columns.
  • Use underscores for separating words. If you still want to use more than one word for naming a table, column, etc., so use underscores for separating_the_words, this helps for legibility (your eyes and your stressed brain are going to thank you).
  • Be consistent. Once you have your own standard, follow it. Don´t be the person that create the rules and is the first who breaking them, that is shameful.

And what about the "Plural vs Singular" naming? Well, this is most a situation of personal preferences. In my case I try to use plural names for tables because I think a table as a collection of elements or a package containig elements, so a plural name make sense for me; and singular names for columns because I see columns as attributes that describe singularly to those table elements.

妄想挽回 2024-12-19 03:08:35

一致性是每个人都强烈建议的,剩下的就看你的了,只要它有效。

对于初学者来说,很容易得意忘形,我们当时想怎么命名就怎么命名。这在当时是有道理的,但后来却令人头疼。

foo foobarfoo_bar 很棒。
我们尽可能直接地命名我们的表,并且仅在它们是两个不同的单词时才使用下划线。 studentregistrationstudent_registration

就像 @Zbyszek 所说,有一个简单的 id 对于自动增量来说已经足够了。越简单越好。为什么需要foo_id?我们很早就遇到了同样的问题,我们用表前缀命名所有列。例如 foo_idfoo_namefoo_age。我们现在删除了表名,只保留了尽可能短的列。

由于我们仅使用 PK 的 id,因此我们将使用 foo_bar_fk (表名是唯一的,后跟唯一的 PK,后跟 _fk)作为外键。我们不会将 id 添加到列名称中,因为据说名称“id”始终是给定表的 PK。所以我们只有表名和末尾的 _fk

对于约束,我们删除所有下划线并使用驼峰式命名法(表名 + Colname + Fk)foobarUsernameFk(对于 username_fk col)进行连接。这只是我们遵循的一种方式。我们为每个名称结构保留一个文档。

当保持 col 名称简短时,我们还应该注意 RESTRICTED 名称。

+------------------------------------+
|               foobar               |
+------------------------------------+
| id (PK for the current table)      |
| username_fk (PK of username table) |
| location (other column)            |
| tel (other column)                 |
+------------------------------------+

Consistency is what everyone strongly suggest, the rest is upto you as long as it works.

For beginners its easy to get carried away and we name whatever we want at that time. This make sense at that point but a headache later.

foo foobar or foo_bar is great.
We name our table straight forward as much as possible and only use underscore if they are two different words. studentregistration to student_registration

like @Zbyszek says, having a simple id is more than enough for the auto-increment. The simplier the better. Why do you need foo_id? We had the same problem early on, we named all our columns with the table prefix. like foo_id, foo_name, foo_age. We dropped the tablename now and kept only the col as short as possible.

Since we are using just an id for PK we will be using foo_bar_fk (tablename is unique, folowed by the unique PK, followed by the _fk) as foreign key. We don't add id to the col name because it is said that the name 'id' is always the PK of the given table. So we have just the tablename and the _fk at the end.

For constrains we remove all underscores and join with camelCase (tablename + Colname + Fk) foobarUsernameFk (for username_fk col). It's just a way we are following. We keep a documentation for every names structures.

When keeping the col name short, we should also keep an eye on the RESTRICTED names.

+------------------------------------+
|               foobar               |
+------------------------------------+
| id (PK for the current table)      |
| username_fk (PK of username table) |
| location (other column)            |
| tel (other column)                 |
+------------------------------------+
荒人说梦 2024-12-19 03:08:35

正如@fabrizio-valencia所说,使用小写。在 Windows 中,如果导出 mysql 数据库(phpmyadmin),表名称将转换为小写,这会导致各种
问题。
请参阅 MySQL 中的表名称区分大小写吗?

as @fabrizio-valencia said use lower case. in windows if you export mysql database (phpmyadmin) the tables name will converted to lower case and this lead to all sort of
problems.
see Are table names in MySQL case sensitive?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文