Database triggers timed event instances +

Description: library management system, library table to borrow, fine table is fine, penalty rules table rule. When the borrow table extended for 30 days, by adding the relevant information to the fine table, and increments the value on a daily basis to a fine penalty rules.

1, built table: Key: BORROW in borrowTime library refers to the number of days already, once this value is 30 days, it will add a fine table.

fine table money means extended amount of money, the number of daily fines (only one table to keep this data) based on rule table daliyfine.

-- ----------------------------
-- Table structure for `borrow`
-- ----------------------------
DROP TABLE IF EXISTS `borrow`;
CREATE TABLE `borrow` (
  `rNo` int(6) NOT NULL,
  `bNo` int(11) NOT NULL,
  `borrowDate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `returnDate` timestamp NULL DEFAULT NULL,
  `borrowTime` int NOT NULL DEFAULT 0;
  PRIMARY KEY (`rNo`,`bNo`,`borrowDate`),
  KEY `bNo` (`bNo`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of borrow
-- ----------------------------
INSERT INTO `borrow` VALUES ('1', '10000', '2018-9-23 22:59:18', '2018-10-09 22:59:42','29');
INSERT INTO `borrow` VALUES ('2', '10001', '2018-10-09 22:59:36', '2018-10-09 22:59:48','14');


-- ----------------------------
-- Table structure for `fine`
-- ----------------------------
DROP TABLE IF EXISTS `fine`;
CREATE TABLE `fine` (
  `fNo` int(6) unsigned zerofill NOT NULL AUTO_INCREMENT,
  `rNo` int(6) NOT NULL,
  `bNo` int(11) NOT NULL,
  `fineDate` timestamp NULL DEFAULT NULL,
  `money` double(6,0) unsigned zerofill DEFAULT NULL,
  `returnFineDate` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`fNo`),
  UNIQUE KEY `fNo` (`fNo`),
  KEY `rNo` (`rNo`),
  KEY `bNo` (`bNo`),
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of fine
-- ----------------------------
INSERT INTO `fine` VALUES ('000001', '1', '10000', '2017-09-09 22:59:18', '000403', '2018-10-18 10:43:57');
INSERT INTO `fine` VALUES ('000002', '1', '10001', '2018-10-17 16:11:04', '000002', '2018-10-19 16:46:52');
INSERT INTO `fine` VALUES ('000003', '2', '10000', '2018-10-15 17:11:39', '000004', '2018-10-19 16:46:52');


-- ----------------------------
-- Table structure for `rule`
-- ----------------------------
DROP TABLE IF EXISTS `rule`;
CREATE TABLE `rule` (
  `rrNo` int(6) NOT NULL AUTO_INCREMENT,
  `limitedTime` int(11) NOT NULL DEFAULT '0',
  `daliyFine` double NOT NULL DEFAULT '0',
  `statu` varchar(10) DEFAULT NULL,
  PRIMARY KEY (`rrNo`),
  UNIQUE KEY `rrNo` (`rrNo`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of rule
-- ----------------------------
INSERT INTO `rule` VALUES ('1', '30', '0.5', 'able');

2. Analysis: borrow can update the day, to borrowTime + 1, using borrowTime after update trigger judgment is equal to 30. Once equal to 30, in which a borrow will be added to the fine. Then updated regularly fine.

Flowchart is as follows:

code show as below:

#添加触发器,当update之后大于30就将这个数据添加到fine中。
delimiter $
create trigger INSERT_TO_FINE after update on borrow for each ROW
BEGIN
DECLARE s1 double DEFAULT 0;
if (NEW.borrowTime>=30)
 then
	insert into fine(rNo,bNo,fineDate,money,returnFineDate) values(NEW.rNo,NEW.bNO,CURRENT_TIMESTAMP(),s1,NULL); 
end if;
END $
delimiter ;

#存储过程:更新fine表
DELIMITER //
CREATE PROCEDURE UPDATE_MONEY()
BEGIN
	declare s1 double;
	set s1 = (select daliyFine from rule limit 0,1);
	update fine set money = money+s1;
  END;
//
DELIMITER ;

#存储过程:更新borrow表
DELIMITER //
CREATE PROCEDURE UPDATE_BORROWTIME()
BEGIN
	update borrow set borrowTime= borrowTime+1;
  END;
//
DELIMITER ;

#开启事件
SET GLOBAL event_scheduler =on;

#事件 每天00:00:00更新borrow表(调用过程update_borrowTime)
CREATE EVENT if not exists update_borrow
ON SCHEDULE EVERY 1 DAY STARTS '2018-10-22 00:00:00'
ON COMPLETION NOT PRESERVE
ENABLE
DO
call update_borrowTime;

#事件 每天00:05:00更新fine表(调用过程update_fine)
CREATE EVENT if not exists update_fine
ON SCHEDULE EVERY 1 DAY STARTS '2018-10-22 00:05:00'
ON COMPLETION NOT PRESERVE
ENABLE
DO
call update_money;

 

 

Guess you like

Origin blog.csdn.net/sinat_38439143/article/details/83280324