fdsfdsgf
fdsfdsgf

Reputation: 35

How would I describe a list of multiple cars in a specific garage?

If I have 2 tables: Cars and Garages how would I contain a car list in a specific garage if I can't have multiple entries of carIds in a 'CarList' column.

Upvotes: 0

Views: 54

Answers (2)

netexile
netexile

Reputation: 16

You could create a third table called something like GarageCarList which would contain the CarId and GarageId columns then do a Join on the other two tables

Upvotes: 0

Bohemian
Bohemian

Reputation: 425063

Since a car can't be in multiple garages at once, but multiple cars can be in the one garage, you have an optional many-to-one relationship between car and garage, ie a foreign key:

create table garage (
    id int,
    ...
)
create table car (
    ...
    garage_id int -- nullable
)

To display a list, write a query using group_concat in MySQL for example.

Storing a "list" of cars in garage is a DB design anti-pattern (1NF).

Upvotes: 1

Related Questions