generated columns

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群版 2核4GB 100GB
推荐场景:
搭建个人博客
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
简介: 1.generated columns:可以定义一个带有函数表达的列例1:CREATE TABLE triangle (sidea DOUBLE,sideb DOUBLE,sidec DOUBLE AS (SQRT(sidea * sidea + sideb * s...

1.generated columns:可以定义一个带有函数表达的列

例1:

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);


mysql>show create table triangle\G

*************************** 1. row ***************************

       Table: triangle

Create Table: CREATE TABLE `triangle` (

  `sidea` double DEFAULT NULL,

  `sideb` double DEFAULT NULL,

  `sidec` double GENERATED ALWAYS AS (sqrt(((`sidea` * `sidea`) + (`sideb` * `sideb`)))) VIRTUAL

) ENGINE=InnoDB DEFAULT CHARSET=utf8


2.generated columns参数

virtual:没有存储列的值,不占存储空间,在5.7.8之前,虚拟列不支持索引,到5.7.8,Innodb引擎开始支持secondary  indexse,不指定参数,默认是虚拟列,如例1;


stored:列真实存在,并需要存储空间,支持索引;


例2:

mysql>create table t3(c1 int,c2 int generated always as (c1+1) virtual,c3 int generated always as (c1+1) stored);   


mysql>insert into t3(c1) values(1);


mysql>select * from t3;

+------+------+------+

| c1   | c2   | c3   |

+------+------+------+

|    1 |    2 |    2 |

+------+------+------+


添加索引:

mysql>alter table t3 add index index_c2(c2);

mysql>alter table t3 add index index_c3(c3);


查看表的状态信息:

mysql>show table status like 't3'\G

*************************** 1. row ***************************

           Name: t3

         Engine: InnoDB

        Version: 10

     Row_format: Dynamic

           Rows: 1

 Avg_row_length: 16384

    Data_length: 16384

Max_data_length: 0

   Index_length: 0

      Data_free: 0

 Auto_increment: NULL

    Create_time: 2016-12-22 22:45:38

    Update_time: 2016-12-22 22:40:49

     Check_time: NULL

      Collation: utf8_general_ci

       Checksum: NULL

 Create_options:

        Comment:


查看执行计划:


mysql>desc select * from t3 where c2=2\G   

*************************** 1. row ***************************

           id: 1

  select_type: SIMPLE

        table: t3

   partitions: NULL

         type: ref

possible_keys: index_c2

          key: index_c2

      key_len: 5

          ref: const

         rows: 1

     filtered: 100.00

        Extra: NULL

1 row in set, 1 warning (0.00 sec)



mysql>explain select * from t3 where c2=2\G

*************************** 1. row ***************************

           id: 1

  select_type: SIMPLE

        table: t3

   partitions: NULL

         type: ref

possible_keys: index_c2

          key: index_c2

      key_len: 5

          ref: const

         rows: 1

     filtered: 100.00

        Extra: NULL

1 row in set, 1 warning (0.00 sec)


也可以在虚拟列上再创建虚拟列:


mysql>alter table t3 add c4 int generated always as (c2+1) virtual;


mysql>alter table t3 drop c4;


相关实践学习
如何在云端创建MySQL数据库
开始实验后,系统会自动创建一台自建MySQL的 源数据库 ECS 实例和一台 目标数据库 RDS。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
12月前
|
关系型数据库 MySQL 数据库
View ‘information_schema.SCHEMATA‘ references invalid table(s) or column(s) or function(s) or define
View ‘information_schema.SCHEMATA‘ references invalid table(s) or column(s) or function(s) or define
169 0
|
数据库
Incorrect table definition; there can be only one auto column and it must be defined as a key
Incorrect table definition; there can be only one auto column and it must be defined as a key
135 0
Incorrect table definition; there can be only one auto column and it must be defined as a key
The Linknode for Linear table | Data
The Code in Data book (5th Edition) from the 49 page to 52 page
66 0
The Sqlist for Linear table | Data
The Code in Data book (5th Edition) from the 35 page to 37 page
68 0
|
数据库 Python
AssertionError: Model app can‘t have more than one auto-generated field.
AssertionError: Model app can‘t have more than one auto-generated field.
323 0
AssertionError: Model app can‘t have more than one auto-generated field.
|
SQL Java 数据库连接
JPA异常:Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
JPA异常:Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
1838 0
How is new Appoinment id generated in my task followup scenario
How is new Appoinment id generated in my task followup scenario
How is new Appoinment id generated in my task followup scenario
|
数据库
Data truncation: Out of range value for column ‘estimate_score‘
Data truncation: Out of range value for column ‘estimate_score‘
|
SQL 存储 Oracle
Implementation of Global Temp Table
作者| 曾文旌阿里云数据库高级技术专家
343 0
Implementation of Global Temp Table