There are two tables, Part and PartSupply. Part has columns partkey, name, and p
ID: 3732189 • Letter: T
Question
There are two tables, Part and PartSupply. Part has columns partkey, name, and price. PartSupply has columns partkey, supplykey, quantity, and supplycost. I need to implement a constraint or trigger that if a part's price changes then the supplycost for any row in partsupply should change by the same amount. So if a part's price increased by 10 percent then all rows in supplycost that correspond to that part should have a 10 percent increase in the supplycost. This needs to be in postgresql. I was wondering if someone could explain to me how to go about doing this.
Explanation / Answer
Solution:
Query:
CREATE TRIGGER changePrice BEFORE INSERT ON Part FOR EACH ROW IF NEW.price!= OLD.price THEN SET NEW.supplyCost= price*OLD.supplyCost; END IF;
The above query will fulfill the requirement of the trigger as stated in the problem statement. with the help of IF we will be able to check if there is a change in price column in the Part table.
I hope this helps if you find any problem. Please comment below. Don't forget to give a thumbs up if you liked it. :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.