table names
choose short, unambiguous names, using no more than one or two words
distinguish tables easily
facilitates the naming of unique field names as well as lookup and linking tables
give tables singular names, never plural (update: i still agree with the reasons given for this convention, but most people really like plural table names, so i’ve softened my stance)... follow the link above please
Table names singular. Let's say you were modelling a realtionship between someone and their address.
For example, if you are reading a datamodel would you prefer
'each person may live at 0,1 or many address.' or
'each people may live at 0,1 or many addresses.'
I think its easier to pluralise address, rather than have to rephrase people as person. Plus collective nouns are quite often dissimlar to the singular version.
Table Name: It should be singular, as it is a singular entity representing a real world object and not objects, which is singlular.
Column Name: It should be singular only then it conveys that it will hold an atomic value and will confirm to the normalization theory. If however, there are n number of same type of properties, then they should be suffixed with 1, 2, ..., n, etc.
Prefixing Tables / Columns: It is a huge topic, will discuss later.
Casing: It should be Camel case
My friend, Patrick Karcher, I request you to please not write anything which may be offensive to somebody, as you wrote, "•Further, foreign keys must be named consistently in different tables. It should be legal to beat up someone who does not do this.". I have never done this mistake my friend Patrick, but I am writing generally. What if they together plan to beat you for this? :)
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.
Either CamelCase (my preferred) or underscore_separated for both table names and column names.
However, like it has been mentioned, any convention is better than no convention. No matter how you choose to do it, document it so that future modifications follow the same conventions.
I work in a database support team with three DBAs and our considered options are:
Any naming standard is better than no standard.
There is no "one true" standard, we all have our preferences
If there is standard already in place, use it. Don't create another standard or muddy the existing standards.
We use singular names for tables. Tables tend to be prefixed with the name of the system (or its acronym). This is useful if the system complex as you can change the prefix to group the tables together logically (ie. reg_customer, reg_booking and regadmin_limits).
For fields we'd expect field names to be include the prefix/acryonm of the table (i.e. cust_address1) and we also prefer the use of a standard set of suffixes ( _id for the PK, _cd for "code", _nm for "name", _nb for "number", _dt for "Date").
The name of the Foriegn key field should be the same as the Primary key field.
i.e.
SELECT cust_nm, cust_add1, booking_dt
FROM reg_customer
INNER JOIN reg_booking
ON reg_customer.cust_id = reg_booking.cust_id
When developing a new project, I'd recommend you write out all the preferred entity names, prefixes and acronyms and give this document to your developers. Then, when they decide to create a new table, they can refer to the document rather than "guess" what the table and fields should be called.
我认为表名应该是复数。 表是实体的集合(表)。 每行代表一个实体,表代表集合。 因此,我将把 Person 实体表称为 People(或 Person,无论您喜欢什么)。
对于那些喜欢在查询中看到单数“实体名称”的人,这就是我使用表别名的原因:
SELECT person.Name
FROM People person
有点像 LINQ 的“from person in people select person.Name”。
至于2、3和4,我同意@Lars。
Ok, since we're weighing in with opinion:
I believe that table names should be plural. Tables are a collection (a table) of entities. Each row represents a single entity, and the table represents the collection. So I would call a table of Person entities People (or Persons, whatever takes your fancy).
For those who like to see singular "entity names" in queries, that's what I would use table aliases for:
SELECT person.Name
FROM People person
A bit like LINQ's "from person in people select person.Name".
Very late to the party but I still wanted to add my two cents about column prefixes
There seem to be two main arguments for using the table_column (or tableColumn) naming standard for columns, both based on the fact that the column name itself will be unique across your whole database:
1) You do not have to specify table names and/or column aliases in your queries all the time
2) You can easily search your whole code for the column name
I think both arguments are flawed. The solution for both problems without using prefixes is easy. Here's my proposal:
Always use the table name in your SQL. E.g., always use table.column instead of column.
It obviously solves 2) as you can now just search for table.column instead of 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.
edit: yes, I am aware that naming the columns with the prefix enforces the correct usage whereas my approach relies on the programmers
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%';
I know this is late to the game, and the question has been answered very well already, but I want to offer my opinion on #3 regarding the prefixing of column names.
All columns should be named with a prefix that is unique to the table they are defined in.
E.g. Given tables "customer" and "address", let's go with prefixes of "cust" and "addr", respectively. "customer" would have "cust_id", "cust_name", etc. in it. "address" would have "addr_id", "addr_cust_id" (FK back to customer), "addr_street", etc. in it.
When I was first presented with this standard, I was dead-set against it; I hated the idea. I couldn't stand the idea of all that extra typing and redundancy. Now I've had enough experience with it that I'd never go back.
The result of doing this is that all of the columns in your database schema are unique. There is one major benefit to this, which trumps all arguments against it (in my opinion, of course):
You can search your entire code base and reliably find every line of code that touches a particular column.
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.
Another, relatively minor benefit to it is that you only have to use table-aliases when you do a self join:
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%';
While it appears to make sense for the simple selection (select * from Orders) it makes less sense for the OO equivalent (Orders x = new Orders).
A table in a DB is really the set of that entity, it makes more sense once you're using set-logic:
select Orders.*
from Orders inner join Products
on Orders.Key = Products.Key
That last line, the actual logic of the join, looks confusing with plural table names.
I'm not sure about always using an alias (as Matt suggests) clears that up.
2) They should be singular as they only hold 1 property
3) Never, if the column name is ambiguous (as above where they both have a column called [Key]) the name of the table (or its alias) can distinguish them well enough. You want queries to be quick to type and simple - prefixes add unnecessary complexity.
4) Whatever you want, I'd suggest CapitalCase
I don't think there's one set of absolute guidelines on any of these.
As long as whatever you pick is consistent across the application or DB I don't think it really matters.
I think the best answer to each of those questions would be given by you and your team. It's far more important to have a naming convention then how exactly the naming convention is.
As there's no right answer to that, you should take some time (but not too much) and choose your own conventions and - here's the important part - stick to it.
Of course it's good to seek some information about standards on that, which is what you're asking, but don't get anxious or worried about the number of different answers you might get: choose the one that seems better for you.
Just in case, here are my answers:
Yes. A table is a group of records, teachers or actors, so... plural.
Yes.
I don't use them.
The database I use more often - Firebird - keeps everything in upper case, so it doesn't matter. Anyway, when I'm programming I write the names in a way that it's easier to read, like releaseYear.
I hear the argument all the time that whether or not a table is pluralized is all a matter of personal taste and there is no best practice. I don't believe that is true, especially as a programmer as opposed to a DBA. As far as I am aware, there are no legitimate reasons to pluralize a table name other than "It just makes sense to me because it's a collection of objects," while there are legitimate gains in code by having singular table names. For example:
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.
To sum it up, if you pluralize your table names you are losing all sorts of advantages in making your code smarter and easier to handle. There may even be cases where you have to have lookup tables/arrays to convert your table names to object or local code names you could have avoided. Singular table names, though perhaps feeling a little weird at first, offer significant advantages over pluralized names and I believe are best practice.
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.
Use any case in naming items: PascalCase for both tables and columns.
Elaboration:
(1) What you must do. There are very few things that you must do a certain way, every time, but there are a few.
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.
(2) What you should probably do.
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) What you should consider.
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.
Definitely keep table names singular, person not people
Same here
No. I've seen some terrible prefixes, going so far as to state what were dealing with is a table (tbl_) or a user store procedure (usp_). This followed by the database name... Don't do it!
Naming conventions allow the development team to design discovereability and maintainability at the heart of the project.
A good naming convention takes time to evolve but once it’s in place it allows the team to move forward with a common language. A good naming convention grows organically with the project. A good naming convention easily copes with changes during the longest and most important phase of the software lifecycle - service management in production.
Here are my answers:
Yes, table names should be plural when they refer to a set of trades, securities, or counterparties for example.
Yes.
Yes. SQL tables are prefixed with tb_, views are prefixed vw_, stored procedures are prefixed usp_ and triggers are prefixed tg_ followed by the database name.
Column name should be lower case separated by underscore.
Naming is hard but in every organisation there is someone who can name things and in every software team there should be someone who takes responsibility for namings standards and ensures that naming issues like sec_id, sec_value and security_id get resolved early before they get baked into the project.
So what are the basic tenets of a good naming convention and standards: -
Use the language of your client and
your solution domain
Be descriptive
Be consistent
Disambiguate, reflect and refactor
Don’t use abbreviations unless they
are clear to everyone
"Tables are Collections of Entities, and follow Collection naming guidelines. Ideally, a collective name is used: eg., Personnel. Plural is also correct: Employees. Incorrect names include: Employee, tblEmployee, and EmployeeTable."
As always, there are exceptions to rules e.g. a table which always has exactly one row may be better with a singular name e.g. a config table. And consistency is of utmost importance: check whether you shop has a convention and, if so, follow it; if you don't like it then do a business case to have it changed rather than being the lone ranger.
发布评论
评论(23)
基本数据库命名约定(和样式)(点击这里有更详细的描述)
表名
选择简短、明确的名称,最多使用一两个单词
轻松区分表格
方便命名唯一的字段名称以及查找和链接表
给表命名为单数,而不是复数(更新:我仍然同意这个约定的原因,但大多数人真的喜欢复数表名,所以我软化了我的立场)...请点击上面的链接
Essential Database Naming Conventions (and Style) (click here for more detailed description)
table names
choose short, unambiguous names, using no more than one or two words
distinguish tables easily
facilitates the naming of unique field names as well as lookup and linking tables
give tables singular names, never plural (update: i still agree with the reasons given for this convention, but most people really like plural table names, so i’ve softened my stance)... follow the link above please
表名单数。 假设您正在对某人与其地址之间的关系进行建模。
例如,如果您正在阅读数据模型,您会更喜欢
“每个人可能住在 0,1 个或多个地址。” 或者
“每个人可能住在 0,1 个或多个地址。”
我认为使用复数称呼更容易,而不是必须将人改写为人。 另外,集体名词通常与单数名词不同。
Table names singular. Let's say you were modelling a realtionship between someone and their address.
For example, if you are reading a datamodel would you prefer
'each person may live at 0,1 or many address.' or
'each people may live at 0,1 or many addresses.'
I think its easier to pluralise address, rather than have to rephrase people as person. Plus collective nouns are quite often dissimlar to the singular version.
表名称:它应该是单数,因为它是代表现实世界对象的单数实体,而不是单数的对象。
列名:它应该是单数,只有这样它才能表达它将持有原子值并符合规范化理论。 然而,如果有n个相同类型的属性,那么它们应该以1、2、...、n等为后缀。
给表/列添加前缀:这是一个很大的话题,稍后会讨论。
大小写:应该是 Camel 大小写
我的朋友,Patrick Karcher,我请求您不要写任何可能冒犯某人的内容,正如您所写的,“•此外,外键的命名必须与殴打不这样做的人应该是合法的。” 我的朋友帕特里克我从来没有犯过这个错误,但我写得一般。 如果他们一起计划为此击败你怎么办? :)
Table Name: It should be singular, as it is a singular entity representing a real world object and not objects, which is singlular.
Column Name: It should be singular only then it conveys that it will hold an atomic value and will confirm to the normalization theory. If however, there are n number of same type of properties, then they should be suffixed with 1, 2, ..., n, etc.
Prefixing Tables / Columns: It is a huge topic, will discuss later.
Casing: It should be Camel case
My friend, Patrick Karcher, I request you to please not write anything which may be offensive to somebody, as you wrote, "•Further, foreign keys must be named consistently in different tables. It should be legal to beat up someone who does not do this.". I have never done this mistake my friend Patrick, but I am writing generally. What if they together plan to beat you for this? :)
指的是其中一项记录所代表的人,而不是人。
好的。 那是我的 0.02 美元
Person, not persons is how you would refer to whoever one of the records represents.
Ok. Thats my $0.02
这些是我学到的约定,但您应该适应您开发软管使用的任何内容。
These are the conventions I was taught, but you should adapt to whatever you developement hose uses.
这是提供一些选择的链接。 我正在寻找一个可以遵循的简单规范,而不必依赖于部分定义的规范。
http://justinsomnia.org/writings/naming_conventions.html
Here's a link that offers a few choices. I was searching for a simple spec I could follow rather than having to rely on a partially defined one.
http://justinsomnia.org/writings/naming_conventions.html
我们的偏好:
表名应该是复数吗?
绝不。 认为它是一个集合的论点是有道理的,但你永远不知道表将包含什么(0,1 或许多项目)。 复数规则使命名变得不必要的复杂。 1 栋房子,2 栋房子,老鼠对老鼠,人对人,我们甚至没有看过任何其他语言。
更新人员集属性 = 'value'
作用于表中的每个人员。Select * from person where person.name = 'Greg'
返回人员行的集合/行集。列名应该是单数吗?
通常是的,除非您违反规范化规则。
我应该为表或列添加前缀吗?
主要是平台偏好。 我们更喜欢在列前加上表名作为前缀。 我们不为表添加前缀,但为视图 (v_) 和存储过程(sp_ 或 f_(函数))添加前缀。 这可以帮助那些想要尝试更新 v_person.age 的人,它实际上是视图中的计算字段(无论如何都无法更新)。
这也是避免关键字冲突的好方法(delivery.from 会中断,但 payment_from 不会)。
它确实使代码更加冗长,但通常有助于提高可读性。
bob = new person()
bob.person_name = '鲍勃'
bob.person_dob = '1958-12-21'
...非常易读且明确。 但这可能会失控:
customer.customer_customer_type_id
表示 customer 和 customer_type 表之间的关系,表示 customer_type 表 (customer_type_id) 上的主键,如果您在调试查询时看到“customer_customer_type_id”,您会立即知道它来自哪里(客户表)。< /p>
或者您在 customer_type 和 customer_category 之间存在 MM 关系(仅某些类型适用于某些类别)
customer_category_customer_type_id
...有点长(!)。
我应该在命名项目时使用大小写吗?
是 - 小写:),带下划线。 这些都是非常可读且跨平台的。 结合上面的3点也是有道理的。
不过,其中大部分都是偏好。 - 只要保持一致,任何必须阅读它的人都应该是可预测的。
our preference:
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.
查看 ISO 11179-5:命名和标识原则
您可以在这里获取:http://metadata-standards.org/11179/#11179-5
我不久前在博客上写过它:ISO- 11179 命名约定
Take a look at ISO 11179-5: Naming and identification principles
You can get it here: http://metadata-standards.org/11179/#11179-5
I blogged about it a while back here: ISO-11179 Naming Conventions
我认为:
然而,正如已经提到的,任何约定都比没有约定好。 无论您选择如何执行,都将其记录下来,以便将来的修改遵循相同的约定。
In my opinion:
However, like it has been mentioned, any convention is better than no convention. No matter how you choose to do it, document it so that future modifications follow the same conventions.
我在一个拥有三名 DBA 的数据库支持团队工作,我们考虑的选项是:
我们对表使用单数名称。 表往往以系统名称(或其缩写)为前缀。 如果系统复杂,这非常有用,因为您可以更改前缀以将表逻辑分组在一起(即 reg_customer、reg_booking 和 regadmin_limits)。
对于字段,我们希望字段名称包含表的前缀/缩写(即 cust_address1),并且我们也更喜欢使用一组标准后缀(_id 表示 PK,_cd 表示“code”,_nm 表示“name” ",_nb 表示“数字”,_dt 表示“日期”)。
外键字段的名称应与主键字段的名称相同。
即,
在开发新项目时,我建议您写出所有首选实体名称、前缀和首字母缩略词,并将此文档提供给您的开发人员。 然后,当他们决定创建新表时,他们可以参考文档,而不是“猜测”表和字段应该被称为什么。
I work in a database support team with three DBAs and our considered options are:
We use singular names for tables. Tables tend to be prefixed with the name of the system (or its acronym). This is useful if the system complex as you can change the prefix to group the tables together logically (ie. reg_customer, reg_booking and regadmin_limits).
For fields we'd expect field names to be include the prefix/acryonm of the table (i.e. cust_address1) and we also prefer the use of a standard set of suffixes ( _id for the PK, _cd for "code", _nm for "name", _nb for "number", _dt for "Date").
The name of the Foriegn key field should be the same as the Primary key field.
i.e.
When developing a new project, I'd recommend you write out all the preferred entity names, prefixes and acronyms and give this document to your developers. Then, when they decide to create a new table, they can refer to the document rather than "guess" what the table and fields should be called.
好吧,既然我们正在权衡意见:
我认为表名应该是复数。 表是实体的集合(表)。 每行代表一个实体,表代表集合。 因此,我将把 Person 实体表称为 People(或 Person,无论您喜欢什么)。
对于那些喜欢在查询中看到单数“实体名称”的人,这就是我使用表别名的原因:
有点像 LINQ 的“from person in people select person.Name”。
至于2、3和4,我同意@Lars。
Ok, since we're weighing in with opinion:
I believe that table names should be plural. Tables are a collection (a table) of entities. Each row represents a single entity, and the table represents the collection. So I would call a table of Person entities People (or Persons, whatever takes your fancy).
For those who like to see singular "entity names" in queries, that's what I would use table aliases for:
A bit like LINQ's "from person in people select person.Name".
As for 2, 3 and 4, I agree with @Lars.
聚会已经很晚了,但我仍然想添加关于列前缀的两分钱 对于
使用列的 table_column (或 tableColumn)命名标准似乎有两个主要论点,两者都基于列名本身是唯一的事实在整个数据库中:
1)您不必一直在查询中指定表名和/或列别名
2)您可以轻松地在整个代码中搜索列名
我认为这两个参数都有缺陷。 不使用前缀解决这两个问题的方法很简单。 这是我的建议:
始终在 SQL 中使用表名。 例如,始终使用 table.column 而不是 column。
它显然解决了 2),因为您现在只需搜索 table.column 而不是 table_column。
但我能听到你尖叫,这怎么解决1)? 这正是为了避免这种情况。 是的,确实如此,但该解决方案存在严重缺陷。 为什么? 好吧,前缀解决方案归结为:
为了避免在出现歧义时必须指定 table.column,您可以将所有列命名为 table_column!
但这意味着从现在起您每次指定列时都必须写入列名。 但如果你无论如何都必须这样做,那么与总是显式编写 table.column 相比,有什么好处呢? 确切地说,没有任何好处,输入的字符数完全相同。
编辑:是的,我知道用前缀命名列会强制执行正确的用法,而我的方法依赖于程序员
Very late to the party but I still wanted to add my two cents about column prefixes
There seem to be two main arguments for using the table_column (or tableColumn) naming standard for columns, both based on the fact that the column name itself will be unique across your whole database:
1) You do not have to specify table names and/or column aliases in your queries all the time
2) You can easily search your whole code for the column name
I think both arguments are flawed. The solution for both problems without using prefixes is easy. Here's my proposal:
Always use the table name in your SQL. E.g., always use table.column instead of column.
It obviously solves 2) as you can now just search for table.column instead of 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.
edit: yes, I am aware that naming the columns with the prefix enforces the correct usage whereas my approach relies on the programmers
我知道这已经晚了,而且这个问题已经得到了很好的回答,但我想就列名前缀的#3 提出我的意见。
所有列都应使用定义它们的表唯一的前缀命名。
例如,给定表“customer”和“address”,让我们使用前缀“cust”和“addr”,分别。 “customer”中将包含“cust_id”、“cust_name”等。 “地址”将包含“addr_id”、“addr_cust_id”(FK 返回给客户)、“addr_street”等。
当我第一次看到这个标准时,我坚决反对它。 我讨厌这个主意。 我无法忍受所有额外的打字和冗余的想法。 现在我已经有了足够的经验,我再也不会回去了。
这样做的结果是数据库模式中的所有列都是唯一的。 这样做有一个主要好处,它胜过了所有反对它的论点(当然,在我看来):
您可以搜索整个代码库并可靠地找到涉及特定列的每一行代码。
#1 的好处是非常巨大的。 我可以弃用某个列,并确切地知道需要更新哪些文件,然后才能安全地从架构中删除该列。 我可以更改列的含义并确切地知道哪些代码需要重构。 或者我可以简单地判断列中的数据是否正在系统的特定部分中使用。 我无法计算有多少次这将一个潜在的庞大项目变成了一个简单的项目,也无法计算我们在开发工作中节省了多少时间。
另一个相对较小的好处是,在进行自连接时只需使用表别名:
I know this is late to the game, and the question has been answered very well already, but I want to offer my opinion on #3 regarding the prefixing of column names.
All columns should be named with a prefix that is unique to the table they are defined in.
E.g. Given tables "customer" and "address", let's go with prefixes of "cust" and "addr", respectively. "customer" would have "cust_id", "cust_name", etc. in it. "address" would have "addr_id", "addr_cust_id" (FK back to customer), "addr_street", etc. in it.
When I was first presented with this standard, I was dead-set against it; I hated the idea. I couldn't stand the idea of all that extra typing and redundancy. Now I've had enough experience with it that I'd never go back.
The result of doing this is that all of the columns in your database schema are unique. There is one major benefit to this, which trumps all arguments against it (in my opinion, of course):
You can search your entire code base and reliably find every line of code that touches a particular column.
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.
Another, relatively minor benefit to it is that you only have to use table-aliases when you do a self join:
我对此的看法是:
1)不,表名应该是单数。
虽然它对于简单选择(
select * from Orders
)似乎有意义,但对于 OO 等效项(Orders x = new Orders
)意义不大。数据库中的表实际上是该实体的集合,一旦您使用集合逻辑,它就更有意义:
最后一行(连接的实际逻辑)看起来与复数表名称混淆。
我不确定总是使用别名(正如马特建议的那样)可以解决这个问题。
2) 它们应该是单数,因为它们只包含 1 个属性
3) 决不,如果列名不明确(如上所述,它们都有一个名为 [Key] 的列),表的名称(或其别名)可以很好地区分它们足够的。 您希望查询能够快速输入且简单 - 前缀会增加不必要的复杂性。
4)无论你想要什么,我建议使用大写字母
我认为对于这些都没有一套绝对的指导方针。
只要您选择的内容在应用程序或数据库中保持一致,我认为这并不重要。
My opinions on these are:
1) No, table names should be singular.
While it appears to make sense for the simple selection (
select * from Orders
) it makes less sense for the OO equivalent (Orders x = new Orders
).A table in a DB is really the set of that entity, it makes more sense once you're using set-logic:
That last line, the actual logic of the join, looks confusing with plural table names.
I'm not sure about always using an alias (as Matt suggests) clears that up.
2) They should be singular as they only hold 1 property
3) Never, if the column name is ambiguous (as above where they both have a column called [Key]) the name of the table (or its alias) can distinguish them well enough. You want queries to be quick to type and simple - prefixes add unnecessary complexity.
4) Whatever you want, I'd suggest CapitalCase
I don't think there's one set of absolute guidelines on any of these.
As long as whatever you pick is consistent across the application or DB I don't think it really matters.
我认为这些问题的最佳答案将由您和您的团队给出。 拥有一个命名约定比命名约定的精确程度要重要得多。
由于没有正确的答案,您应该花一些时间(但不要太多)并选择自己的约定 - 这是重要的部分 - 坚持下去。
当然,最好寻求一些有关标准的信息,这就是您所要求的,但不要担心或担心您可能得到的不同答案的数量:选择一个看起来更适合您的答案。
以防万一,我的回答是:
I think the best answer to each of those questions would be given by you and your team. It's far more important to have a naming convention then how exactly the naming convention is.
As there's no right answer to that, you should take some time (but not too much) and choose your own conventions and - here's the important part - stick to it.
Of course it's good to seek some information about standards on that, which is what you're asking, but don't get anxious or worried about the number of different answers you might get: choose the one that seems better for you.
Just in case, here are my answers:
我经常听到这样的争论:表是否复数完全取决于个人喜好,并且没有最佳实践。 我不相信这是真的,尤其是作为一名程序员而不是 DBA。 据我所知,除了“这对我来说有意义,因为它是对象的集合”之外,没有任何合理的理由将表名复数,而使用单数表名可以在代码中获得合法的收益。 例如:
它避免了由复数歧义引起的错误和错误。 程序员并不以其拼写专业知识而闻名,并且某些单词的复数形式令人困惑。 例如,复数单词是以“es”结尾还是仅以“s”结尾? 是人还是人? 当您与大型团队一起处理项目时,这可能会成为一个问题。 例如,团队成员使用不正确的方法对其创建的表进行复数化的实例。 当我与该表交互时,它已在我无法访问或需要很长时间才能修复的代码中使用。 结果是我每次使用该表时都必须记住将其拼写错误。 与此非常相似的事情发生在我身上。 您越能让团队的每个成员一致、轻松地使用准确、正确的表名称,而不会出现错误,也不必一直查找表名称,那就越好。 单数版本在团队环境中更容易处理。
如果您使用表名的单数版本并在主键前添加表名前缀,那么您现在可以轻松地通过代码从主键确定表名,反之亦然。 您可以得到一个带有表名的变量,将“Id”连接到末尾,现在您可以通过代码获得表的主键,而无需执行额外的查询。 或者,您可以从主键末尾截去“Id”,以通过代码确定表名。 如果您使用“id”而没有主键的表名称,则无法通过代码从主键确定表名称。 此外,大多数使用复数表名并在 PK 列前加上表名的人在 PK 中使用表名的单数版本(例如 statuses 和 status_id),因此根本不可能做到这一点。
如果将表名称设置为单数,则可以让它们与它们代表的类名称相匹配。 再一次,这可以简化代码并允许您做一些非常巧妙的事情,例如只用表名来实例化一个类。 它还只会使您的代码更加一致,从而导致...
如果您将表名称设为单数,它会使您的命名方案一致、有组织且易于在每个位置维护。 您知道,在代码中的每个实例中,无论是在列名称中、作为类名称还是作为表名称,它都是完全相同的名称。 这使您可以进行全局搜索以查看使用数据的所有位置。 当您使用复数形式的表名称时,有时您将使用该表名称的单数版本(它在主键中变成的类)。 在某些情况下,您的数据不会被称为复数,而在某些情况下,数据会被称为单数,这是有意义的。
总而言之,如果您使用复数形式的表名称,您将失去使代码更智能、更易于处理的各种优势。 甚至在某些情况下,您必须使用查找表/数组将表名转换为您可以避免的对象或本地代码名称。 单一表名虽然一开始可能感觉有点奇怪,但与复数名称相比具有显着的优势,我相信这是最佳实践。
I hear the argument all the time that whether or not a table is pluralized is all a matter of personal taste and there is no best practice. I don't believe that is true, especially as a programmer as opposed to a DBA. As far as I am aware, there are no legitimate reasons to pluralize a table name other than "It just makes sense to me because it's a collection of objects," while there are legitimate gains in code by having singular table names. For example:
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.
To sum it up, if you pluralize your table names you are losing all sorts of advantages in making your code smarter and easier to handle. There may even be cases where you have to have lookup tables/arrays to convert your table names to object or local code names you could have avoided. Singular table names, though perhaps feeling a little weird at first, offer significant advantages over pluralized names and I believe are best practice.
表名应始终是单数,因为它们代表一组对象。 正如你所说的“牧群”是指一群羊,或者“羊群”是指一群鸟。 不需要复数。 当表名由两个名称组成并且命名约定为复数时,很难知道复数名称应该是第一个单词还是第二个单词还是两者。
这是逻辑——Object.instance,而不是objects.instance。 或 TableName.column,而不是 TableNames.column。
Microsoft SQL 不区分大小写,当表名或列名由两个或多个名称组成时,如果使用大写字母,则更容易读取表名或列名。
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.
这里迟到的答案,但简而言之:
详细说明:
(1)你必须做的事情。很少有事情你必须每次都以某种方式做,但是有一些。
(2) 您可能应该做什么。
(3) 你应该考虑什么。
为列添加前缀。
Late answer here, but in short:
Elaboration:
(1) What you must do. There are very few things that you must do a certain way, every time, but there are a few.
(2) What you should probably do.
(3) What you should consider.
to prefix columns.
我建议查看 Microsoft 的 SQL Server 示例数据库:
https://github.com/Microsoft/sql-server-samples/releases /tag/adventureworks
AdventureWorks 示例使用非常清晰且一致的命名约定,该约定使用架构名称来组织数据库对象。
I recommend checking out Microsoft's SQL Server sample databases:
https://github.com/Microsoft/sql-server-samples/releases/tag/adventureworks
The AdventureWorks sample uses a very clear and consistent naming convention that uses schema names for the organization of database objects.
命名约定允许开发团队在项目的核心设计可发现性和可维护性。
良好的命名约定需要时间来发展,但一旦到位,团队就可以使用共同语言继续前进。 良好的命名约定会随着项目的发展而有机地发展。 良好的命名约定可以轻松应对软件生命周期中最长且最重要的阶段(生产中的服务管理)期间的变化。
以下是我的回答:
命名很困难,但在每个组织中都有一个人可以命名事物,并且在每个软件团队中都应该有一个人负责命名标准并确保像 sec_id、sec_value 这样的命名问题em> 和 security_id 在融入项目之前就得到了解决。
那么良好的命名约定和标准的基本原则是什么: -
您的解决方案领域
大家都清楚
列名
Naming conventions allow the development team to design discovereability and maintainability at the heart of the project.
A good naming convention takes time to evolve but once it’s in place it allows the team to move forward with a common language. A good naming convention grows organically with the project. A good naming convention easily copes with changes during the longest and most important phase of the software lifecycle - service management in production.
Here are my answers:
Naming is hard but in every organisation there is someone who can name things and in every software team there should be someone who takes responsibility for namings standards and ensures that naming issues like sec_id, sec_value and security_id get resolved early before they get baked into the project.
So what are the basic tenets of a good naming convention and standards: -
your solution domain
are clear to everyone
column names
我也赞成 ISO/IEC 11179 风格的命名约定,并指出它们是指导方针而不是规定性的。
请参阅维基百科上的数据元素名称:
“表是实体的集合,并遵循集合命名准则。理想情况下,使用集体名称:例如,Personnel 也是正确的:Employees 不正确的名称包括:Employee、tblEmployee 和 EmployeeTable。
与往常一样,规则也有例外,例如,总是只有一行的表可能最好使用单一名称,例如配置表。 一致性至关重要:检查您的购物是否有惯例,如果有,请遵守; 如果你不喜欢它,那就做一个商业案例来改变它,而不是成为独行侠。
I'm also in favour of a ISO/IEC 11179 style naming convention, noting they are guidelines rather than being prescriptive.
See Data element name on Wikipedia:
"Tables are Collections of Entities, and follow Collection naming guidelines. Ideally, a collective name is used: eg., Personnel. Plural is also correct: Employees. Incorrect names include: Employee, tblEmployee, and EmployeeTable."
As always, there are exceptions to rules e.g. a table which always has exactly one row may be better with a singular name e.g. a config table. And consistency is of utmost importance: check whether you shop has a convention and, if so, follow it; if you don't like it then do a business case to have it changed rather than being the lone ranger.