brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: a mariadb-dump with INSERT IGNORE and no CREATE merges rows into a live database table

Database
~/tips/tip-database-mariadb-dump-insert-ignore-merge

mariadb-dump --no-create-info --insert-ignore shop orders > orders-rows.sql

Dumps only rows as INSERT IGNORE, so loading them adds missing rows and keeps the rest.

Sometimes you don’t want to replace a table, only put back the rows that went missing. A normal dump starts with DROP TABLE, which is the opposite of that.

mariadb-dump --no-create-info --insert-ignore shop orders > orders-rows.sql
mariadb shop < orders-rows.sql

--no-create-info leaves out the DROP TABLE and CREATE TABLE statements, so the file holds only data. --insert-ignore writes INSERT IGNORE INTO instead of INSERT INTO. When a row with the same primary key already exists on the target, the server skips it with a warning and goes on with the next one. Rows that are missing get added.

I find this handy when somebody deleted a few rows by accident and the rest of the table has new data since the backup. Load the dump from before the delete and only the gone rows come back.

Know what IGNORE throws away, though. It also silences other errors, such as values that do not fit a column, and it never updates an existing row. If a row was edited after the backup, you keep the edited version. When you want the backup’s version to win, --replace writes REPLACE INTO instead, which deletes and reinserts the matching rows.

Further reading: mariadb-dump documentation

More Database tips mariadb -e 'CREATE DATABASE IF NOT EXISTS blog' SELECT table_schema, table_name, CHECKSUM TABLE shop.orders; services: services: All Database tips (53) All topics

Link