本文介绍了MySQL 5.6中生成列的替代方法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MySQL alter语句

I have a MySQL alter statement

ALTER TABLE `employee`
ADD `employee_name_generator` CHAR(20) GENERATED ALWAYS AS
    (COALESCE(concat(`employee_name`), '^')) VIRTUAL;

这是在唯一约束中添加employee_name_generator所必需的.在MySQL 5.7及更高版本中,此方法运行良好.但是我需要的环境是MySQL 5.6. MySQL 5.6是否有替代选择?

This is needed for adding employee_name_generator in a unique constraint. This works fine in MySQL 5.7 onwards. But the environment where I need this has MySQL 5.6. Is there an alternative for MySQL 5.6?

推荐答案

如果您的早期版本的MySQL不支持生成的列,那么您必须在查询时计算该列.一种选择是视图:

If your earlier version of MySQL does not support generated columns, then you'll have to compute that column at the time you query. One option would be a view:

CREATE VIEW yourView AS (
    SELECT *, COALESCE(CONCAT(employee_name, '^')) AS employee_name_generator
    FROM employee
)

MySQL不支持(直接)实例化视图.因此,如果您确实需要实例化视图的行为,则必须执行类似的操作,例如,在我上面给出的视图中使用选择逻辑来创建临时表.

MySQL does not support materialized views (directly). So, if you really needed the behavior of a materialized view, you would have to do something like create a temporary table using the select logic in the view I gave above.

这篇关于MySQL 5.6中生成列的替代方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 04:07