Reputation: 5
enter image description hereI have created these tables in phpmyadmin
Payment table
DROP TABLE IF EXISTS Payment;
CREATE TABLE IF NOT EXISTS Payment (
pay_id int(5) NOT NULL,
number int(25) default NULL,
amount decimal(20,2) default NULL,
CONSTRAINT Payment_pk_payid
PRIMARY KEY (pay_id) )
ENGINE=InnoDB DEFAULT CHARSET=utf8;
Policy_payment table
DROP TABLE IF EXISTS Policy_payment;
CREATE TABLE IF NOT EXISTS Policy_payment(
id int(5) NOT NULL,
policy_id int(5) NOT NULL,
pay_id int(5) NOT NULL,
date date default NULL,
CONSTRAINT Policy_payment_pk_id_policyid_payid
PRIMARY KEY (id,policy_id,pay_id),
CONSTRAINT Policy_payment_fk_policyid
FOREIGN KEY (policy_id)
REFERENCES Policy (policy_id),
CONSTRAINT Policy_payment_fk_payid
FOREIGN KEY (pay_id)
REFERENCES Payment (pay_id) )
ENGINE=InnoDB DEFAULT CHARSET=utf8;
So when i try to insert the the values in Policy_payment table, it gives a #1452 error. What am i doing wrong?
--Dumping data for table Policy_payment
INSERT INTO Policy_payment (
id, policy_id, pay_id, date)
VALUES
('70111', '88881', '20001', '2019-01-10'),
('70112', '88882', '20002', '2019-09-25'),
('70113', '88883', '20003', '2019-04-18'),
('70114', '88884', '20004', '2020-11-11'),
('70115', '88885', '20005', '2020-06-23'),
('70116', '88886', '20006', '2021-12-11'),
('70117', '88887', '20007', '2021-08-20'),
('70118', '88888', '20008', '2018-03-04'),
('70119', '88889', '20009', '2018-03-20'),
('70110', '88810', '20010', '2016-02-09');
this is the error
#1452 - Cannot add or update a child row: a foreign key constraint fails (S11185754
.Policy_payment
, CONSTRAINT Policy_payment_fk_payid
FOREIGN KEY (pay_id
) REFERENCES Payment
(pay_id
))
Thanks
Upvotes: -1
Views: 112
Reputation: 9818
You are trying to insert a record with a pay_id that doesn't match any record in the payment table i.e. exactly what the error message says.
Upvotes: 0