開発メモ

開発用のメモです。

MySQL Trigger でパスワードの暗号化

DELIMITER $$

-- insert
CREATE TRIGGER `users_before_insert` BEFORE INSERT ON `users` 
FOR EACH ROW
    BEGIN
        SET NEW.`userpass` = password(NEW.`userpass`);
    END
$$


-- update
CREATE TRIGGER `users_before_update` BEFORE UPDATE ON `users` 
FOR EACH ROW
    BEGIN
        IF NEW.`userpass` <> OLD.`userpass` THEN
            SET NEW.`userpass` = password(NEW.`userpass`);
        END IF;
    END
$$
DELIMITER ;
Twitter: @asahina_alice