Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it bad to omit semicolon in MySQL queries? [closed]

Tags:

sql

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?

like image 576
rtuner Avatar asked Jul 02 '13 14:07

rtuner


People also ask

Is it mandatory to close SQL query with semicolon?

The semicolon (;) is used in SQL code as a statement terminator. For most SQL Server T-SQL statements it is not mandatory.

What is the purpose of the semicolon in mysql queries?

By default, mysql itself recognizes the semicolon as a statement delimiter, so you must redefine the delimiter temporarily to cause mysql to pass the entire stored program definition to the server.

Does whitespace matter in mysql?

h , whitespace does not matter. They are interpreted as function calls only when used in expression context and may be used freely as identifiers otherwise.

Is it mandatory to enclose SQL query?

It is not mandatory if you run a single query at time, it comes necessary instead if you want to run multiple query with a single command. As a rule of thumb, in JDBC semicolon is not necessary at all, if you need multiple statement use addBatch .


1 Answers

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.

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.

like image 185
Jonathan Leffler Avatar answered Sep 21 '22 13:09

Jonathan Leffler