予約語のテーブル

以下のようなSQLファイルを与えてテーブルを作ると「order」という予約語であってもテーブルは作成されます。

CREATE TABLE `order` (
  `id` int(11) NOT NULL auto_increment,
  `title` text,
  `express` text,
  `client_name` text default NULL,
  `client_mail_address` text,
  `add_date` datetime default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1;

しかし、普通に操作する為のDMLを普通に打っても何もかもがシンタックスエラーになってしまいます。

mysql> desc order;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'order' at line 1

予約語の場合は「`」で囲まないとダメなのですね。

mysql> desc `order`;
+---------------------+----------+------+-----+---------+----------------+
| Field               | Type     | Null | Key | Default | Extra          |
+---------------------+----------+------+-----+---------+----------------+
| id                  | int(11)  | NO   | PRI | NULL    | auto_increment |
| title               | text     | YES  |     | NULL    |                |
| express             | text     | YES  |     | NULL    |                |
| client_name         | text     | YES  |     | NULL    |                |
| client_mail_address | text     | YES  |     | NULL    |                |
| add_date            | datetime | YES  |     | NULL    |                |
+---------------------+----------+------+-----+---------+----------------+
6 rows in set (0.03 sec)

mysql>

もちろんこんなテーブルは扱いにくいので「work_order」のように予約語でない単語に命名を見直すことになります。

mysql> drop table order;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'order' at line 1
mysql> drop table `order`;
Query OK, 0 rows affected (0.00 sec)

mysql>