我有下面的SQL表,其中有一个positive列和一个negative列,都是int的。
positive negative
----------------------
5 3
3 1
10 7如何创建第三列,例如等于total的positive - negative。此外,我希望每次positive或negative列的元素发生更改时都更新positive列。
我如何在SQL中做到这一点?
编辑:,我正在使用MariaDB
发布于 2015-02-26 10:38:00
使用计算列,如下所述
您可以将表创建为
create table table_name ( positive int, negitive int, difference as positive-negitive)然后在创建之后,如果您输入值为
insert into table_name values(3,2)-不需要输入第三列,它称为计算列。
然后,在插入后的差异将出现在第三栏“差异”。
发布于 2015-02-26 10:31:15
使用虚拟计算列,如这里所解释的,https://mariadb.com/kb/en/mariadb/virtual-computed-columns/
create table table1
(
positive int not null,
negative int not null,
difference int as (positive - negative) virtual
);https://stackoverflow.com/questions/28739869
复制相似问题