查找指定教室的空闲时间(数据库设计问题)
我正在尝试设计一个用于创建学校教学大纲的数据库。简而言之,所有教室都可以从上午 8 点到晚上 18 点活跃 10 个小时。如何创建数据库来回答以下问题?
给我周一 A101 教室的空闲时间吗?
我在下面做了这个,但它不足以有效地回答这个问题|: http://i51.tinypic.com/110drgk.png
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我只涉足数据库,但这个问题对我来说似乎很有趣。
据我所知,听起来您对如何编写查询比如何存储数据更感兴趣。根据您的解释,您想要的是一个输出如下所示的查询:
假设架构测试,表类。类保存 startTime 和 endTime 值。此处忽略将执行过滤的其他值,只关注您感兴趣的逻辑。此查询在 MySQL 上运行。
三个子查询。
第一个创建所有结束时间(附加当天的开始时间)与所有开始时间(附加当天的结束时间)的叉积。这给你的是一系列代表每个可以想象的开放时间块的行。 (排列为块开始/块结束)
第二个子查询通过过滤掉块开始发生在块结束之后的值来解析第一个子查询的结果,然后按块开始、块结束对值进行排序。这使得当值按块开始分组时,您会得到一个很好的对列表,可以轻松找到类连续的位置。
然后,第三个查询会过滤掉时间相同的值,只提供相关的时间块。假设第三个查询可以使用比较来排除课程间隔不到一小时的情况。
我相信其他人可能会为您提供更简单的解决方案。
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.
你的数据库图表中甚至没有教室。
如果您主要关心的问题之一是阅读提前时间,我会建议遵循以下内容:
一个 timeSlot 表,其中包含一天中时间块的列(因为这可以分解为相对较小的有限数字,例如每 30 分钟 1 次)。
教室表将有 5 或 7 个 ID,引用 timeSlot 表中的 5 或 7 个条目
希望这会有所帮助,
Ryan Taylor
You don't even have classroom in your database diagram.
If one of your main concerns is read premoance time I would suggest something along the lines of the following:
A timeSlot table that has column for the blocks of time in a day (since this can be broken down into a relatively small finite number, say one per 30 minutes).
A classroom table would then have 5 or 7 IDs referring to 5 or 7 entries in the timeSlot table
Hope this helps,
Ryan Taylor
支持通用间隔类型的 DBMS 使这变得非常简单:
使用属性 和 定义一个相关变量。
然后只需查询“X NOT MATCHING Y”,其中:
X 是一个关系文字,提及适当的教室标识符和时间间隔 08.00-18.00,
Y 是持有的相关变量,
(NOT MATCHING 是您需要的任何关键字调用关系代数的 SEMIMINUS 运算符)。
例如,在 SIRA_PRISE 中,您可以有一个名为 SCH 的相关变量,其中包含“scheduled_during”元组 {classRoomID:"A101" ScheduledDuring:[1999-01-02-09.30.00-1999-01-02 -12.00.00]}。
关系文字可以是“RELATION{TUPLE{classRoomID:"A101"scheduledDuring:[1999-01-02-08.00.00-1999-01-02-18.00.00]}}”。
然后查询 SEMIMINUS(RELATION{TUPLE{...}},SCH) 将产生元组
classRoomID:"A101" ScheduledDuring:[1999-01-02-08.00.00-1999-01-02-09.30.00] 和
classRoomID:"A101" ScheduledDuring:[1999-01-02-12.00.00-1999-01-02-18.00.00]
编辑
PS 不要被该属性名称 ScheduledDuring 显示房间未安排的时间段所迷惑。值的含义位于查询的“外部谓词”中,而不是属性名称中。
DBMS's that support generic interval types make this dead easy :
Define a relvar with the attributes and .
Then just query 'X NOT MATCHING Y', where :
X is a relation literal mentioning the appropriate classroom identifier and the time interval 08.00-18.00,
Y is your relvar holding the <scheduled_during>,
(and NOT MATCHING is whatever keyword you need to invoke the relational algebra's SEMIMINUS operator).
In SIRA_PRISE, for example, you could have a relvar named, e.g., SCH, holding, e.g., the "scheduled_during" tuple {classRoomID:"A101" scheduledDuring:[1999-01-02-09.30.00-1999-01-02-12.00.00]}.
A relation literal could be "RELATION{TUPLE{classRoomID:"A101" scheduledDuring:[1999-01-02-08.00.00-1999-01-02-18.00.00]}}".
And querying SEMIMINUS(RELATION{TUPLE{...}},SCH) would then yield the tuples
classRoomID:"A101" scheduledDuring:[1999-01-02-08.00.00-1999-01-02-09.30.00] and
classRoomID:"A101" scheduledDuring:[1999-01-02-12.00.00-1999-01-02-18.00.00]
EDIT
PS don't be confused by that attribute name scheduledDuring showing periods during which the room is NOT scheduled. The meaning of the values is in the "external predicate" of your queries, not in the attribute names.
您的解决方案是一个基本的预订系统。在预订系统中,资源是用可用期限来定义的。如果业务规则是以单小时增量使用资源,我将设计具有 24 个周期的资源表来表示一天中的每个小时。如果业务规则规定资源在上午 8 点到上午 10 点期间可用,我会在表示该时间范围的期间列中放置一个“真实值”。预订在一个单独的表中进行跟踪,并使用返回资源的外键。预留表具有来自资源表的匹配周期列。如果在资源可用期间进行预订,则该列中会放置“真实值”。 来查找可预留的资源。
在第三步的计算中,我们可以排除任何期间可用性为“False”的资源,然后我们查看预订表中的聚合值,并声明如果存在预订(“True”),则可用性为“False”。
下面的代码是用 SQL Server 2008 编写的,演示了上述原理。
下面的代码将返回三个结果集。第一组是可用性计算,第二组是教室表列表,最后是预订表。
Your solution is a basic reservation system. In a reservation system the resources are defined with an availability period. If the business rule was the resource was used in single hour increments I would design the resource table with 24 periods to represent each hour of the day. If the business rule stated the resource was available during 8AM to 10AM I would place a "True value" in the period columns representing that time frame. Reservations are tracked in a seperate table with a foreign key back to the resource. The reservation table has matching period columns from the resource table. When a reservation is made during a period the resource is available a "True value" is placed in the column. Resources available for reservation are found by
In the calculation for step three we can rule out any resource who's period availability is "False" then we look at the aggregate value from the reservations table and state if there is a reservation("True") then the availability is "False".
The code below is written in SQL Server 2008 and demonstrates the principals above.
The code below will return three result sets. The first set is the availability calculation, second is the list of the classroom table and finally the reservation table.