Consider the following example:
A MySQL table (
SELECT `id` FROM `table` WHERE `unique_id` = x; //where x is a `unique_id`
SELECT `id` FROM `table` WHERE `unique_id` = x;
SQLSTATE: Cardinality violation: 1242 Subquery returns more than 1 row
With the approach you are using, the answer is most likely that two records can be created. The first thing to do is to actually create
UNIQUE INDEX on your
unique_id column. According to your question it does not seem to have one.
That raises another question. Do you really need both an
unique_id in your table? It may be possible to use only one or the other.
If you dropped the
unique_id and relied merely on the primary key and converted that to an auto increment field, this problem would not exist.
If you want to continue with your current approach, add the unique index and then do the INSERT first.