1、有一个 oracle 的数据库,内有三个表:学生基本信息(学号,姓名,年龄,性别,班级);
课程信息(课程编号,课程名,课程类型,周学时,任课教师,开设学期,考核方式);学
生成绩(学号,课程编号,成绩)
问题一:查询年龄在 20 和 30 之间的学生有多少人,写出此 sql 语句。
select count(年龄)
from 学生基本信息
where 年龄 >=20 and 年龄<=30;
问题二:给学生基本信息表的学号字段添加主键约束,性别字段添加条件约束(即
sex='男' or sex='女'),给学生成绩表的学号字段添加外键约束,其中主表是学生基
本信息的学号,从表是学生成绩表,写出此 sql 语句。
alter table 学生基本信息
constraint student_学号_pk
primary key(学号);
alter table 学生基本信息
constraint student_学号_fk
foreign key(学号) references 学生成绩表(学号);
alter table 学生基本信息
add constraint student_ sex_ck
性别 check(sex='男' or sex='女');
问题三:设计一个行级触发器,在学生成绩表进行插入或更新之后,把不及格学生的信息
保存在另一个表中。
设不及格学生表结构:不及格学生(学号,姓名,性别,课程名,成绩)
create table sco.不及格学生
(
学号 varchar(20),
姓名 varchar(20),
专业 varchar(20),
课程名称 varchar(20),
分数 number(20)
)
create or replace trigger tri_score
aer insert or update on score
for each row
declare
cursor cursor_stu(p_stuno varchar2, p_courno varchar2) is
select student.学号,student.姓名, student.专业,course.课程名称,score.分数
from student,score,course
where student.学号=score.学号 and course.课程编号=score.课程编号
and score.学号=p_stuno and score.课程编号=p_courno;
v_stu cursor_stu%rowtype;
begin
if :new.分数<=60 then
open cursor_stu(:new.学号,:new.课程编号);
fetch cursor_stu into v_stu;
insert into 不及格学生
values(:new.学号,v_stu.姓名, v_stu.专业, v_stu.课程名称, :new.分数);
close cursor_stu;
end if;
end tri_score;