回答

收藏

一对多/多对多SQL

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

我正在使用mysql并遇到一些混乱。我创建了两个表academy和courses。我需要确定如何构造表字段的帮助。例如one to5 @  V7 j* ]( X
many架构。一个学院可以提供许多课程,并且可以为很多学院提供一门课程。下表的结构是否正确?; t9 i# r) b' f
create table academy) Z5 N! e& [+ M; N( a* @9 w
(, N4 y2 F4 M" o  {
  academy_id int(11) not null auto_increment,
; T* t( d6 `( l# Z: G5 S9 A/ n& [) o& ?  course_id int()  NOT NULL ,+ I/ o2 c2 q* u0 ~
  name varchar(25) not null,
" N7 r7 N' _) U. u# N& y  primary key (id),. T2 W8 E7 y9 a, _4 P+ D
);1 x$ E8 G. M: x( X
CREATE TABLE course
, t5 ^: q0 r8 ~. R(1 Y. f# N7 a7 C
course_id     int(11) not null auto_increment,4 {9 [# I. b* e- A# ^" y! B
course_name   VARCHAR(50)  NOT NULL ,$ O) r( d8 }5 g. v0 R
primary key (course_id),! A3 J$ R4 [8 m; W# R! R) o
foreign key (academy_id) REFERENCES academy (academy_id) on delete cascade
3 N5 V  I' s8 c);
: p; P3 f% l& |; k- t# u* w# c预期结果示例# ], F0 V# M7 Q. k" J( W
    id Name                  Course
7 J2 T6 I1 e* w; o    1  The Alamo School      125 Intro to Programming
: A6 a- |5 V, P% r& O    2  Bearcat High School   125 Intro to Programming" \# f! x/ `1 w! T/ y+ F/ L5 V
                : x- x* H! i4 U7 e
解决方案:
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则