If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > PostgreSQL > Auto update a field

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-06-11, 10:45
kbum kbum is offline
Registered User
 
Join Date: Mar 2011
Posts: 12
Auto update a field

Hello. I have a table with a follow fields:
date_next date
status character

I need update a the field status if the date_next < current_date.
This update need to be automatic. The check would be maked daily.

In DB2 i maked a Event Monitor. But how i can make this in PostgreSQL?

I'm using the PostgreSQL 8.2.
Reply With Quote
  #2 (permalink)  
Old 05-06-11, 10:49
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,408
Don't update anything.

Create a view that returns the status based on the current_date
Reply With Quote
  #3 (permalink)  
Old 05-06-11, 10:57
kbum kbum is offline
Registered User
 
Join Date: Mar 2011
Posts: 12
But the idea is, if the date_next is less than the current date, the status field must be updated.

Currently the update is done manually. I would like to automate this update.
Reply With Quote
  #4 (permalink)  
Old 05-06-11, 11:11
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,408
No, you don't need the update. Just create a view that calculates the status "on-the-fly" and retrieve the status information through the view.

Code:
CREATE VIEW status_info
AS 
SELECT some_column, 
       date_next
       case 
          when date_next < current_date then 'STATUS_ONE'
          else 'STATUS_OTHER'
       end as status
FROM your_table
One rule in relational databases: do not store things you can determine from existing data.

If your application cannot be changed to use the view, rename the table, create the view with the name of the table and create RULEs to make the view updateable.

Then you never need to worry about the status column, it will always be correct without any background jobs.
Reply With Quote
Reply

Tags
auto, automatic, date, postgresql, update

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On