用规则在PostgreSQL中创建可查询插入修改删除的表视图(二)

2014-11-24 18:11:23 · 作者: · 浏览: 2
ent(sname,sadds,sphon,semail) values(new.name,new.adds,new.phon,email);
错误: Attribute "email" not found
mydb1=# create rule r_insert_v_president AS ON insert TO v_president do instead
mydb1-# insert into president(sname,sadds,sphon,semail) values(new.name,new.adds,new.phon,new.email);
CREATE RULE
mydb1=# insert into v_president values ('金大中','汉城','050-12345678','金大中@韩国.汉城');
INSERT 17287 1
mydb1=# select * from v_president ;
name | adds | phon | email
--------+------+--------------+------------------
金大中 | 汉城 | 050-12345678 | 金大中@韩国.汉城
(1 行)


mydb1=# select * from president ;
sname | sadds | sphon | semail
--------+-------+--------------+------------------
金大中 | 汉城 | 050-12345678 | 金大中@韩国.汉城
(1 行)


mydb1=# 呵呵,现在可以插入数据了!再来改写、删除!


mydb1=# create rule r_delete_v_president AS ON delete TO v_president do instead
mydb1-# delete from president where sname=old.name;
CREATE RULE
mydb1=# delete from v_president where name ~ '^金';
DELETE 1
mydb1=# select * from v_president ;
name | adds | phon | email
------+------+------+-------
(0 行)


mydb1=# select * from president ;
sname | sadds | sphon | semail
-------+-------+-------+--------
(0 行)


mydb1=# 看来删除是没有问题了!来更新!
mydb1=# create rule r_update_v_president AS ON update TO v_president do instead
mydb1-# update president set sname=new.name,sadds=new.adds,sphon=new.phon,semail=new.email
mydb1-# where sname=new.name;
CREATE RULE
mydb1=# insert into v_president values ('金大中','汉城','050-12345678','金大中@韩国.汉城');
INSERT 17290 1
mydb1=# select * from v_president ;
name | adds | phon | email
--------+------+--------------+------------------
金大中 | 汉城 | 050-12345678 | 金大中@韩国.汉
(1 行)


mydb1=# select * from president ;
sname | sadds | sphon | semail
--------+-------+--------------+------------------
金大中 | 汉城 | 050-12345678 | 金大中@韩国.汉
(1 行)


mydb1=# update v_president set email='金大中@汉城.韩国' where name='金大中';
UPDATE 1
mydb1=# select * from v_president ;
name | adds | phon | email
--------+------+--------------+------------------
金大中 | 汉城 | 050-12345678 | 金大中@汉城.韩国
(1 行)


mydb1=# select * from president ;
sname | sadds | sphon | semail
--------+-------+--------------+------------------
金大中 | 汉城 | 050-12345678 | 金大中@汉城.韩国
(1 行)


mydb1=# 好了,现在我们的视图 v


mydb1=# 好了,现在我们的视图可以查询、插入、修改、删除数据了!用规则还可以做触发器!;
mydb1=#