Mysql 一对多/多对多SQL
我在使用mysql时遇到了一些困惑。我创建了两个表Mysql 一对多/多对多SQL,mysql,sql,Mysql,Sql,我在使用mysql时遇到了一些困惑。我创建了两个表academy和courses。我需要帮助确定如何构造表字段。例如,一对多模式。一个学院可以提供许多课程,一个课程可以与许多学院一起提供。下表的结构是否正确 create table academy ( academy_id int(11) not null auto_increment, course_id int() NOT NULL , name varchar(25) not null, primary key (id)
academy
和courses
。我需要帮助确定如何构造表字段。例如,一对多
模式。一个学院可以提供许多课程,一个课程可以与许多学院一起提供。下表的结构是否正确
create table academy
(
academy_id int(11) not null auto_increment,
course_id int() NOT NULL ,
name varchar(25) not null,
primary key (id),
);
CREATE TABLE course
(
course_id int(11) not null auto_increment,
course_name VARCHAR(50) NOT NULL ,
primary key (course_id),
foreign key (academy_id) REFERENCES academy (academy_id) on delete cascade
);
期望结果的示例
id Name Course
1 The Alamo School 125 Intro to Programming
2 Bearcat High School 125 Intro to Programming
您真正需要的是一个学院表、一个课程表和一个可以存储多对多关系的关系表。我将查询留给您,以获得您要查找的结果:)
您真正需要的是一个学院表、一个课程表和一个可以存储多对多关系的关系表。我将查询留给您,以获得您要查找的结果:)
现在这是有道理的。我需要第三个表来创建多对多关系。如果我有导师参与,这会适用吗?这现在是有道理的。我需要第三个表来创建多对多关系。如果我有导师参与,这是否适用?
CREATE TABLE academy
(
academy_id int(11) not null auto_increment,
name varchar(25) not null,
primary key (id),
);
CREATE TABLE course
(
course_id int(11) not null auto_increment,
course_name VARCHAR(50) NOT NULL ,
primary key (course_id),
);
CREATE TABLE accademy_course
(
academy_id int(11) not null,
course_id int(11) not null ,
primary key (academy_id, course_id),
foreign key (academy_id) REFERENCES academy (academy_id) on delete cascade,
foreign key (course_id) REFERENCES course (course_id) on delete cascade
);