I have a MySQL table that has a two column primary key, the latter of which was auto increment when it used to be MyISAM. When I switched it to InnoDB, I had to convert the auto_increment column to a normal int that updated itself to max()+1 with a before insert trigger. No problems so far..
The Java PreparedStatement that does the insert needs to know the last_insert_id of the row that was just inserted, and without the RETURNING function of Oracle/etc. I'm not sure how best to retrieve that value. Is there a way to get that emulated auto increment value without wrapping all of my insert functions into a stored function or requiring a second query?
Just for reference, something along the lines of (just writing this out, so might not be perfect, but it should get the idea across):
CREATE TABLE t ( id1 int NOT NULL, id2 int NOT NULL DEFAULT '0', PRIMARY KEY (id1, id2) ) ENGINE=InnoDB; DELIMITER $$ CREATE TRIGGER t_auto_increment BEFORE INSERT ON t FOR EACH ROW BEGIN SET NEW.id2 = (SELECT MAX(id2)+1 FROM t WHERE id1=NEW.id1); END$$ DELIMITER ;
I know with other DB engines I can just tell it to return id2 with the insert query, which I assume would solve my problem, but MySQL doesn't seem to support this. I'd rather not make what would be a number of stored functions to wrap every insert, so the best I thought of was setting a variable and looking for it afterwards.. though I'd much prefer something that doesn't require a second query to the server, of course, despite said query would be light.