When creating an account, the caller knows the username and the database knows when the record gets written. To let SQLite supply the creation time, declare the column with DEFAULT CURRENT_TIMESTAMP and leave it out of the INSERT column list. Declaring just TIMESTAMP NOT NULL requires a value. If the column has no default and the insert supplies no value, SQLite reports a not-null constraint error.1
What does SQLite store if I omit the timestamp column?
Try this two-column example in an empty database. Supply only a username when inserting the account, then read back the timestamp and its storage type.
CREATE TABLE users (
user_name TEXT NOT NULL UNIQUE,
user_created_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
INSERT INTO users (user_name) VALUES ('lin');
SELECT user_created_time, typeof(user_created_time)
FROM users
WHERE user_name = 'lin';
The first output column is the UTC time of insertion, in YYYY-MM-DD HH:MM:SS format, with one-second precision. The second is text. SQLite has no dedicated date/time storage class. This default writes text. If you later change the username with UPDATE, the creation time stays the same.12
Does explicitly supplying NULL trigger the default too?
No. A Stack Overflow answer I read at the time specifically cautioned: “However, if your INSERT command explicitly sets this field to NULL, it will be set to NULL.”3 That answer used a nullable timestamp column. With the NOT NULL schema above, the following statement returns NOT NULL constraint failed: users.user_created_time.
INSERT INTO users (user_name, user_created_time)
VALUES ('chen', NULL);
This matters when you build the insert statement in your application. If it contains two placeholders and you bind a missing creation time to NULL, SQLite receives an explicit null. To have the database fill in the time, list only user_name. Conversely, when importing an existing account, you can explicitly supply its original creation time to preserve it.
When did I add this default to the users table?
On the afternoon of April 22, 2025, I was building the users table for an asset management system. A question about automatic timestamps led me to SQLite's date function documentation. In the first version of the table I committed that day, the creation time was still declared simply as user_created_time TIMESTAMP NOT NULL.4
When I added login on April 24, I needed to create a default administrator during database initialization. This time I changed the field to TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP. The administrator insert listed fields such as login name, nickname, and email address, but left out the creation time, letting the database fill it in.5 For the separate unique constraint on the login name in the same table, see Can both PRIMARY KEY and UNIQUE speed up SQLite queries?.
Footnotes
-
SQLite. (n.d.). CREATE TABLE, §3.2 “The DEFAULT clause”. SQLite Documentation. Values for omitted columns and the UTC format ↩ ↩2
-
SQLite. (n.d.). Date and time functions, §1 “Overview”. SQLite Documentation. Date and time storage in SQLite ↩
-
CL. (2013, January 22). How to have an automatic timestamp in SQLite? [Answer, paragraph following the CREATE TABLE example]. Stack Overflow. Defaults and explicit NULL ↩
-
borui. (2025, April 22). Fix actions endpoint error handling; add user table creation [Commit dfc8bd4]. GitHub. First version of the users table ↩
-
borui. (2025, April 24). Add login feature; add router using wouter [Commit b94e7c7]. GitHub. Timestamp default and administrator INSERT ↩