MySQL kann keine Fremdschlüsseleinschränkung erstellen

Ich habe einige Probleme beim Erstellen eines Fremdschlüssels für eine vorhandene Tabelle in einer MySQL-Datenbank.

Ich habe den tischexp:

+-------------+------------------+------+-----+---------+-------+
| Field       | Type             | Null | Key | Default | Extra |
+-------------+------------------+------+-----+---------+-------+
| EID         | varchar(45)      | NO   | PRI | NULL    |       |
| Comment     | text             | YES  |     | NULL    |       |
| Initials    | varchar(255)     | NO   |     | NULL    |       |
| ExpDate     | date             | NO   |     | NULL    |       |
| InsertDate  | date             | NO   |     | NULL    |       |
| inserted_by | int(11) unsigned | YES  | MUL | NULL    |       |
+-------------+------------------+------+-----+---------+-------+

und ich möchte keine neue Tabelle mit dem Namen erstellensample_df Verwenden Sie dazu die folgenden Befehle:

CREATE TABLE sample_df (
df_id mediumint(5) unsigned AUTO_INCREMENT primary key,
sample_type mediumint(5) unsigned NOT NULL,
df_10 BOOLEAN NOT NULL,
df_100 BOOLEAN NOT NULL,
df_1000 BOOLEAN NOT NULL,
df_above_1000 BOOLEAN NOT NULL,
target INT(11) unsigned NOT NULL,
assay MEDIUMINT(5) unsigned zerofill NOT NULL,
insert_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
inserted_by INT(11) unsigned NOT NULL,
initials varchar(255),
experiment VARCHAR(45),
CONSTRAINT FOREIGN KEY (inserted_by) REFERENCES user (iduser),
CONSTRAINT FOREIGN KEY (target) REFERENCES protein (PID),
CONSTRAINT FOREIGN KEY (sample_type) REFERENCES sample_type (ID),
CONSTRAINT FOREIGN KEY (assay) REFERENCES assays (AID),
CONSTRAINT FOREIGN KEY (experiment) REFERENCES exp (EID)
);

Aber ich bekomme den Fehler:

ERROR 1215 (HY000): Cannot add foreign key constraint

Um weitere Informationen zu erhalten, habe ich Folgendes getan:

SHOW ENGINE INNODB STATUS\G

Von dem ich bekam:

FOREIGN KEY (experiment) REFERENCES exp (EID)
):
Cannot find an index in the referenced table where the
referenced columns appear as the first columns, or column types
in the table and the referenced table do not match for constraint.

Mir scheinen die Spaltentypen übereinzustimmen, da sie beide varchar (45) sind. (Ich habe auch versucht, das zu setzenexperiment spalte nicht auf null, aber das hat es nicht behoben) Also ich schätze das Problem muss das seinCannot find an index in the referenced table where the referenced columns appear as the first columns. Aber ich bin mir nicht ganz sicher, was dies bedeutet oder wie ich es überprüfen / beheben soll. Hat jemand irgendwelche Vorschläge? Und was ist damit gemeintfirst columns?

LÖSUNG:

Ich habe mein Problem herausgefunden. Ich hatte eine andere Sortierung für die Exp-Tabelle (utf8_unicode_ci) als die Standardeinstellung für die Datenbank (utf8_general_ci). Also füge hinzu

CHARACTER SET utf8 COLLATE utf8_unicode_ci

am Ende der Tabellenerstellungsabfrage wurde mein Problem behoben.

Aber ich bin immer noch gespannt, was damit gemeint istfirst columns. Hat das etwas mit Indizes zu tun?

Antworten auf die Frage(9)

Ihre Antwort auf die Frage