当前位置: 移动技术网 > IT编程>数据库>Mysql > MYSQL5.7生成列简介及创建

MYSQL5.7生成列简介及创建

2019年05月21日  | 移动技术网IT编程  | 我要评论

1、说明

生成列是由已存在的字段通过表达式计算得来的

2、生成列类型

virtual,即虚拟类型,字段值不实际存储,当读取行时再计算,虚拟列类型不占存储
stored,即存储类型,字段值会实际存储起来,当插入或更新时,字段值会计算出来并存储起来

3、用法

col_name data_type [generated always] as (expr)
  [virtual | stored] [not null | null]
  [unique [key]] [[primary] key]
  [comment 'string']

如:

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 '全名'
);

再如:

alter table person add full_name_gc varchar(21) 
generated always as (concat(first_name,'_',last_name)) virtual not null comment '全名(虚拟列)'

详见

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网