Solved

Postgres Update Triggers (an update in an update)

Posted on 2011-03-09
2
1,034 Views
Last Modified: 2012-05-11
Postgres 9.0

I want to have a trigger for an update on TableA, in that trigger, part of the process is to update other rows in TableA.

What is the best way/method to do this without causing a loop.

Simple example, Table A has field "is_master" which I want only 1 record of x,y,z  to have is_master = 't'. When a new row is inserted and is x,y,z (and also newer version), I want for new row to have is_master = 't', and the old row is_master = null.

The table already has a unique constraint on (x,y,z,is_master).

Somehow, I need to set old is_master to null so I can insert new row with is_master = 't';
0
Comment
Question by:freshgrill
2 Comments
 
LVL 19

Expert Comment

by:elimesika
ID: 35089539
I would create a stored procedure for the update in do inside of it:

update all is_master to null
insert new row with is_master ='t'

surround this with a transaction + COMMIT / ROLLBACK and you done....
0
 
LVL 39

Accepted Solution

by:
lcohan earned 500 total points
ID: 35096893
I think the constraint won't let you put a AFTER (by default) trigger with that action but if you have a id column on that table you could use OLD.id in a BEFORE  trigger to identify existing x,y,z and set is_master = 0 for that table.id then set is_master = 1 WHERE  table.id = NEW.id
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Best database to use for Maps is PostgreSQL. This is an open source database. Comes as a package with most Linux OS. For more info visit the following site: http://www.postgresql.org/ (http://www.postgresql.org/) This requires some add-o…
Many developers have database experience, but are new to PostgreSQL. It has some truly inspiring capabilities. I have several years' experience with Microsoft's SQL Server. When I began working with MySQL, I wanted a quick-reference to MySQL (htt…
Steps to create a PostgreSQL RDS instance in the Amazon cloud. We will cover some of the default settings and show how to connect to the instance once it is up and running.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now