patternsqlMinor
Merge multiple tables in a database
Viewed 0 times
databasetablesmultiplemerge
Problem
I have a MySQL database with table names like
Each tables have
This is how all my 2000 table's structure look like
Now I would like to create a new database with name
Can someone tell me how to import all my tables in
My problem here is all my tables has
I would like to have unique ids from
I'm using ubuntu. So if someone suggest me a terminal command, that would be awesome.
1,2,3... upto 1000Each tables have
2000 rows and has the same structure.This is how all my 2000 table's structure look like
id,url,title,contentid has type int, primary key,auto increment.Now I would like to create a new database with name
merged. Also a table with name merged_table.Can someone tell me how to import all my tables in
merged_table?My problem here is all my tables has
id from 1 to 2000. So I have no idea how to import them.I would like to have unique ids from
1 to 2000000 in my new merged_tableI'm using ubuntu. So if someone suggest me a terminal command, that would be awesome.
Solution
Create the first table, then load each table into the merged_table:
Here is a way to script it (if the database is mydb)
Look at the output of the file
If it looks like the output I gave at the top of my answer, then run
Give it a Try !!!
CREATE TABLE merged_table LIKE `1`;
INSERT INTO merged_table (url,title,content) SELECT url,title,content FROM `1`;
INSERT INTO merged_table (url,title,content) SELECT url,title,content FROM `2`;
INSERT INTO merged_table (url,title,content) SELECT url,title,content FROM `3`;
...
...
;
INSERT INTO merged_table (url,title,content) SELECT url,title,content FROM `999`;
INSERT INTO merged_table (url,title,content) SELECT url,title,content FROM `1000`;Here is a way to script it (if the database is mydb)
DB="mydb"
MYSQL_CONN="-uroot -ppassword"
echo "CREATE TABLE merged_table LIKE \`1\`;" > MergeData.sql
SQL="SELECT CONCAT('INSERT INTO merged_table (url,title,content)"
SQL="${SQL} SELECT url,title,content FROM \`998\`')"
SQL="${SQL} FROM information_schema.tables"
SQL="${SQL} WHERE table_name REGEXP "$[0-9]"
mysql ${MYSQL_CONN} -ANe"${SQL}" >> MergeData.sqlLook at the output of the file
less MergeData.sqlIf it looks like the output I gave at the top of my answer, then run
mysql ${MYSQL_CONN} -D${DB} < MergeData.sqlGive it a Try !!!
Code Snippets
CREATE TABLE merged_table LIKE `1`;
INSERT INTO merged_table (url,title,content) SELECT url,title,content FROM `1`;
INSERT INTO merged_table (url,title,content) SELECT url,title,content FROM `2`;
INSERT INTO merged_table (url,title,content) SELECT url,title,content FROM `3`;
...
...
;
INSERT INTO merged_table (url,title,content) SELECT url,title,content FROM `999`;
INSERT INTO merged_table (url,title,content) SELECT url,title,content FROM `1000`;DB="mydb"
MYSQL_CONN="-uroot -ppassword"
echo "CREATE TABLE merged_table LIKE \`1\`;" > MergeData.sql
SQL="SELECT CONCAT('INSERT INTO merged_table (url,title,content)"
SQL="${SQL} SELECT url,title,content FROM \`998\`')"
SQL="${SQL} FROM information_schema.tables"
SQL="${SQL} WHERE table_name REGEXP "$[0-9]"
mysql ${MYSQL_CONN} -ANe"${SQL}" >> MergeData.sqlless MergeData.sqlmysql ${MYSQL_CONN} -D${DB} < MergeData.sqlContext
StackExchange Database Administrators Q#48299, answer score: 3
Revisions (0)
No revisions yet.