SQLException: ORA-02292: integrity constraint (SCHOOL.STUDENT_STAGE_FK) violated - child record found
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
add a comment |
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
add a comment |
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
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
java sql jdbc oracledb
edited Nov 26 '18 at 6:55
a_horse_with_no_name
304k46466561
304k46466561
asked Nov 26 '18 at 2:09
mohamednmohamedn
55
55
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
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
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 namedSCHOOL.STUDENT_STAGE_FK
for 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
|
show 1 more comment
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.
add a comment |
You must pass Value in where clause which student id you need to delete.
String sql = "DELETE FROM STUDENT WHERE ID = '"+stdId+"' ";
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
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 namedSCHOOL.STUDENT_STAGE_FK
for 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
|
show 1 more comment
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
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 namedSCHOOL.STUDENT_STAGE_FK
for 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
|
show 1 more comment
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
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
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 namedSCHOOL.STUDENT_STAGE_FK
for 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
|
show 1 more comment
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 namedSCHOOL.STUDENT_STAGE_FK
for 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_FK
for 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_FK
for 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
|
show 1 more comment
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 26 '18 at 7:02


Alermikon Alermikon
214
214
add a comment |
add a comment |
You must pass Value in where clause which student id you need to delete.
String sql = "DELETE FROM STUDENT WHERE ID = '"+stdId+"' ";
add a comment |
You must pass Value in where clause which student id you need to delete.
String sql = "DELETE FROM STUDENT WHERE ID = '"+stdId+"' ";
add a comment |
You must pass Value in where clause which student id you need to delete.
String sql = "DELETE FROM STUDENT WHERE ID = '"+stdId+"' ";
You must pass Value in where clause which student id you need to delete.
String sql = "DELETE FROM STUDENT WHERE ID = '"+stdId+"' ";
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
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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