I have the following simple table:
Employees:
Name (VARCHAR), Increment (INT, auto increment), Uid (INT)
For example, the Uid may be composed of the Increment value concatenated with another identifier (e.g. mac address of the server).
Currently my node.js application (using node-mysql bindings) creates a new row to generate the auto increment value, then takes this value:
var first_payload = {
name: "Example"
}
connection.query("INSERT INTO employees SET ?", first_payload, function(error, result){
var auto_increment_value = result.insertId; //Capture auto increment value
});
Then I take the auto increment value, concatenate this with another identifier and insert back into the row as the Uid:
var second_payload = {
Uid: concatenated_auto_increment,
}
connection.query("INSERT INTO employees SET ? WHERE Increment = '"+auto_increment_valiue+"' ", second_payload, function(error, result){
//...
})
Is there a more efficient way of executing this operation?
Such as generate the auto increment value and then create the Uid within the same query? Or perhaps the initial query creating the auto increment could return a direct pointer to that row, to remove the need for the second query to search the table?