diff --git "a/10 \346\270\251\350\264\265\351\233\257/\350\241\250\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.md" "b/10 \346\270\251\350\264\265\351\233\257/\350\241\250\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.md" new file mode 100644 index 0000000000000000000000000000000000000000..0ec6dd257ccddf26f67ec8be1734f7dff1f9ea91 --- /dev/null +++ "b/10 \346\270\251\350\264\265\351\233\257/\350\241\250\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.md" @@ -0,0 +1,144 @@ +# 数据库高级 + +## 表之间的关系: + +### 1.一对一的关系: + + 一个学生(学号,编号,身份证寒外键),只有一个身份证(身份证号)将其中任一表中的主键,放到另一个表当外健 + +### 2.一对多的关系(多对一的关系): + +一个班级 (班级编号),有多个学生(学生编号)班级编号将一所在的表的主键,放到多的表当外键 + +### 3.多对多的关系: + + 一个学生可以选修多门课程,一门课程可以被多个学生选修必须第三张表,将前面两个表的主键放进来当外健 + + + +## 作业 + +以我们学院的组织框架,及学习的课程来做系统,院系,专业,班级,学生,教师,课程,课程表,教室 + +```java + +create database student charset utf8; +use student; + +create table college( + co_id int primary key, + co_name varchar(50) not null +); +insert into college values +(1,"软件工程"), +(2,"医疗护理"), +(3,"信息工程"), +(4,"智能制造"); + +create table major( + m_id int primary key, + m_name varchar(20) not null, + co_id int, + foreign key(co_id) references college(co_id) +); + +insert into major values +(1,"前端",1), +(2,"后端",1), +(3,"财务管理",2), +(4,"大数据技术",3), +(5,"电气自动化技术",4); + + +create table class( + c_id int primary key, + c_name varchar(20) not null, + m_id int, + foreign key(m_id) references major(m_id) +); + +insert intoclass values +(1,"软件技术2班",2), +(2,"软件技术7班",1), +(3,"人力财务管理1班",3), +(4,"大数据技术4班",4), +(5,"电气自动化技术3班",5); + + +create table student( + s_id int primary key, + s_name varchar(10) not null, + sex varchar(2) not null, + c_id int, + foreign key(c_id) references class(c_id) +); + +insert into student values +(1,"张三","男",1), +(2,"李四","男",2), +(3,"王","女",3), +(4,"锅","男",4), +(5,"刘","女",5); + + +create table course_inf( + c_id int primary key, + c_name varchar(10) not null +); + +insert into course_inf values +(1,"MySQL"), +(2,"java"), +(3,"财务管理"), +(4,"大数据"), +(5,"电气自动化"); + + +create table classroom( + cr_id int primary key, + address varchar(20) not null +); +# 插入数据 +insert into classroom values +(1,"望云楼实训室8"), +(2,"望云楼阶"), +(3,"岩声楼"), +(4,"辛耕楼"), +(5,"辛耕楼二楼"); + + +create table teacher( + t_id int primary key, + t_name varchar(5) not null, + sex varchar(2) not null, + co_id int, + foreign key(co_id) references college(co_id) +); + +insert into teacher values +(001,"丘老师","男",1), +(002,"丘老师","男",1), +(003,"刘老师","女",2), +(004,"王老师","男",3), +(005,"李老师","女",4); + + +create table course( + c_id int, + cr_id int, + t_id int, + s_id int, + foreign key(c_id) references course_inf(c_id), + foreign key(cr_id) references classroom(cr_id), + foreign key(t_id) references teacher(t_id), + foreign key(s_id) references student(s_id) +); +insert into course values +(1,1,1,1), +(2,2,2,2), +(3,3,3,3), +(4,4,4,4), +(5,5,5,5); + + +```