What is the best way to generate a manually int primary key with mysql?
I dont want to use the autoincrement feature.
I taught to create a table with one column of type int named sequence. So every time I’m going to insert a new record first check this column and use the value as the primary key. Then after the insert is successful increment the value of sequence.
Is this a good aproach?
Create a table with one int column of type InnoDB:
Then when you want to get a new value for the key you must start a transaction (using the API you are using) and then:
Then read the value returned, it is the value for your primary key. Then to increment the value while still in the same transaction execute:
Then commit your transaction.
This will ensure complete consistency if and only if you use transaction as described above and you use the InnoDB table type.