1

My first thought was to check server version but it is not the nicest solution. Is there something like PHP's function_exists() in mysql?

I'd like to detect JSON_EXTRACT() or the JSON data type: CREATE TABLE t1 (jdoc JSON);

Thank you.

0

4 Answers 4

1

One answer is that all currently supported versions of MySQL support JSON.

MySQL 5.6 does not support JSON, but that branch passed its end-of-life in February 2021 (over a year ago as I write this). You could legitimately make a policy that your minimum required version of MySQL is 5.7, and you don't support any versions of MySQL that are past their end-of-life. If your code returns an error when it references any JSON functions or data types, you are not obligated to fix that error.

In MySQL 8.0, a new information_schema table called KEYWORDS can be queried for the word "JSON". If that table exists, and "JSON" appears in the table, then that instance of MySQL definitely supports JSON. But that table is not present in older versions of MySQL, even 5.7 which does support JSON. So it is not conclusive.

If this were my task, I would just make a condition based on the version of MySQL, as you mentioned in your question. The future of MySQL is that new features can show up in any minor release, so we'll all have to get used to checking the full version.


MariaDB has a similar state: JSON is supported in version 10.2, and all versions of MariaDB 10.1 or older are past their end-of-life date.

0

I don't think there is any such feature in MariaDB or MySQL themselves at the moment (MariaDB 10.3, MySQL 5.7, 8.0).

So the only way I can imagine actually detecting support for specific data types and built-in functions on-the-fly would be to try using the data type or funtion, and then catching the exception, if any.

It's also possible that certain software libraries can give you a hint as to what features the underlying RDBMS can offer. If so, this is probably done by storing a list of features associated with each RDBMS version inside the software library.

0
0

You could see if any existing columns are of the JSON data type (MySQL only) with something like:

SELECT * 
FROM columns
WHERE TABLE_SCHEMA='demo'
AND column_type='json'\G
0

there is not such internet method in mysql. but you can try to create table with column type JSON if your db engine does not support it will give you error.

you can also select specific table column types and see if there is a column with json type.

select data_type from information_schema.columns
 where table_schema = 'db_name' and table_name = 'tableName';

you can read more about information_schema table https://www.mssqltips.com/sqlservertutorial/183/information-schema-columns/

1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Mar 11, 2022 at 13:52

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.