generated columns

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;

时间: 2024-10-27 06:39:51

generated columns的相关文章

MySQL 5.7 虚拟列 (virtual columns)

参考资料: Generated Columns in MySQL 5.7.5 MySQL 5.7新特性之Generated Column(函数索引) MySQL 5.7原生JSON格式支持 Generated Column 在MySQL 5.7中,支持两种Generated Column,即Virtual Generated Column和Stored Generated Column,前者只将Generated Column保存在数据字典中(表的元数据),并不会将这一列数据持久化到磁盘上:后者

Mybatis自动生成key值

介绍 像MySQL.Sql Server有自动生成主键(递增),Oracle只能用序列生成,或者UUID 那么在MyBatis怎么处理呢,有两个处理方式,那么我们接下来看看怎么处理 编码 useGeneratedKeys useGeneratedKeys (insert and update only) This tells MyBatis to use the JDBC getGeneratedKeys method to retrieve keys generated internally

ExtJS4.2 仅需配置URL动态加载GridPanel列(带分页)

最近做ExtJS一直想做个傻瓜式的GridPanel,今天折腾了一天,从GitHub找到的老外写的解决方案, 在他的基础上做了一些改动,增加了分页,增加了columns手动配置(原本只能动态生成),大家有兴趣可以自由扩展,我做了很详细的注释 效果图如下,仅需在html页面引入ext.all,并创建自定义控件,配置url即可创建带分页效果的GirdPanel 代码: 效果图: 一.动态加载自定义控件 自定义脚本包括两部分:DynamicGrid.js和DynamicReader.js 将Ext目录

MySQL 5.7.15 发布,新增 InnoDB 配置选项

MySQL 5.7.15 发布了.更新如下: 安全事项: The validate_password plugin now supports the capability of rejecting passwords that match the current session user name, either forward or in reverse. To enable control over this capability, the plugin exposes a validate

datatables增删改查的实现

学习可参考:http://www.guoxk.com/node/jquery-datatables http://yuemeiqing2008-163-com.iteye.com/blog/2006942 分别导入css和js文件 <link href="~/Content/bootstrap.css" rel="stylesheet" /> <link href="~/Content/datatables/css/dataTables.

jquery datatables api(2)

二: 列属性 aoColumnDefs: This array allows you to target a specific column, multiple columns, or all columns, using the aTargets property of each object in the array (please note that aoColumnDefs was introduced in DataTables 1.7). This allows great flex

MYSQL 5.7 新增150多个新功能

http://www.thecompletelistoffeatures.com/ There are over 150 new features in MySQL 5.7. The MySQL manual is very good, but verbose. This is a list of new features in short bullet form. I have tried very hard to make sure each feature is only mentione

最近在写后台管理平台,用到jQuery dataTable 一个比较实用的例子分享下

学习可参考:http://www.guoxk.com/node/jquery-datatables http://yuemeiqing2008-163-com.iteye.com/blog/2006942 1:导入包: URL:http://www.datatables.net/ 分别导入css和js文件 Html代码   <style type="text/css" title="currentStyle"> @import "css/dem

jquery datatables api

原文地址 学习可参考:http://www.guoxk.com/node/jquery-datatables http://yuemeiqing2008-163-com.iteye.com/blog/2006942 分别导入css和js文件 <link href="~/Content/bootstrap.css" rel="stylesheet" /> <link href="~/Content/datatables/css/dataTa