/**
 * qa_answer相关触发器
 * create time: 2011-5-27 上午11:04:18
 * @version $Id: db.trigger.sql 30 2011-06-05 02:36:00Z liqt $
 * @author LiQintao
 */
DELIMITER |

DROP TRIGGER IF EXISTS t_answer_insert_after |

CREATE TRIGGER t_answer_insert_after AFTER INSERT ON qa_answer
	FOR EACH ROW BEGIN
		# 添加一个答案则在相应的问题表中更新答案数量等
		UPDATE qa_question SET q_answer_quantity = q_answer_quantity+1, q_lastanswer_time =  NEW.a_answer_time, q_lastanswer_id = NEW.a_owner_id  WHERE q_id = NEW.q_id;
	END
|


DROP TRIGGER IF EXISTS t_answer_delete_after |

CREATE TRIGGER t_answer_delete_after AFTER DELETE ON qa_answer
	FOR EACH ROW BEGIN
		# 删除一个答案则在相应的问题表中更新答案数量(先判断对应问题是否存在)
		IF EXISTS(SELECT * FROM qa_question WHERE q_id=OLD.q_id) THEN
			UPDATE qa_question SET q_answer_quantity = q_answer_quantity-1   WHERE q_id = OLD.q_id;
		END IF;
	END
|