Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>The value 2 (did) with the update instruction shouldn't be 4?</p> </blockquote> <p><strong>No, it is 4, and it should.</strong></p> <p>Because the <code>UPDATE</code> statements in SQL, are <strong>All-at-once</strong> operations. Meaning that all the expressions that you have, in the same logical phase are evaluated as if at the same point in time. In your example:</p> <pre><code>UPDATE discipline SET did = did + did WHERE did=2; </code></pre> <p>the <code>did</code> will equal to <code>4</code> because the expression <code>did = did + did</code> is evaluated wit the value <code>did = 2</code> at the same time.</p> <p><a href="http://www.sqlfiddle.com/#!2/a1b10/4" rel="nofollow"><strong>SQL Fiddle Demo</strong></a></p> <p>This will make your table looks like:</p> <pre><code>| DID | CODE | UNIVERSITY | NUMBER_STUDENTS | --------------------------------------------- | 1 | BD | U1 | 30 | | 3 | SIBD | U3 | 30 | | 4 | IBD | U2 | 30 | &lt;&lt;&lt;&lt;&lt; </code></pre> <hr> <blockquote> <p>I don't understand why the result of the instruction SELECT SUM (DISTINCT did) is 6 and isn't 9 (1+4+3).</p> </blockquote> <p>The values of <code>Did</code> after update will be:</p> <pre><code>1 3 4 </code></pre> <p>Therefore:</p> <pre><code>SELECT SUM(DISTINCT did) FROM discipline </code></pre> <p>Will equal to <code>8</code>.</p> <p><a href="http://www.sqlfiddle.com/#!2/a1b10/5" rel="nofollow"><strong>SQL Fiddle Demo</strong></a></p> <hr> <p>In your updated example, you won't be able to <code>UPDATE did</code>, becuase of the foreign key constraint and you will got an exception:</p> <blockquote> <p>Cannot delete or update a parent row: a foreign key constraint fails (<code>db_2_65e0c</code>.<code>precedence</code>, CONSTRAINT <code>precedence_ibfk_2</code> FOREIGN KEY (<code>advanced</code>) REFERENCES <code>discipline</code> (<code>did</code>))</p> </blockquote> <p><a href="http://www.sqlfiddle.com/#!2/14345" rel="nofollow"><strong>SQL Fiddle Demo</strong></a></p> <p>In this case, you will need to add the <code>ON DELETE CASCADE ON UPDATE CASCADE</code> to the other foreign key constraint:</p> <pre><code> FOREIGN KEY (advanced) REFERENCES discipline(did) </code></pre> <p>Then the <code>UPDATE</code>:</p> <pre><code>UPDATE discipline SET did=did + did WHERE did=2; </code></pre> <p>will be the same as explained before, and </p> <pre><code>SELECT SUM(DISTINCT did) FROM discipline </code></pre> <p>Will equal to <code>8</code>.</p> <p><a href="http://www.sqlfiddle.com/#!2/82c86/3" rel="nofollow"><strong>SQL fiddle demo of the two table(working)</strong></a></p>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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