create table Interventions
(
InterventionID BIGINT IDENTITY (1,1) PRIMARY KEY NOT NULL,
InterventionCustomerID BIGINT FOREIGN KEY REFERENCES Customer(CustomerID) NOT NULL,
InterventionMalfunctionDescription NVARCHAR(MAX) NOT NULL,
CustomerID BIGINT FOREIGN KEY REFERENCES Customer(CustomerID),
EmployeeID INT FOREIGN KEY REFERENCES Employees(EmployeeID),
TypeOfEquipementID INT FOREIGN KEY REFERENCES TypeOfEquipements(TypeOfEquipementID),
StatusID INT FOREIGN KEY REFERENCES TypeOfStatus(StatusID),
SerialNumber VARCHAR(255) FOREIGN KEY REFERENCES SerialNumbers(SerialNumber),
GroupID INT FOREIGN KEY REFERENCES TypeOfGroup(GroupID) NOT NULL,
InterventionCreateDate DATETIME NOT NULL,
InterventionStartDate DATETIME,
InterventionFinnishDate DATETIME,
InterventionArchiveDate DATETIME
);
create table Customer
(
CustomerID BIGINT IDENTITY(1,1) PRIMARY KEY NOT NULL,
OrganizationName VARCHAR(255) NOT NULL,
Telephone VARCHAR(255),
MobileTelephone VARCHAR(255) NOT NULL,
CustomerAdressLine1 VARCHAR(255) NOT NULL,
CustomerAdressLine2 VARCHAR(255),
PostalCode VARCHAR(255) NOT NULL,
FederalTaxID VARCHAR(255) UNIQUE NOT NULL,
EmailAddress VARCHAR(255) UNIQUE NOT NULL,
SageCustomerID BIGINT UNIQUE
);
INSERT INTO Interventions(InterventionCustomerID, InterventionMalfunctionDescription, CustomerID, EmployeeID, TypeOfEquipementID, StatusID, SerialNumber, GroupID, InterventionCreateDate, InterventionStartDate, InterventionFinnishDate, InterventionArchiveDate) VALUES
('1', 'abc', '4', '4', '1', '1', 'SerialNumber1', '1', '2019-01-01', '2019-01-02', '2019-01-03', '2019-01-04'),
('2', 'abc', '5', '5', '2', '2', 'SerialNumber2', '2', '2019-02-01', '2019-02-02', '2019-02-03', '2019-02-03'),
('3', 'abc', '6', '6', '3', '3', 'SerialNumber3', '3', '2019-03-01', '2019-03-02', '2019-03-03', '2019-03-04');
Select * from Customer;
Erro:
INSERT语句与FOREIGN KEY约束“FK__Intervent__Inter__6C190EBB”冲突。冲突发生在数据库“ADSGLOBAL”,表“dbo.Customer”,列'CustomerID'。
答案 0 :(得分:2)
您对InterventionCustomerID
的约束表明它引用了Customer(CustomerID)
。
您的插入声明表示您尝试将值1
,2
,3
插入该列。
您的SELECT * FROM customer
表示该表中不存在客户1
,2
,3
。
在1
表中创建客户之前,您无法为客户2
,3
,Customer
插入记录。