<p>I have to create something similiar to Google Calendar, so I created an events table that contains all the events for a user.</p>

<p>The hard part is handling re-occurring events, the row in the events table has an event_type field that tells you what kind of event it is, since an event can be for a single date only, OR a re-occuring event every x days.</p>

<p><strong>The main design challenge is handling re-occurring events.</strong></p>

<p>When a user views the calendar, using the month's view, how can I display all the events for the given month?  The query is going to be tricky, so I thought it would be easier to create another table and create a row for each and every event, including the re-occuring events.</p>

<p>What do you guys think?</p>

[{'answer_id': 12615, 'author': 'DevelopingChris', 'author_id': 1220, 'author_profile': 'https://Stackoverflow.com/users/1220', 'pm_score': 2, 'selected': False, 'text': "<p>I would say start with the ical standard. If you use it as your model, then you'll be able to do everything that google calendar, outlook, mac ical (the program), and get virtually instant integration with them. </p>\n\n<p>From there, time to bone up on your ajax and javascript cuz you can't have a flashy web ui with drag drop and multiple calendars without a ton of ajax and javascript.</p>\n"}, {'answer_id': 12616, 'author': 'Matt Miller', 'author_id': 763, 'author_profile': 'https://Stackoverflow.com/users/763', 'pm_score': 0, 'selected': False, 'text': '<p>I think I understand your second paragraph to mean you are considering a second events table that has a row for each occurrence of an event. I would avoid that.</p>\n\n<p>Re-occurring events should have a start date and a stop date (which could be Null for events that continue every X days "forever") You\'ll have to decide what kinds of frequency you want to allow -- every X days, the Nth day of each month, every given weekday, etc.</p>\n\n<p>I\'d probably tend toward two tables - one for one time events and a second for recurring events. You\'ll have to query and display the two separately.</p>\n\n<p>If I were going to tackle this (and I\'d try as hard as I can to avoid reinventing this wheel) I\'d look for open-source libraries or, at the very least, open source projects with Calendars that you can look at. Any recommendations guys?</p>\n'}, {'answer_id': 12618, 'author': 'GateKiller', 'author_id': 383, 'author_profile': 'https://Stackoverflow.com/users/383', 'pm_score': 1, 'selected': False, 'text': '<p>From past experience I would create a new record for each occurring event and then have a column which references the previous event so you can track all events in a series.</p>\n\n<p>This has two advantages:</p>\n\n<ol>\n<li>No complicated routines to work out the next event date</li>\n<li>Individual occurrences can be edited without effecting the rest</li>\n</ol>\n\n<p>Hope this gives you some food for thought :)</p>\n'}, {'answer_id': 12621, 'author': 'Darren Kopp', 'author_id': 77, 'author_profile': 'https://Stackoverflow.com/users/77', 'pm_score': 2, 'selected': False, 'text': '<p>You should have a start date, end date, and expiration date. Single day events would have the same start date and end date, and allows you to do partial day events as well. As for re-occuring events, then the start and end date would be for the same day, but have different times, then you have an enumeration or table that specifies the repeat frequency (daily, weekly, monthly, etc).</p>\n\n<p>This allows you to say "this event appears every day" for daily, "this event appears on the 2nd day of every week" for weekly, "this event appears on the 5th day of every month" for monthly, "this event appears on the 215th day of every year" for yearly as long as the date is less than the expiration date.</p>\n'}, {'answer_id': 12622, 'author': 'James A. Rosen', 'author_id': 1190, 'author_profile': 'https://Stackoverflow.com/users/1190', 'pm_score': 1, 'selected': False, 'text': "<p>I have to agree with @ChanChan on reading the ical spec for how to store these things.  There is no easy way to handle recurrences, especially ones that have exceptions.  I've built and rebuilt and rebuilt a database to handle this, and I keep coming back to ical.</p>\n\n<p>It's not a bad idea to create a subordinate table, depending on use cases.  The algorithm for calculating exactly when occurrences . . . um, occur . . . can indeed be quite complex.  There's no getting away from running it, but it's worth considering caching the results.</p>\n"}, {'answer_id': 12627, 'author': 'Matt Miller', 'author_id': 763, 'author_profile': 'https://Stackoverflow.com/users/763', 'pm_score': 1, 'selected': False, 'text': '<p>@GateKiller</p>\n\n<p>I hadn\'t thought of the case where you edit individual occurrences. It makes sense you would store the occurrences separately in that case.</p>\n\n<p>When you do that, though, how far in the future do you store events? Do you pick an arbitrary date? Do you auto-generate the new occurrences the first time a user browses out into future months/years?</p>\n\n<p>Also, how do you handle the case where the user wants to edit the whole series. "We\'ve had a meeting every Tuesday morning at 10:30 but we\'re going to start meeting on Wednesday at 8"</p>\n'}, {'answer_id': 12629, 'author': 'public static', 'author_id': 1368, 'author_profile': 'https://Stackoverflow.com/users/1368', 'pm_score': 2, 'selected': False, 'text': '<p>Darren,</p>\n\n<p>That is how I have designed my events table actually, but when thinking about it, say I have 100K users who have create events, this table will be hit pretty hard, especially if I am going to be sending out emails to remind people of their events (events can be for specific times of the day also!), so I could be polling this table every 15 minutes potentially!</p>\n\n<p>This is why I wanted to create another table that would expand out all the events/re-occuring events, this way I can simple hit that table and get the users months view of events without doing any complicated querying and business logic, AND it will make polling much more effecient also.</p>\n\n<p>The question is, should this secondary table be for the next day or month?  What makes more sense?  Since the maximum a user can view is a months view, I am leaning towards a table that writes out all the events for a given month.</p>\n\n<p>(ofcourse I will have to maintain this secondary table for any edits the user might make to the original events table).</p>\n\n<p>ChanChan,</p>\n\n<p>I have designed it with the same sort of functionality actually, but I am just referring to how I will go about storing events, specifically how to handle re-occurring events.</p>\n'}, {'answer_id': 12675, 'author': 'Derek Park', 'author_id': 872, 'author_profile': 'https://Stackoverflow.com/users/872', 'pm_score': 2, 'selected': False, 'text': '<p>Attempting to store each instance of every event seems like it would be really problematic and, well, impossible.  If someone creates an event that occurs "every thursday, forever", you clearly cannot store all the future events.</p>\n\n<p>You <em>could</em> try to generate the future events on demand, and populate the future events only when necessary to display them or to send notification about them.  However, if you are going to build the "on-demand" generation code anyway, why not use it all the time?  Instead of pulling from the event table, and then having to use on-demand event generation to fill in any new events that haven\'t been added to the table yet, just use the on-demand event generation exclusively.  The end result will be the same.  With this scheme, you only need to store the start and end dates and the event frequency.</p>\n\n<p>I don\'t see any way that you can avoid having on-demand event generation, so I can\'t see the utility in the event table.  If you want it for the sake of caching, then I think you\'re taking the wrong approach.  First, it\'s a poor cache because you can\'t avoid on-demand event generation anyway.  Second, you should probably be caching at a higher level anyway.  If you want to cache, then cache generated pages, not events.</p>\n\n<p>As far as making your polling more efficient, if you are only polling every 15 minutes, and your database and/or server can\'t handle the load, then you are already doomed.  There\'s no way your database will be able to handle users if it can\'t handle much, <em>much</em> more frequent polling without breaking a sweat.</p>\n'}, {'answer_id': 12772, 'author': 'Steve T', 'author_id': 415, 'author_profile': 'https://Stackoverflow.com/users/415', 'pm_score': 3, 'selected': False, 'text': '<p>As previously stated, <strong><em>don\'t reinvent the wheel</em></strong>, just enhance it.</p>\n\n<p>Checkout <a href="http://www.ultraapps.com/app_overview.php?app_id=19" rel="nofollow noreferrer">VCalendar</a>, it is open source, and comes in PHP, ASP, and ASP.Net (C#)!</p>\n\n<p>Also you could check out <a href="http://www.daypilot.org/" rel="nofollow noreferrer">Day Pilot</a> which offers a calendar written in Asp.Net 2.0. They offer a lite version that you could check out, and if it works for you, you could purchase a license.</p>\n\n<p><strong>Update (9/30/09):</strong></p>\n\n<p>Unless of course the wheel is broken! Also, you can put a shiny new coat of paint if you like (ie: make a better UI).  But at least try to find some foundation to build off of, since the calendar system can be tricky (with Repeating events), and it\'s been done thousands of times.</p>\n'}, {'answer_id': 12800, 'author': 'Zack Peterson', 'author_id': 83, 'author_profile': 'https://Stackoverflow.com/users/83', 'pm_score': 0, 'selected': False, 'text': '<blockquote>\n  <p><strong><a href="https://stackoverflow.com/users/1368/undefined">undefined</a></strong> wrote:</p>\n  \n  <p>&hellip;this table will be hit pretty\n  hard, especially if I am going to be\n  sending out emails to remind people of\n  their events (events can be for\n  specific times of the day also!), so I could be polling this table every 15 minutes potentially!</p>\n</blockquote>\n\n<p>Create a table for the notifications.  Poll only it.</p>\n\n<p>Update the notification table when events (recurring or otherwise) are updated.</p>\n\n<p>EDIT: A database View might not violate normal forms, if that\'s a concern.  But, you\'ll probably want to track which notifications were sent and which have not yet been sent somewhere anyway.</p>\n'}, {'answer_id': 12821, 'author': 'public static', 'author_id': 1368, 'author_profile': 'https://Stackoverflow.com/users/1368', 'pm_score': 0, 'selected': False, 'text': "<p>Derek Park,\nI would be creating each and every instance of an event in a table, and this table would be regenerated every month (so any event that was set to reoccurr 'forever' would be regenerated one month in advance using a windows service or maybe at the sql server level).\nThe polling won't only be done every 15 minutes, that might only be for polls related to email notifications.  When someone wants to view their events for a month, I will have to fetech all their events, and re-occuring events and figure out which events to display (since a re-occuring event might have been created 6 months ago, but relates to a month the user is viewing).</p>\n\n<p>Zack, i'm not too concerned with having a perfectly normalized database, the fact that I'm thinking of creating a secondary table is already breaking one of the rules hehe.  My core database tables are following 'the rules', but I don't mind creating secondary tables/columns at times when it benefits things performance wise.</p>\n"}, {'answer_id': 12988, 'author': 'Darren Kopp', 'author_id': 77, 'author_profile': 'https://Stackoverflow.com/users/77', 'pm_score': 0, 'selected': False, 'text': '<blockquote>\n  <p>That is how I have designed my events table actually, but when thinking about it, say I have 100K users who have create events, this table will be hit pretty hard, especially if I am going to be sending out emails to remind people of their events (events can be for specific times of the day also!), so I could be polling this table every 15 minutes potentially!</p>\n</blockquote>\n\n<p>Databases do exception jobs of handling sets of data, so i wouldn\'t be too worried about that. What you should do is use this as your primary table, and then as events expire then move them into another table (like an archive).</p>\n\n<p>The next thing is you want to try is to query the db as little as possible, so move the information into a caching tier (like <a href="http://msdn.microsoft.com/en-us/data/cc655792.aspx" rel="nofollow noreferrer">velocity</a>) and just persist data to the database.</p>\n\n<p>Then, you can partition the information across multiple databases for scaling purposes. ie users 1-10000 calendars exist on server 1, 10001 - 20000 exist on server 2, etc.</p>\n\n<p>That\'s how i would scale a solution like this, but i still think the original solution i proposed is the way to go, it\'s just how you scale it that becomes the question.</p>\n'}, {'answer_id': 12993, 'author': 'eplawless', 'author_id': 1370, 'author_profile': 'https://Stackoverflow.com/users/1370', 'pm_score': 2, 'selected': False, 'text': "<p>The brute-force-ish but still reasonable way would be to create a new row in your single events table for every instance of the recurring event, all pointing <strong>not to the event preceding it in the series</strong> but to the <em>first</em> event in the series. This simplifies selecting and/or deleting all elements in a particular series, since you can select based on parent id. It also allows users to delete individual items from a series without affecting the rest of them.</p>\n\n<p>This query gets you the series that starts on element 3:</p>\n\n<pre><code>SELECT * FROM events WHERE id = 3 OR parentid = 3\n</code></pre>\n\n<p>To get all items for this month, assuming you'd have a start date and an end date in your events table, all you'd have to do is:</p>\n\n<pre><code>SELECT * FROM events WHERE startdate &gt;= '2008-08-01' AND enddate &lt;= '2008-08-31'\n</code></pre>\n\n<p>Handling the creation/modification of series programmatically wouldn't be very difficult, but it really would depend on the feature set you want to provide and how you think it'll be used. If you want to differentiate between series and events, you could have a separate series table and a nullable series_id on your events, allowing you the freedom to muck about with individual events while still retaining control over your series.</p>\n"}, {'answer_id': 23653, 'author': 'Matt S.', 'author_id': 1458, 'author_profile': 'https://Stackoverflow.com/users/1458', 'pm_score': 3, 'selected': False, 'text': '<p>I\'m tackling exactly this problem, and I had completely spaced <a href="http://en.wikipedia.org/wiki/ICalendar" rel="nofollow noreferrer">iCalendar</a> (<a href="https://www.rfc-editor.org/rfc/rfc2445" rel="nofollow noreferrer">rfc 2445</a>) up until reading this thread, so I have no idea how well this will or won\'t integrate with that. Anyway the design I\'ve come up with so far looks something like this:</p>\n<ul>\n<li>You can\'t possibly store all the instances of a recurring event, at least not before they occur, so I simply have one table that stores the first instance of the event as an actual date, an optional expiration, and nullable repeat_unit and repeat_increment fields to describe the repetition. For single instances the repition fields are null, otherwise the units will be \'day\', \'week\', \'month\', \'year\' and increment is simply the multiple of units to add to start date for the next occurrence.</li>\n<li>Storing past events only seems advantageous if you need to establish relationships with other entities in your model, and even then it\'s not necessary to have an explicit &quot;event instance&quot; table in every case. If the other entities already have date/time &quot;instance&quot; data then a foreign key to the event (or join table for a many-to-many) would most likely be sufficient.</li>\n<li>To do &quot;change this instance&quot;/&quot;change all future instances&quot;, I was planning on just duplicating the events and expiring the stale ones. So to change a single instances, you\'d expire the old one at it\'s last occurrence, make a copy for the new, unique occurrence with the changes and without any repetition, and another copy of the original at the following occurrence that repeats into the future. Changing all future instances is similar, you would just expire the original and make a new copy with the changes and repition details.</li>\n</ul>\n<p>The two problems I see with this design so far are:</p>\n<ol>\n<li>It makes MWF-type events hard to represent. It\'s possible, but forces the user to create three separate events that repeat weekly on M,W,F individually, and any changes they want to make will have to be done on each one separately as well. These kind of events aren\'t particularly useful in my app, but it does leave a wart on the model that makes it less universal than I\'d like.</li>\n<li>By copying the events to make changes, you break the association between them, which could be useful in some scenarios (or, maybe it would just be occasionally problematic.) The event table could theoretically contain a &quot;copied_from&quot; id field to track where an event originated, but I haven\'t fully thought through how useful something like that would be. For one thing, parent/child hierarchical relationships are a pain to query from SQL, so the benefits would need to be pretty heavy to outweigh the cost for querying that data. You could use a <a href="http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/" rel="nofollow noreferrer">nested-set</a> instead, I suppose.</li>\n</ol>\n<p>Lastly I think it\'s <em>possible</em> to compute events for a given timespan using straight SQL, but I haven\'t worked out the exact details and I think the queries usually end up being too cumbersome to be worthwhile. However for the sake of argument, you can use the following expression to compute the difference in months between the given month and year an event:</p>\n<pre><code>(:month + (:year * 12)) - (MONTH(occursOn) + (YEAR(occursOn) * 12))\n</code></pre>\n<p>Building on the last example, you could use MOD to determine whether difference in months is the correct multiple:</p>\n<pre><code>MOD(:month + (:year * 12)) - (MONTH(occursOn) + (YEAR(occursOn) * 12), repeatIncrement) = 0\n</code></pre>\n<p>Anyway this isn\'t perfect (it doesn\'t ignore expired events, doesn\'t factor in start / end times for the event, etc), so it\'s only meant as a motivating example. Generally speaking though I think most queries will end up being too complicated. You\'re probably better off querying for events that occur during a given range, or don\'t expire before the range, and computing the instances themselves in code rather than SQL. If you <em>really</em> want the database to do the processing then a stored procedure would probably make your life a lot easier.</p>\n'}, {'answer_id': 316056, 'author': 'Thomas Hansen', 'author_id': 29746, 'author_profile': 'https://Stackoverflow.com/users/29746', 'pm_score': 0, 'selected': False, 'text': '<p>The <a href="http://ra-ajax.org/samples/Viewport-Calendar-Starter-Kit.aspx" rel="nofollow noreferrer">Ra-Ajax Calendar starter-kit</a> features a sample of handling the RenderDate event which can modify the dates of specifically. Though the "recurring events" is more of an algorithmic thing and here I doubt very few calendars will help you much...</p>\n'}, {'answer_id': 501175, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>If anyone is doing Ruby there\'s a great library Runt that does this kind of thing.  Worth checking out.  <a href="http://runt.rubyforge.org/" rel="nofollow noreferrer">http://runt.rubyforge.org/</a></p>\n'}]
['https://Stackoverflow.com/questions/12611', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1368/']