2011-07-20 39 views
6

嗯,我已經找了很多地方在互聯網上的mysql error #1442的原因它說mysql錯誤1442的真正原因是什麼?

無法更新,因爲它 已經使用的語句表中存儲函數/觸發「unlucky_table」,這調用這個存儲 函數/觸發器

有人說這是一個在mysql或它不提供的功能的錯誤。

MySQL triggers can't manipulate the table they are assigned to. All other major DBMS support this feature so hopefully MySQL will add this support soon.

有人聲稱,這是由於遞歸行爲 when you insert a record mysql is doing some lock stuff. you can't insert/update/delete rows of the same table where you insert.. because then the trigger would called again and again.. ending up in a recursion

During the insert/update you have access to the NEW object which contains all of the fields in the table involved. If you do a before insert/update and edit the field(s) that you want to change in the new object it will become a part of the calling statement and not be executed as a separately (eliminating the recursion)

現在我無法理解爲什麼這是遞歸的。我在我有2個表table1table2的情況下,我運行SQL查詢作爲

update table1 set avail = 0 where id in (select id from table2 where duration < now() - interval 2 hour); 

現在我上table1具有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; 
現在

當我執行更新查詢我得到1442錯誤。 在這種情況下什麼是遞歸?

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? 

回答

4

更新它時無法引用表。

/* my sql does not support this */ 
UPDATE tableName WHERE 1 = (SELECT 1 FROM tableName) 

MySQL Docs:

觸發器可以在自己的表訪問新老數據。觸發器也可以影響其他表,但不允許修改已調用該函數或觸發器的語句(用於讀取或寫入)的表。 (在MySQL 5.0.10之前,觸發器無法修改其他表)
+0

其UPDATE表名WHERE id(SELECT id FROM tableName) – lovesh

+0

我只是普遍評論說,你不能從你正在更新的表中選擇。只要它具有相同的表名,它在哪裏並不重要。我正在查找爲什麼該觸發器現在失敗。但我99%確定以上是爲什麼它不起作用。 – BradLaney

+0

我知道它不工作這就是我要求的原因 – lovesh