MySQL Forums
Forum List  »  Triggers

Autoincrement ID copied by trigger
Posted by: Dave Banthorpe
Date: March 30, 2015 01:27PM

Hi,

I've got a simply trigger that copies fields from one table to another in a different DB on the same server. Both source and destination tables use the column id as an auto-increment as the primary key.

In the trigger I explicitly exclude the source id value but for some reason when it inserts into the target table it uses the same id value as the source rather than using it's own auto-increment value. How can I stop this? FYI this is an AFTER_INSERT trigger.

BEGIN

INSERT INTO mastergps.gps
(tstamp, deviceid, lat, lng, speed, alt)

VALUES
(NEW.tstamp,NEW.radioid, NEW.lat, NEW.lng, NEW.speed, NEW.alt);

END

This is the source table:
CREATE TABLE `gps` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`tstamp` datetime DEFAULT NULL,
`radioid` int(11) DEFAULT '0',
`lat` double DEFAULT '0',
`lng` double DEFAULT '0',
`ngr` varchar(16) DEFAULT NULL,
`speed` double DEFAULT '0',
`alt` double DEFAULT '0',
PRIMARY KEY (`id`),
KEY `radioid` (`radioid`)
) ENGINE=InnoDB AUTO_INCREMENT=10647 DEFAULT CHARSET=latin1;


This is the target table:
CREATE TABLE `gps` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`tstamp` datetime DEFAULT NULL,
`deviceid` double DEFAULT '0',
`lat` double DEFAULT '0',
`lng` double DEFAULT '0',
`speed` double DEFAULT '0',
`alt` double DEFAULT '0',
PRIMARY KEY (`id`),
KEY `radioid` (`deviceid`)
) ENGINE=InnoDB AUTO_INCREMENT=10647 DEFAULT CHARSET=latin1;

Options: ReplyQuote


Subject
Views
Written By
Posted
Autoincrement ID copied by trigger
2271
March 30, 2015 01:27PM


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.