content_image/868330.jpg
The following simple example shows a table that stores the lengths of the sides of right triangles in the sidea  and sideb  columns, and computes the length of the hypotenuse in sidec  (the square root of the sums of the squares of the other sides): 

CREATE TABLE triangle ( sidea DOUBLE, sideb DOUBLE, sidec DOUBLE AS (SQRT(sidea * sidea + sideb * sideb)) ); INSERT INTO triangle (sidea, sideb) VALUES(1,1),(3,4),(6,8); 

Selecting from the table yields this result: 

<img src='content_image/868330.jpg'>

Any application that uses the triangle  table has access to the hypotenuse values without having to specify the expression that calculates them. 

Generated column definitions have this syntax: 

col_name data_type  [GENERATED ALWAYS] AS ( expr ) [VIRTUAL | STORED] [NOT NULL | NULL] [UNIQUE [KEY]] [[PRIMARY] KEY] [COMMENT ’ string ’] 

AS ( expr )  indicates that the column is generated and defines the expression used to compute column values. AS  may be preceded by GENERATED ALWAYS  to make the generated nature of the column more explicit. Constructs that are permitted or prohibited in the expression are discussed later. 

The VIRTUAL  or STORED  keyword indicates how column values are stored, which has implications for column use: 

• VIRTUAL : Column values are not stored, but are evaluated when rows are read, immediately after any BEFORE  triggers. A virtual column takes no storage. 

InnoDB  supports secondary indexes on virtual columns. See Section 13.1.20.9, “Secondary Indexes and Generated Columns” . 

• STORED : Column values are evaluated and stored when rows are inserted or updated. A stored column does require storage space and can be indexed. 

The default is VIRTUAL  if neither keyword is specified. 

It is permitted to mix VIRTUAL  and STORED  columns within a table. 

Other attributes may be given to indicate whether the column is indexed or can be NULL , or provide a comment. 

Generated column expressions must adhere to the following rules. An error occurs if an expression contains disallowed constructs. 

•Literals, deterministic built-in functions, and operators are permitted. A function is deterministic if, given the same data in tables, multiple invocations produce the same result, independently of the connected 

