I’m newbie with Triggers, but I’m think that I need code one for my case:
CREATE TABLE `events` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`device_id` varchar(15) DEFAULT NULL,
`event_type` varchar(15) DEFAULT NULL,
`event_value` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4987 DEFAULT CHARSET=utf8$$
CREATE TABLE `sequence` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`device_id` varchar(15) DEFAULT NULL,
`values_sum` varchar(15) DEFAULT NULL,
`event_value` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4987 DEFAULT CHARSET=utf8$$
I need when is inserted a row of events table, check if the event_type is 1 or 2
If 1 I need copy in the second table the first data and add every 1 event until
the next insert the event_type will be 2..
I don’t know how do that, any help are welcome…
Here’s some sample syntax that will help get you started.
You’ll need to figure out what you are going to store in the
values_sumcolumn of thesequencetable.Also, the trigger below is copying the
idvalue from the events table into the sequence table, and that is going to be a problem (i.e. duplicate key exception) if some other process inserts and generates new id values in thesequencetable.This example trigger will be “fired” after an insert to the events table. For any row inserted to the events table where the event_type column is a 1, then this trigger will issue a corresponding INSERT against the sequence table. Should be enough to get you started.