26

In MySQL we can create queries with or without the backtick (`) symbol. Example:

  1. SELECT * FROM TEST;
  2. SELECT * FROM `TEST`;

Both works fine in mysql-console.

Is there any technical difference between them?

Is there any benefit using (`) over over simple queries?

34

They are called quoted identifiers and they tell the parser to handle the text between them as a literal string. They are useful for when you have a column or table that contains a keyword or space. For instance the following would not work:

CREATE TABLE my table (id INT);

But the following would:

CREATE TABLE `my table` (id INT);

Also, the following would get an error, because COUNT is a reserved keyword:

SELECT count FROM some_table

But the following would be parsed correctly:

SELECT `count` FROM some_table

I hope this helps you.

|improve this answer|||||
24

If you want to use something around object identifiers, use at least the standard double quotes: "

This works in MySQL, PostgreSQL, SQL Server, Oracle, etc. etc. For MySQL you might need the SQL mode ansi_quotes, depending on the default configuration:

SET sql_mode = 'ANSI_QUOTES';

Backticks ` are only used in MySQL, you learn a type of SQL that won't work in any other brand of DBMS.

|improve this answer|||||
6

It means you can have spaces in table names. Not particular appealing, of course. Same with SQL Server's [].

|improve this answer|||||
3

It can be helpful if you have a column with name that is reserved,

eg: You can query a statement like this:

select * from tablename group by `group`;
|improve this answer|||||
1

From my point of view, @Mr.Brownstone answer is only partially correct.

The backtick ` is used in MySQL to delimit literals that represent identifiers (not strings).

This allows you to use not usually accepted characters like spaces, reserved words, etc. as identifiers. For example:

SELECT * FROM `Strange table name`;

Also important is that it also allows the parser to know if you are doing something wrong like selecting a column that doesn't exist. For example:

SELECT notexistingcolumn FROM atable;

If atable exists but notexistingcolumn column doesn't exist it will be interpreted as the literal string "notexistingcolumn" selecting it as a value (and probably emitting a warning).

Instead, if you use:

SELECT `notexistingcolumn` FROM `atable`;

It will recognize that notexistingcolumn is the name of a column that doesn't exist and throw an error.

I think is always preferable to surround identifiers with backticks including the alias and try to avoid using reserved words and strange characters for identifiers.

You can read more about the backtick of MySQL on https://dev.mysql.com/doc/refman/8.0/en/identifiers.html.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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