The current existing table looks like this:
id number amount
1 123 30000.00
2 123 15000.00
3 321 45000.00
... ... ...
1000 567 75000.00
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
It is possible? Im using MySQL Workbench GUI.
Syntax. The syntax to add a column in a table in MySQL (using the ALTER TABLE statement) is: ALTER TABLE table_name ADD new_column_name column_definition [ FIRST | AFTER column_name ]; table_name.
Try this: ALTER TABLE table1 ADD COLUMN foo INT DEFAULT 0; From the documentation that you linked to: ALTER [ONLINE | OFFLINE] [IGNORE] TABLE tbl_name alter_specification [, alter_specification] ...
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With