Generated Column column in MySQL (calculated column)

Generated Column column in MySQL (calculated column)

MySQL's Generated Column is also called virtual column or calculated column. The value of the Generated Column column is calculated by including a calculation expression when the column is defined.

1. Define the syntax format of the Generated Column column

The syntax for defining Generated column is as follows:

列名 类型
[GENERATED ALWAYS] AS (expr) [VIRTUAL | STORED]
[NOT NULL | NULL] 
[UNIQUE [KEY]] 
[[PRIMARY] KEY]
[COMMENT 'string']

Description:
(1) AS (expr) is used to generate expressions for calculating column values.
(2) The VIRTUAL or STORED keywords indicate whether to store the value of the calculated column:
VIRTUAL: The column value is not stored, and the virtual column does not occupy storage space. The default setting is VIRTUAL.
STORED: Calculate and store column values ​​when adding or updating rows. Storage columns require storage space, and indexes can be created.

Second, the requirements of calculated column expressions

The generated column expression must follow the following rules. If the expression contains a definition that is not allowed, an error will occur.

(1) Text, built-in functions and operators are allowed, but functions with uncertain return values, such as NOW(), are not allowed.
(2) Stored functions and user-defined functions are not allowed.
(3) Stored procedures and function parameters are not allowed.
(4) Variables (system variables, user-defined variables, and stored program local variables) are not allowed.
(5) Subqueries are not allowed.
(6) A calculated column can refer to other calculated columns when it is defined, but only the columns that appear earlier in the table definition.
(7) You can create an index on a calculated column, but you cannot create a clustered index on a VIRTUAL type of calculated column.

Three, calculation examples

1. The definition of the table

mysql> create table sales(
    -> goods_id int primary key,
    -> goods_name char(20),
    -> unit_price int,
    -> quantity int,
    -> amount int generated always as (unit_price*quantity));
Query OK, 0 rows affected (0.02 sec)

2. Insert data

mysql> insert into sales(goods_id,goods_name,unit_price,quantity) values(100101,'Apple',2,4);
Query OK, 1 row affected (0.00 sec)

mysql> select * from sales;
+----------+------------+------------+----------+--------+
| goods_id | goods_name | unit_price | quantity | amount |
+----------+------------+------------+----------+--------+
|   100101 | Apple      |          2 |        4 |      8 |
+----------+------------+------------+----------+--------+
1 row in set (0.00 sec)

3. View the statement that creates the table

It can be seen that the default type of the calculated column is VIRTUAL.

mysql> show create table sales\G
*************************** 1. row ***************************
       Table: sales
Create Table: CREATE TABLE `sales` (
  `goods_id` int(11) NOT NULL,
  `goods_name` char(20) DEFAULT NULL,
  `unit_price` int(11) DEFAULT NULL,
  `quantity` int(11) DEFAULT NULL,
  `amount` int(11) GENERATED ALWAYS AS ((`unit_price` * `quantity`)) VIRTUAL,
  PRIMARY KEY (`goods_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
1 row in set (0.00 sec)

Go style

create table json_tab
(
  id        int unsigned primary key auto_increment comment '主键',
  json_info json comment 'json数据',
  json_id   int generated always as (json_info -> '$.id') comment 'json数据的虚拟字段',
  index json_info_id_idx (json_id)
)

Guess you like

Origin blog.csdn.net/weixin_44377973/article/details/107806406