Home  >  Q&A  >  body text

Error 1452: Unable to add or update child row: foreign key constraint failed

<p>我在 MySQL Workbench 中创建了表,如下所示:</p> <p><strong>ORDRE 表:</strong></p> <pre class="brush:php;toolbar:false;">CREATE TABLE Ordre ( OrdreID INT NOT NULL, OrdreDato DATE DEFAULT NULL, KundeID INT DEFAULT NULL, CONSTRAINT Ordre_pk PRIMARY KEY (OrdreID), CONSTRAINT Ordre_fk FOREIGN KEY (KundeID) REFERENCES Kunde (KundeID) ) ENGINE = InnoDB;</pre> <p><strong>产品表:</strong></p> <pre class="brush:php;toolbar:false;">CREATE TABLE Produkt ( ProduktID INT NOT NULL, ProduktBeskrivelse VARCHAR(100) DEFAULT NULL, ProduktFarge VARCHAR(20) DEFAULT NULL, Enhetpris INT DEFAULT NULL, CONSTRAINT Produkt_pk PRIMARY KEY (ProduktID) ) ENGINE = InnoDB;</pre> <p>和<strong>ORDRELINJE表:</strong></p> <pre class="brush:php;toolbar:false;">CREATE TABLE Ordrelinje ( Ordre INT NOT NULL, Produkt INT NOT NULL, AntallBestilt INT DEFAULT NULL, CONSTRAINT Ordrelinje_pk PRIMARY KEY (Ordre, Produkt), CONSTRAINT Ordrelinje_fk FOREIGN KEY (Ordre) REFERENCES Ordre (OrdreID), CONSTRAINT Ordrelinje_fk1 FOREIGN KEY (Produkt) REFERENCES Produkt (ProduktID) ) ENGINE = InnoDB;</pre> <p>因此,当我尝试将值插入 <code>ORDRELINJE</code> 表时,我得到:</p> <blockquote> <p>错误代码:1452。无法添加或更新子行:外键约束失败(<code>srdjank</code>.<code>Ordrelinje</code>,CONSTRAINT <code>Ordrelinje_fk</code> FOREIGN关键(<code>Ordre</code>)参考<code>Ordre</code>(<code>OrdreID</code>))</p> </blockquote> <p>我看过有关此主题的其他帖子,但没有运气。 我是否在监督某些事情或知道要做什么? </p>
P粉434996845P粉434996845447 days ago547

reply all(1)I'll reply

  • P粉154798196
  • Cancelreply