0
I’m trying to create a simple chat system using database but want to automatically when it reaches a limit of Rows in the database it automatically run the truncate.
The Trigger code I made was this:
BEGIN
SELECT COUNT(*) INTO @a FROM sandbox_webchat;
IF @a >= 120 THEN
TRUNCATE sandbox_webchat;
END IF
END
However he accuses IF and END IF as a syntax error, I even tested with parentheses but still he accuses error.
IF (@a >= 120) THEN ... END IF
One or more errors have occurred while Processing your request: The following query has failed: "CREATE TRIGGER
QuandoAtingirLimite
BEFORE INSERT ONsandbox_webchat
FOR EACH ROW BEGIN SELECT COUNT(*) INTO @a FROM sandbox_webchat; IF @a >= 120 THEN TRUNCATE sandbox_webchat; END IF END"Mysql said: #1064 - 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 'END' at line 6
In a chat, where the server can easily receive multiple messages per second, running a Count at all of these times is a bad idea (it will waste a lot of processing, overload the table, slow down the exchange of msgs in the chat). If you want to ensure storage space by cleaning old messages, consider creating a periodic event (run daily or in another specific time interval)
– rodorgas
hm. thanks for the tip, but the IF still gives error. phpmyadmin tells me to check the syntax
– FRNathan13
In fact, you do not have this "IF" statement in mysql. What you have is
SELECT IF
, has a look at the documentation http://dev.mysql.com/doc/refman/5.7/en/control-flow-functions.html– rodorgas
Vish and now how I will create this condition.
– FRNathan13