mardi 5 mai 2015

MySQL is there a chance of accidental offset of automatic increment

If inserting into two mysql tables at the same time is there a chance that another user could insert at the same time making the tables offset?

For example hypothetically: (In php)

$conn = new mysqli($servername, $username, $password, $dbname);

$sql = "INSERT INTO Name (user_id, name, email)
VALUES (DEFAULT, 'User name', 'john@example.com')";

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}
$sql = "INSERT INTO Location (user_id, addressline1, addressline1, postcode)
VALUES (DEFAULT, '123 street', 'anytown, country', '123456')";

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();

If 'John' ran this at the same times as 'Bob' is it possible for john's Name to be given the auto-increment id of 1 and his Location to be given 2 because Bob's Location was given 1 first even though his Name was given 2.

Kinda hard to explain.

  • I want to know if 1000's if not millions of people are running this could it occur?
  • is there some reason MySQL will stop this from happening?
  • if not what can/can anything be done to prevent it?

Aucun commentaire:

Enregistrer un commentaire