0

I have a table called 'tbl_abcd' with columns 'id, user_id', 'carbon_value', 'total_carbon' and 'area'in which id is the primary key.I want to write a trigger function when user update any change in 'carbon_value' column and that trigger will invoke a function that should update 'total_carbon' column value that will be calculated by the formula 'area*carbon_value' for which total_carbon will be changed.

for example carbon_value = 3, area = 5hectare, total_carbon = 3*5 = 15. when carbon value will changed automatically total carbon will be changed.

Any help will be highly appreciated. Rutuparna Panda

1 Answer 1

3
CREATE FUNCTION total_carbon() RETURNS trigger AS $$
  BEGIN
    NEW.total_carbon := NEW.carbon_value * NEW.area;

    RETURN NEW;
  END
$$ LANGUAGE plpgsql;


CREATE TRIGGER
  total_carbon
BEFORE INSERT OR UPDATE ON
  tbl_abcd
FOR EACH ROW EXECUTE PROCEDURE
  total_carbon();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.