如何更优雅的写出你的SQL语句

毫无疑问,编写代码是一门艺术而非科学,没有程序员可以编写出既可读又可维护的漂亮代码,即使有经验也是如此。

SQL查询也是如此。构建查询的方式,编写它的方式,如果正确的话,可以很好地将您的意图传达给其他开发人员。

常常,在我看到来自多个开发人员的电子邮件中的SQL查询时,我能看出他们的编写风格的显著差异。一些开发人员编写得非常整洁,并且对查询进行了适当的缩进,这使得很容易发现关键细节,例如,从哪个表中提取哪些列以及哪些是条件。

因为在实际的项目中,SQL查询并不是单行的,所以当您需要以后阅读这些SQL查询或需要将该查询共享给某人进行检查或执行时,这种情况下,学习正确的编写SQL查询的方式会带来很大的不同。

在本文中,我将向您展示一些我过去尝试过的编写风格,介绍它们的优缺点以及我认为最好的SQL查询编写方式。

除非你有很好的理由不使用我推荐的风格,例如:你有更好的风格,或者你想坚持项目中一直使用的风格(一致性压倒一切),没有理由不使用它。

顺便说一句,我希望您熟悉SQL,了解SQL查询中的不同子句及其含义。如果不是,你最好通过参加一个很好的课程来获得SQL经验。

第一种写SQL的方式

SELECT 
e.emp_id, e.emp_name, d.dept_name, p.project_name from Employee e
INNER JOIN Department d ON e.dept_id = d.dept_id
INNER JOIN Projects p ON e.project_id = p.project_id
Where d.dept_name="finance" and e.emp_name like '%A%' and e.salary > 5000;

优点:

使用了大小写混合将关键字从列和表名之间分开,如SELECT使用大写,而Employee使用首字母大写,但如果你喜欢一致性的话,可以让from关键字也大写,没有研究只显示那种写法更好。

缺点:

1)大小写混合

2)整个查询写在一行上,一旦表和列的数量增加,这一行就不可读了

3)在添加新条件或删掉条件时没有灵活性

第二种书写SQL查询的方式

SELECT e.emp_id, e.emp_name, d.dept_name, p.project_name
from Employee e
INNER JOIN Department d ON e.dept_id = d.dept_id
INNER JOIN Projects p ON e.project_id = p.project_id
Where d.dept_name="finance" 
and e.emp_name like '%A%' 
and e.salary > 500;

改进:

1) SQL查询被划分为多行,使其更具可读性

问题

1)大小写混合

2) where子句上的所有条件位于同一行上,也就是说通过注释排除它们不是那么容易。


第三种书写SQL的方式,也是做好的一种

select e.emp_id, e.emp_name, d.dept_name
from Employee e
inner join Department d on e.dept_id = d.dept_id
where d.dept_name = 'finance'
and e.emp_name like '%A%'
and e.salary > 500;

1)将SQL查询划分为多行可以提高可读性

2)使用适当的缩进可以很容易地找到数据源,例如表和join

3)让条件语句都放在单独的行上,可以容易的注释掉某个条件进行调试。

select e.emp_id, e.emp_name, d.dept_name
from Employee e
inner join Department d on e.dept_id = d.dept_id
where d.dept_name = 'finance'
-- and e.emp_name like '%A%';
add e.salary > 5000

顺便说一句,如果您喜欢用大写字母表示关键字,您还可以编写如下所示的相同的SQL查询,这些规则是相同的,只是关键字是大写字母。

这就是我要说的如何编写可读和更易于维护的SQL查询。对于SQL查询的缩进或样式化,您有什么看法?

这是一种简单的技术,但非常强大,对于提高涉及多个连接的复杂SQL查询的可读性大有帮助,如我前面的示例所示。

如果您喜欢,也可以在网上使用各种SQL格式器,但我建议您学习一种样式并坚持使用它,而不是依赖于格式器。

来源:SQL数据库开发

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SQL代码美化程序 SQL Pretty Printer 3.2.8 Copyright 2005-2011, Gudu Software. All Rights Reserved http://www.dpriver.com -------------------------------------------------------- Overview -------- SQL Pretty Printer is a tool that will help you beautify your SQL code. Using hotkey functionality, SQL Pretty Printer can reformat SQL statements for a wide variety of database tools such as Microsoft Query Analyzer, SQL Server Management Studio (SSMS), TOAD and PL/SQL Developer, development environments such as Visual Studio 2003/2005/2008 and Eclipse, and popular editors such as UltraEditor and EditPlus. In addition to beautifying SQL code, SQL Pretty Printer can translate SQL code into C#, Java, PHP, DELPHI and other program languages. SQL Pretty Printer also includes command line functionality, with the ability to format single files, single directories and multiple directories. SQL Pretty Printer is designed to deal with the syntax used by most popular database systems including Microsoft SQL Server, Oracle, IBM DB2, MySQL and Microsoft Access (Informix, Sybase, and PostgreSQL support is currently in development). Output conforms to most of the entry level SQL99 Standard. Add-Ins for SSMS and Visual Studio 2003/2005/2008/2011 are available. APIs for dotnet and COM version are available. features: ** Beautifies SQL statements utilizing highly customizable format options. ** Formats SQL on-the-fly in popular tools and editors using hotkey functionality. ** Minimizes to the system tray for quick access. ** Includes a command line for batch conversion of single files, single directories or directory trees (use the command line API in your own program!) ** Verifies SQL syntax with detailed error information. ** Converts monochrome SQL code into colorful RTF document. ** Converts monochrome SQL code into colorful HTML for easy placement in blogs and forums. ** Converts SQL to various programming languages including C#, Java, DELPHI, PHP and others. ** Currently supports SQL syntax for Microsoft SQL Server, Oracle, IBM DB2, MySQL and Microsoft Access (Informix, Sybase, and PostgreSQL support is currently in development). ** Add-In for SQL Server Management Studio available. ** Add-In for Visual Studio 2003/2005/2008 available. Requirements ------------ Pentium class CPU or higher Windows 95/98/NT/2000/XP/Vista/win7
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值