create table s(sno number(10,0) primary key,sname varchar2(10),birthday date,age number(3,0),sex varchar2(2));
create table c(cno number(10,0) primary key,cname varhcar2(10),credit number(20,2),teacher varchar2(10));
create table sc(sno number(10,0) references s(sno),cno number(10,0) references c(cno),grade number(5,2),seldate date);
create trigger tri_sc
before insert or update on sc
for each row
declare
exc1,exc2 exception;
temp_count1,temp_count2 number;
begin
select count(*) into temp_count1 from sc where :new.sno=sc.sno group by sno;
select count(*) into temp_count2 from sc where :new.cno=sc.cno group by cno;
if temp_count1 >=5 then
raise exc1;
end if;
if temp_count2 >=3 then
raise exc2;
end if;
exception
when exc1 then
dbms_output.put_line('最多只能选修5门课程'); --异常处理这部分你可以自己决定咯
when exc2 then
dbms.output.put_line('选修本门课程人数已满');
end;
create trigger tri_s
before update on s
for each row
declare
exc exception;
begin
if :new.age < :old.age then
raise exc;
end if;
exception
when exc then
dbms_output.put_line('年龄只能增加');
end;
(1)
create trigger triggrnameone on sc
for insert
as
begin
declare @insno varchar(20)
select @insno = sno from inserted
declare @count int
select @count = count(*) from sc where sno = @insno
if @count > 5
begin
rollback
print '该学生选课不能超过五门'
end
end
(2)
create trigger triggernametwo on s
for update
as
begin
if update(age)
begin
declare @oldage int
declare @newage int
select @oldage = age from deleted
select @newage = age from inserted
if @newage < @oldage
begin
rollback
print "更改后的年龄要比原值大"
end
end
end
(3)--只考虑了修改课程号,没有考虑修改学生号
create trigger triggernaemthree on sc
for update
as
begin
if update(cno)
begin
declare @cno varchar(10)
declare @count int
select @cno = cno from inserted
select @count = count(*) from sc where cno =@cno
if @count > 3
begin
rollback
print "课程选修人数不超过3 ,请重新选择"
end
end
end
你确定是用触发器吗?
reference就是引用,用于在创建数据表的时候主外键关联的。