Trigger not creating in mySQL - 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;

Related

MySQL Workbench "OLD" syntax changed?

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.

Delete a row after insert with trigger mysql

I'm trying to create a trigger to delete my IP from my internal app (as a dev half of the logs are mine) to keep the log cleaner. To do that I constantly go and query manually in phpMyAdmin:
DELETE FROM `log` WHERE `ip1` = "xxx.xxx.xxx.xxx";
I tried creating a trigger in phpMyAdmin with that code but I keep getting the syntax error. I tried with both old. and new., also before and after, nothing works I get this error:
Error: SQLSTATE[HY000]: General error: 1442 Can't update table 'log' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
(the trigger is accepted in phpMyAdmin but when I access the app I get that).
As I read the reason it's because you can't use delete on a insert trigger, because the table gets locked in the first place, etc., but I don't find any way around it to achieve what it seemed to me like a simple task... sorry my knowledge on MySQL is quite limited. Surely there must be a way...

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

MySQL How do I create a trigger that selects from another table?

I am working on a project, and I had to make a few changes to a table in order to help speed I am working on a project, and I had to make a few changes to a table in order to help speed things up. I tried to create the following trigger, but it does not work and just gives me the standard #1064 you have an error in your syntax error.
create trigger _wc_insert_trigger before insert on widget_components
set new.component_id = (select id from components c where c.part_no=new.part_no)
I've never worked with triggers before, and I don't really understand everything that I get from Google responses.things up. I tried to create the following trigger, but it does not work and just gives me the standard #1064 you have an error in your syntax error.
Any ideas on how to get something like this to work? I don't have the budget to go in and refactor the existing application to take into account this change.
delimiter |
create trigger wc_insert_trigger before insert on widget_components
FOR EACH ROW BEGIN
set new.component_id = (select id
from components c
where c.part_no=new.part_no);
END
|
delimiter ;

MS Access - Syntax error on CREATE TABLE Statement in MySql - using Trigger

For some reason, i can't get this to work..
CREATE TRIGGER triggerupdate
INSTEAD OF UPDATE ON ORDERDETAILS
AS
IF UPDATE(ORDERVALUE) BEGIN
Print ('INSTEAD OF Trigger [triggerupdate] - Trigger executed!!')
Print('You cannot update Order Value')
END
I'm trying to do a trigger if someone tries to update OrderValue. IT kept saying that Syntax error on Create Table.
Let me know what I have wrong.
Thanks
You can't use MS Access. Access does not support TRIGGERs, that's why you're getting Syntax errors, because "CREATE TRIGGER" is not supported SQL in Access.
Also, Print isn't a function in Access SQL either.
It's possible that you're meant to use Access as a front-end for MSSQL Server, which can be done, and where your SQL statements will become valid (but your use of Print is archaic).