database 表命名:下划线与驼峰式大小写?命名空间?单数还是复数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1881123/
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
Table Naming: Underscore vs Camelcase? namespaces? Singular vs Plural?
提问by MarioRicalde
I've been reading a couple of questions/answers on StackOverflow trying to find the 'best', or should I say must accepted way, to name tables on a Database.
我一直在阅读有关 StackOverflow 的几个问题/答案,试图找到“最佳”,或者我应该说必须接受的方式,来命名数据库中的表。
Most of the developers tend to name the tables depending on the language that requires the database (JAVA, .NET, PHP, etc). However I just feel this isn't right.
大多数开发人员倾向于根据需要数据库的语言(JAVA、.NET、PHP 等)来命名表。然而我就是觉得这不对。
The way I've been naming tables till now is doing something like:
到目前为止,我一直在为表命名的方式是:
doctorsMain
doctorsProfiles
doctorsPatients
patientsMain
patientsProfiles
patientsAntecedents
The things I'm concerned are:
我担心的事情是:
- Legibility
- Quick identifying of the module the table is from (doctors||patients)
- Easy to understand, to prevent confusions.
- 易读性
- 快速识别表格来自的模块(医生||患者)
- 通俗易懂,防止混淆。
I would like to read any opinions regarding naming conventions. Thank you.
我想阅读有关命名约定的任何意见。谢谢你。
回答by David Oneill
Being consistent is far more important than what particular scheme you use.
保持一致远比您使用的特定方案重要得多。
回答by Justin Niessner
I typically use PascalCase and the entities are singular:
我通常使用 PascalCase 并且实体是单数的:
DoctorMain
DoctorProfile
DoctorPatient
It mimics the naming conventions for classes in my application keeping everything pretty neat, clean, consistent, and easy to understand for everybody.
它模仿了我的应用程序中类的命名约定,使每个人都保持整洁、干净、一致且易于理解。
回答by terR0Q
Case insensitive nature of SQL supports Underscores_Scheme
. Modern software however supports any kind of naming scheme. However sometimes some nasty bugs, errors or human factor can lead to UPPERCASINGEVERYTHING
so that those, who selected both Pascal_Case
and Underscore_Case
scheme live with all their nerves in good place.
SQL 不区分大小写的特性支持Underscores_Scheme
. 然而,现代软件支持任何类型的命名方案。然而,有时一些令人讨厌的错误、错误或人为因素可能会导致UPPERCASINGEVERYTHING
那些选择两者Pascal_Case
并Underscore_Case
计划的人在良好的位置上全力以赴。
回答by KenFar
An aggregation of most of the above:
以上大部分内容的汇总:
- don't rely on case in the database
- don't consider the case or separator part of the name - just the words
- do use whatever separator or case is the standard for your language
- 不要依赖数据库中的大小写
- 不要考虑名称的大小写或分隔符部分 - 只是单词
- 请使用任何分隔符或大小写是您语言的标准
Then you can easily translate (even automatically) names between environments.
然后您可以轻松地在环境之间翻译(甚至自动)名称。
But I'd add another consideration: you may find that there are other factors when you move from a class in your app to a table in your database: the database object has views, triggers, stored procs, indexes, constraints, etc - that also need names. So for example, you may find yourself only accessing tables via views that are typically just a simple "select * from foo". These may be identified as the table name with just a suffix of '_v' or you could put them in a different schema. The purpose for such a simple abstraction layer is that it can be expanded when necessary to allow changes in one environment to avoid impacting the other. This wouldn't break the above naming suggestions - just a few more things to account for.
但是我要添加另一个考虑因素:当您从应用程序中的类移动到数据库中的表时,您可能会发现还有其他因素:数据库对象具有视图、触发器、存储过程、索引、约束等 -还需要名字。因此,例如,您可能会发现自己只能通过通常只是简单的“select * from foo”的视图访问表。这些可以被标识为仅带有“_v”后缀的表名,或者您可以将它们放在不同的模式中。这样一个简单的抽象层的目的是在必要时可以扩展它以允许在一个环境中进行更改以避免影响另一个环境。这不会破坏上述命名建议 - 只需要考虑更多的事情。
回答by Ray
I use underscores. I did an Oracle project some years ago, and it seemed that Oracle forced all my object names to upper case, which kind of blows any casing scheme. I am not really an Oracle guy, so maybe there was a way around this that I wasn't aware of, but it made me use underscores and I have never gone back.
我使用下划线。几年前我做了一个 Oracle 项目,似乎 Oracle 强制我所有的对象名称都大写,这有点打击任何大小写方案。我不是一个真正的 Oracle 人,所以也许有一种我不知道的方法可以解决这个问题,但它让我使用了下划线并且我再也没有回去过。
回答by rich remer
Since the question is not specific to a particular platform or DB engine, I must say for maximum portability, you should always use lowercase table names.
由于问题不是特定于特定平台或数据库引擎,我必须说为了最大的可移植性,您应该始终使用小写的表名。
/[a-z_][a-z0-9_]*/ is really the only pattern of names that seamlessly translates between different platforms. Lowercase alpha-numeric+underscore will always work consistently.
/[a-z_][a-z0-9_]*/ 确实是唯一一种可以在不同平台之间无缝转换的名称模式。小写字母数字+下划线将始终保持一致。
As mentioned elsewhere, relation(table) names should be singular: http://www.teamten.com/lawrence/programming/use-singular-nouns-for-database-table-names.html
正如其他地方提到的,关系(表)名称应该是单数:http: //www.teamten.com/lawrence/programming/use-singular-nouns-for-database-table-names.html
回答by Mark Rendle
I tend to agree with the people who say it depends on the conventions of language you're using (e.g. PascalCase for C# and snake_case for Ruby).
我倾向于同意那些说这取决于您使用的语言约定(例如,C# 的 PascalCase 和 Ruby 的 snake_case)的人的看法。
Never camelCase, though.
不过,从来没有骆驼案例。
回答by Vlad Manuel Mure?an
After reading a lot of other opinions I think it's very important to use the naming conventions of the language, consistency is more important than naming conventions only if you're (and will be) the only developer of the application. If you want readability (which is of huge importance) you better use the naming conventions for each language. In MySQL for example, I don't suggest using CamelCase since not all platforms are case sensitive. So here underscore goes better.
在阅读了很多其他意见之后,我认为使用语言的命名约定非常重要,只有当您是(并且将是)应用程序的唯一开发人员时,一致性才比命名约定更重要。如果您想要可读性(这非常重要),您最好使用每种语言的命名约定。例如,在 MySQL 中,我不建议使用 CamelCase,因为并非所有平台都区分大小写。所以这里下划线更好。
回答by Pavel_K
These are my five cents. I came to conclusion that if DBs from different vendors are used for one project there are two best ways:
这是我的五分钱。我得出的结论是,如果将来自不同供应商的 DB 用于一个项目,则有两种最佳方法:
- Use underscores.
- Use camel case with quotes.
- 使用下划线。
- 使用带引号的驼峰案例。
The reason is that some database will convert all characters to uppercase and some to lowercase. So, if you have myTable
it will become MYTABLE
or mytable
when you will work with DB.
原因是某些数据库会将所有字符转换为大写,有些将转换为小写。因此,如果您拥有myTable
它,它将成为MYTABLE
或mytable
何时您将使用 DB。
回答by Chris Golledge
Naming conventions exist within the scope of a language, and different languages have different naming conventions.
命名约定存在于一种语言的范围内,不同的语言有不同的命名约定。
SQL is case-insensitive by default; so, snake_case is a widely used convention. SQL also supports delimited identifiers; so, mixed case in an option, like camelCase (Java, where fields == columns) or PascalCase (C#, where tables == classes and columns == fields). If your DB engine can't support the SQL standard, that's its problem. You can decide to live with that or choose another engine. (And why C# just had to be different is a point of aggravation for those of us who code in both.)
SQL 默认不区分大小写;所以,snake_case 是一个广泛使用的约定。SQL 还支持分隔标识符;所以,在一个选项中混合大小写,如驼峰式(Java,其中字段 == 列)或 PascalCase(C#,其中表 == 类和列 == 字段)。如果您的数据库引擎不能支持 SQL 标准,那就是它的问题。您可以决定接受它或选择其他引擎。(为什么 C# 必须与众不同,这让我们这些同时编写这两种代码的人感到恼火。)
If you intend to ever only use one language in your services and applications, use the conventions of that language at all layers. Else, use the most widely used conventions of the language in the domain where that language is used.
如果您打算在您的服务和应用程序中只使用一种语言,请在所有层使用该语言的约定。否则,在使用该语言的领域中使用该语言的最广泛使用的约定。