MySQL Forums
Forum List  »  Triggers

Before Insert Triger and Last_Insert_ID()
Posted by: uwe resch
Date: May 25, 2011 12:41AM

my problem is simple i think but i dont know how to solve it.

I have 2 differen mysql server:

Server1 127.0.0.1 :

CREATE TABLE `t1` (
`a` bigint(20) NOT NULL AUTO_INCREMENT,
`b` varchar(255) DEFAULT NULL,
PRIMARY KEY (`a`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1

CREATE TABLE `t2` (
`a` bigint(20) NOT NULL AUTO_INCREMENT,
`b` varchar(255) DEFAULT NULL,
PRIMARY KEY (`a`)
) ENGINE=FEDERATED DEFAULT CHARSET=latin1 CONNECTION='mysql://root:pass@192.168.0.2:3306/test/t1'

CREATE
TRIGGER `t1_BI` BEFORE INSERT ON `t1`
FOR EACH ROW BEGIN
INSERT INTO t2(b)
VALUES(new.b);
SET new.a= (SELECT a FROM t2 WHERE a=LAST_INSERT_ID());
END IF;
END;

Server2 192.168.0.2:

CREATE TABLE `t1` (
`a` bigint(20) NOT NULL AUTO_INCREMENT,
`b` varchar(255) DEFAULT NULL,
PRIMARY KEY (`a`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1

Insert into t1(b)Values('xxx');

Select Last_Insert_ID() -> 1 --thats still ok

now back to server1:

Insert into t1(b)Values('yyy');

now there is a problem the insert is ok with a=2 b=yyy
Auto_increment is 3 now thats ok too but:

Select Last_Insert_ID() -> 0 ?????

Why is the Last_Insert_ID not 2 ?

Options: Reply• Quote


Subject
Views
Written By
Posted
Before Insert Triger and Last_Insert_ID()
3377
May 25, 2011 12:41AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.