USE parts;

DROP TRIGGER IF EXISTS parts.before_insert_DOG_User_Change_Set;

DELIMITER //
CREATE TRIGGER parts.before_insert_DOG_User_Change_Set
BEFORE INSERT ON parts.DOG_User_Change_Set
FOR EACH ROW
BEGIN
	IF NEW.updated_last_on <=> NULL THEN
		SET NEW.updated_last_on = NOW();
	END IF;
END //
DELIMITER ;


DELIMITER //
CREATE TRIGGER parts.before_update_DOG_User_Change_Set
BEFORE UPDATE ON parts.DOG_User_Change_Set
FOR EACH ROW
BEGIN
	IF NOT EXISTS(SELECT * FROM parts.DOG_User WHERE id_user = NEW.id_user_updated_last_by) THEN
		SIGNAL SQLSTATE '45000'
        SET MESSAGE_TEXT = 'New Updated-Last-By User ID must be provided.';
	END IF;
END //
DELIMITER ;
