Solved

Postgres Update Triggers (an update in an update)

Posted on 2011-03-09
2
1,018 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
Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

771 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

11 Experts available now in Live!

Get 1:1 Help Now