Delete a row after insert with trigger mysql - 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...

Related

Can't edit mysql table: Getting odd error that I can't find anything on: #1305 - PROCEDURE db_name.limit_control does not exist [duplicate]

I'm using following update statement to update a row in a table of my database
update department
set budget = budget + 0.01
where dept_name = 'Physics';
However, running this code gives the following error:
ERROR 1305 (42000): PROCEDURE university.update_budget_proc does not exist
I'm not able to make anything out of this error message. I have attached the screenshot of when I type the commands in terminal.
Also, to mention that I have never created any procedure 'update_budget_proc' which is mentioned in the error message. Does it really have anything to do with the update statement ?
The guess in the comments above turned out to be right.
The UPDATE was executing a trigger, which in turn had a reference to a stored procedure that no longer exists.

MySQL trigger Error: 1442

DB: MySQL
Using: MySQL Workbench
I am currently working on a project for class where we have to design a airport website in which users can login/register and buy tickets for various flights offered. After working on the project for some time I realized that my trigger is not working.
I receive the error: Error Code: 1442. Can't update table 'reservation' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
I stumbled upon this error by trying to insert a reservation before creating the form for users to select and buy tickets. I was wondering if someone could take a look at my database, insert command use, and also my trigger code. Any help would be much appreciated because I am at wits end trying to figure this out for hours.
Insert command: http://pastebin.com/tyBPWNDd
Entire DB and Trigger in question: http://pastebin.com/BvUj1NdH
Solved: thanks to all helpers I was able to identify my problem which was using recursive triggers when MySQL does not support them. I solved my problem by removing the recursive code and letting my website backend handle the current_date set instead of the trigger. I will post the fixed trigger shortly.
Yes, the error is self explanatory. You are trying to create a recursive trigger which is not supported in MySQL as can be seen in your posted code
Create Trigger reservation_all
after insert on Reservation
for each row
begin
update Reservation
Check MySQL Documentation on same for more infromation. Quoting from that
Stored functions cannot be used recursively.
A stored function or trigger cannot modify a table that is already being used (for reading or writing) by the statement that invoked the
function or trigger.

Why am I getting a table doesn't exist error for a table that exists?

I am running a simple update statement:
UPDATE sometab
SET `somefield1` = '19',
`somefield2` = '3734941'
WHERE somefield3 = '1234';
and I am getting the error:
ERROR 1146 (42S02): Table 'prod._sometab_new' doesn't exist
I can successfully select from the table where somefield3 is 1234.
Why am I getting a table doesn't exist error for a table that exists? And why does the error message refer to a different table? I don't see any triggers associated with the table.
Additional information: A colleague just noticed that it is referring to a prod scheme, but the statement is running in a dev schema built from prod. The update statement works in DBs that were built a few days ago using the same method, but all of the DBs built after some, as of yet, unknown time exhibit the error.
The current theory is that a conversion script to move us to UTF-8 is currently running and creating tables like _ORIG_new as part of its conversion. We are going to wait for the conversion script to finish and then rebuild the dev databases and see if the error still persists.
Does this happen if you also try Insert into or Delete statements ?
Insert INTO sometab(somefield1, somefield2) VALUES (a, b).
If that works you should not have problems probably, otherwise you have problems accessing your database.
Second, are you sure you are using the correct database file and that are you connected to it properly. If you are using it in external application (c#), check your connection strings.
Also check how are you executing the query. I cant think of other more specific solution to your problem

Insert Error on Delete

So I am trying to delete a record from a table and it is getting the strangest error.
The query is
DELETE FROM table where column1='x' and column2='y'
note I entered mock names for the table and columns for security reasons.
Any way I am receiving the error:
Msg 213, Level 16, State 1, Procedure CustomVLANPicsMaintenance, Line 11
Insert Error: Column name or number of supplied values does not match table definition.
Which is strange since this is a delete and not an insert. There are many many stored procedures that are on this server but this specific one "CustomVLANPicsMaintenance" does not exsist anywhere!!!!!! Its not even related to the table I'm trying to delete rows from. Whats even more odd is that until recently, the delete method has been working. I cannot find any links from this table or anything that says it should be running a procedure on delete (although Im not too entirely sure where I would find this).
Has anyone gotten something like this before?
I'd first try checking if there are any DELETE triggers present on the table. In Management Studio trigger list is located under Databases/%DB name%/Tables/dbo.%Table name%/Triggers.

MySQL Triggers - AFTER INSERT trigger + UDF sys_exec() issue

Problem: I've got a table which holds certain records. After the insert has been done, I want to call an external program (php script) via MySQL's sys_* UDFs.
Now, the issue - the trigger I have passes the ID of the record to the script.
When I try to pull the data out via the script, I get 0 rows.
During my own testing, I came to a conclusion that the trigger invokes the php script and passes the parameters BEFORE the actual insert occured, thus I get no records for given ID.
I've tested this on MySQL 5.0.75 and 5.1.41 (Ubuntu OS).
I can confirm that parameters get passed to the script before actual insert happens because I've added sleep(2); to my php script and I've gotten the data correctly.
Without sleep(); statement, I'm receiving 0 records for given ID.
My question is - how to fix this problem without having to hardcode some sort of delay within the php script?
I don't have the liberty of assuming that 2 seconds (or 10 seconds) will be sufficient delay, so I want everything to flow "naturally", when one command finishes - the other gets executed.
I assumed that if the trigger is of type AFTER INSERT, everything within the body of the trigger will get executed after MySQL actually inserts the data.
Table layout:
CREATE TABLE test (
id int not null auto_increment PRIMARY KEY,
random_data varchar(255) not null
);
Trigger layout:
DELIMITER $$
CREATE TRIGGER `test_after_insert` AFTER INSERT ON `test`
FOR EACH ROW BEGIN
SET #exec_var = sys_exec(CONCAT('php /var/www/xyz/servers/dispatcher.php ', NEW.id));
END;
$$
DELIMITER ;
Disclaimer: I know the security issues when using sys_exec function, my problem is that the MySQL doesn't insert FIRST and THEN call the script with necessary parameters.
If anyone can shed some light on how to fix this or has a different approach that doesn't involve SELECT INTO OUTFILE and using FAM - I'd be very grateful. Thanks in advance.
Even if you use an AFTER trigger, the row isn't committed yet. But sys_exec() doesn't return until the php script exits, so the AFTER trigger can't complete, therefore you can't commit the INSERT either.
This is by design. After all, you may do more operations within the same transaction, or you may roll back the transaction. That's the problem with invoking external processes from a trigger: external processes can't see data within the scope of the transaction in the database.
You shouldn't do this task with a trigger. At best, you should use the trigger to set a "flag" column and then write an external process to look for rows with the flag set and then invoke that PHP script. That way only rows that have successfully been inserted AND committed will be processed.
If I understand it clearly, you insert a row in your DB. That invoke a trigger that launch an external command written in PHP. That command queries in its turn the same DB by using the id of the inserted row?
I don't think this is a problem of "delay".
The real "problem" is your initial insert and you external command connect to the same DB on two different sessions -- probably in two different transactions (depending your database engine and your transaction isolation level).
I assume, when the trigger in invoked the row insert is not yet committed to the DB. So the external command still see the DB as it was before.
BTW, if the above explanation is quite speculative -- what is more evident to me is that you should probably think about a different design than trying to made that work as it is.