I need to come up with an SQL that can ALTER a JSON field.
- A JSON field was configured with default value of
{} - But I'm getting
_utf8mb4'{}'back when querying the DDL - I was expecting the default value to be
{}. - Any idea where is
_utf8mb4documented and how do I handle it correctly?
UPDATE:
- Version is 8.0.33-0ubuntu0.22.04.2
ALTER TABLE demo MODIFY COLUMN settings JSON NOT NULL DEFAULT ('{}');
SHOW CREATE TABLE demo;
CREATE TABLE `demo` (
...
`settings` json NOT NULL DEFAULT (_utf8mb4'{}'),
...
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
https://dev.mysql.com/doc/refman/8.0/en/json.html says:
The syntax you see,
_utf8mb4'{}'is just a string literal with a character set introducer.https://dev.mysql.com/doc/refman/8.0/en/string-literals.html says:
Since JSON must use utf8mb4, I am guessing it is adding the introducer to the default expression to ensure that the string is encoded with the proper character set, just in case someone tries to insert into the table from a session configured with a different session character set.
The same thing happens if you add other text columns: