MySQL Workbench "OLD" syntax changed? - mysql

I had a trigger set in MySQL workbench that used to accomplish what I wanted. Recently, my workplace updated MySQL workbench to version 6.3.10 across all computers and the triggers were dropped during the update for some reason.
Basically, the trigger should update a column with the current date when a different column is changed. This was previously achieved by using the following syntax:
if (NEW.colname <> OLD.colname or (OLD.colname is null && NEW.colname is not null))
then
set NEW.othercolname = current_date()
The issue is when I try to recreate the same trigger in the new MySQL version and apply it, I am met with the error:
ERROR 1363: There is no OLD row in on INSERT trigger
It seems to me that MySQL is looking for a row named OLD, which does not exist; however, this syntax used to work in previous versions for the intended result. Can anyone suggest an alternative syntax to accomplish this?

First of all, the error message comes from mysql, not from mysql workbench, so mysql workbench's version is irrelevant here.
Secondly, the error message is pretty clear: you are trying to refer to the OLD row in an insert trigger. This is obviously impossible, since there is no old row when you insert a new one.
You need to change either the trigger to catch an update event or change the trigger logic.

ERROR 1363: There is no OLD row in on INSERT trigger
You've got MySQL Workbench set to create an INSERT trigger. As the error message says, there's no OLD row in an INSERT trigger.
What you want is an UPDATE trigger.

Related

Trigger not creating in mySQL

I am trying to write a trigger for my database in mySQL yet when I run the query and then run SHOW TRIGGERS nothing is showing. I believe my syntax is correct and am not running into any errors when running the query. The query is below, any ideas why this is happening?
CREATE TRIGGER after_violation_insert
AFTER INSERT
ON violation
FOR EACH ROW
UPDATE listing
SET Listing_True_False = "true"
WHERE Listing_ID = new.Listing_ID;

SQL Server implementation of MySQL's "new" functionality?

MySQL has this incredibly useful yet proprietary record holding the "new row"(NEW).
I wonder if there is any SQL server command to replace the New in MySQL.
In mysql, in a trigger, I would use something like this.
INSERT INTO products(idProduct,idReference,date)
VALUES (NEW.idProduct,idReference,NOW());
However I don't know how if is it possible to do the same command but in Sql Server.
I hope I have been clear in my question.
If not explicit, or if it isn't possible to implement what I want, I apologize.
Thank you all.
In your trigger
INSERT products(idProduct,idReference,date)
SELECT idProduct, idReference, GETDATE()
FROM inserted
new is only available in a trigger if I'm not mistaken.
As SQL Server uses statement-level triggers and MySQL uses row-level triggers there is not direct equivalent to the concept of "the new row" in a trigger. Read the documentation on the inserted virtual table that is available in SQL Server.
Example usage: SQL Insert trigger to update INSERTED table values

UPDATE my_table doesn't work correctly

I'm using the following query to replace old link with a new one:
UPDATE my_table SET file = 'link' WHERE my_table.file ='old_link';
In my tests I can't duplicate that and I'm not sure what's wrong with that query, but apparently sometimes it leaves the old entry and inserts a new one instead of updating!
mysql ver:
5.6.12-56 Percona Server, table type: innodb
The query looks fine to me. UPDATE should never create new rows, only modify existing rows. The problem is probably in another part of the code.
Although an UPDATE won't fire an insert in its own, there could exist triggers in the database that would fire an INSERT whenever a record gets updated.
Here are some links that you should check:
CREATE TRIGGER Syntax
Trigger Syntax and Examples

error 1064 in trigger mysql

This is my first time using mysql and I am tying to learn how to use trigger.
Im using navicat, i go to table design and then go triggers tab. I create a trigger named testing and in definition I typed:
delimiter |
CREATE TRIGGER lpr.mytesting AFTER INSERT ON lpr.lpr_opt_upload
FOR EACH ROW BEGIN
set new.lpr_text := reverse(new.lpr_text);
END;
| delimiter ;
All im tying to do is whenever something new is inserted, I reverse the text in lpr_text field. However, i get "1064 - you have an error in your SQL; check the manual that corresponds o your MySql server version for the right syntax to yse 'ON lpr_opt_upload' FOR EACH ROW create trigger testing before insert on lpr_op' at line 1." I dont understand what Im doing wrong, I am just copying an trigger example.
//-------------------------------------------------------------------------//
I figured out the problem. I am using navicat and in navicat trigger tab, you only type the body into the definition, Not the header (ex: CREATE TRIGGER lpr.mytesting AFTER INSERT ON lpr.lpr_opt_upload). There are check box next to the name of your trigger and you use those instead of writing your own header.
The DELIMITER command is a client command, which not all clients support (it doesn't get sent to the server at all, it just instructs the client how to tell statements apart in order that they get sent to the server correctly). References to it in the MySQL manual assume that you are using the 'official' MySQL clients, such as the mysql command-line tool or MySQL Workbench (both of which support the DELIMITER command).
I don't know how to change the statement delimiter in Navicat, but that is the root of your problem.

MySQL Trigger converting

I have such SqLite trigger:
CREATE TRIGGER update_rating AFTER UPDATE ON gameServers
FOR EACH ROW BEGIN UPDATE gameServers
SET rated_order=NEW.rating || '#' || NEW._address
WHERE rowid=NEW.rowid; END;
Help me, please, with converting it into MySQL.
CREATE TRIGGER update_rating BEFORE UPDATE ON gameServers
FOR EACH ROW SET NEW.rated_order=CONCAT(NEW.rating,' # ',NEW.address);
OF COURSE - this does nothing ON INSERT...(!)
Note that I changed it from AFTER to BEFORE (quite deliberately): Apart from the question why I should start another UPDATE after the one triggering the trigger, there is the issue Updating table in trigger after update on the same table
Your (full) "UPDATE" statement in your AFTER trigger would cause a circular trigger-calling-trigger-calling-trigger... (which mysql would prevent by refusing the statement)
EDIT: At first I wanted to use the '||' too for string concatenation, but this is MySQL and not Oracle :)
It should work, I actually tested it on one of my own tables just to be sure. This trigger stuff is fickle at times :)