Why this give me an error The INSERT statement conflicted with the FOREIGN KEY constraint "FK__products__Suppli__4D94879B". when e try to enter this INSERT INTO products (ProductName,ProductPrice,SupplierID) VALUES ('apple', 1.50, 7473), ('milk', 1.30, 4252), ('biscuits', 2.20, 5352), ('bread', 1.20, 5425), ('cheese', 2.50, 45728);
Why this give me an error
The INSERT statement conflicted with the FOREIGN KEY constraint "FK__products__Suppli__4D94879B".
when e try to enter this
INSERT INTO products (ProductName,ProductPrice,SupplierID)
VALUES ('apple', 1.50, 7473),
('milk', 1.30, 4252),
('biscuits', 2.20, 5352),
('bread', 1.20, 5425),
('cheese', 2.50, 45728);
The fool code:
1. CREATE
2. USE company;
zad 1.
CREATE TABLE employees (
id int identity not null ,
EmpFirstName nvarchar(255),
EmplastName nvarchar(255),
PRIMARY KEY (id),
);
CREATE TABLE suppliers (
id int identity not null ,
CompanyName nvarchar(50),
CompanyAddress nvarchar(255),
CompanyPhone bigint not null,
PRIMARY KEY (id),
);
CREATE TABLE products (
id int identity not null,
ProductName nvarchar(50),
Productprice float,
SupplierID int not null,
PRIMARY KEY (id),
FOREIGN KEY (SupplierID) REFERENCES suppliers(id)
);
CREATE TABLE orders (
EmployeeId int not null,
ProductId int not null,
OrderDate date,
Quantity int not null,
PRIMARY KEY (EmployeeId,ProductId,OrderDate),
FOREIGN KEY (EmployeeId) REFERENCES employees(id),
FOREIGN KEY (ProductId) REFERENCES products(id),
);
za zad.2.
ALTER TABLE employees ADD EmployeeAddress nvarchar(255)
za zad.3.
INSERT INTO employees(EmpFirstName,EmplastName,EmployeeAddress)
VALUES ('Иван','Иванов','Хаджи Димитър 25'),
('Милена','Димитрова','Панайот Хитов 3'),
('Миглена','Панайотова', 'Васил Априлов' ),
('Васил','Трайков','Иван Вазов бл 4'),
('Димитринка', 'Станчева','Стефан Стамболов');
INSERT INTO orders (EmployeeId,ProductId,OrderDate,Quantity)
VALUES (1,2,'09-08-2021',8),
(2,1,'10-03-2019',5),
(3,3,'05-04-2020', 3),
(4,5,'10.12.2021',10),
(10,12,'15.01.2021',4);
INSERT INTO products (ProductName,ProductPrice,SupplierID)
VALUES ('apple', 1.50, 7473),
('milk', 1.30, 4252),
('biscuits', 2.20, 5352),
('bread', 1.20, 5425),
('cheese', 2.50, 45728);
Step by step
Solved in 2 steps