This is conceptual. Worm it in your world if it is useful.
Scheme
create table AnimalSize ( id int auto_increment primary key, animal varchar(100) not null, size varchar(100) not null, unique key(animal,size)
Some queries
-- truncate table animalsize; -- clobber and reset auto_increment back to 1 insert ignore AnimalSize(animal,size) values ('snake','small'); -- id=1 select last_insert_id(); -- 1 insert ignore AnimalSize(animal,size) values ('snake','small'); -- no real insert but creates id GAP (ie blows slot 2) select last_insert_id(); -- 1 insert ignore AnimalSize(animal,size) values ('snake','small'); -- no real insert but creates id GAP (ie blows slot 3) select last_insert_id(); -- 1 insert ignore AnimalSize(animal,size) values ('frog','medium'); -- id=4 select last_insert_id(); -- 4 insert ignore AnimalSize(animal,size) values ('snake','small'); -- no real insert but creates id GAP (ie blows slot 3) select last_insert_id(); -- 4
Note: insert ignore says it does this, and ignores the fact that it could die. In our case, this may fail due to a unique key (which is good). In general, do not use insert ignore unless you know what you are doing.
This is often talked about in conjunction with the insert on duplicate key update (IODKU) call. Or should I think about how, as in, How can I solve this current predicament. But that (IODKU) will be stretched in this case. However, keep both in your solution tool.
After the insert ignores fire, you know, anyway, that the line is there.
Forgetting the INNODB GAP aspect, the above assumes that if the string already exists before ignoring the insert, then
You cannot rely on last_insert_id() for id
So, after disabling ignoring the insert, go and enter the identifier, which, as you know, should be there. Use this in subsequent calls against CountryAnimalSize
continue this line of reasoning for the tables in the CountryAnimalSize table where the row may or may not be there.
There is no reason to formalize the solution here, because, as you say, these are not even your tables in question.
Also, return to INNODB GAP . Google it. Find out if you can live with the spaces created.
Most people have large fish to fry, while maintaining tight and alkaline clothes.
Other people (read: OCD) are so obsessed with the perceived burst problem that they explode on it for days.
So, these are general comments intended to help a wider audience than the answers to your question, which, as you say, is not even your outline.