I am having trouble creating a BEFORE INSERT trigger.
table schema:
CREATE TABLE IF NOT EXISTS `myReferenceTable` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`linkFrom` bigint(20) NOT NULL,
`linkTo` bigint(20) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `linkFrom` (`linkFrom`,`linkTo`),
KEY `linkTo` (`linkTo`)
) ENGINE=InnoDB
inserting data:
INSERT INTO `myReferenceTable` (`id`, `linkFrom`, `linkTo`) VALUES
(1, 1, 2), // allowed
(2, 2, 1); // allowed
My failed attempt at creating a BEFORE INSERT trigger which will not allow linkFrom
and linkTo
to equal each another. This table articleReferncesTable can not have any article refering to it self,
/* This fails */
create trigger myReferenceTable_noDuplicate
BEFORE INSERT
ON myReferenceTable
FOR EACH ROW
BEGIN
IF NEW.linkFrom = NEW.linkTo
insert ignore()
END IF;
END;
Example:
INSERT INTO `myReferenceTable` (`id`, `linkFrom`, `linkTo`) VALUES
(3, 1, 1), // should fail
(4, 2, 2); // should fail
the above data is not allowed. So i want this table to be a "set" table the following data is allowed:
INSERT INTO `myReferenceTable` (`id`, `linkFrom`, `linkTo`) VALUES
(3, 1, 2), // allowed
(4, 1, 3); // allowed