Skip to content
Advertisement

TRIGGER that checks if that delete removes all the table values

Basicly I just don’t wanna allow that DELETE FROM X delete ALL rows.

For example:

DELETE FROM X WHERE ID = 3 OR ID = 4; –> Allowed because don’t try to remove all table.

DELETE FROM X; –> Not allowed because it will be delete all table.

How I can do it with a trigger?

Advertisement

Answer

You may not actually need a trigger to prevent delete without a where clause.

You may be able to do what you want just by setting:

SET SQL_SAFE_UPDATES = 1;

The documentation for this is here.

Note: This of course applies to all tables. If you only want this restriction on one table, then a trigger might be appropriate.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement