Teb*_*ebo 14 database-design database-schema
我希望能够创建可以基于固定日期执行的日程表,每天重复,在一周中的特定日期重复,在一年中的特定月份重复,在每年的特定日期重复,并在一天的特定时间.
请问我该如何为这个问题构建数据库表?
编辑#1
基本上,我正在编写一个应用程序,允许用户安排在各种预先配置的时间发送预配置的问候语.我知道我需要一个存储有关时间表信息的表格(例如圣诞节,营销一,... |以及时间表应该运行).然后是另一个表,用于记录已运行的计划,发送的问候语,人员和电子邮件; 基本上是一个交易表.
我的问题是设计Schedule表,因为我希望允许用户在特定日期,一周的特定日期(经常性),每个月的特定日期,每天的特定时间以及每年特定的日/月(例如25/12).
如何为计划创建一组表格,以灵活的方式处理这些输入?
Teb*_*ebo 18
这是我提出的表格结构;
Schedule
- ScheduleName
- ScheduleTypeId (Daily, Weekly, Monthly, Yearly, Specific)
- StartDate
- IntervalInDays
- Frequency
- FrequencyCounter
ScheduleDaily
- ScheduleDailyId
- ScheduleId
- TimeOfDay
- StartDate
- EndDate
ScheduleMonthly
- ScheduleMonthlyId
- ScheduleId
- DayOfMonth
- StartDate
- EndDate
ScheduleSpecific
- ScheduleSpecificId
- ScheduleId
- SpecificDate
- StartDate
...
ScheduleJob
- ScheduleJobId
- ScheduleId
- ScheduleTypeId
- RunDate
- ScheduleStatusId
Run Code Online (Sandbox Code Playgroud)
Rob*_*aft 17
Microsoft SQL Server具有高效灵活的设计:https://msdn.microsoft.com/en-us/library/ms178644.aspx?f = 255&MSPPError = -2147217396
我仔细阅读了上面的答案,我认为很多东西都是不必要的,如果我错了,请纠正我。
这是我认为应该做的:
日程
ID
类型(每日、每月、每周、固定、每年)- 枚举
频率(可以是 1-7[一周中的天]、1-30(或 28)[一个月中的天]、1-365[一年中的天] 或 null(每日、固定) - ArrayField(整数) - [ 1, 7] 或 [23] 或 [235] 或空
时间(UTC 中的时间) - ArrayField(字符字符串 - ['9:00', '13:30']
日期(固定类型) - 日期时间 - 2009-03-21
is_active (boolean) - 用于启用、禁用计划
name (CharField) - 如果您想命名时间表
其余字段需要您正在构建的内容的上下文。
现在,为此我正在考虑每 30 分钟运行一个 cronjob(我正在以 30 分钟分隔的时间输入)运行一个脚本(在我的例子中是 django 管理命令),该脚本从该表中过滤需要运行的计划:
查询将是这样的:
current_day_of_week = 3
current_day_of_month = 24
current_day_of_year = 114
current_time = 13:30
current_date = 2019-04-24
Filter records that match the below query(not even psuedo code)(I'm using Q objects(https://docs.djangoproject.com/en/2.2/topics/db/queries/#complex-lookups-with-q-objects)
Q(daily AND current_time) OR
Q(weekly AND current_day_of_week AND current_time) OR
Q(monthly AND current_day_of_month AND current_time) OR
Q(yearly AND current_day_of_year AND current_time) OR
Q(fixed AND current_date AND current_time)
Run Code Online (Sandbox Code Playgroud)
我认为接受的答案比它需要的要复杂得多。这可以通过像这样的单个表来完成:
Schedules
- Id :int
- Greetingid :int
- Startdate :date
- Frequencytype :char(1)
- Frequencyinterval :int
- Timeofday :time
Run Code Online (Sandbox Code Playgroud)
Frequencytype 将是以下值之一
Frequencyinterval 将是数字,该值的含义取决于 frequencytype 的值