当前位置:网站首页>How to modify enum type values of fields in MySQL
How to modify enum type values of fields in MySQL
2022-06-09 21:41:00 【Falling ink painting snow】
1: Background description
mysql The database has the following table structure , What we need to pay attention to status Type of field , This time, the requirement is that you want to set the enumeration value of this field to error Changed to: initialize_failed
mysql> desc servers;
+-------------------+--------------------------------------------------------+------+-----+--------------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+--------------------------------------------------------+------+-----+--------------+-------+
| id | varchar(36) | NO | PRI | NULL | |
| name | varchar(36) | NO | | NULL | |
| gw_id | varchar(36) | NO | | NULL | |
| client_ip_pool | varchar(36) | NO | | NULL | |
| eip_id | varchar(36) | YES | | NULL | |
| proto | enum('UDP') | YES | | UDP | |
| status | enum('initializing','initialized','error') | YES | | initializing | |
| port | int(11) | NO | | NULL | |
| compress | tinyint(1) | YES | | NULL | |
| url | varchar(512) | YES | | NULL | |
| created_at | datetime | YES | | NULL | |
| updated_at | datetime | YES | | NULL | |
| deleted_at | datetime | YES | | NULL | |
| deleted | bigint(20) | YES | | NULL | |
+-------------------+--------------------------------------------------------+------+-----+--------------+-------+
2: Problem analysis
The first thing to mention is , The project on which this table depends has been running for some time , That is, there are certain data in the table , Probably status Property already has error Data exists for , After all, you suddenly change the type of a field , It must also mean that the source code of the project has also been changed here ,db In this table, this field appears error The data is understandable . So for safety , Query the... In this table status The status of the field
mysql> select * from vpn_servers where initialize_status = 'error' limit 2;
Empty set (0.00 sec)
mysql> select distinct initialize_status from vpn_servers;
+-------------------+
| initialize_status |
+-------------------+
| initialized |
| initializing |
+-------------------+
2 rows in set (0.00 sec)
It's a surprise , The value of this field in this table does not error Of , Then there are two ways to modify the enumeration value of this field .
(1) Modify table structure directly , Redefine the enum type value .
(2) Modify table structure , Add the enumeration type value initialize_failed, Then the original error Is changed to initialize_failed, Finally, redefine the enumeration value and delete error
Be careful : If there is this in this table error The data of , Then we must go the way 2, If you want to go the way 1, Then, it is better to balance the production environment with the test environment , Is it safe to delete this status have error The data of , Whether this table has foreign keys with other tables ? Cascade delete Association ?, If you want to delete , Be sure to pass api To delete , In case of db, Dirty data is left in the cache .
3: Problem solving
Method (1): Because in this example, I found that there is no information about this in this table status Field has error The data of , So the diagram is convenient , Can be modified directly
alter table servers modify `status` enum ('initializing','initialized','initialize_failed') default 'initializing';
** Method (2)** Start talking about methods 2 front , Imagine the following , If the table contains error The data of , As shown below
MariaDB [test_using]> select * from servers where status = 'error' \G
*************************** 1. row ***************************
id: 12345
name: test
gw_id: 11111
client_ip_pool: NULL
eip_id: 22222
status: error
1 row in set (0.00 sec)
At this time in status Field has error In the case of data , Think again like a method 1 That way, direct changes will occur Unpredictable problems , In this case , Direct change , Leading to the original error The data of becomes empty directly ( No null), In this case, if it is a production environment , A small screw problem may lead to a series of major accidents .
MariaDB [test_using]> alter table servers modify `status` enum ('initializing','initialized','initialize_failed') default 'initializing';
Query OK, 1 row affected, 1 warning (0.02 sec)
Records: 1 Duplicates: 0 Warnings: 1
MariaDB [test_using]> select * from servers where status = 'error' \G
Empty set (0.00 sec)
MariaDB [test_using]>
MariaDB [test_using]>
MariaDB [test_using]> select * from servers \G
*************************** 1. row ***************************
id: 12345
name: test
gw_id: 11111
client_ip_pool: NULL
eip_id: 22222
status:
1 row in set (0.00 sec)
therefore , The correct and safe way is as follows
Check the table first enum Add a new type value to the field
alter table servers modify `status` enum ('initializing','initialized','error','initialize_failed') default 'initializing';
next update This table , Replace the original type value to be replaced error The data of becomes the new type value ’initialize_failed’
update servers set status = 'initialize_failed' where status = 'error';
Last , Change the table again status Of enum Type value of , Get rid of error
alter table servers modify `status` enum ('initializing','initialized','initialize_failed') default 'initializing';
After querying data
MariaDB [test_using]> select * from servers \G
*************************** 1. row ***************************
id: 12345
name: test
gw_id: 11111
client_ip_pool: NULL
eip_id: 22222
status: initialize_failed
1 row in set (0.00 sec)
4: Postscript
In fact, there is another method that is not recommended , That is to delete the table and rebuild , Applicable to offline environment , Or when the data in the table is garbage data , It doesn't hurt to delete it .
边栏推荐
- Mmdetection train your own coco data set and common problems
- Acitivit's mental journey: activiti6.0 customizes any jump command [including rollback]
- Make money by doing sideline work. These popular we media platforms have made a lot of profits
- sqlserver2012 不允许修改表结构的问题解决 0607
- 通过快排O(n)内查找第K大元素思路
- Example: use C # Net to teach you how to develop wechat official account (19) -- use wechat payment to transfer to wechat fans' change accounts
- PaddleNLP通用信息抽取技术UIE【一】产业应用实例:信息抽取{实体关系抽取、中文分词、精准实体标。情感分析等}、文本纠错、问答系统、闲聊机器人、定制训练
- Crontab scheduled task execution
- Browser from download to rendering
- Logistic regression summary
猜你喜欢
![[flow analysis] Buu_ [an Xun cup 2019]attack](/img/6a/e8bd90e931ef8dfdca1cd9c66f0695.png)
[flow analysis] Buu_ [an Xun cup 2019]attack

Sequence traversal of binary tree

二叉搜索树

极大似然估计

Sort - quick sort

兰吉尔FFC3 电表电能量采集终端替代方案(DLMS红外光电采集器)

Pycharm进入debug模式后一直显示collecting data解决方法
![Fedformer:frequency enhanced decomposedtransformer for long term series forecasting[still learning...]](/img/58/d73cbaaeefc255816fe8cdc7165419.png)
Fedformer:frequency enhanced decomposedtransformer for long term series forecasting[still learning...]

Analysis of source code data anti leakage solution

多线程场景下使用 ArrayList 丢数据
随机推荐
深入理解 Go Modules 的 go.mod 與 go.sum
Go installation tutorial
如何定位到服务器CPU飙高的原因
Upper computer development (opening)
Analysis of 403 problems of Pro backstage sub administrator
js 强制类型转换 和 隐式类型转换 和 Unicode编码
Mysql:1062 Duplicate entry '1' for key 'PRIMARY'
sqlserver2012 不允许修改表结构的问题解决 0607
What is wave field TRX wallet development
Browser from download to rendering
What is the difference between a WiFi 6 router and a WiFi 5 router
Design and Simulation of SD card reading and writing based on FPGA Verilog
spider pi 智能视觉六足机器人自动避障 0604
paddleNLP-BUG和一些细节记录【一】
A thorough understanding of the very important ticket lock in concurrent programming -- stampedlock
Why can't Google search page infinite?
Database SQL and Gorm practice of design pattern | notes on youth training camp
深入理解 Go Modules 的 go.mod 与 go.sum
JS auto increment and auto decrement (unary operator)
Binary search tree