简体   繁体   中英

mySQL , Primary key to be used as foreign key in another table

I have 3 tables

| Car........| User........| UserCar......|
|------------|-------------|--------------|
| IdCar    Pk| IdUser   PK.| UserId   FK..|
| color......| name........| CarId    FK..|

I want to create a mySQL Stored procedure to which i am passing the IdCar(Car) that already exists and the name(User) of a user to be created.

The IdUser that has been generated later need to be added to the table UserCar.

If the tables can be acessed by multiple users what is the best way? triggers or LOCK table.

If you already use a procedure, you can make the INSERT INTO user , get back the id and make your new UserCar record.

If you worry about two allocation of the same car, just use a UNIQUE constraint on UserCar.CarId and catch the error if you have a duplicate.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM