MySQL———作业实验

一、创建数据库表

1.创建数据库

mysql> create database mydb11_stu;

mysql> use mydb11_stu;

2.建表

(1)创建student表

mysql> create table student (

-> id int(10) not null unique primary key,

-> name varchar(20) not null,

-> sex varchar(4),

-> birth_year int,

-> department varchar(20),

-> address varchar(50)

-> );

2)创建score表

mysql> create table score (

-> id int(10) not null unique primary key auto_increment,

-> stu_id int(10) not null,

-> c_name varchar(20),

-> grade int(10)

-> );

二、插入数据

1.向student表插入记录如下:

mysql> insert into student values (901, '张三丰', '男', 2002, '计算机系', '北京海淀区');

mysql> insert into student values (902, '李四平', '男', 2000, '中文系', '北京顺义区');

mysql> insert into student values (903, '王小明', '男', 2003, '英语系', '北京朝阳区');

mysql> insert into student values (904, '李白', '男', 1999, '英语系', '辽宁省鞍山市');

mysql> insert into student values (905, '赵云', '男', 2004, '艺术系', '湖南省衡阳市');

mysql> insert into student values (906, '黄蓉', '女', 1998, '计算机', '湖南省常德市');

2.向score表插入记录如下:

mysql> insert into score values (null, 901, '计算机', 98);

mysql> insert into score values (null, 901, '英语', 80);

mysql> insert into score values (null, 902, '计算机', 65);

mysql> insert into score values (null, 902, '中文', 88);

mysql> insert into score values (null, 903, '中文', 94);

mysql> insert into score values (null, 904, '计算机', 78);

mysql> insert into score values (null, 905, '英语', 85);

mysql> insert into score values (null, 906, '计算机', 94);

mysql> insert into score values (null, 906, '英语', 83);

三、查询

1.分别查询student表和score表的所有记录

mysql> select * from student;

mysql> select * from score;

2.查询student表的第2条到5条记录

mysql> select * from student limit 1, 4;

3.从student表中查询计算机系和英语系的学生的信息

mysql> select * from student where department in ('计算机系', '英语系');

4.从student表中查询年龄小于22岁的学生信息

mysql> select * from student where (year(curdate()) - birth_year) < 22;

5.从student表中查询每个院系有多少人

mysql> select department, count(*) as student_count from student group by department;

6.从score表中查询每个科目的最高分

mysql> select c_name, max(grade) as highest_grade from score group by c_name;

7.查询李广昌的考试科目(c_name)和考试成绩(grade)

mysql> select c_name, grade from score where stu_id = (select id from student where name = '李广昌');

8.用连接的方式查询所有学生的信息和考试信息

mysql> select s.*, sc.c_name, sc.grade

-> from student s

-> join score sc on s.id = sc.stu_id;

9.计算每个学生的总成绩

mysql> select stu_id, sum(grade) as total_grade from score group by stu_id;

10.计算每个考试科目的平均成绩

mysql> select c_name, avg(grade) as average_grade from score group by c_name;

11.查询计算机成绩低于95的学生信息

mysql> select s.* from student s join score sc on s.id = sc.stu_id where sc.c_name = '计算机' and sc.grade < 95;

12.将计算机考试成绩按从高到低进行排序

mysql> select * from score where c_name = '计算机' order by grade desc;

13.从student表和score表中查询出学生的学号,然后合并查询结果

mysql> select id as stu_id from student union select stu_id from score;

14.查询姓张或者姓王的同学的姓名、院系和考试科目及成绩

mysql> select s.name, s.department, sc.c_name, sc.grade from student s join score sc on s.id = sc.stu_id where s.name like '张%' or s.name like '王%';

15.查询都是湖南的学生的姓名、年龄、院系和考试科目及成绩

mysql> select s.name, (year(curdate()) - s.birth_year) as age, s.department, sc.c_name, sc.grade from student s join score sc on s.id = sc.stu_id where s.address like '湖南%';

相关推荐
FYKJ_20105 分钟前
springboot雅集社区养老管理系统04456-计算机课程设计、毕业设计
vue.js·spring boot·python·mysql·typescript·spark·django
FYKJ_201011 分钟前
express皖美特色农产品网售系统53118-计算机课程设计、毕业设计
javascript·vue.js·spring boot·mysql·typescript·spark·express
FYKJ_201044 分钟前
springboot羽毛球场地管理系统00626-计算机课程设计、毕业设计
vue.js·spring boot·python·mysql·typescript·spark·django
FYKJ_20102 小时前
django药饮个性化推荐系统54839-计算机课程设计、毕业设计
vue.js·spring boot·python·mysql·typescript·spark·django
IT枫斗者枫哥7 小时前
MyBatis一对多分页:LIMIT 20,为什么凑不齐20个订单?
java·数据库
Flynt7 小时前
"MySQL搜不动就上ES"?我先在50万行数据上测了它自带的ngram全文索引
mysql·elasticsearch·搜索引擎
Y007 小时前
PostgreSQL 的锁:为什么你的 ALTER TABLE 会卡住,以及怎么查
数据库·postgresql
imDwAaY7 小时前
Redo Log 和 Binlog 为什么需要两阶段提交?
后端·mysql
灰原喜欢柯南7 小时前
OceanBase 运维管理知识点——OCP 监控、诊断与变更控制
数据库·oceanbase
码农-0047 小时前
Springboot 集成 Ehcache操作数据库显示SQL语句设置
数据库·spring boot·sql