错误#1452,外键约束失败

时间:2019-06-22 11:05:39

标签: mysql sql

我收到以下SQL错误,请您告诉我此语句有什么问题?

 Sub Main() ' Recursive Version
    Try
        Dim stdin As Stream = Console.OpenStandardInput()
        Dim stdout As Stream = Console.OpenStandardOutput()

        Dim MsgLength As Integer = 0
        Dim InputData As String = ""
        Dim LenBytes As Byte() = New Byte(3) {}
        stdin.Read(LenBytes, 0, 4)
        MsgLength = BitConverter.ToInt32(LenBytes, 0)
        Dim Buffer As Byte() = New Byte(MsgLength - 1) {}
        stdin.Read(Buffer, 0, Buffer.Length)
        InputData = Encoding.UTF8.GetString(Buffer)

        Dim Str As String = Newtonsoft.Json.JsonConvert.SerializeObject("[" & Now & "] Arrrr!")
        Dim buff() As Byte = Encoding.UTF8.GetBytes(Str)
        stdout.Write(BitConverter.GetBytes(buff.Length), 0, 4)
        stdout.Write(buff, 0, buff.Length)
        Main()
    Catch ex As Exception
        MsgBox(ex.Message, MsgBoxStyle.Critical, "Oh, Snap!")
    End Try
End Sub

MySQL答案:

  

#1452-无法添加或更新子行:外键约束   失败(ALTER TABLE `patients` ADD FOREIGN KEY (`ID_Medecin`) REFERENCES `medecin`(`ID`) ON DELETE RESTRICT ON UPDATE RESTRICT; dentaire,约束#sql-744_723   外键(#sql-744_723_ibfk_1)参考ID_Medecinmedecin))

1 个答案:

答案 0 :(得分:1)

欢迎s_Akashi

此网站适用于讲英语的人,因此请用英语问您的问题。我不确定目前是否有会讲法语的stackoverflow网站,但是如果这些语言更适合您,则肯定会有西班牙语和葡萄牙语(es.stackoverflow.com和pt.stackoverflow.com)。我确实调整了问题,如果它通过审查,现在应该可以了。

回答您的问题:您尝试在外键Patient.ID_Medecin和medecin.ID之间添加关系,但是,患者表中至少有一个条目具有不存在的ID_Medecin,需要更正首先。

您可以通过LEFT OUTER JOIN查询找到它们(我通常使用Microsoft SQL Server,因此不确定此语法是否100%正确):

SELECT * FROM patients LEFT OUTER JOIN medecin ON patients.ID_Medecin = medecin.ID WHERE medecin.ID IS NULL