When designing your database tables, it's crucial to include audit fields to track the creation, modification, and deletion of records. These audit fields provide essential insights into data changes, improve accountability, and help meet regulatory or compliance standards. In this article, we'll cover the key audit fields you should consider adding to your tables, including timestamps, user tracking, and versioning, ensuring a transparent and maintainable record lifecycle.
Improve your readability in:
Audit fields typically includes columns that track changes or events in a record’s lifecycle. These fields are used to monitor and maintain the integrity of the data, often for accountability, traceability, or compliance purposes. Below is a common list of audit fields: Standard Audit Fields created_at Tracks when the record was created. Typically set to the current timestamp at the time of insertion. Immutable (should not be updated after creation). updated_at Tracks the last time the record was modified. Automatically updated whenever the record changes. Optional Audit Fields deleted_at Tracks when the record was “soft deleted” (logical deletion without removing the data from the database). Useful in soft-delete mechanisms where data is hidden instead of permanently deleted. Nullable by default (null if the record isn’t deleted). created_by Tracks the ID or username of the user/system that created the record. Useful for identifying the origin of the record. updated_by Tracks the ID or username of the user/system that last updated the record. Ensures accountability for modifications. deleted_by Tracks the ID or username of the user/system that performed a soft delete. Advanced or Compliance-Specific Audit Fields version Tracks the version of the record, often used in. optimistic_locking Increments with each update to detect conflicts in concurrent transactions. change_reason Logs the reason for a record update or deletion. Often used in systems requiring strict audit trails. restored_at Tracks when a soft-deleted record is restored. restored_by Tracks the ID of the user/system that restored a soft-deleted record. Example Table with Audit Fields CREATE TABLE audit_example ( id INT AUTO_INCREMENT PRIMARY KEY, data TEXT, created_at DATETIME DEFAULT CURRENT_TIMESTAMP, updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, deleted_at DATETIME DEFAULT NULL, created_by INT, updated_by INT, deleted_by INT, change_reason VARCHAR(255), version INT DEFAULT 1 ); Best Practices for Audit Fields Use default values and triggers (if needed) to automate timestamping and updates. Store user identifiers in created_by, updated_by, etc., to link changes to specific users. Normalize audit fields across all tables to maintain consistency. Use soft-delete (deleted_at) instead of physical deletion to avoid accidental data loss. This ensures you can track not only the “what” and “when” of changes but also the “who” and “why,” which is essential for systems requiring detailed audit trails. References https://docs.oracle.com/cd/F47663_05/books/OnDemWebSvcs/c-Audit-Fields-aku1252063.html https://en.wikipedia.org/wiki/Optimistic_concurrency_control https://stackoverflow.com/questions/2762906/why-do-we-need-audit-columns-in-database-tables
Originally published at https://blog.ardenov.com.
Top comments (0)