在mysql数据库中,我一共有7个表。一般情况下,一个教官只属于一个学院,但一个学院可以有多个教官。同理,一所学院可以教授许多课程。我没有记录什么讲师教了哪门课程,而是记录了 x 或 y 学院正在教什么课程。此外,每个学院都有一个主要联系人,与导师不同,一个联系人可以是多个学院的一部分。最后,我想运行一个查询,在那里我可以看到链接到我选择的特定学院的数据。
下表的结构是否可以存储/显示数据,没有相同的讲师、课程等的重复值?
CREATE TABLE IF NOT EXISTS `academies` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(25) NOT NULL,
`mou_id` int(11) default NULL,
`academy_id` int(11) NOT NULL,
`street_address` varchar(50) NOT NULL,
`city` varchar(25) NOT NULL,
`state` varchar(25) NOT NULL,
`country` varchar(25) NOT NULL,
`zipcode` varchar(5) NOT NULL,
`telephone` varchar(15) NOT NULL,
`comments` text,
`last_contacted` date default NULL,
PRIMARY KEY (`id`)
) ;
CREATE TABLE IF NOT EXISTS `academy_courses` (
`id` int(11) NOT NULL auto_increment,
`academy_id` int(11) NOT NULL,
`course_name` varchar(75) NOT NULL,
`course_id` int(11) NOT NULL,
`instructor_id` int(11) NOT NULL,
`start_date` date default NULL,
PRIMARY KEY (`unique_id`),
KEY `course_id` (`academy_id`,`course_id`)
);
CREATE TABLE IF NOT EXISTS `courses` (
`course_id` int(11) NOT NULL auto_increment,
`course_name` varchar(75) NOT NULL,
PRIMARY KEY (`course_id`)
);
INSERT INTO `courses` (`course_id`, `course_name`) VALUES
(1, 'Math'),
(2, 'English'),
(3, 'Science'),
(4, 'HIstory');
CREATE TABLE IF NOT EXISTS `instructors` (
`instructor_id` int(11) NOT NULL auto_increment,
`academy_id` int(11) NOT NULL,
`instructor_name` varchar(50) NOT NULL,
`instructor_phone` varchar(15) default NULL,
`instructor_email` varchar(55) default NULL,
PRIMARY KEY (`instructor_id`),
KEY `academy_id` (`academy_id`)
);
CREATE TABLE IF NOT EXISTS `last_contacted` (
`talkedto_id` int(11) NOT NULL auto_increment,
`academy_id` int(11) NOT NULL,
`talkedto_name` varchar(50) NOT NULL,
PRIMARY KEY (`talkedto_id`),
KEY `academy_id` (`academy_id`)
);
CREATE TABLE IF NOT EXISTS `main_contact` (
`contact_id` int(11) NOT NULL auto_increment,
`academy_id` int(11) NOT NULL,
`contact_name` varchar(50) NOT NULL,
`contact_phone` varchar(15) default NULL,
`contact_email` varchar(55) default NULL,
PRIMARY KEY (`contact_id`),
KEY `academy_id` (`academy_id`)
);
CREATE TABLE IF NOT EXISTS `main_contact_bridge` (
`academy_id` int(11) NOT NULL,
`contact_id` int(11) NOT NULL,
PRIMARY KEY (`contact_id`,`academy_id`),
KEY `academy_id` (`academy_id`)
);
更新:基于 Emmad Kareem 的回答
CREATE TABLE IF NOT EXISTS `person` (
id int(11) NOT NULL auto_increment,
`academy_id` int(11) NOT NULL,
`person_name` varchar(50) NOT NULL,
`person_email` varchar(50) default NULL,
`person_phone` varchar(15) default NULL,
PRIMARY KEY (`id`),
CONSTRAINT `academy_id` FOREIGN KEY (`academy_id`) REFERENCES `academy` (`academy_id`) ON DELETE CASCADE ON UPDATE CASCADE,
);