-1

我删除了外键约束以添加数据,然后我更改了表以重新添加外键。但是,随着一个特定的表被更改,我不断收到 1452 错误代码。

数据

INSERT INTO Invoice
values
(323, 'Head Injury Patient', 'Surgery and Bed Stay', 150.00, 918),
(543, 'Eye Injury Patient', 'Medicine and hourly eye drops', 50.00, 910),
(859, 'Leg Injury Patient', 'Surgery and Bed Stay', 100.00, 915);

添加外键

Alter table Invoice
Add Foreign Key(Medicine_ID) References Medicine(Medicine_ID);

发票的构造

Create table Invoice (
Invoice_ID INT,
Invoice_description varchar(150),
Treatment varchar (150) NOT NULL,
Hospital_Cost INT NOT NULL,
Medicine_ID INT,
Primary Key(Invoice_ID)
);

医药建设

Create table Medicine (
Medicine_ID INT,
Medicine_name varchar(150),
Medicine_cost INT NOT NULL,
Medicine_description Varchar (150),
Brand_name varchar(150),
Manufacturer varchar(150),
Generic_name varchar(150),
price_per_unit INT,
Quantity_in_stock INT,
Primary Key(Medicine_ID)
);
4

1 回答 1

1

我相信该错误意味着表中有一行具有InvoiceMedicine_ID中不存在的值Medicine

要找出表Invoice中不存在哪些值,请Medicine尝试以下查询:

SELECT
    i.*
FROM
    Invoice i
    LEFT JOIN Medicine m
        ON i.Medicine_ID = m.Medicine_ID
WHERE
    m.Medicine_ID IS NULL

您必须将相应的行添加到Medicine表中,或者从表中删除这些行Invoice

于 2014-10-21T03:55:02.003 回答