Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge Multiple .sql Table Dump Files Into A Single File

Tags:

shell

mysql

Suppose I have database A and Table b. Given multiple .sql files b1,b2,...,bn each of which corresponds to a mutually exclusive table dump of b how would I go about combining all files b1,b2,...,bn into a single .sql table file? Or how would I combine the import of the individual files into a single table?

like image 318
arete Avatar asked Jul 10 '13 23:07

arete


People also ask

How do I create a .SQL file to dump?

Click Export. In the File format section, click SQL to create a SQL dump file. In the Data to export section, click One or more databases in this instance to export specific databases. Use the drop-down menu to select the databases you want to export from.

How do I import multiple SQL files into MySQL workbench?

Import Multiple SQL files to Database by using Data Import/Restore in MySQL Workbench is great feature when you need to import multiple SQL files into MySQL Server. If you have created multiple files by using Data Export feature in MySQL Server, SQL Dump file project can be imported by using MySQL Import/Restore.

How do I run multiple SQL files in SSMS?

From there, CTRL+V into an SSMS window. It works with multiple files too. Select two or more files in Explorer, right-click any of the highlighted files, and select "Copy as path". Repeat steps in SSMS.


1 Answers

There are no special tools to do this. You can simply concatenate the files:

$ cat b1.sql b2.sql b3.sql > b_all.sql

Except that the typical content of these .sql files is a DROP TABLE, then a CREATE TABLE, then a lot of INSERT statements. If each of the individual dump files is formatted like that, then if you restore them in sequence, each will DROP TABLE and erase the data imported by the preceding file.

You can create a dump file without the DROP/CREATE statements:

$ mysqldump --no-create-info <database> <table> ...

But if you have the dump files already (can't re-dump them), and you want to get rid of the DROP/CREATE statements in all but the first file:

$ ( cat b1.sql ; cat b2.sql b3.sql | sed -e '/^DROP TABLE/,/^-- Dumping data/d' ) > b_all.sql
like image 192
Bill Karwin Avatar answered Oct 27 '22 16:10

Bill Karwin