我为客户和订单创建了如下表格:
mysql> create table customers (
-> customer_id int PRIMARY KEY,
-> lastname varchar(30),
-> firstname varchar(30),
-> address varchar(100),
-> phone_num char(12));
mysql> create table orders (
-> order_id int primary key,
-> date_placed date,
-> delivery_date date,
-> total_cost float,
-> paid boolean,
-> customer_id int,
-> foreign key (customer_id) references orders(customer_id));
我创建了一个名为 Joseph Smith 的客户,如下所示:
insert into customers values(1, 'Smith', 'Joseph', '1 Grey Street', '800-867-5309');
我想将 Joseph Smith 的订单插入到“订单”表中,如下所示:
insert into orders values(1, '01-24-1990', '01-26-1990', true, 1);
但是,我收到以下错误消息:
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`mr3bn/orders`, CONSTRAINT `orders_ibfk_1` FOREIGN KEY (`customer_id`) REFERENCES `orders` (`customer_id`))
希望澄清一下
- 为什么这不起作用和
- 如何真正让它发挥作用。谢谢!