I need to create a table to hold a schedule for meetings.
A meeting can be scheduled to be:
Daily
'Ever X days'. where X can be between 1 and 6.
Ending after X sessions. Where 'sessions' is basically the number of repeats.
Weekly
Which days during the week it can occur. Mon, Tue, etc. Can select more than one day per week.
The Date on which it ends.
Monthly
Use can select the day of the month it can occur (1st, 2nd etc)
OR they can select from a lookup of '1st, 2nd, 3rd, 4th or Last' and a Day 'Mon, Tues', saying, for example "The 2nd Friday" of the month.
How can I store all these scenarios in a single table?
I was thinking:
CREATE TABLE schedule
(
ID INT NOT NULL IDENTITY(1,1) PRIMARY KEY,
StartDate DATETIME NOT NULL,
EndTime TIME NULL,
RepeatTypeID INT NOT NULL, // Daily, Weekly, Monthly, None
// For Daily
EveryDayCount INT NULL, // to handle 'every 3 days',
RepeatCount INT NULL, // How many occurances. Can be shared with different RepeatTypes
// weekly
IsMonday BIT,
IsTuesday BIT,
etc // A field per day selection. Is there a better way?
// Monthly
MonthlyDayNumber INT NULL,
MonthlyRepeatIntervalID INT, // Lookup table with '1st, 2nd, 3rd, 4th, Last'
MonthlyDayRepeatSelection INT // Lookup on Monday, Tuesday etc
)
But this seems inefficient. Is there a better design pattern for these sorts of requirements?
So basically I once implemented the same functionality and I found that rather than ease of storage, that ease of retrieval and edit/update was of paramount importance.
You don't want to calculate all dates every single time, you query the DB for meeting dates or say like you have a function like showAllMeetingsForADate(somedate date) then you would not want to calculate dates for meeting at run time.
Holistically the most optimal storage is that you store meeting information calculation logic in a table and all meeting dates in another table like below.
However for the storage of meeting information, you should go with a normalized form.
Schedule Detail Tables
CREATE TABLE DailyScheduleDetails
(
ScheduleDetailsID INT PRIMARY KEY IDENTITY(1,1),
RecurrenceCount INT NOT NULL
)
CREATE TABLE WeeklyScheduleDetails
(
ScheduleDetailsID INT PRIMARY KEY IDENTITY(1,1),
OnMonday bit,
OnTuesday bit,
OnWednesday bit,
-- ...
OnSunday bit,
EndByDate Date NOT NULL
)
CREATE TABLE MonthlyScheduleDetails
(
ScheduleDetailsID INT PRIMARY KEY IDENTITY(1,1),
MonthlyDayNumber INT NULL,
MonthlyRepeatIntervalID INT, // Lookup table with '1st, 2nd, 3rd, 4th, Last'
-- Here I'd suggest using 0 for Last
MonthlyDayRepeatSelection INT // Lookup on Monday, Tuesday etc
)
Schedule
CREATE TABLE schedule
(
ID INT NOT NULL IDENTITY(1,1) PRIMARY KEY,
StartDateTime DATETIME NOT NULL,
EndDateTime DATETIME NULL,
RepeatTypeID INT NOT NULL, // Daily, Weekly, Monthly, None
ScheduleDetailsID INT
)
MeetingDates
CREATE TABLE MeetingDates
(
ID INT NOT NULL PRIMARY KEY IDENTITY(1,1),
MeetingID int,
MeetingStartDate datetime,
MeetingEndDate datetime -- because you can have meeting spanning days like 11:00 PM to 1:00 AM
--,user or guest information too
,CONSTRAINT FK_MeetingDates_Schedule FOREIGN KEY (MeetingID)
REFERENCES Schedule(ID)
)
Use an existing standard. That standard is iCalendar RRules and ExDates.
Just store the recurrance rule in the db as a varchar
Use an existing library (C#) to calculate upcoming dates
Even though you have daily, weekly, monthly etc... still means that a meeting will occur on some specific day ... right ?
Thus
CREATE TABLE schedule
(
ID INT NOT NULL IDENTITY(1,1) PRIMARY KEY,
StartDate DATETIME NOT NULL,
EndDate DATETIME NOT NULL,
RepeatTypeID INT NOT NULL, // Daily, Weekly, Monthly, None
RepeatCount INT NOT NULL,
DayOn INT NOT NULL, // can be a calculated field based on start date using DAY function
)
I believe this can capture all your schedule options.
Related
I am trying to create a SQL view on two tables I'm working with:
CREATE TABLE availability
(
doctor varchar(20) NOT NULL,
avl_date date NOT NULL,
avl_start time NOT NULL,
avl_end time NOT NULL,
CONSTRAINT pk_availability PRIMARY KEY (doctor, avl_date)
);
And
CREATE TABLE appointments
(
patient varchar(20) NOT NULL,
doctor varchar(20) NOT NULL,
apt_date date NOT NULL,
apt_start time NOT NULL,
apt_end time NOT NULL,
CONSTRAINT pk_appointments PRIMARY KEY (patient, apt_date)
);
The view I am trying to create lists all maximal time periods (apt date, apt start, apt end) during which no further appointments are possible (consider doctors’ availability as well).
Any help is greatly appreciated, thanks.
this should work if the appointment time-duration is constant. You can optimize it by re-writing with inner join instead of minus. Also, remember to put availability table above minus.
select doctor
, avl_date
, avl_start
, avl_end
from availability
minus
select doctor
, apt_date
, apt_start
, apt_end
from appointments
I want to create a stored procedure to update a column based on an amount of time. For example, to update the interest generated column every 15 days.
Here is my code. Please help.
create table Loan(
Loan_ID int not null primary key,
Loan_custID int not null foreign key references Customers(Cust_ID),
Loan_Amount int not null,
Loan_Interest int not null,
Loan_Date date not null unique,
)
Create table Interestgenerated(
IG_ID int not null primary key,
Loan_ID int not null foreign key references Loan1(Loan_ID),
Loan_Date date null foreign key references Loan1(Loan_Date),
IG_Amount int not null,
IG_Date datetime not null
)
create procedure InsertINtoInterestgenerated1
#PresentDate Datetime
as
set #PresentDate=getdate()
select Loan_ID from Loan
set IG_Date=Loan_Date
IG_Date=dateadd(day,15, IG_Date)
if #PresentDate=IG_Date
begin
update Interestgenerated1 set IG_Date = #PresentDate, IG_Amount=IG_Amount*0.15
end
Considering you want to automate the update of the value in column IG_Amount every 15 days,
you can schedule a job to run every 15 days at midnight like on the 1st and 16th of every month.
the below link might help you:
how to schedule a job for sql query to run daily?
I have a problem with finding how to declare the following type of association :
Say I have a table "Weekly" as such :
Weekly {
Id : Int <= PK
Week : Int
Year : Int
}
And a table "Monthly" :
Monthly{
Id : Int <= PK
Month: Int
Year : Int
}
I also have a "WeekMonth" Table :
Monthly{
Week : Int <= PK
Month : Int <= PK
Year : Int <= PK
}
As you my have guessed, i whant to be able to link the Weekly with WeekMonth and Monthly with WeekMonth too.
However, i can't seam to be able to do this : a foreign key on part of the composite primary key. Nevertheless, in my WeekMonth table, both the year and week and the year and month field are obviouly unique, so it should be able to work.
I've tried multiple approch to this problem , but as the custom mapping of week per month is a business need, I a bit stuck with it
in my WeekMonth table, both the year and week and the year and month field are obviouly unique
That isn't true. 'Year and week' may be unique, but it depends what 'week' is here - if it's the week within the month (i.e. 1-5) then it is not unique. If it's the week within the year (1-53) then it is; but you don't have a unique or primary key on that combination. And 'year and month' is not unique, as you will have multiple entries - either 4 or 5 - for each combination.
If you have a composite primary (or unique) key then a foreign key has to refer to all of the columns in that PK - otherwise they would not necessarily be unique.
A natural key isn't really working for you here. As well as not allowing the relationships you want, you're duplicating data in the parent and child tables. It would be better to have a synthetic key, e.g. set from a sequence:
WeekMonth{
WeekMonth_Id : Int <= PK (synthetic, e.g. from sequence)
Week : Int <= }
Month : Int <= } UK
Year : Int <= }
}
Weekly {
Weekly_Id : Int <= PK
WeekMonth_Id : Int <= FK to WeekMonth
}
Monthly{
Monthly_Id : Int <= PK
WeekMonth_Id : Int <= FK to WeekMonth
}
You don't need to duplicate the year/month/week values in the child tables as you can get them from the parent. And you shouldn't duplicate them, as you can't easily guarantee that the match the related parent record, as well as for general normalisation reasons.
I'm assuming you have other data in the weekly and monthly tables, otherwise they would be a bit pointless; any other table that has an FK to one of those could use an FK to WeekMonth instead.
If you do want to have the individual year/month/week values duplicated in the child tables then you will need separate unique keys for those combinations, in addition to your current PK. So you'd modify WeekMonth to have a unique key on year and month (which may be possible, depending what 'week' represents), and another unique key on year and month - but as that is not a unique combination you can't create that key.
Assuming that the WeekMonth table has Week values 1 through 53 for the year then:
CREATE TABLE WeekMonth(
Week INT,
Month INT,
Year INT,
CONSTRAINT WeekMonth__W_M_Y__PK PRIMARY KEY ( Week, Month, Year ),
CONSTRAINT WeekMonth__W_Y__PK UNIQUE ( Week, Year )
);
CREATE TABLE Monthly(
ID INT PRIMARY KEY,
Month INT,
Year INT,
FirstWeek INT GENERATED ALWAYS
AS ( TO_NUMBER(
TO_CHAR(
NEXT_DAY(
TO_DATE( month||'-'||year, 'MM-YYYY' ) - 1,
'MONDAY'
),
'WW'
)
)
),
CONSTRAINT Monthly__M_Y__PK FOREIGN KEY ( FirstWeek, Month, Year )
REFERENCES WeekMonth( Week, Month, Year )
);
CREATE TABLE Weekly(
ID INT PRIMARY KEY,
Week INT,
Year INT,
CONSTRAINT Weekly__W_Y__PK FOREIGN KEY ( Week, Year )
REFERENCES WeekMonth( Week, Year )
);
Below is DDL for the table I want to create. However, I want the attribute 'Appointment_datetime' to be a future date and during working hours (between 8:00AM and 5:00PM). I can get the future date part with -'CHECK (Appointment_datetime >= GETDATE()) But how do I get between 8AM and 5PM ontop of this constraint?
CREATE TABLE tAppointment
(
Appointment_ID int NOT NULL PRIMARY KEY,
Appointment_datetime datetime NOT NULL, -- CHECK CONSTRAINTS NEEDED
Appointment_week int NOT NULL,
Appointment_room varchar(5) NOT NULL,
Vet_ID int NOT NULL REFERENCES tVet(Vet_ID),
Owner_ID int NOT NULL REFERENCES tOwner(Owner_ID),
Pet_ID int NOT NULL REFERENCES tPet(Pet_ID)
)
You can just add it in. Here is a method using the hour:
CHECK (Appointment_datetime >= GETDATE() AND
DATEPART(HOUR, GETDATE()) NOT BETWEEN 8 AND 16
)
Note: If you want to take weekends and holidays into account, that is more difficult and probably requires a user-defined function.
I am currently using the FullCalendar JQuery module to allow a user to create a personal timetable. The events are added/updated to an SQL Server database. This is working fine.
I am trying to create a facility where each user has a database which has stored all of their events for the year, some of which can be recurring and occur every week.
I then wish to have users be able to organize meetings with other users based on the timeslots available in their timetables.
I'm not sure how to integrate these recurring events into my system, or how my algorithm would work with these recurring events.
The design I have at the moment is :
CREATE TABLE Users (
user_id INT NOT NULL AUTO_INCREMENT,
email VARCHAR(80) NOT NULL,
password CHAR(41) NOT NULL,
PRIMARY KEY (user_id)
);
CREATE TABLE Events (
event_id INT NOT NULL AUTO_INCREMENT,
title VARCHAR(80) NOT NULL,
description VARCHAR(200),
start_time DATETIME,
end_time DATETIME,
group_id INT NOT NULL,
recurring boolean
);
CREATE TABLE Groups (
group_id INT NOT NULL,
user_id INT NOT NULL
);
Will this be sufficient? How will I have it so that recurring events are rendered on the calendar for every week? If I am lacking in any detail, please ask! Thank you very much.
You could use something like the following:
SELECT *
FROM Events
WHERE Recurring = 0
UNION
SELECT Event_ID,
Title,
Description,
DATEADD(WEEK, Interval, Start_Time) [Start_Time],
DATEADD(WEEK, Interval, End_Time) [End_Time],
Group_ID,
Recurring
FROM Events,
( SELECT ROW_NUMBER() OVER(ORDER BY Object_ID) [Interval]
FROM SYS.ALL_OBJECTS
) i
WHERE Recurring = 1
AND Interval <= 52 -- reccurs for 1 year
This will make all events repeat for 52 weeks (or whatever period you want).
As an aside, in the question you mentioned sql server, and you have tagged the question as SQL server but all your syntax appears to be MySQL (AUTO_INCREMENT, Boolean data type).