Sorry, I meant MySQL doesn't support rollback or any kind of transaction stuff *within a trigger*. Basically, I need the trigger to do something to cause the entire transaction to fail.
Main Topics
Browse All TopicsI am trying to use a trigger to implement row-level permissions using mysql. I have a table "t1" with a tinyint field called "is_editable". The trigger checks the user and the "is_editable" field to determine whether to allow the transaction to go through. "user1" is not allowed to edit rows where the "is_editable" field is set to 0.
Here is my almost-complete trigger:
delimiter //
CREATE TRIGGER check_row_priv BEFORE UPDATE ON t1
FOR EACH ROW
BEGIN
IF (USER()='user1@localhost')
THEN
WHAT DO I NEED TO DO TO CAUSE THE TRANSACTION TO FAIL?
END IF;
END; //
delimiter ;
The question, as you can see, is what do I need to do to cause this transaction to fail?
Apprently I can't use "ROLLBACK" or anything like that, because MySQL doesn't support it.
Or, if there's a completely better way to implement row-level permissions, I'm all ears.
I am using mysql 5.0.22
Thanks for any help!
Rusty
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Sounds like RETURN FALSE is what you want. Check here for the comment from Tim H.:
http://dev.mysql.com/doc/r
Yup, I saw Tom H.'s comment and stopped reading. The next three comments on the page explain that strategy does not work.
The only other method I've found is to create a deliberate error inside the trigger, like violating a primary key constraint or similar. This is messy, but it would work since the failure of a trigger stops the execution and rollsback any work-to-date (on transaction-based tables, anyways). You'll still have to deal with the failure of the trigger at some point...I'm guessing through the front-end app.
See bug 11661 for this issue.
Don't do this as a trigger - that's not really what they are there for (although, in theory, you could do something like execute a SQL statement that has a SQL error, which should cause th trigger to fail). Do this as a view, instead. Views can be updated, and you can create a view for each user:
CREATE VIEW v1 AS SELECT * FROM t1 WHERE isUpdatable != 0;
Then give the user access to perform operations on t1 (you might want to hide the isUpdatable column so they can't change it, but the worst they could do would be to make something they can see into something they can't see).
Here's an example of how you *can* use a trigger to prevent an update - by making it do something it should not do:
mysql> CREATE TABLE trig_test (id INT NOT NULL);
Query OK, 0 rows affected (0.09 sec)
mysql> CREATE TABLE const (id INT PRIMARY KEY);
Query OK, 0 rows affected (0.16 sec)
mysql> INSERT INTO const VALUES (1);
Query OK, 1 row affected (0.06 sec)
mysql> DELIMITER //
mysql> CREATE TRIGGER check_row_priv BEFORE UPDATE ON trig_test
-> FOR EACH ROW
-> BEGIN
-> INSERT INTO const VALUES (1);
-> END; //
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter ;
mysql>
mysql> INSERT INTO trig_test VALUES (1);
Query OK, 1 row affected (0.06 sec)
mysql> UPDATE trig_test SET id = 3;
ERROR 1062 (23000): Duplicate entry '1' for key 1
mysql> SELECT * FROM trig_test;
+----+
| id |
+----+
| 1 |
+----+
1 row in set (0.00 sec)
mysql>
You can modify the trigger to include your logic of checking the isUpdatable flag or whatever conditions you need to prevent updates.
Ultimately, you can't abort the transaction - you can only cause the statement being executed to fail. It's up to the application to rollback the transaction as a whole, retry the statement that failed, or ignore it.
All that being said, I'd recommend you use views instead of triggers - row-based permission is what they are there for.
They do the update on the views - which only allow access to certain rows. See the update at the end of this example:
mysql> CREATE TABLE t1 (id INT, owner INT);
Query OK, 0 rows affected (0.09 sec)
mysql> CREATE VIEW v1 AS SELECT * FROM t1 WHERE owner = 1;
Query OK, 0 rows affected (0.02 sec)
mysql> INSERT INTO v1 VALUES (1, 1);
Query OK, 1 row affected (0.14 sec)
mysql> INSERT INTO v1 VALUES (2, 2);
Query OK, 1 row affected (0.09 sec)
mysql> SELECT * FROM v1;
+------+-------+
| id | owner |
+------+-------+
| 1 | 1 |
+------+-------+
1 row in set (0.05 sec)
mysql> UPDATE v1 SET id = 3;
Query OK, 1 row affected (0.09 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> SELECT * FROM t1;
+------+-------+
| id | owner |
+------+-------+
| 3 | 1 |
| 2 | 2 |
+------+-------+
2 rows in set (0.00 sec)
mysql>
Business Accounts
Answer for Membership
by: routinetPosted on 2007-04-16 at 13:30:08ID: 18920481
>>> Apprently I can't use "ROLLBACK" or anything like that, because MySQL doesn't support it.
efman/5.0/ en/innodb- transactio ns- with-di fferent-ap is.html
MySQL does support transactions, but only for certain storage engines. Check here for some info:
http://dev.mysql.com/doc/r
You should be able to rollback the transaction with no difficulties.