【发布时间】:2018-01-10 01:46:05
【问题描述】:
执行此删除代码时:
$update = "DELETE from `products` WHERE `product_code` = $id; ";
$updateResult = mysqli_query($link, $update) OR DIE("Update Query Error ".mysqli_error($link)."Query [".$update ."]");
它说一个错误:
更新查询错误 无法删除或更新父行:外键约束失败 (gibson_db.trans, CONSTRAINT prod_trans_fk FOREIGN KEY (product_code) REFERENCES products (product_code))查询[从products 删除product_code = 1; ]
这是我的相关表格:
CREATE TABLE IF NOT EXISTS `products` (
`product_code` int(4) NOT NULL AUTO_INCREMENT,
`product_name` varchar(2500) NOT NULL,
`description` varchar(1000) DEFAULT 'N/A',
`price` float NOT NULL DEFAULT '0',
`quantity` int(11) NOT NULL DEFAULT '0',
`product_type` int(4) NOT NULL,
`img_path` varchar(255) NOT NULL DEFAULT 'products/',
PRIMARY KEY (`product_code`),
KEY `prod_type_fk` (`product_type`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=37 ;
INSERT INTO `products` (`product_code`, `product_name`, `description`, `price`, `quantity`, `product_type`, `img_path`)
CREATE TABLE IF NOT EXISTS `producttype` (
`product_type` int(4) NOT NULL AUTO_INCREMENT,
`description` varchar(255) NOT NULL DEFAULT 'N/A',
PRIMARY KEY (`product_type`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;
INSERT INTO `producttype` (`product_type`, `description`)
ALTER TABLE `products`
ADD CONSTRAINT `prod_type_fk` FOREIGN KEY (`product_type`) REFERENCES `producttype` (`product_type`);
【问题讨论】:
-
什么是
prod_trans_fk? -
这是我另一个名为
trans.ALTER TABLEtrans 的表上的约束名称 ADD CONSTRAINTprod_trans_fkFOREIGN KEY (product_code) REFERENCESproducts(product_code),添加约束user_trans_fk外键(user_code)参考user_tbl(user_code);` -
检查我的答案,它会解决你的问题
-
@sumit,好的先生。我会尽力。谢谢!
-
这就是它出错的原因,因为当您删除
products时,trans表中的子记录会变成孤立的。您可以使用@sumit 的回答来解决您的问题。