I want to do something like this:
SELECT * FROM TABLE where *condition*
... TEST for a row being returned
IF NOT
INSERT the row
In other words I only want to insert a row into the table if it is not already there. My concern is that while I am testing the result set the row could in fact be inserted by another process. I won't know this and two rows will get inserted. I don't want this to happen.
I thought of wrapping the two statements in a transaction but I suspect this isn't the answer; I don't know if transactions have an effect similar to locking the table while they are being executed thus preventing any other inserts between my SELECT and INSERT? This is the question.
I suspect the best way to do it is simply to set a unique key across the columns in the row and so prevent duplicate inserts. But I still wonder if my idea about using transactions is at all valid or wildly off?