0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

MySQL 予約語と同じカラム名にINDEXを貼ろうとしてハマった

Last updated at Posted at 2017-01-22

テーブルのカラム名をkeyという名前で定義していて、そのカラムにINDEXを貼ろうとして2時間ぐらいハマった。

crt_configs.sql
CREATE TABLE `configs` (
	`id` int(11) NOT NULL AUTO_INCREMENT,
	`key` varchar(255) NOT NULL,
	`value` text,
	`note` text,
	PRIMARY KEY(`id`),
	INDEX idx_configs_key(key(255))
) ENGINE=INNODB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

Syntaxエラー

ERROR 1064 (42000) at line 4 in file: 'path/crt_configs.sql': 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 'key(255))
) ENGINE= INNODB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci' at line 7

解決法

keyはMySQLの予約語として既に存在するので、バッククォートで囲んでやらなければいけない。

crt_configs.sql
CREATE TABLE `configs` (
	`id` int(11) NOT NULL AUTO_INCREMENT,
	`key` varchar(255) NOT NULL,
	`value` text,
	`note` text,
	PRIMARY KEY(`id`),
	INDEX idx_configs_key(`key`(255)) # key -> `key` (バッククォートで囲む) 
) ENGINE=INNODB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

参考

識別子と予約語

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?