生成列是由已存在的字段經過表達式計算得來的html
VIRTUAL,即虛擬
類型,字段值不實際存儲,當讀取行時再計算,虛擬列類型不佔存儲
STORED,即存儲
類型,字段值會實際存儲起來,當插入或更新時,字段值會計算出來並存儲起來mysql
col_name data_type [GENERATED ALWAYS] AS (expr) [VIRTUAL | STORED] [NOT NULL | NULL] [UNIQUE [KEY]] [[PRIMARY] KEY] [COMMENT 'string']
如:sql
CREATE TABLE person ( first_name VARCHAR(10) NOT NULL COMMENT '名', last_name VARCHAR(10) NOT NULL COMMENT '姓', full_name VARCHAR(21) GENERATED ALWAYS AS (CONCAT(first_name,' ',last_name)) STORED NOT NULL COMMENT '全名' );
再如:code
ALTER TABLE person ADD full_name_gc VARCHAR(21) GENERATED ALWAYS AS (CONCAT(first_name,'_',last_name)) VIRTUAL NOT NULL COMMENT '全名(虛擬列)'
詳見 https://dev.mysql.com/doc/refman/5.7/en/create-table-generated-columns.htmlhtm