Warm tip: This article is reproduced from stackoverflow.com, please click
mysql triggers

Using trigger to make sure inserted data doesnt cross limit

发布于 2020-07-10 10:14:23

I want to compare the sum of values in table one with the limit value in table 2 . I tried with constraint or triggers but did not work any help

As in the photo below there are 2 table table 1 has 2 columns reserve and available The in these columns come from users and the it must not excees more than the limit on second table ..

As example user can enter any amount where sum of all the column must be less than the limit

As of table one i can enter value in booked column that can be between 0 to the limit in table 2 but if the limit is reached then it cannot add more rows for that element

create table booked (room_id foreign key (room_id),
   booked datetime, booked_seats int, remaining_seats);

create table rooms ( room_id primary key
   room_size int);

booked seats are done by user, i have seperate trigger for that to ensure user enters corrected, in want a trigger or something similar that will check the sum of all seats booked in the firsts table and compare with the room_size in second table. if the sum is less that the size it will edit or else return error

create trigger test after insert on booked 
begin 
if sum of all (new.booked ) where the id is same > table2.limit then
    ....
end

I used trigger to compare values but it did not work

Questioner
Deamon
Viewed
4
nbk 2020-04-20 04:12

For a quick and good answer you need more things than you provided a functioning example data for example

This Trigger will block any attempts to insert if the room_size is smaller than the the sumed up seats.

Please read the end there i explain, where you must put some work in

DELIMITER $$
CREATE TRIGGER check_roomsize_Before_Insert BEFORE insert on booked 
FOR EACH ROW
begin 
    if (SELECT SUM(booked_seats) + NEW.booked_seats FROM booked  WHERE room_id  = NEW.room_id  AND booked  = NEW.booked  GROUP BY room_id) 
        > (select room_size from rooms where rooms.room_id= new.room_id) then
        SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Roomsize too smal!';
    end if;
END$$
DELIMITER ;

Schema (MySQL v5.7)

create table rooms ( room_id int primary key,
   room_size int);

 create table booked (room_id int,
   booked datetime, booked_seats int, remaining_seats int,    CONSTRAINT fk_category
    FOREIGN KEY (room_id) 
        REFERENCES rooms(room_id));

INSERT INTO rooms VALUES ( 1,5);

DELIMITER $$
CREATE TRIGGER check_roomsize_Before_Insert BEFORE insert on booked 
FOR EACH ROW
begin 
    if (SELECT SUM(booked_seats) + NEW.booked_seats FROM booked  WHERE room_id  = NEW.room_id  AND booked  = NEW.booked  GROUP BY room_id) 
        > (select room_size from rooms where rooms.room_id= new.room_id) then
        SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Roomsize too smal!';
    end if;
END$$
DELIMITER ;

INSERT INTO booked VALUES (1,now(),3,2);
#INSERT INTO booked VALUES (1,now(),3,0);

Query #1

SELECT * FROM booked;

| room_id | booked              | booked_seats | remaining_seats |
| ------- | ------------------- | ------------ | --------------- |
| 1       | 2020-04-19 20:04:07 | 3            | 2               |

View on DB Fiddle

As you can see in the example 1 row is inserted and the second, gives an exception.

you need to improve the part where i sum up the booked seats there i make a

AND booked = NEW.booked

Because i don't know absolutely what time criteria will count to sum the complete seats number. The now timestamp makes no sense put to test the trigger i need some date.