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
这可能是眼睛疲劳的情况,但我很难过。我的枚举语句有什么问题?
通过 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 保留关键字 。当您使用 MySQL RESERVED KEYWORDS
作为标识符时,建议使用 backtick
括起来。
问题出在您用于 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;
并且在执行此查询之前确保用户 table 存在并且具有列 username;
变化:
`tag` ENUM(‘red’,’orange’,’yellow’,’green’,’blue’,’violet’,’brown’,’black’)
收件人:
`tag` ENUM('red', 'orange','yellow','green','blue','violet','brown','black')
在枚举中使用单引号而不是反引号。这对我有用:
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')
在“”的情况下使用“”。
我正在输入以下内容:
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
这可能是眼睛疲劳的情况,但我很难过。我的枚举语句有什么问题?
通过 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 保留关键字 。当您使用 MySQL RESERVED KEYWORDS
作为标识符时,建议使用 backtick
括起来。
问题出在您用于 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;
并且在执行此查询之前确保用户 table 存在并且具有列 username;
变化:
`tag` ENUM(‘red’,’orange’,’yellow’,’green’,’blue’,’violet’,’brown’,’black’)
收件人:
`tag` ENUM('red', 'orange','yellow','green','blue','violet','brown','black')
在枚举中使用单引号而不是反引号。这对我有用:
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')
在“”的情况下使用“”。