mysql 23000,MySQL错误1452(23000):无法添加或更新子行

I need a help with this problem. I don't understand how to resolve this problem. I will be happy if you find out why this problem happens. Here is my SQL:

Create database Order_Purchase;

Use Order_Purchase;

Create table Customer (

customerID int(10),

cname varchar(30),

city varchar(30),

Constraint customer_PK Primary Key (customerID))

Engine = innodb;

Create table Item (

itemID int(10),

description varchar(30),

price double,

Constraint item_PK Primary Key (itemID))

Engine = innodb;

Create table Order_ (

orderID int(10),

customerID int(10),

orderDate date,

Constraint order_PK Primary Key (orderID),

Constraint order_FK1 Foreign Key (customerID) references Customer(customerID))

Engine = innodb;

Create table Order_Item (

orderID int(10) null default null,

itemID int(10) null default null,

quantity int,

Constraint order_item_PK Primary Key (orderID, itemID),

Constraint order_item_FK1 Foreign Key (orderID) references Order_(orderID),

Constraint order_item_FK2 Foreign Key (itemID) references Item(itemID))

Engine = innodb;

Create table Warehouse (

warehouseID int(10),

city varchar(30),

Constraint warehouse_PK Primary Key (warehouseID))

Engine = innodb;

Create table Shipment (

orderID int(10),

warehouseID int(10),

shipDate date,

Constraint shipment_PK Primary Key (orderID, warehouseID),

Constraint shipment_FK1 Foreign Key (orderID) references Order_(orderID),

Constraint shipment_FK2 Foreign Key (warehouseID) references Warehouse(warehouseID))

ENGINE = innodb;

My code for inserting:

Insert into Order_Item (orderID, itemID, quantity) values (10000, 105472, 1);

Insert into Order_Item (orderID, itemID, quantity) values (10001, 105472, 4);

Insert into Order_Item (orderID, itemID, quantity) values (10002, 104375, 1);

Insert into Order_Item (orderID, itemID, quantity) values (10003, 100870, 2);

Insert into Order_Item (orderID, itemID, quantity) values (10003, 103798, 1);

Insert into Order_Item (orderID, itemID, quantity) values (10004, 103798, 1);

Insert into Order_Item (orderID, itemID, quantity) values (10004, 105472, 2);

Insert into Order_Item (orderID, itemID, quantity) values (10005, 100475, 1);

Insert into Order_Item (orderID, itemID, quantity) values (10006, 100870, 1);

Insert into Order_Item (orderID, itemID, quantity) values (10006, 105472, 1);

Insert into Order_Item (orderID, itemID, quantity) values (10006, 103798, 1);

The result:

mysql> Insert into Order_Item (orderID, itemID, quantity) values (10000, 105472, 1);

Query OK, 1 row affected (0.05 sec)

mysql> Insert into Order_Item (orderID, itemID, quantity) values (10001, 105472, 4);

Query OK, 1 row affected (0.01 sec)

mysql> Insert into Order_Item (orderID, itemID, quantity) values (10002, 104375, 1);

Query OK, 1 row affected (0.01 sec)

mysql> Insert into Order_Item (orderID, itemID, quantity) values (10003, 100870, 2);

Query OK, 1 row affected (0.00 sec)

mysql> Insert into Order_Item (orderID, itemID, quantity) values (10003, 103798, 1);

ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`order_purchase`.`order_item`, CONSTRAINT `o

rder_item_FK2` FOREIGN KEY (`itemID`) REFERENCES `item` (`itemID`))

mysql> Insert into Order_Item (orderID, itemID, quantity) values (10004, 103798, 1);

ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`order_purchase`.`order_item`, CONSTRAINT `o

rder_item_FK2` FOREIGN KEY (`itemID`) REFERENCES `item` (`itemID`))

mysql> Insert into Order_Item (orderID, itemID, quantity) values (10004, 105472, 2);

Query OK, 1 row affected (0.01 sec)

解决方案

As the error tells you, you need to first insert values in the item table. Run the following insert statement before the failing one:

INSERT INTO Item(itemID, description) values(103798, 'This was the missing item');

Now, you should be able to insert the intended row into the Order_Item table.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值