标签:engine null 化学 delete mys core 综合 数学 sid
#Step1-创建数据库LuffyCity_MySQL; #CREATE DATABASE LuffyCity_MySQL CHARSET utf8; #Step2-创建数据表; #年级表; CREATE TABLE class_grade ( gid INT NOT NULL PRIMARY KEY AUTO_INCREMENT, gname VARCHAR (12) NOT NULL UNIQUE ) ENGINE = INNODB CHARSET = utf8; #班级表; CREATE TABLE class ( cid INT NOT NULL PRIMARY KEY AUTO_INCREMENT, caption VARCHAR (16) NOT NULL, grade_id INT NOT NULL, FOREIGN KEY (grade_id) REFERENCES class_grade (gid) ) ENGINE = INNODB CHARSET = utf8; #学生表; CREATE TABLE student ( sid INT NOT NULL PRIMARY KEY AUTO_INCREMENT, sname VARCHAR (12) NOT NULL, gender ENUM (‘男‘, ‘女‘) NOT NULL, class_id INT NOT NULL, FOREIGN KEY (class_id) REFERENCES class (cid) ) ENGINE = INNODB CHARSET = utf8; #老师表; CREATE TABLE teacher ( tid INT NOT NULL PRIMARY KEY AUTO_INCREMENT, tname VARCHAR (8) NOT NULL ) ENGINE = INNODB CHARSET = utf8; #课程表; CREATE TABLE course ( cid INT NOT NULL PRIMARY KEY AUTO_INCREMENT, cname VARCHAR (8) NOT NULL UNIQUE, teacher_id INT NOT NULL, FOREIGN KEY (teacher_id) REFERENCES teacher (tid) ) ENGINE = INNODB CHARSET = utf8; #成绩表; CREATE TABLE score ( sid INT NOT NULL UNIQUE AUTO_INCREMENT, student_id INT NOT NULL, course_id INT NOT NULL, score INT NOT NULL, PRIMARY KEY (student_id, course_id), FOREIGN KEY (student_id) REFERENCES student (sid) ON DELETE CASCADE ON UPDATE CASCADE, FOREIGN KEY (course_id) REFERENCES course (cid) ON DELETE CASCADE ON UPDATE CASCADE ) ENGINE = INNODB CHARSET = utf8; #班级任职表; CREATE TABLE teach2cls ( tcid INT NOT NULL UNIQUE AUTO_INCREMENT, tid INT NOT NULL, cid INT NOT NULL, PRIMARY KEY (tid, cid), FOREIGN KEY (tid) REFERENCES teacher (tid) ON DELETE CASCADE ON UPDATE CASCADE, FOREIGN KEY (cid) REFERENCES course (cid) ON DELETE CASCADE ON UPDATE CASCADE )ENGINE = INNODB CHARSET = utf8;
#Step3-插入测试数据; #年级表; INSERT INTO class_grade (gname) VALUES (‘一年级‘), (‘二年级‘), (‘三年级‘), (‘四年级‘), (‘五年级‘), (‘六年级‘); #班级表; INSERT INTO class (caption, grade_id) VALUES (‘一年级1班‘, 1), (‘一年级2班‘, 1), (‘一年级3班‘, 1), (‘二年级1班‘, 2), (‘一年级2班‘, 2), (‘三年级1班‘, 3), (‘四年级1班‘, 4), (‘四年级2班‘, 4), (‘四年级3班‘, 4), (‘四年级4班‘, 4), (‘五年级1班‘, 5), (‘六年级1班‘, 6), (‘六年级2班‘, 6); #学生表; INSERT INTO student (sname, gender, class_id) VALUES (‘高志粉‘, ‘女‘, 1), (‘李静瓶‘, ‘女‘, 2), (‘崔晓昭‘, ‘男‘, 2), (‘崔晓姗‘, ‘女‘, 2), (‘崔晓思‘, ‘女‘, 2), (‘崔青良‘, ‘男‘, 1), (‘崔晓磊‘, ‘男‘, 3), (‘高志国‘, ‘男‘, 1), (‘崔晓岩‘, ‘女‘, 4), (‘高晨曦‘, ‘女‘, 1), (‘陈浩‘, ‘男‘, 5), (‘陈浩茹‘, ‘女‘, 6), (‘高若曦‘, ‘女‘, 3), (‘武倩倩‘, ‘女‘, 5), (‘武若冰‘, ‘女‘, 5);
#老师表; INSERT INTO teacher (tname) VALUES (‘崔树齐‘), (‘宋俊泽‘), (‘孙增良‘), (‘张传伟‘), (‘邓琼‘); #课程表; INSERT INTO course (cname, teacher_id) VALUES (‘语文‘, 3), (‘数学‘, 1), (‘英语‘, 2), (‘物理‘, 1), (‘化学‘, 2), (‘地理‘, 4), (‘历史‘, 5); #成绩表; INSERT INTO score (student_id, course_id, score) VALUES (1, 1, 60), (1, 2, 38), (2, 2, 78), (2, 3, 90), (3, 5, 85), (4, 4, 59), (4, 2, 49), (4, 3, 70), (5, 5, 55), (6, 1, 45), (7, 3, 91), (8, 3, 82), (8, 4, 73), (9, 5, 50), (10, 4, 58), (11, 3, 57), (11, 2, 56); #班级任职表; INSERT INTO teach2cls (tid, cid) VALUES (1, 1), (1, 2), (1, 3), (2, 2), (2, 3), (2, 1), (3, 4), (4, 6), (4, 5), (5, 3), (5, 4);
标签:engine null 化学 delete mys core 综合 数学 sid
原文地址:https://www.cnblogs.com/tqtl911/p/9388230.html