是否有已发布的 SQL 编码风格指南?

发布于 2024-11-06 08:38:18 字数 347 浏览 1 评论 0原文

通常我会得到非常复杂的 SQL 语句,我想知道是否有样式指南规定了布置查询各个方面的通用方法。

我正在寻找类似于 Python's PEP8Zend Frameworks 指南,而不是示例代码。

我的大部分查询都是为 MySQL 编写的。

那么您知道通用风格指南吗?也许你自己写过一篇。请分享您的指导方针。

Often I will end up with very complex SQL statements and I wondered if there was style guideline out there that dictates a common way of laying various aspects of a query out.

I am look for something descriptive along the lines of Python's PEP8 or Zend Frameworks guidelines and not code by example.

Most of my queries are written for MySQL.

So do you know of a universal style guide? Perhaps you have written one yourself. Please share your guidelines.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

離殇 2024-11-13 08:38:18

自从提出这个问题以来,我根据知识共享署名-相同方式共享许可编写了一份公共 SQL 风格指南,该指南与 Joe Celko 的 SQL 编程风格书兼容。

它可以在 www.sqlstyle.guide直接从 GitHub 存储库降价

Since asking this question I have written a public SQL style guide that is compatible with Joe Celko's SQL Programming Style book under the Creative Commons Attribution-ShareAlike licence.

It is available over at www.sqlstyle.guide or as markdown directly from the GitHub repo.

何其悲哀 2024-11-13 08:38:18

以下是我们收集的一些 SQL 编程指南和最佳实践:

  • 不要在查询中使用 SELECT *。
  • 当您的 SQL 语句涉及多个源时,请始终使用表别名。
  • 使用更具可读性的 ANSI 标准连接子句而不是旧式连接。
  • 不要在 ORDER BY 子句中使用列号。
  • 始终在 INSERT 语句中使用列列表。
  • 切勿在 T-SQL 代码中使用双引号。
  • 不要在存储过程名称前添加“sp_”前缀。
  • 始终使用 SQL 格式化程序来格式化您的 SQL,例如 即时 SQL 格式化程序(免费且在线

)可以在 这篇博文

Here are some SQL programming guidelines and best practices we collected:

  • Do not use SELECT * in your queries.
  • Always use table aliases when your SQL statement involves more than one source.
  • Use the more readable ANSI-Standard Join clauses instead of the old style joins.
  • Do not use column numbers in the ORDER BY clause.
  • Always use a column list in your INSERT statements.
  • Don't ever use double quotes in your T-SQL code.
  • Do not prefix your stored procedure names with “sp_”.
  • Always use a SQL formatter to format your SQL like Instant SQL Formatter (free and online)

You can check detailed explanation of those best practices in this blog post.

会傲 2024-11-13 08:38:18

我知道的两个指南是 Joe Celko 的 SQL 编程风格 和可敬的 代码完成

还有 SQL-92 标准。它不包含样式部分,但您可能认为它的样式是隐式规范的。

Two guides I know of are Joe Celko's SQL Programming Style and the venerable Code Complete.

There's also the SQL-92 standard. It doesn't contain a style section, but you might consider it's style to be implicitly canonical.

如此安好 2024-11-13 08:38:18

MySQL对其或多或少严格的规则有一个简短的描述:

https:// /dev.mysql.com/doc/internals/en/coding-style.html

Simon Holywell 的 MySQL 最常见编码风格:

http://www.sqlstyle.guide/

另请参阅这个问题:
MySQL 有命名约定吗?

MySQL has a short description of their more or less strict rules:

https://dev.mysql.com/doc/internals/en/coding-style.html

Most common codingstyle for MySQL by Simon Holywell:

http://www.sqlstyle.guide/

See also this question:
Is there a naming convention for MySQL?

梅窗月明清似水 2024-11-13 08:38:18

Kickstarter 这里有一个风格指南。我为那些喜欢小写 SQL 和 Celko 的“河流”

我的风格指南在这里。这是一个示例:

-- basic select example
select p.Name as ProductName
     , p.ProductNumber
     , pm.Name as ProductModelName
     , p.Color
     , p.ListPrice
  from Production.Product as p
  join Production.ProductModel as pm
    on p.ProductModelID = pm.ProductModelID
 where p.Color in ('Blue', 'Red')
   and p.ListPrice < 800.00
   and pm.Name like '%frame%'
 order by p.Name

-- basic insert example
insert into Sales.Currency (
    CurrencyCode
    ,Name
    ,ModifiedDate
)
values (
    'XBT'
    ,'Bitcoin'
    ,getutcdate()
)

-- basic update example
update p
   set p.ListPrice = p.ListPrice * 1.05
     , p.ModifiedDate = getutcdate()
  from Production.Product as p
 where p.SellEndDate is null
   and p.SellStartDate is not null

-- basic delete example
delete cc
  from Sales.CreditCard as cc
 where cc.ExpYear < '2003'
   and cc.ModifiedDate < dateadd(year, -1, getutcdate())

Kickstarter has a style guide here. I have a modified version of that for people who prefer lowercase SQL and Celko's "river".

My style guide is here. Here is a sample:

-- basic select example
select p.Name as ProductName
     , p.ProductNumber
     , pm.Name as ProductModelName
     , p.Color
     , p.ListPrice
  from Production.Product as p
  join Production.ProductModel as pm
    on p.ProductModelID = pm.ProductModelID
 where p.Color in ('Blue', 'Red')
   and p.ListPrice < 800.00
   and pm.Name like '%frame%'
 order by p.Name

-- basic insert example
insert into Sales.Currency (
    CurrencyCode
    ,Name
    ,ModifiedDate
)
values (
    'XBT'
    ,'Bitcoin'
    ,getutcdate()
)

-- basic update example
update p
   set p.ListPrice = p.ListPrice * 1.05
     , p.ModifiedDate = getutcdate()
  from Production.Product as p
 where p.SellEndDate is null
   and p.SellStartDate is not null

-- basic delete example
delete cc
  from Sales.CreditCard as cc
 where cc.ExpYear < '2003'
   and cc.ModifiedDate < dateadd(year, -1, getutcdate())
深巷少女 2024-11-13 08:38:18

您是否考虑过让您的团队使用具有内置格式化功能的工具?Toad for MySql 有这个。它本身不会成为指南,但至少会带来一定的一致性。

Have you thought about getting your team to use a tool with built in formatting capabilities?Toad for MySql has this. Its not going be a guide as such but a least will bring some consistency.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文