I have an application that I am working on that will query a database and display the results. The program also makes changes to the database (update, delete, insert). I had most of these features working correctly until recently when I made some changes. Now I am getting an SQLException that tells me I am violating a foreign key constraint. I have looked it up and found out that the violation is a result of more than one table sharing data. Is there a way to overcome this? How would I make the update without violating the constraint? here is my update method:
InstructorEditorPanel updateEditorPanel = new InstructorEditorPanel();
updateEditorPanel.setFieldText(InstructorEditorPanel.FieldTitle.B_NUMBER, updBNumber);
updateEditorPanel.setFieldText(InstructorEditorPanel.FieldTitle.FIRST_NAME, updFName);
updateEditorPanel.setFieldText(InstructorEditorPanel.FieldTitle.LAST_NAME, updLName);
int result = JOptionPane.showConfirmDialog(null, updateEditorPanel,
"Update Instructor", JOptionPane.OK_CANCEL_OPTION,JOptionPane.PLAIN_MESSAGE);
if(result == JOptionPane.OK_OPTION)
{
for (InstructorEditorPanel.FieldTitle fieldTitle : InstructorEditorPanel.FieldTitle
.values()) {
bNum = getBNumber(updateEditorPanel.getFieldText(fieldTitle.values()[0]));
fName = getFirstName(updateEditorPanel.getFieldText(fieldTitle.values()[1]));
lName = getLastName(updateEditorPanel.getFieldText(fieldTitle.values()[2]));
}
try
{
connection = DriverManager.getConnection(URL);
updateInstructor = connection.prepareStatement(
"UPDATE Instructor SET BNUMBER = ?, FIRSTNAME = ?, LASTNAME = ? WHERE BNUMBER = ?");
}catch(SQLException sqlException){
sqlException.printStackTrace();
System.exit(1);
}//end catch
try
{
updateInstructor.setString(1, bNum);
updateInstructor.setString(2, fName);
updateInstructor.setString(3, lName);
updateInstructor.setString(4,mNumber);
updateInstructor.executeUpdate();
}catch(SQLException sqlException){
sqlException.printStackTrace();
}//end of catch
finally
{
close();
}//end
}
Display(panel); }
I had the method working without any problems until recently when I made some changes. I don’t know what I did, but now I am getting the foreign key constraint violated exception
here is the exception:java.sql.SQLIntegrityConstraintViolationException: UPDATE on table ‘INSTRUCTOR’ caused a violation of foreign key constraint ‘SQL120408141918440’ for key (1234500000). The statement has been rolled back.
database info
CREATE TABLE Instructor (
BNumber varchar(10) NOT NULL,
FirstName varchar(20),
LastName varchar(30),
PRIMARY KEY (BNumber)
);
CREATE TABLE Section (
CRN int NOT NULL,
Term varchar(6) NOT NULL,
SectionNumber varchar(3),
CourseID varchar(9),
Enrollment smallint,
BNumber varchar(10),
PercentResp numeric(5,2),
CONSTRAINT CRN_Term PRIMARY KEY (CRN,Term),
FOREIGN KEY (CourseID) REFERENCES Course (CourseID),
FOREIGN KEY (BNumber) REFERENCES Instructor (BNumber)
);
INSERT INTO Instructor (BNumber, FirstName, LastName)
VALUES
('0000012345','Bill','Smith'),
('0000023456','Sue','Taylor'),
('0000034567','Skilar','Ramsey'),
('1234500000','Sam','Jones'),
('2345600000','Tyson','Quilez');
INSERT INTO Section (CRN, Term, SectionNumber, CourseID, Enrollment,
BNumber, PercentResp)
VALUES
(40035,'201040','02B','CHM2210', 31,'0000034567',100),
(40001,'201040','02B','CGS1000', 27,'0000012345',100),
(40002,'201040','70B','CGS2100', 25,'0000012345',100),
(40003,'201040','71B','CGS2100', 19,'0000012345',100),
(40004,'201040','01B','COP1000', 15,'0000012345',100),
(40030,'201040','01B','BSCC1005',30,'0000023456',100),
(40031,'201040','02B','BSCC1005',25,'0000023456',100),
(40032,'201040','70B','BSCC1005',24,'0000023456',100),
(40000,'201040','01B','CGS1000', 15,'0000012345',100),
(40034,'201040','01B','CHM2210', 27,'0000034567',100);
Replace
With
You should not update the primary key if it’s the matching condition in your update query.