【问题标题】:What is my syntax error in this mySQL create table statement?这个 mySQL create table 语句中的语法错误是什么?
【发布时间】:2016-03-23 04:58:59
【问题描述】:

我正在输入以下内容:

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

这可能是眼睛疲倦的情况,但我很难过。我的枚举语句有什么问题?

【问题讨论】:

  • 不要使用 Word 构建查询
  • @Strawberry 实际上,我使用的是 Mac 的 TextEdit,以确保文档是纯文本而不是富文本。
  • 嗯,你从某个地方得到了聪明的报价!

标签: mysql create-table


【解决方案1】:

变化:

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

收件人:

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

【讨论】:

  • 太棒了。我以为我快疯了,但是我的文本编辑器中纯文本中使用的单引号与命令行中解析的单引号不同。
【解决方案2】:

通过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;

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

【讨论】:

  • 谢谢。关于保留关键字的注意事项。
  • 事件不是保留关键字。
  • 谢谢,可能是我错了。但是event 的合适术语应该是什么? here 声明事件是关键字。@Strawberry
【解决方案3】:

问题在于您用于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 表存在并且有 username 列;

【讨论】:

    【解决方案4】:

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

    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;
    

    【讨论】:

      【解决方案5】:

      改变

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

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

      如果是'',请使用''。

      【讨论】:

        猜你喜欢
        • 2012-05-31
        • 2012-07-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-06
        • 1970-01-01
        • 2017-06-30
        相关资源
        最近更新 更多