Reading history

SQL Soft Delete and Audit Columns

5 min read 0 views

Learn soft delete with a deleted_at column, views that hide deleted rows, audit columns, and a trigger that writes an automatic change log.

A shop assistant deletes a customer by mistake. With a normal DELETE, the row and its history are gone. Many real systems never really delete important rows. Instead they mark them as deleted, and they also record who changed what and when. These patterns are called soft delete and audit columns, and they make data safer and easier to explain later.

In this lesson you will learn

  • The common audit columns: created_at, updated_at, deleted_at
  • How to soft delete and restore a row
  • How a view hides deleted rows from the app
  • How a trigger writes an audit log automatically
  • When a real delete is still the right choice

You should know views and triggers.

Audit Columns

The sample customers table has three extra columns besides name and phone:

  • created_at: when the row was added. A DEFAULT fills it in. Real systems use DEFAULT CURRENT_TIMESTAMP; the examples use fixed times so the output never changes.
  • updated_at: when the row was last changed.
  • deleted_at: NULL while the row is active, and the deletion time once it is “deleted”.

A view, active_customers, shows only rows where deleted_at IS NULL. The app reads from the view, so it never has to remember the filter.

Soft Delete and Restore

-- "Delete" Tuhin by stamping the time instead of removing the row
UPDATE customers
SET deleted_at = '2026-11-20 15:30'
WHERE id = 4;

-- The app reads from the view, so Tuhin disappears
SELECT * FROM active_customers;

-- But the row is still there for reports and audits
SELECT id, name, deleted_at FROM customers WHERE deleted_at IS NOT NULL;

-- Restoring is just clearing the stamp
UPDATE customers SET deleted_at = NULL WHERE id = 4;
SELECT COUNT(*) AS active FROM active_customers;

Output

+----+-------+-------------+
| id | name  | phone       |
+----+-------+-------------+
| 1  | Karim | 01711000001 |
| 2  | Rina  | 01811000002 |
| 3  | Joya  | 01911000003 |
+----+-------+-------------+
(3 rows)

+----+-------+------------------+
| id | name  | deleted_at       |
+----+-------+------------------+
| 4  | Tuhin | 2026-11-20 15:30 |
+----+-------+------------------+
(1 row)

+--------+
| active |
+--------+
| 4      |
+--------+
(1 row)
  • The “delete” is an UPDATE that stamps deleted_at. Tuhin vanishes from the view at once.
  • The row is still in the table, so old orders that point to Tuhin still make sense, and a report can list who was removed and when.
  • Undoing a mistake is one UPDATE setting deleted_at back to NULL. After that, all 4 customers are active again.

An Automatic Audit Log

Columns tell you that a row changed. An audit table tells you what changed. A trigger can write to it automatically, so nobody can forget.

CREATE TABLE customer_audit (
  id         INTEGER PRIMARY KEY,
  customer_id INTEGER NOT NULL,
  field      TEXT NOT NULL,
  old_value  TEXT,
  new_value  TEXT,
  changed_at TEXT NOT NULL
);

-- Record every phone number change automatically
CREATE TRIGGER trg_phone_audit
AFTER UPDATE OF phone ON customers
WHEN OLD.phone IS NOT NEW.phone
BEGIN
  INSERT INTO customer_audit (customer_id, field, old_value, new_value, changed_at)
  VALUES (NEW.id, 'phone', OLD.phone, NEW.phone, '2026-11-25 10:00');
  UPDATE customers SET updated_at = '2026-11-25 10:00' WHERE id = NEW.id;
END;

UPDATE customers SET phone = '01711999999' WHERE id = 1;
UPDATE customers SET phone = '01811000002' WHERE id = 2;   -- same number: no audit row

SELECT customer_id, field, old_value, new_value, changed_at FROM customer_audit;
SELECT id, name, phone, updated_at FROM customers WHERE id IN (1, 2);

Output

+-------------+-------+-------------+-------------+------------------+
| customer_id | field | old_value   | new_value   | changed_at       |
+-------------+-------+-------------+-------------+------------------+
| 1           | phone | 01711000001 | 01711999999 | 2026-11-25 10:00 |
+-------------+-------+-------------+-------------+------------------+
(1 row)

+----+-------+-------------+------------------+
| id | name  | phone       | updated_at       |
+----+-------+-------------+------------------+
| 1  | Karim | 01711999999 | 2026-11-25 10:00 |
| 2  | Rina  | 01811000002 | NULL             |
+----+-------+-------------+------------------+
(2 rows)
  • AFTER UPDATE OF phone fires only when the phone column is updated.
  • WHEN OLD.phone IS NOT NEW.phone skips updates that set the same value, so Rina’s unchanged number left no audit row. IS NOT also treats NULLs safely, unlike <>.
  • The trigger also sets updated_at. In a real system it would use CURRENT_TIMESTAMP and the user’s name or ID.

Soft or Hard Delete?

Soft delete is good forHard delete is needed for
Customers, orders, invoices and other records with historyData a person has the legal right to have erased
Undoing mistakesTemporary or junk data that only grows the table
Reports on what was removedRows that must disappear for security, such as old login tokens

Soft delete has costs too. Every query must exclude deleted rows, which is why the view matters. A UNIQUE email column can block a new customer from reusing the email of a deleted one; a partial unique index on active rows (… WHERE deleted_at IS NULL) solves this in SQLite and PostgreSQL.

Common Mistakes

  1. Forgetting the filter. Querying the table directly shows deleted rows. Read through the view.
  2. Using a Yes/No flag only. A timestamp tells you both whether and when.
  3. Updating updated_at by hand. It will be forgotten; let a trigger or the app framework do it.
  4. Keeping personal data forever. Soft delete is not a reason to ignore privacy rules.

Practice Exercises

Exercise 1. Write a query that lists customers deleted in November 2026.

Show answer
SELECT id, name, deleted_at
FROM customers
WHERE deleted_at >= '2026-11-01' AND deleted_at < '2026-12-01';

Exercise 2. Why does the trigger use OLD.phone IS NOT NEW.phone instead of OLD.phone <> NEW.phone?

Show answer

If either value is NULL, <> gives unknown and the trigger body would be skipped, so a change from NULL to a number would not be logged. IS NOT treats NULL as a normal value.

Frequently Asked Questions

What is a soft delete in SQL?

Instead of removing a row with DELETE, you mark it as deleted, usually by setting a deleted_at timestamp. Queries and views then hide marked rows, but the data can still be restored or used in reports.

How do I keep a history of changes in a SQL table?

Create an audit table with columns for the record ID, field, old value, new value and time, and add a trigger that inserts a row there whenever the main table is updated.

Summary

  • Audit columns record when rows are created, updated and deleted.
  • Soft delete sets deleted_at; restore by setting it back to NULL.
  • A view of active rows keeps deleted data out of everyday queries.
  • Triggers can fill an audit log automatically; hard delete is still right for erasure and junk data.

Next: SQL Project: Hospital Appointments Database.

Lessons are also marked complete when you click Next.

Leave a Reply

Your email address will not be published. Required fields are marked *