What is CRUD — Simple Explanation with Examples
CRUD (Create, Read, Update, Delete) is the four basic operations for persistent data storage, mapping directly to SQL statements and HTTP methods in RESTful APIs.
In this guide, you’ll learn what each CRUD operation does, how they map to databases and APIs, and how to implement them in Python and SQL. CRUD is the foundation of virtually every data-driven application.
Why CRUD Exists — The Problem It Solves
Every application that stores data needs four fundamental capabilities:
- Create — add new data (register a user, create a post)
- Read — retrieve existing data (view profile, list products)
- Update — modify existing data (change email, edit a comment)
- Delete — remove data (cancel account, delete a file)
These four operations cover 95% of data interactions in any application. CRUD provides a universal mental model that works across databases, APIs, and user interfaces.
Why “CRUD” Instead of Something Else?
The term was popularized in the 1980s (possibly earlier) as a way to describe the essential operations of persistent storage. Every data system — from SQL databases to NoSQL stores to file systems — implements these four operations in some form.
The Analogy — Library Book Management
Think of a library’s book management system:
| CRUD | Library Operation | Example |
|---|---|---|
| Create | Add a new book to the catalog | Register a new ISBN entry |
| Read | Look up a book by title or author | Search the catalog |
| Update | Change a book’s location or status | Mark as “checked out” |
| Delete | Remove a withdrawn book from catalog | Delete damaged books |
The library clerk (your application) needs all four operations to manage the collection. Without any one of them, the system is incomplete.
CRUD → SQL Mapping
| Operation | SQL Command | Description |
|---|---|---|
| Create | INSERT | Add a new row to a table |
| Read | SELECT | Retrieve rows from a table |
| Update | UPDATE | Modify existing rows |
| Delete | DELETE | Remove rows from a table |
SQL Examples
-- CREATE: Add a new user
INSERT INTO users (name, email, age)
VALUES ('Alice', 'alice@example.com', 30);
-- READ: Retrieve users
SELECT * FROM users WHERE age > 25;
-- UPDATE: Change Alice's email
UPDATE users
SET email = 'alice@newdomain.com'
WHERE name = 'Alice';
-- DELETE: Remove users under 18
DELETE FROM users WHERE age < 18;CRUD → HTTP Method Mapping
| Operation | HTTP Method | REST URI | Status Code |
|---|---|---|---|
| Create | POST | /users | 201 Created |
| Read | GET | /users/{id} | 200 OK |
| Update | PUT or PATCH | /users/{id} | 200 OK |
| Delete | DELETE | /users/{id} | 204 No Content |
REST API Examples
# CREATE
POST /api/users
Content-Type: application/json
{"name": "Bob", "email": "bob@example.com"}
→ 201 Created
→ Location: /api/users/42
# READ
GET /api/users/42
→ 200 OK
→ {"id": 42, "name": "Bob", "email": "bob@example.com"}
# UPDATE (full replacement)
PUT /api/users/42
Content-Type: application/json
{"name": "Robert", "email": "robert@example.com"}
→ 200 OK
# UPDATE (partial)
PATCH /api/users/42
Content-Type: application/json
{"email": "newemail@example.com"}
→ 200 OK
# DELETE
DELETE /api/users/42
→ 204 No ContentCRUD Example in Python with SQLite
import sqlite3
conn = sqlite3.connect('library.db')
cursor = conn.cursor()
# Create table
cursor.execute('''
CREATE TABLE IF NOT EXISTS books (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL,
author TEXT NOT NULL,
year INTEGER
)
''')
# --- CREATE ---
cursor.execute(
'INSERT INTO books (title, author, year) VALUES (?, ?, ?)',
('1984', 'George Orwell', 1949)
)
print(f"Created book with id: {cursor.lastrowid}")
# --- READ ---
cursor.execute('SELECT * FROM books WHERE author = ?', ('George Orwell',))
books = cursor.fetchall()
for book in books:
print(f"Read: {book}")
# --- UPDATE ---
cursor.execute(
'UPDATE books SET year = ? WHERE title = ?',
(1950, '1984')
)
print(f"Updated {cursor.rowcount} row(s)")
# --- DELETE ---
cursor.execute('DELETE FROM books WHERE year < ?', (1900,))
print(f"Deleted {cursor.rowcount} row(s)")
conn.commit()
conn.close()Expected output:
Created book with id: 1
Read: (1, '1984', 'George Orwell', 1949)
Updated 1 row(s)
Deleted 0 row(s)REST API CRUD with Flask
from flask import Flask, request, jsonify
app = Flask(__name__)
books = [
{"id": 1, "title": "1984", "author": "George Orwell"},
{"id": 2, "title": "Brave New World", "author": "Aldous Huxley"},
]
next_id = 3
# CREATE
@app.route('/api/books', methods=['POST'])
def create_book():
global next_id
data = request.get_json()
book = {"id": next_id, "title": data['title'], "author": data['author']}
books.append(book)
next_id += 1
return jsonify(book), 201
# READ (all)
@app.route('/api/books', methods=['GET'])
def get_books():
return jsonify(books)
# READ (one)
@app.route('/api/books/<int:book_id>', methods=['GET'])
def get_book(book_id):
book = next((b for b in books if b['id'] == book_id), None)
if book is None:
return jsonify({"error": "Not found"}), 404
return jsonify(book)
# UPDATE
@app.route('/api/books/<int:book_id>', methods=['PUT'])
def update_book(book_id):
book = next((b for b in books if b['id'] == book_id), None)
if book is None:
return jsonify({"error": "Not found"}), 404
data = request.get_json()
book.update(data)
return jsonify(book)
# DELETE
@app.route('/api/books/<int:book_id>', methods=['DELETE'])
def delete_book(book_id):
global books
book = next((b for b in books if b['id'] == book_id), None)
if book is None:
return jsonify({"error": "Not found"}), 404
books = [b for b in books if b['id'] != book_id]
return '', 204
if __name__ == '__main__':
app.run(debug=True)Common Use Cases
1. User management systems
Register (Create), log in (Read), edit profile (Update), delete account (Delete) — the classic CRUD pattern.
2. Content management systems
Create articles, read published posts, update drafts, delete outdated content. WordPress and other CMS platforms are essentially CRUD interfaces.
3. E-commerce catalogs
Add products (Create), browse listings (Read), update prices (Update), remove discontinued items (Delete).
4. Task management apps
Create tasks, view today’s tasks, mark as complete (Update), delete completed tasks. Every to-do app is CRUD.
5. Database admin tools
phpMyAdmin, Adminer, and DBeaver are graphical CRUD interfaces that let users perform SQL operations without writing SQL.
FAQ
Related Terms
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro