-- Collection of code snippets by Arne Vajhøj 
-- (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
CREATE TABLE fun (
    id INTEGER NOT NULL,
    val INTEGER,
    PRIMARY KEY(id)
);

DELIMITER //

CREATE TRIGGER duplcheck
BEFORE INSERT
ON fun
FOR EACH ROW BEGIN
    DECLARE n INT;
    SET n = (SELECT COUNT(*) FROM fun WHERE val = new.val);
    IF(n > 0) THEN
        INSERT INTO fun VALUES('This is not an integer',NULL);  -- this will cause an error
    END IF;
END//

DELIMITER ;

INSERT INTO fun VALUES(1, 1);
INSERT INTO fun VALUES(2, 2);
INSERT INTO fun VALUES(3, 2);

SELECT * FROM fun;

DROP TABLE fun;
