https://github.com/harelba/recreate-inserts
Generate Mysql INSERTs from SELECT output
https://github.com/harelba/recreate-inserts
Last synced: about 1 month ago
JSON representation
Generate Mysql INSERTs from SELECT output
- Host: GitHub
- URL: https://github.com/harelba/recreate-inserts
- Owner: harelba
- Created: 2013-01-06T20:54:44.000Z (over 12 years ago)
- Default Branch: master
- Last Pushed: 2013-01-07T12:27:10.000Z (over 12 years ago)
- Last Synced: 2025-03-24T23:48:39.553Z (6 months ago)
- Language: Python
- Size: 113 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Purpose
This utility gets the output of a Mysql SELECT query and generates a set of matching INSERT commands. It can be used for copying selected parts into another table with identical structure, or to copy parts of a table between different database instances.
It is important to note that no DB connection of any kind is performed. The utility just outputs the INSERT command text lines. These commands can be fed to a mysql client for execution.
The utility supports batching of INSERTs, for better efficiency.
## Difference from other tools
`mysqldump` - The two major differences are that with recreate-inserts it is possible to reuse existing text data, and being able to use a real SELECT query instead of passing parameters to mysqldump.`INSERT...SELECT` - Insert-Select commands are very powerful, but they require a tight coupling between the source and target. Both have to be on the same DB instance. This tool allows copying selected data between separate database instances or over the wire.
# Example
echo "select * from mydb.my_table limit 100" | ./recreate-inserts -b 20 mydb.my_other_tableThis example will take a maximum of 100 rows from mydb.mytable and generate INSERT commands for table my_other_table. Each insert command will contain a maximum of 20 rows.
# Usage
Usage:
recreate-inserts [options]Options:
-h, --help show this help message and exit
-b BATCH, --batch=BATCH
Batch size - how many rows to put in each insert.
-d DELIMITER, --delimiter=DELIMITER
Delimiter
-c COLUMN_LIST, --column-list=COLUMN_LIST
User-set column list (instead of reading the list from
the first row)
-r, --relaxed Fill with nulls missing fields or trim extra fields## Usage Details
Batch size is controlled by the -b option.
The first row of input is expected to have the table's column names (as in mysql's output). The user can provide his own set of column names using the -c option.
The utility uses the default \t delimiter of mysql. If you use other delimiters, use the -d option.
If needed, turn on relaxed mode by using -r. It will cause missing fields to be filled with nulls and extra fields to be ignored.
NOTE: Please note that there is no way to determine the table name from the output, so it must be provided as an argument
to the script. Look at it as a feature that allows a simple way to copy rows between tables with identical structure.