Winson.Wu
Winson.Wu

Reputation: 135

What is the error with this SQL insert statement?

CREATE TABLE `api_key` (
  `id` char(36) NOT NULL,
  `user_id` char(36) NOT NULL,
  `platform` char(2) NOT NULL,
  `key_type` char(1) NOT NULL,
  `key` varchar(100) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

INSERT  INTO api_key (key,key_type,platform,user_id,id) values ('123', '0', '00', '123', 'c89116d2');

Always output:

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,key_type,platform,user_id,id) values ('123', '0', '00', '123', 'c89116d2')' at line 1

Upvotes: 1

Views: 37

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522762

key is a reserved keyword in MySQL, and you should avoid using it as a name for columns, tables, etc. (read: don't use if at all possible). However, you can get around this problem by escaping the column name key with backticks. Try the following INSERT:

INSERT INTO api_key (`key`, `key_type`, `platform`, `user_id`, `id`)
VALUES ('123', '0', '00', '123', 'c89116d2');

Note that I escaped all columns in backticks for consistency, though only the key column should require it to make the INSERT work.

Upvotes: 1

Related Questions