SQL数据库外键问题(INSERT语句与FOREIGN KEY约束冲突“FK__Orders__Employee__0F624AF8”

时间:2017-05-21 13:05:24

标签: mysql sql-server database

任何帮助都会非常感激,因为这是我的第一个sql项目。我以正确的顺序创建了表,但是当我尝试将数据插入orders表时,它会抛出错误。插入订单时请查看最后一张表。谢谢

CREATE TABLE Suppliers (
SupplierId int IDENTITY (1,1) NOT NULL,
CompanyName nvarchar(50) NOT NULL,
ContactName nvarchar(50) NOT NULL,
City  nvarchar(20) NOT NULL,
Phone nvarchar(20) NOT NULL,
Homepage ntext NOT NULL
CONSTRAINT PK_Suppliers PRIMARY KEY (SupplierId)

 )  
  INSERT INTO Suppliers
(CompanyName,ContactName,City,Phone,Homepage)
VALUES
('GardenWorld','JimL','Dublin','085-1588902','Gardenworld.ie'),
('HomeFixLtd','AnneG','Cork','087-1584402','HomeFix.ie'),
('PleasureDome','PaulH','Dublin','086-3248902','PleasureDome.ie'),
('BuildBase','GarthN','Belfast','004485-1588902','BuildBase.co.uk'),
('RenewTools','HarryH','Galway','087-4567835','RenewTools.ie')

CREATE TABLE Products
(
  ProductID int IDENTITY(1,1) NOT NULL PRIMARY KEY,
  ProductName nvarchar(50) NOT NULL,
  SupplierId int NOT NULL,
  Category   nvarchar(50) NOT NULL,
 UnitPrice money NOT NULL,
 UnitsInStock smallint NOT NULL,
  UnitsOnOrder smallint NOT NULL,
  ReOrderLevel smallint NOT NULL,
 FOREIGN KEY (SupplierId) REFERENCES Suppliers (SupplierId)

)
 INSERT INTO dbo.Products
    (ProductName,SupplierId,Category,UnitPrice,UnitsInStock,UnitsOnOrder,ReOrderLeve   l)
VALUES

('Fork Handles','1','Garden','10.00','125','0','25'),
 ('Hammer','2','DIY','3.00','129','0','40'),
('Bucket','4', 'Garden','5.00','25','30','30'),
('Solar Battery','2','DIY','15.00','112','0','35'), 
('Lawnmower','1','Garden','150.00','24','8','25'),
('Kitchen Sink','4','DIY','80.00','115','0','30'),
('Cement Bag','4','DIY','10.00','130','0','25'),
('Compost Bag','1','Garden','5.00','135','0','20'),
('Chainsaw','1','Garden','100.00','11','15','15'),
('Sun Lounger','3','Leisure','30.00','4','30','10'),
('Light Bulb','4','DIY','5.00','17','50','35'),
('Paint Set','5','DIY','10.00','25','0','30'),
('Ladder','2','DIY','28.00','5','20','10'),
 ('Wheel Barrow','1','Garden','50.00','125','0','24'),
 ('Dinghy','3','Leisure','100.00','4','20','10')

    CREATE TABLE Customers(
CustomerId int IDENTITY (1,1) NOT NULL,
FirstName nvarchar(50) NOT NULL,
LastName nvarchar(50) NOT NULL,
CompanyName nvarchar(40) NOT NULL,
City nvarchar(20) NOT NULL,
Country nvarchar(20) NOT NULL,
Phone nvarchar(25) NOT NULL,
Email nvarchar(25) NOT NULL
   CONSTRAINT PK_Customers PRIMARY KEY (CustomerId))

  INSERT INTO Customers

   (FirstName,LastName,CompanyName,City,Country,Phone,Email)

  VALUES

  ('Edgar','Codd','n/a','Galway','Eire','085-1233456','ec@yahoo.com'),
  ('John','Byrne','JB Landscapes','Limerick','Eire','086-2347656','jc@gmail.com'),
  ('Jane','Ryan','n/a','Dublin','Eire','087-1569846','jr@live.com'),
  ('Bill','Bates','n/a','Cork','Eire','085-1245356','bb@yahoo.com'),
  ('Ridley','Scott','RS Carpentry','Galway','Eire','085-8933456','rs@yahoo.com'),
 ('Anne','Smith','n/a','Dublin','Eire','087-7988456','as@yahoo.com'),
  ('Paul','Jones','PJ Plumbers','Belfast','UK','085-          6723456','PJ@yahoo.com'),
  ('James','Ryan','JR Bros','London','UK','085-6723456','jr@yahoo.com'),
 ('Jim','Reilly','n/a','Cork','Eire','085-6888456','jj@yahoo.com'),
 ('Emily','Rigby','n/a','London','UK','085-6778946','er@gmail.com')

  CREATE TABLE Employees(
EmployeeId int IDENTITY (1,1) PRIMARY KEY NOT NULL,
FirstName nvarchar(50) NOT NULL,
LastName nvarchar(50) NOT NULL,
Department nvarchar(20) NOT NULL,
City nvarchar(20) NOT NULL,
Age smallint NOT NULL,
Salary int NOT NULL,
Commission_Pct decimal(2,2) NOT NULL
  )

   INSERT INTO Employees
   (FirstName,LastName,Department,City,Age,Salary,Commission_Pct)
  VALUES
  ('John','Cusack','Garden','Dublin','44','30000','0.00'),
  ('Todd','Louiso','DIY','Dublin','32','25000','0.05'),
   ('Jack','Black','Garden','Galway','22','22000','0.20'),
   ('Joan','Barrett','DIY','Dublin', '34','30000','0.05'),
   ('Barry','Behan','DIY', 'Belfast','55','35000','0.00')


 CREATE TABLE Orders
 (
  OrderID int IDENTITY(1,1)NOT NULL PRIMARY KEY,
  CustomerID int NOT NULL,
  ProductID int NOT NULL,
  UnitPrice money NOT NULL,
  Quantity smallint NOT NULL,   
  EmployeeID int NOT NULL,
  OrderDate date NOT NULL,
  DispatchDate date NOT NULL,  
  FOREIGN KEY (ProductID) REFERENCES Products(ProductID),
  FOREIGN KEY(CustomerID) REFERENCES Customers(CustomerId),
  FOREIGN KEY(EmployeeID) REFERENCES Employees(EmployeeId)
 )

以下是INSERT

上的问题Orders
  INSERT INTO Orders
(CustomerID,ProductID,UnitPrice,Quantity,EmployeeID,OrderDate,DispatchDate)
 VALUES
('2','1','3.00','5','3','2017-05-19','2017-05-23'),
('3','4','15.00','2','4','2017-05-10','2017-05-19'),
('7','3','5.00','10','4','2017-05-14','2017-05-17'),
 ('5','9','100.00','3','8','2017-05-05','2017-05-11'),
('6','10','30.00','4','9','2017-05-05','2017-05-09'),
('1','8','5.00','10','4','2017-05-05','2017-05-07'),
('3','11','5.00','15','5','2017-05-11','2017-05-17'),
('7','13','28.00','3','2','2017-05-15','2017-05-19'),
('4','6','80.00','10','5','2017-05-15','2017-05-19'),
('10','5','150.00','2','1','2017-05-15','2017-05-18')

这会产生以下错误。

The INSERT statement conflicted with the FOREIGN KEY constraint "FK__Orders__Employee__0F624AF8". The conflict occurred in database "Billy Joes Hardware/Assignment", table "dbo.Employees", column 'EmployeeId'. The statement has been terminated.

2 个答案:

答案 0 :(得分:1)

看起来你实际上有2行会违反约束。

('5','9','100.00','3','8','2017-05-05','2017-05-11'),
('6','10','30.00','4','9','2017-05-05','2017-05-09')

如果你运行SELECT EmployeeId FROM Employees,你就会得到这个。

EmployeeId
-----------
1
2
3
4
5

或者您可以运行SELECT MAX(EmployeeId) FROM Employees并获得 5 作为结果。

您收到错误是因为插入的第4行EmployeeId 8 9 Employees 插入的第5行。由于8表中只有5行,9EmployeeId无效EmployeeId。您需要更改插入中的Employees或在locate表格中插入更多行

我创建了这张图表。这将显示您的外键关系。这可以帮助您查看另一个表中需要哪个有效值。

Database Diagram

答案 1 :(得分:0)

在您输入EmployeeId的表订单中,检查第5个值是9:

('6','10','30.00','4','9','2017-05-05','2017-05-09'),

你在Employees表上只插入了5条记录,我非常确定你的最大ID是5,你不能使用Employeeid = 9作为Orders表,因为员工不存在;并且您对Orders表有这个限制:FOREIGN KEY(EmployeeID) REFERENCES Employees(EmployeeId)

<强>解决方案

在Orders中插入之前,首先检查Employees表上可用的EmployeesId。要检查此项,请使用SELECT * FROM EMPLOYEES

(不推荐)如果您想要无限制地插入任何EmployeesId值,那么您应该删除此行:FOREIGN KEY(EmployeeID) REFERENCES Employees(EmployeeId)

希望这有帮助