mysql and trigger usage question

Posted by dhruvbird on Stack Overflow See other posts from Stack Overflow or by dhruvbird
Published on 2010-06-10T17:42:48Z Indexed on 2010/06/10 18:13 UTC
Read the original article Hit count: 280

Filed under:
|
|

I have a situation in which I don't want inserts to take place (the transaction should rollback) if a certain condition is met. I could write this logic in the application code, but say for some reason, it has to be written in MySQL itself (say clients written in different languages will be inserting into this MySQL InnoDB table) [that's a separate discussion].

Table definition:

CREATE TABLE table1(x int NOT NULL);

The trigger looks something like this:

CREATE TRIGGER t1 BEFORE INSERT ON table1
FOR EACH ROW
  IF (condition) THEN
    NEW.x = NULL;
  END IF;
END;

I am guessing it could also be written as(untested):

CREATE TRIGGER t1 BEFORE INSERT ON table1
FOR EACH ROW
  IF (condition) THEN
    ROLLBACK;
  END IF;
END;

But, this doesn't work:

CREATE TRIGGER t1 BEFORE INSERT ON table1 ROLLBACK;

You are guaranteed that:

  1. Your DB will always be MySQL
  2. Table type will always be InnoDB
  3. That NOT NULL column will always stay the way it is

Question: Do you see anything objectionable in the 1st method?

© Stack Overflow or respective owner

Related posts about mysql

Related posts about design