Do and Don't: Create, Read, Update and Delete

Create, read, update and delete are the four basic things apps do with data. Each has a few habits that keep your data safe and your code clear. Here they are as simple do-and-don't pairs, with the reason behind each.

Create (INSERT)

Do: name the columns you are filling.

INSERT INTO pets (name, age) VALUES ('Rex', 4);

Don't: rely on column order with INSERT INTO pets VALUES (...).

Why: if a column is added later, positional inserts break or put values in the wrong place.

Do: let the database create ids and timestamps, using auto-numbered keys and defaults.

Don't: compute the "next id" yourself with MAX(id) + 1.

Why: two users at once can get the same number.

Read (SELECT)

Do: select the columns you need in app code, and add ORDER BY when order matters.

Don't: assume rows come back in insertion order.

Why: without ORDER BY, the database may return rows in any order, and it can change.

Do: use LIMIT when showing lists.

Don't: load an entire large table to show the first ten rows.

Why: it wastes time and memory.

Update (UPDATE)

Do: always include a WHERE clause, and check it first with a SELECT.

SELECT * FROM pets WHERE id = 3;
UPDATE pets SET age = 5 WHERE id = 3;

Don't: run UPDATE pets SET age = 5; by accident.

Why: without WHERE, every row changes.

Do: update in a single statement when you can, such as SET stock = stock - 1.

Don't: read a value into your program, change it, and write it back when two users might do so at once.

Why: one user's change can overwrite the other's.

Delete (DELETE)

Do: use WHERE with a primary key, and consider a transaction while learning.

BEGIN;
DELETE FROM pets WHERE id = 3;
SELECT COUNT(*) FROM pets;
COMMIT;

Don't: delete by a vague condition such as a name that might match several rows.

Why: keys identify exactly one row; names do not.

Do: think about related rows. What should happen to a pet's vet visits?

Don't: leave orphaned rows pointing to things that no longer exist.

Why: foreign keys with a chosen delete rule keep data consistent; see the complete read on keys.

For all four

DoWhy
Use placeholders for values from usersPrevents SQL injection
Group related changes in a transactionAll or nothing
Keep scripts for test dataRebuild after mistakes
Check results, such as rows affectedCatch silent failures

Where next

The long read on create, read, update and delete explains each operation more fully, and the CRUD walkthrough with a tiny to-do list builds a small working example.

A free learning page for beginners. Practise on a test database, never on real customer data, and check the official documentation for your database version.