The Long Read: Create, Read, Update and Delete

CRUD stands for Create, Read, Update and Delete: the four things almost every app does with its data. Learn these four operations well and you can build a surprising amount. This long read takes each in turn.

Four verbs, four statements

LetterMeaningSQL statement
CCreate a new rowINSERT
RRead rowsSELECT
UUpdate existing rowsUPDATE
DDelete rowsDELETE

We will use one small table throughout:

CREATE TABLE notes (id INTEGER PRIMARY KEY, title TEXT NOT NULL, body TEXT, pinned INTEGER DEFAULT 0);

Create: INSERT

INSERT INTO notes (title, body) VALUES ('Shopping', 'Eggs, bread');

You name the columns you are filling, then give the values in the same order. Columns you leave out get their default (pinned becomes 0) or become empty. The id is filled in automatically because it is the primary key.

A good habit: always list the column names. If someone adds a column to the table later, your INSERT still works.

Read: SELECT

SELECT id, title FROM notes WHERE pinned = 1 ORDER BY title;

Reading is the most common operation by far. Most screens in an app, such as lists, detail pages and search results, are a SELECT underneath.

Update: UPDATE

UPDATE notes SET pinned = 1 WHERE id = 3;

SET says what changes; WHERE says which rows. This is the statement where beginners have the most accidents, because forgetting WHERE changes every row:

UPDATE notes SET pinned = 1;   -- pins every note!

Before running an UPDATE, run a SELECT with the same WHERE to see which rows it will touch.

Delete: DELETE

DELETE FROM notes WHERE id = 3;

Exactly the same warning applies. DELETE FROM notes; with no WHERE empties the table. Many apps avoid real deletes altogether and use a "soft delete": a column such as deleted_at that marks a row as removed while keeping it for recovery.

How apps map CRUD to screens

A to-do app is a nice example:

Web APIs often mirror this too: creating is usually an HTTP POST, reading a GET, updating a PUT or PATCH, and deleting a DELETE. The match is not perfect, but it is a useful mental map.

Safety rules for all four

  1. Use placeholders for every value that comes from a user. WHERE id = ? with the value passed separately, never glued into the string.
  2. Check WHERE twice on UPDATE and DELETE.
  3. Use transactions when several changes must succeed or fail together, such as moving money between two accounts.
  4. Practise on test data. Keep a copy, or use SQLite and simply recreate the file.

Transactions in one paragraph

A transaction groups statements so they all happen or none do:

BEGIN;
UPDATE accounts SET balance = balance - 10 WHERE id = 1;
UPDATE accounts SET balance = balance + 10 WHERE id = 2;
COMMIT;

If something fails before COMMIT, ROLLBACK undoes everything since BEGIN.

Where to practise

Our chaptered walkthrough of CRUD with a tiny to-do list builds all four operations into a working script, one chapter at a time.

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.

Illustrative outline only: no live session, recording, registration or certificate is offered on this site. Any speakers, panels or schedules mentioned are hypothetical.