user1341873
user1341873

Reputation: 1

Mysql atomical id increment

I want to create a revision control system and the query for a new entry with revision nr 1 would be like this:

The id and revision number combined are the primary key.

insert into contentfile (id, name, revision, active) 
values ((select max(id) +1 from contentfile), 'name', 1, 1)

Will this be atomical enough or is it possible that the generated id with the subquery will concurrently be chosen by another invokation of this query?

Thanks in advance.

Upvotes: 0

Views: 57

Answers (1)

Jocelyn
Jocelyn

Reputation: 11393

Don't try to re-invent something that already exists. Modify your table by adding the autoincrement option to the id field, then insert your records like this:

INSERT INTO contentfile(name, revision, active) VALUES('name', 1, 1);

The id will be automatically incremented by the MySQL server. All id values will be unique.

Upvotes: 1

Related Questions