简体   繁体   English

我的 INSERT 语句与 FOREIGN KEY 约束冲突

[英]My INSERT statement conflicted with the FOREIGN KEY constraint

enter image description here在此处输入图像描述

Those are my diagrams - I create procedure这些是我的图表 - 我创建程序

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[hasta]
    @Ad nchar(50),
    @Soyadı nvarchar(100),
    @TcKimlik nchar(11), 
    @DogumTarihi varchar(8),
    @TelefonNo nvarchar(11) 
AS 
BEGIN 
    SET NOCOUNT ON;
   
    INSERT INTO [dbo].Hastalar ([Ad], [Soyadı], [TcKimlik], [DogumTarihi], [TelefonNo])
    VALUES (@Ad, @Soyadı, @TcKimlik, @DogumTarihi, @TelefonNo)
END

After I want add data in Hastalar table, I write在我想在 Hastalar 表中添加数据之后,我写

exec  hasta  'Emir','Yılmaz','35635993564','1995.11.19','05347331085'

but I get this error但我得到这个错误

Msg 547, Level 16, State 0, Procedure hasta, Line 13消息 547,第 16 级,State 0,过程 hasta,第 13 行
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Hastalar_Testler". INSERT 语句与 FOREIGN KEY 约束“FK_Hastalar_Testler”冲突。 The conflict occurred in database "hastane", table "dbo.Testler", column 'TestID'.冲突发生在数据库“hastane”、表“dbo.Testler”、列“TestID”中。 The statement has been terminated.该语句已终止。

What part of the error do you not understand?您不明白错误的哪一部分?

The message is saying that one of the columns -- "TestId" -- is referring to another table.该消息是说其中一个列 - “TestId” - 正在引用另一个表。 And the value being inserted is not already in that table.并且插入的值不在该表中。

You are not explicitly inserting a value for TestId , so that value is being set using a default constraint or trigger.您没有为TestId显式插入值,因此该值是使用默认约束或触发器设置的。 I would suggest that you explicit provide the value in the insert -- even if that value is NULL .我建议您在insert中明确提供值 - 即使该值是NULL

In your table [dbo].Hastalar, it has a foreign key reference to another table.在您的表 [dbo].Hastalar 中,它具有对另一个表的外键引用。 The way a FK works is it cannot have a value in that column that is not also in the primary key column of the referenced table. FK 的工作方式是它不能在该列中具有不在引用表的主键列中的值。

If you have SQL Server Management Studio, open it up and sp_help '[dbo].Hastalar'.如果您有 SQL Server Management Studio,请打开它并使用 sp_help '[dbo].Hastalar'。 See which column that FK is on, and which column of which table it references.查看 FK 在哪一列,以及它引用哪个表的哪一列。 You're inserting some bad data.您正在插入一些错误数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM