MySQL Forums
Forum List  »  Triggers

Enforce business rule with before insert trigger?
Posted by: MySQL User
Date: April 17, 2005 01:10PM

Hi,

I'm experimenting with MySQL 5's trigger support, and have happened across a nut I'm unable to crack. Suppose I want to enforce some biz rule in a trigger any time an INSERT query is executed on the table "product". My desired action for the query not following this rule is failed execution.

So for example suppose I want the trigger to make sure any inserted product price is greater than $10. Therefore the trigger would check whether this is the case, and if not, produce some sort of error messages and deny the target query from occurring. That trigger might look like this:

create trigger bi_product before insert on product
FOR EACH ROW
BEGIN
SET @price = "";
IF NEW.price < 10 THEN
/* DO SOMETHING TO CAUSE OPERATION TO FAIL */
END IF;
END//

Can anybody tell me what I'm supposed to enter in the /* */ line in order to make this happen? Perhaps speaking in more general terms, how can I use MySQL's trigger feature to enforce business rules in this manner, also making sure I'm passing some sort of coherent message back to the user regarding the problem?

Thank you!
G

Options: ReplyQuote


Subject
Views
Written By
Posted
Enforce business rule with before insert trigger?
8302
April 17, 2005 01:10PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.