GENERATED is a reserved keyword defined in the SQL standard and adopted by most modern databases to declare columns whose contents are produced automatically rather than supplied by the user. Two major forms exist:1. Identity columns – The database assigns a unique sequence value to the column. Declared with "GENERATED ALWAYS AS IDENTITY" or "GENERATED BY DEFAULT AS IDENTITY". "ALWAYS" forbids user-supplied values, while "BY DEFAULT" lets applications override the sequence when needed.2. Computed (or virtual) columns – The column is calculated from an expression referencing other columns in the same table. Declared with "GENERATED ALWAYS AS (expression)". In MySQL and MariaDB you must add "VIRTUAL" or "STORED" to specify whether the value is materialised on disk.Key behaviors:- The database enforces the generation rule on INSERT and UPDATE.- Generated values participate in indexes, constraints, and queries like normal columns.- A generated column cannot usually be directly updated.Caveats:- Not all dialects support both identity and expression variants.- Expression syntax must be deterministic and free of sub-queries in many engines.- Changing a generated column after creation often requires recreating the table.
ALWAYS
(keyword) - Database always supplies the value; user inserts are rejected.BY DEFAULT
(keyword) - Database supplies a value unless the insert provides one.IDENTITY_OPTIONS
(clause) - Start, increment, min, max, cycle, cache settings for the sequence.expression
(SQL expression) - Deterministic formula used to compute the column.VIRTUAL
(keyword) - Value is calculated at read time and not stored on disk.STORED
(keyword) - Value is materialised and kept on disk like a normal column.AUTO_INCREMENT, SERIAL, IDENTITY, COMPUTED COLUMN, DEFAULT, PRIMARY KEY
SQL:2003
PostgreSQL, MySQL, MariaDB, Oracle, SQL Server, SQLite, DB2, and others implement at least one form of GENERATED columns.
No. The database treats the column as read-only. To modify it, you must update the source columns or recreate the table definition.
DEFAULT inserts a static or function-based value when the row omits the column, but applications may still override it. GENERATED ALWAYS forbids overrides and can derive its value from other columns.
Identity columns have minimal overhead. STORED computed columns add storage but speed reads. VIRTUAL columns save disk space but calculate on the fly, which can slow heavy reads.