Oracle9i上机操作指南 安徽工业大学计算机学院
*
第 1 行出现错误:
ORA-02292: 违反完整约束条件 (U09074331.SNO_FK) - 已找到子记录
例2-9: (删除数据) 删除Course表中课程号为‘003’ 的课程信息,根据返回信息解释其原因。 SQL> delete from course where cno='003'; delete from course where cno='003' *
第 1 行出现错误:
ORA-02292: 违反完整约束条件 (U09074331.CNO_FK) - 已找到子记录
例2-10: (删除数据) 删除学生表Student_Temp中学号以96打头的学生信息。 Savepoint a;
select * from student_temp; ROLLBACK a;
(此操作后,注意用ROLLBACK回退可能更新的数据)
例2-11: (删除数据) 删除数据库表Student_Temp中所有学生的数据。 Delete from student_temp; Rollback;
例2-12:(删除表) 删除数据库表Student_Temp和Student_Gr。 Drop table student_temp; Drop table cstudent_gr;
实验三:数据查询语言
3. SQL数据查询语句:
例3-1: (选择表中的若干列) 求全体学生的学号、姓名、性别和年龄。 Select sno,sname,ssex,sage From student;
例3-2: (不选择重复行) 求选修了课程的学生学号。 Select distinct sno From score;
例3-3: (选择表中的所有列) 求全体学生的详细信息。 Select * from student:
例3-4: (使用表达式) 求全体学生的学号、姓名和出生年份。 Select sno,sname,(2012-sage) as \
例3-5: (使用列的别名) 求学生的学号和出生年份,显示时使用别名“学号”和“出生年份”。 select sno as \学号\,(2012-sage) as \出生年份\例3-6: (比较大小条件) 求年龄大于19岁的学生的姓名和年龄。
5/10
Oracle9i上机操作指南 安徽工业大学计算机学院
select sname as \姓名\,sage as \年龄\
例3-7: (比较大小条件) 求计算机系或信息系年龄大于18岁的学生的姓名、系和年龄。 select sname as \姓名\,sdept as \系\年龄\and sdept='CS' or sdept= 'IS';
例3-8: (确定范围条件) 求年龄在19岁与22岁(含20岁和22岁)之间的学生的学号和年龄。 select sno,sage from student where sage>=20 and sage<=22; select sno,sage from student where sage between 20 and 22;
例3-9: (确定范围条件) 求年龄不在19岁与22岁之间的学生的学号和年龄。 select sno,sage from student where sage not between 19 and 22; 例3-10:(确定集合条件) 求在下列各系的学生信息:数学系、计算机系。 select * from student where sdept='CS'; select * from student where sdept='MA';
例3-11:(确定集合条件) 求不是数学系、计算机系的学生信息。
select student.sno,sname,sage,sdept,cname,score from student,course,score where student.sno=score.sno and score.cno=course.cno and sdept='MA'; 例3-12:(匹配查询) 求姓名是以“李”打头的学生。 select * from student where sname like '李%'; 例3-13:(匹配查询) 求姓名中含有“志”的学生。 select * from student where sname like '%志%';
例3-14:(匹配查询) 求姓名长度至少是三个汉字且倒数第三个汉字必须是“马”的学生。 select * from student where sname like '%马__' and sname like '___%';
例3-15:(匹配查询) 求选修课程001或003,成绩在80至90之间,学号为96xxx的学生的学号、课程号和成绩。
select * from score where cno in('001','003') and score between 80 and 90 and sno like '96%'; 例3-16:(匹配查询) 求课程名中包含 ’_’ 字符的课程号、课程名和学时数。 select * from course where cname like '%!_%' escape '!'; 例3-17:(涉及空值查询) 求缺少学习成绩的学生的学号和课程号。 select * from score where score=null;
例3-18:(控制行的显示顺序) 求选修003课程或004课程的学生的学号、课程号和分数,要求按课程号升序、分数降序的顺序显示结果。
select * from score where cno in ('003','004') order by cno asc,score desc; select * from score where cno in ('003','004') order by cno asc; select * from score where cno in ('003','004') order by score desc; 例3-19:(组函数) 求学生总人数。 Select count(sno) from student;
6/10
Oracle9i上机操作指南 安徽工业大学计算机学院
例3-20:(组函数) 求选修了课程的学生人数。 select count(distinct sno) from score; 例3-21:(组函数) 求计算机系学生的平均年龄。 Select avg(sage) from student;
例3-22:(组函数) 求选修了课程001的最高、最低与平均成绩。 select max(score),min(score),avg(score) from score where cno='001'; 例3-23:(分组查询) 求各门课程的平均成绩与总成绩。 select cno,sum(score),avg(score) from score group by cno; 例3-24:(分组查询) 求各系、各班级的人数和平均年龄。 select sdept,count(sno),avg(sage) from student group by sdept; select sclass,count(sno),avg(sage) from student group by sclass;
例3-25:(分组查询) 输入以下查询语句并执行,观察出现的其结果并分析其原因。 SELECT SNAME,SDEPT,COUNT(*)FROM STUDENT WHERE SDEPT=’CS’ GROUP BY SDEPT;
SQL> SELECT SNAME,SDEPT,COUNT(*)FROM STUDENT WHERE SDEPT=’CS’ GROUP BY SDEPT; WHERE SDEPT=’CS’ GROUP BY SDEPT *第 2 行出现错误:ORA-00911: 无效字符
例3-26:(分组查询) 分析以下语句为什么会出现错误。并给出正确的查询语句。 SELECT SAGE FROM STUDENT GROUP BY SNO; SQL> SELECT SAGE FROM STUDENT GROUP BY SNO; SELECT SAGE FROM STUDENT GROUP BY SNO *
第 1 行出现错误:ORA-00979: 不是 GROUP BY 表达式
例3-27:(分组查询) 求学生人数不足3人的系及其相应的学生数。
select sdept,count(sno) from student group by sdept having count(sno)<3; 例3-28:(分组查询) 求各系中除01班之外的各班的学生人数。
select sclass,count(sno) from student where sclass<>'01' group by sclass;
例3-29:(涉及空值的查询) 分别观察各组函数、行的显示顺序以及分组查询与空值的关系。 例3-30:(连接查询) 求选修了课程001且成绩在70分以下或成绩在90分以上的学生的姓名、课程名称和成绩。
select sname,cname,score from student,score,course where student.sno=score.sno and course.cno=score.cno and score not between 70 and 90;
例3-31:(连接查询与表的别名) 求选修了课程的学生的学生姓名、课程号和成绩。 例3-32:(自身连接查询) 求年龄大于 ’李丽’ 的所有学生的姓名、系和年龄。 select x.sname,x.sdept,x.sage from student x,student y where x.sage>y.sage and y.sname='李丽';
7/10
Oracle9i上机操作指南 安徽工业大学计算机学院
例3-33:(外部连接查询) 求选修了课程002或003的学生的学号、课程号、课程名和成绩,要求必须将002和003课程的相关信息显示出来。
select sno,course.cno,cname,score from course,score where score.cno=course.cno(+) and score.cno in ('002','003');
例3-34:(子查询) 求与 ‘黎明’ 年龄相同的学生的姓名和系。
select sname,sdept from student
where sage in(select sage
from student where sname='黎明');
例3-35:(子查询) 求选修了课程名为 ’数据结构’ 的学生的学号和姓名。
8/10
Oracle9i上机操作指南 安徽工业大学计算机学院
select student.sno,sname from student,course,score where student.sno=score.sno and score.cno=course.cno and course.cname='数据结构';
例3-36:(子查询ANY) 求比数学系中某一学生年龄大的学生的姓名和系。
select sname,sdept from student where sage>any(select sage from student where sdept='MA'); 例3-37:(子查询ALL) 求比数学系中全体学生年龄大的学生的姓名和系。
select sname,sdept from student where sage>all(select sage from student where sdept='MA');
9/10