You can implement the above scenario following the three steps given below :
1) First you have to enable Mysql event scheduler.
SET GLOBAL event_scheduler = 1;
2) Create a procedure in mysql that will do the borrow status update related functionalities.
delimiter //
CREATE PROCEDURE UBS()
BEGIN
UPDATE borrow set borrow_remain = borrow_remain - 1
WHERE borrow_status='borrowing';
UPDATE borrow SET borrow_status = 'finished'
WHERE borrow_remain=0;
END //
delimiter ;
3) Create an event which will be scheduled in 24 hours interval.
CREATE
EVENT UBSEvent
ON SCHEDULE EVERY 24 HOUR STARTS '2015-03-04 00:00:00'
ON COMPLETION PRESERVE
ENABLE
DO
CALL UBS();
Note :
- UBS is the procedure name.[ UBS = Update Borrow Status (just a
relevant abbreviation ]
- The event will start from tomorrow (4 March 2015) at 12:00 AM
Have a nice day!
EDIT: To increment book_count in book table whenever a borrow_status in borrow table gets updated to 'finished' you need a trigger.
Suppose you have a book table where the count of the book is denoted by book_count; Then the following trigger will do what you want.
DELIMITER $$
CREATE TRIGGER IncBookCount AFTER UPDATE ON borrow
FOR EACH ROW
BEGIN
IF NEW.borrow_status = 'finished' THEN
UPDATE book SET book_count = book_count+1;
END IF;
END $$
DELIMITER;
SUGGESTION : You need to improve your database design. Otherwise you might get more troubles in designing a complex architecture in future. Stay well!