Tengo un problema con el uso de Triggers.
Hace un tiempo hize un trigger que me evitó duplicaciones antes de realizar un insert en una tabla. Ahora debo pasar ese proyecto de mi ordenador a un servidor y resulta que los triggers que tenía funcionando han dejado de hacerlo.
Mi versión es la 5.1.41 y la del servidor es la 5.0.32. He estado buscando información sobre el cambio de sintaxis que debo aplicar, pero no hay manera de que me funcione.
Mi tabla es la siguiente:
Código PHP:
CREATE TABLE IF NOT EXISTS `user` (
`id_user` int(11) NOT NULL AUTO_INCREMENT,
`login` varchar(255) COLLATE utf8_bin DEFAULT NULL,
`password` varchar(255) COLLATE utf8_bin DEFAULT NULL,
`state` tinyint(1) DEFAULT NULL,
`name` varchar(255) COLLATE utf8_bin DEFAULT NULL,
`description` varchar(255) COLLATE utf8_bin DEFAULT NULL,
`email` varchar(255) COLLATE utf8_bin DEFAULT NULL,
`data_insert` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id_user`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
Código PHP:
DELIMITER //
CREATE TRIGGER user_no_repeat BEFORE INSERT ON user
FOR EACH ROW begin
if (select count(*) from user where login=new.login and password=new.password) > 0 then
call fail('Repeated insert into "user"');
end if;
end
//
DELIMITER ;
Código PHP:
CREATE PROCEDURE Fail(_Message VARCHAR(255))
INSERT INTO error (Message) VALUES (_Message);
Error
Parece haber un error en su consulta de SQL. La salida generada por el servidor de MySQL, de existir, aparece abajo, en cuyo caso puede ayudar a diagnosticar el problema.
ERROR: Signo de puntuación desconocido @ 11
STR: //
SQL: DELIMITER //
CREATE TRIGGER user_no_repeat BEFORE INSERT ON user
FOR EACH ROW begin if (select count(*) from user where login=new.login and password=new.password) > 0 then call fail('Repeated insert into "user"');
consulta SQL:
DELIMITER // CREATE TRIGGER user_no_repeat BEFORE INSERT ON user FOR EACH ROW begin if (select count(*) from user where login=new.login and password=new.password) > 0 then call fail('Repeated insert into "user"');
MySQL ha dicho:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DELIMITER //
CREATE TRIGGER user_no_repeat BEFORE INSERT ON user
FOR EACH ROW' at line 1
He probado a cambiar las // por &&, por %% y por ;; y nada, me da el mismo error.
Finalmente en algún sitio he visto que decía que funcionaba si quitabas los delimitadores, de hecho el procedimiento ‘Fail’ lo he creado quitando los delimiter. Así que sin ellos el error que obtengo es el siguiente:
Código PHP:
CREATE TRIGGER user_no_repeat BEFORE INSERT ON user
FOR EACH ROW begin
if (select count(*) from user where login=new.login and password=new.password) > 0 then
call fail('Repeated insert into "user"');
end if;
end
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 4
Si ejecuto solamente la linea “call fail('Repeated insert into "user"');” me funciona perfectamente, incluso he intentado cambiar la llamada por el insert a la tabla de error directamente:
Código PHP:
CREATE TRIGGER user_no_repeat BEFORE INSERT ON user
FOR EACH ROW begin
if (select count(*) from user where login=new.login and password=new.password) > 0 then
insert into error (id_error, message, data_insert) values (null, "Repeated insert into user", now());
end if;
end
MySQL ha dicho:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'insert into error (EID, message, data_insert) values (null, "Repeated insert int' at line 4
Y si ejecuto solo el insert, se realiza correctamente la inserción a la tabla de error.
La verdad es que he estado mirando y buscando alguna solución y no lo he encontrado… alguna idea?
Gracias!!!