【问题标题】:Where is the error in this query [closed]此查询中的错误在哪里[关闭]
【发布时间】:2014-01-30 17:01:42
【问题描述】:

我一遍又一遍地看这个,想看看我是否能得到更多的关注。我正在尝试运行创建表查询,但我似乎无法让它工作。我得到的错误是

#1064 - 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 '`1`, `id_shop` int(11) NOT NULL DEFAULT `1`, `id_lang` int(10) NOT NULL DEFAUL' at line 4

这里是我要运行的查询

CREATE TABLE IF NOT EXISTS `PREFIX_quote` (
`id_quote` int(10) NOT NULL AUTO_INCREMENT,
`reference` varchar(9) DEFAULT NULL,
`id_shop_group` int(11) NOT NULL DEFAULT `1`,
`id_shop` int(11) NOT NULL DEFAULT `1`,
`id_lang` int(10) NOT NULL DEFAULT `1`,
`id_customer` int(10) NOT NULL,
`total_discounts` decimal(17,2) NOT NULL DEFAULT `0.00`,
`total_products` decimal(17,2) NOT NULL DEFAULT `0.00`,
`total_product_wt` decimal(17,2) NOT NULL DEFAULT `0.00`,
`total_shipping` decimal(17,2) NOT NULL DEFAULT `0.00`,
`quote_number` int(10) NOT NULL DEFAULT `0`,
`quote_date` datetime NOT NULL,
`valid` int(1) NOT NULL DEFAULT `1`,
`id_employee` int(11) NOT NULL,
`date_add` datetime NOT NULL,
`date_upd` datetime NOT NULL,
PRIMARY KEY (`id_quote`),
KEY `id_customer` (`id_customer`),
KEY `id_employee` (`id_employee`));

【问题讨论】:

  • 我相信这是主题,因为有一个特定的错误消息和代码示例。这不是一般的“为我调试代码”问题。

标签: mysql create-table


【解决方案1】:

去掉整数周围的反引号。见the fiddle

CREATE TABLE IF NOT EXISTS `PREFIX_quote` (
`id_quote` int(10) NOT NULL AUTO_INCREMENT,
`reference` varchar(9) DEFAULT NULL,
`id_shop_group` int(11) NOT NULL DEFAULT 1,
`id_shop` int(11) NOT NULL DEFAULT 1,
`id_lang` int(10) NOT NULL DEFAULT 1,
`id_customer` int(10) NOT NULL,
`total_discounts` decimal(17,2) NOT NULL DEFAULT 0.00,
`total_products` decimal(17,2) NOT NULL DEFAULT 0.00,
`total_product_wt` decimal(17,2) NOT NULL DEFAULT 0.00,
`total_shipping` decimal(17,2) NOT NULL DEFAULT 0.00,
`quote_number` int(10) NOT NULL DEFAULT 0,
`quote_date` datetime NOT NULL,
`valid` int(1) NOT NULL DEFAULT 1,
`id_employee` int(11) NOT NULL,
`date_add` datetime NOT NULL,
`date_upd` datetime NOT NULL,
PRIMARY KEY (`id_quote`),
KEY `id_customer` (`id_customer`),
KEY `id_employee` (`id_employee`));

【讨论】:

    【解决方案2】:

    反引号(`)在MySQL中用于引用identifiers,这样可以在其中出现保留字或特殊字符。它们通常用于包含您正在执行的列名称,即使它通常是不必要的,但不是 :从默认值中删除反引号:

    CREATE TABLE IF NOT EXISTS `PREFIX_quote` (
    `id_quote` int(10) NOT NULL AUTO_INCREMENT,
    `reference` varchar(9) DEFAULT NULL,
    `id_shop_group` int(11) NOT NULL DEFAULT 1,
    ...
    

    【讨论】:

      【解决方案3】:

      您的值是反引号:`1`,这是不正确的。这可以用引号引起来,或者因为它是一个数字,所以不加引号。

      `id_shop_group` int(11) NOT NULL DEFAULT 1,
      

      确保对所有字段都执行此操作。

      【讨论】: