Wednesday, September 3, 2008

Triggers wont work with jdbc queries

I wanted to execute trigger creation through ant scripts so that whole installation process will be automated. But unfortunately trigger creation script kept failing. After some googling I figured out that problem is in the command DELIMITER |. This command is one of commands available in mysql client and is not included in sql specification. Mysql default delimiter ; is used in trigger which is unfortunately mis guiding clients like jdbc query to stop at wrong delimiters, thus creating errors.
For the moment it seems like I have to invoke triggers using command line. Anyway if I found out a solution I will let you all know.

Tuesday, September 2, 2008

My first trigger...

I've been occupied with lot of work and could not find the time to sit and write some posts...
But this was sooo interesting and couldn't keep from coming here and posting it for ur knowledge...
This is the first trigger I have written.. It's something that is used to update a tables called values_table and template_table. I have changed some of the names of the table since it's from my cmpny project ;)
DELIMITER |


CREATE TRIGGER xxtriggername AFTER INSERT ON channel
FOR EACH
ROW BEGIN
INSERT INTO values_table
VALUES
(NULL , NEW.channel_id, 1, 1, 0.0),
(NULL , NEW.channel_id, 1, 2, 0.0),
(NULL , NEW.channel_id, 1, 3, 0.0),
(NULL , NEW.channel_id, 1, 4, 0.0),
(NULL , NEW.channel_id, 1, 5, 0.0),
(NULL , NEW.channel_id, 1, 6, 0.0),
(NULL , NEW.channel_id, 1, 7, 0.0),
(NULL , NEW.channel_id, 1, 8, 0.0),
(NULL , NEW.channel_id, 1, 9, 0.0),
(NULL , NEW.channel_id, 1, 10, 0.0),
(NULL , NEW.channel_id, 1, 11, 0.0),
(NULL , NEW.channel_id, 1, 12, 0.0),
(NULL , NEW.channel_id, 1, 13, 0.0),
(NULL , NEW.channel_id, 1, 14, 0.0),
(NULL , NEW.channel_id, 1, 15, 0.0),
(NULL , NEW.channel_id, 1, 16, 0.0),
(NULL , NEW.channel_id, 1, 17, 0.0),
(NULL , NEW.channel_id, 1, 18, 0.0),
(NULL , NEW.channel_id, 1, 19, 0.0),
(NULL , NEW.channel_id, 1, 20, 0.0),
(NULL , NEW.channel_id, 1, 21, 0.0),
(NULL , NEW.channel_id, 1, 22, 0.0),
(NULL , NEW.channel_id, 1, 23, 0.0),
(NULL , NEW.channel_id, 1, 24, 0.0);

INSERT INTO template_table VALUES (1,'Default template',NEW.channel_id);
END;

|
DELIMITER ;


Btw these links should help you get started if you are also new to triggers...

http://dev.mysql.com/doc/refman/5.0/en/create-trigger.html
http://www.digitalpropulsion.org/Programming/Triggers_in_MySQL_5_0