Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use backticks or not when escaping keywords in MySQL?

Tags:

sql

mysql

Should all table names in MySQL be enclosed in backticks (`) to prevent collisions with reserved keywords? The reason I ask is because their use makes the SQL less portable as not all databases allow backticks.

So would avoiding table and column names containing keywords be a better course of action? If so what can be done to mitigate the risk of MySQL adding a new keyword in the next version that might collide with your schema.

Is there a best practice regarding this?

like image 622
Treffynnon Avatar asked May 10 '11 15:05

Treffynnon


People also ask

Are Backticks necessary in SQL?

Backticks ( ` ) are used to indicate database, table, and column names. Unless you're using reserved or conflicting words for table and database names, you'll not need to use them.

What is the purpose of the Backtick character in MySQL?

Backticks are mainly used to prevent an error called "MySQL reserved word".

When should I use quotes in MySQL?

This function in MySQL is used to return a result that can be used as a properly escaped data value in an SQL statement. The string is returned enclosed by single quotation marks and with each instance of backslash (\), single quote ('), ASCII NULL, and Control+Z preceded by a backslash.

How do you escape a keyword in SQL?

To escape reserved keywords in SQL SELECT statements and in queries on views, enclose them in double quotes ('').


4 Answers

The most portable way (between the systems) is to use double quotes, however, it would require enabling ANSI_QUOTES which is off by default on most installations.

So while keeping arguably useful compatibility between different engines (and incompatibility does not limit itself to backticks only but to zillion other things different between MySQL and other systems) you are killing the compatibility between different setups of MySQL which is by far more important.

Avoiding the reserved keywords is always the best solution.

like image 100
Quassnoi Avatar answered Oct 09 '22 17:10

Quassnoi


This is a matter of opinion. But portable code outweighs their use. As you noted, backticks can allow you to use reserved words, which is never a good thing. That, for me, already proves they do more harm than good.

like image 24
Jason McCreary Avatar answered Oct 09 '22 18:10

Jason McCreary


So would avoiding table and column names containing keywords be a better course of action?

In short, yes. And there isn't much you can do with respect to future keywords except avoiding obvious candidates, e.g. with, window, over, etc.

like image 42
Denis de Bernardy Avatar answered Oct 09 '22 18:10

Denis de Bernardy


One common practice is to prefix all your table names with a few letters and an underscore. It prevents collisions if you need to house two different applications in the same database, and you'll likely never run into reserved words.

like image 25
Sam Dufel Avatar answered Oct 09 '22 19:10

Sam Dufel