回答

收藏

一对多/多对多SQL

技术问答 技术问答 458 人阅读 | 0 人回复 | 2023-09-11

我正在使用mysql并遇到一些混乱。我创建了两个表academy和courses。我需要确定如何构造表字段的帮助。例如one to
/ W3 f, @2 S, T. ]' M$ A! O: Vmany架构。一个学院可以提供许多课程,并且可以为很多学院提供一门课程。下表的结构是否正确?1 ]) K6 a  d* D7 x6 I# I
create table academy# a+ q9 t8 K4 j$ \& B3 |
(
5 w3 N, a# a4 @  academy_id int(11) not null auto_increment,% }1 X* u% \* D8 ]& Y+ P/ ^& z( Z
  course_id int()  NOT NULL ,$ m* l5 H4 H* }
  name varchar(25) not null,
0 ^1 M3 Y+ I+ ]+ i, G% n- \  primary key (id),) N# V; _9 f- a/ N. J. |
);
$ \* T+ Q- e# @5 d! p+ [# g8 j4 MCREATE TABLE course6 Y8 u2 @# x) C9 A
(: j$ t- ]1 R  d8 ]: G* f
course_id     int(11) not null auto_increment,; c# O7 D4 `$ @5 P' `
course_name   VARCHAR(50)  NOT NULL ,/ d6 Z8 v3 Q8 Q" M: ?
primary key (course_id),
2 w! j. l8 y" @1 D4 k6 t" Q! W: iforeign key (academy_id) REFERENCES academy (academy_id) on delete cascade1 v9 x2 |/ M& K; ^( x% U
);
6 [- Q! M6 J) k/ u3 y预期结果示例9 B/ y* p! M0 ?0 L9 E
    id Name                  Course; i2 |+ }8 A" ?/ H* A
    1  The Alamo School      125 Intro to Programming
5 T$ M$ F. }; n- U& j/ a    2  Bearcat High School   125 Intro to Programming6 P# q# t3 x% [  w/ j# k8 `
               
3 R$ m" D, i6 J2 |, {解决方案:
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则