To encrypt a value in MD5 , just use the function of the same name, for example:
SELECT MD5('Psr');
-> '0dd833a62f068acadd6604eec8daf236'
With this you can use trigger_time BEFORE
with trigger_event INSERT
. For example:
/* Cria a tabela com o campos necessários */
CREATE TABLE 'user' (
'id' INT(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
'email' VARCHAR(40) NOT NULL,
'email_hash' VARCHAR(32) NULL
);
delimiter //
/* Cria o trigger para setar, antes de inserir no DB, o valor do campo 'email_hash' */
CREATE TRIGGER md5Email BEFORE INSERT ON 'user'
FOR EACH ROW
BEGIN
SET NEW.'email_hash' = MD5( NEW.'email' );
END;//
delimiter ;
/* Insere o valor */
INSERT INTO 'user' ('email') VALUES ("[email protected]");