Writing Data: INSERT, UPDATE, DELETE

Site Admin · 11 Sep 2026 · 6 views

Writing Data: INSERT, UPDATE, DELETE

Adding Rows with INSERT

Queries that read data are half the story. To store new records you use INSERT, to modify them UPDATE, and to remove them DELETE. These are the Data Manipulation Language commands, and they change rows, not the table structure.

INSERT INTO users (username, email)
VALUES ('jdoe', 'jdoe@example.com');

You list the columns in parentheses and supply matching values. The order in VALUES must match the column list. Columns with NOT NULL constraints must be included unless they have a default, so this statement omits id and created_at because MySQL fills them automatically.

You can insert several rows at once, which is faster than one statement per row:

INSERT INTO users (username, email) VALUES
('mfox', 'mfox@example.com'),
('sku', 'sku@example.com');

Updating Rows with UPDATE

UPDATE changes matching rows. The SET clause lists the new values, and the WHERE clause selects which rows to change. Leaving out WHERE updates every row, so compose it carefully.

UPDATE users
SET email = 'jdoe@newexample.com'
WHERE username = 'jdoe';

The WHERE clause filters exactly like it does in SELECT. A common safety habit is to run a SELECT with the same WHERE first to confirm which rows would be affected.

Deleting Rows with DELETE

DELETE removes rows that match the WHERE clause. Again, without WHERE it empties the entire table. If you want to keep the table structure but have no rows, TRUNCATE TABLE is faster, though it cannot be rolled back as easily.

DELETE FROM users WHERE id = 7;

Handling Conflicts

Inserting a row that violates a unique key raises an error. MySQL offers ON DUPLICATE KEY UPDATE to turn that conflict into an update, which is exactly the pattern this seed script uses to stay idempotent.

Key Points

  • INSERT adds rows; list columns and their values together.
  • UPDATE changes rows selected by WHERE; never omit WHERE carelessly.
  • DELETE removes rows selected by WHERE.
  • Multi-row INSERT is more efficient than many single inserts.
  • ON DUPLICATE KEY UPDATE resolves unique-key conflicts gracefully.
Share this post:

Comments (0)

Please login or register to comment.