MySQL 有命名约定吗?
我是这样做的:
- 表名是小写的,使用下划线分隔单词,并且是单数(例如
foo
、foo_bar
等。 - 我通常(并不总是)有自动增量 PK。我使用以下约定:
tablename_id
(例如foo_id
、foo_bar_id
等) - 。是外键,我只是从该键来自的任何表中复制该键的列名称,例如,假设表
foo_bar
具有 FKfoo_id
(其中foo_id
)。是foo
的 PK)。 - 在定义 FK 来强制引用完整性时,我使用以下内容:
tablename_fk_columnname
(例如,进一步示例 3,它将是)foo_bar_foo_id
)。由于这是一个表名/列名组合,因此它在数据库中保证是唯一的 - :PKs、FKs,然后按字母顺序排列其余列
。有更好、更标准的方法吗?
Here's how I do it:
- Table names are lower case, use underscores to separate words, and are singular (e.g.
foo
,foo_bar
, etc. - I generally (not always) have an auto increment PK. I use the following convention:
tablename_id
(e.g.foo_id
,foo_bar_id
, etc.). - 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 FKfoo_id
(wherefoo_id
is the PK offoo
). - When defining FKs to enforce referential integrity, I use the following:
tablename_fk_columnname
(e.g. furthering example 3, it would befoo_bar_foo_id
). Since this is a table name/column name combination, it is guaranteed to be unique within the database. - 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我首先要说的是:保持一致。
我认为您已经差不多了解了您在问题中概述的约定。不过有几点评论:
我认为第 1 点和第 2 点很好。
第 3 点 - 遗憾的是这并不总是可能的。考虑一下您将如何处理包含列
foo_id
和another_foo_id
的单个表foo_bar
,这两个列都引用foo
表foo_id
列。您可能需要考虑如何处理这个问题。不过,这有点特殊!第 4 点 - 与第 3 点类似。您可能需要在外键名称末尾引入一个数字,以满足多个引用列的需要。
第 5 点 - 我会避免这种情况。它为您提供的功能很少,并且当您以后想要在表中添加或删除列时,它会变得令人头疼。
其他一些要点是:
索引命名约定
您可能希望引入索引命名约定 - 这对于您可能想要执行的任何数据库元数据工作都有很大帮助。例如,您可能只想调用索引
foo_bar_idx1
或foo_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 columnsfoo_id
andanother_foo_id
both of which reference thefoo
tablefoo_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
orfoo_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!
一致性是任何命名标准的关键。只要逻辑一致且一致,就 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).
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?
值得庆幸的是,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...
简单答案:不
,至少是 Oracle 或社区鼓励的命名约定,不,但是,基本上您必须注意遵循标识符的规则和限制,例如 MySQL 中所示的规则和限制文档: https://dev.mysql.com/doc/refman/8.0/en/identifiers.html
关于你遵循的命名约定,我认为还可以,只是数字5是一个有点不必要,我认为大多数用于管理数据库的可视化工具都提供了对列名称进行排序的选项(我使用 DBeaver,它有它),所以如果目的是对表进行良好的视觉呈现,您可以使用我提到的这个选项。
根据个人经验,我建议这样做:
lower_case_table_names
配置不正确,您的服务器仅仅因为无法识别您的驼峰命名法或帕斯卡命名法标准(区分大小写问题)而开始抛出错误。那么“复数与单数”命名又如何呢?嗯,这主要是个人喜好的情况。就我而言,我尝试对表使用复数名称,因为我认为表是元素的集合或包含元素的包,因此复数名称对我来说是有意义的;列的名称为单数,因为我将列视为对这些表元素进行单数描述的属性。
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:
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).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.
一致性是每个人都强烈建议的,剩下的就看你的了,只要它有效。
对于初学者来说,很容易得意忘形,我们当时想怎么命名就怎么命名。这在当时是有道理的,但后来却令人头疼。
foo
foobar
或foo_bar
很棒。我们尽可能直接地命名我们的表,并且仅在它们是两个不同的单词时才使用下划线。
studentregistration
到student_registration
就像 @Zbyszek 所说,有一个简单的
id
对于自动增量来说已经足够了。越简单越好。为什么需要foo_id
?我们很早就遇到了同样的问题,我们用表前缀命名所有列。例如foo_id
、foo_name
、foo_age
。我们现在删除了表名,只保留了尽可能短的列。由于我们仅使用 PK 的 id,因此我们将使用
foo_bar_fk
(表名是唯一的,后跟唯一的 PK,后跟_fk
)作为外键。我们不会将id
添加到列名称中,因为据说名称“id”始终是给定表的 PK。所以我们只有表名和末尾的_fk
。对于约束,我们删除所有下划线并使用驼峰式命名法(表名 + Colname + Fk)
foobarUsernameFk
(对于 username_fk col)进行连接。这只是我们遵循的一种方式。我们为每个名称结构保留一个文档。当保持 col 名称简短时,我们还应该注意 RESTRICTED 名称。
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
orfoo_bar
is great.We name our table straight forward as much as possible and only use underscore if they are two different words.
studentregistration
tostudent_registration
like @Zbyszek says, having a simple
id
is more than enough for the auto-increment. The simplier the better. Why do you needfoo_id
? We had the same problem early on, we named all our columns with the table prefix. likefoo_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 addid
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.
正如@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?