I’m stuck on a the type of trigger needed to for this constraint.
I will have a price and a commission. The price determines the commission amount, < 100 – 4%, < 200 – 5% etc.
My idea.
the database contains a separate table that will hold 4 price values , 101, 201, 401, 601, with their own matching comission %, this will be called PC. When I create a property listing I want to calculate the commission they earn depending on the price entered.
on insert, I need to check the new.price and compare it to the prices in PC. Once new.price is less than the price tuple, I set the price to that commission value
create or replace TRIGGER findCommission BEFORE INSERT OR UPDATE ON HASLISTING
FOR each ROW
BEGIN
IF (:NEW.ASKING_PRICE < 100001) THEN
:NEW.COMMISSION = 6.0;
END IF;
IF (:NEW.ASKING_PRICE < 250001) THEN
:NEW.COMMISSION = 5.5;
END IF;
IF (:NEW.ASKING_PRICE < 1000001) THEN
:NEW.COMMISSION = 5.0;
END IF;
IF (:NEW.ASKING_PRICE > 1000000) THEN
:NEW.COMMISSION = 4.0;
END IF;
END;
I would suggest using a function for calculations like this and then call it in the trigger. That way the commission logic stays seperate and can be changed.
Also, your commission table description might help.
–this is also assuming that the ranges do not overlap and if they do, they get the maximum commision 😉
Now to get the commission values…
— and your trigger definition could be as simple as this. Looking at your question, it seems you have the percentage in the table and you need the commission value in your final table.
I haven’t tested the trigger code for syntax errors, please test on your end.