数据库练习(二)

建表  teacher ,student ,classes

create table teacher(
   tno varchar2(30) primary key ,
   tname varchar2(50),
   tsex varchar2(5),
   tage number(3),
   tsal number(6,2),
   tpostion varchar2(30),
   cno varchar2(30)
);

create table classes(
   cno varchar2(30) primary key,
   cname varchar(100)
);

create table STUDENT
(
  SNO      VARCHAR2(20) primary key,
  SNAME    VARCHAR2(30) not null,
  SEX      VARCHAR2(10),
  AGE      NUMBER(3),
  CNO    VARCHAR2(30)
);

准备工作插入数据

--1、给teacher表添加20条记录,要求有3条记录cno没有值,2条记录cno的值与Classes表中的cno值不匹配,剩余15条记录的cno取 Classes表中的部分cno,不要全取
insert into teacher values('t0001','李累','男','28','2348.65','讲师','');
insert into teacher values('t0002','孙丽','女','26','3348.65','讲师','');
insert into teacher values('t0003','王伟','男','36','6348.65','教授','');
insert into teacher values('t0004','李刚','男','34','4348.65','讲师','c0008');
insert into teacher values('t0005','刘艳','女','28','4348.65','讲师','c0010');

insert into teacher values('t0006','李磊','男','28','2348.65','讲师','c0001');
insert into teacher values('t0007','杨磊','男','32','1348.65','讲师','c0002');
insert into teacher values('t0008','赵超','男','36','6348.65','教授','c0003');
insert into teacher values('t0009','张超','男','34','9348.65','讲师','c0004');
insert into teacher values('t0010','江燕','女','24','4348.65','讲师','c0005');

insert into teacher values('t0011','孙伟超','男','28','5148.65','讲师','c0006');
insert into teacher values('t0012','杨丽荣','男','32','948.65','讲师','c0002');
insert into teacher values('t0013','刘子杰','男','36','7348.65','教授','c0003');
insert into teacher values('t0014','钱学森','男','68','9999.9','教授','c0004');
insert into teacher values('t0015','江一燕','女','20','3348.65','讲师','c0006');

insert into teacher values('t0016','小鱼儿','男','18','5148.65','讲师','c0001');
insert into teacher values('t0017','花无缺','男','19','4948.65','讲师','c0001');
insert into teacher values('t0018','李大嘴','男','36','8348.65','教授','c0003');
insert into teacher values('t0019','导演','男','68','348.65','教授','c0002');
insert into teacher values('t0020','王丽娟','女','20','3348.65','讲师','c0001');

--2、给classes表中添加7条记录,分别创建"软件工程","JAVA开发","IOS开发","android开发","HTML5开发","PHP开发",".NET开发"
insert into classes values('c0001','软件工程');
insert into classes values('c0002','JAVA开发');
insert into classes values('c0003','IOS开发');
insert into classes values('c0004','android开发');
insert into classes values('c0005','HTML5开发');
insert into classes values('c0006','PHP开发');
insert into classes values('c0007','.NET开发');
--3、给student表中添加50条记录,其中8条学生数据,没有选择课程,7条学生数据选择了不存在的课程,剩下的35条数据,随机分散到课程表中的5个课程中
insert into student values('s0001','宋江','男','26','');
insert into student values('s0002','张飞','男','30','c0001');
insert into student values('s0003','吴用','男','26','c0002');
insert into student values('s0004','关羽','男','33','c0003');
insert into student values('s0005','关胜','男','25','c0004');
insert into student values('s0006','林冲','男','25','c0005');
insert into student values('s0007','秦明','男','20','c00006');
insert into student values('s0008','杨志','男','21','c0007');
insert into student values('s0009','关铃','男','18','c0008');
insert into student values('s0010','李师师','女','21','c0009');
insert into student values('s0011','王昭君','女','22','c0001');
insert into student values('s0012','杨玉环','女','19','c0002');
insert into student values('s0013','赵飞燕','女','18','');
insert into student values('s0014','梁红玉','女','20','c0011');
insert into student values('s0015','赵云','男','18','c0003');
insert into student values('s0016','黄忠','男','40','c0005');
insert into student values('s0017','黄盖','男','44','c0007');
insert into student values('s0018','孙尚香','女','20','c0022');
insert into student values('s0019','孙新','男','27','c0033');
insert into student values('s0020','孙立','男','28','c0034');
insert into student values('s0021','杨再兴','男','27','c0035');
insert into student values('s0022','杨继周','男','11','');
insert into student values('s0023','杨延德','男','37','c0003');
insert into student values('s0024','杨延昭','男','25','c0004');
insert into student values('s0025','杨延嗣','男','20','c0005');
insert into student values('s0026','杨宗保','男','13','');
insert into student values('s0027','杨玉真','女','15','');
insert into student values('s0028','杨排风','女','22','c0007');
insert into student values('s0029','杨广','男','24','');
insert into student values('s0030','杨戬','男','82','c0001');
insert into student values('s0031','杨坚','男','42','c0001');
insert into student values('s0032','李元霸','男','11','');
insert into student values('s0033','李元吉','男','17','');
insert into student values('s0034','李世民','男','22','c0002');
insert into student values('s0035','李建成','男','25','c0003');
insert into student values('s0036','李秀宁','女','20','c0004');
insert into student values('s0037','秦琼','男','27','c0007');
insert into student values('s0038','秦用','男','15','c0007');
insert into student values('s0039','薛仁贵','男','22','c0006');
insert into student values('s0040','薛宝钗','女','20','c0001');
insert into student values('s0041','林黛玉','女','18','c0004');
insert into student values('s0042','王熙凤','女','25','c0005');
insert into student values('s0043','贾元春','女','23','c0006');
insert into student values('s0044','贾惜春','女','22','c0006');
insert into student values('s0045','贾探春','女','24','c0005');
insert into student values('s0046','贾迎春','女','22','c0007');
insert into student values('s0047','贾宝玉','男','19','c0001');
insert into student values('s0048','孙悟空','男','72','c0004');
insert into student values('s0049','李靖','男','27','c0003');
insert into student values('s0050','李广','男','31','c0006');

完成下列题目

--实现功能
--1、给"JAVA开发"的老师的工资提高80%
    --select cno from classes where cname='JAVA开发';
    --select TSAL from teacher where cno=(select cno from classes where cname='JAVA开发');
    update teacher set tsal=tsal*1.8 
    where tsal=any(select TSAL from teacher where cno=(select cno from classes where cname='JAVA开发'));

    update teacher set tsal=tsal*1.8 where cno=(select cno from classes where cname='JAVA开发');
--2、将不教课的老师的工资扣"50%"
    --select tno,tsal from teacher where cno is null;
    update teacher set tsal=tsal*0.5 where tsal=any(select tsal from teacher where cno is null);

     update teacher set tsal=tsal*0.5 where cno is null;
--3、查询教"HTML5开发"的姓江的老师信息
    --select cno from classes where cname='HTML5开发';
    select * from teacher where cno=(select cno from classes where cname='HTML5开发') and tname like '江%';
--4、查询每门课程的学生人数
    select cno,count(sno) from student where cno in(select cno from classes) group by CNO;
--5、查询所有老师的平均工资,最低工资,最高工资,工资总和
    select max(tsal) 最高工资,min(tsal) 最低工资,avg(tsal) 平均工资,sum(tsal) 工资总和 from teacher;
--6、查询没有学习任何课程的学生的人数
    select count(sno) from student where cno is null or cno not in(select cno from classes);
--7、查询"JAVA开发"班所有学生名字含有"用"的学生信息和课程信息 
    --显式内连接
    select s.* ,c.CNAME
    from student s
    inner JOIN classes c on s.CNO=c.CNO
    where s.cno=any(select c.cno from classes where c.cname='JAVA开发') and s.SNAME like '%用%';
    --隐式内连接
    select s.*,cname from student s,classes c
    where c.cno=s.cno and sname like '%用' and cname='JAVA开发';
--8、查询"李磊"老师教的课程信息和学生信息
   --显式内连接
    select s.*,c.cname,t.tname
    from student s
    inner join teacher t on t.cno=s.cno
    inner join classes c on c.cno=s.cno
    where t.cno=any(select t.cno from teacher where t.tname='李磊');
    --隐式内连接
    select s.*,s.sname,t.tname from student s ,classes c,teacher t
    where c.cno=s.cno and t.cno=c.cno and t.tname='李磊';
--9、查询学习"IOS开发"的学生的男生人数
    --select cno from classes where cname='IOS开发';
    select count(sno) 男生人数 from student 
    where cno=(select cno from classes where cname='IOS开发') and SEX='男';
--10、查询年龄在15-20,没有学习任何课程的女学生。
    select * from student where( cno not in (select cno from classes) or cno is null) and age between 15 and 20 and sex='女';
--11、查找姓杨的姓名是两个字的同学
    select sname from student where sname like '杨_';
--12、查找年龄比杨广小的女性
    --select age from student where sname='杨广';
    select * from student where age<(select age from student where sname='杨广') and sex='女';
--13、查找任意年龄比李姓同学年龄小的同学
    --select age from student where sname like '李%';
    select sname from student where ageall(select age from student where sname like '贾%');
    select sname,age from student where age >all(select max (age) from student where sname like '贾%');
--15、查询年龄比王熙凤小的所有女性的平均年龄,别名平均年龄
    --select age from student where sname='王熙凤';
    select avg(age) 平均年龄 from student where age<(select age from student where sname='王熙凤' and sex='女'); 
--16、查询选择学科的最多人数 学科名称
    select max(count(*)) from student where cno in (select cno from classes)group by CNO;
    select cno from student group by cno having count(*)=6;
    select cname from classes where cno in('c0001','c007');
    select cname from classes where cno in(select cno from student group by cno having count(*)=
    (select max(count(*)) from student where cno in (select cno from classes)group by CNO));
--17、查询选择无效学科及未选择学科的学生信息
    --select cno from classes;
    select * from student where cno!=all(select cno from classes) or cno is null;
    select * from student where cno not in (select cno from classes) or cno is null;
    select * from student s full join classes c on s.cno=c.cno where c.cname is null;
    select * from student s where s.cno not in(select c.cno from classes c where c.cno=s.cno);
     -- =的运行效率大于in
     --子查询的运行效率链接查询,内连接的运行效率大于外连接
--18、查询查询年龄25以上的各学科学生数量,按从低向高排列
    --select count(sno) from student where age > 25;
    select count(sno) from student 
    where age > 25 group by cno order by count(sno) asc ;
    /*查询语句关键字顺序 
    select 字段 from 表1名
    inner/left/right/full jion 表1 on 表1.字段=表.字段
    where 条件
    group by 分组字段
    having 过滤
    order by 字段
    */
--19、查询选择科目女生大于1人的各学科女生数量(考察having语句)
    select cno,count(*) from student 
    where cno in(select cno from classes) and sex='女'
    group by cno
    having count(*)>1;

 

你可能感兴趣的:(数据库)