How to update table when view is updated?

倖福魔咒の 提交于 2019-12-31 02:53:13

问题


I want to update table when her view is updated. I use postgresql/postgis.

I create view.

CREATE VIEW filedata_view
AS SELECT num, id, ST_TRANSFORM(the_geom,900913)
FROM filedata

And now when its updated i want to update TABLE with this data. But i heared that triggers cant be puted in VIEW. So how to do this?

Now i use this function

CREATE OR REPLACE FUNCTION update_table() RETURNS TRIGGER AS ' 
BEGIN 
UPDATE filedata SET id=NEW.id, the_geom=ST_TRANSFORM(NEW.st_transform,70066) where num=NEW.num ;
END;
' LANGUAGE plpgsql;

its fine. But another problem. How to add trigger to view i do this

CREATE TRIGGER up_table AFTER UPDATE ON filedata_view
FOR EACH ROW EXECUTE PROCEDURE update_table ();

but get error

ERROR: "filedata_view" is not a table.

UPDATE

How to set column name AS SELECT num, id, ST_TRANSFORM(the_geom,900913) if i use this i get columns : num,id and st_transform. How to set third column's name to the_geom?


回答1:


For PostgreSQL 9.1 and above, use a DO INSTEAD trigger on the view. View triggers are much less difficult to get right and are less prone to weird problems with multiple evaluation, etc.

For PostgreSQL 9.0 and below, you should use the rule system - see CREATE RULE ... DO INSTEAD. It is generally better to update to 9.1 and use a view trigger if you possibly can, especially for new users. Rules are tricky things.



来源:https://stackoverflow.com/questions/11664302/how-to-update-table-when-view-is-updated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!