MariaDB INSERT SELECT: Copy Query Results and Check Before Commit
Use INSERT INTO target (columns) SELECT … to insert query results into an existing MariaDB table. Values match target columns by position. The statement copies matching rows and leaves the source rows in place.
Map the columns and preview the rows
For this INSERT … SELECT exercise, choose a separate practice database and run the examples in the same connection. You need permission to create tables, insert rows, and query the data. Use a fresh practice database if the table names already exist.
CREATE TABLE select_source_demo (
id INT PRIMARY KEY,
product_name VARCHAR(50) NOT NULL,
stock INT NOT NULL,
status CHAR(1) NOT NULL
) ENGINE=InnoDB;
CREATE TABLE select_target_demo (
id INT AUTO_INCREMENT PRIMARY KEY,
source_id INT NOT NULL UNIQUE,
item_name VARCHAR(50) NOT NULL,
quantity INT NOT NULL,
note VARCHAR(30) NOT NULL DEFAULT 'copied'
) ENGINE=InnoDB;
INSERT INTO select_source_demo VALUES
(1, 'Notebook', 5, 'S'),
(2, 'Pencil', 10, 'S'),
(3, 'Ruler', 7, 'D');
SELECT id, product_name, stock
FROM select_source_demo
WHERE status = 'S' ORDER BY id;
The preview returns (1, Notebook, 5) and (2, Pencil, 10). Source id, product_name, and stock map to target source_id, item_name, and quantity. The target generates its own id and fills note with its default. Explicit column lists make that mapping clear. Check the target types and constraints with SHOW CREATE TABLE.
Inspect the insert and roll it back
Use START TRANSACTION to inspect the inserted rows in the same connection and undo them before commit. Keep implicitly committing statements such as CREATE, ALTER, and TRUNCATE outside this block.
START TRANSACTION;
INSERT INTO select_target_demo (source_id, item_name, quantity)
SELECT id, product_name, stock
FROM select_source_demo
WHERE status = 'S';
SELECT ROW_COUNT() AS inserted_rows;
SELECT source_id, item_name, quantity, note
FROM select_target_demo
ORDER BY source_id;
ROLLBACK;
SELECT COUNT(*) AS rows_after_rollback FROM select_target_demo;
SELECT COUNT(*) AS original_rows FROM select_source_demo;
Run ROW_COUNT() immediately after the insert. The example reports 2 and displays (1, Notebook, 5, copied) and (2, Pencil, 10, copied). After ROLLBACK, the target count is 0 and the source still has 3 rows. Auto-increment values may have gaps, so verify the actual rows and values.
Commit a verified copy
To keep the verified result, run the insert again in a new transaction. Review the rows before running the final line of the exercise. For a real copy, decide between COMMIT and ROLLBACK based on the actual rows and keys.
START TRANSACTION;
INSERT INTO select_target_demo (source_id, item_name, quantity)
SELECT id, product_name, stock
FROM select_source_demo
WHERE status = 'S';
SELECT ROW_COUNT() AS inserted_rows;
SELECT source_id, item_name, quantity, note
FROM select_target_demo
ORDER BY source_id;
COMMIT;
The source can change between the preview and the insert. Use a stable batch key or staged data when you need a fixed set. Large writes can affect locks, logs, and replication. Check the batch size and execution plan in your environment. To copy between databases, qualify table names as database_name.table_name and obtain permissions on both sides.
Keep existing rows on a rerun
Running the same insert again hits the target’s UNIQUE(source_id) constraint. To preserve rows already copied, use NOT EXISTS to select only IDs that are absent from the target.
INSERT INTO select_target_demo (source_id, item_name, quantity)
SELECT s.id, s.product_name, s.stock
FROM select_source_demo AS s
WHERE s.status = 'S'
AND NOT EXISTS (
SELECT 1
FROM select_target_demo AS t
WHERE t.source_id = s.id
);
SELECT ROW_COUNT() AS newly_inserted_rows;
After the earlier commit, newly_inserted_rows is 0. Concurrent runs can still choose the same new key. Keep the uniqueness constraint and decide whether to abort or check the target before retrying. Use ON DUPLICATE KEY UPDATE only if updating a matching row is the intended behavior. INSERT IGNORE can convert some other data errors into warnings or adjusted values as well. Preserve schema and related objects separately if you also need the source definition.




