diff --git "a/\346\225\260\346\215\256\345\272\223\351\253\230\347\272\247\347\254\254\344\270\200\350\257\276\347\254\224\350\256\260.md" "b/\346\225\260\346\215\256\345\272\223\351\253\230\347\272\247\347\254\254\344\270\200\350\257\276\347\254\224\350\256\260.md" new file mode 100644 index 0000000000000000000000000000000000000000..9b06e10d197b43bcae2a8e90965bbcbcf9310ebc --- /dev/null +++ "b/\346\225\260\346\215\256\345\272\223\351\253\230\347\272\247\347\254\254\344\270\200\350\257\276\347\254\224\350\256\260.md" @@ -0,0 +1,11 @@ +# 开学第一课笔记 + +```mysql +大二学习计划: +1、做好学习规划 +2、安排合理的学习时间 +3、做到让效率达到最高 +4、不限于书本知识 +5、自学其他语言和框架 +``` + diff --git "a/\346\225\260\346\215\256\345\272\223\351\253\230\347\272\247\347\254\254\344\272\214\350\257\276\347\254\224\350\256\260.md" "b/\346\225\260\346\215\256\345\272\223\351\253\230\347\272\247\347\254\254\344\272\214\350\257\276\347\254\224\350\256\260.md" new file mode 100644 index 0000000000000000000000000000000000000000..903b846f611641fbe1d71a47fb8b7840dad03eac --- /dev/null +++ "b/\346\225\260\346\215\256\345\272\223\351\253\230\347\272\247\347\254\254\344\272\214\350\257\276\347\254\224\350\256\260.md" @@ -0,0 +1,100 @@ +# 数据库高级第二课笔记 + +```mysql +1、一对多:是最基础的表间关系,意思是一张表A中的一条记录可以对应另一张表B中的多条记录,另一张表B中的一条记录只能对应一张表A中的一条记录 +2、一对多:在多的那一端设置外键 +3、多对多:引入第三张表,将两个主键放进来当外键 +E-R图组成:实体,关系,属性 + + +总结 +在写sql语句中最重要的就是找表之间的关系,只有搞清楚各种表之间的联系,才不容易出错; + +这两句口诀再强调一下: + +一对多,两张表,多的表加外键 + +多对多,三张表,关系表加外键 + + + + + + + +use blog; +# ----------------------------------------院系表---------------------------- + +create table faculty( + id int primary key auto_increment, + faculty_name char(20) +); +# ----------------------------------------专业表---------------------------- + +create table specialty( + id int primary key auto_increment, + specialty_name char(20), + specialty_id int, + foreign key (specialty_id) references faculty (id), + foreign key (specialty_id) references student_info (studentId) +); + +# ----------------------------------------学生表---------------------------- + +# 1、-------------------将专业表的specialty_id和班级表的class_room_id进行连接(一对多)--- +# 2、-------------------将专业表的faculty_id和院系表的id进行连接(一对多)--------------- + +create table class_room( + id int primary key auto_increment , + class_room_name char(20), + class_room_id int, + zhuanye_id int, + foreign key (class_room_id) references specialty (specialty_id), + foreign key (zhuanye_id) references specialty (specialty_id) +); + + + +create table student_info( + studentId int primary key auto_increment , + student_name char(20) +); + +# -------------------------------------学生表和课程表的中间表--------------------- +create table StudentToCourse( + id int primary key auto_increment , + student_id int, + foreign key(student_id) references student_info(studentId), + course_id int, + foreign key(course_id) references course(courseId) +); + +# ----------------------------------------课程表---------------------------- +create table course( + courseId int primary key auto_increment , + course_name char (20), + foreign key (courseId) references teacher (id) +); + + +# ----------------------------------------课程表表---------------------------- +create table class_schedule( + id int primary key, + week_day char (20), + which_lesson char (20) +) + + +# ----------------------------------------教师表---------------------------- +create table teacher( + id int primary key auto_increment , + teacher_name char(10) +) + +# select * from faculty f right join specialty s on f.id = s.specialty_id order by specialty_id; + +select * from +(SELECT f.faculty_name,f.id,s.specialty_name,s.specialty_id,s.zhuanye_id FROM faculty f RIGHT JOIN specialty s ON f.id = s.zhuanye_id) +as a right join class_room cr on a.zhuanye_id = cr.zhuanye_id +``` +