Database Reference
In-Depth Information
We'll see how we can make an updateable view by creating a trigger as follows:
warehouse_db=# CREATE FUNCTION triggerfunc_on_view()
RETURNS trigger AS $$
BEGIN
IF (TG_OP = 'INSERT') THEN
INSERT INTO tab_view VALUES
(NEW.emp_id, NEW.emp_name, NEW.emp_city);
RETURN NEW;
END IF;
RETURN NULL;
END;
$$ LANGUAGE plpgsql;
Now, create a trigger and bind it to the view_select view as follows:
warehouse_db=# CREATE TRIGGER trigger_on_view
INSTEAD OF INSERT ON view_select
FOR EACH ROW
EXECUTE PROCEDURE triggerfunc_on_view();
Let's check what view_select contains at this moment in the following manner:
warehouse_db=# SELECT * FROM view_select;
emp_id | emp_name | emp_city
--------+----------+----------
1 | Adam | Chicago
2 | John | Miami
3 | Smith | Dallas
(3 rows)
Now, try to insert a row in the view_select view as follows:
warehouse_db=# INSERT INTO view_select VALUES
(4, 'Gary', 'Houston');
Curious? Let's check the content of the table to see whether a row has been inserted
in the following manner:
warehouse_db=# SELECT * FROM tab_view;
emp_id | emp_name | emp_city
--------+----------+----------
1 | Adam | Chicago
2 | John | Miami
3 | Smith | Dallas
4 | Gary | Houston
(4 rows)
 
Search WWH ::




Custom Search