Lego490
Lego490

Reputation: 185

Inheriting Parent Table with identifier (Postgres)

Sorry if this is a relatively easy problem to solve; I read the docs on inheritance and I'm still confused on how I would do this.

Let's say I have the parent table being car_model, which has the name of the car and some of it's features as the columns (e.g. car_name, car_description, car_year, etc). Basically a list of cars.

I have the child table being car_user, which has the column user_id.

Basically, I want to link a car to the car_user, so when I call SELECT car_name FROM car_user WHERE user_id = "name", I could retrieve the car_name. I would need a linking component that links car_user to the car.

How would I do this?

I was thinking of doing something like having car_name column in car_user, so when I create a new data row in car_user, it could link the 2 together.

What's the best way to solve this problem?

Upvotes: 0

Views: 47

Answers (1)

Vesa Karjalainen
Vesa Karjalainen

Reputation: 1107

Inheritance is something completely different. You should read about foreign keys and joins.

If one user drives only one car, but many users can drive same car, you need to build one-to-many -relation. Add car_name to your user table and JOIN using that field.

Upvotes: 1

Related Questions