我再举一个数据完整性(Integerity)的问题。例如网上报帐的系统,它需要两个相关的表,一个是帐务的记录表(loan_history),一个是帐务的清除表(loan_clear),每当在loan_clear表中增加一个记录条的时候,必须在相应的
loan_history表中清除相应的loan记录。那么我们将该过程写实现在App Server端上怎么办呢?有点难度,来两个SQL语句吧:
insert into loan_clear values(loan_id,*,*,*) (1)
delete from
loan_history lh inner join loan_clear lc where lh.loan_id=lc.loan_id (2)
那么万一在第一条语句执行完后,停电了怎么办(买UPS吧)?万一有人就执行第一条语句怎么办?这些在app Server端令人头痛的问题放到database端就不成问题了,我可以用事务处理来解决:
begin transaction
insert into loan_clear values(loan_id,*,*,*)
delete from
loan_history lh inner join loan_clear lc where lh.loan_id=lc.loan_id
commit transaction
再将这个事务处理的过程写成一个Procedure。或者做个触发器(trigger)
Create Trigger fresh_loan on loan_clear
For insert
delete from
loan_history lh inner join loan_clear lc where lh.loan_id=lc.loan_id
好了,3H都讨论到了,我这里讨论的主要的是SQL Server系列,其实Oracle的体系结构(Frame)和解决的很多方案是和SQL Server相同的。不过一个是用了Transaction SQL,一个是用了PL/SQL。当然性能是有区别的,当然价格也是有区别的(得去找一个economical analysis engineering了)。希望我的这篇拙作能对各位开发者小有作用,不然就当笑谈了吧。
loan_history表中清除相应的loan记录。那么我们将该过程写实现在App Server端上怎么办呢?有点难度,来两个SQL语句吧:
insert into loan_clear values(loan_id,*,*,*) (1)
delete from
loan_history lh inner join loan_clear lc where lh.loan_id=lc.loan_id (2)
那么万一在第一条语句执行完后,停电了怎么办(买UPS吧)?万一有人就执行第一条语句怎么办?这些在app Server端令人头痛的问题放到database端就不成问题了,我可以用事务处理来解决:
begin transaction
insert into loan_clear values(loan_id,*,*,*)
delete from
loan_history lh inner join loan_clear lc where lh.loan_id=lc.loan_id
commit transaction
再将这个事务处理的过程写成一个Procedure。或者做个触发器(trigger)
Create Trigger fresh_loan on loan_clear
For insert
delete from
loan_history lh inner join loan_clear lc where lh.loan_id=lc.loan_id
好了,3H都讨论到了,我这里讨论的主要的是SQL Server系列,其实Oracle的体系结构(Frame)和解决的很多方案是和SQL Server相同的。不过一个是用了Transaction SQL,一个是用了PL/SQL。当然性能是有区别的,当然价格也是有区别的(得去找一个economical analysis engineering了)。希望我的这篇拙作能对各位开发者小有作用,不然就当笑谈了吧。