简体   繁体   中英

The correct database schema for one-to-many relationship

I'm building a hobby site about cars. If a user clicks on a car I want to display a list of similar cars for the chosen car.

I have a main table witch stores the basic information about each car, CarDataMain, two of the columns in the table are CarID (Pk) and SimilarCarsID (Fk).

I have another table called “SimilarCars”, it has three columns; SimilarCarsID (Pk), CarGroupID and CarID.

So the SimilarCarsID-column in the SimilarCars-table has a relationship with the column SimilarCarsID in the CarDataMain-table.

Is this the correct approach, or “best practice”?

Each car can only belong to one CarGroup (CarGroupID).

Another solution would be to create a third table witch holds the relationship between CarDataMain and SimilarCars-data, but as there is a one-to-many relationship I guess this is over kill? That way I could however put the same foreign key-value in CarDataMain for all cars witch belong to the same CarGroup, witch somehow feels appealing…

A third solution would be to skip the SimilarCarsID column in CarDataMain and make the CarID a foreign key in the SimilarCars table, if you understand what I mean. But I guess there are some drawbacks with that solution…

Sorry for newbie questions (and if this question has been up before), but I want to get it right before I start :)

Ludde

Note: re-written

  • Design tables that describe things like Car , Group , others?
  • Use "join tables" to give the desired one-to-many relationships.

Tables

  • CarID - primary key

Group

  • GroupID - primary key.
  • Name - the name of the group.

The GroupID may not be necessary. If Name is unique then use that as the primary key.

CarGroup

  • CarID - foreign key to Car Table
  • GroupID - foreign key to Group table

This is a join table. Here you can map a given car to many groups, and map a given group to many cars.

SimilarCar

I may be misunderstanding, but is seems like you want to match cars as similar, for no particular reason. If you want to match by group, that is above; and if you want "these 2 cars are similar, period", here you go. This is not mutually exclusive to the groups.

  • CarID - FK to Car table
  • SimilarCarID - Fk to Car table

A constraint so CarId cannot match SimilarCarID -- unless you do want a car to be similar to itself. But if so then just read the car table instead.

I think your concepts need to be cleared up first. Isn't a "similar car" a car that is in the same "group"? If so, you only need 1 term. Since "group" is a reserved word it is better to use something else.

I would go with the term "category". So my Cars table would have a CategoryId column. Picture the page on the front-end where someone would add a new car, wouldn't they have a drop down list to select the Category?

So my model would be:

Cars
-Id (PK)
-Name
-CategoryId  (FK to Categories)
...

Categories
-Id (PK)
-Name

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