6

I am interested in seeing suggestions for a database design regarding business hours.

It would be quite similar to what Facebook has - alt text http://uploader.ws/upload/200903/widget.png

I have a list of businesses, and I would like for users to be able to input multiple sets of available hours for that business. e.g.,

Monday: open 9-5; Tuesday: open 9-12; 1-5; etc. I would not like to be limited to two sets of hours per day. Ideally, N sets of hours per day. If that's not practical, no more than 4... no less than 2.

I am interested in the "best" (theoretical) and the most practical solutions.

The DBMS I'm using is MySQL.

4 Answers 4

6

How about:

create table business (
  id int not null auto_increment primary key,
  name varchar(255)
);

create table open_hour_range (
  id int not null auto_increment primary key,
  business_id int,
  day_of_week tinyint, /* 0-6 */
  open_time time,
  close_time time,
  foreign key(business_id) references business(id)
);

This allows you any combination of hours, including multiple per day. However, it may be a bit slow from a querying perspective, in that you'll need to do a fair amount of joining to come up with the list of what hours a business is open.

Also, if you want to be able to display hours in a format like:

M-F 9-5 Sa-Su 9-12

You'd need to merge similar ranges in code, outside the database. If you wanted this sort merging, you could change day_of_week to a start_day and an end_day.

3
  • 1
    This is similar to what I was going to post. I wouldn't consider the joins an issue however... I would do a single separate query to select all the applicable hour ranges for a business, and let the app code handle the formatting. You can always cache the output as well.
    – zombat
    Mar 27, 2009 at 23:47
  • What is the benefit of the open_hour_range.id column? Mar 28, 2009 at 0:56
  • The open_hour_range.id column gives a way to uniquely identify a particular open_hour_range for updating/deletion. This may be a bad habit - I guess you could make the full column set of the table be the primary key. Mar 28, 2009 at 1:26
3

A minor tweak to Scotty Allen's model:

business table:
id - int
business_name - string
open_hour_range table:
id - int
business_id - int //foreign key to business
days_of_week- int // (bitmask) 1-127
open_time - time
close_time - time

1
  • Ooh, I like the bitmask idea - clever:) Mar 28, 2009 at 17:52
0

With Microsoft SQL Server database you could store that data as typed XML and still be able to sort and serach data based on one or more values in that field. Make any calculations if needed etc.

A value in that column could look similar to this:

<businessDays>
    <monday>
        <hours from="12" to="15" />
        <hours from="16" to="21" />
    </monday>
    <friday>
        <hours from="13" to="15" />
        <hours from="16" to="18" />
        <hours from="19" to="21" />
    </friday>
</businessDays>

References on Typed XML:

References on indexing and quering XML data in SQL Server:

In your application you could serialize/deserialize this data into and from business objects.

Extremely simple and efficient.

5
  • Why would that be an advantage? Mar 28, 2009 at 0:03
  • Only 1 column is used for storing this unstractured data. In the application you can deserialize it into business entity by using existing serialization/deserialization features of .NET. If you split that data into several columns or even tables like other guys suggest -> result is "overnormilized" Mar 28, 2009 at 0:11
  • Sorry, I tagged the question as MySQL but I didn't specify that as a requirement in the question. Updating the question.
    – ahill
    Mar 28, 2009 at 3:46
  • What if you want to find all businesses open 24 hours a day or something of that nature? You sort of limit the entire point of a database with this type of structure. Mar 28, 2009 at 3:58
  • @d03boy, it's not a problem. You still will be able to index and query that data. Check out the references I specified. Mar 28, 2009 at 9:19
0

I wolud rather use datetime in columns open_time and close_time. Its for meetings that will start at night and will end in the morning next day. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.