繁体   English   中英

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

[英]What is my syntax error in this mySQL create table statement?

我输入以下内容:

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

这可能是眼睛疲劳的情况,但我很沮丧。 我的enum语句有什么问题?

Single Quote将数据括在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;

注意: event是一个MySQL RESERVED KEYWORD 当您使用MySQL RESERVED KEYWORDS作为标识符时,建议使用backtick将其backtick

更改:

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

至:

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

问题在于您用于ENUM值的双引号( 'red' )试试这个;)

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表存在并且具有列用户名。

在枚举中使用单引号而不是反引号。 这为我工作:

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;

更改

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

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

如果是,请使用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM