0

I have implemented a trigger. But once I try to update the table it is giving me the error:

can't update table 'booking' in stored function/trigger because it is already used by the statement which invoked this stored function/trigger

This is the trigger

DELIMITER //
CREATE TRIGGER CANCEL_BOOK AFTER UPDATE on BOOKING
FOR EACH ROW
BEGIN
DELETE FROM BOOKING
 WHERE NEW.F_ID=NULL AND NEW.H_ID=NULL;
END //
DELIMITER ;

please help

McNets
24k11 gold badges51 silver badges90 bronze badges
asked Dec 2, 2018 at 10:45
14
  • 3
    The error message explains what is wrong. This restriction cannot be bypassed. So the way you select to solve your task is wrong. Publish/explain this task, maybe it can be solved without using a trigger. Commented Dec 2, 2018 at 14:00
  • 2
    An excellent example of an X-Y problem. Commented Dec 2, 2018 at 14:27
  • thank you @Akina but its is in my project to use a trigger and a saved procedure hence i need to use the trigger or the saved procedure my project is about flights database reservation and cancellation i've created the database and the tables are login, registration, flights and booking i have already inserted the values to the database all i'm left to do is add a trigger and a short procedure so if you can help please guide me i need help before 4th december Commented Dec 2, 2018 at 14:30
  • thank you @mustaccio..... but i dont know much about what you are telling if you can explain it will be very helpful i am very keen on learning about such stuffs Commented Dec 2, 2018 at 14:35
  • 1
    Explain the logic that you need to implement, using human language. You seem to be using inappropriate tools for the job, but we don't know what your job is. Commented Dec 2, 2018 at 15:10

2 Answers 2

1

The error occurs because your trigger tries to DELETE from the BOOKING table, which is already locked by the UPDATE statement. MySQL/MariaDB doesn't allow modifying the same table in a trigger.

A potential solution would be to use a BEFORE UPDATE trigger to prevent NULL values or raise an error:

DELIMITER //
CREATE TRIGGER CANCEL_BOOK BEFORE UPDATE ON BOOKING
FOR EACH ROW
BEGIN
 IF NEW.F_ID IS NULL AND NEW.H_ID IS NULL THEN
 SIGNAL SQLSTATE '45000'
 SET MESSAGE_TEXT = 'Cannot update row with both F_ID and H_ID as NULL';
 END IF;
END //
DELIMITER ;

Alternatively, handle the cleanup outside the trigger with a separate DELETE statement:

DELETE FROM BOOKING WHERE F_ID IS NULL AND H_ID IS NULL;

Could you clarify your goal (e.g., delete the updated row or others)? This will help refine the solution.

answered May 15 at 5:12
0

Prevent record creation with F_ID and H_ID having NULL values (needs 5.5 or newer server version):

DELIMITER @@;
CREATE TRIGGER cancel_insert_book BEFORE INSERT ON booking
FOR EACH ROW
BEGIN
 IF ((NEW.F_ID IS NULL) AND (NEW.H_ID IS NULL)) THEN
 SIGNAL SQLSTATE '23000'
 SET MESSAGE_TEXT = 'Both F_ID and H_ID cannot be NULL at the same time', 
 MYSQL_ERRNO = 1048;
 END IF;
END; 
@@;
CREATE TRIGGER cancel_update_book BEFORE UPDATE ON booking
FOR EACH ROW
BEGIN
 IF ((NEW.F_ID IS NULL) AND (NEW.H_ID IS NULL)) THEN
 SIGNAL SQLSTATE '23000'
 SET MESSAGE_TEXT = 'Both F_ID and H_ID cannot be NULL at the same time', 
 MYSQL_ERRNO = 1048;
 END IF;
END; 
@@;
DELIMITER ;

Remove record which have F_ID and H_ID having NULL values:

DELIMITER @@;
CREATE TRIGGER cancel_book AFTER UPDATE ON booking
FOR EACH ROW
BEGIN
 IF ((NEW.F_ID IS NULL) AND (NEW.H_ID IS NULL)) THEN
 CREATE EVENT remove_record
 ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 SECOND
 DO
 DELETE FROM booking
 WHERE ((f_id IS NULL) AND (h_id IS NULL));
 END IF;
END; 
@@;
DELIMITER ;
answered Dec 2, 2018 at 18:24
10
  • thank you for you time and help but I want to remove that row if it is null I don't want it to show any error message. I mean that once those 2 values is null I no longer need that record in my database I want it to be deleted Commented Dec 2, 2018 at 18:49
  • I want to remove that row WHAT ROW? This trigger do not allow to create the record which have both field with NULL values - so there is no rows to remove. Commented Dec 2, 2018 at 19:35
  • this trigger won't allow me to set h_id and f_id as null but my question is that let's say if a person has booked both flight seat and hotel room so all the values in that row will be filled now suppose the person doesn't want to go so he starts cancelling the flight ticket and the room so once he does that the values for h_id and f_id will become null so I want a trigger that will delete such rows as soon as they become null Commented Dec 2, 2018 at 20:09
  • 1
    @Akina note also that MySQL Server 5.1 does not support SIGNAL. This was not introduced until 5.5. Commented Dec 2, 2018 at 23:24
  • @pksv I want a trigger that will delete such rows as soon as they become null Updated. Commented Dec 3, 2018 at 5:17

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.