0

ERROR 1452 (23000) at line 15: Cannot add or update a child row: a foreign key constraint fails (cjcarey.products, CONSTRAINT products_ibfk_2 FOREIGN KEY (sName) REFERENCES suppliers (suppliersname))

-- Add products information

insert into Products values(1, 'Bike', 3, 200.00, 'Vehicle', 'Walmart', 180.00, 5, '2017-12-09');
insert into Product values(2, 'Couch', 12, 450.00, 'Household', 'Homegoods', 300.00, 3, '2018-05-14');

-- Add suppliers information

insert into Suppliers values('Walmart', '329 Carver Rd');
insert into Suppliers values('Homegoods', '4561 Parks Dr');
insert into Suppliers values('Hyvee', '2349 Morril St');
insert into Suppliers values('Lowes', '8828 WallyWorld Ave');

Suppliers table:

create table Suppliers(
SuppliersName varchar(255) not null,
address varchar(255),
primary key (SuppliersName)
);

Product table:

create table Products(
itemID int not null,
ProductName varchar(255),
RemainingQTY int,
Price float,
ProdType varchar(255) not null,
sName varchar(255) not null,
PurchasePrice float,
QTY int,
DateOfPurchase date,
primary key (itemID),
foreign key (ProdType) references ProductTypes(TypeName),
foreign key (sName) references Suppliers(SuppliersName)
);

I am not entirely sure what this error means? I think I referenced my foreign keys correctly..

jarlh
  • 42,561
  • 8
  • 45
  • 63
  • No SuppliersID column in Suppliers? – jarlh Sep 18 '18 at 13:44
  • 1
    Works fine for me if you insert to suppliers before you attempt to insert to products. – P.Salmon Sep 18 '18 at 13:50
  • "Works fine for me if you insert to suppliers before you attempt to insert to products" that is questionable @P.Salmon i would change the insert queries to a more ANSI SQL insert complaint query like this.. `insert into Suppliers (SuppliersName. address ) values ('Walmart', '329 Carver Rd');` makes the inserts less error prone in cases the table column order changes or columns are added. – Raymond Nijland Sep 18 '18 at 13:55

0 Answers0