Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to store weekly event in MySQL?

I have a table of weekly events that run on certain days of the week (e.g. MTWTh, MWF, etc.) and run on a certain time (e.g. 8am-5pm). What's the best way to store day of week information in MySQL to make retrieving and working with the data easiest? My CakePHP app is going to need to retrieve all events happening NOW().

For time of day, I would just use TIME. For days of the week, I had considered a 7-bit bitfield, a varchar ("MTWThFr" type deal) for the days of the week, but both of those seem like clunky solutions (the varchar being clunkier).

Any suggestions?

like image 956
erjiang Avatar asked May 14 '10 17:05

erjiang


People also ask

How can I get current week record in MySQL?

WEEK() function in MySQL is used to find week number for a given date. If the date is NULL, the WEEK() function will return NULL. Otherwise, it returns the value of week which ranges between 0 to 53. The date or datetime from which we want to extract the week.

How do I create a scheduled event in MySQL?

Creating new MySQL events First, specify the name of the event that you want to create the CREATE EVENT keywords. The event names must be unique within the same database. Second, specify a schedule after the ON SCHEDULE keywords. Third, place SQL statements after the DO keyword.

How do I show events in MySQL?

To see events for a specific schema, use the FROM clause. For example, to see events for the test schema, use the following statement: SHOW EVENTS FROM test; The LIKE clause, if present, indicates which event names to match.

What is on completion preserve?

Normally, once an event has expired, it is immediately dropped. You can override this behavior by specifying ON COMPLETION PRESERVE . Using ON COMPLETION NOT PRESERVE merely makes the default nonpersistent behavior explicit. You can create an event but prevent it from being active using the DISABLE keyword.


3 Answers

Here's a straightforward way:

EventID Title    Mon Tue Wed Thu Fri Sat Sun BeginningDate EndDate
1       MyEvent  0   0   0   1   0   0   0   14-01-2010    14-01-2033

How to use:

Simply set a 1 on the days you want to run it. Since the 7-days calendar is not likely to change any time soon, that structure should be immutable. You can choose any combination of days.

To recap:

Run every Thursdays:

EventID Title    Mon Tue Wed Thu Fri Sat Sun BeginningDate EndDate
1       MyEvent  0   0   0   1   0   0   0   14-01-2010    14-01-2033

Run every Thursdays & Mondays:

EventID Title    Mon Tue Wed Thu Fri Sat Sun BeginningDate EndDate
1       MyEvent  1   0   0   1   0   0   0   14-01-2010    14-01-2033

Further more, you get only one row per event schedule, which is easier and cleaner to handle programmatically.

For example, to find all events to be executed on monday, do:

select * from Events where Mon = 1
like image 96
Wadih M. Avatar answered Nov 15 '22 21:11

Wadih M.


Can you add an DayOfWeek column in your table and make it an int? Valid values for that would be 1 thru 7. You could add a constraint on that to enforce that rule. For time, how about a BeginTime columns and an EndTime column? They would be int's as well 0-24

For an event at 5:00 pm on Monday would look like this in your table

Event_ID DayOfWeek BeginTime EndTime
1        2         1700      1800
like image 35
Billy Coover Avatar answered Nov 15 '22 20:11

Billy Coover


Why not have several lines, each line having only one column containing the day of week. This column would be just a simple :

ENUM("Monday", "Tuesday", ...)

Then, in PHP you could use date and strtotime functions to get the name of the day :

echo date('l');
echo date('l', strtotime('mon'));
// Outputs "Monday"

It is way more readable to store the name of the day.

like image 38
mexique1 Avatar answered Nov 15 '22 21:11

mexique1