How to add MySQL table column with a default value of another column of the existing row

后端 未结 2 1848
渐次进展
渐次进展 2021-01-19 06:21

The current existing table looks like this:

id    number    amount    
1     123       30000.00
2     123       15000.00
3     321       45000.00
...   ...           


        
相关标签:
2条回答
  • 2021-01-19 07:21

    Virtual Column:

    alter table Table1 
    add allocated_amount integer GENERATED ALWAYS AS (amount) VIRTUAL;
    
    0 讨论(0)
  • 2021-01-19 07:23

    It is not possible as default column. You can write a trigger and do that or add virtual column in Mysql 5.7.

    OR

    alter table Tab1 add allocated_amount int;  -- Add column
    update Tab1 set allocated_amount= amount;   -- Set the value
    

    Or you could create a Virtual Column:

    alter table Table1 
    add allocated_amount integer GENERATED ALWAYS AS (amount) VIRTUAL;
    
    0 讨论(0)
提交回复
热议问题