学生课程
create database xskc character set=utf8; use xskc; create table s(sno char(2) primary key,sname char(10),ssex char(2),sage smallint,ssdept char(4)); insert into s values('01','AAA','女',17,'IS'); insert into s values('02','BBB','男',18,'IS'); insert into s values('03','CCC','女',17,'CS'); insert into s values('04','DDD','男',19,'CS'); insert into s values('05','EEE','男',18,'CS'); insert into s values('06','FFF','女',17,'CS'); create table c(cno char(3) primary key,cname char(12),cpno char(3),credit smallint); insert into c values('c1','程序设计','c2',2); insert into c values('c2','高等数学','c2',3); insert into c values('c3','数据结构','c1',3); insert into c values('c4','离散数学','',2); insert into c values('c5','人工智能','c4',2); create table sc(sno char(2) not null,cno char(3) not null,grade smallint,primary key(sno,cno)); insert into sc values('01','c1',90); insert into sc values('01','c2',80); insert into sc values('01','c3',60); insert into sc values('02','c1',80); insert into sc values('02','c2',70); insert into sc values('02','c3',80); insert into sc values('03','c1',80); insert into sc values('03','c3',70); insert into sc values('01','c4',70); insert into sc values('01','c5',70); #1. 查询所有课程成绩都小于60分的学生的学号、姓名; select sno,sname from s where sno not in (select sno from sc where grade >= 60) and sno in (select sno from sc); #2. 查询没有选修课程的学生的学号、姓名; select sno,sname from s where sno not in (select sno from sc); #3. 查询每门课程选修人数,格式为课程名称,人数; select c.cname,count(sc.cno) 人数 from c,sc where c.cno=sc.cno group by sc.cno; #4. 查询两门以上不及格课程的同学的学号及其平均成绩 ; select sno,avg(grade) from sc where sno in (select a.sno from sc a,sc b where a.sno=b.sno and a.grade<60 and b.grade<60 and a.cno!=b.cno) group by sno; #5. 查询每门课程成绩最高的学生学号、课程号、成绩; select sno,sc.cno,grade from sc,(select cno,max(grade) zgf from sc group by cno) as a where sc.cno=a.cno and sc.grade=zgf; #6. 查询所有学生学号,姓名,以及年龄(其中年龄>=19,显示偏大,否则显示刚好) select sno,sname,sage,if(sage>=19,'偏大','刚好') 年龄 from s; #7. 建立视图v1,包含所有选修C1课程的学生学号,姓名,所在系,并且要求在对视图更新时检查建立视图的条件 create or replace view v1 as select s.sno,s.sname,s.ssdept from s,sc where s.sno=sc.sno and cno='c1' with check option; select * from v1; #8. 查询选修了全部课程的学生学号 select count(*) into @s from c; select sno from sc group by sno having count(sno)=@s;
仓库管理
create database ckgl character set=utf8; use ckgl; create table warehouse(wnum char(3) primary key,area smallint null,city char(12)); insert into warehouse values('01',300,'北京'); insert into warehouse values('02',500,'天津'); insert into warehouse values('03',200,'上海'); insert into warehouse values('04',300,'北京'); insert into warehouse values('05',600,'上海'); create table employee(enum char(4) primary key,wnum char(2),salary smallint); insert into employee values('z1','01',3000); insert into employee values('z2','01',3500); insert into employee values('z3','01',5000); insert into employee values('z4','02',4000); insert into employee values('z5','02',2000); insert into employee values('z6','03',3000); insert into employee values('z7','03',6000); create table orders(onum char(5) primary key,enum char(3),snum char(3),amount int); insert into orders values('d1','z1','s1',8000); insert into orders values('d2','z1','s2',6000); insert into orders values('d3','z2','s1',10000); insert into orders values('d4','z2','s2',5000); insert into orders values('d5','z2','s3',20000); insert into orders values('d6','z3','s1',5000); create table supplier(snum char(4) primary key,sname char(14),city char(12)); insert into supplier values('s1','晨星公司','北京'); insert into supplier values('s2','华大公司','天津'); insert into supplier values('s3','芯片生产厂','北京'); insert into supplier values('s4','全能公司','重庆'); #1查询低于本仓库平均工资的职工信息 select a.* from zg a where 工资<(select avg(工资) from zg b where b.仓库号=a.仓库号); #2查询北京仓库职工的平均工资 select avg(工资) from zg where 仓库号 in (select 仓库号 from ck where 城市='北京'); #3按面积降序查询仓库信息 select * from ck order by 面积 desc; #4查询城市仓库总面积超过800的城市名和仓库面积和 select 城市,sum(面积) from ck group by 城市 having sum(面积)>800; #5查询每个仓库的职工人数,查询结果按人数降序排序 select 仓库号,count(*) 人数 from zg group by 仓库号 order by 人数 desc; #6查询职工工作的仓库和供应商地址在同一城市的职工和供应商所签订的订单信息 SELECT dd.* from ck,zg,dd,gys where ck.仓库号=zg.仓库号 and zg.职工号=dd.职工号 and dd.供应商号=gys.供应商号 and ck.城市=gys.城市; #7查询北京仓库工作的职工所签订单金额总和 select sum(金额) from dd where 职工号 in (select 职工号 from zg where 仓库号 in (select 仓库号 from ck where 城市='北京')); #8查询和职工'E1'签了订单,并且金额高于10000的供应商名 select 供应商名 from dd,gys where dd.供应商号=gys.供应商号 and 职工号='E1' and 金额>10000; #9查询至少签了两条订单的职工姓名 select 姓名 from zg where 职工号 in (select 职工号 from dd group by 职工号 having count(*)>=2); #10查询出在'c1'仓库工作的'姓张'的职工信息 select * from zg where 仓库号='c1' and 姓名 like '张%';