Search code examples
mysqlphpmyadmindatabase-trigger

SQL delete rows when no more row on other table


I'm trying to create a trigger on phpmyadmin. I want to delete all rows from table NEXT_GAME as soon as there is no more row in WAITING_ROOM.

I tried the following solution :

CREATE TRIGGER delete_begin_date AFTER DELETE
ON NEXT_GAME FOR EACH ROW
BEGIN
    IF NOT EXISTS (SELECT * FROM WAITING_ROOM) THEN
        DELETE FROM NEXT_GAME;
    END IF;
END

I got the following error :

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 5

What did I do wrong ?


Solution

  • You need a specific delimiter to tell MySQL that the next ; doesn't ends the CREATE TRIGGER statement.

    DELIMITER $$
    CREATE TRIGGER delete_begin_date AFTER DELETE
    ON NEXT_GAME FOR EACH ROW
    BEGIN
        IF NOT EXISTS (SELECT * FROM WAITING_ROOM) THEN
            DELETE FROM NEXT_GAME;
        END IF;
    -- vv----------------------- Notice this
    END$$
    DELIMITER ; -- switch back to the regular ;