I am trying to design a database for creating a school syllabus. In a nutshell, all classrooms can be active for 10 hours from 8.00am to 18.00 pm. How to create the database to answer the below ?
Get me empty hours for the classroom which is A101 on Monday ?
I made this one below but its not enough good to answer this question efficiently |:
http://i51.tinypic.com/110drgk.png
I only dabble in database, but the problem seemed interesting to me.
From what I’m getting, it sounds like you are more interested in how to write the query than how to store the data. Based on your explanation, what you want is a query whose output looks something like this:
Assuming schema test, table class. Class holds a startTime and endTime value. Other values on which filtering will be performed are ignored here to focus on just the logic you are interested in. This query was run on MySQL.
Three subqueries.
The first creates a cross-product of all end times (with the start time for the day appended) by all start times (with the end time for the day appended in). What this gives you is a series of rows representing every conceivable open block of time. (arranged as block start/ block end)
The second then pares the results of the first subquery by filtering out values where block start happens after block end, then sorts the values by block start, block end. This makes it so that when the values are grouped by block-start, you get a nice list of pairs that make it easy to spot places where classes are continuous.
The third query then filters out the values where the times are the same, giving you just the time blocks that are relevant. Hypothetically the third query could use a comparison that ruled out cases where classes were less than an hour apart as well.
I’m sure others might have somewhat simpler solutions for you.