Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I don't know your queries, but seems like you're inserting row into child table, and then doing an update of row in parent table.</p> <p>If that's true, you've hit this issue in MySQL: <a href="http://bugs.mysql.com/bug.php?id=48652" rel="noreferrer">http://bugs.mysql.com/bug.php?id=48652</a></p> <blockquote> <p>If a FOREIGN KEY constraint is defined on a table, any insert, update, or delete that requires the constraint condition to be checked sets shared record-level locks on the records that it looks at to check the constraint. InnoDB also sets these locks in the case where the constraint fails.</p> <p>On a single record of the first table you have: </p> <ol> <li>S lock from transaction 1 set, </li> <li>S lock from transaction 2 set, </li> <li>X lock from transaction 1 requested, blocked by S lock from transaction 2, </li> <li>X lock from transaction 2 requested, blocked by S lock from transaction 1</li> </ol> </blockquote> <p>Possible solution is to first update parent table, then insert row into child table. Suppose we need to increment some counter upon child row insertion, then queries would be:</p> <pre><code>UPDATE &lt;parent row&gt; SET count = count + 1; INSERT &lt;child row&gt;; /* if the INSERT fails, roll back the trx */ </code></pre> <p>If you want to update parent row only after inserting child row, you can use <code>FOR UPDATE</code> statement to set a lock on parent row:</p> <pre><code>SELECT &lt;parent row&gt; FOR UPDATE; INSERT &lt;child row&gt;; /* if the INSERT fails, roll back the trx */ UPDATE &lt;parent row&gt; SET count = count + 1; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload