views:

2419

answers:

4

If I have a trigger BEFORE UPDATE on a table. How can I throw an error that prevents the update on that table?

+3  A: 

Here is one hack that might work. Isn't clean, but it looks like it might work:

http://www.brokenbuild.com/blog/2006/08/15/mysql-triggers-how-do-you-abort-an-insert-update-or-delete-with-a-trigger/

Essentially you just try to update a column that doesn't exist.

Justin
+1  A: 

I've been using the approach that Justin linked for a while now. Works great, but definitely hackish.

As that link states, eventually MySQL plans to have real exception raising via the SQL SIGNAL and RESIGNAL statements.


P.S. Justin: for convenience, you may want to make that URL an actual hyperlink; or just wait for this: http://stackoverflow.uservoice.com/pages/general/suggestions/14397

Liron Yahdav
A: 

Ha. That was my suggestion!

Justin
A: 

What does your trigger do? Is it something that could just as easily be wrapped in the update's transaction?

That could save you hassle down the line.

Rob Allen