PostgreSQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesPostgreSQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old April 11th, 2003, 04:00 PM
bcyde's Avatar
bcyde bcyde is offline
Me likey breadsticks...
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jan 2003
Location: Los Angeles
Posts: 1,189 bcyde User rank is Sergeant (500 - 2000 Reputation Level)bcyde User rank is Sergeant (500 - 2000 Reputation Level)bcyde User rank is Sergeant (500 - 2000 Reputation Level)bcyde User rank is Sergeant (500 - 2000 Reputation Level)bcyde User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 3 Days 1 h 12 m 58 sec
Reputation Power: 17
Send a message via AIM to bcyde Send a message via Yahoo to bcyde
Rules, Views and Updating multiple tables

Ok, I have a view that joins two tables and I want to be able to insert on that view that will insert into both tables.

Here's my current rule:
PHP Code:
 CREATE RULE userinfo_ins AS ON INSERT TO userinfo
    
DO INSTEAD
    INSERT INTO users VALUES 
(NEW.emp_id, NEW.password, NEW.fname, NEW.mi, NEW.lname, NEW.emp_ssa, NEW.emp_status


This first rule seems to work well, however I'd also like to bundle the following query with it:
PHP Code:
 INSERT INTO signon VALUES (NEW.emp_id, NEW.signon_id


I just don't know what the syntax is. In the manual it says the syntax is:

CREATE RULE rule_name AS ON event
TO object [WHERE rule_qualification]
DO [INSTEAD] [action | (actions) | NOTHING];

but the sample only seems to show 1 action being taken. I'm sure I'm just missing something simple. Also, is there a way I can use some sort of CASE where if NEW.signon_id is null I don't do the insert to the second table? If I'm missing any pertinent info, just let me know and I'll post it.
Here's my setup:
RH 7.2
Postgres 7.1.3 (yes I'm in dire need of an upgrade)
phpPGAdmin 2.4a (btw, is there functionality in phpPGAdmin to view rules)

UPDATE
Apparently the problem I was having was with phpPGAdmin when trying to create the rule it halts on the first semi-colon when doing mult-action rules. This rule works when I enter it in psql
PHP Code:
 CREATE RULE userinfo_ins AS ON INSERT TO userinfo
    
DO INSTEAD    
    
(INSERT INTO users VALUES (NEW.emp_id, NEW.password, NEW.fname, NEW.mi, NEW.lname, NEW.emp_ssa, NEW.emp_status);       
    
INSERT INTO signon VALUES (NEW.emp_id, NEW.signon_id); ); 


Any ideas regarding my case question or phppgadmin question about viewing rules?
Thanks,
-b
__________________
PostgreSQL, it's what's for dinner...

Last edited by bcyde : April 11th, 2003 at 04:48 PM.

Reply With Quote
  #2  
Old April 14th, 2003, 01:03 PM
rycamor rycamor is offline
Gödelian monster
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jul 1999
Location: Central Florida, USA
Posts: 2,306 rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Days 6 h 42 m 51 sec
Reputation Power: 60
Hey bcyde,

Congratulations . You figured out how to add as many SQL statements as you want to your RULE. Yes, i agree that its frustrating that the documentation doesn't show an example of this. I suppose I should post an example in the contrib notes underneath. (makes mental note...)

Anyway, i don't know if you can have conditional execution of multiple queries, but have you even tried adding the CASE logic?

What about
Code:
CREATE RULE userinfo_ins AS ON INSERT TO userinfo

    DO INSTEAD    

    (INSERT INTO users VALUES (NEW.emp_id, NEW.password, NEW.fname, NEW.mi, NEW.lname, NEW.emp_ssa, NEW.emp_status);       

    (CASE WHEN condition='met' THEN

        INSERT INTO signon VALUES (NEW.emp_id, NEW.signon_id);

    END; )

 );


(not sure of the correct syntax without testing, but see if something like this will work.)

If this doesn't work, remember, there is always another way to do things. The most obvious solution is to just write a stored procedure, and create a trigger, thus forgetting about the rule completely. Procedures can be written in a completely procedural language (heh), thus allowing you more freedom in some areas. But, with a little more exploration of SQL and the RULE system, I have a feeling it could be handled completely in a rule.
__________________
The real n-tier system:

FreeBSD -> PostgreSQL -> [any_language] -> Apache -> Mozilla/XUL

Amazon wishlist -- rycamor (at) gmail.com

Reply With Quote
  #3  
Old April 14th, 2003, 01:43 PM
MattR MattR is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2001
Location: High above the mucky-muck (Columbus, OH)
Posts: 266 MattR User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 12
Send a message via ICQ to MattR
Quote:
Originally posted by rycamor
But, with a little more exploration of SQL and the RULE system, I have a feeling it could be handled completely in a rule.


It *should* be handled invisibly by the DBMS. That is to say the view should be updateable/insertable. Can PostgreSQL do that? If not, do they have any plans to do so?

Reply With Quote
  #4  
Old April 14th, 2003, 03:58 PM
bcyde's Avatar
bcyde bcyde is offline
Me likey breadsticks...
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jan 2003
Location: Los Angeles
Posts: 1,189 bcyde User rank is Sergeant (500 - 2000 Reputation Level)bcyde User rank is Sergeant (500 - 2000 Reputation Level)bcyde User rank is Sergeant (500 - 2000 Reputation Level)bcyde User rank is Sergeant (500 - 2000 Reputation Level)bcyde User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 3 Days 1 h 12 m 58 sec
Reputation Power: 17
Send a message via AIM to bcyde Send a message via Yahoo to bcyde
Thanks rycamor,
I've tried different variants of the CASE statment but the parser always seems to not like it. I'll give it a few more shots and if I can't get it I'll probably just fire the question off to the pg mailing list.

Regarding using a trigger instead, I was under the impression (probably false) that in order to insert into a view composed of multiple tables a rule was required. I'm not exactly sure how postgres sees the relationship between views/triggers (I know that in the docs it says that views are just implemented as rules), but will postgres be able to "see" the trigger and allow insertion into the view?

Thanks for your help... again

-b

Reply With Quote
  #5  
Old April 14th, 2003, 05:17 PM
rycamor rycamor is offline
Gödelian monster
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jul 1999
Location: Central Florida, USA
Posts: 2,306 rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Days 6 h 42 m 51 sec
Reputation Power: 60
Quote:
Originally posted by MattR
It *should* be handled invisibly by the DBMS. That is to say the view should be updateable/insertable. Can PostgreSQL do that? If not, do they have any plans to do so?


Please explain to me how a standard view-updating system, such as in Sybase, Oracle, etc... would handle extra logic such as bcyde is trying to handle. In other words, this is not just a standard view-updating operation, AFAIK.

But anyway, yes, PostgreSQL does not automatically provide view updating, but the RULE system is an extremely easy way to handle view updating, even though you do it "by hand". The benefit is that you get to make it do whatever tricks you want. This can be an advantage in certain cases, because view updating can be ambiguous at times, especially because the DBMS may not be able to know exactly what you want.

The next version of PostgreSQL will be automatically creating update rules for views, but then the DBA will be able to modify them. Sounds like the best of both worlds to me...

Reply With Quote
  #6  
Old April 14th, 2003, 08:17 PM
MattR MattR is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2001
Location: High above the mucky-muck (Columbus, OH)
Posts: 266 MattR User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 12
Send a message via ICQ to MattR
Quote:
Originally posted by rycamor
Please explain to me how a standard view-updating system, such as in Sybase, Oracle, etc... would handle extra logic such as bcyde is trying to handle. In other words, this is not just a standard view-updating operation, AFAIK.


Unless you use a trigger, they can't either (although Oracle's materialized views may be able to filter down).

View updates are view updates.

Quote:
The next version of PostgreSQL will be automatically creating update rules for views, but then the DBA will be able to modify them. Sounds like the best of both worlds to me... [/B]


Sounds about right. Although again view updating is not an impossible task, just a difficult one, and if any DBMS wanted to spend a lot of time and money they could do it correctly. Triggers are probably the 'next best thing', it's too bad though it would make everyone's lives easier.

Reply With Quote
  #7  
Old April 15th, 2003, 09:04 AM
bcyde's Avatar
bcyde bcyde is offline
Me likey breadsticks...
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jan 2003
Location: Los Angeles
Posts: 1,189 bcyde User rank is Sergeant (500 - 2000 Reputation Level)bcyde User rank is Sergeant (500 - 2000 Reputation Level)bcyde User rank is Sergeant (500 - 2000 Reputation Level)bcyde User rank is Sergeant (500 - 2000 Reputation Level)bcyde User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 3 Days 1 h 12 m 58 sec
Reputation Power: 17
Send a message via AIM to bcyde Send a message via Yahoo to bcyde
For future reference for anyone, here's what was recommended to by Tom Lane from the postgres mailing list:
Quote:
CASE is not the solution. Do something like
INSERT INTO target SELECT this, that, theother WHERE condition;


And here's what works for me:
PHP Code:
 CREATE RULE userinfo_ins AS ON INSERT TO userinfo
    
DO INSTEAD    
    
(INSERT INTO users VALUES (NEW.emp_id, NEW.password, NEW.fname, NEW.mi, NEW.lname, NEW.emp_ssa, NEW.emp_status);       
    
INSERT INTO signon (SELECT NEW.emp_id, NEW.signon_id WHERE NEW.signon_id IS NOT NULL); ); 


Thanks rycamor and MattR for your input,
-b

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesPostgreSQL Help > Rules, Views and Updating multiple tables

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap