My pleasure 🙂
I actually don't see the need for a transaction.
LAST_INSERT_ID() is maintained on a per conncetion basis, so you can always get the last (automatically generated) insert id in this fashion
3. select query to see if book title/author combination already exists
4. if not exists, insert book, SELECT LAST_INSERT_ID(), and use to add author-to-book entry
As for using a transaction, the results will remain the same in both cases (one being the book/author does not exist, and the other when they do).
No transaction
1. book/author exist? - yes
2. someone else deletes it
3. you do nothing since it existed...
a. book/author exist? - no
b. you insert and get last insert id
With transaction
1. start transaction
2. check if book/author exist? - yes
3. someone else wants to delete book. they have to wait
4. you don't insert anything. transaction ends.
5. that someone else gets their query through. book is still deleted.
a. start transaction
b. check if book/author eixsts? - no
c. insert and get last insert id. will always be same as without transaction.
d. end transaction
And the only time I can think of things going bad here is if you allow a user to pull up a book/author and add co-authors later on. Between those two requests, someone else might have deleted the book entirely.
But the simple use of foreign keys will prevent author_to_book from containing things not in book or not in author. So if a reference integrity fails, you can detect it this way and either just let that slide and assume that the book should indeed be deleted, or you might as well just insert it anew and generate a new id for it.