gpt4 book ai didi

mysql - 将现有的 MySQL 列更改为 JSON 数据类型

转载 作者:可可西里 更新时间:2023-11-01 06:41:46 27 4
gpt4 key购买 nike

我正在尝试将 MySQL 列从 varchar(9000) NULL 更改为 MySQL 5.7 中的新 JSON 数据类型。该列包含有效的 JSON 字符串,但某些值为空。当我尝试以下操作时:

alter table log modify request json

失败并出现以下错误:

Invalid JSON text: "The document is empty." at position 0 in value for column '#sql-2f36_168a6.request'

但是,当我创建一个新列时:

alter table log add request_json json

然后插入相同的数据:

update log set request_json=json where request != ''

新的 request_json 列已更新。如何在不创建新列的情况下将现有列修改为 JSON 数据类型并保留 JSON 数据?

最佳答案

12.6 The JSON Data Type

...

  • Automatic validation of JSON documents stored in JSON columns. Invalid documents produce an error.

...

mysql> SHOW CREATE TABLE `log`\G
*************************** 1. row ***************************
Table: log
Create Table: CREATE TABLE `log` (
`request` json DEFAULT NULL
) ENGINE=InnoDB
1 row in set (0,00 sec)

mysql> SELECT `request`, JSON_VALID(`request`)
-> FROM `log`;
+-----------------+-----------------------+
| request | JSON_VALID(`request`) |
+-----------------+-----------------------+
| {"type": "bug"} | 1 |
| NULL | NULL |
| NULL | NULL |
+-----------------+-----------------------+
3 rows in set (0,00 sec)

mysql> UPDATE `log`
-> SET `request` = ''
-> WHERE `request` IS NULL;
ERROR 3140 (22032): Invalid JSON text: "The document is empty." at position 0 in value for column 'log.request'.

尝试:

mysql> DROP TABLE IF EXISTS `log`;
Query OK, 0 rows affected (0,00 sec)

mysql> CREATE TABLE IF NOT EXISTS `log` (
-> `request` VARCHAR(9000) NULL
-> );
Query OK, 0 rows affected (0,01 sec)

mysql> INSERT INTO `log`
-> (`request`)
-> VALUES
-> ('{"type": "bug"}'),
-> (NULL),
-> ('');
Query OK, 3 rows affected (0,00 sec)
Records: 3 Duplicates: 0 Warnings: 0

mysql> SELECT `request`, JSON_VALID(`request`)
-> FROM `log`;
+-----------------+-----------------------+
| request | JSON_VALID(`request`) |
+-----------------+-----------------------+
| {"type": "bug"} | 1 |
| NULL | NULL |
| | 0 |
+-----------------+-----------------------+
3 rows in set (0,00 sec)

mysql> ALTER TABLE `log` MODIFY `request` JSON;
ERROR 3140 (22032): Invalid JSON text: "The document is empty." at position 0 in value for column '#sql-1bab_4.request'.

mysql> UPDATE `log`
-> SET `request` = NULL
-> WHERE `request` = '';
Query OK, 1 row affected (0,00 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> ALTER TABLE `log` MODIFY `request` JSON;
Query OK, 3 rows affected (0,00 sec)
Records: 3 Duplicates: 0 Warnings: 0

mysql> SELECT `request`, JSON_VALID(`request`)
-> FROM `log`;
+-----------------+-----------------------+
| request | JSON_VALID(`request`) |
+-----------------+-----------------------+
| {"type": "bug"} | 1 |
| NULL | NULL |
| NULL | NULL |
+-----------------+-----------------------+
3 rows in set (0,00 sec)

mysql> SHOW CREATE TABLE `log`\G
*************************** 1. row ***************************
Table: log
Create Table: CREATE TABLE `log` (
`request` json DEFAULT NULL
) ENGINE=InnoDB
1 row in set (0,00 sec)

关于mysql - 将现有的 MySQL 列更改为 JSON 数据类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37812581/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com