Link to home
Start Free TrialLog in
Avatar of aswam1975
aswam1975

asked on

how to write an update trigger

I would like to write an update trigger which does the following.
When a column called counter is updated in table1 , the trigger updates the counter value in table2.
eg:
table 1 has columns id, counter
table2 columns id, counter.

Now if the id in table 1 does not exist in table2, then when table1 's counter is updated that id and counter are inserted into table2.

I have never triggers before and would appreciate any help
Thanks in advance
ASKER CERTIFIED SOLUTION
Avatar of earth man2
earth man2
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of aswam1975
aswam1975

ASKER

Thank you very much for your help.
Can you let me know what the term "select 1" does?
It returns a constant that doesn't take up much space -- the purpose of that query is to just find any rows that meet the where criteria -- i.e., to see if there are any rows in table2 that have an "id" column whose value matches the new record's id column value. Depending on the server involved, it might handle this select by actually creating a table with the result set and then checking to see if that table has any rows. If that's the strategy that the server uses, then it makes sense to construct the smallest table possible. I'm not sure how modern implementations of PostgreSQL handle such a query -- it's possible that they just check for the existence of a row and don't actually build the temporary table containing the result set. However, such a trick is common and should result in the fastest query on pretty much any SQL engine.
thanks again!