简体   繁体   English

SQL调度系统

[英]SQL Scheduling System

I want to create an Exam Scheduling system. 我想创建一个考试计划系统。 now my objective is to avoid the user to insert a conflict time schedule for the same room in SQL Server 2008. for example: 现在,我的目标是避免用户为SQL Server 2008中的同一房间插入冲突时间表。例如:

for room 202 there's a schedule of 202房间的时间表

02/27/2017 4:00pm - 5:00pm

so what if the user input for room 202 is 那么,如果用户输入202房间是

02/27/2017 3:50pm - 5:10pm

obviously this is conflict. 显然这是冲突。

if I'm going to use between obviously it will not work for the given example input. 如果我要在之间使用,显然对于给定的示例输入将不起作用。

Kindly help. 请帮助。 Thanks 谢谢

Like Nick said, between is exactly what you need. 就像尼克说, between是正是你需要的。 In conjunction with exists or not exists in this case, you will be able to avoid schedule conflict. 在这种情况下,结合existsnot exists ,您将能够避免计划冲突。 You do the insert only if there is no entry in schedule for the same room, for the same day, that have a starting or ending time that is between the new time slot (which indicated a conflict). 仅当在同一房间,同一天的时间表中没有条目具有开始或结束时间在新时隙(表示冲突)之间的条目时,才进行insert

Look at this example : 看这个例子:

SQL Fiddle SQL小提琴

MS SQL Server 2008 Schema Setup : MS SQL Server 2008架构设置

CREATE TABLE schedule
    ([room] int, [date] date, [start] time, [finish] time)
;

INSERT INTO schedule
    ([room], [date], [start], [finish])
SELECT  202, '2017-02-26', '16:00', '17:00'
WHERE NOT EXISTS (SELECT * 
                  FROM schedule 
                  WHERE room = '202' AND date = '2017-02-26' AND 
                   (start BETWEEN '16:00' AND '17:00'
                    OR finish BETWEEN '16:00' AND '17:00'))
;

INSERT INTO schedule
    ([room], [date], [start], [finish])
SELECT  202, '2017-02-26', '15:50', '17:10'
WHERE NOT EXISTS (SELECT * 
                  FROM schedule 
                  WHERE room = '202' AND date = '2017-02-26' AND 
                   (start BETWEEN '15:50' AND '17:10'
                    OR finish BETWEEN '15:50' AND '17:10'))
;

INSERT INTO schedule
    ([room], [date], [start], [finish])
SELECT  202, '2017-02-26', '15:00', '15:59'
WHERE NOT EXISTS (SELECT * 
                  FROM schedule 
                  WHERE room = '202' AND date = '2017-02-26' AND
                   (start BETWEEN '15:00' AND '15:59'
                    OR finish BETWEEN '15:00' AND '15:59'))
;

Query 1 : 查询1

SELECT  *
FROM schedule

Results : 结果

| room |       date |            start |           finish |
|------|------------|------------------|------------------|
|  202 | 2017-02-26 | 16:00:00.0000000 | 17:00:00.0000000 |
|  202 | 2017-02-26 | 15:00:00.0000000 | 15:59:00.0000000 |

In an application, make it look like this : 在应用程序中,使其如下所示:

Pseudo : 伪:

SELECT count(*)
FROM schedule 
WHERE room = '202' AND date = '2017-02-26' AND
      (start BETWEEN '15:00' AND '15:59'
       OR finish BETWEEN '15:00' AND '15:59')

if count(*) = 0 then
     INSERT INTO schedule
       ([room], [date], [start], [finish])
     SELECT  202, '2017-02-26', '15:00', '15:59'
else
    ERROR "CONFLICT WITH ANOTHER SCHEDULE"
end if

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM