sqldatabasepostgresqlsql-deletesql-view

Delete rows from a table with a view in PostgreSQL


Is it possible to delete rows from a table with a view?

If yes, how can we do that in PostgreSQL?
If no, what is the alternative?


Solution

  • Beginning from version 9.3, views are uptatable in PostgreSql.
    Refer to documentation : http://www.postgresql.org/docs/9.3/static/sql-createview.html (look for "updatable views") to know conditions that must be meet by the view to be "updatable".

    A very simple example:

    create table tbl1  
    ( rollnumber int,  
    name varchar) ;
    
    insert into tbl1 values(1,'abc');  
    insert into tbl1 values(2,'def');  
    insert into tbl1 values(3,'ghi');
    
    create view view1 as select * from tbl1;
    
    
    delete from view1 where rollnumber=2;
    

    working demo: http://sqlfiddle.com/#!15/22c2f/1