TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

How I Write SQL, Part 1: Naming Conventions (2014)

164 点作者 sehrope大约 7 年前

12 条评论

meritt大约 7 年前
I personally prefer <i>person_id</i> to be the primary key name (instead of <i>id</i>) in both the person table and any table which has it as a foreign key. One reason is for join syntax:<p><pre><code> select * from person join team_member using (person_id) </code></pre> The other reason is <i>person_id</i> now unambiguously refers to the same field regardless if we&#x27;re looking at the PK or a FK. It&#x27;s always <i>person_id</i>.
评论 #16906611 未加载
评论 #16904758 未加载
评论 #16905224 未加载
elchief大约 7 年前
Joe Celko, as well as ISO-11179, tell us to use collective names (&quot;personnel&quot;) or plural names (&quot;employees&quot;) for tables<p>As well, fewer keywords are plural, compared to singular, so there&#x27;s less chance of accidentally using a keyword if you use plurals<p>Haven&#x27;t yet seen an &quot;octopus&quot; table in production...
评论 #16909131 未加载
评论 #16909284 未加载
daigoba66大约 7 年前
Also important is adapting to the existing naming conventions of the database, even if you don&#x27;t like it. (Unless the existing naming conventions cause more trouble that its worth like requiring quoted identifiers or redundant prefixes&#x2F;suffixes).
评论 #16906958 未加载
评论 #16909302 未加载
daphneokeefe大约 7 年前
For naming stored procedures, there were a lot of helpful answers to my question &quot;What is your naming convention for stored procedures?&quot; on StackOverflow a few years ago. <a href="https:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;238267&#x2F;what-is-your-naming-convention-for-stored-procedures" rel="nofollow">https:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;238267&#x2F;what-is-your-nami...</a>
0xffff2大约 7 年前
Without a rigorous attempt at justifying each of these rules, I don&#x27;t find this article particularly useful. For example, can someone link to or provide a formal explanation for why table names should be singular? I actually really wanted to read the full relational algebra rational for that one.
评论 #16907149 未加载
评论 #16907266 未加载
评论 #16906975 未加载
评论 #16907450 未加载
评论 #16906946 未加载
jcadam大约 7 年前
I know a reasonable amount of SQL but am by no means a database guru. Naturally, this makes me the local database expert at my current place of employment :&#x2F;<p>The other devs&#x27; eyes glaze over when I say things like &#x27;stored procedures&#x27; and &#x27;trigger functions.&#x27; Bah.
wvenable大约 7 年前
I don&#x27;t understand this one:<p>&gt; Mixed case identifier names means that every usage of the identifier will need to be quoted in double quotes<p>I&#x27;ve used quite a few RDBMS engines, including most mentioned by the author, and I&#x27;ve never had to quote mixed-case identifier names. They work just the same as all lower-cased names or as any other case-sensitive language.<p>Most of the programming languages I use typically have the convention of using PascalCase for classes and public fields&#x2F;properties so I prefer to use that convention for tables and columns (and then everything else for consistency). When doing operations between the application and the database, the name is exactly the same without the need for translation.<p>Otherwise, I think it&#x27;s a good list.
评论 #16910253 未加载
评论 #16909914 未加载
walshemj大约 7 年前
How come I have <i>never</i> in a multi decade career come across &quot;i18n&quot;.<p>I though the canonical way of doing this was to write KEYWORDS in caps and use camel case for Variables.<p>Also never really brought into adding the type as part of a name - your type is already defined in your schema.
评论 #16904681 未加载
评论 #16905354 未加载
sytelus大约 7 年前
Lot of these is debatable. For example, I have preferred FirstName or even “[First Name]” instead of first_name in sql because lot of tooling uses these names to generate UX. Similarly using Person.PersonID instead of Person.ID gives consistency in diagrams and foreign key naming. I have used both approaches with its own pro and cons.
评论 #16906988 未加载
评论 #16904778 未加载
评论 #16904568 未加载
评论 #16906149 未加载
评论 #16906322 未加载
barrkel大约 7 年前
You should follow the conventions that make life easier in the rest of your tooling.<p>The fact is, you&#x27;re probably going to be issuing more SQL via abstractions like ORMs or querying libraries than raw SQL. If you need to work against the grain of those libraries to map your model, what upside are you getting?<p>If most of your data is queried via ActiveRecord, for example, you should use plural table names.
评论 #16904668 未加载
评论 #16904495 未加载
iblaine大约 7 年前
&gt; Avoid reserved words<p>Glad you cleared this up for the rest of us.<p>FWIW, naming conventions are like opinions. Everyone has them, and they usually differ from person to person. The best naming convention is a consistent naming convention. Also, naming conventions differ greatly by environment. A group of SQL Server engineers are going to have different standards than those of people working on mysql.
youpassbutter大约 7 年前
No. That&#x27;s how you write sql within your organization. Also there are syntactic differences between SQL flavors ( postgres, mysql, mssql, oracle, etc ) that make a SQL standard unrealistic.<p>The only generic rule is &quot;be consistent&quot;. Whatever convention&#x2F;style you choose, it should be consistent.