Mysql trigger delimiter in version 5.5.5 - mysql

I've created a mysql trigger on before insert to behave just like an autoincrement field starting from 1110
DROP TRIGGER IF EXISTS set_nr_claim_AI;
DELIMITER $$
CREATE TRIGGER set_nr_claim_AI BEFORE INSERT ON users_claim
FOR EACH ROW
BEGIN
DECLARE max_nr INT;
SET max_nr = (SELECT MAX(nr_claim) FROM users_claim);
IF max_nr IS NULL OR max_nr = 0 THEN
SET NEW.nr_claim = 1100;
ELSE
SET NEW.nr_claim = max_nr +1;
END IF;
END; $$
DELIMITER ;
In mysql version 5.5.5 I have this error : #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'DELIMITER' at line 1
But all works fine in version 5.6 of mysql and I could not find the solution.
I need to resolve also on older version of mysql(my staging environment has 5.5.5)

I've managed to resolve the problem. All I needed is to add a new line after DELIMITER ;

Related

Syntax error during creation of trigger on mySQL 10.3.17-MariaDB-log version

I'm trying to add a trigger that should automatically update a datetime column only if is not present a SQL variable. The trigger code is the following
delimiter |
CREATE TRIGGER update_sync_date
BEFORE UPDATE ON posts
FOR EACH ROW
BEGIN
IF #disable_sync_date_update IS NULL OR #disable_sync_date_update <> 1 THEN
SET new.sync_date = CURRENT_TIMESTAMP
END IF
END|
delimiter ;
The error that I recieve is
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'END IF
END' at line 7
Thanks for helping
I've found the solution, here is the correct code:
delimiter |
CREATE TRIGGER update_sync_date
BEFORE UPDATE ON posts
FOR EACH ROW
BEGIN
IF #disable_sync_date_update IS NULL OR #disable_sync_date_update <> 1 THEN
SET new.sync_date = CURRENT_TIMESTAMP;
END IF;
END|
delimiter ;

I am trying to learn triggers on my sql and getting the following errors

This is the code I am executing
create trigger salarydifff
after update on office
for each row
declare sal_diff number
begin
sal_diff := :new.salary- :old.salary
end
and I am getting the following error:
Error Code : 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 'declare sal_diff number begin sal_diff := :new.salary- :old.salary end' at line 4
Your code looks like Oracle not mysql
Mysql would look like this
drop trigger if exists salarydiff;
delimiter //
create trigger salarydifff
after update on office
for each row
begin
declare sal_diff integer ;
set sal_diff = new.amt - old.amt ;
end //
delimiter ;

Create trigger error: invalid syntax

SQL query:
CREATE TRIGGER tg_newuser_insert
BEFORE INSERT ON tbl_newuser
FOR EACH ROW
BEGIN
INSERT INTO tbl_seq VALUES (NULL)
SET NEW.id = CONCAT('YTUM', LPAD(LAST_INSERT_ID(), 8, '00000'));
END
MySQL said: Documentation
#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 'SET NEW.id = CONCAT('YTUM', LPAD(LAST_INSERT_ID(), 8, '00000'));
END' at line 6
Your Formatting is off, remember DELIMITER $$ statements are required, as it is no different than creating a stored procedure.
Check the documentation any time you run into issues like this.
Scroll about half way down the documents to see the example.
But, This is what your looking for:
DELIMITER $$
CREATE TRIGGER tg_newuser_insert
BEFORE UPDATE ON tbl_newuser
FOR EACH ROW
BEGIN
SET NEW.id = CONCAT('YTUM', LPAD(LAST_INSERT_ID(), 8, '00000'));
END $$
DELIMITER ;

Auto update/fill a DATETIME column in MySQL using a trigger

I'm trying to auto update/fill datetime columns in my database.
I'm current running:
MYSQL server: 5.1.56-log
MySQL client version: mysqlnd 5.0.10
I've created two triggers based on the documentation (http://dev.mysql.com/doc/refman/5.0/en/create-trigger.html):
INSERT TRIGGER:
create trigger 'games_insert'
before insert
on games for each row
begin
set new.created = NOW();
set new.modified = NOW();
end;
MODIFICATION TRIGGER
create trigger 'games_update'
before update
on games for each row
begin
set new.modified = NOW();
end;
Both seem to be valid since you can only modify the new row prior to updating/inserting. The AFTER yields many more errors.
However, I keep getting the following syntax error and I'm not sure why:
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 ''games_insert' before insert on games for each row
begin set new.created =' at line 1
I'm interacting with the sql server through phpmyadmin and have set the delimiter to be $$
don't use "'" in trigger name
create trigger 'games_insert' (X)
create trigger games_insert (O)

Trigger, no inserting

Error
SQL query: Documentation
CREATE TRIGGER `triggers_div` AFTER INSERT ON `produits`
FOR EACH
ROW
BEGIN
INSERT INTO `div_extension` ( `devID` )
SELECT new.devID
FROM `produits`
WHERE new.depID =1;
MySQL said: Documentation
#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 5
Can somebody help me?
DELIMITER $$
CREATE TRIGGER triggers_div AFTER INSERT ON produits
FOR EACH
ROW
BEGIN
// SOME CODING HERE....
END ;$$
DELIMITER ;
It s work now when I add the DELIMITER like this DELIMITER $$ and not like this DELIMITER$$.