SQLException: ORA-02292: integrity constraint (SCHOOL.STUDENT_STAGE_FK) violated - child record found












0















Here i have a problem in my code when i want to delete a record:(student) from database and i know there is a related key

but i cannot solve this problem i tried deleting student that on the stage and this is the code:



public int deleteStudentStage(int stdId) //delete student by stId
{
DatabaseConnection dbconn = new DatabaseConnection();
Connection conn = dbconn.getConn();
PreparedStatement pStm = null;
int rows = 0;
try
{
//String sql = "DELETE FROM STUDENT_STAGE WHERE STUDENT_ID = ? AND STAGE_ID = ?";

String sql = "DELETE FROM STUDENT WHERE ID = ? ";

pStm = conn.prepareStatement(sql);

//fill SQL parameters from student: //
pStm.setInt(1, stdId);

// excute //
rows = pStm.executeUpdate();
}
catch (SQLException ex)
{
System.out.println("SQLException: " + ex.getMessage());
}
finally
{
try {
conn.commit();
pStm.close();
conn.close();
} catch (SQLException ex) {
System.out.println("SQLException: " + ex.getMessage());;
}
}
return rows;
}


here the schema










share|improve this question





























    0















    Here i have a problem in my code when i want to delete a record:(student) from database and i know there is a related key

    but i cannot solve this problem i tried deleting student that on the stage and this is the code:



    public int deleteStudentStage(int stdId) //delete student by stId
    {
    DatabaseConnection dbconn = new DatabaseConnection();
    Connection conn = dbconn.getConn();
    PreparedStatement pStm = null;
    int rows = 0;
    try
    {
    //String sql = "DELETE FROM STUDENT_STAGE WHERE STUDENT_ID = ? AND STAGE_ID = ?";

    String sql = "DELETE FROM STUDENT WHERE ID = ? ";

    pStm = conn.prepareStatement(sql);

    //fill SQL parameters from student: //
    pStm.setInt(1, stdId);

    // excute //
    rows = pStm.executeUpdate();
    }
    catch (SQLException ex)
    {
    System.out.println("SQLException: " + ex.getMessage());
    }
    finally
    {
    try {
    conn.commit();
    pStm.close();
    conn.close();
    } catch (SQLException ex) {
    System.out.println("SQLException: " + ex.getMessage());;
    }
    }
    return rows;
    }


    here the schema










    share|improve this question



























      0












      0








      0








      Here i have a problem in my code when i want to delete a record:(student) from database and i know there is a related key

      but i cannot solve this problem i tried deleting student that on the stage and this is the code:



      public int deleteStudentStage(int stdId) //delete student by stId
      {
      DatabaseConnection dbconn = new DatabaseConnection();
      Connection conn = dbconn.getConn();
      PreparedStatement pStm = null;
      int rows = 0;
      try
      {
      //String sql = "DELETE FROM STUDENT_STAGE WHERE STUDENT_ID = ? AND STAGE_ID = ?";

      String sql = "DELETE FROM STUDENT WHERE ID = ? ";

      pStm = conn.prepareStatement(sql);

      //fill SQL parameters from student: //
      pStm.setInt(1, stdId);

      // excute //
      rows = pStm.executeUpdate();
      }
      catch (SQLException ex)
      {
      System.out.println("SQLException: " + ex.getMessage());
      }
      finally
      {
      try {
      conn.commit();
      pStm.close();
      conn.close();
      } catch (SQLException ex) {
      System.out.println("SQLException: " + ex.getMessage());;
      }
      }
      return rows;
      }


      here the schema










      share|improve this question
















      Here i have a problem in my code when i want to delete a record:(student) from database and i know there is a related key

      but i cannot solve this problem i tried deleting student that on the stage and this is the code:



      public int deleteStudentStage(int stdId) //delete student by stId
      {
      DatabaseConnection dbconn = new DatabaseConnection();
      Connection conn = dbconn.getConn();
      PreparedStatement pStm = null;
      int rows = 0;
      try
      {
      //String sql = "DELETE FROM STUDENT_STAGE WHERE STUDENT_ID = ? AND STAGE_ID = ?";

      String sql = "DELETE FROM STUDENT WHERE ID = ? ";

      pStm = conn.prepareStatement(sql);

      //fill SQL parameters from student: //
      pStm.setInt(1, stdId);

      // excute //
      rows = pStm.executeUpdate();
      }
      catch (SQLException ex)
      {
      System.out.println("SQLException: " + ex.getMessage());
      }
      finally
      {
      try {
      conn.commit();
      pStm.close();
      conn.close();
      } catch (SQLException ex) {
      System.out.println("SQLException: " + ex.getMessage());;
      }
      }
      return rows;
      }


      here the schema







      java sql jdbc oracledb






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 26 '18 at 6:55









      a_horse_with_no_name

      304k46466561




      304k46466561










      asked Nov 26 '18 at 2:09









      mohamednmohamedn

      55




      55
























          3 Answers
          3






          active

          oldest

          votes


















          0














          can you give more information about it?, normally that happens when you have a foregin key, in this case of the table STUDENT, you need to delete all rows in the other table first before delete STUDENT, you also can solve it, adding a delete on cascade, when you create the foregin key



          you need then do the following:



          DELETE FROM STUDENT_STAGE WHERE STUDENT_ID=? 
          DELETE FROM STUDENT WHERE ID = ?


          also you can update the foregin key in the table for cascade delete:



          ALTER TABLE STUDENT_STAGE DROP FOREIGN KEY STUDENT_STAGE_FK; 
          ALTER TABLE STUDENT_STAGE ADD CONSTRAINT STUDENT_STAGE_FK FOREIGN KEY STUDENT_ID REFERENCES STUDENT (ID) ON DELETE CASCADE ON UPDATE CASCADE;


          Whith the update of the foregin key, deleting a studen, you also delete all the information of this student in the table STUDENT_STAGE






          share|improve this answer


























          • yes i know this happens but i have but i want to delete a specific student on a specific stage instead of delete on cascade ,, do you understand me?

            – mohamedn
            Nov 26 '18 at 2:52











          • i think that yes, i understoo you @mohamedn , but you need to delete the info in the table stage, for this student, before delete the student, or you can update the foregin key named SCHOOL.STUDENT_STAGE_FKfor delete in cascade

            – Marco Pens
            Nov 26 '18 at 3:02











          • this the point , how can i delete the info in table stage for specific student however, the specific stage has more one student so how can i fix this problem. can you write a code sql command for this as an example

            – mohamedn
            Nov 26 '18 at 3:18













          • yes, i can write it, but can you share the schema of those tables?

            – Marco Pens
            Nov 26 '18 at 3:20











          • of course , am added a link above that contain the schema image @MarcoPens

            – mohamedn
            Nov 26 '18 at 4:15



















          0














          First you need to delete elements from child table, you even have almost right query for this.



          DELETE FROM STUDENT_STAGE WHERE STUDENT_ID=?


          And after this delete student with current query.






          share|improve this answer































            -1














            You must pass Value in where clause which student id you need to delete.



            String sql = "DELETE FROM STUDENT WHERE ID = '"+stdId+"' ";





            share|improve this answer

























              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53473932%2fsqlexception-ora-02292-integrity-constraint-school-student-stage-fk-violated%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              0














              can you give more information about it?, normally that happens when you have a foregin key, in this case of the table STUDENT, you need to delete all rows in the other table first before delete STUDENT, you also can solve it, adding a delete on cascade, when you create the foregin key



              you need then do the following:



              DELETE FROM STUDENT_STAGE WHERE STUDENT_ID=? 
              DELETE FROM STUDENT WHERE ID = ?


              also you can update the foregin key in the table for cascade delete:



              ALTER TABLE STUDENT_STAGE DROP FOREIGN KEY STUDENT_STAGE_FK; 
              ALTER TABLE STUDENT_STAGE ADD CONSTRAINT STUDENT_STAGE_FK FOREIGN KEY STUDENT_ID REFERENCES STUDENT (ID) ON DELETE CASCADE ON UPDATE CASCADE;


              Whith the update of the foregin key, deleting a studen, you also delete all the information of this student in the table STUDENT_STAGE






              share|improve this answer


























              • yes i know this happens but i have but i want to delete a specific student on a specific stage instead of delete on cascade ,, do you understand me?

                – mohamedn
                Nov 26 '18 at 2:52











              • i think that yes, i understoo you @mohamedn , but you need to delete the info in the table stage, for this student, before delete the student, or you can update the foregin key named SCHOOL.STUDENT_STAGE_FKfor delete in cascade

                – Marco Pens
                Nov 26 '18 at 3:02











              • this the point , how can i delete the info in table stage for specific student however, the specific stage has more one student so how can i fix this problem. can you write a code sql command for this as an example

                – mohamedn
                Nov 26 '18 at 3:18













              • yes, i can write it, but can you share the schema of those tables?

                – Marco Pens
                Nov 26 '18 at 3:20











              • of course , am added a link above that contain the schema image @MarcoPens

                – mohamedn
                Nov 26 '18 at 4:15
















              0














              can you give more information about it?, normally that happens when you have a foregin key, in this case of the table STUDENT, you need to delete all rows in the other table first before delete STUDENT, you also can solve it, adding a delete on cascade, when you create the foregin key



              you need then do the following:



              DELETE FROM STUDENT_STAGE WHERE STUDENT_ID=? 
              DELETE FROM STUDENT WHERE ID = ?


              also you can update the foregin key in the table for cascade delete:



              ALTER TABLE STUDENT_STAGE DROP FOREIGN KEY STUDENT_STAGE_FK; 
              ALTER TABLE STUDENT_STAGE ADD CONSTRAINT STUDENT_STAGE_FK FOREIGN KEY STUDENT_ID REFERENCES STUDENT (ID) ON DELETE CASCADE ON UPDATE CASCADE;


              Whith the update of the foregin key, deleting a studen, you also delete all the information of this student in the table STUDENT_STAGE






              share|improve this answer


























              • yes i know this happens but i have but i want to delete a specific student on a specific stage instead of delete on cascade ,, do you understand me?

                – mohamedn
                Nov 26 '18 at 2:52











              • i think that yes, i understoo you @mohamedn , but you need to delete the info in the table stage, for this student, before delete the student, or you can update the foregin key named SCHOOL.STUDENT_STAGE_FKfor delete in cascade

                – Marco Pens
                Nov 26 '18 at 3:02











              • this the point , how can i delete the info in table stage for specific student however, the specific stage has more one student so how can i fix this problem. can you write a code sql command for this as an example

                – mohamedn
                Nov 26 '18 at 3:18













              • yes, i can write it, but can you share the schema of those tables?

                – Marco Pens
                Nov 26 '18 at 3:20











              • of course , am added a link above that contain the schema image @MarcoPens

                – mohamedn
                Nov 26 '18 at 4:15














              0












              0








              0







              can you give more information about it?, normally that happens when you have a foregin key, in this case of the table STUDENT, you need to delete all rows in the other table first before delete STUDENT, you also can solve it, adding a delete on cascade, when you create the foregin key



              you need then do the following:



              DELETE FROM STUDENT_STAGE WHERE STUDENT_ID=? 
              DELETE FROM STUDENT WHERE ID = ?


              also you can update the foregin key in the table for cascade delete:



              ALTER TABLE STUDENT_STAGE DROP FOREIGN KEY STUDENT_STAGE_FK; 
              ALTER TABLE STUDENT_STAGE ADD CONSTRAINT STUDENT_STAGE_FK FOREIGN KEY STUDENT_ID REFERENCES STUDENT (ID) ON DELETE CASCADE ON UPDATE CASCADE;


              Whith the update of the foregin key, deleting a studen, you also delete all the information of this student in the table STUDENT_STAGE






              share|improve this answer















              can you give more information about it?, normally that happens when you have a foregin key, in this case of the table STUDENT, you need to delete all rows in the other table first before delete STUDENT, you also can solve it, adding a delete on cascade, when you create the foregin key



              you need then do the following:



              DELETE FROM STUDENT_STAGE WHERE STUDENT_ID=? 
              DELETE FROM STUDENT WHERE ID = ?


              also you can update the foregin key in the table for cascade delete:



              ALTER TABLE STUDENT_STAGE DROP FOREIGN KEY STUDENT_STAGE_FK; 
              ALTER TABLE STUDENT_STAGE ADD CONSTRAINT STUDENT_STAGE_FK FOREIGN KEY STUDENT_ID REFERENCES STUDENT (ID) ON DELETE CASCADE ON UPDATE CASCADE;


              Whith the update of the foregin key, deleting a studen, you also delete all the information of this student in the table STUDENT_STAGE







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Nov 26 '18 at 11:25

























              answered Nov 26 '18 at 2:31









              Marco PensMarco Pens

              916




              916













              • yes i know this happens but i have but i want to delete a specific student on a specific stage instead of delete on cascade ,, do you understand me?

                – mohamedn
                Nov 26 '18 at 2:52











              • i think that yes, i understoo you @mohamedn , but you need to delete the info in the table stage, for this student, before delete the student, or you can update the foregin key named SCHOOL.STUDENT_STAGE_FKfor delete in cascade

                – Marco Pens
                Nov 26 '18 at 3:02











              • this the point , how can i delete the info in table stage for specific student however, the specific stage has more one student so how can i fix this problem. can you write a code sql command for this as an example

                – mohamedn
                Nov 26 '18 at 3:18













              • yes, i can write it, but can you share the schema of those tables?

                – Marco Pens
                Nov 26 '18 at 3:20











              • of course , am added a link above that contain the schema image @MarcoPens

                – mohamedn
                Nov 26 '18 at 4:15



















              • yes i know this happens but i have but i want to delete a specific student on a specific stage instead of delete on cascade ,, do you understand me?

                – mohamedn
                Nov 26 '18 at 2:52











              • i think that yes, i understoo you @mohamedn , but you need to delete the info in the table stage, for this student, before delete the student, or you can update the foregin key named SCHOOL.STUDENT_STAGE_FKfor delete in cascade

                – Marco Pens
                Nov 26 '18 at 3:02











              • this the point , how can i delete the info in table stage for specific student however, the specific stage has more one student so how can i fix this problem. can you write a code sql command for this as an example

                – mohamedn
                Nov 26 '18 at 3:18













              • yes, i can write it, but can you share the schema of those tables?

                – Marco Pens
                Nov 26 '18 at 3:20











              • of course , am added a link above that contain the schema image @MarcoPens

                – mohamedn
                Nov 26 '18 at 4:15

















              yes i know this happens but i have but i want to delete a specific student on a specific stage instead of delete on cascade ,, do you understand me?

              – mohamedn
              Nov 26 '18 at 2:52





              yes i know this happens but i have but i want to delete a specific student on a specific stage instead of delete on cascade ,, do you understand me?

              – mohamedn
              Nov 26 '18 at 2:52













              i think that yes, i understoo you @mohamedn , but you need to delete the info in the table stage, for this student, before delete the student, or you can update the foregin key named SCHOOL.STUDENT_STAGE_FKfor delete in cascade

              – Marco Pens
              Nov 26 '18 at 3:02





              i think that yes, i understoo you @mohamedn , but you need to delete the info in the table stage, for this student, before delete the student, or you can update the foregin key named SCHOOL.STUDENT_STAGE_FKfor delete in cascade

              – Marco Pens
              Nov 26 '18 at 3:02













              this the point , how can i delete the info in table stage for specific student however, the specific stage has more one student so how can i fix this problem. can you write a code sql command for this as an example

              – mohamedn
              Nov 26 '18 at 3:18







              this the point , how can i delete the info in table stage for specific student however, the specific stage has more one student so how can i fix this problem. can you write a code sql command for this as an example

              – mohamedn
              Nov 26 '18 at 3:18















              yes, i can write it, but can you share the schema of those tables?

              – Marco Pens
              Nov 26 '18 at 3:20





              yes, i can write it, but can you share the schema of those tables?

              – Marco Pens
              Nov 26 '18 at 3:20













              of course , am added a link above that contain the schema image @MarcoPens

              – mohamedn
              Nov 26 '18 at 4:15





              of course , am added a link above that contain the schema image @MarcoPens

              – mohamedn
              Nov 26 '18 at 4:15













              0














              First you need to delete elements from child table, you even have almost right query for this.



              DELETE FROM STUDENT_STAGE WHERE STUDENT_ID=?


              And after this delete student with current query.






              share|improve this answer




























                0














                First you need to delete elements from child table, you even have almost right query for this.



                DELETE FROM STUDENT_STAGE WHERE STUDENT_ID=?


                And after this delete student with current query.






                share|improve this answer


























                  0












                  0








                  0







                  First you need to delete elements from child table, you even have almost right query for this.



                  DELETE FROM STUDENT_STAGE WHERE STUDENT_ID=?


                  And after this delete student with current query.






                  share|improve this answer













                  First you need to delete elements from child table, you even have almost right query for this.



                  DELETE FROM STUDENT_STAGE WHERE STUDENT_ID=?


                  And after this delete student with current query.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 26 '18 at 7:02









                  Alermikon Alermikon

                  214




                  214























                      -1














                      You must pass Value in where clause which student id you need to delete.



                      String sql = "DELETE FROM STUDENT WHERE ID = '"+stdId+"' ";





                      share|improve this answer






























                        -1














                        You must pass Value in where clause which student id you need to delete.



                        String sql = "DELETE FROM STUDENT WHERE ID = '"+stdId+"' ";





                        share|improve this answer




























                          -1












                          -1








                          -1







                          You must pass Value in where clause which student id you need to delete.



                          String sql = "DELETE FROM STUDENT WHERE ID = '"+stdId+"' ";





                          share|improve this answer















                          You must pass Value in where clause which student id you need to delete.



                          String sql = "DELETE FROM STUDENT WHERE ID = '"+stdId+"' ";






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Nov 26 '18 at 7:47









                          Andrei Suvorkov

                          4,20541030




                          4,20541030










                          answered Nov 26 '18 at 4:45









                          Arsalan Ali ShahArsalan Ali Shah

                          9




                          9






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53473932%2fsqlexception-ora-02292-integrity-constraint-school-student-stage-fk-violated%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Create new schema in PostgreSQL using DBeaver

                              Deepest pit of an array with Javascript: test on Codility

                              Costa Masnaga