我试图在我的 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'sJSON
data type. MariaDB implements this as aLONGTEXT
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/