All we need is an easy explanation of the problem, so here it is.
I know that what I’m asking can’t be done or shouldn’t be done, but I really don’t know how to do it. This is the problem:
2 tables:
- Drivers (id_driver, name, etc...)
- Trucks (id_truck, model, id_driver,
etc...)
The user will fill a form with the truck’s info, but a truck may have more than one driver, and thats my problem. I don’t know what to do there.
Thanks in advance.
How to solve :
I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.
Method 1
This is likely to be a many to many relationship.
create table drivers (
driver_id int primary key,
...
);
create table trucks (
truck_id int primary key,
...
);
create table drivers_trucks (
driver_id int references drivers(driver_id),
truck_id int references trucks(truck_id),
primary key (driver_id, truck_id)
);
Method 2
Swap the foreign key so that the Drivers
table has id_truck
. This way multiple drivers can be associated with the same truck.
Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0