Hi

I have two queries that have to run as a complete atomic unit.
The first query calculates an avaialble ID for a person.
The second query updates the database - inserting that ID.
The two queries (put together) should prevent two people from sharing the same ID
I don't want the following scenario to happen:

thread 1: next available ID = 1
thread 2: next available ID = 1
thread 1: insert ID = 1 to database
thread 2: insert ID = 1 to database

The correct order should be:
thread 1: next available ID = 1
thread 1: insert ID = 1 to database
thread 2: next available ID = 2
thread 2: insert ID = 2 to database

I thought about using locks to make sure this will work.
Am I correct in my direction?

here is my first query:

SELECT (tab1.ID) missing FROM people tab1 LEFT JOIN people tab2 ON tab1.ID + 1 = tab2.ID WHERE tab2.ID IS NULL ORDER BY missing ASC LIMIT 1;

Note that I am using aliases for the people table.

The second query (the one updating the database) is:
INSERT INTO people (ID) VALUES (...); // here the ID of the person will be (done in PHP)

Here was my best attempt to lock the table:
LOCK TABLES people WRITE, people as tab1 WRITE, people as tab2 WRITE;
this gives me a syntax error!

Here is the manual syntax:
LOCK TABLES
tbl_name [AS alias] {READ [LOCAL] | [LOW_PRIORITY] WRITE}
[, tbl_name [AS alias] {READ [LOCAL] | [LOW_PRIORITY] WRITE}] ...

Could someone tell me what is the mistake, and whether the solution I am looking for is the right one?
Should I be using a READ or WRITE lock?

thanks in advance