SQLite Generated Columns
Generated Columns
A generated column gets its value from a calculation, usually with other columns in the same row. You cannot insert or update its value yourself.
Example
CREATE TABLE IF NOT EXISTS Lines (
Product TEXT,
Price REAL,
Quantity INTEGER,
Total REAL GENERATED ALWAYS AS
(Price * Quantity)
);
INSERT INTO Lines (Product, Price, Quantity)
VALUES ('Pen', 2.5, 4), ('Book', 12, 2);
SELECT * FROM Lines;
Try it Yourself »
Note: Generated columns work in SQLite 3.31.0 and later.
A generated column can be VIRTUAL or STORED:
VIRTUAL(the default) calculates the value every time it is readSTOREDcalculates the value when the row is written, and saves it in the file
You can leave out GENERATED ALWAYS, and write just AS (expression).