-1

I removed the foreign key constraints to add data in, then I later altered the tables to add the foreign keys back in. However, with one particular table thats being altered, I keep getting an 1452 error code back.

Data

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);

Adding Foreign Key

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

Construction of Invoice

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)
);

Construction of Medicine

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)
);
  • Yeah you were right. The data inputed for 'Invoice' didn't match the 'Medicine ID' –  Oct 21 '14 at 03:57

1 Answers1

1

I believe that error means that there is a row in the Invoice table that has a Medicine_ID value that does not exist in the Medicine table.

To figure out which values from Invoice do not exist in the Medicine table, try this query:

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

You'll have to either add the corresponding rows to the Medicine table, or remove these rows from the Invoice table.

Travesty3
  • 14,351
  • 6
  • 61
  • 98