Database Design – How to Design a Table for Holidays

databasedatabase-design

I'm creating a system to store holidays and events. I have a basic understanding of the schema for the static dates (every 23rd of march for instance).

My trouble lies with the relative dates (i.e. : every 3rd friday of march).

For the static dates we can safely have a schema that looks like this :

+----------+--------------+
| Col Name |     Type     |
+----------+--------------+
| Id       | Int          |
| Name     | varchar(255) |
| Month    | Int          |
| Day      | Int          |
+----------+--------------+

How would it be possible to add the relative dates in this schema ?

Best Answer

What you have is not flexible enough.

The basic requires are you need to be able to express e.g. the following:

  • U.S. Memorial Day is the last Monday of May.
  • U.S. Thanksgiving is the fourth Thursday of November.
  • Christmas is the 25th day of December.
  • Easter is... maybe just scrape Wikipedia?

One simple way to express this is to add fields for the possible ways to define holidays:

  • Month
  • Day of Week
  • Week of Month (use negative to count backward for e.g. Memorial Day)
  • Day of Month

This is not ideal because one can easily put bad data in (e.g. Monday, first week, and 25th day), but it is simple and easy to understand.

There are other ways to define holidays that I have seen elsewhere, but they basically require implementing a rules engine which is very complex and easy to misuse. These behemoths have a bad reputation that is well-earned, but sometimes they really are the best tool for the job.

Related Topic