Does "SELECT FOR UPDATE" prevent other connections inserting when the row is not present?

Viewed 17577

I'm interested in whether a SELECT FOR UPDATE query will lock a non-existent row.

Example

Table FooBar with two columns, foo and bar, foo has a unique index.

  • Issue query SELECT bar FROM FooBar WHERE foo = ? FOR UPDATE
  • If the first query returns zero rows, issue a query
    INSERT INTO FooBar (foo, bar) values (?, ?)

Now is it possible that the INSERT would cause an index violation or does the SELECT FOR UPDATE prevent that?

Interested in behavior on SQLServer (2005/8), Oracle and MySQL.

5 Answers
Related