mysql触发器问题
图书类别表如下:
+------+
| bkn |
+------+
| 健康 |
| 军事 |
| 医药 |
| 哲学 |
| 小说 |
| 政治 |
| 教育 |
| 散文 |
| 科幻 |
| 童话 |
| 经济 |
| 美食 |
+------+
图书表里面有很多这些类别的书,图书表中图书类别的外键是图书类别表的图书类别。
我想做一个触发器,在我更改图书类别前,(因为外键约束,改哪个表的类别都是不行的),能够自动向图书类别表增加要改成的那个新类别,改完之后,再把旧类别从图书类别表中删去。
就这么点问题,却遇到了莫名其妙的错误。
DELIMITER $ CREATE TRIGGER change_bk BEFORE UPDATE ON b FOR EACH ROW BEGIN insert into bkind values(new.bk); END$
mysql> update b set bk = '医学' where bk = '医药'$
ERROR 1062 (23000): Duplicate entry '医学' for key 'PRIMARY'
我好像明白了,b表里有不止一条要改的记录,他每次都要执行一遍插入操作,所以。。。
做了个测试,真的是这样。
mysql> insert into bkind values('lala');
-> $
Query OK, 1 row affected (0.03 sec)
mysql> insert into b values('1', '1', 'lala', '1', 1, 1, '1', '1', '1');
-> $
Query OK, 1 row affected (0.03 sec)
mysql> update b set bk = 'la' where bk = 'lala'$
Query OK, 1 row affected (0.03 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from bkind;
-> $
+------+
| bkn |
+------+
| la |
| lala |
插进来了!!!
哇哇哇!!!就在几秒前,我终于把标题最前面的待解决三个字去掉了!!!
血汗泪啊!!!
DELIMITER $ CREATE TRIGGER change_bk BEFORE UPDATE ON b FOR EACH ROW BEGIN if(new.bk not in (select bkn from bkind)) then insert into bkind values(new.bk); end if; END$
mysql> update b set bk = 'hj' where bk = '医药'$
Query OK, 2 rows affected (0.04 sec)
Rows matched: 2 Changed: 2 Warnings: 0
mysql> select * from b where bk = 'hj';
-> $
+---------------+----------------------+------+-------+----------+------+--------+-----------------------+-------------+
| isbn | bn | bk | bs | maxstore | rest | author | publisher | publishdate |
+---------------+----------------------+------+-------+----------+------+--------+-----------------------+-------------+
| 9787502581091 | 医药学基础 | hj | 正常 | 3 | 2 | 戴敏 | 化学工业出版社发行部 | 2006-3 |
| 9787806621950 | 中国苗族药物彩色图集 | hj | 报废 | 3 | 3 | 汪毅 | 贵州科技出版社 | 2002-1 |
+---------------+----------------------+------+-------+----------+------
————————————感谢坚持解决问题的自己——————————————————————————————
整理:
mysql> update b set bk = '医学' where bk = '医药'$
Query OK, 2 rows affected (0.04 sec)
Rows matched: 2 Changed: 2 Warnings: 0
mysql> select * from b where bk = '医学'$
+---------------+----------------------+------+-------+----------+------+--------+-----------------------+-------------+
| isbn | bn | bk | bs | maxstore | rest | author | publisher | publishdate |
+---------------+----------------------+------+-------+----------+------+--------+-----------------------+-------------+
| 9787502581091 | 医药学基础 | 医学 | 正常 | 3 | 2 | 戴敏 | 化学工业出版社发行部 | 2006-3 |
| 9787806621950 | 中国苗族药物彩色图集 | 医学 | 报废 | 3 | 3 | 汪毅 | 贵州科技出版社 | 2002-1 |
+---------------+----------------------+------+-------+----------+------+--------+-----------------------+-------------+
2 rows in set (0.00 sec)
mysql> select * from bkind;
-> $
+------+
| bkn |
+------+
| la |
| lala |
| 健康 |
| 军事 |
| 医学 |
| 哲学 |
| 小说 |
| 政治 |
| 教育 |
| 散文 |
| 科幻 |
| 童话 |
| 经济 |
| 美食 |
+------+
14 rows in set (0.00 sec)
完整代码:
DELIMITER $ CREATE TRIGGER change_bk BEFORE UPDATE ON b FOR EACH ROW BEGIN if(new.bk not in (select bkn from bkind)) then insert into bkind values(new.bk); end if; END$ DELIMITER $ CREATE TRIGGER change_bk1 AFTER UPDATE ON b FOR EACH ROW BEGIN if(old.bk not in (select bk from b)) then delete from bkind where bkn = old.bk; end if; END$
总结:问题是不难,找对路是关键。