2

So, I've got a table roughly as follows:

CREATE TABLE CUSTOMER (
    CUSTID              INT NOT NULL AUTO_INCREMENT,
    NAME                CHAR (45),
    CONSTRAINT CUSTOMER_PRIMARY_KEY PRIMARY KEY (CUSTID))
AUTO_INCREMENT = 100;

I'm auto incrementing the CUSTID so that it's possible to simply insert a name and have it created with the next available CUSTID. However, I also want to ensure that it isn't possible to set the CUSTID value to zero, either on creation of the row or on update so I've constructed the following trigger:

DELIMITER $$
CREATE TRIGGER `custid_before_insert` BEFORE INSERT ON `CUSTOMER`
FOR EACH ROW
BEGIN
    IF (NEW.CUSTID) <= 0 THEN
        SIGNAL SQLSTATE '12345'
            SET MESSAGE_TEXT = 'Check constraint on CUSTOMER.CUSTID failed';
    END IF;
END$$

CREATE TRIGGER `custid_before_update` BEFORE UPDATE ON `CUSTOMER`
FOR EACH ROW
BEGIN
    IF (NEW.CUSTID) <= 0 THEN
        SIGNAL SQLSTATE '12345'
            SET MESSAGE_TEXT = 'Check constraint on CUSTOMER.CUSTID failed';
    END IF;
END$$
DELIMITER ;

Unfortunately in my blissful ignorance of how AUTO_INCREMENT worked, I've come to the conclusion that this is the wrong way to go about this. Trying to insert a customer with no CUSTID value is tripping the trigger causing the insert to fail which I presume is due to the value being a zero before insertion when AUTO_INCREMENT assigns it a value.

Would the best way to do this really be to change the trigger to occur after the insert and delete the row or is there a better way to do this to just throw an error?

2
  • Normally you'd use a CHECK constraint for this like CHECK(custid > 0) but..."The CHECK clause is parsed but ignored by all storage engines." Unless there's a specific business reason why an ID can't be 0, there's no data integrity reason to disallow it.
    – Schwern
    Commented Nov 10, 2015 at 6:19
  • Gotta love MySQL huh? It's unfortunately my spec with this that the ID can't ever be allowed to be below 1. The original Postgres I'm porting from actually uses a check exactly as you've suggested which works fine but I'm struggling to do the same with MySQL.
    – Hexodus
    Commented Nov 10, 2015 at 6:35

2 Answers 2

2

The insert trigger is not needed.

From Auto_Increment

No value was specified for the AUTO_INCREMENT column, so MySQL assigned sequence numbers automatically. You can also explicitly assign 0 to the column to generate sequence numbers.

E.G.

create table t(id int auto_increment, primary key(id));
insert into t(id) values (0);
select id from t;
# 1

Update:

To allow the insert to complete when CUSTID is not specified,

INSERT INTO customer(name) VALUES('Chuck');

check for null in the trigger:

IF NEW.CUSTID IS NOT NULL AND NEW.CUSTID <= 0 THEN
2
  • Removing the insert trigger of course does get rid of the error, but it sort of defeats the object. I'm still able to do an insert of a CUSTID of -12 or something similar which is what I'm trying to prevent. I'm working under the assumption that the user of the database will try this sort of thing and it must be handled somehow. Any ideas?
    – Hexodus
    Commented Nov 10, 2015 at 6:30
  • You could modify your existing insert trigger to check for NULL, then when CUSTID is not specified it will get the auto_increment value, otherwise it cannot be 0 or less.
    – JRD
    Commented Nov 10, 2015 at 15:58
2

Inserting '0' into an auto-increment column causes it to increment the same as inserting NULL, so you really neither need nor want the INSERT trigger. Try it with just the UPDATE trigger.

5
  • Removing the insert trigger of course does get rid of the error, but it sort of defeats the object. I'm still able to do an insert of a CUSTID of -12 or something similar which is what I'm trying to prevent. I'm working under the assumption that the user of the database will try this sort of thing and it must be handled somehow. Any ideas?
    – Hexodus
    Commented Nov 10, 2015 at 6:30
  • Add a declarative constraint CHECK (custid >= 0). Commented Nov 10, 2015 at 6:32
  • I don't believe MySQL supports using CHECK and has no alternative :(
    – Hexodus
    Commented Nov 10, 2015 at 6:36
  • Oh, yeah, MySQL. :P I know MySQL permits declaring CHECK constraints even if it maybe still doesn't enforce them. You could try it and see what happens. Commented Nov 10, 2015 at 6:42
  • I'm an idiot. Just have your triggers check for custid < 0 instead of custid <= 0. Commented Nov 10, 2015 at 6:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.