Question

Pour une raison quelconque, ce MySQL échoue:

CREATE SCHEMA IF NOT EXISTS `partB` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci ;
USE `partB`;

CREATE TABLE Employees ( ssn CHAR(11),
Name CHAR(30),
mlot INTEGER,
PRIMARY KEY(ssn))
ENGINE = InnoDB;

CREATE TABLE Dept_Mgr ( did INTEGER,
dname CHAR(20),
ssn CHAR(11) NOT NULL,
PRIMARY KEY (did),
FOREIGN KEY (ssn) REFERENCES Employees
ON DELETE NO ACTION)
ENGINE = InnoDB;

Il donne l'erreur:

  

ERREUR 1005 (HY000): Impossible de créer la table   partb.dept_mgr (errno: 150)

Que peut être la cause?

Était-ce utile?

La solution

Cette commande:

SHOW ENGINE INNODB STATUS;

est votre ami quand vous avez du mal à créer des clés étrangères. Sortie (abrégée)

------------------------
LATEST FOREIGN KEY ERROR
------------------------
100225  2:51:42 Error in foreign key constraint of table test/dept_mgr:
FOREIGN KEY (ssn) REFERENCES Employees
ON DELETE NO ACTION)
ENGINE = InnoDB:
Syntax error close to:

ON DELETE NO ACTION)
ENGINE = InnoDB

Si vous changez votre déclaration à:

CREATE TABLE Dept_Mgr ( 
    did INTEGER,
    dname CHAR(20),
    ssn CHAR(11) NOT NULL,
    PRIMARY KEY (did),
    FOREIGN KEY (ssn) REFERENCES Employees(ssn)
) engine = innodb;

il fonctionne.

Autres conseils

Vous devez spécifier la colonne (s) dans la table étrangère pour la clé:

FOREIGN KEY (ssn) REFERENCES Employees (ssn) ...

Vous avez seulement la clé étrangère faisant référence à la table, pas la colonne.

Essayez: références Employee.ssn

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top