2

I was reading some code on mysql queries etc. and I came across the use of "`", some kind of tick mark resembling the PHP backticks. I was wondering what they mean, their official name and a link to the mysql reference on how to or when to use them.

Here is an example of what I'm talking about:

$update = mysql_query ("UPDATE tablename SET (column1,`".$EXPfields."`) =('$something','".$EXPvalues."') 

WHERE .... ");

Robert
  • 10,126
  • 19
  • 78
  • 130
  • it's for column names that might be in the reserved words list – Dave Chen Jun 11 '13 at 23:21
  • 2
    possible duplicate of [Using backticks around field names](http://stackoverflow.com/questions/261455/using-backticks-around-field-names) – dev-null-dweller Jun 11 '13 at 23:22
  • Check this out: [Google](https://www.google.com.au/#gs_rn=17&gs_ri=psy-ab&tok=AIbLUi403MmFNnBWKP3ZHw&suggest=p&cp=16&gs_id=2&xhr=t&q=apostrophe+mysql&es_nrs=true&pf=p&output=search&sclient=psy-ab&oq=apostrophe+mysql&gs_l=&pbx=1&bav=on.2,or.r_qf.&bvm=bv.47810305,d.dGI&fp=3f16c0639275e9a0&biw=1600&bih=839) – Eduardo M Jun 11 '13 at 23:22

4 Answers4

4

It's the backtick operator and is used to escape words referring to field or table names that are reserved words in MySQL's dialect of SQL, or contain invalid characters such as spaces. So if you have a table called select, with a field called select, you'd have to retrieve it like this:

select `select` from `select`

The backticks allow MySQL to correctly parse this as a working query without getting confused on the meaning of the words.

Official documentation is here.

Niels Keurentjes
  • 41,402
  • 9
  • 98
  • 136
1

MySQL has Reserved Words that cannot be used as table or column names. Technically, the only place in which it is absolutely necessary to use backticks is when you have a table or column that has one of those reserved words for its name. This tells MySQL that it shouldn't interpret the name as a reserved word, but rather simply a table/column name.

However, in practice, I find that it's good to always use backticks, as it helps to distinguish between table/column names and functions, operands, and other constructs in SQL.

jraede
  • 6,846
  • 5
  • 29
  • 32
1

The backtick is the identifier quote character of mysql. As the name suggest, its purpose is to quote identifiers' (column, tables ... names), so as to make sure mysql understands what you meant.

You can find some documentation on them in mysql page about identifiers (search backtick)

Obvious example where they would help is when your column/table name include spaces, or reserved word (both of which are discouraged practices, by the way)

// error, since order is a reserved word
SELECT order FROM table
// works, the column name is quoted
SELECT `order` FROM table

// error, since there is a space
SELECT field FROM my table
// works, the column name is quoted
SELECT field FROM `my table`

There is no downside to using them, they only help mysql in parsing your queries.

Some more infos:

Benefits of using backtick (`) in MySQL queries? [dba.stackexchange.com]

Using backticks around field names [stackoverflow.com]

Community
  • 1
  • 1
Lepidosteus
  • 11,779
  • 4
  • 39
  • 51
0

They are essentially quotes around variables that contain characters which would make it invalid SQL otherwise

Using backticks around field names

has much more information on them

Community
  • 1
  • 1
exussum
  • 18,275
  • 8
  • 32
  • 65