VillageSQL is a drop-in replacement for MySQL with extensions.
All examples on this page work on VillageSQL. Install Now →
INSERT can carry as many rows as you like. Separate each row’s brackets
with a comma. That is less typing than 1 statement per row, and it is 1 trip to
the server rather than many.
These examples use the sakila_practice database from
INSERT. Run the setup block there again before you start,
then open the client with mysql -u root -p sakila_practice.
How a multi-row INSERT works
TABLE_NAME with the table, COLUMN_LIST with the columns you are
supplying, and each of FIRST_ROW_VALUES and SECOND_ROW_VALUES with 1 value
for each of those columns.
The column list is written once and governs every row. Each set of brackets
supplies values in that order.
Examples
1) 3 rows in 1 statement
VALUES list and any
INSERT SELECT. Records counts the rows you sent,
Duplicates the rows that collided with a row already stored, and Warnings
anything the server had to adjust. All 3 numbers agreeing with what you sent is the
sign that nothing was quietly changed.
One bad row throws the whole statement away
The statement below carries 3 rows. The middle one reuses Mary Smith’s address, which theUNIQUE index on email refuses.
INSERT IGNORE turns the error into a warning
INSERT IGNORE keeps the rows it can and skips the rest.
Records: 2 Duplicates: 1 reports exactly what happened: 2 rows offered, 1
refused. SHOW WARNINGS then names it, and the same ERROR 1062 text arrives
as a warning rather than as a failure.
IGNORE when a duplicate genuinely means “already have it”. It is a blunt
instrument otherwise, because it downgrades other errors too, including a value
too long for its column and a row with no value for a required column. Run
SHOW WARNINGS after every INSERT IGNORE or you will not know what it
dropped. When the right answer is to update the row already stored, use
upsert instead.
Summary
- Separate each row’s brackets with a comma, and write the column list once.
- Read
Records,DuplicatesandWarningsto confirm nothing was adjusted. - Expect 1 rejected row to abort the entire statement and write nothing.
- Use
INSERT IGNOREto keep the good rows, and readSHOW WARNINGSafterwards. - Prefer 1 multi-row statement over many single-row ones.
See also
- INSERT — the single-row form and the errors it raises
- INSERT SELECT — rows taken from a query
- Upsert — update the existing row instead of skipping it
- Bulk inserts in MySQL — batch sizes, LOAD DATA and the settings that matter

