Mysql – How to store multiple data in a cell inside MySQL

database-designMySQL

I want to store multiple dates in a row. Each row has a unique id and each corresponding id can have multiple action dates. The number of dates may differ for each row. I have multiple ID's and each ID has multiple dates.

ID   Date1        Date2        Date3        ...          ...         ...
1    2018-05-19   2018-04-09   2018-08-09
2    2016-01-30   2017-05-04   2016-06-07   2020-05-17   2021-06-23  ...
3    2001-05-06
4    2018-11-05   2013-06-09

What is the best way to store multiple dates for a given id?

Best Answer

Unless each date is a specific property (start date, end date, due date, date created, ...) then storing them as columns is probably bad form.

It is likely that you would be better storing the dates in separate rows like so:

ItemDates
===========
ID     (PK)
ItemId 
Date

so:

ID   Item   Date
 1   1      2018-05-19   
 2   1      2018-04-09
 3   1      2018-08-09
 4   2      2016-01-30
 5   2      2017-05-04
 6   2      2016-06-07
 7   2      2020-05-17
 8   2      2021-06-23  
 9   3      2001-05-06
10   4      2018-11-05
11   4      2013-06-09

Or if the dates will be unique per item, you can do without the surrogate key (ID) and declare ItemId,Date to be the primary key.

If you are not careful this could turn into a property bag (the Entity/Attribute/Value or EAV pattern, which is often considered an anti-pattern) though. For more specific advice for your use case, you should update the question with a bit more information about what the date values represent in the system that you are modelling.