When using phpMyAdmin to work on mySQL databases, you may have noticed that database and table names, as well as fields, are surrounded by backticks. If you do a query, the result may look something like this:
SELECT * FROM `product` WHERE `name` LIKE 'Articulation'
But you can take them out and it still works.
SELECT * FROM product WHERE name LIKE 'Articulation'
There is a discussion of this at StackOverflow and the consensus is that it is a non-standard practice that could cause problems if you migrate to another DBMS, but it has advantages if you are sticking with mySQL.
If you use backticks in your code, you can use reserved keywords for table names. You can also use table names with spaces.
“It’s a lot easier to search your code-base for something in backticks. Say you have a table named event. grep -r “event” * might return hundreds of results. grep -r “\`event\`” * will return anything probably referencing your database.” (EllisGL)
If you use them consistently, it does make the code a bit easier to read, but harder to write. Note that if you need more than the table name, you put backticks around the database as well as the table. e.g. `myDatabase`.`myTable` NOT `myDatabase.myTable`
If you are doing a bunch of things in a query it can get a bit messy. Here’s an example with and without.
SELECT `product`.`name`, `category`.`name`
FROM `product`
INNER JOIN product_category AS category
ON `product`.`category_id` = `category`.`id`
But you can take them out and it still works.
SELECT product.name, category.name
FROM product
INNER JOIN product_category AS category
ON product.category_id = category.id
You can even take out just some of them and it works fine, however it is probably not good coding practice.
SELECT product.name, `category`.name
FROM `product`
INNER JOIN product_category AS category
ON `product`.`category_id` = `category`.`id`
Note that you still need to use quotes for search terms (like ‘Articulation’ above) and when inserting into non-numeric fields.
I haven’t given this topic any thought before now, but looking at my PHP code, I noticed that I never use backticks in my queries.