Scrivio Interview library
DesignIntermediate6 min read · updated 12 November 2026

Which audit columns would you add to business tables (created_at, updated_at, created_by and so on), how do you populate them reliably, and when are they not enough?

I would add created_at, created_by, updated_at and updated_by, plus a version column for optimistic locking. They are populated by Spring Data JPA auditing (@EnableJpaAuditing, @CreatedDate, @LastModifiedBy and an AuditorAware bean), backed by database defaults or triggers for writes that bypass the application, stored as timestamptz in UTC, ideally from the database clock when several services write. These columns only record the latest change, so real change history needs a history table, Hibernate Envers or CDC into an audit store, and regulated domains need tamper-evident logs.

Why this question is asked

Almost every business table ends up with audit columns, and almost every team discovers the same gaps: a batch job that leaves updated_by empty, timestamps in three time zones, or an auditor asking who changed a value last March when the column only knows about yesterday. Interviewers ask this design question to see whether we know how to fill the columns reliably and, just as important, what they cannot tell us. In this answer, we will choose the columns, populate them with Spring Data auditing, cover writes outside the application, get time right, and then see when we need real history.

1Choosing the Columns

The usual set answers four questions about each row: when was it created (created_at), by whom (created_by), when was it last changed (updated_at) and by whom (updated_by). A fifth column, version, is not strictly an audit column but sits naturally beside them: it is a number incremented on every update and used for optimistic locking, where an update only succeeds if the version has not changed since the row was read.

SQL
CREATE TABLE invoices (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
amount numeric(12,2) NOT NULL,
created_at timestamptz NOT NULL DEFAULT now(),
created_by text NOT NULL,
updated_at timestamptz NOT NULL DEFAULT now(),
updated_by text NOT NULL,
version bigint NOT NULL DEFAULT 0
);

created_by and updated_by hold a stable identifier of the actor, such as a user id or a service name like 'billing-batch', not a display name that can change. NOT NULL on all of them forces every write path to supply them, which is how we find the paths that do not.

2Populating Them With Spring Data JPA Auditing

Spring Data JPA can fill these fields automatically whenever an entity is saved through JPA. We enable auditing, annotate the fields, and tell Spring who the current user is.

Java
@Configuration
@EnableJpaAuditing
class AuditConfig {
@Bean
AuditorAware<String> auditor() {
return () -> Optional.ofNullable(SecurityContextHolder.getContext().getAuthentication())
.map(Authentication::getName)
.or(() -> Optional.of("system"));
}
}
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public abstract class Audited {
@CreatedDate @Column(updatable = false) Instant createdAt;
@CreatedBy @Column(updatable = false) String createdBy;
@LastModifiedDate Instant updatedAt;
@LastModifiedBy String updatedBy;
@Version long version;
}
invoiceRepository.save(new Invoice(new BigDecimal("120.00")));
// insert ... created_at = 2026-11-05T09:14:03Z, created_by = 'ana', version = 0

AuditingEntityListener runs before insert and update and fills the fields; AuditorAware tells it the current actor, here from Spring Security with a fallback for background jobs. updatable = false keeps the creation fields from being overwritten later. The catch is the word 'through JPA': a bulk JPQL update, a native query, a Flyway data migration or another service writing the table never passes through the listener.

3Covering Writes Outside the Application

For created_at and updated_at the database can protect itself. A DEFAULT now() covers inserts, and a small trigger can set updated_at on every update regardless of who writes.

SQL
CREATE FUNCTION touch_updated_at() RETURNS trigger AS $$
BEGIN
NEW.updated_at := now();
RETURN NEW;
END $$ LANGUAGE plpgsql;
CREATE TRIGGER invoices_touch BEFORE UPDATE ON invoices
FOR EACH ROW EXECUTE FUNCTION touch_updated_at();
UPDATE invoices SET amount = 130.00 WHERE id = 1; -- updated_at changes even from psql

The actor is harder, because the database only knows the connection's user, usually one shared application account. Some teams pass the real user into the session with SET LOCAL app.user = 'ana' at the start of each transaction and read it in a trigger with current_setting('app.user', true). That works but needs care with connection pools. A simpler rule is that scripts must set updated_by explicitly, which the NOT NULL constraint enforces.

4Getting Time Right

Timestamps from several services and servers are only comparable if they mean the same thing. In PostgreSQL we use timestamptz, which stores an absolute moment in time (internally UTC) and converts on display, instead of timestamp, which stores a wall-clock value with no zone and is ambiguous across regions and daylight saving changes. In Java we map it to Instant or OffsetDateTime and keep the JVM and the JDBC session in UTC so nothing converts silently.

Clocks also drift. If two services write the same row and each stamps updated_at from its own clock, the 'later' change may have an earlier time. Using the database clock, now() in a default or trigger, gives one source of time for each database. Note that now() in PostgreSQL returns the start time of the transaction, so all rows changed in one transaction get the same timestamp, which is usually what we want.

5When Audit Columns Are Not Enough

The columns only describe the last change. If an invoice amount went from 100 to 120 to 130, the row tells us that 'bob' changed it at 10:42, but not that 'ana' changed it before, nor what the old values were. For that we need history: every version of the row, with who and when.

Ways to record change history
ApproachWhat it recordsGood for
Audit columnsLast change onlyQuick 'who touched this' answers
History table with triggerEvery version, any writerFull history inside the database
Hibernate EnversEvery version via JPA, with revisionsJPA apps, little code
CDC into an audit storeEvery committed change from the WALMany services, central audit
Tamper-evident logSigned or hash-chained entriesRegulated domains, auditors

Envers (annotate the entity with @Audited) writes each change to an _AUD table with a revision number, but like the auditing listener it only sees JPA writes. A history table filled by a trigger sees every writer. CDC reads the write-ahead log, so it captures every committed change across all tables without touching the write path. In regulated domains such as payments or health, auditors may require that history cannot be quietly edited, even by an administrator. Columns the application can update guarantee nothing of the sort; that needs an append-only store with restricted access, hash chaining or write-once storage.

6Follow-up questions

Should created_by store a user id or a username?

A stable user id, or a service identifier for automated writes. Usernames and emails can change and may be personal data that has to be erased later, while an id can be resolved to the current name when displayed and anonymised in one place.

Why do bulk updates leave updated_at untouched?

Because a JPQL or native bulk update goes straight to the database without loading entities, so JPA entity listeners never run. Either set the columns explicitly in the bulk statement, or let a database trigger maintain updated_at so every path is covered.

Is the version column an audit column?

It is mainly for optimistic locking: the update includes WHERE version = ? and fails if someone else changed the row first. It does tell us roughly how many times the row was updated, but it is not history.

7Conclusion

In this answer, we chose created_at, created_by, updated_at, updated_by and a version column, filled them with Spring Data JPA auditing and an AuditorAware bean, and covered writes outside JPA with database defaults and triggers. We stored times as timestamptz in UTC from the database clock, and saw that these columns only know the last change, so history tables, Envers or CDC give full history, and regulated domains need tamper-evident audit logs.

What to say in the interviewAbout 90 seconds aloud

Open with

I would add created_at, created_by, updated_at and updated_by to business tables, plus a version column, which is really for optimistic locking but sits naturally with them. All of them not null, and the by columns hold a stable user id or service name, not a display name.

Then, if they want more

In a Spring application I would fill them with Spring Data JPA auditing: @EnableJpaAuditing, an AuditorAware bean that reads the current user from Spring Security with a fallback like system for jobs, and a mapped superclass with @CreatedDate, @CreatedBy, @LastModifiedDate, @LastModifiedBy and @Version, with the created fields not updatable. The weakness is that this only works for writes through JPA.

Bulk JPQL updates, native SQL, migrations and other services skip it, so I back the timestamps with database defaults and a before update trigger that sets updated_at. For time, I use timestamptz in PostgreSQL, Instant in Java, UTC everywhere, and preferably the database clock when several services write, so ordering is consistent.

Then I would be clear about the limit: these columns only describe the last change. If someone asks who changed the amount last March and from what, we need history: a trigger-filled history table, Hibernate Envers with its audit tables, or CDC into an audit store.

And in regulated domains the audit log has to be tamper-evident, append-only with restricted access, which application-updated columns can never guarantee.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Which audit columns would you add to business tables (created_at, updated_at, created_by...": https://scrivio-interview-library.netlify.app/databases/data-modeling/0bcca0c280/