mysql test1@127.0.0.1:test> alter table test add constraint uk_test unique (a,b);
Query OK, 0 rows affected
Time: 0.090s
mysql test1@127.0.0.1:test> insert into test (a,b,c) values ('1','2','3');
Query OK, 1 row affected
Time: 0.004s
mysql test1@127.0.0.1:test> select * from test;
+---+---+---+
| a | b | c |
+---+---+---+
| 1 | 2 | 3 |
+---+---+---+
1 row in set
Time: 0.012s
mysql test1@127.0.0.1:test>
在设置约束键后,当我在 insert 一条相同的数据时,我想 new.c + old.c;
#当我添加一条相同的数据时;
mysql test1@127.0.0.1:test> insert into test (a,b,c) values ('1','2','3');
Query OK, 1 row affected
Time: 0.004s
#正常情况会报错,但我想要得到下面的效果 c = (new.c + old.c);
mysql test1@127.0.0.1:test> select * from test;
+---+---+---+
| a | b | c |
+---+---+---+
| 1 | 2 | 6 |
+---+---+---+
1 row in set
Time: 0.012s
有这种操作吗?
#或者可以这样吗:
alter table test add constraint uk_test unique (a,b) on add sum(c);