0

I have making room that has limit of space. In this case suppose limit = 100.

And I have total that is when user join the room then total = total + 1.

  1. If user join the room then check total <= limit.

  2. If true then user can join the room if false can't.

So what I want to do for it in Mysql is

making before insert trigger for checking room is available before user join the room.

 CREATE DEFINER = CURRENT_USER TRIGGER `relay_novel`.`RoomJoinedUsers_BEFORE_INSERT` BEFORE INSERT ON `RoomJoinedUsers` FOR EACH ROW
    BEGIN
        SET @total = 0;
        SET @limit = 0; 
        SELECT @total := total FROM roomjoinedusersInfo WHERE roomId = NEW.roomId;
        SELECT @limit := limit FROM rooms WHERE id = NEW.roomId;

        IF (@total > @limit) {
            // DON'T DO INSERT (HOW?)
        }

    END

My question is

  1. How to do not inserting data into the database in before insert trigger?

  2. Is there are better way to do it?

Please let me know if you need more info.

Thanks.

JillAndMe
  • 3,989
  • 4
  • 30
  • 57
  • 1
    https://dba.stackexchange.com/questions/30137/comparing-dates-in-a-before-insert-trigger should give you an indication how to solve your problem. – zedfoxus Apr 18 '19 at 03:17
  • 2
    Possible duplicate of [MySQL Trigger to prevent INSERT under certain conditions](https://stackoverflow.com/questions/2981930/mysql-trigger-to-prevent-insert-under-certain-conditions) – zedfoxus Apr 18 '19 at 03:18
  • @zedfoxus thanks! that helps a lot for me! but please help me about question #2. "Is there are better way to do it?" – JillAndMe Apr 18 '19 at 03:19

0 Answers0