违反外键约束
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了违反外键约束相关的知识,希望对你有一定的参考价值。
我遇到这个问题INSERT在表'PURCHASEDETAILS'上导致密钥(P0003)违反外键约束'SQL141108214008671'。声明已经回滚。我似乎无法找到问题所在。
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;
}
我不知道问题出在哪里。每当我进入,这个错误就出来了。
答案
您似乎正在尝试在Purchase表中插入引用“P0003”的记录,但该表不包含具有该键值的记录。因此,你得到一个外键约束。
首先需要插入缺少的购买记录,或者将输入中使用的值更改为在购买表中找到的参考值。
以上是关于违反外键约束的主要内容,如果未能解决你的问题,请参考以下文章
Laravel 迁移 - 违反完整性约束:1452 无法添加或更新子行:外键约束失败
为啥在尝试删除违反外键约束的记录时,Azure Web 应用程序的性能会受到如此大的影响?
ForeignKeyConstraintError [SequelizeForeignKeyConstraintError]:在表“头像”上插入或更新违反了外键约束