数据库表中 ID 列的命名
我想知道人们对数据库表中 ID 列的命名有何看法。
如果我有一个名为 Invoices 的表,其中包含一个标识列的主键,我会将该列称为 InvoiceID,这样就不会与其他表发生冲突,而且它是什么很明显。
在我当前工作的地方,他们将所有 ID 列称为 ID。
所以他们会执行以下操作:
Select
i.ID
, il.ID
From
Invoices i
Left Join InvoiceLines il
on i.ID = il.InvoiceID
现在,我在这里看到一些问题:
1.您需要为 select 上的列指定别名
2. ID = InvoiceID 不适合我的大脑
3. 如果您没有为表添加别名并引用 InvoiceID,那么它位于哪个表上是否显而易见?
其他人对这个话题有什么想法?
I was wondering peoples opinions on the naming of ID columns in database tables.
If I have a table called Invoices with a primary key of an identity column I would call that column InvoiceID so that I would not conflict with other tables and it's obvious what it is.
Where I am workind current they have called all ID columns ID.
So they would do the following:
Select
i.ID
, il.ID
From
Invoices i
Left Join InvoiceLines il
on i.ID = il.InvoiceID
Now, I see a few problems here:
1. You would need to alias the columns on the select
2. ID = InvoiceID does not fit in my brain
3. If you did not alias the tables and referred to InvoiceID is it obvious what table it is on?
What are other peoples thoughts on the topic?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(24)
对于 id 列,我总是首选 ID,而不是 TableName + ID,然后是 TableName + ID 作为外键。 这样,所有表的 id 字段都具有相同的名称,并且没有多余的描述。 这对我来说似乎更简单,因为所有表都具有相同的主键字段名称。
至于连接表而不知道哪个Id字段属于哪个表,在我看来,应该编写查询来处理这种情况。 在我工作的地方,我们总是在语句中使用的字段前面加上表/表别名。
I always preferred ID to TableName + ID for the id column, and then TableName + ID for a foreign key. That way, all tables have the same name for the id field and there isn't a redundant description. This seems simpler to me because all the tables have the same primary key field name.
As far as joining tables and not knowing which Id field belongs to which table, in my opinion, the query should be written to handle this situation. Where I work, we always prefix the fields we use in a statement with the table/table alias.
最近我公司里就这件事发生了一场书呆子争论。 LINQ 的出现使得冗余的表名+ID 模式在我看来更加明显愚蠢。 我认为大多数理性的人都会说,如果您以必须指定表名来区分 FK 的方式手动编写 SQL,那么这不仅可以节省打字时间,而且还可以节省时间。仅使用 ID 可以使 SQL 更加清晰,因为您可以清楚地看到哪个是PK,哪个是FK。
例如,
不仅告诉我两者是相关的,而且还告诉我哪个是PK,哪个是FK。 而在旧风格中,你被迫要么看起来要么希望它们被命名得好。
There's been a nerd fight about this very thing in my company of late. The advent of LINQ has made the redundant tablename+ID pattern even more obviously silly in my eyes. I think most reasonable people will say that if you're hand writing your SQL in such a manner as that you have to specify table names to differentiate FKs, then it's not only a savings on typing, but it adds clarity to your SQL to use just the ID in that you can clearly see which is the PK and which is the FK.
E.g.
tells me not only that the two are linked, but which is the PK and which is the FK. Whereas in the old style, you're forced to either look or hope that they were named well.
ID 是 SQL 反模式。
请参阅 http://www.amazon.com/s/ref=nb_sb_ss_i_1_5?url=search-alias%3Dstripbooks&field-keywords=sql+antipatterns&sprefix=sql+a
如果您有许多 ID 为id,你让举报变得更加困难。 它模糊了含义,使复杂的查询更难以阅读,并且要求您使用别名来区分报告本身。
此外,如果有人愚蠢到在可用的数据库中使用自然联接,您将联接到错误的记录。
如果您想使用某些数据库允许的 USING 语法,则如果使用 ID 则不能。
如果您使用 ID,如果您碰巧复制连接语法(不要告诉我没有人这样做过!)并且忘记更改连接条件中的别名,则很容易导致错误的连接。
所以现在你的
意思是
如果你使用 tablenameID 作为 id 字段,这种意外错误发生的可能性要小得多,而且更容易发现。
ID is a SQL Antipattern.
See http://www.amazon.com/s/ref=nb_sb_ss_i_1_5?url=search-alias%3Dstripbooks&field-keywords=sql+antipatterns&sprefix=sql+a
If you have many tables with ID as the id, you are making reporting that much more difficult. It obscures meaning and makes complex queries harder to read as well as requiring you to use aliases to differentiate on the report itself.
Further, if someone is foolish enough to use a natural join in a database where they are available, you will join to the wrong records.
If you would like to use the USING syntax that some dbs allow, you cannot if you use ID.
If you use ID, you can easily end up with a mistaken join if you happen to be copying the join syntax (don't tell me that no one ever does this!) and forget to change the alias in the join condition.
So you now have
when you meant
If you use tablenameID as the id field, this kind of accidental mistake is far less likely to happen and much easier to find.
我们使用
InvoiceID
,而不是ID
。 它使查询更具可读性 - 当您单独看到ID
时,它可能意味着任何内容,尤其是当您将表别名为i
时。We use
InvoiceID
, notID
. It makes queries more readable -- when you seeID
alone it could mean anything, especially when you alias the table toi
.我同意 Keven 和其他一些人的观点,即表的 PK 应该只是 Id,外键列出了 OtherTable + Id。
然而,我想补充一个最近更加重视这一论点的原因。
在我目前的职位上,我们正在使用 POCO 生成的实体框架。 使用 Id 的标准命名约定,PK 允许继承带有验证的 poco 基类,例如共享一组公共列名的表。 使用 Tablename + Id 作为每个表的 PK 会破坏使用这些表的基类的能力。
只是一些值得深思的东西。
I agree with Keven and a few other people here that the PK for a table should simply be Id and foreign keys list the OtherTable + Id.
However, I wish to add one reason which recently gave more weight to this argument.
In my current position, we are employing the entity framework using POCO generation. Using the standard naming convention of Id, the PK allows for inheritance of a base poco class with validation and such for tables which share a set of common column names. Using the Tablename + Id as the PK for each of these tables destroys the ability to use a base class for these.
Just some food for thought.
这并不重要。 您可能会在所有命名约定中遇到类似的问题。
保持一致很重要,这样您就不必在每次编写查询时都查看表定义。
It's not really important. You are likely to run into similar problems in all naming conventions.
It is important to be consistent so you don't have to look at the table definitions every time you write a query.
我的偏好也是主键 ID 和外键 TableNameID。 我还喜欢在大多数表中都有一个“名称”列,其中保存条目的用户可读标识符(即名称:-))。 这种结构为应用程序本身提供了很大的灵活性,我可以用同样的方式处理大量的表。 这是一个非常强大的东西。 通常OO软件是建立在数据库之上的,但是OO工具集无法应用,因为数据库本身不允许。 拥有列 id 和 name 仍然不是很好,但这是一个步骤。
为什么我不能这样做?
在我看来,这是非常易读且简单的。 一般来说,将变量命名为 i 和 il 是一个糟糕的选择。
My preference is also ID for primary key and TableNameID for foreign key. I also like to have a column "name" in most tables where I hold the user readable identifier (i.e. name :-)) of the entry. This structure offers great flexibility in the application itself, I can handle tables in mass, in the same way. This is a very powerful thing. Usually an OO software is built on top of the database, but the OO toolset cannot be applied because the db itself does not allow it. Having the columns id and name is still not very good, but it is a step.
Why cant I do this?
In my opinion this is very much readable and simple. Naming variables as i and il is a poor choice in general.
我刚刚开始在一个只使用“ID”的地方工作(在核心表中,由外键中的 TableNameID 引用),并且已经发现了两个由它直接引起的生产问题。
在一种情况下,查询使用“... where ID in (SELECT ID FROM OtherTable ...”而不是“... where ID in (SELECT TransID FROM OtherTable ...”)。
任何人都可以诚实地说这不会有如果在错误语句为“... where TransID in (SELECT OtherTableID from OtherTable ...”的地方使用完整、一致的名称,那么更容易发现?我不这么认为。
重构代码时会出现另一个问题如果您使用临时表,而之前查询离开了核心表,则旧代码显示为“... dbo.MyFunction(t.ID) ...”,如果未更改,但“t”现在指的是。使用临时表而不是核心表,您甚至不会收到错误 - 只是错误的结果
如果生成不必要的错误是目标(也许有些人没有足够的工作?),那么这种命名约定很棒。否则,一致的命名是正确的选择。
I just started working in a place that uses only "ID" (in the core tables, referenced by TableNameID in foreign keys), and have already found TWO production problems directly caused by it.
In one case the query used "... where ID in (SELECT ID FROM OtherTable ..." instead of "... where ID in (SELECT TransID FROM OtherTable ...".
Can anyone honestly say that wouldn't have been much easier to spot if full, consistent names were used where the wrong statement would have read "... where TransID in (SELECT OtherTableID from OtherTable ..."? I don't think so.
The other issue occurs when refactoring code. If you use a temp table whereas previously the query went off a core table then the old code reads "... dbo.MyFunction(t.ID) ..." and if that is not changed but "t" now refers to a temp table instead of the core table, you don't even get an error - just erroneous results.
If generating unnecessary errors is a goal (maybe some people don't have enough work?), then this kind of naming convention is great. Otherwise consistent naming is the way to go.
我个人更喜欢(如上所述)PK的Table.ID和TableID FK。 甚至(请不要向我开枪)Microsoft Access 也推荐这样做。
然而,我也知道一些生成工具更喜欢使用 TableID 进行 PK,因为它们倾向于链接所有包含 'ID' 一词的列名,包括 ID!!!< /em>
甚至查询设计者也在 Microsoft SQL Server 上执行此操作(对于您创建的每个查询,您最终都会删除列 ID 上的所有表上所有不必要的新创建的关系)
因此,尽管我内心的强迫症讨厌它,但我遵循TableID约定。 让我们记住,它被称为数据BASE,因为它将成为未来许多应用程序的基础。 所有技术都应该受益于具有清晰描述模式的良好规范化。
不用说,当人们开始使用 TableName、TableDescription 等时,我确实会划清界限。 在我看来,惯例应该做到以下几点:
表别名:完整表名称,单数形式。 例如。
[更新]
此外,此线程中有一些关于由于“某种关系”或角色而导致重复列的有效帖子。 例如,如果商店有一个 EmployeeID,它告诉我蹲下。 所以我有时会做一些类似Store.EmployeeID_Manager的事情。 当然它有点大,但至少人们不会疯狂地寻找表 ManagerID 或 EmployeeID 在那里做什么。 当查询是 WHERE 时,我会将其简化为:
从商店中选择 EmployeeID_Manager 作为 ManagerID
I personally prefer (as it has been stated above) the Table.ID for the PK and TableID for the FK. Even (please don't shoot me) Microsoft Access recommends this.
HOWEVER, I ALSO know for a fact that some generating tools favor the TableID for PK because they tend to link all column name that contain 'ID' in the word, INCLUDING ID!!!
Even the query designer does this on Microsoft SQL Server (and for each query you create, you end up ripping off all the unnecessary newly created relationships on all tables on column ID)
THUS as Much as my internal OCD hates it, I roll with the TableID convention. Let's remember that it's called a Data BASE, as it will be the base for hopefully many many many applications to come. And all technologies Should benefit of a well normalized with clear description Schema.
It goes without saying that I DO draw my line when people start using TableName, TableDescription and such. In My opinion, conventions should do the following:
Table alias: Full table Name, singularized. Ex.
[Update]
Also, there are some valid posts in this thread about duplicated columns due of the "kind of relationship" or role. Example, if a Store has an EmployeeID, that tells me squat. So I sometimes do something like Store.EmployeeID_Manager. Sure it's a bit larger but at leas people won't go crazy trying to find table ManagerID, or what EmployeeID is doing there. When querying is WHERE I would simplify it as:
SELECT EmployeeID_Manager as ManagerID FROM Store
为了简单起见,大多数人将列命名为表 ID。 如果它在另一个表上有外键引用,那么在连接的情况下,他们显式地调用它 InvoiceID (使用您的示例),无论如何您都会为表别名,因此显式的 inv.ID 仍然比 inv.InvoiceID 更简单
For the sake of simplicity most people name the column on the table ID. If it has a foreign key reference on another table, then they explicity call it InvoiceID (to use your example) in the case of joins, you are aliasing the table anyway so the explicit inv.ID is still simpler than inv.InvoiceID
从正式数据字典的角度来看,我将数据元素命名为
invoice_ID
。 通常,数据元素名称在数据字典中是唯一的,并且理想情况下始终具有相同的名称,但有时可能需要基于上下文的附加限定术语,例如名为employee_ID
的数据元素可以使用两次在组织结构图中,因此分别符合supervisor_employee_ID
和sublined_employee_ID
的条件。显然,命名约定是主观的并且是风格问题。 我发现 ISO/IEC 11179 指南是一个有用的起点。
对于 DBMS,我将表视为实体的集合(除了那些只包含一行的表,例如 cofig 表、常量表等),例如,我的
employee_ID
为键的表将被命名为 <代码>人员。 因此,TableNameID
约定立即对我不起作用。我已经看到在大数据模型上使用的
TableName.ID=PK TableNameID=FK
样式,不得不说我发现它有点令人困惑:我更喜欢标识符的名称在整个 ie 中保持不变名称基于它恰好出现在哪个表中。需要注意的是,上述样式似乎在商店中使用,这些商店向每个IDENTITY(自动增量)列> 表,同时避免外键中的自然键和复合键。 这些商店往往没有正式的数据字典,也没有根据数据模型构建。 再说一次,这只是一个风格问题,我个人并不认同这个问题。 所以最终,它不适合我。话虽如此,当表的名称提供这样做的上下文时,我可以看到有时从列名称中删除限定符的情况,例如名为
employee_last_name
的元素可能会简单地变为last_name
在Personnel
表中。 这里的基本原理是,域是“人们的姓氏”,并且更有可能与其他表中的last_name
列进行UNION
组合,而不是用作另一个表中的外键,但话又说回来......我可能会改变主意,有时你永远无法判断。 事情就是这样:数据建模一半是艺术,一半是科学。Coming at this from the perspective of a formal data dictionary, I would name the data element
invoice_ID
. Generally, a data element name will be unique in the data dictionary and ideally will have the same name throughout, though sometimes additional qualifying terms may be required based on context e.g. the data element namedemployee_ID
could be used twice in the org chart and therefore qualified assupervisor_employee_ID
andsubordinate_employee_ID
respectively.Obviously, naming conventions are subjective and a matter of style. I've find ISO/IEC 11179 guidelines to be a useful starting point.
For the DBMS, I see tables as collections of entites (except those that only ever contain one row e.g. cofig table, table of constants, etc) e.g. the table where my
employee_ID
is the key would be namedPersonnel
. So straight away theTableNameID
convention doesn't work for me.I've seen the
TableName.ID=PK TableNameID=FK
style used on large data models and have to say I find it slightly confusing: I much prefer an identifier's name be the same throughout i.e. does not change name based on which table it happens to appear in. Something to note is the aforementioned style seems to be used in the shops which add anIDENTITY
(auto-increment) column to every table while shunning natural and compound keys in foreign keys. Those shops tend not to have formal data dictionaries nor build from data models. Again, this is merely a question of style and one to which I don't personally subscribe. So ultimately, it's not for me.All that said, I can see a case for sometimes dropping the qualifier from the column name when the table's name provides a context for doing so e.g. the element named
employee_last_name
may become simplylast_name
in thePersonnel
table. The rationale here is that the domain is 'people's last names' and is more likely to beUNION
ed withlast_name
columns from other tables rather than be used as a foreign key in another table, but then again... I might just change my mind, sometimes you can never tell. That's the thing: data modelling is part art, part science.关于这个问题已经有很多答案,但我想添加上面我没有看到的两件主要事情:
很多时候,客户或用户甚至其他部门的开发人员遇到了障碍,并联系我们说他们在操作时遇到问题。 我们询问他们对哪条记录有疑问。 现在,他们在屏幕上看到的数据(例如包含客户名称、订单数量、目的地等的网格)是许多表格的聚合。 他们说他们在 id 83 方面遇到了麻烦。如果它只是被称为“id”,则无法知道它是什么、它是哪个表。
也就是说,一行数据没有给出任何指示它来自哪个表。 除非您碰巧非常了解数据库的架构(在复杂系统或被告知要接管的非绿地系统上很少有这种情况),否则即使您有更多数据,您也不知道 id=83 意味着什么例如姓名、地址等(甚至可能不在同一张表中!)。
该 ID 可能来自网格,也可能来自 API 中的错误,或者将错误消息转储到屏幕或日志文件的错误查询。
通常,开发人员只是将“ID”转储到列中,然后就忘记了它,并且数据库通常有许多类似的表,例如 Invoice、InvoiceGrouping、InvoicePlan,ID 可能适用于其中任何一个。 沮丧的是,您查看代码以查看它是哪一个,并发现他们也在模型上将其称为 Id,因此您必须深入研究页面模型的构建方式。 我无法计算出有多少次我必须这样做才能弄清楚 Id 是什么。 很多。 有时您还必须挖掘出一个仅返回“Id”作为标头的 SPROC。 恶梦。
SQL 通常会给出非常糟糕的错误消息。 “无法插入 ID 为 83 的项目,列将被截断”或类似的内容很难调试。 通常错误消息并不是很有帮助,但通常损坏的东西会通过转储主键名称和值来模糊地尝试告诉您哪些记录被损坏。 如果它是“ID”,那么它根本没有帮助。
这只是我觉得其他答案中没有提到的两件事。
我还认为很多评论是“如果你以 X 方式编程,那么这不是问题”,并且我认为上面的观点(以及这个问题的其他观点)是有效的,特别是因为人们编程的方式,并且因为他们没有时间、精力、预算和远见来进行完美的日志记录和错误处理编程,也没有改变快速 SQL 和代码编写的根深蒂固的习惯。
There are lots of answers on this already, but I wanted to add two major things that I haven't seen above:
Many times a customer or user or even dev of another department have hit a snag and have contacted us saying they're having a problem doing an operation. We ask them what record they're having a problem with. Now, the data they see on the screen, e.g. a grid with customer name, number of orders, destination etc is an aggregate of many tables. They say they've having trouble with id 83. There's no way to know what id that is, which table it is, if it's just called 'id'.
Namely, a row of data does not give any indication which table it is from. Unless you happen to know the schema of your database well, which is rarely the case on complex systems or non-greenfield systems you've been told to take over, you don't know what id=83 means even if you have more data like name, address, etc (which might not even be in the same table!).
This id could be coming from a grid, or it could be coming from an error in your API, or a faulty query dumping the error message to the screen, or to a log file.
Often a developer just dumps 'ID' into a column and forgets about it, and often DBs have many similar tables like Invoice, InvoiceGrouping, InvoicePlan and the ID could be for any of them. In frustration you look in the code to see which one it is, and see that they've called it Id on the model as well, so you then have to dig into how the model for the page was constructed. I cannot count how many times I've had to do this to figure out what an Id is. It's a lot. Sometimes you have to dig out a SPROC as well that just returns 'Id' as a header. Nightmare.
Often SQL can give pretty crappy error messages. "Could not insert item with ID 83, column would be truncated" or something like that is very hard to debug. Often error messages are not very helpful, but usually the thing that broke will make a vague attempt to tell you what record was broken by just dumping out the primary key name and the value. If it's "ID" then it doesn't really help at all.
This is just two things that I didn't feel were mentioned in the other answers.
I also think that a lot of comments are 'if you program in X way then this isn't an issue', and I think the points above (and other points on this question) are valid specifically because of the way people program and because they don't have the time, energy, budget and foresight to program in perfect logging and error handling or change engrained habits of quick SQL and code writing.
我投票支持 InvoiceID 作为表 ID。 当它用作外键时,我也使用相同的命名约定,并在查询中使用智能别名。
当然,它比其他一些例子更长。 但微笑吧。 这是为了子孙后代,有一天,一些可怜的初级程序员将不得不改变你的杰作。 在此示例中,没有任何歧义,并且随着附加表被添加到查询中,您将感谢它的冗长。
My vote is for InvoiceID for the table ID. I also use the same naming convention when it's used as a foreign key and use intelligent alias names in the queries.
Sure, it's longer than some other examples. But smile. This is for posterity and someday, some poor junior coder is going to have to alter your masterpiece. In this example there is no ambiguity and as additional tables get added to the query, you'll be grateful for the verbosity.
FWIW,我们的新标准(呃,我的意思是“随着每个新项目的发展”而变化)是:
pk_
前缀表示主键_id
后缀表示整数,自增 IDfk_
前缀表示外键(无需后缀)_VW
视图后缀is_
布尔值前缀因此,名为 NAMES 的表可能具有字段
pk_name_id、first_name、last_name、is_alive 和
fk_company 和一个名为
LIVING_CUSTOMERS_VW
的视图,定义如下:不过,正如其他人所说,只要一致并且不会不必要地混淆您的含义,几乎任何方案都可以工作。
FWIW, our new standard (which changes, uh, I mean "evolves", with every new project) is:
pk_
prefix means primary key_id
suffix means an integer, auto-increment IDfk_
prefix means foreign key (no suffix necessary)_VW
suffix for viewsis_
prefix for booleansSo, a table named NAMES might have the fields
pk_name_id, first_name, last_name, is_alive,
andfk_company
and a view calledLIVING_CUSTOMERS_VW
, defined like:As others have said, though, just about any scheme will work as long as it is consistent and doesn't unnecessarily obfuscate your meanings.
我绝对同意在 ID 字段名称中包含表名称,这正是您给出的原因。 一般来说,这是我唯一包含表名称的字段。
I definitely agree with including the table name in the ID field name, for exactly the reasons you give. Generally, this is the only field where I would include the table name.
我确实讨厌简单的 id 名称。 我强烈喜欢始终使用发票 ID 或其变体。 当我需要时,我总是知道哪个表是 id 的权威表,但这让我很困惑
最糟糕的是你提到的混合,完全令人困惑。 我不得不使用一个数据库,除了最常用的 id 之一之外,几乎总是 foo_id 。 那简直就是地狱。
I do hate the plain id name. I strongly prefer to always use the invoice_id or a variant thereof. I always know which table is the authoritative table for the id when I need to, but this confuses me
What's worst of all is the mix you mention, totally confusing. I've had to work with a database where almost always it was foo_id except in one of the most used ids. That was total hell.
我认为只要保持一致,您就可以使用任何内容作为“ID”。 包括表名很重要。 我建议使用像 Erwin 这样的建模工具来强制执行命名约定和标准,以便在编写查询时很容易理解表之间可能存在的关系。
第一条语句的意思是,您可以使用“recno”等其他内容来代替 ID。 那么这个表就会有invoice_recno 的PK 等等。
干杯,
本
I think you can use anything for the "ID" as long as you're consistent. Including the table name is important to. I would suggest using a modeling tool like Erwin to enforce the naming conventions and standards so when writing queries it's easy to understand the relationships that may exist between tables.
What I mean by the first statement is, instead of ID you can use something else like 'recno'. So then this table would have a PK of invoice_recno and so on.
Cheers,
Ben
对于数据库中的列名称,我将使用“InvoiceID”。
如果我通过 LINQ 将字段复制到未命名的结构中,如果它是结构中唯一的 ID,我可以将其命名为“ID”。
如果该列不打算在外键中使用,因此它仅用于唯一标识要编辑或删除的行,我会将其命名为“PK”。
For the column name in the database, I'd use "InvoiceID".
If I copy the fields into a unnamed struct via LINQ, I may name it "ID" there, if it's the only ID in the structure.
If the column is NOT going to be used in a foreign key, so that it's only used to uniquely identify a row for edit editing or deletion, I'll name it "PK".
如果您为每个键指定一个唯一的名称,例如“invoices.invoice_id”而不是“invoices.id”,那么您可以毫无顾虑地使用“自然连接”和“using”运算符。 例如,
代替
SQL 已经足够冗长,但不会使其变得更冗长。
If you give each key a unique name, e.g. "invoices.invoice_id" instead of "invoices.id", then you can use the "natural join" and "using" operators with no worries. E.g.
instead of
SQL is verbose enough without making it more verbose.
为了使自己保持一致(表具有用作 ID 的单列主键),我所做的就是命名表的主键
Table_pk
。 任何有外键指向该表主键的地方,我都会将该列称为PrimaryKeyTable_fk
。 这样我就知道,如果我的客户表中有一个Customer_pk
,并且我的订单表中有一个Customer_fk
,我就知道订单表引用了客户中的一个条目桌子。对我来说,这很有意义,特别是对于我认为读起来更容易的连接。
What I do to keep things consistent for myself (where a table has a single column primary key used as the ID) is to name the primary key of the table
Table_pk
. Anywhere I have a foreign key pointing to that tables primary key, I call the columnPrimaryKeyTable_fk
. That way I know that if I have aCustomer_pk
in my Customer table and aCustomer_fk
in my Order table, I know that the Order table is referring to an entry in the Customer table.To me, this makes sense especially for joins where I think it reads easier.
我更喜欢域名 || 'ID'。 (即DomainName + ID)
DomainName 通常(但并非总是)与TableName 相同。
ID 本身的问题在于它无法向上扩展。 一旦您拥有大约 200 个表,每个表都有一个名为 ID 的第一列,数据就开始看起来很相似。 如果您始终使用表名来限定 ID,这会有一点帮助,但没有那么大。
域名 & ID 可用于命名外键和主键。 当外键以其引用的列命名时,这可以提供助记帮助。 形式上,不必将外键的名称与其引用的键绑定在一起,因为引用完整性约束将建立引用。 但在读取查询和更新时它非常方便。
有时,域名 || 不能使用“ID”,因为同一个表中会有两列具有相同的名称。 示例:Employees.EmployeeID 和Employees.SupervisorID。 在这些情况下,我使用 RoleName || “ID”,如示例所示。
最后但并非最不重要的一点是,我尽可能使用自然键而不是合成键。 在某些情况下,自然密钥不可用或不可信,但在很多情况下,自然密钥是正确的选择。 在这些情况下,我让自然键采用它自然具有的名称。 这个名字通常甚至没有字母“ID”。 示例:OrderNo,其中 No 是“Number”的缩写。
I prefer DomainName || 'ID'. (i.e. DomainName + ID)
DomainName is often, but not always, the same as TableName.
The problem with ID all by itself is that it doesn't scale upwards. Once you have about 200 tables, each with a first column named ID, the data begins to look all alike. If you always qualify ID with the table name, that helps a little, but not that much.
DomainName & ID can be used to name foreign keys as well as primary keys. When foriegn keys are named after the column that they reference, that can be of mnemonic assistance. Formally, tying the name of a foreign key to the key it references is not necessary, since the referential integrity constrain will establish the reference. But it's awfully handy when it comes to reading queries and updates.
Occasionally, DomainName || 'ID' can't be used, because there would be two columns in the same table with the same name. Example: Employees.EmployeeID and Employees.SupervisorID. In those cases, I use RoleName || 'ID', as in the example.
Last but not least, I use natural keys rather than synthetic keys when possible. There are situations where natural keys are unavailable or untrustworthy, but there are plenty of situations where the natural key is the right choice. In those cases, I let the natural key take on the name it would naturally have. This name often doesn't even have the letters, 'ID' in it. Example: OrderNo where No is an abbreviation for "Number".
请参阅 Interakt 网站的 命名约定用于经过深思熟虑的表和列的命名系统。 该方法为每个表使用后缀(
_prd
用于产品表,或_ctg
用于类别表),并将其附加到给定表中的每一列。 因此,产品表的标识列将为 id_prd,因此在数据库中是唯一的。他们更进一步来帮助理解外键:产品表中引用类别表的外键将是 idctg_prd ,这样它属于哪个表就很明显了(
_prd
后缀)及其引用的表(类别)。优点是不同表中的标识列不会产生歧义,并且您可以通过列名一眼看出查询引用的是哪些列。
See the Interakt site's naming conventions for a well thought out system of naming tables and columns. The method makes use of a suffix for each table (
_prd
for a product table, or_ctg
for a category table) and appends that to each column in a given table. So the identity column for the products table would beid_prd
and is therefore unique in the database.They go one step further to help with understanding the foreign keys: The foreign key in the product table that refers to the category table would be
idctg_prd
so that it is obvious to which table it belong (_prd
suffix) and to which table it refers (category).Advantages are that there is no ambiguity with the identity columns in different tables, and that you can tell at a glance which columns a query is referring to by the column names.
对于每个表,我选择一个树形字母简写(例如,Employees => Emp),
这样数字自动编号主键就变成了nkEmp。
它很短,在整个数据库中是唯一的,我一眼就知道它的属性。
我在 SQL 和我使用的所有语言(主要是 C#、Javascript、VB6)中保留相同的名称。
For each table I choose a tree letter shorthand(e.g. Employees => Emp)
That way a numeric autonumber primary key becomes nkEmp.
It is short, unique in the entire database and I know exactly its properties at a glance.
I keep the same names in SQL and all languages I use (mostly C#, Javascript, VB6).
您可以使用以下命名约定。 它有其缺陷,但它可以解决您的特定问题。
inv
、InvoiceLines -invl
inv_id
、invl_id
invl_inv_id
作为名称。这样你就可以说
You could use the following naming convention. It has its flaws but it solves your particular problems.
inv
, InvoiceLines -invl
inv_id
,invl_id
invl_inv_id
for the names.this way you could say