how to delete from a view

From: "Rick Schumeyer" <rschumeyer(at)ieee(dot)org>
To: "'PgSql General'" <pgsql-general(at)postgresql(dot)org>
Subject: how to delete from a view
Date: 2005-02-09 03:07:58
Message-ID: 002d01c50e54$8f8ad7d0$0200a8c0@dell8200
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

I know how to create a rule to delete from a view. But I can't

figure this one out.

Let's say I have two tables, t1 and t2, and a view tview that

uses columns from both t1 and t2. I don't want users to

have access to t1 and t2, only to tview. I have a rule that

handles inserts and updates to tview, but I can't figure out

how to do the delete. I would like a delete to tview to delete

the row from both tables.

Both t1 and t2 have a field called 'id'. t1 also has a field

called 'type'. tview is defined like

select t1.a, t1.b, t2.c, t2.d from t1, t2 where t1.type=1 and t1.id=t2.id;

tview includes only some rows from t1 but all rows from t2.

If I create a rule like:

create rule tviewdel as on delete to view do instead (

delete from t1 where id=old.id;

delete from t2 where id=old.id;

);

The first delete works. The second delete does not.

I assume that is because the row is no longer in tview

once the row is deleted from one of the underlying tables?

I've tried playing with triggers, but have not found the

right combination.

Any help is appreciated! Also, please let me know

if I'm not making any sense. It's a little late.

Browse pgsql-general by date

  From Date Subject
Next Message Mike Nolan 2005-02-09 03:10:17 Re: checking SQL statement/subexpression validity
Previous Message David Garamond 2005-02-09 02:55:47 checking SQL statement/subexpression validity