数据库第三次作业
创建数据库mydb11_stu并进入数据库:
create database mydb11_stu; use mydb11_stu;
创建student表
create table student(id int(10) not NULL unique primary key, name varchar(20) not NULL, sex varchar(4), birth year, department varchar(20), address varchar(50));
创建score表:
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));
插入数据:
向score表插入:
insert into score values(null,901,'计算机',98); insert into score values(null,901,'英语',80); insert into score values(null,902,'计算机',65); insert into score values(null,902,'中文',88); insert into score values(null,903,'中文',95); insert into score values(null,904,'计算机',70); insert into score values(null,904,'英语',92); insert into score values(null,905,'英语',94); insert into score values(null,906,'计算机',49); insert into score values(null,906,'英语',83);
向student 表插入:
insert into student values(901,'张三丰','男',2002,'计算机系','北京市海淀区'); insert into student values(902,'周全有','男',2000,'中文系','北京市昌平区'); insert into student values(903,'张思维','女',2003,'中文系','湖南省永州市'); insert into student values(904,'李广昌','男',1999,'英语系','辽宁省皋新市'); insert into student values(905,'王翰','男',2004,'英语系','福建省厦门市'); insert into student values(906,'王心凌','女',1998,'计算机系','湖南省衡阳市');
查询:
(1)
select * from student; select * from score;
(2)
select * from student order by id limit 1,4;
(3)
select * from student where department in ('计算机系','英语系');
(4)
select * from student where 2025-birth<22;
(5)
select department ,count(*) as 人数 from student group by department;
(6)
select c_name,max(grade) as 最高分 from score group by c_name;
(7)
select s.c_name,s.grade from score s join student st on s.stu_id=st.id where st.name='李广昌';
(8)
select st.*,s.c_name,s.grade from student st join score s on st.id=s.stu_id;
(9)
select st.name,sum(s.grade) as 总成绩 from student st join score s on st.id=s.stu_id group by st.id;
(10)
select c_name,avg(grade) as 平均成绩 from score group by c_name;
(11)
select st.* from student st join score s on st.id=s.stu_id where s.c_name='计算机' and s.grade<95;
(12)
select st.name,s.grade from student st join score s on st.id=s.stu_id where s.c_name='计算机' order by s.grade desc;
(13)
select id as 学号 from student union select stu_id from score;
(14)
select st.name,st.department,s.c_name,s.grade from student st join score s on st.id=s.stu_id where st.name like '张%' or st.name like '王%';
(15)
select st.name,2025-st.birth as 年龄,st.department,s.c_name,s.grade from student st join score s on st.id=s.stu_id where st.address like '%湖南省%';