sql的关键语法,,如创建 存储过程,触发器,等代码怎么写?
- 提问者网友:我的未来我做主
- 2021-08-22 00:23
- 五星知识达人网友:摆渡翁
- 2021-08-22 00:35
- 1楼网友:鱼芗
- 2021-08-22 01:05
我就把上面的写的解释一片给你听:
存储过程:DELIMITER $$
DROp PROCEDURE IF EXISTS `shopping`.`getfreight`$$ //如果存在存储过程shopping`.`getfreight就删除
CREATE PROCEDURE `shopping`.`getfreight`(INorderitemsid int) //创建存储过程shopping`.`getfreight
BEGIN
DECLARE productid int; //DECLARE 关键声明变量
DECLARE number int;
DECLARE weight double;
DECLARE price double;
DECLARE freightmoney double;
DECLARE freightmeikg double;
select goodsid into productid from orderformitems where orderformitemsid=INorderitemsid;
select goodsnumber into number from orderformitems where orderformitemsid=INorderitemsid;
select freighteverykg into freightmeikg from orderformitems where orderformitemsid=INorderitemsid;
select goodsweight into weight from goods where goodsid=productid;
select goodsprice into price from goods where goodsid=productid;
update orderformitems set freight=number*weight*freightmeikg where orderformitemsid=INorderitemsid;
select freight into freightmoney from orderformitems where orderformitemsid=INorderitemsid;
update orderformitems set goodsallprice=number*price+freightmoney where orderformitemsid=INorderitemsid;
//上面都是一些sql语句....
END$$
DELIMITER ;
触发器:
DELIMITER $$
//如果存在触发器shopping`.`deleteorderformitemstrigger就删除
DROP TRIGGER `shopping`.`deleteorderformitemstrigger`$$
//TRIGGER 关键字创建触发器,他这里创建的是删除的触发器
CREATE TRIGGER `shopping`.`deleteorderformitemstrigger` AFTER DELETE on `shopping`.`orderform`
FOR EACH ROW BEGIN
delete from orderformitems where orderformid=old.orderformid;
delete from orderformindex where orderformid=old.orderformid;
END$$
DELIMITER ;
- 2楼网友:过活
- 2021-08-22 00:45
存储过程:DELIMITER $$
DROp PROCEDURE IF EXISTS `shopping`.`getfreight`$$
CREATE PROCEDURE `shopping`.`getfreight`(INorderitemsid int)
BEGIN DECLARE productid int; DECLARE number int; DECLARE weight double; DECLARE price double; DECLARE freightmoney double; DECLARE freightmeikg double;
select goodsid into productid from orderformitems where orderformitemsid=INorderitemsid;
select goodsnumber into number from orderformitems where orderformitemsid=INorderitemsid;
select freighteverykg into freightmeikg from orderformitems where orderformitemsid=INorderitemsid; select goodsweight into weight from goods where goodsid=productid; select goodsprice into price from goods where goodsid=productid; update orderformitems set freight=number*weight*freightmeikg where orderformitemsid=INorderitemsid;
select freight into freightmoney from orderformitems where orderformitemsid=INorderitemsid; update orderformitems set goodsallprice=number*price+freightmoney where orderformitemsid=INorderitemsid; END$$
DELIMITER ;
触发器:
DELIMITER $$
DROP TRIGGER `shopping`.`deleteorderformitemstrigger`$$
CREATE TRIGGER `shopping`.`deleteorderformitemstrigger` AFTER DELETE on `shopping`.`orderform` FOR EACH ROW BEGIN delete from orderformitems where orderformid=old.orderformid; delete from orderformindex where orderformid=old.orderformid; END$$
DELIMITER ;
以上是我做的一个项目的存储过程与触发器的书写,希望对你有帮助