[英]Trying To Change MYSQL's Table Field Type From BLOB to JSON
嘗試為 MySql 數據庫運行語句時,我不斷收到錯誤消息。 我想要做的是將列名的類型從 BLOB 更改為 JSON。
這是聲明:
ALTER TABLE `main_db`.`main_table`
CHANGE COLUMN `name` `name` JSON CHARACTER SET 'utf8mb4' NULL DEFAULT NULL ;
這是工作台自動為我創建的東西,當我運行它時......我收到這個錯誤:
Operation failed: There was an error while applying the SQL script to the database.
Executing:
ALTER TABLE `main_db`.`main_table`
CHANGE COLUMN `name` `name` JSON CHARACTER SET 'utf8mb4' NULL DEFAULT NULL ;
ERROR 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CHARACTER SET 'utf8mb4' NULL DEFAULT NULL' at line 2
SQL Statement:
ALTER TABLE `main_db`.`main_table`
CHANGE COLUMN `name` `name` JSON CHARACTER SET 'utf8mb4' NULL DEFAULT NULL
任何幫助將不勝感激。
謝謝 :)
嘗試簡化命令並消除您沒有更改的部分:
ALTER TABLE `main_db`.`main_table`
CHANGE COLUMN `name` `name` JSON;
如何通過 TEXT 分兩步將列轉換為 JSON?
ALTER TABLE `main_db`.`main_table` MODIFY `name` TEXT;
ALTER TABLE `main_db`.`main_table` MODIFY `name` JSON;
使用MODIFY 而不是 CHANGE,因為您不需要重命名列。
這就是我所做的,顯式轉換、刪除舊列並重命名臨時列:
alter table testjson add jdata json;
update testjson set jdata = cast(convert(cast(xdata as char(999999)) using utf8) as json);
alter table testjson drop column xdata;
alter table testjson change column jdata xdata json;
值得注意的是 LOAD..INFILE 中有一個相關的技巧:
LOAD DATA LOCAL INFILE "/tmp/another.csv" INTO TABLE another(@hid, @htype, @bindata) set id=unhex(@hid), type=unhex(@htype), jdata=cast(convert(cast(@bindata as char(999999)) using utf8) as json);
要獲取現有數據,您需要創建一個新的 json 格式臨時列。 通過將 blob 列中的數據轉換為字符串類型,將其復制到臨時列中。
update main_db set temp = CAST(name AS CHAR(100000));
現在重命名這兩個列,您將獲得現有數據。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.