簡體   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