MYSQL:您的SQL语法有错误

时间:2018-03-24 19:26:13

标签: mysql sql

我遇到了这条SQL语句的问题

CREATE TABLE users
(
  'id' INT(11) UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE,
  'first_name' VARCHAR(250) NOT NULL,
  'last_name' VARCHAR(250) NOT NULL,
  'user_name' VARCHAR(50) NOT NULL UNIQUE,
  'email' VARCHAR(250) NOT NULL UNIQUE,
  'password' VARCHAR(250) NOT NULL,
  'last_access' DATETIME DEFAULT NULL,
  'phone' VARCHAR(250) DEFAULT NULL,
  'cellphone' VARCHAR(250) DEFAULT NULL,
  'status' ENUM('Active', 'Inactive', 'Blocked', 'Deleted', 'Newreg') DEFAULT 'Active',
  PRIMARY KEY ('id')
) ENGINE=InnoDB CHARSET=utf8;

MySQL Workbench没有显示任何语法错误, 但执行时会抛出以下错误

[2018-03-24 16:07:49] [42000][1064] 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 ''id' INT(11) UNSIGNED AUTO_INCREMENT UNIQUE,
[2018-03-24 16:07:49] 'first_name' VARCHAR(250) NOT NUL' at line 3

1 个答案:

答案 0 :(得分:0)

'id'是一个字符串,而不是变量名。省略单引号:

CREATE TABLE users
(
  id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE,
  ...
  PRIMARY KEY (id)
)

或者用反引号替换它们,例如:

CREATE TABLE users
(
  `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE,
  ...
  PRIMARY KEY (`id`)
)