The idea is to have one global unique id for every entries in MySQL tables. So instead of using auto_increment property for primary key I would like to have some kind of global generator. Here is the first draft of the solution.
1. First step is to create a table in which the ids will be stored:
CREATE TABLE `uuid` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`stub` TINYINT(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB;
Next we have to define triggers. Lets assume that we have some tables t2 and t3. We have to create triggers on each of them. The trigger is simple as a rock:
DELIMITER $$
CREATE TRIGGER `t3_uuid` BEFORE INSERT
ON `t3`
FOR EACH ROW BEGIN
REPLACE INTO `uuid` (`stub`) VALUES (1);
SET NEW.id = LAST_INSERT_ID();
END$$
DELIMITER ;
We create the trigger t3_uuid on the table t3 which will be run before each insert and will set `id` from the LAST_INSERT_ID() command.
1 comment:
Worker perfectly. Thanks
Post a Comment