前端做网站需要,上海突发新闻,网站开发模块就业前景,200平别墅装修25万效果1、概述 在项目开发中#xff0c;在进行数据库表结构设计时#xff0c;会根据业务需求以及业务模块之间的关系#xff0c;分析并设计表结构#xff0c;由于业务之间相互关联#xff0c;所以各个表结构之间也存在着各种对应关系
2、多表关系分类
#xff08;1#xff0…1、概述 在项目开发中在进行数据库表结构设计时会根据业务需求以及业务模块之间的关系分析并设计表结构由于业务之间相互关联所以各个表结构之间也存在着各种对应关系
2、多表关系分类
1一对多
2多对多
3一对一
3、一对多
1基础举例 4、多对多
1基础举例 2 代码举例
use other;
select database();
create table student(id int auto_increment primary key comment 主键ID,name varchar(10) comment 姓名,no varchar(10) comment 学号
)comment 学生表;
insert into student values (null,lom,42201),(null,kom,42202),(null,jom,42203),(null,hom,42204);create table course(id int auto_increment primary key comment 主键ID,class varchar(10) comment 课程名称
);
insert into course values (null,chinese),(null,math),(null,chinese),(null,english);create table student_course(id int auto_increment primary key comment 主键,StudentId int not null comment 学生ID,CourseId int not null comment 课程ID,constraint fk_CourseId foreign key (CourseId) references course(id),constraint fk_StudentId foreign key (StudentId) references student(id)
)comment 学生课程中间表;
insert into student_course values (null,1,1),(null,1,2),(null,1,4),(null,2,1),(null,2,2),(null,3,1),(null,3,2),(null,3,3),(null,3,4),(null,4,2);
运行之后会建立三个表
student存放着学生信息的表其字段“id”作为student_source表中的StudentId字段的外键 source存放着课程名称其字段“id”作为student_source表中的SourceId字段的外键 student_sources作为一个中间表将student和source两张表连接在一起多对多关系 5、一对一
1基础举例
在创建子表结构时给外键字段设置限制“唯一”unique即可 2代码举例