sql - #1064 - 你的 SQL 语法有错误;检查与您的 MariaDB 服务器相对应的手册

标签 sql mariadb

我试图在我的 sql 服务器上运行以下查询:

CREATE TABLE `e_store`.`products`(
    `id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
    `name` VARCHAR(250) NOT NULL ,
    `brand_id` INT UNSIGNED NOT NULL ,
    `category_id` INT UNSIGNED NOT NULL ,
    `attributes` JSON NOT NULL ,
    PRIMARY KEY(`id`) ,
    INDEX `CATEGORY_ID`(`category_id` ASC) ,
    INDEX `BRAND_ID`(`brand_id` ASC) ,
    CONSTRAINT `brand_id` FOREIGN KEY(`brand_id`) REFERENCES `e_store`.`brands`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE ,
    CONSTRAINT `category_id` FOREIGN KEY(`category_id`) REFERENCES `e_store`.`categories`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE
);

我的 e_store 数据库中已经有品牌和类别表。

但我收到以下错误:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'JSON NOT NULL ,
    PRIMARY KEY(`id`) ,
    INDEX `CATEGORY_ID`('category_id' ' at line 6

最佳答案

对于那些面临与我类似的问题的人:
MariaDB 本身没有实现 JSON 数据类型,但它使用它作为 LONGTEXT 的别名。出于兼容性原因。根据文档( https://mariadb.com/kb/en/library/json-data-type/ ):

JSON is an alias for LONGTEXT introduced for compatibility reasons with MySQL's JSON data type. MariaDB implements this as a LONGTEXT rather, as the JSON data type contradicts the SQL standard, and MariaDB's benchmarks indicate that performance is at least equivalent.

In order to ensure that a a valid json document is inserted, the JSON_VALID function can be used as a CHECK constraint.


因此,如果您在使用 JSON 时遇到问题MariaDB 中的数据类型只需简单地使用 LONGTEXT . ;-)

关于sql - #1064 - 你的 SQL 语法有错误;检查与您的 MariaDB 服务器相对应的手册,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42185598/

相关文章:

mysql - 通过非规范化表进行 SQL 连接 - 选择语法是什么?

php - 将日期转换为此格式

mysql - 从两个相对于最近日期的单独表创建一个表

sql - Oracle 索引排序顺序和连接

sql - 在 SSMS 对象资源管理器的过滤器设置中使用通配符/正则表达式来查找对象

mysql - READ COMMITTED ERROR 1665 无法写入二进制日志

mysql - MariaDB ER_TRUNCATED_WRONG_VALUE : Incorrect datetime value for values before 1970-01-01 00:00:00

mysql - 在其他服务器上恢复mysqldump不会恢复密码

mysql - 如何使用 mysql.server start 指定启动选项

mysql - 使用 IF-THEN-ELSE 的触发器语法错误