Using SQL keyword in title of table or column

2019-01-26 17:08发布

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;

2条回答
Animai°情兽
2楼-- · 2019-01-26 17:40

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

查看更多
Bombasti
3楼-- · 2019-01-26 17:49

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.

查看更多
登录 后发表回答