Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Data Bases > Pgsql Sql > Re: rule for up...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 2 of 2 Topic 3407 of 3631
Post > Topic >>

Re: rule for update view that updates/inserts into 2 tables

by tharakan@[EMAIL PROTECTED] ("Robins Tharakan") Apr 15, 2008 at 06:03 AM

------=_Part_22136_17421385.1208219635842
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

Hi Chad,

(Pardon me if I am shooting the stars here...)

Don't you think that on each update, you would be creating a new row that
satisfies that very given condition for the view ?

By that I mean that when you do a 'UPDATE... WHERE my_table_id=3D1' the
RUL=
E
now inserts another row with my_table_id =3D 1... which now makes two rows
that satisfy the criteria for the view.

The second time you run the update, the RULE inserts a row (in the
my_audit_table) for each row found (on the second run it'd be two rows)
...
and then so on .

Therefore, you probably want to use this CREATE RULE query instead...

CREATE OR REPLACE VIEW my_view AS
 SELECT t.my_table_id, t.a, t.b, au.audit_id, au.c
   FROM my_table t, my_audit_table au
  WHERE t.my_table_id =3D au.my_table_id
  AND au.audit_id =3D (SELECT max(audit_id) FROM my_audit_table WHERE
au.my_table_id =3D my_audit_table.my_table_id);

Of course this brings us into another problem that the INSERT / UPDATE
statements bomb because of the aggregate that is now there in the view...
and then I am drawing a blank here !

(Note: As mentioned in PG Docs, I have already tried creating a blanket DO
NOTHING rule coupled with Chad's rule as a DO ALSO rule ... but that
doesn'=
t
work either)

Anyone else with some ideas ?

*Robins*

On Mon, Apr 14, 2008 at 10:17 PM, Chad Showalter
<cshowalter@[EMAIL PROTECTED]
>
wrote:

>  I would like to create a rule that, by updating a view, allows me to
> update one table and insert into another.
>
>
>
> The following example illustrates what I'm trying to do:
>
>
>
> --Create Tables
>
> CREATE TABLE my_table
>
> (
>
>                 my_table_id serial,
>
>                 a character varying(255),
>
>                 b character varying(255),
>
> CONSTRAINT my_table_id_pk PRIMARY KEY (my_table_id)
>
> );
>
>
>
> CREATE TABLE my_audit_table
>
> (
>
>                 audit_id serial,
>
>                 my_table_id int,
>
>                 c character varying(255),
>
> CONSTRAINT audit_id_pk PRIMARY KEY (audit_id)
>
> );
>
>
>
> --Create View
>
> CREATE OR REPLACE VIEW my_view AS
>
> SELECT
>
> t.my_table_id,
>
> t.a,
>
> t.b,
>
> au.audit_id,
>
> au.c
>
> FROM
>
>                 my_table t, my_audit_table au
>
> WHERE
>
>                 t.my_table_id =3D au.my_table_id;
>
>
>
> --Create Rules
>
> CREATE OR REPLACE RULE insert_to_my_view AS
>
> ON INSERT TO my_view
>
> DO INSTEAD(
>
> INSERT INTO my_table (a,b)
>
> VALUES(new.a, new.b);
>
> INSERT INTO my_audit_table(my_table_id, c)
>
> VALUES
>
> (currval('my_table_my_table_id_seq'), new.c);
>
> );
>
>
>
> CREATE OR REPLACE RULE update_my_view AS
>
> ON UPDATE TO my_view DO INSTEAD
>
>  ( UPDATE my_table SET
>
>                 a =3D new.a,
>
>                 b =3D new.b
>
> WHERE
>
>                 my_table_id =3D old.my_table_id;
>
> INSERT INTO my_audit_table
>
>                  (my_table_id,
>
>                  c)
>
> VALUES
>
>                  (new.my_table_id,
>
>                  new.c);
>
> );
>
>
>
> --The insert statement below inserts one row into my_table, and one row
> into my_audit_table
>
> --(This works the way I would like)
>
> insert into my_view(a,b,c) values('a contents','b contents', 'c
> contents');
>
>
>
> --The update statement below doesn't work the way I want.
>
> --What I would like this to do is to update one row in my_table, and
> insert
>
> --one row into my_audit table.  It does the update fine, but the insert
t=
o
> my_audit_table
>
> --doesn't work as I had anticipated.
>
> update my_view set a =3D 'new a contents', b =3D 'new b contents', c 
=3D=
 'new c
> contents' where my_table_id =3D 1;
>
>
>
>
>
>
>
>
>
> If I execute the above update statement multiple times, multiple rows
wil=
l
> be
>
> inserted with each call after the first call.
>
>
>
> Specifically,
>
> =B7         after the first call, 1 row is inserted
>
> =B7         after the second call, 2 rows are inserted
>
> =B7         after the third call, 4 rows are inserted
>
> =B7         after the fourth call, 8 rows are inserted... and so on
>
>
>
> The problem is due to the INSERT in the update_my_view rule:
>
>
>
> INSERT INTO my_audit_table
>
>              (my_table_id,
>
>             c)
>
> VALUES
>
>              (new.my_table_id,
>
>              new.c);
>
>
>
> Apparently, "new.my_table_id" in this case references more than one row,
> if more than one row with
>
> the given id already exists in my_audit_table.
>
>
>
> How do I accomplish what I want to accomplish here?  I'd prefer not to
us=
e
> a sp.
>
>
>
> Thanks,
>
> Chad
>
>
>

------=_Part_22136_17421385.1208219635842
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

Hi Chad,<br><br>(Pardon me if I am shooting the stars
here...)<br><br>Don&#=
39;t you think that on each update, you would be creating a new row that
sa=
tisfies that very given condition for the view ?<br><br>By that I mean
that=
 when you do a &#39;UPDATE... WHERE my_table_id=3D1&#39; the RULE now
inser=
ts another row with my_table_id =3D 1... which now makes two rows that
sati=
sfy the criteria for the view.<br>
<br>The second time you run the update, the RULE inserts a row (in the
my_a=
udit_table) for each row found (on the second run it&#39;d be two rows)
...=
 and then so on .<br><br>Therefore, you probably want to use this CREATE
RU=
LE query instead...<br>
<br>CREATE OR REPLACE VIEW my_view AS <br>&nbsp;SELECT t.my_table_id, t.a,
=
t.b, au.audit_id, au.c<br>&nbsp;&nbsp; FROM my_table t, my_audit_table
au<b=
r>&nbsp; WHERE t.my_table_id =3D au.my_table_id<br>&nbsp; AND au.audit_id
=
=3D (SELECT max(audit_id) FROM my_audit_table WHERE au.my_table_id =3D
my_a=
udit_table.my_table_id);<br>
<br>Of course this brings us into another problem that the INSERT / UPDATE
=
statements bomb because of the aggregate that is now there in the view...
a=
nd then I am drawing a blank here !<br><br>(Note: As mentioned in PG Docs,
=
I have already tried creating a blanket DO NOTHING rule coupled with
Chad&#=
39;s rule as a DO ALSO rule ... but that doesn&#39;t work either)<br>
<br>Anyone else with some ideas ? <br>
<br><b>Robins</b><br><br><div class=3D"gmail_quote">On Mon, Apr 14, 2008
at=
 10:17 PM, Chad Showalter &lt;<a
href=3D"mailto:cshowalter@[EMAIL PROTECTED]
">c=
showalter@[EMAIL PROTECTED]
>&gt; wrote:<br><blockquote
class=3D"gmail_quote"=
 style=3D"border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt
0.=
8ex; padding-left: 1ex;">









<div link=3D"blue" vlink=3D"purple" lang=3D"EN-US">

<div>

<p>I would like to create a rule that, by updating a view,
allows me to update one table and insert into another.</p>

<p>&nbsp;</p>

<p>The following example illustrates what I'm trying to
do:</p>

<p>&nbsp;</p>

<p>--Create Tables</p>

<p>CREATE TABLE my_table </p>

<p>(</p>

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=
&nbsp;&nbsp;&nbsp;
my_table_id serial,</p>

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=
&nbsp;&nbsp;&nbsp;
a character varying(255),</p>

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=
&nbsp;&nbsp;&nbsp;
b character varying(255),</p>

<p>CONSTRAINT my_table_id_pk PRIMARY KEY (my_table_id)</p>

<p>);</p>

<p>&nbsp;</p>

<p>CREATE TABLE my_audit_table</p>

<p>(</p>

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=
&nbsp;&nbsp;&nbsp;
audit_id serial,</p>

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=
&nbsp;&nbsp;&nbsp;
my_table_id int,</p>

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=
&nbsp;&nbsp;&nbsp;
c character varying(255),</p>

<p>CONSTRAINT audit_id_pk PRIMARY KEY (audit_id)</p>

<p>);</p>

<p>&nbsp;</p>

<p>--Create View</p>

<p>CREATE OR REPLACE VIEW my_view AS</p>

<p>SELECT
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;
</p>

<p>t.my_table_id,</p>

<p>t.a,</p>

<p>t.b,</p>

<p>au.audit_id,</p>

<p>au.c</p>

<p>FROM</p>

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=
&nbsp;&nbsp;&nbsp;
my_table t, my_audit_table au</p>

<p>WHERE</p>

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=
&nbsp;&nbsp;&nbsp;
t.my_table_id =3D au.my_table_id;</p>

<p>&nbsp;</p>

<p>--Create Rules</p>

<p>CREATE OR REPLACE RULE insert_to_my_view AS </p>

<p>ON INSERT TO my_view </p>

<p>DO INSTEAD(&nbsp; </p>

<p>INSERT INTO my_table (a,b)</p>

<p>VALUES(new.a, new.b);&nbsp; </p>

<p>INSERT INTO my_audit_table(my_table_id, c)&nbsp; </p>

<p>VALUES&nbsp; </p>

<p>(currval(&#39;my_table_my_table_id_seq&#39;), new.c); </p>

<p>);</p>

<p>&nbsp;</p>

<p>CREATE OR REPLACE RULE update_my_view AS </p>

<p>ON UPDATE TO my_view DO INSTEAD&nbsp; </p>

<p>&nbsp;( UPDATE my_table SET&nbsp; </p>

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=
&nbsp;&nbsp;&nbsp;
a =3D new.a,&nbsp; </p>

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=
&nbsp;&nbsp;&nbsp;
b =3D new.b&nbsp;&nbsp; </p>

<p>WHERE&nbsp; </p>

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=
&nbsp;&nbsp;&nbsp;
my_table_id =3D old.my_table_id;&nbsp; </p>

<p>INSERT INTO my_audit_table </p>

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=
&nbsp;&nbsp;&nbsp;
&nbsp;(my_table_id,&nbsp; </p>

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=
&nbsp;&nbsp;&nbsp;
&nbsp;c)&nbsp; </p>

<p>VALUES&nbsp; </p>

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=
&nbsp;&nbsp;&nbsp;
&nbsp;(new.my_table_id,&nbsp; </p>

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=
&nbsp;&nbsp;&nbsp;
&nbsp;new.c); </p>

<p>);</p>

<p>&nbsp;</p>

<p>--The insert statement below inserts one row into my_table,
and one row into my_audit_table</p>

<p>--(This works the way I would like)</p>

<p>insert into my_view(a,b,c) values(&#39;a contents&#39;,&#39;b
contents&#=
39;,
&#39;c contents&#39;);</p>

<p>&nbsp;</p>

<p>--The update statement below doesn't work the way I
want.</p>

<p>--What I would like this to do is to update one row in
my_table, and insert</p>

<p>--one row into my_audit table.&nbsp; It does the update
fine, but the insert to my_audit_table</p>

<p>--doesn&#39;t work as I had anticipated.&nbsp; </p>

<p>update my_view set a =3D &#39;new a contents&#39;, b =3D &#39;new b
contents&#39;, c&nbsp; =3D &#39;new c contents&#39; where my_table_id =3D
1=
;</p>

<p>&nbsp;</p>

<p>&nbsp;</p>

<p>&nbsp;</p>

<p>&nbsp;</p>

<p>If I execute the above update statement multiple times,
multiple rows will be </p>

<p>inserted with each call after the first call.</p>

<p>&nbsp;</p>

<p>Specifically,</p>

<p style=3D"text-indent: -0.25in;"><span style=3D"font-family:
Symbol;"><sp=
an>=B7<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span></span>after the first call, 1 row is inserted</p>

<p style=3D"text-indent: -0.25in;"><span style=3D"font-family:
Symbol;"><sp=
an>=B7<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span></span>after the second call, 2 rows are inserted</p>

<p style=3D"text-indent: -0.25in;"><span style=3D"font-family:
Symbol;"><sp=
an>=B7<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span></span>after the third call, 4 rows are inserted</p>

<p style=3D"text-indent: -0.25in;"><span style=3D"font-family:
Symbol;"><sp=
an>=B7<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span></span>after the fourth call, 8 rows are inserted...
and so on</p>

<p>&nbsp;</p>

<p>The problem is due to the INSERT in the update_my_view rule:</p>

<p>&nbsp;</p>

<p>INSERT INTO my_audit_table </p>

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
(my_table_id,&nbsp; </p>

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
c)&nbsp; </p>

<p>VALUES&nbsp; </p>

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
(new.my_table_id,&nbsp; </p>

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
new.c);</p>

<p>&nbsp;</p>

<p>Apparently, &quot;new.my_table_id&quot; in this case
references more than one row, if more than one row with </p>

<p>the given id already exists in my_audit_table.</p>

<p>&nbsp;</p>

<p>How do I accomplish what I want to accomplish here?&nbsp;
I&#39;d prefer not to use a sp.</p>

<p>&nbsp;</p>

<p>Thanks,</p>

<p>Chad</p>

<p>&nbsp;</p>

</div>

</div>


</blockquote></div><br>

------=_Part_22136_17421385.1208219635842--
 




 2 Posts in Topic:
rule for update view that updates/inserts into 2 tables
cshowalter@[EMAIL PROTECT  2008-04-14 12:47:30 
Re: rule for update view that updates/inserts into 2 tables
tharakan@[EMAIL PROTECTED  2008-04-15 06:03:55 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Thu Aug 21 23:15:56 CDT 2008.