Migration to change default value for a field and change all existing record's value to new default value only if it has old default value.

后端 未结 2 593
死守一世寂寞
死守一世寂寞 2021-02-01 15:51

I need to change the default value of a field from 0 to 3, but the catch is i have thousands of records already and want those records to change the value to 3 from 0 only if th

2条回答
  •  说谎
    说谎 (楼主)
    2021-02-01 16:44

    ALTER TABLE your_table MODIFY your_column tinyint(1) NOT NULL DEFAULT 3;
    UPDATE your_table SET your_column=3 WHERE your_column=0;
    
    1. assuming your column is tinyint(1), replace your self if not the same
    2. NOT NULL is assuming you always force the column to be NOT NULl

提交回复
热议问题