MariaDB INSERT, UPDATE, and DELETE: add, change, and remove rows
Use INSERT to add rows, UPDATE to change values in existing rows, and DELETE to remove whole rows in MariaDB. With no WHERE clause, UPDATE and DELETE target every row in the table. The exercise uses primary keys to select targets and checks both row counts and the data that remains.
Create a practice table
Use a SQL client in the practice database learning. You need CREATE, SELECT, INSERT, UPDATE, and DELETE permissions. If dml_basics_demo already exists, use an empty practice database or rename the table throughout. Check the InnoDB engine and column definitions with SHOW CREATE TABLE, then work through the examples in one connection. Start with a fresh connection with autocommit enabled; the transaction exercise starts its own transaction.
USE learning;
CREATE TABLE dml_basics_demo (
id INT PRIMARY KEY,
name VARCHAR(40) NOT NULL,
quantity INT NOT NULL DEFAULT 0,
note VARCHAR(100) NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
SELECT DATABASE() AS current_database;
SHOW CREATE TABLE dml_basics_demo;
| Statement | Action | What to check |
|---|---|---|
| INSERT INTO | Add rows | Column list and order of values |
| UPDATE … SET | Change existing values | WHERE and updated columns |
| DELETE FROM | Remove rows | WHERE and foreign-key rules |
Add rows with INSERT
For INSERT, match the order of the columns to the order of the VALUES. Put text in single quotes. To insert several rows in one statement, separate the parenthesized groups of values with commas. MariaDB Docs · INSERT
INSERT INTO dml_basics_demo (id, name, quantity, note)
VALUES (1, 'Pencil', 10, 'Shelf A');
SELECT ROW_COUNT() AS inserted_rows;
INSERT INTO dml_basics_demo (id, name)
VALUES (2, 'Notebook');
SELECT ROW_COUNT() AS inserted_rows;
INSERT INTO dml_basics_demo (id, name, quantity)
VALUES (3, 'Eraser', 5), (4, 'Ruler', 7);
SELECT ROW_COUNT() AS inserted_rows;
SELECT id, name, quantity, note
FROM dml_basics_demo ORDER BY id;
The three inserts report 1, 1, and 2 rows. For ID 2, quantity takes its default of 0 and note is NULL. NULL represents an absent value and is distinct from an empty string. Reusing a primary key produces a duplicate-key error. If you need to edit an existing row, define which row to UPDATE.
Check UPDATE targets and changed rows
In UPDATE, SET specifies the columns and values and WHERE selects the rows. Preview the key and values with a SELECT using the same condition. The first update changes ID 1 from 10 to 12 and edits its note. The second adds 2 to ID 3’s quantity of 5. MariaDB Docs · UPDATE
SELECT id, name, quantity, note
FROM dml_basics_demo WHERE id = 1;
UPDATE dml_basics_demo
SET quantity = 12, note = 'Stock checked'
WHERE id = 1;
SELECT ROW_COUNT() AS changed_rows;
UPDATE dml_basics_demo
SET quantity = quantity + 2
WHERE id = 3;
SELECT ROW_COUNT() AS changed_rows;
SELECT id, name, quantity, note
FROM dml_basics_demo ORDER BY id;
UPDATE dml_basics_demo SET quantity = 12 WHERE id = 1;
SELECT ROW_COUNT() AS unchanged_rows;
UPDATE dml_basics_demo SET quantity = 12 WHERE id = 999;
SELECT ROW_COUNT() AS missing_rows;
SELECT id, quantity FROM dml_basics_demo WHERE id IN (1, 999) ORDER BY id;
Call ROW_COUNT() immediately after each change. The example sets ID 1 to 12 again and then targets the missing ID 999. With default connection settings, both report 0. The final SELECT shows that ID 1 exists. A client configured to count matched rows can report 1 for the first of those two updates. MariaDB Docs · ROW_COUNT()
Delete a specific row
DELETE removes a whole row. To clear only the note, identify the row and use UPDATE with note = NULL. On tables with foreign keys, deletion may be rejected or may change related rows according to the constraint. MariaDB Docs · DELETE
SELECT id, name, quantity
FROM dml_basics_demo WHERE id = 2;
DELETE FROM dml_basics_demo WHERE id = 2;
SELECT ROW_COUNT() AS deleted_rows;
SELECT id, name, quantity
FROM dml_basics_demo ORDER BY id;
The example deletes only ID 2. The count is 1 and IDs 1, 3, and 4 remain. The table definition survives. DROP TABLE removes the definition too, while TRUNCATE TABLE is a separate statement for emptying an entire table.
Inspect and roll back a transaction
Run the block from START TRANSACTION through the query after ROLLBACK in one connection. The first query shows 99 for ID 1 and no ID 4. After ROLLBACK, IDs 1, 3, and 4 have quantities of 12, 7, and 7. When you intend to keep a real change, review it before choosing COMMIT. MariaDB Docs · START TRANSACTION
START TRANSACTION;
UPDATE dml_basics_demo SET quantity = 99 WHERE id = 1;
DELETE FROM dml_basics_demo WHERE id = 4;
SELECT id, quantity FROM dml_basics_demo ORDER BY id;
ROLLBACK;
SELECT id, quantity FROM dml_basics_demo ORDER BY id;
The table must use a transactional engine such as InnoDB. ROLLBACK cannot undo a change that autocommit already finalized. Keep statements that cause implicit commits, including CREATE TABLE, ALTER TABLE, and TRUNCATE TABLE, outside a transaction for data changes. MariaDB Docs · Implicit commit
What to check before changing a real table
Verify the database, key condition, and expected row count. A preview does not guarantee the same result after concurrent writes; a transaction or application-level concurrency control may be needed. If Safe Updates rejects a statement, review the key condition first. When building application queries, bind external values as parameters in prepared statements.




