mysql增删查改练习

建表
班级表
create table class(
    cid int auto_increment unique,
    caption varchar(32) not null default ''
)charset utf8;

insert into class (caption) values ('三年一班'),('三年二班'),('三年三班'),('三年四班');

mysql> select * from class;
    +-----+----------+
    | cid | caption  |
    +-----+----------+
    |   1 | 三年一班  |
    |   2 | 三年二班  |
    |   3 | 三年三班  |
    |   4 | 三年四班  |
    +-----+----------+
    4 rows in set (0.01 sec)
学生表
create table student(
    sid int auto_increment unique,
    sname varchar(32) not null default '',
    gender enum('女','男'),
    class_id int not null default 0,
    
    constraint stu_cla foreign key (class_id) references class(cid)
)charset utf8;

insert into student (sname,gender,class_id) values ('张三','女',1);
insert into student (sname,gender,class_id) values ('李四','女',2);
insert into student (sname,gender,class_id) values ('钢弹','男',3);
insert into student (sname,gender,class_id) values ('张铁蛋','男',3);
insert into student (sname,gender,class_id) values ('张兰兰','女',4);
insert into student (sname,gender,class_id) values ('王二','男',1);

mysql> select * from student;
    +-----+--------+--------+----------+
    | sid | sname  | gender | class_id |
    +-----+--------+--------+----------+
    |   1 | 张三   | 女      |        1 |
    |   2 | 李四   | 女      |        2 |
    |   3 | 钢弹   | 男      |        3 |
    |   4 | 张铁蛋 | 男      |        3 |
    |   5 | 张兰兰 | 女      |        4 |
    |   6 | 王二   | 男      |        1 |
    +-----+--------+--------+----------+
    6 rows in set (0.01 sec)
老师表
create table teacher(
    tid int auto_increment unique,
    tname varchar(32) not null default ''
)charset utf8;

insert into teacher (tname) values ('叶平'),('李琴'),('李翠翠'),('李福贵'),('孙泉');


mysql> select * from teacher;
    +-----+--------+
    | tid | tname  |
    +-----+--------+
    |   1 | 叶平   |
    |   2 | 李琴   |
    |   3 | 李翠翠 |
    |   4 | 李福贵 |
    |   5 | 孙泉   |
    +-----+--------+
    5 rows in set (0.00 sec)
课程表
create table course(
    cid int auto_increment unique,
    cname varchar(32) not null default '',
    teacher_id int not null default 0,
    
    constraint cour_tea foreign key (teacher_id) references teacher(tid)
)charset utf8;

insert into course (cname,teacher_id) values ('生物',1),('体育',5),('物理',4),('语文',2),('英语',3);


mysql> select * from course;
    +-----+-------+------------+
    | cid | cname | teacher_id |
    +-----+-------+------------+
    |   1 | 生物  |          1 |
    |   2 | 体育  |          5 |
    |   3 | 物理  |          4 |
    |   4 | 语文  |          2 |
    |   5 | 英语  |          3 |
    +-----+-------+------------+
    5 rows in set (0.00 sec)
成绩表
create table score(
    sid int auto_increment unique,
    student_id int,
    corse_id int,
    number int not null default 0,
    
    constraint sco_stuid foreign key (student_id) references student(sid),
    constraint sco_corid foreign key (corse_id) references course(cid)
)charset utf8;

insert into score(student_id,corse_id,number) values (1,1,60),(1,2,59),(1,3,100),(1,4,85),(1,5,78);
insert into score(student_id,corse_id,number) values (2,1,77),(2,2,47),(2,3,89),(2,4,76),(2,5,38);
insert into score(student_id,corse_id,number) values (3,1,100),(3,2,84),(3,3,60),(3,4,68),(3,5,71);
insert into score(student_id,corse_id,number) values (3,1,86),(4,2,45),(5,3,39);


mysql> select * from score;
    +-----+------------+----------+--------+
    | sid | student_id | corse_id | number |
    +-----+------------+----------+--------+
    |   1 |          1 |        1 |     60 |
    |   2 |          1 |        2 |     59 |
    |   3 |          1 |        3 |    100 |
    |   4 |          1 |        4 |     85 |
    |   5 |          1 |        5 |     78 |
    |   6 |          2 |        1 |     77 |
    |   7 |          2 |        2 |     47 |
    |   8 |          2 |        3 |     89 |
    |   9 |          2 |        4 |     76 |
    |  10 |          2 |        5 |     38 |
    |  11 |          3 |        1 |    100 |
    |  12 |          3 |        2 |     84 |
    |  13 |          3 |        3 |     60 |
    |  14 |          3 |        4 |     68 |
    |  15 |          3 |        5 |     71 |
    |  16 |          3 |        1 |     86 |
    |  17 |          4 |        2 |     45 |
    |  18 |          5 |        3 |     39 |
    +-----+------------+----------+--------+
    20 rows in set (0.00 sec)   
    
数据库语句练习
-- 1. 查询所有大于60分的学生的姓名和学号 (DISTINCT: 去重)

mysql> select distinct student.sid,student.sname from student left join score on student_id = student.sid where number>60;
    +-----+-------+
    | sid | sname |
    +-----+-------+
    |   1 | 张三  |
    |   2 | 李四  |
    |   3 | 钢弹  |
    +-----+-------+
    3 rows in set (0.01 sec)
-- 2.查询每个老师教授的课程数量 和 老师信息

mysql> select teacher.tid,teacher.tname,count(course.teacher_id) as course_cou from teacher left join course on teacher.tid=teacher_id group by course.teacher_id;                                                                                     
    +-----+--------+------------+
    | tid | tname  | course_cou |
    +-----+--------+------------+
    |   5 | 孙泉   |          1 |
    |   4 | 李福贵 |          1 |
    |   1 | 叶平   |          1 |
    |   3 | 李翠翠 |          1 |
    |   2 | 李琴   |          1 |
    +-----+--------+------------+
    5 rows in set (0.00 sec)
-- 3. 查询学生的信息以及学生所在的班级信息

mysql> select student.sid,student.sname,student.gender,class.caption from class left join student on class_id= class.cid;
    +------+--------+--------+----------+
    | sid  | sname  | gender | caption  |
    +------+--------+--------+----------+
    |    1 | 张三   | 女     | 三年一班 |
    |    6 | 王二   | 男     | 三年一班 |
    |    2 | 李四   | 女     | 三年二班 |
    |    3 | 钢弹   | 男     | 三年三班 |
    |    4 | 张铁蛋 | 男     | 三年三班 |
    |    5 | 张兰兰 | 女     | 三年四班 |
    +------+--------+--------+----------+
    6 rows in set (0.00 sec)
-- 4、学生中男生的个数和女生的个数

mysql> select student.gender,count(student.gender) as gender_sum from class left join student on class_id= class.cid group by student.gender;
    +--------+------------+
    | gender | gender_sum |
    +--------+------------+
    | 女     |          3 |
    | 男     |          3 |
    +--------+------------+
    2 rows in set (0.00 sec)
-- 5、获取所有学习'生物'的学生的学号和成绩;姓名

mysql> select student.sid,student.sname,number from student left join score on student_id= student.sid left join course on corse_id = course.cid where course.cid = 1;
    +-----+-------+--------+
    | sid | sname | number |
    +-----+-------+--------+
    |   1 | 张三  |     60 |
    |   2 | 李四  |     77 |
    |   3 | 钢弹  |    100 |
    |   3 | 钢弹  |     86 |
    +-----+-------+--------+
    4 rows in set (0.00 sec)
-- 6、查询平均成绩大于60分的同学的学号和平均成绩;

mysql> select student.sid,avg(score.number) from student left join score on student_id= student.sid group by score.student_id having avg(score.number)>60;
    +-----+-------------------+
    | sid | avg(score.number) |
    +-----+-------------------+
    |   1 |           76.4000 |
    |   2 |           65.4000 |
    |   3 |           78.1667 |
    +-----+-------------------+
    3 rows in set (0.01 sec)
-- 7、查询姓“李”的老师的个数;

mysql> select count(tname) as li_scount from teacher where tname like '李%';
    +-----------+
    | li_scount |
    +-----------+
    |         3 |
    +-----------+
    1 row in set (0.00 sec)
-- 8、查询课程成绩小于60分的同学的学号、姓名;

mysql> select student.sid,student.sname from student left join score on student_id= student.sid left join course on corse_id = course.cid where score.number<60;
    +-----+--------+
    | sid | sname  |
    +-----+--------+
    |   1 | 张三   |
    |   2 | 李四   |
    |   2 | 李四   |
    |   4 | 张铁蛋 |
    |   5 | 张兰兰 |
    +-----+--------+
    5 rows in set (0.00 sec)
-- 9. 删除学习“叶平”老师课的SC表记录

# 修改之前
mysql> select * from score;
    +-----+------------+----------+--------+
    | sid | student_id | corse_id | number |
    +-----+------------+----------+--------+
    |   1 |          1 |        1 |     60 |
    |   2 |          1 |        2 |     59 |
    |   3 |          1 |        3 |    100 |
    |   4 |          1 |        4 |     85 |
    |   5 |          1 |        5 |     78 |
    |   7 |          2 |        2 |     47 |
    |   8 |          2 |        3 |     89 |
    |   9 |          2 |        4 |     76 |
    |  10 |          2 |        5 |     38 |
    |  11 |          3 |        1 |    100 |
    |  12 |          3 |        2 |     84 |
    |  13 |          3 |        3 |     60 |
    |  14 |          3 |        4 |     68 |
    |  15 |          3 |        5 |     71 |
    |  16 |          3 |        1 |     86 |
    |  17 |          4 |        2 |     45 |
    |  18 |          5 |        3 |     39 |
    +-----+------------+----------+--------+
    17 rows in set (0.00 sec)
# 修改成功
mysql> delete from score where corse_id in (select course.cid from course left join teacher on course.teacher_id=teacher.tid where teacher.tname='叶平');
    Query OK, 3 rows affected (0.01 sec)

# 修改之后
mysql> select * from score;
    +-----+------------+----------+--------+
    | sid | student_id | corse_id | number |
    +-----+------------+----------+--------+
    |   2 |          1 |        2 |     59 |
    |   3 |          1 |        3 |    100 |
    |   4 |          1 |        4 |     85 |
    |   5 |          1 |        5 |     78 |
    |   7 |          2 |        2 |     47 |
    |   8 |          2 |        3 |     89 |
    |   9 |          2 |        4 |     76 |
    |  10 |          2 |        5 |     38 |
    |  12 |          3 |        2 |     84 |
    |  13 |          3 |        3 |     60 |
    |  14 |          3 |        4 |     68 |
    |  15 |          3 |        5 |     71 |
    |  17 |          4 |        2 |     45 |
    |  18 |          5 |        3 |     39 |
    +-----+------------+----------+--------+
    14 rows in set (0.00 sec)
-- 10.查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分;

mysql> select course.cid,max(number),min(number) from course left join score on corse_id= course.cid group by score.corse_id;
    +-----+-------------+-------------+
    | cid | max(number) | min(number) |
    +-----+-------------+-------------+
    |   1 |         100 |          60 |
    |   2 |          84 |          45 |
    |   3 |         100 |          39 |
    |   4 |          85 |          68 |
    |   5 |          78 |          38 |
    +-----+-------------+-------------+
    5 rows in set (0.00 sec)
-- 11.查询每门课程被选修的学生数

mysql> select course.cid,count(student_id) from course left join score on corse_id= course.cid group by score.corse_id;
    +-----+-------------------+
    | cid | count(student_id) |
    +-----+-------------------+
    |   1 |                 4 |
    |   2 |                 4 |
    |   3 |                 4 |
    |   4 |                 3 |
    |   5 |                 3 |
    +-----+-------------------+
    5 rows in set (0.00 sec)
-- 12.查询姓“张”的学生名单;

mysql> select sname as zhang_count from student where sname like '张%';
    +-------------+
    | zhang_count |
    +-------------+
    | 张三        |
    | 张铁蛋      |
    | 张兰兰      |
    +-------------+
    3 rows in set (0.00 sec)
-- 13.查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列

mysql> select course.cid,avg(number) from course left join score on corse_id= course.cid group by score.corse_id order by avg(number),course.cid desc;
    +-----+-------------+
    | cid | avg(number) |
    +-----+-------------+
    |   2 |     58.7500 |
    |   5 |     62.3333 |
    |   3 |     72.0000 |
    |   4 |     76.3333 |
    |   1 |     80.7500 |
    +-----+-------------+
    5 rows in set (0.00 sec)
-- 14.查询平均成绩大于85的所有学生的学号、姓名和平均成绩

# 因为自己录入的成绩平均分没有85以上,就降低要求  70分以上
mysql> select student.sid,student.sname,avg(number) from student left join score on student_id= student.sid group by student_id having avg(number)>70;
    +-----+-------+-------------+
    | sid | sname | avg(number) |
    +-----+-------+-------------+
    |   1 | 张三  |     76.4000 |
    |   3 | 钢弹  |     78.1667 |
    +-----+-------+-------------+
    2 rows in set (0.00 sec)
-- 15.查询课程编号为3且课程成绩在80分以上的学生的学号和姓名;

mysql> select student.sid,student.sname from student left join score on student_id= student.sid where corse_id = 3 and number >80;
    +-----+-------+
    | sid | sname |
    +-----+-------+
    |   1 | 张三  |
    |   2 | 李四  |
    +-----+-------+
    2 rows in set (0.00 sec)    
-- 16.查询各个课程及相应的选修人数

mysql> select course.cname,count(student_id) as stu_sum from course left join score on corse_id= course.cid group by corse_id;
    +-------+---------+
    | cname | stu_sum |
    +-------+---------+
    | 生物  |       4 |
    | 体育  |       4 |
    | 物理  |       4 |
    | 语文  |       3 |
    | 英语  |       3 |
    +-------+---------+
    5 rows in set (0.00 sec)
-- 17.查询“4”课程分数小于60,按分数降序排列的同学学号

# 因为录入的4课程成绩分数大于60,就换成2课程
mysql> select student.sid from student left join score on student_id= student.sid where corse_id = 2 and number<60 order by student.sid desc;
    +-----+
    | sid |
    +-----+
    |   4 |
    |   2 |
    |   1 |
    +-----+
    3 rows in set (0.00 sec)
-- 18.删除学号为“2”的同学的“1”课程的成绩

# 未修改之前
mysql> select * from score;
    +-----+------------+----------+--------+
    | sid | student_id | corse_id | number |
    +-----+------------+----------+--------+
    |   1 |          1 |        1 |     60 |
    |   2 |          1 |        2 |     59 |
    |   3 |          1 |        3 |    100 |
    |   4 |          1 |        4 |     85 |
    |   5 |          1 |        5 |     78 |
    |   6 |          2 |        1 |     77 |
    |   7 |          2 |        2 |     47 |
    |   8 |          2 |        3 |     89 |
    |   9 |          2 |        4 |     76 |
    |  10 |          2 |        5 |     38 |
    |  11 |          3 |        1 |    100 |
    |  12 |          3 |        2 |     84 |
    |  13 |          3 |        3 |     60 |
    |  14 |          3 |        4 |     68 |
    |  15 |          3 |        5 |     71 |
    |  16 |          3 |        1 |     86 |
    |  17 |          4 |        2 |     45 |
    |  18 |          5 |        3 |     39 |
    +-----+------------+----------+--------+
    18 rows in set (0.00 sec)

# 修改成功
mysql> delete from score where corse_id = 1 and student_id = 2;
    Query OK, 1 row affected (0.01 sec)
    
# 修改成功后
mysql> select * from score;
    +-----+------------+----------+--------+
    | sid | student_id | corse_id | number |
    +-----+------------+----------+--------+
    |   1 |          1 |        1 |     60 |
    |   2 |          1 |        2 |     59 |
    |   3 |          1 |        3 |    100 |
    |   4 |          1 |        4 |     85 |
    |   5 |          1 |        5 |     78 |
    |   7 |          2 |        2 |     47 |
    |   8 |          2 |        3 |     89 |
    |   9 |          2 |        4 |     76 |
    |  10 |          2 |        5 |     38 |
    |  11 |          3 |        1 |    100 |
    |  12 |          3 |        2 |     84 |
    |  13 |          3 |        3 |     60 |
    |  14 |          3 |        4 |     68 |
    |  15 |          3 |        5 |     71 |
    |  16 |          3 |        1 |     86 |
    |  17 |          4 |        2 |     45 |
    |  18 |          5 |        3 |     39 |
    +-----+------------+----------+--------+
    17 rows in set (0.00 sec)

猜你喜欢

转载自www.cnblogs.com/jinhongquan/p/11851683.html