练习1
use 29_db1;
create table student(
id int,
name varchar(20),
chinese float,
english float,
math float
);
-- 2. 请添加2列信息,出生日期,籍贯。
alter table student add birthday date;
alter table student add birthplace varchar(20);
-- 3,请修改语文成绩的数据类型为int型.
alter table student modify chinese int;
-- 4. 请将各科成绩修改为默认值为0 ★★★★
alter table student alter column chinese SET DEFAULT 0;
alter table student alter column math SET DEFAULT 0;
alter table student alter column english SET DEFAULT 0;
-- 5. 请在里面插入10名学生数据。(名字请以汉字输入)
insert into student values (1,"张三",80,90,70,'2002-7-4',"江苏盐城");
insert into student values (2,"李四",80,50,70,'2001-2-11',"北京西城");
insert into student values (3,"王五",80,90,50,'2002-9-7',"浙江衢州");
insert into student values (4,"赵六",49,90,70,'1997-7-6',"福建福州");
insert into student values (5,"赵耀",80,22,70,'1996-4-16',"江苏盐城");
insert into student values (6,"笨笨张",79,49,70,'1998-6-1',"安徽蚌埠");
insert into student values (7,"王二麻子",80,90,70,'2002-7-16',"江苏盐城");
insert into student values (8,"黑哥",80,90,90,'2002-7-16',"四川德阳");
insert into student values (9,"小张伟",57,90,59,'2000-12-16',"江苏盐城");
insert into student values (10,"四书五经",80,32,70,'2002-4-19',"贵州遵义");
desc student;
select * from student;
-- 6. 请分别找出单科成绩最高的学生名单。语文最高的,英语最高的,数学最高的。
select * from student order by chinese desc limit 1;
select * from student order by english desc limit 1;
select * from student order by math desc limit 1;
-- 7. 请找出总成绩最高的学生。
select * from student order by chinese+math+english desc limit 1;
-- 10.假设10名同学中有同姓的,如王,请找出姓王同学的信息.
select * from student where name like '王%';
-- 11.请找出各科不及格学生的信息。
select * from student where chinese<60 and english<60 and math<60;
-- 12.请找出有任何一科不及格学生的名称
select * from student where chinese<60 or english<60 or math<60;
-- 13.请找出两科成绩在90分以上的学生名称。
select name from student where chinese+english>90 or english+math>90 or chinese+math>90;
-- 14.请找出没有一科挂科的学生名称。
select * from student where chinese>=60 and english>=60 and math>=60;
练习2
-- 新建一个学生表S,有包含如下信息
-- 学号 id,
-- 学生姓名 name,
-- 性别 gender,
-- 年龄 age,
-- 专业 dept
create table S(
id int,
name varchar(20),
gender varchar(20),
age int,
dept char(10)
);
insert into S values (1,"张三",'male',20,'信息系');
insert into S values (2,"李四",'female',23,'数学系');
insert into S values (3,"王五",'male',24,'信息系');
insert into S values (4,"赵六",'male',21,'计算机科学系');
insert into S values (5,"田七",'female',17,'数学系');
insert into S values (6,"麻花",'male',28,'计算机科学系');
insert into S values (7,"张雪高",'female',28,'信息系');
insert into S values (8,"二蛋",'male',19,'数学系');
insert into S values (9,"大脚丫子",'female',20,'计算机科学系');
insert into S values (10,"五和",'male',24,'数学系');
select * from S;
-- 1. 查询全体学生的学号与姓名。
select id,name from S;
-- 3. 查询全体学生的详细记录。
select * from S;
-- 5. 查询全体学生的姓名、出生年份和所有系使用列别名改变查询结果的列标题
select name,2021-age as birthyear,dept from S;
-- 7. 查询所有年龄在20岁以下的学生姓名及其年龄。
select name,age from S where age<20;
-- 8. 查询年龄在20~23岁(包括20岁和23岁)之间的学生的姓名、系别和年龄。
select name,dept,age from S where age between 20 and 23;
-- 9. 查询年龄不在20~23岁之间的学生姓名、系别和年龄。
select name,dept,age from S where age not between 20 and 23;
-- 10. 查询信息系、数学系和计算机系学生的姓名和性别。
select name,gender from S where dept in('信息系','数学系','计算机系');
-- 11. 查询既不是信息系、数学系,也不是计算机科学系的学生的姓名和性别。
select name,gender from S where dept not in('信息系','数学系','计算机系');
-- 12. 查询学号为200518的学生的详细情况。
select * from S where id=200518;
-- 13. 查询所有姓刘学生的姓名、学号和性别。
select name,id,gender from S where name like '王%';
-- 14. 查询姓“李”且全名为两个汉字的学生的姓名。
select name from S where name like '李_';
-- 15. 查询名字中第2个字为“立"字的学生的姓名和学号。
select name,id from S where name like '_立%';
-- 16. 查询所有不姓刘的学生姓名。
select name from S where name not like '刘%';
-- 17. 查询学号在201801~201809之间的学生姓名。
select name from S where id between 201801 and 201809;
-- 18. 查询不同院系学生的人数。★★★★
select dept,count(1) from S group by dept;
-- 20. 查询计算机系年龄在20岁以下的学生姓名。
select name from S where age<20 and dept='信息系';
练习3
-- 练习3:某大学研究生院有若干研究生导师,包括职工编号、姓名、职称、研究方向,其中每个导师的职工编号是唯一的。
-- 若干研究生,包括学号、姓名、性别、入学日期,其中每个研究生的学号是唯一的。
-- 每个导师可以带若干研究生,但每个研究生只能有一个导师。
-- 请设计一个数据库,要求可以正确体现导师和研究生之间的关系。
-- 设计完毕之后,请插入一定量的数据,并验证你设计的数据库是否满足要求。
-- 在你设计的表中插入若干数据。
use 29_db2;
create table teacher (
id int primary key auto_increment,
name varchar(20),
title varchar(20),
direation varchar(20)
);
drop table teacher;
create table student(
id int primary key auto_increment,
name varchar(20),
gender varchar(20),
admission date,
supervisor_id int
);
drop table student;
INSERT INTO teacher VALUES (1, '王一', '教授', '人工智能');
INSERT INTO teacher VALUES (2, '谢二', '讲师', '大数据');
INSERT INTO teacher VALUES (3, '张三', '副教授', '计算机图形学');
INSERT INTO teacher VALUES (4, '李四', '研究员', '数据挖掘');
INSERT INTO teacher VALUES (5, '王五', '教授', '人工智能');
INSERT INTO teacher VALUES (6, '赵六', '讲师','物联网');
INSERT INTO student VALUES (1, '赵日天', '男', '2009-09-08', 2);
INSERT INTO student VALUES (2, '叶良辰', '男', '2011-03-04 ', 4);
INSERT INTO student VALUES (3, '龙傲天', '男', '2015-09-04 ', 3);
INSERT INTO student VALUES (4, '马保国', '男', '2019-01-23 ', 1);
INSERT INTO student VALUES (5, '翠花', '女', '2011-04-06 ', 2);
INSERT INTO student VALUES (6, '李雷', '男', '2020-11-14 ', 6);
INSERT INTO student VALUES (7, '韩梅梅', '女', '2020-11-14 ', 4);
INSERT INTO student VALUES (8, '杜子腾', '男', '2020-09-01 ', 6);
INSERT INTO student VALUES (9, '钱多多', '女', '2015-09-04 ', 1);
INSERT INTO student VALUES (10, '李狗蛋', '男', '2017-09-01', 5);
-- 1.请查出每个导师所带研究生的姓名。
select t.name as t_name,s.name as s_name from
teacher as t inner join student as s on t.id = s.supervisor_id
order by s.supervisor_id;
========================================================================
-- 2.清查出特定姓名的导师所带研究生的姓名。
select t.name as t_name,s.name as s_name from
teacher as t inner join student as s on t.id = s.supervisor_id
and t.name like "王%" order by s.supervisor_id;
========================================================================
-- 3.请查出每个导师所带研究生的数量。
select t.name, count(s.name) from teacher as t inner join student as s
on t.id = s.supervisor_id group by s.supervisor_id;
========================================================================
-- 4.请查出每个导师所带的男研究生的数量。
select t.name, count(s.name) from teacher as t inner join student as s
on t.id = s.supervisor_id and s.gender = "男" group by s.supervisor_id;
========================================================================
-- 5.请找出选择哪个研究方向的导师最多。
select direation,count(id) from teacher group by direation order by count(id)
desc limit 1;
========================================================================
-- 6.请统计不同职称的导师的个数。
select title,count(id) from teacher group by title;
练习3_Version2
作业3:某大学研究生院有若干研究生导师,包括职工编号、姓名、职称、研究方向,其中每个导师的职工编号是唯一的。
若干研究生,包括学号、姓名、性别、入学日期,其中每个研究生的学号是唯一的。
每个导师可以带若干研究生,但每个研究生只能有一个导师。
请设计一个数据库,要求可以正确体现导师和研究生之间的关系。
研究生导师表
guider
use 29_db1;
create table guider(
id int primary key auto_increment,
name varchar(20) not null,
title char(25) not null,
direction varchar(20)
);
select * from guider;
研究生表
pg_student
create table pg_student(
id int primary key auto_increment,
name varchar(20) not null,
gender char(5),
entrytime date,
guider_id int
);
select * from pg_student;
设计完毕之后,请插入一定量的数据,并验证你设计的数据库是否满足要求。
在你设计的表中插入若干数据。
insert into guider values(1,"张导师","副教授","人工智能");
insert into guider values(2,"王导师","教授","大数据");
insert into guider values(3,"钱导师","副教授","人工智能");
insert into guider values(4,"林导师","副教授","大数据");
insert into guider values(5,"刘导师","院长教授","Python");
insert into pg_student values(101,"赵学生","female","2020-3-19",1);
insert into pg_student values(102,"钱学生","male","2021-7-9",1);
insert into pg_student values(103,"孙学生","female","2019-7-12",2);
insert into pg_student values(104,"李学生","male","2019-9-19",1);
insert into pg_student values(105,"赵周学生","female","2020-4-13",4);
1.请查出每个导师所带研究生的姓名。
select t.name,s.name from guider as t left join pg_student as s on t.id = s.guider_id;
2.请查出特定姓名的导师所带研究生的姓名。
select t.name,s.name from guider as t inner join pg_student as s on t.id = s.guider_id and t.name like '张导师';
3.请查出每个导师所带研究生的数量。
select t.name,count(1) from guider as t left join pg_student as s on t.id = s.guider_id group by t.name;
4.请查出每个导师所带的男研究生的数量。
select t.name,count(s.gender) from guider as t left join pg_student as s on (t.id = s.guider_id and s.gender = 'male') group by t.name;
5.请找出选择哪个研究方向的导师最多。
select max(t.direction) from guider as t left join pg_student as s on t.id = s.guider_id;
6.请统计不同职称的导师的个数。
select title,count(guider.id) from guider group by title;