这个mySQL create table语句中的语法错误是什么?

时间:2016-03-23 04:59:00

标签: mysql create-table

我正在输入以下内容:

CREATE TABLE events (
`id` mediumint unsigned not null auto_increment,
`user` varchar(30) not null,
`time` datetime not null,
`duration` decimal(5,2) default 1.0,
`title` tinytext not null,
`location` text default null,
`tag` ENUM(‘red’,’orange’,’yellow’,’green’,’blue’,’violet’,’brown’,’black’) default null,
PRIMARY KEY (`id`),
FOREIGN KEY (`user`) references users (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

我收到以下回复:

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 '‘red’,’orange’,’yellow’,’green’,’blue’,’violet’,’brown' at line 8

这可能是眼睛疲惫的情况,但我很难过。我的枚举声明出了什么问题?

5 个答案:

答案 0 :(得分:2)

ENUM 中的数据包含在Single Quote

CREATE TABLE `events` (
`id` mediumint unsigned not null auto_increment,
`user` varchar(30) not null,
`time` datetime not null,
`duration` decimal(5,2) default 1.0,
`title` tinytext not null,
`location` text default null,
`tag` ENUM('red','orange','yellow','green','blue','violet','brown','black') default null,
PRIMARY KEY (`id`),
FOREIGN KEY (`user`) references users (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

注意: event MySQL保留关键字。当您使用MySQL RESERVED KEYWORDS作为标识符时,建议使用backtick

答案 1 :(得分:2)

变化:

`tag` ENUM(‘red’,’orange’,’yellow’,’green’,’blue’,’violet’,’brown’,’black’) 

要:

`tag` ENUM('red', 'orange','yellow','green','blue','violet','brown','black') 

答案 2 :(得分:1)

问题在于您用于‘red’值的引号(ENUM)尝试此操作;)

CREATE TABLE events (
`id` mediumint unsigned not null auto_increment,
`user` varchar(30) not null,
`time` datetime not null,
`duration` decimal(5,2) default 1.0,
`title` tinytext not null,
`location` text default null,
`tag` ENUM('red','orange','yellow','green','blue','violet','brown','black') default null,
PRIMARY KEY (`id`),
FOREIGN KEY (`user`) references users (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

在执行此查询之前,请确保users表存在且具有列用户名;

答案 3 :(得分:0)

在枚举中使用单引号而不是反引号。这对我有用:

CREATE TABLE events ( `id` mediumint unsigned not null auto_increment, `user` varchar(30) not null, `time` datetime not null, `duration` decimal(5,2) default 1.0, `title` tinytext not null, `location` text default null, `tag` ENUM('red','orange','yellow','green','blue','violet','brown','black') default null,  PRIMARY KEY (`id`), FOREIGN KEY (`user`) references users (`username`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

答案 4 :(得分:0)

更改

`tag` ENUM(‘red’,’orange’,’yellow’,’green’,’blue’,’violet’,’brown’,’black’)

`tag` ENUM('red','orange','yellow','green','blue','violet','brown','black')

在''的情况下使用''。