更新sql外键约束

时间:2012-04-24 14:33:32

标签: java sql

我有一个我正在处理的应用程序,它将查询数据库并显示结果。该程序还对数据库进行了更改(更新,删除,插入)。直到最近我做了一些更改,我才能正常使用这些功能。现在我得到一个SQLException,告诉我我违反了外键约束。我查了一下,发现违规是多个表共享数据的结果。有办法克服这个问题吗?如何在不违反约束的情况下进行更新?这是我的更新方法:

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); }

直到最近我做了一些更改,我才使用该方法没有任何问题。我不知道我做了什么,但现在我得到外键约束违反异常

这是例外:java.sql.SQLIntegrityConstraintViolationException:表'INSTRUCTOR'上的UPDATE导致密钥(1234500000)违反外键约束'SQL120408141918440'。声明已经回滚。

数据库信息

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);

2 个答案:

答案 0 :(得分:4)

替换

"UPDATE Instructor SET BNUMBER = ?, FIRSTNAME = ?, LASTNAME = ? WHERE BNUMBER = ?"

"UPDATE Instructor SET FIRSTNAME = ?, LASTNAME = ? WHERE BNUMBER = ?"

如果主键是更新查询中的匹配条件,则不应更新主键。

答案 1 :(得分:0)

您应该在引用

之后放置ON UPDATE CASCADE