MYSQL:与几个可能的父母之一的表

I'm going to keep this as broad as possible because I don't want to prejudice the answers.

I have three tables. Campuses, Buildings and Rooms.

Some of the campuses have many buildings and the buildings have many rooms: campuses->buildings->rooms

Some of the campuses are a single building and I would like the relationship to be: campuses->rooms (no building involved).

Said another way, some campuses have buildings, some campuses have rooms. All buildings have rooms. Some rooms belong to a campus, some rooms belong to a building.

What would be the best way to model this in MySQL? Or in general?

I can provide more information on the database structure as needed.

The best way to handle this is to stick with your formulation:

  • Campuses consist of buildings
  • Buildings consist of rooms

Some campuses have multiple buildings. Some campuses have only one building. No problem. In fact, for the one-building campus, the building name might be really, really similar to the campus name.

All campuses have rooms, and these rooms are accessed by looking at the buildings on the campus.

The rest of the data model with the primary and foreign key references follows from this simplification.