I am not sure if you understand what foreign keys are used for. The foreign key basically says: "there must be an entry in the parent table for this record." You said user_id is foreign key in vehicle table , which is not clear to me.
So let's say you have a table definition as follows:
CREATE TABLE vehicles (`id_car` int, `car_model` varchar(2), `car_owner` int); CREATE TABLE users (`user_id` int, `user_name` varchar(5), `user_phone` varchar(7) , CONSTRAINT `fk_your_foreign_key` FOREIGN KEY (user_id) REFERENCES vehicles(car_owner) );
If you want to insert a new user into the table, user_id must be existing in the car_owner column in the vehicle table.
There are foreign keys for implementing business rules. Every user must be the owner of the car? Or vice versa, should every car belong to someone? If you can answer both questions without a no, then do not use foreign keys for this case. But do it if you answer โYesโ for sure.
To get the information you are looking for, simply follow
SELECT * FROM vehicles INNER JOIN users ON vehicles.car_owner = users.user_id
source share