简体   繁体   中英

Merge two database into a third one

I have two MySQL database k_db1 and k_db2 on a single server. In k_db1, I have k_db1.table1 and k_db1.table2. In k_db2, I have k_db2.table3 and k_db2.table4.

I want to create a third database k_db3 where I copy/paste tables of others databases. It will result in k_db3.db1-table1, k_db3.db1-table2, k_db3.db2-table3, k_db3.db2-table4. I want to transfer data, indexes etc... and I don't want to delete k_db1 and k_db2 tables in the process. It must duplicate datas.

Do you know a way to do this just with SQL command?

Thanks in advance for your help.

You can try something like this:

DROP TABLE IF EXISTS k_db3.db1_table_1;

CREATE TABLE k_db3.db1_table_1 AS
SELECT * FROM db1.table_1;

Then you can recreate the indexes on the new table via ALTER TABLE statements.

Also I would avoid using - in table names.

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