MySQL Forums
Forum List  »  Newbie

Can't use OLD or NEW in triggers?
Posted by: Jan Andersen
Date: January 30, 2015 10:26AM

I am trying to create a simple trigger - this is the table:

mysql> show create table test\G
*************************** 1. row ***************************
Table: test
Create Table: CREATE TABLE `test` (
`id` int(11) NOT NULL DEFAULT '0',
`ref` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `ref` (`ref`),
CONSTRAINT `c1` FOREIGN KEY (`ref`) REFERENCES `test` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)

And this is the trigger:

delimiter .
create trigger test_insert
before insert on test
for each row
begin
if new.id=new.ref and new.id!=1
then
signal sqlstate '23000' set message_text='ID=REF and ID!=1',mysql_errno=1452;
end if;
end.
delimiter ;

But I get an error message:

ERROR 1064 (42000): 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
ERROR 1064 (42000): 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 'id=new' at line 1
ERROR 1064 (42000): 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 'ref and new' at line 1
ERROR 1064 (42000): 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 'id!=1
then
signal sqlstate '23000' set message_text='ID=REF and ID!=1',mysql_' at line 1


What am I doing wrong here? It looks to me like it doesn't like or understand 'new'. If I leave it out, it gets created, apparently, but that's not what I need.

Options: ReplyQuote


Subject
Written By
Posted
Can't use OLD or NEW in triggers?
January 30, 2015 10:26AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.