Last active
February 28, 2018 17:42
-
-
Save danielpereirabp/2217d7f93dfab3fefd9262287a398e93 to your computer and use it in GitHub Desktop.
Create trigger to stop insert
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE TABLE orders ( | |
id INT(10) UNSIGNED AUTO_INCREMENT PRIMARY KEY, | |
total DECIMAL(8,2) NOT NULL, | |
delivery_date DATE NOT NULL, | |
status SMALLINT(6) NOT NULL DEFAULT '0', | |
client_id INT(10) UNSIGNED NOT NULL, | |
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, | |
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP | |
); | |
DELIMITER $ | |
CREATE TRIGGER trigger_check_create_order BEFORE INSERT ON orders | |
FOR EACH ROW | |
BEGIN | |
IF (SELECT COUNT(1) FROM orders WHERE orders.client_id = NEW.client_id AND DATE_FORMAT(orders.created_at, '%Y-%m-%d') = DATE_FORMAT(NEW.created_at, '%Y-%m-%d') AND orders.status != 4) THEN | |
SIGNAL SQLSTATE '45000' | |
SET MESSAGE_TEXT = 'Access Forbidden'; | |
END IF; | |
END; $ | |
DELIMITER ; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment