我遇到了这个问题INSERT on table' PURCHASEDETAILS'导致违反外键约束' SQL141108214008671' SQL141108214008671'用于键(P0003)。声明已经回滚。 我似乎无法找到问题。
CREATE TABLE STOCKS (
stockID VARCHAR(5) not null,
stockName VARCHAR(50),
stockPrice DOUBLE,
stockDescrp VARCHAR(100),
stockQty INTEGER,
PRIMARY KEY (stockID)
);
INSERT INTO STOCKS VALUES ('S0001', 'Vitamin C For Kids', 12.90, 'Vitamin C for kids under age of 8.', 100);
INSERT INTO STOCKS VALUES ('S0002', 'Syntha-6 Whey Protein', 180.00,'Ultra lean muscle protein for healthy adults over 18 years old.', 100);
INSERT INTO STOCKS VALUES ('S0003', 'Serious Mass', 190.00,'Extra calories that allows anyone to increase mass efficiently.', 100);
INSERT INTO STOCKS VALUES ('S0004', 'Vitamin C Elite', 15.50, 'Vitamin C for everyone.', 100);
CREATE TABLE PURCHASE (
purchaseID VARCHAR(5) not null,
purchaseDate DATE,
memberID VARCHAR(5),
PRIMARY KEY (purchaseID),
FOREIGN KEY (memberID) REFERENCES MEMBERS(memberID)
);
INSERT INTO PURCHASE VALUES ('P0001','2014-10-23','S0001');
INSERT INTO PURCHASE VALUES ('P0002','2014-10-23','G0001');
CREATE TABLE PURCHASEDETAILS (
purchaseDetailID VARCHAR(7) not null,
purchaseID VARCHAR(5),
stockID VARCHAR(5),
OrderQty INTEGER,
PRIMARY KEY (purchaseDetailID),
FOREIGN KEY (purchaseID) REFERENCES PURCHASE(purchaseID),
FOREIGN KEY (stockID) REFERENCES STOCKS(stockID)
);
INSERT INTO PURCHASEDETAILS VALUES ('PD00001', 'P0001', 'S0001', 4);
INSERT INTO PURCHASEDETAILS VALUES ('PD00002', 'P0001', 'S0002', 10);
INSERT INTO PURCHASEDETAILS VALUES ('PD00003', 'P0002', 'S0003', 5);
这里是插入记录的DA部分:
public void insertRecord(Purchase purc){
String queryStr = "INSERT INTO "+ tableName +" VALUES (?,?,?,?)";
try{
stmt = conn.prepareStatement(queryStr);
stmt.setString(1,purc.getPurchaseDetailsID());
stmt.setString(2,purc.getPurchaseID());
stmt.setString(3, purc.getStockID());
stmt.setInt(4, purc.getQuantity());
stmt.executeUpdate();
}
catch (SQLException ex){
JOptionPane.showMessageDialog(null,ex.getMessage(),"ERROR",JOptionPane.ERROR_MESSAGE);
}
}
这是UI部分:
if(e.getSource() == jbtCreate)
{
String stockID = jTF.getText();
String quantity = jTF2.getText();
Purchase purc = new Purchase();
if(stockID.isEmpty() || quantity.isEmpty())
{
JOptionPane.showMessageDialog(null,"Fields must not be empty","ERROR",JOptionPane.ERROR_MESSAGE);
}
else{
String pID = PID();
count = count +1;
String pDID = PDID(count);
int q = Integer.parseInt(jTF2.getText());
purc.setQuantity(q);
String sID= jTF.getText();
int quantityC = Integer.parseInt(jTF2.getText());
String priceStr = purcControl.selectPrice(sID);
double realPrice = Double.parseDouble(priceStr);
purc.setPurchaseDetailsID(pDID);
purc.setPrice(realPrice);
purc.setPurchaseID(pID);
purc.setQuantity(quantityC);
purc.setStockID(sID);
double price = purc.getPrice();
tableModel.addRow(new Object[]{pDID,sID,price,quantityC});
purcControl.addRecord(purc);
}
}
控制:
public void addRecord(Purchase purc){
purcDA.insertRecord(purc);
}
域:
public Purchase(String purchaseDetailsID,String purchaseID,String stockID,double price,int quantity)
{
this.purchaseDetailsID = purchaseDetailsID;
this.purchaseID = purchaseID;
this.stockID = stockID;
this.price = price;
this.quantity = quantity;
counter ++;
}
public String getPurchaseID(){
return purchaseID;
}
public void setPurchaseID(String u){
this.purchaseID = u;
}
public int getCounter(){
return counter;
}
public String getPurchaseDetailsID(){
return purchaseDetailsID ;
}
public double getPrice(){
return price;
}
public String getStockID(){
return stockID;
}
public int getQuantity(){
return quantity;
}
public void setPurchaseDetailsID(String r){
this.purchaseDetailsID = r ;
}
public void setPrice(double p){
this.price = p;
}
public void setStockID(String s){
this.stockID = s;
}
public void setQuantity(int q){
this.quantity = q;
}
我不知道问题出在哪里。每当我进入时,这个错误就出来了。
答案 0 :(得分:1)
您似乎正在尝试插入引用' P0003'在Purchase表中,但该表不包含具有该键值的记录。因此你得到一个外键约束。
首先需要插入缺少的购买记录,或者将您在输入中使用的值更改为在购买表中找到的参考值。