Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server

I was trying to run following Query on my sql server :

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
);

I have already brands and categories tables on my e_store database.

But I got the following Error :

#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
like image 645
User57 Avatar asked Feb 12 '17 08:02

User57


4 Answers

I think you are getting error for JSON datatype.

For Mysql 5.7 you can get help from below link.

https://dev.mysql.com/doc/refman/5.7/en/json.html

You can check vesrion using below query.

select version() as 'mysql version'
like image 81
Tajinder Avatar answered Nov 06 '22 13:11

Tajinder


For those who are facing this issue similar to me:

MariaDB does not natively implement the JSON data type but it uses it as an alias for LONGTEXT for compatibility reasons. According to the documentation (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.

So if you are having issues with the JSON data type in MariaDB, simply just change to LONGTEXT. ;-)

like image 37
user2633725 Avatar answered Nov 06 '22 13:11

user2633725


"JSON" is parsed in the server. JSON is one of the points of divergence.

MySQL 5.7 introduced the JSON datatype, which matches your syntax.

MariaDB 10.0.16 introduced a ENGINE=CONNECT table_type=JSON which does not match your attempted syntax.

like image 24
Rick James Avatar answered Nov 06 '22 12:11

Rick James


You have given single quotes in your index definitions instead of backticks

Try this:

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) ,  -- Changed single quotes to backticks
    INDEX `BRAND_ID`(`brand_id` ASC) ,   -- Changed single quotes to backticks
    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
);
like image 1
Gurwinder Singh Avatar answered Nov 06 '22 13:11

Gurwinder Singh