日期:2014-05-17  浏览次数:20752 次

求教2个触发器的写法啊,急求
SQL code
create table kehu 
(memberNo  char(18) primary key,
memberName char(8) not null,
memberPassword char(8) not null,
membersex char(3) not null,
membertel char(8) null,
memberaddr varchar(30) null,
yizushuliang int not null check((yizushuliang<7) and (yizushuliang>0))
);



create table pinzhong
(pinzhongNo char(8) primary key
,pinzhongName char(8) not null
,dailyRental Decimal(4,2) not null
);



create table video
(videoNo char(8) primary key 
,videoName char(20) not null
,price number(4,2) not null
,publishtime char(4) not null
,director char(10) not null
,mainactor char(10) not null
,leibie char(10) not null
,publishaddr char(10) not null
,amount int 
,pinzhongNo  char(8)
,constraint fk foreign key(pinzhongNo) references pinzhong(pinzhongNo)
);

create table zujiejilu
(zujieNO char(8) primary key,
videoNo char(8),
memberNo char(18),
dateout date default sysdate,
datereturn date default sysdate+7 ,
yajin decimal(4,2),
zhuangtai char(8) default '未付款',
shishou decimal(4,2) default 0.00,
foreign key(videoNo) references video(videoNo),
foreign key(memberNo) references kehu(memberNo)
);

利用这四张表,在表租借记录中用触发器实现数据更改
具体要求是
1,当zujiejilu插入一条数据时,yajin列自动插入video.price*2的值
2,当改变zhuangtai列的值,根据不同的状态值(已付款,提前归还,按期归还,损坏丢失,超期等)对zujiejilu表执行不同的更改,比如改成已付款时,要将shishou的值改成yajin的值;改成提前归还时,更改datereturn为当前时间,更改shishou=(datereturn-dateout)*dailyRental

------解决方案--------------------
SQL code

create or replace trigger test_trigger
before insert or update on zujiejilu
for each row
declare
video_price number; --价格
shishou number;--实收
datereturn number/Date--什么类型楼主自己定
begin

if inserting then
 select video.price * 2 into video_price from video;
 insert into  tab(.....) values(:new.zujieNO,...,video_price,...);
elseif updating then
 if :new.zhuangtai == '已付款' then
   update zujiejilu set zhuangtai=:new.zhuangtai,shishou=:old.yajin;
 elseif ... then
   ...
 end if;
end if;

end;
/

--说实话,实在没看出楼主的存储过程有啥必须,都是更新自己表,又不是更新关联表,
要什么存储过程啊,你把更新语句判断好,多更新几个字段不就行了吗?

------解决方案--------------------
用一个触发器就可以搞定。
测试数据:
SQL code

create table kehu 
(memberNo  char(18) primary key,
memberName char(8) not null,
memberPassword char(8) not null,
membersex char(3) not null,
membertel char(8) null,
memberaddr varchar(30) NULL

);



create table pinzhong
(pinzhongNo char(8) primary key
,pinzhongName char(8) not null
,dailyRental Decimal(4,2) not null
);



create table video
(videoNo char(8) primary key 
,videoName char(20) not null
,price number(4,2) not null
,publishtime char(4) not null
,director char(10) not null
,mainactor char(10) not null
,leibie char(10) not null
,publishaddr char(10) not null
,amount int 
,pinzhongNo  char(8)

);

INSERT INTO Video VALUES('00000001', '碟中谍4', 20, '2012', '斯皮尔', 
'汤母', '01', 'www.dlut', 3, '03');


create table zujiejilu
(zujieNO char(8) primary key,
videoNo char(8),
memberNo char(18),
dateout date default sysdate,
datereturn date default sysdate+7 ,
yajin decimal(4,2),
zhuangtai char(20) default '未付款',
shishou decimal(4,2) default 0.00
);