[sql]代码库
create table class(
classno number(2)primary key,
cname varchar(10) not null
);
create table student(
stuno number(8) primary key,--主键
sname varchar2(12) not null,
sex char(4) default '女',--默认
birthday char(8),
email varchar2(20) unique,--唯一
score number(5,2),
classno number(2) references class(classno)
);
drop table student;
insert into class
select 1,'一班' from dual union
select 2,'二班' from dual union
select 3,'三班' from dual;
insert into student values
(21,'tom','男','19790203','tom@163.net',89.50,1);
insert into student values
(56,'jerry',default,null,null,0,2);
insert into student values
(33,'wenney','男','19890203','wenney@189.net',null,3);
SELECT * FROM student;
delete from student;
--将student表中所有一班学生的成绩加10分
update student set score=score+10
where classno=(select classno from class where cname='一班');
--删除student表中所有3班出生日期晚于1988年5月12日的学生
delete student where to_date(birthday,'yyyymmdd')>to_date
('19880512','yyyymmdd') and classno=(select classno from class
where cname='三班'
);
--按班级升序 成绩降序排序
select * from student order by classno,score desc;
--查询三班成绩为空的学生
select * from student where classno=(select classno
from class where cname='三班') and score is null;
--查询所有学生的学号,姓名,成绩,班级名称
select s.stuno,s.sname,s.score,d.cname from student s join
class d on s.classno=d.classno;
--按班级统计每个班的人数,最高分,最低分,平均分,按平均分降序排序
select count(*) "人数",max(score) "最高分",min(score) "最低分",
avg(nvl(score,0)) "平均分" from student group by classno
order by avg(nvl(score,0)) desc;
by: 发表于:2017-09-28 14:48:25 顶(0) | 踩(0) 回复
??
回复评论