Last Updated: February 25, 2016
·
540
· claylo

Breaking up MySQL batches

Know how sometimes you need to do a big database strike, and you wind up writing epic, single (or occasional) use .sql command sequences?

With MySQL, you can break those up using the SOURCE statement.

So, instead of this:

-- start by cleaning house
ALTER DATABASE foo CHARACTER SET utf8 COLLATE utf8_unicode_ci;
...

-- then drop the old stuff
DROP TABLE old_stuff, old_crud, old_hooey;

-- THEN do some other stuff
INSERT INTO ...

... and so on. We've all done this, right?

You can instead do THIS:

-- clean
SOURCE /path/to/cleanhouse.sql

-- then drop
SOURCE /path/to/drop_step_1.sql

-- then insert
SOURCE /path/to/inserts.sql

Sometimes it just makes it easier to break these monsters into modular chunks.

Source: MySQL Batch Commands