4

I want to name one of my tables as ORDER. Can I to use in SQL keywords as names? How bad is this practice? Whether to do so?

And if such situation already exists then how do I use name of this table in queries? For example, I want:

SELECT * FROM ORDER;

or

SELECT * FROM ORDER ORDER BY NAME;
John Woo
  • 258,903
  • 69
  • 498
  • 492
  • it will be bad for the guy who has to take over your code one day... – G-Man Feb 17 '13 at 13:54
  • possible duplicate of [Syntax error due to using a reserved word as a table or column name in MySQL](http://stackoverflow.com/questions/23446377/syntax-error-due-to-using-a-reserved-word-as-a-table-or-column-name-in-mysql) – nietonfir May 06 '14 at 20:48

2 Answers2

6

wrap the tableName with backtick as ORDER is a reserved keyword.

SELECT * FROM `ORDER` ORDER BY NAME;

In my own opinion, using reserved keywords are fine except that you do not forget to handle it properly as it will give you such pain in the neck.

But through the years of designing schema, I never had used reserved keywords :D

John Woo
  • 258,903
  • 69
  • 498
  • 492
  • I think it's a hideously bad practice. Reading the code is very difficult with reserved words used for purposes other than their intended purpose. You can make up almost any name you want, why use the few that are reserved? – Joe Feb 17 '13 at 13:54
  • @Joe yes but what if it is already existing as the OP points out? anyway, it's my own opinion. – John Woo Feb 17 '13 at 13:54
  • @user2080483 what do you mean it's not working? see here http://www.sqlfiddle.com/#!2/9cf35/1 – John Woo Feb 17 '13 at 13:57
  • @user2080483 `:D` wrapping tablenames with single quotes generates syntax error. – John Woo Feb 17 '13 at 14:05
  • OP was asking if it was a good practice to do it, and I answered that. Of course if the tables are already that way, you can use them - but I'd change them as quickly as I could. – Joe Feb 17 '13 at 14:26
5

As observed in another answer, backticks are used in MySQL to indicate that a word is a database object, thus preventing confusion with reserved words - in other dialects of SQL, square brackets [ ] (SQLServer) or double quotes " " (most other dialects) are used instead.

In my experience, using reserved words in table structures tends to cause problems, especially when writing new queries - it can take a while to realise that the error being returned is due to a reserved word being a column name. There's also the point (as the existence of this question indicates) that not everyone knows that you can get around the problem by using the appropriate quoting characters in your query.