Well, I searched a lot of places on the Internet for the reason mysql error #1442 , which says
mysql error #1442
It is not possible to update the "unlucky_table" table in a stored function / trigger because it is already in use by the statement that called this Function / trigger
Some say this is a bug in mysql or a function that it does not provide.
MySQL triggers cannot control the table to which they are bound. All other major DBMSs support this feature, so hopefully MySQL will add this support soon.
Some argue that this is due to recursive behavior when you insert a record, mysql does some locking. you cannot insert / update / delete rows of the same table you are inserting .. because then the trigger will call again and again .. ends in recursion
During insert / update, you have access to the NEW object, which contains all the fields in the table. If you do before inserting / updating and edit the fields that you want to change in the new object, it will become part of the calling statement and will not be executed separately (excluding recursion)
Now I canβt understand why this is recursive. I have a case where I have 2 tables table1 and table2 , and I run the sql query as
table1
table2
update table1 set avail = 0 where id in (select id from table2 where duration < now() - interval 2 hour);
now I have after update trigger on table1 as
after update trigger
CREATE TRIGGER trig_table1 AFTER UPDATE ON table1 FOR EACH ROW begin if old.avail=1 and new.avail=0 then delete from table2 where id=new.id; end if;
Now when I execute the update request, I get error 1442. What is recursive in this case?
is this error a lack of feature in mysql? OR does this have to do with how mysql executes queries? OR is there something logically wrong with executing such queries?
You cannot reference a table when updating it.
/* my sql does not support this */ UPDATE tableName WHERE 1 = (SELECT 1 FROM tableName)
From MySQL Docs:
A trigger can access old and new data in its table. A trigger can also affect other tables, but it is not allowed to modify a table that is already in use (for reading or writing) by the operator invoking the function or trigger. (Before MySQL 5.0.10, a trigger cannot modify other tables.)
Source: https://habr.com/ru/post/893118/More articles:Errors Using ThashSet from DeHL Collection Library - delphiCheck the exit status of the last command in ipython - pythoncanvas ImageData removes white pixels - javascriptDynamically configure fetchLimit for NSFetchedResultsController - iossocket.io: client-side callback does not work - javascriptHow to call jquery function using href - functionhttps://translate.googleusercontent.com/translate_c?depth=1&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/893120/getting-iis-application-pool-recycle-events-to-be-logged-in-the-windows-event-log&usg=ALkJrhic1kLejBjrHjixLNkBNqId5WyjNAAdding extra context in the built-in exit mode from Django - djangoCSS3 Flexible Box Model and Fixed Compression Ratio - cssServing static files using WSGI and Python 3 - pythonAll Articles