简体   繁体   中英

How to insert data as new column without primary keys to existing table in MySQL?

I have existing table with number of columns. And I have list of strings with the same number of rows as number of rows in existing table. I want to add new column to table and insert all the data quickly in that column. What the best way to achieve this? I can create new table with one column, insert data in that table and then merge somehow two tables or somehow insert data in first table. I don't know names of columns in first table. Also my tables doesn't have primary keys. Is this possible?

You can use an alter table for add the new column eg:

ALTER TABLE my_table
ADD my_column varchr(255);

If the table are someway related
Then use an update with join for update the added colum with the value form the table my_table_with_values eg:

update  my_table 
join my_table_with_values on my_table.id = my_table_with_values.key_id
set my_column  = my_table_with_values.value
;

Otherwise if the two table have not relation between them ..this mean that you have not a proper value to related each others so the relation is only row num based in this case you can server side select from the two tables and loop over the first table

and update each row of thw first table with the corresponding (index based) value of the second

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