MySQL Trigger Validate Before Insert I am new to MySQL TRIGGER. I want to know is it possible to check before insert using last record found?

Table AccountChecking

 ====================================================
 || ID ||  ACID  || Debit  ||    Credit   ||  Saldo ||
 ====================================================
 || 1  ||   10    || 0      ||      0     ||  100   ||
 || 2  ||   20    || 20     ||      0     ||  120   ||
 || 3  ||   30    || 0      ||      20    ||  100   ||
 ====================================================


So, what I want trying to do in Mysql is : check the inserted last Saldo with ( Debit or Credit and Saldo ) addition to Table AccountChecking , if not match the MySQL itself reject the inserted Query.

This is my Query :

DELIMITER $$

CREATE 
    TRIGGER `DB`.`balanceCheck` BEFORE INSERT 
    ON `DB`.`AccountChecking`
    FOR EACH ROW BEGIN
        IF(NEW.Saldo != (SELECT (SALDO+NEW.Debit-NEW.Credit) FROM AccountChecking WHERE ACID='10' ORDER BY ID DESC)) 
        THEN 
            signal SQLSTATE '00000' SET message_text = "no no no DONT INSERT !";
        END IF;
    END$$

DELIMITER ;
I think you are on the right track. One thing though is that you need to handle the case when there is no match. Assuming someone adds a row like:

(4, 40, 0, 0, 1000000000)


Your IF statement will evaluate to:

IF ( 1000000000 != null ) THEN ...
IF ( null ) THEN ...


i.e. this will be allowed.

If you add a create table statement and some insert statements, together with info on whether they should be allowed or not I'm pretty sure someone can come up with a suggestion.