I do not know know how to exactly name this question because I'm still learning SQL. I use sqlite3 for simple tests and my problem is the following:
I have two tables with a primary key. The first table auto increments the key on insert and the second references this key. Let's say I inserted into the first table and after that I insert into the second table with the previous inserted key. Now I have a foreign key constraint. That works but let's say before I start to insert into the second table another thread deletes the key from the first table and inserts another key. This would have the same primary key integer value (I've read this in the sqlite documentation). Now the data could be inconsistent.
What is the solution for this case? Does wrapping the two inserts in a transaction help here?