在 MySQL 查询中省略分号是否不好? [英] Is it bad to omit semicolon in MySQL queries?

查看:125
本文介绍了在 MySQL 查询中省略分号是否不好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近一直在 MySQL 查询末尾省略分号,我突然想到这可能会在服务器高负载、缓存等期间产生负面影响.是否有任何此类影响?

I've been omitting the semicolon at the end of my MySQL queries recently, and it occurred to me that this might have possible negative effects maybe during server high load, caching etc. Are there any of such effects?

推荐答案

如果系统能够在没有分号的情况下判断语句的结束,省略它们也无妨.如果系统感到困惑,这很重要.既然你已经能够离开它们,那么显然没有问题.很大程度上取决于您如何编写 SQL.如果您使用 PHP 编写单个语句,然后将它们发送到 MySQL 进行处理,则分号是可选的.

If the system is able to tell the end of the statement without the semicolon, omitting them does no harm. If the system gets confused, it matters. Since you've been able to leave them off, there isn't apparently a problem. A lot depends on how you're writing your SQL. If you're writing single statements in, say, PHP and then sending them to MySQL for processing, the semicolon is optional.

您询问它是否可能在服务器高负载、缓存等期间可能产生负面影响".答案是不".如果它有影响,那就是对你的意思的基本解释,它有效"和它不能编译,更不用说运行"之间的区别几乎是不可避免的.加载或缓存等效果完全独立于分号的存在与否.

You ask if it "might have possible negative effects maybe during server high load, caching etc." The answer to that is 'No'. If it has an effect, it is on the basic interpretation of what you meant, and the difference is almost inevitably between 'it works' and 'it does not compile, let alone run'. Effects such as load or caching are completely independent of the presence or absence of semicolons.

这个答案相当普遍.有一个对 SQL Server 问题的引用,该问题表明 SQL Server 不需要分号,但在更新的版本中进行了更改,因此它们是必需的.它适用于大多数其他 DBMS.处理 SQL 脚本的 SQL 命令解释器需要知道 SQL 语句之间的边界,分号是表示它的标准方式,尽管还有其他约定(我相信有些使用 go,有些使用 a斜线 / 用于作业).但从根本上说,它归结为行得通".如果是,那么分号就没有必要了.

This answer applies fairly generally. There's a reference to an SQL Server question which suggests that SQL Server did not need semicolons but is being changed in more recent editions so that they are necessary. It applies to most other DBMS. SQL command interpreters working on an SQL script need to know the boundaries between SQL statements, and the semicolon is the standard way of representing that, though there are other conventions (I believe some use go and some use a slash / for the job). But fundamentally, it comes down to "does it work". If it does, then the semicolon was not necessary.

这篇关于在 MySQL 查询中省略分号是否不好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆