100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

How Do You Design an Audit Trail Table in a Relational Database?

Learn how to design an audit trail table using triggers, capture old and new values, and keep the history tamper-evident.

mediumQ223 of 228 in Database Est. time: 6 minsLast updated:
Open Code Lab
223 / 228

Expected Interview Answer

An audit trail table records who changed what, when, and how — typically storing the table name, row ID, changed columns, old and new values, the actor, and a timestamp for every insert, update, or delete on the tables being audited, usually populated via triggers or an application-level logging layer.

The two common shapes are a wide history table that mirrors the source table's columns plus metadata (operation, changed_by, changed_at) for a full row snapshot per change, or a narrow event-log table storing one row per changed field with old/new value pairs, which is more flexible across many tables but harder to reconstruct a full row from. Triggers guarantee the audit entry is written in the same transaction as the change, so it can never be bypassed by application code, while application-level logging is easier to enrich with business context (like the reason for a change) but can be skipped if a direct database write happens outside the application. Either way, the audit table should be append-only, indexed by row ID and timestamp for lookups, and never updated or deleted once written.

  • Provides a tamper-evident record of who changed what and when
  • Supports compliance, dispute resolution, and debugging of unexpected data changes
  • Enables reconstructing a row's state at any point in history
  • Trigger-based capture cannot be bypassed by application bugs

AI Mentor Explanation

A cricket board's scorer keeps a separate logbook alongside the official scoreboard, writing down every correction ever made — who changed a run count, from what value to what, and at what minute — even after the scoreboard itself is updated to the corrected number. The logbook is never edited or torn out; it only grows. An audit trail table works the same way: every change to the main data gets a permanent, append-only entry describing exactly what changed and who made the change, separate from the current live data.

Step-by-Step Explanation

  1. Step 1

    Design the audit table shape

    Choose a wide row-snapshot table or a narrow field-level event log, storing table name, row ID, operation, old/new values, actor, and timestamp.

  2. Step 2

    Capture changes via triggers

    Attach AFTER INSERT/UPDATE/DELETE triggers on the source table so every change is logged in the same transaction, with no way to bypass it.

  3. Step 3

    Make the audit table append-only

    Grant only INSERT privileges on the audit table to the application role; never allow UPDATE or DELETE on it.

  4. Step 4

    Index for lookups

    Index by source row ID and by changed_at so you can efficiently pull a row's history or query changes within a time range.

What Interviewer Expects

  • A concrete audit table schema with actor, timestamp, old/new values, and operation type
  • Discussion of trigger-based vs application-level capture and their trade-offs
  • Awareness that the audit table itself must be append-only and tamper-evident
  • Recognition of the storage growth trade-off for high-write tables

Common Mistakes

  • Allowing UPDATE or DELETE privileges on the audit table, defeating its purpose
  • Relying only on application-level logging, which can be bypassed by direct database access
  • Not capturing the actor (who made the change), only the what and when
  • Storing only the new value with no old value, making it impossible to see what changed

Best Answer (HR Friendly)

An audit trail table records every meaningful change made to important data — what changed, who changed it, the old and new values, and when it happened — kept as a permanent, append-only history separate from the live table. I would usually implement it with database triggers so the audit entry is written in the same transaction as the actual change and can never be skipped, and I would lock down the audit table so it only accepts inserts, never updates or deletes, to keep it trustworthy.

Code Example

Audit trail table with a trigger
CREATE TABLE customers_audit (
  audit_id BIGSERIAL PRIMARY KEY,
  customer_id BIGINT NOT NULL,
  operation TEXT NOT NULL,
  old_email TEXT,
  new_email TEXT,
  changed_by TEXT NOT NULL,
  changed_at TIMESTAMPTZ NOT NULL DEFAULT now()
);

CREATE OR REPLACE FUNCTION log_customer_email_change()
RETURNS TRIGGER AS $$
BEGIN
  IF OLD.email IS DISTINCT FROM NEW.email THEN
    INSERT INTO customers_audit (customer_id, operation, old_email, new_email, changed_by)
    VALUES (NEW.customer_id, 'UPDATE', OLD.email, NEW.email, current_user);
  END IF;
  RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER trg_customers_email_audit
AFTER UPDATE ON customers
FOR EACH ROW EXECUTE FUNCTION log_customer_email_change();

Follow-up Questions

  • How would you reconstruct a row's full state as of a specific past date from an audit table?
  • What are the storage implications of auditing a very high-write table, and how would you mitigate them?
  • Would you prefer trigger-based auditing or application-level auditing, and why?
  • How would you prevent even a database administrator from tampering with audit records?

MCQ Practice

1. Why are database triggers often preferred for capturing audit trail entries?

Because triggers fire as part of the same transaction, any write to the source table guarantees a corresponding audit entry, even for direct database access.

2. What privilege should the application role have on an audit trail table?

Restricting the audit table to INSERT-only preserves its integrity as a tamper-evident, append-only history.

3. What core fields does a well-designed audit entry need at minimum?

Without old value, actor, and timestamp, the audit entry cannot answer who changed what, from what, to what, and when.

Flash Cards

What is an audit trail table?An append-only table recording who changed what data, the old and new values, and when the change happened.

Why use triggers for audit logging?Triggers run in the same transaction as the change, so the audit entry cannot be skipped or bypassed.

What privilege should be granted on the audit table?INSERT only — never UPDATE or DELETE — to keep the history tamper-evident.

Wide snapshot vs narrow event log?A wide table stores a full row snapshot per change; a narrow log stores one row per changed field with old/new values.

1 / 4
223 / 228

Continue Learning

Frequently Asked Questions

21 categories · pick one to explore

Where can I practice technical interview questions for free in India?
SkillVeris offers a free interview questions section with readiness scoring, so you can practice technical interview questions at no cost from anywhere in India or worldwide. Questions span AI/ML, programming, web development, DevOps, cloud and security. Combine them with Code Lab for hands-on coding practice and the AI Mentor for instant explanations whenever an answer confuses you.
How do I prepare for a coding interview if I am a complete beginner?
Start by learning one language well, then practice small problems daily before attempting interview questions. On SkillVeris you can take a free structured programming course with 24–40 lessons, write code in the in-browser Code Lab across 6 languages, and then move to the interview questions bank with readiness scoring to measure how prepared you actually are.
What is interview readiness scoring and how does it work?
Interview readiness scoring measures how prepared you are for a technical interview based on your performance across practice questions. On SkillVeris, as you answer interview questions, your readiness score updates so you can see weak topics before the real interview. It turns vague confidence into a concrete signal, telling you which areas need more revision and which are already strong.
How long does it take to get interview ready for a developer job?
Most beginners need a few months of consistent practice to feel interview ready, though timelines vary with background and daily study time. A practical route is finishing a structured SkillVeris course, which includes 24–40 lessons, module assessments and a final exam at an 80 percent pass mark, then drilling the free interview questions until your readiness score is consistently strong.
What are the most common technical interview topics for freshers?
Freshers are commonly tested on programming fundamentals, data structures, problem solving, databases, and basics of the role's stack such as web development or cloud. SkillVeris covers these through 37 free courses, a glossary of around 2,000 terms for quick definitions, cheat sheets for revision, and interview questions with readiness scoring so you can practise exactly what interviewers usually ask.
Can I practice coding interview prep without installing anything?
Yes, SkillVeris Code Lab runs entirely in your browser, so you can practice coding interview prep without installing compilers or editors. It supports 6 languages across 15 categories of exercises. Pair Code Lab sessions with the interview questions bank and readiness scoring, and you have a complete free preparation setup that works on any machine with an internet connection.
How do I explain technical concepts clearly in an interview?
Practice explaining each concept through a simple analogy first, then add the technical detail, because interviewers value clarity over jargon. SkillVeris teaches with its Learn Through Hobbies method, explaining concepts through cricket, music, gaming, cooking and eight more domains. Learning a topic through an analogy gives you a ready-made, memorable way to explain it when an interviewer asks.
Are there free mock interview resources for software engineers?
SkillVeris provides free interview questions with readiness scoring, which works like a self-paced mock interview you can repeat any time. You answer real-style technical questions, get scored, and see where you stand. The AI Mentor is available 24/7 to explain any answer at Quick, Detailed or Deep-dive depth, acting like an always-available interviewer who never gets tired.
How should I prepare for an AI or machine learning interview?
Cover ML fundamentals, Python, and modern topics like LLMs and RAG, then practise explaining projects clearly. SkillVeris has free AI/ML courses including Python for AI and ML, Large Language Models and Retrieval-Augmented Generation, each with 24–40 lessons and assessments. After finishing, use the interview questions bank and readiness scoring to confirm you can answer under pressure.
What should I revise the night before a technical interview?
Revise concise summaries rather than learning anything new: core definitions, common patterns, and your own project stories. SkillVeris cheat sheets and study notes are built exactly for this kind of quick revision, and the glossary of around 2,000 terms lets you verify any definition fast. A short readiness-score check on the interview questions bank confirms nothing critical is shaky.
How do I prepare for a DevOps interview with no work experience?
Build demonstrable knowledge through structured learning and hands-on practice, since you cannot point to job experience. SkillVeris offers free DevOps, Docker and Kubernetes related courses within its 37-course catalog and a DevOps Engineer learning path. Complete the courses, earn certificates by passing the final exams, then use the interview questions section to rehearse common DevOps scenarios.
Do certificates help in clearing technical interviews?
Certificates alone do not clear interviews, but they signal structured learning and give interviewers a starting point for questions. SkillVeris awards certificates when you pass a course final exam at 80 percent, after 24–40 lessons and module assessments, so the certificate reflects genuine effort. Pair it with practised interview answers and Code Lab projects to make a stronger impression.
How can I improve my problem solving speed for coding interviews?
Speed comes from repetition on progressively harder problems, not from rushing. Practise daily in SkillVeris Code Lab, which offers in-browser coding in 6 languages across 15 categories, and time yourself on interview questions. Review every mistake with the AI Mentor, which explains solutions 24/7 at Quick, Detailed or Deep-dive depth, so each session genuinely improves your pattern recognition.
What questions are asked in a Python technical interview?
Python interviews typically cover data types, functions, comprehensions, OOP, error handling and libraries relevant to the role, such as ML packages for AI positions. SkillVeris interview questions include Python topics, and its free Python for AI and ML course builds the underlying knowledge across 24–40 lessons. Readiness scoring then shows whether your Python answers are interview grade.
How do I handle interview questions I don't know the answer to?
Say what you do know, reason aloud toward an approach, and be honest about the gap, because interviewers value thinking over bluffing. Reduce how often this happens by broadening fundamentals: SkillVeris study notes, cheat sheets and the roughly 2,000-term glossary fill knowledge gaps quickly, and readiness scoring highlights weak areas before an interviewer ever finds them.
Is there a free way to check my interview readiness before applying for jobs?
Yes, SkillVeris interview questions come with readiness scoring, all completely free, so you can measure preparation before applying. Once your score looks solid, browse the SkillVeris Jobs page, which aggregates live roles across India, UK, USA, Germany and Remote with salary and experience filters, and apply knowing your technical preparation has been objectively tested.
How do I prepare for web developer interview questions?
Focus on JavaScript fundamentals, your framework of choice, HTTP basics, and building things you can discuss. SkillVeris offers free web development courses among its 37-course catalog, plus a Full Stack Java Developer learning path. After the courses, drill the interview questions bank, practise coding tasks in Code Lab, and use readiness scoring to verify you cover typical frontend and backend topics.
Can the AI Mentor help me practice interview answers?
Yes, the SkillVeris AI Mentor answers questions 24/7 and can explain any concept at Quick, Detailed or Deep-dive depth, which makes it useful for rehearsing interview explanations. Ask it to clarify a topic, then try explaining it back in your own words. Combined with the interview questions bank and readiness scoring, it becomes a free, always-available practice partner.
What is the best free platform for technical interview prep in 2026?
SkillVeris is a strong free option because it combines structured learning with dedicated interview preparation in one place. You get 37 free courses, interview questions with readiness scoring, an in-browser Code Lab for hands-on practice, cheat sheets and study notes for revision, and a 24/7 AI Mentor. Everything is free, which matters for learners in India and worldwide.
How do I stay calm and confident during a technical interview?
Confidence comes from evidence of preparation, so build that evidence deliberately. Complete a SkillVeris course, pass its final exam at the 80 percent bar, and watch your readiness score climb on the interview questions bank. Knowing you have explained concepts through analogies, coded in Code Lab, and scored well on practice questions gives you genuine, grounded calm on the day.

What Learners Say

Real journeys from the SkillVeris community — swipe for more.

SkillVeris taught me Python through Cricket. Now I’m building real projects and feeling confident!
Arjun S. · B.Tech Student
The best platform for hobby-based learning. Concepts finally stick.
Priya R. · Data Analyst
I went from zero coding to a portfolio of projects — all by learning through my love for gaming. Landed my first internship!
Kabir M. · CS Undergraduate
Trending Topics50 popular tags — tap to explore
Trending CoursesAll 37 free courses — tap to browse