所以最初我在想这样的事情。
CREATE TABLE `events` (
`event_id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
`event_name` VARCHAR(200),
`date_added` DATETIME
PRIMARY KEY(event_id)
);
CREATE TABLE `event_date` (
`event_id` INTEGER UNSIGNED NOT NULL,
`start_date` DATE,
`end_date` DATE,
FOREIGN KEY event_id REFERENCES events(event_id) ON DELETE CASCADE
);
CREATE TABLE `event_times` (
`event_id` INTEGER UNSIGNED NOT NULL,
`event_date` DATE,
`start_time` TIME,
`end_time` TIME,
FOREIGN KEY event_id REFERENCES events(event_id) ON DELETE CASCADE
);
是否可以使用此架构确保even_times.event_date属于event_date.start_date和event_date.end_date范围?
答案 0 :(得分:0)
我想你想要创建事件详细信息,事件可以在一个或多个日期发生,每个日期可以有一个或多个小时的完成。
如果是这样,最好在“级联”中建立关系,甚至在EVENT_DATE中引用,反过来将even_date与even_time相关联,从而消除表even_time中的日期字段
答案 1 :(得分:0)
通过这种结构,我认为你可以满足解释的需求
CREATE TABLE events(
event_id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
event_name VARCHAR(200) DEFAULT NULL,
date_added DATETIME DEFAULT NULL,
PRIMARY KEY (event_id)
);
CREATE TABLE event_date(
event_date_id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
event_id INT(10) UNSIGNED NOT NULL,
start_date DATE DEFAULT NULL,
end_date DATE DEFAULT NULL,
PRIMARY KEY (event_date_id),
INDEX FK_event_date_events_event_id (event_id),
CONSTRAINT FK_event_date_events_event_id FOREIGN KEY (event_id)
REFERENCES events (event_id) ON DELETE CASCADE ON UPDATE RESTRICT
);
CREATE TABLE event_times(
event_time_id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
event_date_id INT(10) UNSIGNED NOT NULL,
start_time TIME DEFAULT NULL,
end_time TIME DEFAULT NULL,
PRIMARY KEY (event_time_id),
INDEX FK_event_times_event_date_event_date_id (event_date_id),
CONSTRAINT FK_event_times_event_date_event_date_id FOREIGN KEY (event_date_id)
REFERENCES event_date (event_date_id) ON DELETE CASCADE ON UPDATE RESTRICT
)