tags:

views:

53

answers:

4

In MySQL, is it necessary to surround tablenames with the tickmark? I've often seen code snippets that use the tickmarks, but I've not encountered a case where not surrounding a tablename with the tickmarks does anything different than with.


It looks like the framework I work with automatically parses reserved word keynames with tickmarks.

+3  A: 

Tickmarks are necessary when table or column names use mySQL keywords.

Dexter
+2  A: 

The tickmarks are there to distinguish the table (or column!) names from SQL reserved words. For example, without the tickmarks, a table or column named update could clash with the SQL command update.

It's best practice not to name tables and columns with reserved words in the first place, of course, but if you port an application to a new database engine (or possibly even a new MySQL version), it might have a different set of reserved words, so you may have just broken your app anyway. The tickmarks are your insurance against this sort of oops.

Nicholas Knight
Backticks are MySQL-specific, though -- other DB engines use different quotes for identifiers, like SQL Server's [TableName] or ANSI SQL's "TableName" (double quotes).
cHao
Hmm. So a feature that could be a useful aid to portability is instead implementation-specific.
Brian Hooper
@Brian: Heh, yeah, now that cHao said that, I've looked around a little closer. MySQL isn't the _only_ DBS that supports backticks, but it's the main one. Each DBS seems to prefer to do it a different way. They _do_ all seem to support plain double-quoted identifiers, but some need a particular option or mode explicitly turned on in order to do so.
Nicholas Knight
+2  A: 

It's only necessary if your table name is composed of non-"word" characters (ie: anything besides letters, numbers, and underscores), or happens to be the same as a keyword in MySQL. It's never "bad" to quote the name, but it's bad not to when you have to, so many programs that generate MySQL queries will be conservative and quote the name -- it's just easier that way.

cHao
I'd love to know why everyone's been downvoted with no comment, not even a "nyah!" :P
cHao
ask @col-shrapnel ... the guy stalks me down, down votes my posts and others' answers, and complains that i should increase my accept. irony is that he's never actually answered any of my questions. AND his accept ratio is only 50%
ina
Truth be told, in the accept rate department, he does have a point. Even if his rate is 50%, that's 50% of 4, whereas yours is less than 50% of 78 (as this comment's being written). Kinda pissy to be downvoting everyone else who tries to help, though, if he's the one doing it.
cHao
Looks like everyone on this question got downvoted once.. which I'd say is a bit rough!
Dexter
+1  A: 

I once worked at a job where they had a database of "locations". They called it states. and had everything in tables each table was named by state code. so Missouri was MO, and Arkansas was AR.

Several state codes are also reserve words OR ( Oregon), and IN (Indiana), and ON (Ontario) ... I know not exactly a state.

Even tho I think there were better ways of organizing their database data, there are cases where people want to name things a reserved word. This is an example where the `` marks kept code working.

J.J.