Is there an equivalent to a grouped primary key using an InnoDB MySQL database and allowing auto-increment on the second key?
What I am trying to implement is a table to store image URL’s based on a parent object ID something like:
CREATE TABLE images (
parent_id INT(11) NOT NULL,
image_id INT(11) AUTO_INCREMENT, //will this work with a grouped primary key?
source_url VARCHAR(255) NOT NULL,
caption VARCHAR(255) NOT NULL,
) ENGINE = InnoDB;
So for MyISAM I could do something like:
PRIMARY KEY(parent_id, image_id)
[edit] Using a trigger would be something like: (sorry my SQL is not very strong)
delimiter //
DROP TRIGGER IF EXISTS test_img_trigger;
CREATE TRIGGER test_img_trigger BEFORE INSERT ON venue_images
FOR EACH ROW
BEGIN
DECLARE img_id INT UNSIGNED DEFAULT 0;
SELECT image_id + 1 INTO img_id FROM venue_images WHERE venue_id = NEW.venue_id;
UPDATE venue_images SET image_id = img_id WHERE venue_id = NEW.venue_id;
END IF;
END;//
delimiter;
In case this can help anyone else this was my solution.
I could not simply use a trigger as MySQL does not yet (or at least my version 5.1.53) support the ability to use a trigger to update the table the trigger is called on. So I created a sequence table:
And then created a trigger on the images table:
Not sure if its the optimal solution but it works and allows me to keep track of image numbers in catagories for each parent object.