Re: [SQL] rules help - Mailing list pgsql-sql

From Brook Milligan
Subject Re: [SQL] rules help
Date
Msg-id 199904271720.LAA19445@trillium.nmsu.edu
Whole thread Raw
In response to Re: [SQL] rules help  (jwieck@debis.com (Jan Wieck))
List pgsql-sql
Thanks for the QUICK response, Jan.  I guess I didn't realize one
could put multiple actions in a rule.
      The strangeness here is, that (for me) it should work without      the  GROUP  BY  clause.  But then I got
multipleentries into      table 2. Exactly the number of rows in table 1, but they  all      referenced the correct
entry.So with your test data I got 2x      Sue in Boston, 3x Bill in Chicago and 3x Tom in Boston.
 

I don't see this problem (NetBSD 1.3.2/i386, postgresql 6.4.2).  See
my script below which does what I really want (with one exception).
      The next problem is, that setting up  another  table  with  2      text fields and doing a
   insert into rule_view select * from addr_data;
      doesn't work at all. Seems the rules aren't triggered and the      data is stored in the view relation.

I'll see if I see that, too.
      And the final problem is that after defining the above rule a      select from pg_rules crashes the backend. Oh
man- think I've      held my hands for too long off of the rule system :-(
 

I see the crash, too.

Now, for the one exception.  The delete rule below has one action
commented out, the removal of the original name.  If this is
uncommented, it is still not removed.  Have I misconstrued that action
or is this something else odd?

Thanks again for your help.

Cheers,
Brook

===========================================================================
drop sequence rule_table_1_id_seq;
drop table rule_table_1;
create table rule_table_1
(id        serial,name        text,
unique (name)
);

drop sequence rule_table_2_id_seq;
drop table rule_table_2;
create table rule_table_2
(id        serial,table_1_id    int4        references rule_table_1 (id),address    text
);

drop view rule_view;
create view rule_view as select a.name, b.address from rule_table_1 a, rule_table_2 b
where a.id = b.table_1_id;

create rule rule_view_insert as on insert to rule_view
do instead (       insert into rule_table_1 (name)           select new.name           where not exists
(selecta.id from rule_table_1 a where a.name = new.name);       insert into rule_table_2 (table_1_id, address)
select b.id, new.address from rule_table_1 b           where new.name = b.name group by 1;
 
);

create rule rule_view_delete as on delete to rule_view
do instead (delete from rule_table_2 where table_1_id = (select id from rule_table_1 where name = current.name);
--    delete from rule_table_1 where name = current.name;
);

create rule rule_view_update as on update to rule_view
do instead (update rule_table_2 set address = new.address where table_1_id = (select id from rule_table_1 where name =
current.name);updaterule_table_1 set name = new.name where name = current.name;
 
);

insert into rule_view (name, address) values ('Tom', 'New York');
insert into rule_view (name, address) values ('Sue', 'Boston');
insert into rule_view (name, address) values ('Bill', 'Chicago');
insert into rule_view (name, address) values ('Tom', 'Boston');
select * from rule_view order by name, address;
select * from rule_table_1 order by name;
select * from rule_table_2 order by table_1_id;

update rule_view set address = 'Boston' where name = 'Bill';
select * from rule_view order by name, address;
select * from rule_table_1 order by name;
select * from rule_table_2 order by table_1_id;

delete from rule_view where name = 'Tom';
select * from rule_view order by name, address;
select * from rule_table_1 order by name;
select * from rule_table_2 order by table_1_id;


pgsql-sql by date:

Previous
From: José Soares
Date:
Subject: Re: [SQL] substring
Next
From: Christophe Labouisse
Date:
Subject: Re: [SQL] Strange behavior