1

我正在使用 mysql 并且遇到了一些混乱。我创建了两个表academycourses. 我需要帮助来确定如何构建表格字段。例如one to many架构。一所学院可以提供许多课程,而一门课程可以与许多学院一起提供。下表的结构是否正确?

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 
4

1 回答 1

4

您真正需要的是一个学院表、一个课程表和一个关系表,您可以在其中存储多对多关系。我将查询留给您以获得您正在寻找的结果:)

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
); 
于 2013-11-09T01:24:04.170 回答