MySQL中的计算列

编程入门 行业动态 更新时间:2024-10-26 20:23:39
本文介绍了MySQL中的计算列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试添加计算列.

I am trying to add a computeed column.

alter table datatest add column amount2 double as (amount*rate)

但执行此操作时出现错误

but I got error while executing this

推荐答案

MySQL不支持MySQL 5.7之前的计算列.现在,较新的版本确实支持计算列.

MySQL doesn't support computed columns prior to MySQL 5.7. The more recent versions do now support computed columns.

您可以改用视图:

create view v_datatest as select t.*, (amount * rate) as amount2 from datatest;

注意:

  • 在支持计算列的数据库中,类型不属于列定义.它是从表达式派生的(可以使用 cast()/ convert()转换为特定类型).
  • 使用浮点表示形式存储货币金额是一个坏主意.您应该改用 decimal / numeric .
  • 如果您不想使用视图,则可以向表中添加一列(以及类型),并使用触发器来维护值.
  • In databases that do support computed columns, the type is not part of the column definition. It is derived from the expression (you can use cast()/convert() to convert to a particular type).
  • It is a bad idea to store monetary amounts using floating point representations. You should be using decimal/numeric instead.
  • If you don't want to use a view, you can add a column to the table (along with the type) and use a trigger to maintain the value.

更多推荐

MySQL中的计算列

本文发布于:2023-10-13 20:28:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1488993.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:MySQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!