CRUD Operations, Done Well, in a Small Inventory App
CRUD means Create, Read, Update and Delete. This page shows what doing each one well looks like in a hypothetical inventory app for a school club's equipment cupboard. It is a model to learn from, not a real project or testimonial.
The table
CREATE TABLE items (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
quantity INTEGER NOT NULL CHECK (quantity >= 0),
location TEXT,
updated_on TEXT NOT NULL
);
Already good: a primary key, required fields marked NOT NULL, and a CHECK rule so quantity can never go below zero.
Create, done well
INSERT INTO items (name, quantity, location, updated_on)
VALUES (?, ?, ?, ?);
- Values arrive through placeholders.
- Column names are listed explicitly, so adding a column later does not break the insert.
- The database creates the
id; the app reads it back if it needs it.
Read, done well
SELECT id, name, quantity FROM items WHERE location = ? ORDER BY name;
- Only the columns the page shows.
- A WHERE clause instead of loading everything and filtering in the app.
- ORDER BY so the list is predictable.
For a single item page, the app reads by primary key: WHERE id = ?.
Update, done well
When someone takes two footballs out of the cupboard:
UPDATE items SET quantity = quantity - 2, updated_on = ? WHERE id = ?;
- The change is calculated in the database (
quantity - 2), not by reading the number, subtracting in the app and writing it back. That avoids two people's changes overwriting each other. - The WHERE clause targets one row by id.
- The CHECK rule refuses the update if it would make quantity negative, and the app shows "Not enough in stock".
- The app checks how many rows were affected; zero means the item id was wrong.
Delete, done well
- The app asks "Are you sure?" before deleting.
- It deletes by id:
DELETE FROM items WHERE id = ?; - Items that appear in a
loanstable are protected by a foreign key, so they cannot be deleted while still on loan.
The four, side by side
| Operation | Good habit | Mistake it avoids |
|---|---|---|
| Create | Explicit columns, placeholders | Broken inserts, SQL injection |
| Read | Select needed columns, filter in SQL | Slow pages, wasted memory |
| Update | Calculate in SQL, target by id | Lost updates, changing wrong rows |
| Delete | Confirm, target by id, foreign keys | Accidental mass deletes, orphan rows |
Extras that make the app pleasant
updated_onshows when each item last changed.- A simple log table records who took what, using INSERT only, so history is never overwritten.
- Backups of the database file are taken before term starts.
Compare with your own project
Pick one table in your app and check each CRUD operation against the table above. Our long read on create, read, update and delete explains each statement in more depth, and troubleshooting an UPDATE or DELETE that changed the wrong rows covers what to do when things go wrong.
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 example: the organisations, people, projects and results described on this page are hypothetical. They do not describe a real client or engagement, and no consulting services are offered here.