LuffyCity-MySQL綜合練習50實例
阿新 • • 發佈:2018-07-30
學生 gin arch ble primary 分享 ins png enc
1、請創建如下表,並添加相應約束;
2、自行構造測試數據;
- 新建數據庫
- 創建表
- 構造測試數據
#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, snameVARCHAR (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);
3、查詢練習;
LuffyCity-MySQL綜合練習50實例