如何使用现有行的另一列的默认值添加MySQL表列 [英] How to add MySQL table column with a default value of another column of the existing row
本文介绍了如何使用现有行的另一列的默认值添加MySQL表列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当前现有表如下:
id number amount
1 123 30000.00
2 123 15000.00
3 321 45000.00
... ... ...
1000 567 75000.00
现在,我想在每个现有行中使用amount
列的默认值添加新列allocated_amount
.
Now i would like add new column allocated_amount
with the default value of the amount
column in every existing row.
id number amount allocated_amount
1 123 30000.00 30000.00
2 123 15000.00 15000.00
3 321 45000.00 45000.00
... ... ... ...
1000 567 75000.00 75000.00
有可能吗?我正在使用MySQL Workbench GUI.
It is possible? Im using MySQL Workbench GUI.
推荐答案
不可能作为默认列.您可以编写触发器并执行该操作,也可以在Mysql 5.7中添加虚拟列.
It is not possible as default column. You can write a trigger and do that or add virtual column in Mysql 5.7.
或 修改表Tab1的方法添加allocated_amount int; -添加栏 更新Tab1设置的allocated_amount =数量; -设置值
OR alter table Tab1 add allocated_amount int; -- Add column update Tab1 set allocated_amount= amount; -- Set the value
这篇关于如何使用现有行的另一列的默认值添加MySQL表列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文