- 论坛徽章:
- 0
|
[转贴]已经过了两年了,不知道结论还正确吗?
楼上朋友。首先你真正用过事务处理的数据器吗?
你没用过,自然不知,外键,触发器,视图.....有多方便.
drop table taba;
create table taba(
id integer,
name varchar(50)
);
insert into taba values(1,'chen');
insert into taba values(2,'chen');
insert into taba values(3,'chen');
insert into taba values(4,'chen');
insert into taba values(5,'chen');
insert into taba values(6,'chen');
insert into taba values(7,'chen');
select * from taba;
drop table tabb;
create table tabb(
id integer,
name varchar(50)
);
insert into tabb values(1,'chen');
insert into tabb values(2,'chen');
insert into tabb values(3,'chen');
insert into tabb values(4,'chen');
insert into tabb values(5,'chen');
insert into tabb values(6,'chen');
insert into tabb values(7,'chen');
select * from tabb;
drop function delete_tab ();
CREATE FUNCTION delete_tab () RETURNS opaque AS '
-- DECLARE
BEGIN
Delete from tabb where id = OLD.id;
RETURN NEW;
END;
' LANGUAGE 'plpgsql';
drop TRIGGER delete_tri on taba;
CREATE TRIGGER delete_tri
BEFORE Delete
ON taba FOR EACH ROW
EXECUTE PROCEDURE delete_tab ();
delete from taba where id = 2;
select * from tabb;
当表taba 中删除一条数据中。tabb中的数据同时被删除。
如果是MYSQL你得在程序中用两条SQL实现。
delete from taba where id = 2;
delete from tabb where id = 2;
view 可以省去你在读表时烦琐。
pgsql:select * from view where id =1
mysq:select t1.id,t2.uid,t3.name,t4.pass,t5.email from t1,t2,t3,t4,t5 where t1.id=1 and t2.id=1 and
t3.id =1 and t4.id =1 and t5.id =1
过程,函数。
adduse(integer,integer)
begin
insert into user values($1,$2);
......
end;
exec adduser('user1','passwd');
事务
begin
step 1
step 2
...
step 10
commit;
如果任何一步出错。事务被取消,即。去银行取¥。
1插卡
2密码
4取多少¥¥¥
5在帐号里扣除¥
6出钱
7退卡
如果没有事务处理。5出错。你的¥没了。6没出来¥。7退卡。你再一看。卡里原5000¥现在成了2000¥。银行不诚认。你自己哭吧。
这种环境MySQL不行。
外键。没有外键参考。插入,删除的数据可能不一至。
不多说了。。自己想想吧。 |
|