每当我设计数据库时,我总是想知道是否有一种最好的方法来命名数据库中的项目。我经常问自己以下问题:
表名应该是复数吗? 列名应该是单数吗? 我应该为表或列添加前缀吗? 我应该在命名项目时使用大小写吗?
是否有推荐的指导原则来命名数据库中的项?
每当我设计数据库时,我总是想知道是否有一种最好的方法来命名数据库中的项目。我经常问自己以下问题:
表名应该是复数吗? 列名应该是单数吗? 我应该为表或列添加前缀吗? 我应该在命名项目时使用大小写吗?
是否有推荐的指导原则来命名数据库中的项?
当前回答
基本数据库命名约定(和样式)(点击这里查看更详细的描述)
表名 选择简短、明确的名字,不要超过一两个单词 容易区分表 方便唯一字段名的命名以及查找和链接表 给表取单数名,而不是复数名(更新:我仍然同意这种惯例的原因,但大多数人真的喜欢复数表名,所以我的立场软化了)…请点击上面的链接
其他回答
我们的偏好:
Should table names be plural? Never. The arguments for it being a collection make sense, but you never know what the table is going to contain (0,1 or many items). Plural rules make the naming unnecessarily complicated. 1 House, 2 houses, mouse vs mice, person vs people, and we haven't even looked at any other languages. Update person set property = 'value' acts on each person in the table. Select * from person where person.name = 'Greg' returns a collection/rowset of person rows. Should column names be singular? Usually, yes, except where you are breaking normalisation rules. Should I prefix tables or columns? Mostly a platform preference. We prefer to prefix columns with the table name. We don't prefix tables, but we do prefix views (v_) and stored_procedures (sp_ or f_ (function)). That helps people who want to try to upday v_person.age which is actually a calculated field in a view (which can't be UPDATEd anyway). It is also a great way to avoid keyword collision (delivery.from breaks, but delivery_from does not). It does make the code more verbose, but often aids in readability. bob = new person() bob.person_name = 'Bob' bob.person_dob = '1958-12-21' ... is very readable and explicit. This can get out of hand though: customer.customer_customer_type_id indicates a relationship between customer and the customer_type table, indicates the primary key on the customer_type table (customer_type_id) and if you ever see 'customer_customer_type_id' whilst debugging a query, you know instantly where it is from (customer table). or where you have a M-M relationship between customer_type and customer_category (only certain types are available to certain categories) customer_category_customer_type_id ... is a little (!) on the long side. Should I use any case in naming items? Yes - lower case :), with underscores. These are very readable and cross platform. Together with 3 above it also makes sense. Most of these are preferences though. - As long as you are consistent, it should be predictable for anyone that has to read it.
表名:它应该是单数,因为它是一个表示真实世界对象的单数实体,而不是表示对象的单数实体。
列名:它应该是单数,只有这样它才表示它将持有一个原子值,并将确认归一化理论。然而,如果有n个相同类型的属性,那么它们应该以1,2,…作为后缀。n,等等。
表/列前缀:这是一个巨大的主题,将在后面讨论。
外壳:应该是驼色的
我的朋友Patrick Karcher,我请求你不要写任何可能冒犯别人的东西,就像你写的那样,“此外,外键必须在不同的表中一致命名。如果有人不这样做,殴打他应该是合法的。”我从来没有犯过这样的错误,我的朋友帕特里克,但我写一般。如果他们一起打算为此揍你呢?:)
我总是听到这样的争论,即表格是否多元化完全是个人品味的问题,没有最佳实践。我不相信这是真的,尤其是作为一个程序员而不是DBA。据我所知,除了“这对我来说很有意义,因为它是对象的集合”之外,没有其他合理的理由将表名改为复数形式,而使用单数表名在代码中有合理的好处。例如:
It avoids bugs and mistakes caused by plural ambiguities. Programmers aren't exactly known for their spelling expertise, and pluralizing some words are confusing. For example, does the plural word end in 'es' or just 's'? Is it persons or people? When you work on a project with large teams, this can become an issue. For example, an instance where a team member uses the incorrect method to pluralize a table he creates. By the time I interact with this table, it is used all over in code I don't have access to or would take too long to fix. The result is I have to remember to spell the table wrong every time I use it. Something very similar to this happened to me. The easier you can make it for every member of the team to consistently and easily use the exact, correct table names without errors or having to look up table names all the time, the better. The singular version is much easier to handle in a team environment. If you use the singular version of a table name AND prefix the primary key with the table name, you now have the advantage of easily determining a table name from a primary key or vice versa via code alone. You can be given a variable with a table name in it, concatenate "Id" to the end, and you now have the primary key of the table via code, without having to do an additional query. Or you can cut off "Id" from the end of a primary key to determine a table name via code. If you use "id" without a table name for the primary key, then you cannot via code determine the table name from the primary key. In addition, most people who pluralize table names and prefix PK columns with the table name use the singular version of the table name in the PK (for example statuses and status_id), making it impossible to do this at all. If you make table names singular, you can have them match the class names they represent. Once again, this can simplify code and allow you to do really neat things, like instantiating a class by having nothing but the table name. It also just makes your code more consistent, which leads to... If you make the table name singular, it makes your naming scheme consistent, organized, and easy to maintain in every location. You know that in every instance in your code, whether it's in a column name, as a class name, or as the table name, it's the same exact name. This allows you to do global searches to see everywhere that data is used. When you pluralize a table name, there will be cases where you will use the singular version of that table name (the class it turns into, in the primary key). It just makes sense to not have some instances where your data is referred to as plural and some instances singular.
总而言之,如果你将表名改为复数,那么你就失去了让你的代码更聪明、更容易处理的所有优势。甚至在某些情况下,您必须使用查找表/数组来将表名转换为本可以避免的对象或本地代码名。虽然一开始可能感觉有点奇怪,但单数表名比复数表名具有显著优势,我相信这是最佳实践。
Table names should always be singular, because they represent a set of objects. As you say herd to designate a group of sheep, or flock do designate a group of birds. No need for plural. When a table name is composition of two names and naming convention is in plural it becomes hard to know if the plural name should be the first word or second word or both. It’s the logic – Object.instance, not objects.instance. Or TableName.column, not TableNames.column(s). Microsoft SQL is not case sensitive, it’s easier to read table names, if upper case letters are used, to separate table or column names when they are composed of two or more names.
我认为这些问题的最佳答案将由您和您的团队给出。有一个命名约定比命名约定的具体方式重要得多。
因为这个问题没有正确答案,你应该花点时间(但不要太多)选择你自己的习惯——这是重要的部分——坚持它。
当然,寻求一些关于标准的信息是很好的,这就是你要问的,但不要因为你可能得到的不同答案的数量而焦虑或担心:选择一个对你来说更好的答案。
以防万一,以下是我的答案:
是的。表是一组记录,老师或演员,所以…复数。 是的。 我不用它们。 我经常使用的数据库——Firebird——所有内容都是大写的,所以没关系。不管怎样,当我在编程时,我以一种更容易阅读的方式写名字,比如releaseYear。