How to reject delete statement in before delete tr

2019-03-05 03:28发布

问题:

How to reject delete statement in before delete trigger (mysql) ?

Sample use case : I have a record with specified id that I want to will not be deleted.

回答1:

You could create a table that uses a foreign key constrait referencing your ID column (provided that is the primary key of your table). The insert a row into that "child" table that references ID = 0 and remove all update/delete privileges for your user from that table.

Now if someone tries to delete the row with ID = 0 in the "parent" table that will fail due to an existing child row in the other table.



回答2:

Look here, same thing but with an insert-trigger: How to abort INSERT operation in MySql trigger?