26

I have building MYSQL queries with backticks. For example,

SELECT `title` FROM `table` WHERE (`id` = 3)

as opposed to:

SELECT title FROM table WHERE (id = 3)

I think I got this practice from the Phpmyadmin exports, and from what I understood, even Rails generates its queries like this.

But nowadays I see less and less queries built like this, and also, the code looks messier and more complicated with backticks in queries. Even with SQL helper functions, things would be simpler without them. Hence, I'm considering to leave them behind.

I wanted to find out if there is other implication in this practice such as SQL (MySQL in my case) interpretation speed, etc. What do you think?

6 Answers 6

25

Backticks also allow spaces and other special characters (except for backticks, obviously) in table/column names. They're not strictly necessary but a good idea for safety.

If you follow sensible rules for naming tables and columns backticks should be unnecessary.

8
  • Is date not a sensible column name? Jul 14, 2015 at 21:16
  • 1
    date is one of those that seems sensible, but is a MySQL reserved word. MySQL reserved words are listed here: dev.mysql.com/doc/refman/5.5/en/keywords.html Jul 14, 2015 at 21:25
  • 1
    @Achilles-96 Rename the table or column. It's a seriously bad idea to include syntax-meaningful punctuation or special characters in a table or column name. Jul 7, 2016 at 20:31
  • 1
    @Tenner, I contribute to phpMyAdmin. So, I think you get the idea when I say column name will be entered by the user. Jul 9, 2016 at 11:41
  • 1
    @Achilles-96 Good, because that's about the only proper usage of user-entered column names. ;-) Jul 11, 2016 at 16:55
19

Every time I see this discussed, I try to lobby for their inclusion, because, well, the answer is hidden in here already, although wryly winked away without further thought. When we mistakenly use a keyword as a field or table name, we can escape confusion by various methods, but only the keenly aware back-tick ` allows an even greater benefit!!!

Every word in a sql statement is run through the entire keyword hash table to see if conflicts, therefore, you've done you query a great favor by telling the compiler that, hey, I know what I'm doing, you don't need to check these words because they represent table and field names. Speed and elegance.

Cheers, Brad

1
  • 7
    Speeding up the SQL parser is an entirely specious reason. The speedup that you expect the SQL parser to gain is entirely imaginary. If you think there's a speedup, measure it and quantify. Otherwise, it's an unnecessary and therefore premature optimization. Aug 23, 2013 at 19:03
3

backticks are used to escape reserved keywords in your mysql query, e.g. you want to have a count column—not that uncommon.

you can use other special characters or spaces in your column/table/db names

they do not keep you safe from injection attacks (if you allow users to enter column names in some way—bad practice anyway)

they are not standardized sql and will only work in mysql; other dbms will use " instead

2

Well, if you ensure that you never accidentally use a keyword as an identifier, you don't need the backticks. :-)

0
2

You read the documentation on identifiers at http://dev.mysql.com/doc/refman/5.6/en/identifiers.html

SQL generators will often include backticks, as it is simpler than including a list of all MySQL reserved words. To use any1 sequence of BMP Unicode characters except U+0000 as an identifier, they can simply

  1. Replace all backticks with double backticks
  2. Surround that with single backticks

When writing handmade queries, I know (most of) MySQL's reserved words, and I prefer to not use backticks where possible as it is shorter and IMO easier to read.

Most of the time, it's just a style preference -- unless of course, you have a field like date or My Field, and then you must use backticks.

1. Though see https://bugs.mysql.com/bug.php?id=68676

1

My belief was that the backticks were primarily used to prevent erroneous queries which utilized common SQL identifiers, i.e. LIMIT and COUNT.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.