Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9098519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T00:22:50+00:00 2026-06-17T00:22:50+00:00

I have these SQL instructions: CREATE TABLE discipline ( did INT PRIMARY KEY, code

  • 0

I have these SQL instructions:

 CREATE TABLE discipline (
       did INT PRIMARY KEY,
       code VARCHAR(127) UNIQUE,
       university VARCHAR(127),
       number_students INT
 ) ENGINE=INNODB; 

 CREATE TABLE precedence(
      type VARCHAR(127),
      basic INT,
      advanced INT,
      PRIMARY KEY (basic, advanced), 
      FOREIGN KEY (basic) REFERENCES discipline(did) ON DELETE CASCADE ON UPDATE CASCADE,
      FOREIGN KEY (advanced) REFERENCES discipline(did)
 )ENGINE=INNODB;

 INSERT INTO discipline VALUES (1,'BD','U1',30);
 INSERT INTO discipline VALUES (2,'IBD','U2',30);
 INSERT INTO discipline VALUES (3,'SIBD','U3',30);
 INSERT INTO precedence VALUES ('optional',1,2);
 INSERT INTO precedence VALUES ('mandatory',2,3);
 UPDATE discipline SET did=did+did WHERE did=2;

I don’t understand why the result of the instruction SELECT SUM (DISTINCT did) is 6 and isn’t 8 (1+4+3). The value 2 (did) with the update instruction shouldn’t be 4? Can you explain this to me? Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T00:22:51+00:00Added an answer on June 17, 2026 at 12:22 am

    The value 2 (did) with the update instruction shouldn’t be 4?

    No, it is 4, and it should.

    Because the UPDATE statements in SQL, are All-at-once 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:

    UPDATE discipline 
    SET did = did + did 
    WHERE did=2;
    

    the did will equal to 4 because the expression did = did + did is evaluated wit the value did = 2 at the same time.

    SQL Fiddle Demo

    This will make your table looks like:

    | DID | CODE | UNIVERSITY | NUMBER_STUDENTS |
    ---------------------------------------------
    |   1 |   BD |         U1 |              30 |
    |   3 | SIBD |         U3 |              30 |
    |   4 |  IBD |         U2 |              30 | <<<<<
    

    I don’t understand why the result of the instruction SELECT SUM
    (DISTINCT did) is 6 and isn’t 9 (1+4+3).

    The values of Did after update will be:

    1
    3
    4
    

    Therefore:

    SELECT SUM(DISTINCT did) 
    FROM discipline 
    

    Will equal to 8.

    SQL Fiddle Demo


    In your updated example, you won’t be able to UPDATE did, becuase of the foreign key constraint and you will got an exception:

    Cannot delete or update a parent row: a foreign key constraint fails
    (db_2_65e0c.precedence, CONSTRAINT precedence_ibfk_2 FOREIGN KEY
    (advanced) REFERENCES discipline (did))

    SQL Fiddle Demo

    In this case, you will need to add the ON DELETE CASCADE ON UPDATE CASCADE to the other foreign key constraint:

     FOREIGN KEY (advanced) REFERENCES discipline(did) 
    

    Then the UPDATE:

    UPDATE discipline
    SET did=did + did
    WHERE did=2;
    

    will be the same as explained before, and

    SELECT SUM(DISTINCT did) 
    FROM discipline 
    

    Will equal to 8.

    SQL fiddle demo of the two table(working)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

im using sql server 2005. I have these tables: CREATE TABLE [dbo].[Proyectos2]( [Id] [int]
I have an SQL procedure with these parameters @WorkflowApprovalHistoryId int OUTPUT, @ProjectUid uniqueidentifier, @ProjectId
Hi there I have an SQL table that looks like this: CREATE TABLE IF
Let's say I have these table: What should be my sql request to get
I have these data on a table (using SQL Server 2005): ID ParentID StartTime
This is SQL Server 2008. I have these two tables and a join: DECLARE
We are upgrading/converting several old Access databases to MS-SQL. Many of these databases have
I have a SQL statement which returns a number of rows and these are
I have a few sql scripts that I need to run via SQL*Plus. These
I have a SQL Server database with 500,000 records in table main . There

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.