mysql触发器问题中涉及if总不能执行 20
用的navicat工具,下面是触发器内容begindeclarecounttinyint;selectcount(cno)intocountfromselected_co...
用的navicat工具,下面是触发器内容
begin
declare count tinyint;
select count(cno) into count from selected_corse;
if count>2 then
delete from xxx;
end if;
end
即查询selected_corse表中的选课人数,大于2则插入失败,由于mysql没有抛出异常功能,所以我加了一条“ delete from xxx;”xxx表实际不存在的,但结果无论人数是不是大于2,都无法插入,并且抛出[Err] 1146 - Table 'gxxk.xxx' doesn't exist 展开
begin
declare count tinyint;
select count(cno) into count from selected_corse;
if count>2 then
delete from xxx;
end if;
end
即查询selected_corse表中的选课人数,大于2则插入失败,由于mysql没有抛出异常功能,所以我加了一条“ delete from xxx;”xxx表实际不存在的,但结果无论人数是不是大于2,都无法插入,并且抛出[Err] 1146 - Table 'gxxk.xxx' doesn't exist 展开
2013-06-07
展开全部
delete from xxx;
修改为 delete from 被触发的表;
由于 MySQL 没有直接抛出异常的语句
因此这里通过在触发器里面,插入/删除自己这个表
导致 MySQL 发生异常
下面是一个例子代码:
触发器实现数据完整性的处理1
-- 货物表
CREATE TABLE Goods(
id INT,
Amount INT
);
-- 订单表
CREATE TABLE OrderDetail(
ID INT,
GoodsID INT,
Amount INT
);
-- 库存测试数据:
INSERT INTO Goods VALUES (1, 100);
要求:当订单表插入数据的时候,自动去货物表检查,是否有足够的库存
如果有,那么更新 货物表 的 库存 = 原库存 – 本次订单数量
如果库存不足,抱错返回.
DELIMITER //
CREATE TRIGGER BeforeInsertOrderDetail
BEFORE INSERT ON OrderDetail
FOR EACH ROW
BEGIN
DECLARE
v_nowCount INT;
SELECT
Amount INTO v_nowCount
FROM
Goods
WHERE
ID = new.GoodsID;
IF v_nowCount - new.Amount < 0 THEN
-- 由于 MySQL 没有直接抛出异常的语句
-- 因此这里通过在触发器里面,插入/删除自己这个表
-- 导致 MySQL 发生异常
DELETE FROM
OrderDetail
WHERE
ID = new.GoodsID;
ELSE
UPDATE
Goods
SET
Amount = Amount - new.Amount
WHERE
ID = new.GoodsID;
END IF;
END;
//
DELIMITER ;
处理前
mysql> select * from goods;
+------+--------+
| id | Amount |
+------+--------+
| 1 | 100 |
+------+--------+
1 row in set (0.00 sec)
mysql> select * from OrderDetail;
Empty set (0.00 sec)
mysql> INSERT INTO OrderDetail VALUES(1, 1, 90);
Query OK, 1 row affected (0.02 sec)
mysql> INSERT INTO OrderDetail VALUES(1, 1, 20);
ERROR 1442 (HY000): Can't update table 'orderdetail' in stored function/trigger
because it is already used by statement which invoked this stored function/trigg
er.
处理后
mysql> select * from goods;
+------+--------+
| id | Amount |
+------+--------+
| 1 | 10 |
+------+--------+
1 row in set (0.00 sec)
mysql> select * from OrderDetail;
+------+---------+--------+
| ID | GoodsID | Amount |
+------+---------+--------+
| 1 | 1 | 90 |
+------+---------+--------+
1 row in set (0.00 sec)
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询