要设置默认值,请使用DEFAULT约束,如以下语法所示:
alter table yourTableName modify column yourColumnName JSON NOT NULL DEFAULT ( JSON_OBJECT() );
让我们创建一个表-
mysql> create table demo24 −> ( −> employee_information text −> ) −> ;
这是表的说明。以下是查询-
mysql> desc demo24;
这将产生以下输出-
+----------------------+------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------------------+------+------+-----+---------+-------+ | employee_information | text | YES | | NULL | | +----------------------+------+------+-----+---------+-------+ 1 row in set (0.00 sec)
以下是将其更改为默认值的JSON数据类型的查询-
mysql> alter table demo24 modify column employee_information JSON NOT NULL DEFAULT ( JSON_OBJECT() ); Records: 0 Duplicates: 0 Warnings: 0
现在检查表的描述。以下是查询-
mysql> desc demo24;
这将产生以下输出-
+----------------------+------+------+-----+---------------+-------------------+ | Field | Type | Null | Key | Default | Extra | +----------------------+------+------+-----+---------------+-------------------+ | employee_information | json | NO | | json_object() | DEFAULT_GENERATED | +----------------------+------+------+-----+---------------+-------------------+ 1 row in set (0.00 sec)
借助insert命令将一些记录插入表中-
mysql> insert into demo24 values();;
使用select语句显示表中的记录-
mysql> select *from demo24;
这将产生以下输出-
+----------------------+ | employee_information | +----------------------+ | {} | +----------------------+ 1 row in set (0.00 sec)