MariaDB触发循环

时间:2017-02-01 15:15:26

标签: sql triggers mariadb

我正在为学校创建一个数据库。这就是问题: 我得到了一个属性为A,B和C的表(其中C =(A /(A + B))* 100)。在更新属性A或B时,我需要更新C. 我尝试了不同的东西,但触发器继续进入循环,因为当我更新A或B时触发器更新C以便它继续运行。 这段代码不起作用(sintax错误):

Create trigger nametrigger
after update of (A or B) on tablename

但我不知何故需要指定触发器必须在更新A或B而不是C时激活。

1 个答案:

答案 0 :(得分:1)

根据您的描述,您不需要触发器,您需要C作为computed column(a.k.a.虚拟列)。

MariaDB [test]> CREATE TABLE t (
  a INT, 
  b INT, 
  c INT AS ((a/(a+b))*100) PERSISTENT
);

MariaDB [test]> insert into t (a,b) values (1,1);
MariaDB [test]> insert into t (a,b) values (2,2);

MariaDB [test]> select * from t;
+------+------+------+
| a    | b    | c    |
+------+------+------+
|    1 |    1 |   50 |
|    2 |    2 |   50 |
+------+------+------+
2 rows in set (0.01 sec)

MariaDB [test]> update t set b = b*2;

MariaDB [test]> select * from t;
+------+------+------+
| a    | b    | c    |
+------+------+------+
|    1 |    2 |   33 |
|    2 |    4 |   33 |
+------+------+------+
2 rows in set (0.00 sec)

这只是一个例子,而不是解决方案。您需要阅读计算列以确定需要配置的确切位置。