You can use triggers to achieve custom auto-increment functions.
Create a table to store custom auto-increment values and insert one row with the initial values:
CREATE TABLE autoincrement_id (id_even INT, id_odd INT); INSERT INTO autoincrement_id VALUES (0, 1);
Create triggers that change the row id value accordingly:
CREATE TRIGGER set_id_in_sometable_with_odd_id BEFORE INSERT ON `sometable_with_odd_id` FOR EACH ROW BEGIN SET NEW.id = (SELECT id_odd FROM autoincrement_id LIMIT 1); UPDATE autoincrement_id SET id_odd = id_odd + 2; END; CREATE TRIGGER set_id_in_sometable_with_even_id BEFORE INSERT ON `sometable_with_even_id` FOR EACH ROW BEGIN SET NEW.id = (SELECT id_even FROM autoincrement_id LIMIT 1); UPDATE autoincrement_id SET id_even = id_even + 2; END;
Ruben source share