CREATE TRIGGER giveaccess AFTER INSERT ON `befree_user`
FOR EACH ROW BEGIN
INSERT INTO user_access(user_id) VALUES (NEW.user_id);
END;
you need to define another delimiter than ;
delimiter |
CREATE TRIGGER giveaccess AFTER INSERT ON `befree_user`
FOR EACH ROW BEGIN
INSERT INTO user_access(user_id) VALUES (NEW.user_id);
END;
|
delimiter ;
If not the DB thinks that after the first ; the statement is done and will throw an error.
Related
I'm trying to create this trigger but i'm getting
[Err] 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 '; CREATE TRIGGER ca_passwd_trigger BEFORE UPDATE ON users
FOR EACH ROW BEGIN ' at line 1
delimiter $$
DROP TRIGGER IF EXISTS ca_passwd_trigger ;
$$
CREATE TRIGGER ca_passwd_trigger BEFORE UPDATE ON users
FOR EACH ROW
BEGIN
IF ((NEW.passwd <=> OLD.passwd) = 0) THEN
SET NEW.passwd_modified_at = NOW();
END IF;
END;$$
delimiter ;
In your query you added the query terminator ; with the delimiter $$ in two places. The below query is having proper query terminators and delimiters.
DELIMITER $$
DROP TRIGGER IF EXISTS ca_passwd_trigger; -- removed the delimiter $$ after the terminator ;
CREATE TRIGGER ca_passwd_trigger BEFORE UPDATE ON users
FOR EACH ROW
BEGIN
IF ((NEW.passwd <=> OLD.passwd) = 0) THEN
SET NEW.passwd_modified_at = NOW();
END IF;
END$$ -- removed the terminator ; before the delimiter $$
DELIMITER ;
I'm looking for the solution to a problem. I have a form that sends two fields (ID, info_data) and I'm trying to add a third field (secuencia) by a trigger but I can not do it.
table, result and expected results
Trigger:
DELIMITER $$
CREATE TRIGGER regi_tg
BEFORE INSERT ON registros
FOR EACH ROW
BEGIN
SET NEW.secuencia = new.id;
END$$
DELIMITER;
PD: I work info_data copy data in secuencia, but not copy the ID achievement in secuencia
Test CODE:
DELIMITER $$
CREATE TRIGGER regi_tg AFTER INSERT ON registros FOR EACH ROW
BEGIN
SET OLD.secuencia = NEW.id;
END;
CREATE TRIGGER regi_tg AFTER UPDATE ON registros FOR EACH ROW
BEGIN
SET OLD.secuencia = NEW.id;
END;
END$$
DELIMITER ;
I use this trigger and it worked, I did recently thought for a while and seeing many post but i dont know if a correct form.enter image description here
Trigger
DELIMITER $$
CREATE TRIGGER regi_tg
BEFORE INSERT ON registros
FOR EACH ROW
BEGIN
SET new.secuencia = md5((SELECT ID FROM registros ORDER BY id DESC LIMIT 1)+1);
END$$
DELIMITER ;
I am trying to apply a trigger to an existing table and column through PHP.
When executing the PHP code, no error is thrown, but the trigger is not created.
My PHP code is:
//file: migrations.php
// created_at column already defined in a previous migration
//...
$migrations[3]['message'] = "added created_at, updated_at columns on rate table";
$migrations[3]['created'] = "2015-09-05 08:28:00";
$migrations[3]['sql'] = "
DELIMITER |
CREATE TRIGGER rate_created_at BEFORE INSERT ON rate
FOR EACH ROW
BEGIN
SET NEW.created_at = CURRENT_TIMESTAMP;
END |
DELIMITER ;
";
// apply migration
foreach ($migrations as $key => $value) {
// run the migration SQL for the current iteration
$sth = $dbh->prepare( $value['sql'] );
$sth->execute();
}
heres what does work
Copying and pasting the trigger creating SQL into Phpmyadmin
getting rid of the DELIMITER and BEGIN END statements
e.g. if I reduce the SQL to
$migrations[3]['sql'] = "
CREATE TRIGGER rate_created_at BEFORE INSERT ON rate
FOR EACH ROW
SET NEW.created_at = CURRENT_TIMESTAMP;
";
It does run from PHP, problem is I dont believe I can run multiline statements without DELIMITER and BEGIN END.
I have tried
escaping the delimiter character
using different delimiter characters
What can I do to get the PHP to run the SQL with the DELIMITER and BEGIN END statements?
yes, you will have to get rid of those DELIMITER, just have the below code
$migrations[3]['sql'] = "
CREATE TRIGGER rate_created_at BEFORE INSERT ON rate
FOR EACH ROW
BEGIN
SET NEW.created_at = CURRENT_TIMESTAMP;
END;";
See this another post saying same thing PHP: multiple SQL queries in one mysql_query statement
Dummy question: Have the next code in MySQL, but when run it in HeidiSQL, show a sintaxis error near '// CREATE TRIGGER'. I'm not sure if that happen because the trigger doesn't exists. This is the code:
DROP TRIGGER IF EXISTS oportunidades_movimiento_entregado //
CREATE TRIGGER oportunidades_movimiento_entregado;
AFTER INSERT ON historial_entregado
FOR EACH ROW BEGIN
UPDATE oportunidades
SET oportunidades.fechaModificado = NEW.fecha_creacion
WHERE NEW.oportunidad_id = oportunidades.id;
END//
UPDATE
DELIMITER $$
DROP TRIGGER IF EXISTS oportunidades_movimiento_entregado $$
CREATE TRIGGER oportunidades_movimiento_entregado
AFTER INSERT ON historial_entregado
FOR EACH ROW BEGIN
UPDATE oportunidades
SET oportunidades.fechaModificado = NEW.fecha_creacion
WHERE NEW.oportunidad_id = oportunidades.id;
END; $$
DELIMITER //
This works, but now my doubt is... All the code (the DROP and the CREATE) will run everytime, or just the CREATE? I need this because I'm working with 2 tables, in one of them I insert/update the data, after with a trigger insert some of the columns in another table to keep a historial, but when I make a INSERT or UPDATE query in the first table, MySQL display the error #1442
Can't update table oportunidades in stored/function trigger because it's already used by statement which invoked this stored function/trigger.
To get it working, set the delimiter first and reset it after the CREATE TRIGGER statement.
Remove the semicolon at the end of the CREATE TRIGGER line.
This should work:
DELIMITER //
DROP TRIGGER IF EXISTS oportunidades_movimiento_entregado //
CREATE TRIGGER oportunidades_movimiento_entregado -- no trailing semicolon here
AFTER INSERT ON historial_entregado
FOR EACH ROW BEGIN
UPDATE oportunidades
SET oportunidades.fechaModificado = NEW.fecha_creacion
WHERE NEW.oportunidad_id = oportunidades.id;
END//
DELIMITER ;
whats wrong with my syntax?
CREATE
TRIGGER db_dhruniversity.trigger1
AFTER INSERT
ON jos_dhruprofile
FOR EACH ROW
BEGIN
UPDATE jos_users
SET jos_users.department = jos_dhruprofile.department
WHERE jos_users.id = jos_dhruprofile.uid
END
The syntax should be as follows:
DELIMITER $$ /* if you're not using an editor, you must change the delimiter*/
CREATE
TRIGGER ai_jos_dhruprofile_each
AFTER INSERT
ON jos_dhruprofile
FOR EACH ROW
BEGIN
UPDATE jos_users
SET jos_users.department = NEW.department
WHERE jos_users.id = NEW.uid; /*<<<--- ; after every stament */
END $$ /* changed delimiter after the end */
DELIMITER ; /*make sure you set the delimiter back to the default*/
Note on the naming scheme for triggers
I'd recommend naming your trigger ai (meaning after insert) so you know when it fires on which table, rather than a meaningless name like: db_dhruniversity.trigger1.
I always use [a/b]+[d/i/u]_tablename_each as the triggername, that way I always know when the triggers fires (before/after) for which event (insert/delete/update) and on which table.
It's also good practise to document that the trigger fires on each row, hence the each on the end of the trigger name.
Note that MySQL does not support triggers that fire once per statement yet (But that might change in future).
There are no delimiters in it:
DELIMITER ||
CREATE
TRIGGER db_dhruniversity.trigger1
AFTER INSERT
ON jos_dhruprofile
FOR EACH ROW
BEGIN
UPDATE jos_users
SET jos_users.department = NEW.department
WHERE jos_users.id = NEW.uid;
END ||
DELIMITER;