0

i have the update version mysql as

select version()

return 10.4.11-MariaDB

I able to create json type table but not able to create indexing in mysql

CREATE TABLE `players` (  
   `id` INT UNSIGNED NOT NULL,
   `player_and_games` JSON NOT NULL,
   `names_virtual` VARCHAR(20) GENERATED ALWAYS AS (`player_and_games` ->> '$.name') NOT NULL, 
   PRIMARY KEY (`id`)
);

i run this command its return

1 queries executed, 0 success, 1 errors, 0 warnings

Query:

CREATE TABLE `players` (
  `id` INT UNSIGNED NOT NULL,
  `player_and_games` JSON NOT NULL,
  `names_virtual` VARCHAR(20) GENERATED ALW...

Error Code: 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 '>> '$.name') NOT NULL, 
   PRIMARY KEY (`id`)
)' at line 4

Execution Time : 0 sec
Transfer Time  : 0 sec
Total Time     : 0 sec

I also used mysql offical documentation it also return the same issue.

3 Answers 3

1

Try to use json_value(). And a generated column cannot be declared NOT NULL apparently.

CREATE TABLE `players` (  
   `id` INT UNSIGNED NOT NULL,
   `player_and_games` JSON NOT NULL,
   `names_virtual` VARCHAR(20) GENERATED ALWAYS AS (json_value(`player_and_games`, '$.name')), 
   PRIMARY KEY (`id`)
);
Sign up to request clarification or add additional context in comments.

3 Comments

this is working fine ; but why official documentation query is not working?
What "official documentation" are you referring to?
Care to give more detail? But anyway this is the MySQL documentation and you use MariaDB. Both share the same heritage and therefore might be similar, but they're not the same in every aspect.
1

Be careful -- MySQL and MariaDB are not in lock-step over JSON. In particular, the >> operator exists in one but not the other. (It is just a shorthand for something slightly more verbose.)

Comments

0

I know I am late to the party, but none the less. You are getting this error because you are running MariaDB and not MySQL. They are mostly interchangeable, but their json implementations are a bit different. MariaDB does not support the ->> syntax for extracting values from json documents, instead you need to use json_unquote(json_value(, )).

You can see the list of json functions in MariaDB here: https://mariadb.com/kb/en/json-functions/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.