Database Design – How to Store List of Unknown Size

database-design

I have trouble with database design and don't know how to overcome this. The problem is with list of cities between two chosen city. User chooses start and finish city, and OPTIONALLY, user can write which cities between he will pass, so it can be 0,1,…N. How can I store this in my database ? I already have table with list of States and another table with list of cities.

I thought a bit, and if there is no better option, I will have row in which I will store IDs of cities in format "ID1,ID2,ID3,…" and in software I will separate IDs, but I think this is not normal.

Best Answer

You should create another table with three column

  • Start City [PK]
  • End City [PK]
  • Midway City [PK]

you have triple primary key now if you have city_start and city_finish and for example 4 cities between city_start and city_finish then you should add 4 rows to you table like this

(city_start,city_finish,midway_city_one)
(city_start,city_finish,midway_city_two)
(city_start,city_finish,midway_city_three)
(city_start,city_finish,midway_city_four)

now you know that from city_start to city_finish you will visit midway_city_one