码迷,mamicode.com
首页 > 数据库 > 详细

LuffyCity-MySQL综合练习50实例

时间:2018-07-29 23:55:41      阅读:529      评论:0      收藏:0      [点我收藏+]

标签:engine   null   化学   delete   mys   core   综合   数学   sid   

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,
    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);

3、查询练习;

LuffyCity-MySQL综合练习50实例

标签:engine   null   化学   delete   mys   core   综合   数学   sid   

原文地址:https://www.cnblogs.com/tqtl911/p/9388230.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!