gpt4 book ai didi

mysql - 如何在现有行的另一列中添加具有默认值的MySQL表列

转载 作者:行者123 更新时间:2023-11-29 05:57:55 24 4
gpt4 key购买 nike

当前存在的表如下所示:

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

现在我想在每个现有行中添加新列 allocated_amountamount 列的默认值。

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。

最佳答案

不能作为默认列。您可以编写一个触发器并执行此操作,或者在 Mysql 5.7 中添加虚拟列。

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

或者您可以创建一个虚拟列:

alter table Table1 
add allocated_amount integer GENERATED ALWAYS AS (amount) VIRTUAL;

关于mysql - 如何在现有行的另一列中添加具有默认值的MySQL表列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47665909/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com