Composite primary key plus a separate (surrogate) id column for foreign key references

candidate-keydatabase-designerdprimary-keysurrogate-key

I am trying to build a database about locations like so:

  • Country → Region → Town

Countries will be populated with a list of pre-defined worldwide countries, regions & towns are input by users.

Region may not apply to every location and so a blank string will be allowed (hence the many to many link table).

This is the best design I have come up with:

ER Diagram

The town_id column is set to AUTO_INCREMENT & UNIQUE. It is then used as a foreign key reference in other tables.

The town column on its own is not UNIQUE as you may have the same town name in multiple places.

Is the towns table correct?

The reason I ask is because it just seems odd to me to have the town_id as the reference, however this cannot be included or used as the primary key (because if it did, it would cause duplicates)?

I would welcome feedback if this approach is correct.

Best Answer

I would recommend using it (town_id) as the only primary key (provided my first comment is true about the increment and uniqueness), and the Region/Country IDs should only be foreign keys. This would allow you to have duplicate town names.

You might want to put a unique index on country_id, region_id, town, as I'm pretty sure there's no political subdivision that has two towns of the same name, and it would reflect that business logic.