每当我设计数据库时,我总是想知道是否有一种最好的方法来命名数据库中的项目。我经常问自己以下问题:
表名应该是复数吗? 列名应该是单数吗? 我应该为表或列添加前缀吗? 我应该在命名项目时使用大小写吗?
是否有推荐的指导原则来命名数据库中的项?
每当我设计数据库时,我总是想知道是否有一种最好的方法来命名数据库中的项目。我经常问自己以下问题:
表名应该是复数吗? 列名应该是单数吗? 我应该为表或列添加前缀吗? 我应该在命名项目时使用大小写吗?
是否有推荐的指导原则来命名数据库中的项?
当前回答
虽然很晚了,但我仍然想对列前缀发表我的意见
对于使用table_column(或tableColumn)列命名标准,似乎有两个主要的论据,都是基于列名本身在整个数据库中是唯一的这一事实:
1)你不需要一直在你的查询中指定表名和/或列别名
2)你可以很容易地在整个代码中搜索列名
我认为这两种观点都有缺陷。不使用前缀解决这两个问题很简单。以下是我的建议:
在SQL中始终使用表名。例如,总是用table。列而不是列。
它显然解决了2)你现在只需要搜索表。而不是table_column。
But I can hear you scream, how does it solve 1)? It was exactly about avoiding this. Yes, it was, but the solution was horribly flawed. Why? Well, the prefix solution boils down to: To avoid having to specify table.column when there's ambiguity, you name all your columns table_column! But this means you will from now on ALWAYS have to write the column name every time you specify a column. But if you have to do that anyways, what's the benefit over always explicitly writing table.column? Exactly, there is no benefit, it's the exact same number of characters to type.
编辑:是的,我知道用前缀命名列可以强制使用正确的用法,而我的方法依赖于程序员
其他回答
我知道这有点晚了,这个问题已经得到了很好的回答,但我想就#3关于列名前缀的问题提出我的看法。
所有列都应该使用一个对定义它们的表唯一的前缀命名。
例如,给定表“customer”和“address”,让我们分别使用前缀“cust”和“addr”。"customer"中会有"cust_id", "cust_name"等。“address”将包含“addr_id”,“addr_cust_id”(FK返回给客户),“addr_street”等。
当我第一次看到这个标准时,我坚决反对它;我讨厌这个主意。我无法忍受所有额外的输入和冗余。现在我已经有了足够的经验,我再也不会回去了。
这样做的结果是数据库模式中的所有列都是唯一的。这有一个主要的好处,它压倒了所有反对它的论点(当然,在我看来):
您可以搜索整个代码库,并可靠地找到涉及特定列的每一行代码。
The benefit from #1 is incredibly huge. I can deprecate a column and know exactly what files need to be updated before the column can safely be removed from the schema. I can change the meaning of a column and know exactly what code needs to be refactored. Or I can simply tell if data from a column is even being used in a particular portion of the system. I can't count the number of times this has turned a potentially huge project into a simple one, nor the amount of hours we've saved in development work.
另一个相对较小的好处是,当你进行自连接时,你只需要使用表别名:
SELECT cust_id, cust_name, addr_street, addr_city, addr_state
FROM customer
INNER JOIN address ON addr_cust_id = cust_id
WHERE cust_name LIKE 'J%';
虽然很晚了,但我仍然想对列前缀发表我的意见
对于使用table_column(或tableColumn)列命名标准,似乎有两个主要的论据,都是基于列名本身在整个数据库中是唯一的这一事实:
1)你不需要一直在你的查询中指定表名和/或列别名
2)你可以很容易地在整个代码中搜索列名
我认为这两种观点都有缺陷。不使用前缀解决这两个问题很简单。以下是我的建议:
在SQL中始终使用表名。例如,总是用table。列而不是列。
它显然解决了2)你现在只需要搜索表。而不是table_column。
But I can hear you scream, how does it solve 1)? It was exactly about avoiding this. Yes, it was, but the solution was horribly flawed. Why? Well, the prefix solution boils down to: To avoid having to specify table.column when there's ambiguity, you name all your columns table_column! But this means you will from now on ALWAYS have to write the column name every time you specify a column. But if you have to do that anyways, what's the benefit over always explicitly writing table.column? Exactly, there is no benefit, it's the exact same number of characters to type.
编辑:是的,我知道用前缀命名列可以强制使用正确的用法,而我的方法依赖于程序员
请参阅ISO 11179-5:命名和识别原则 你可以在这里获得:http://metadata-standards.org/11179/#11179-5
我之前写过一篇博文:ISO-11179命名约定
这里的回答有点晚,但简而言之:
复数表名:我的偏好是复数 单个列名:是的 前缀表或列:
表:*通常*没有前缀是最好的。 列:没有。
在命名项时使用任何大小写:表和列都使用PascalCase。
细化:
(1)你必须做什么。很少有事情是你每次都必须以某种方式去做的,但还是有一些。
Name your primary keys using "[singularOfTableName]ID" format. That is, whether your table name is Customer or Customers, the primary key should be CustomerID. Further, foreign keys must be named consistently in different tables. It should be legal to beat up someone who does not do this. I would submit that while defined foreign key constraints are often important, consistent foreign key naming is always important You database must have internal conventions. Even though in later sections you'll see me being very flexible, within a database naming must be very consistent . Whether your table for customers is called Customers or Customer is less important than that you do it the same way throughout the same database. And you can flip a coin to determine how to use underscores, but then you must keep using them the same way. If you don't do this, you are a bad person who should have low self-esteem.
你可能应该做的事。
Fields representing the same kind of data on different tables should be named the same. Don't have Zip on one table and ZipCode on another. To separate words in your table or column names, use PascalCasing. Using camelCasing would not be intrinsically problematic, but that's not the convention and it would look funny. I'll address underscores in a moment. (You may not use ALLCAPS as in the olden days. OBNOXIOUSTABLE.ANNOYING_COLUMN was okay in DB2 20 years ago, but not now.) Don't artifically shorten or abbreviate words. It is better for a name to be long and clear than short and confusing. Ultra-short names is a holdover from darker, more savage times. Cus_AddRef. What on earth is that? Custodial Addressee Reference? Customer Additional Refund? Custom Address Referral?
(3)你应该考虑什么。
I really think you should have plural names for tables; some think singular. Read the arguments elsewhere. Column names should be singular however. Even if you use plural table names, tables that represent combinations of other tables might be in the singular. For example, if you have a Promotions and an Items table, a table representing an item being a part of a promotion could be Promotions_Items, but it could also legitimately be Promotion_Items I think (reflecting the one-to-many relationship). Use underscores consistently and for a particular purpose. Just general tables names should be clear enough with PascalCasing; you don't need underscores to separate words. Save underscores either (a) to indicate an associative table or (b) for prefixing, which I'll address in the next bullet. Prefixing is neither good or bad. It usually is not best. In your first db or two, I would not suggest using prefixes for general thematic grouping of tables. Tables end up not fitting your categories easily, and it can actually make it harder to find tables. With experience, you can plan and apply a prefixing scheme that does more good than harm. I worked in a db once where data tables began with tbl, config tables with ctbl, views with vew, proc's sp, and udf's fn, and a few others; it was meticulously, consistently applied so it worked out okay. The only time you NEED prefixes is when you have really separate solutions that for some reason reside in the same db; prefixing them can be very helpful in grouping the tables. Prefixing is also okay for special situations, like for temporary tables that you want to stand out. Very seldom (if ever) would you want to prefix columns.
我的观点是:
1)不,表名应该是单数。
虽然对于简单的选择(select * from Orders)似乎有意义,但对于OO等效(Orders x = new Orders)则没有意义。
数据库中的表实际上是该实体的集合,当你使用set-logic时,它更有意义:
select Orders.*
from Orders inner join Products
on Orders.Key = Products.Key
最后一行,连接的实际逻辑,看起来与复数表名混淆。
我不确定是否总是使用别名(如Matt建议的那样)可以消除这种情况。
2)它们应该是单数,因为它们只拥有一种属性
3)如果列名有歧义(如上所述,它们都有一个名为[Key]的列),表名(或其别名)永远不能很好地区分它们。您希望查询能够快速键入,并且简单-前缀会增加不必要的复杂性。
4)无论你想要什么,我都推荐CapitalCase
我不认为有任何一套绝对的指导方针。
只要你在应用程序或数据库中选择的是一致的,我不认为这真的很重要。