可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I have created tables in MySQL Workbench as shown below :
ORDRE table:
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;
PRODUKT table:
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;
and ORDRELINJE table:
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;
so when I try to insert values into ORDRELINJE
table i get:
Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails (srdjank
.Ordrelinje
, CONSTRAINT Ordrelinje_fk
FOREIGN KEY (Ordre
) REFERENCES Ordre
(OrdreID
))
I\'ve seen the other posts on this topic, but no luck.
Am I overseeing something or any idea what to do?
回答1:
Taken from Using FOREIGN KEY Constraints
Foreign key relationships involve a parent table that holds the
central data values, and a child table with identical values pointing
back to its parent. The FOREIGN KEY clause is specified in the child
table.
It will reject any INSERT or UPDATE operation that attempts to create
a foreign key value in a child table if there is no a matching
candidate key value in the parent table.
So your error Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails
essentially means that, you are trying to add a row to your Ordrelinje
table for which no matching row (OrderID) is present in Ordre
table.
You must first insert the row to your Ordre
table.
回答2:
You are getting this constraint check because ordre table does not have reference order id provided in insert command.
To insert value in ordrelinje, you first have to enter value in ordre table and use same ordreID in orderlinje table.
Or you can remove not null constraint and insert a NULL value in it.
回答3:
You must delete data in the child table which does not have any corresponding foreign key value to the parent table primary key .Or delete all data from the child table then insert new data having the same foreign key value as the primary key in the parent table . That should work
回答4:
This error generally occurs because we have some values in the referencing field of the child table, which do not exist in the referenced/candidate field of the parent table.
Sometimes, we may get this error when we are applying Foreign Key constraints to existing table(s), having data in them already. Some of the other answers are suggesting to delete the data completely from child table, and then apply the constraint. However, this is not an option when we already have working/production data in the child table. In most scenarios, we will need to update the data in the child table (instead of deleting them).
Now, we can utilize Left Join
to find all those rows in the child table, which does not have matching values in the parent table. Following query (missing from other answers), would be helpful to fetch those non-matching rows:
SELECT child_table.*
FROM child_table
LEFT JOIN parent_table
ON parent_table.referenced_column = child_table.referencing_column
WHERE parent_table.referenced_column IS NULL
Now, you can generally do one (or more) of the following steps to fix the data.
- Based on your \"business logic\", you will need to update/match these unmatching value(s), with the existing values in the parent table. You may sometimes need to set them
null
as well.
- Delete these rows having unmatching values.
- Add new rows in your parent table, corresponding to the unmatching values in the child table.
Once the data is fixed, we can apply the Foreign key constraint using ALTER TABLE
syntax.
回答5:
While inserting the foreign key attribute values, first verify the attributes type, as well as primary key attribute value in the parent relation, if the values in parent relation matches, then you can easily insert/update child attribute values.
回答6:
Your ORDRELINJE
table is linked with ORDER
table using a foreign key constraint constraint Ordrelinje_fk foreign key(Ordre) references Ordre(OrdreID)
according to which Ordre int NOT NULL,
column of table ORDRELINJE
must match any Ordre int NOT NULL,
column of ORDER
table.
Now what is happening here is, when you are inserting new row into ORDRELINJE
table, according to fk constraint Ordrelinje_fk
it is checking ORDER
table if the OrdreID
is present or not and as it is not matching with any OrderId, Compiler is complaining for foreign key violation. This is the reason you are getting this error.
Foreign key is the primary key of the other table which you use in any table to link between both. This key is bound by the foreign key constraint which you specify while creating the table. Any operation on the data must not violate this constraint. Violation of this constraint may result in errors like this.
Hope I made it clear.
回答7:
you should insert at least one raw in each tables (the ones you want the foreign keys pointing at) then you can insert or update the values of the foreign keys
回答8:
you should add data from REFERENCES KEY from PRIMARY TABLE to FOREIGN KEY in CHILD TABLE
it means do not add random data to foreign key just use data from primary key
description of data in foreign key