Application Development Guide


Generated Columns

A generated column is a column that derives the values for each row from an expression, rather than from an insert or update operation. While combining an update trigger and an insert trigger can achieve a similar effect, using a generated column guarantees that the derived value is consistent with the expression.

To create a generated column in a table, use the GENERATED ALWAYS AS clause for the column and include the expression from which the value for the column will be derived. You can include the GENERATED ALWAYS AS clause in ALTER TABLE or CREATE TABLE statements. The following example creates a table with two regular columns, "c1" and "c2", and two generated columns, "c3" and "c4", that are derived from the regular columns of the table.

   CREATE TABLE T1(c1 INT, c2 DOUBLE, 
                   c3 DOUBLE GENERATED ALWAYS AS (c1 + c2),
                   c4 GENERATED ALWAYS AS 
                     (CASE
                        WHEN c1 > c2 THEN 1 
                        ELSE NULL
                      END)
                  );

For more information on using generated columns to improve the performance of your applications, refer to the Administration Guide. For more information on creating generated columns, refer to the CREATE TABLE statement syntax in the SQL Reference.


[ Top of Page | Previous Page | Next Page ]