Skip to content

Instantly share code, notes, and snippets.

@nezarfadle
Last active September 20, 2017 09:03
Show Gist options
  • Save nezarfadle/0a810f5a0e2ebb1e42465324dc08110d to your computer and use it in GitHub Desktop.
Save nezarfadle/0a810f5a0e2ebb1e42465324dc08110d to your computer and use it in GitHub Desktop.
DELIMITER $$
CREATE TRIGGER Prevent_User_From_Insert_Empty_Strings BEFORE INSERT ON users
FOR EACH ROW
BEGIN
   IF NEW.name = '' THEN
       SIGNAL SQLSTATE '45000';
   END IF;
END;
@nezarfadle
Copy link
Author

nezarfadle commented Sep 20, 2017

This example shows how to use MySQL trigger to prevent the user from adding an empty string to the name field in the users table.

This could be handy in case if we want to add another validation layer to our application ( keep in mind that this might decrease the performance a bit ).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment