时间表,间隔,预留等的数据库设计

时间:2012-11-17 18:29:04

标签: sql database-design

我正在开展一个项目,我想要一些解决问题的灵感或见解。我正在编写一个应用程序,用于记录访客到一个祈祷中心(一个有很多房间的地方。人们来祈祷)。我们将整合预订系统。现在,人们可以说“生病,约会,或每周三生病”或“生病每周两次”或“生病每月一次”等。

我不知道如何在关系数据库中维护这种信息。任何人都可以对此有一些见解吗?

如何查询数据库以查找特定日期预定访问的人员?

1 个答案:

答案 0 :(得分:1)

此架构应足以让您入门

VISITORS
id
name
... other atomic data

RESOURCES
id
name
... other atomic data

RESERVATIONS
id
visitor
resource
fromdate
tilldate

因此,如果我想在11月19日10:00至12:00之间预订200号房间,预订表中会有一条记录,其中“访客”字段指向我 visitor 表中的条目,'resource'字段指向 resources 表中200会议室的条目,'fromdate'将等于10:00 19-11 -2012和'tilldate'将等于'12:00 19-11-2012'。

您可以编写查询,以显示在特定日期/时间保留哪些资源,例如

select resources.name, visitors.name
from resources inner join reservations on resources.id = reservations.resource
inner join visitors on reservations.visitor = visitors.id
where reservations.fromdate <= "2012-11-19 06:00"
and reservations.tilldate >= "2012-11-19 23:59"

和查询显示在给定时间哪些资源是免费的

select resources.name
from resources 
where not exists (select 1 from reservations
where reservations.resource = resources.id
and reservations.fromdate >= "2012-11-19 10:00"
and reservations.tilldate <= "2012-11-19 12:00")

如果有人说“我将在每个星期三来”,你的应用程序必须足够智能,根据所需的资源,日期和时间在“预订”表中插入几行。

另请参阅此问题:Database Tables for Reservation site