将默认值设置为MySQL中的JSON类型列?
要设置默认值,请使用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)热门推荐
1 带狗狗训练
4 听力训练狗狗
6 怎训练狗狗
7 腹肌训练狗狗
8 网球训练狗狗
9 记叙文训练狗狗
10 狗狗太凶怎么训练
11 核心力量训练狗狗
12 狗狗乱叫怎么训练
13 发音训练狗狗
14 怎么训练狗狗叼东西给主人
15 荆州训练狗狗
16 篮球训练狗狗
17 做训练狗狗
18 训练狗狗坐姿