简体   繁体   中英

Alter table add column and set default value using the table records in MySQL

Is there a way to add a new column and set its default value comming from the table in MySQL?

for example i have this table "sampleTable"

ID|    column_text|    content
1 |           bla1|     sadas|
2 |           bla2|     sadas|
3 |           bla3|     sadas|
4 |           bla4|     sadas|

then i want to execute a command that add a column and use the default value column_text.

ALTER TABLE sampleTableADD column_text_duplicate longtext DEFAULT *comming from column_text;

So alter the table and then run a simple update query.

ALTER TABLE sampleTableADD  column_text_duplicate longtext;
UPDATE sampleTableADD SET column_text_duplicate = column_text;

But this seems a little unnecessary, why would you want 2 columns containing the same data. I assume you must then be amending the copy column in at least some of the rows!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM