2011-07-20 13 views
6

अच्छी तरह से मैं mysql error #1442 के कारण जो कहते हैं के लिए इंटरनेट पर स्थानों का एक बहुत के लिए ध्यान दिया हैmysql त्रुटि 1442 का असली कारण क्या है?

तालिका संग्रहीत समारोह/ट्रिगर में 'unlucky_table' क्योंकि यह पहले से ही बयान से प्रयोग किया जाता है अद्यतन नहीं कर सकते जो इस संग्रहीत समारोह/ट्रिगर

कुछ लोगों का कहना है कि इस mysql में एक बग या एक विशेषता यह है कि यह does not प्रदान करना है लागू।

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 टेबल table1 और table2 समय है और मैं के रूप में

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

एक एसक्यूएल क्वेरी चलाने अब मैं के रूप में

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; 
अब

table1 पर एक after update trigger है जब मैं अद्यतन क्वेरी मैं निष्पादित 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

इसकी अद्यतन तालिका नाम जहां आईडी (तालिका नाम से चयन आईडी) – lovesh

+0

मैं आम तौर पर टिप्पणी कर रहा था कि आप उस तालिका से चयन नहीं कर सकते जिसे आप अपडेट कर रहे हैं। इससे कोई फर्क नहीं पड़ता कि जहां तक ​​एक ही टैबलेट नाम है। मैं देख रहा हूं कि क्यों ट्रिगर अभी विफल रहता है। लेकिन मुझे 99% यकीन है कि उपर्युक्त यह काम नहीं करता है। – BradLaney

+0

मुझे पता है कि यह काम नहीं करता है क्योंकि मैं – lovesh

संबंधित मुद्दे