0

let's make an example

we have a web site for a car dealer, the db has two tables

1- Person with an auto-increment ID and other useless attributes (name,surname,...)

2- Car with an auto-increment ID and an attribute ownerID that is a foreign key references Person (plus other usuless attributes)

a new client comes in the car dealer and buy a new car, now what we do is:

1- create a row in Person table

2- create a row in Car table but we don't know what is the ownerID

so my idea to do this is:

1- create a row in Person table

2- select the last insert ID in Person

3-create the row in Car table

I make these 3 statements in one single commit, but I think there is the possibility to have another insert of a new user between statement 1 and 2 that brings me to select the wrong ID

how do I proceed?

Frah
  • 1
  • 1
  • It's possible a dealer may sell a car more than once (from new and as second hand trade in) so a junction table may be more appropriate. – P.Salmon Dec 21 '21 at 11:19
  • yeah thanks, using last_insert_id() it seems work quite well. I thought there was a easy way to do it. – Frah Dec 24 '21 at 11:46

0 Answers0