Skip to content

Instantly share code, notes, and snippets.

@rafaeltedesco
Created November 22, 2022 12:25
Show Gist options
  • Select an option

  • Save rafaeltedesco/9f2b00dfb5b64ea7f13e91aba6fe838a to your computer and use it in GitHub Desktop.

Select an option

Save rafaeltedesco/9f2b00dfb5b64ea7f13e91aba6fe838a to your computer and use it in GitHub Desktop.
Guarantee that user cannot insert a manual id on a auto_increment field
DROP TRIGGER IF EXISTS sakila.bf_insert_actor;
DELIMITER $$
CREATE TRIGGER bf_insert_actor
BEFORE INSERT ON sakila.actor
FOR EACH ROW
BEGIN
IF NEW.actor_id IS NOT NULL THEN
SET NEW.actor_id = null;
END IF;
END $$
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment