我正在尝试在我的数据库中创建一个表“SIGNATURE”我希望这个表有一个双主键,并为引用表“TRAITEMENT”的主键添加一个外键约束
CREATE TABLE TRAITEMENT (
id INTEGER NOT NULL,
dateHeurePrevue DATETIME NOT NULL,
commentaire LONGTEXT,
dateValidation TIMESTAMP,
CONSTRAINT PK PRIMARY KEY (id,dateHeurePrevue),
CONSTRAINT FK_traitement_consigne FOREIGN KEY (id) REFERENCES consigne(id)) ENGINE=InnoDB;
create unique index id_dateheureprevue on traitement(id, dateheureprevue);
CREATE TABLE SIGNATURE (
id INTEGER NOT NULL,
idPersonne INTEGER NOT NULL,
dateTimeTraitement DATETIME NOT NULL,
retard INTEGER,
motifRetard INTEGER,
heureEffectiveTraitement DATETIME NOT NULL,
CONSTRAINT PK PRIMARY KEY (id,dateTimeTraitement),
CONSTRAINT FK_id FOREIGN KEY (id) REFERENCES traitement(id),
CONSTRAINT FK_idPersonne FOREIGN KEY (idPersonne) REFERENCES personne(id),
CONSTRAINT FK_motifRetard FOREIGN KEY (motifRetard) REFERENCES retard(id),
CONSTRAINT FK_dateTimeTraitement FOREIGN KEY (dateTimeTraitement) REFERENCES traitement(dateHeurePrevue))ENGINE=InnoDB;
create unique index id_dateTimetraitement on signature(id, dateTimeTraitement);
显然(我试图删除它并且工作正常)问题来自这个forign键:
CONSTRAINT FK_dateTimeTraitement FOREIGN KEY (dateTimeTraitement) REFERENCES traitement(dateHeurePrevue)
我不明白,我没有看到任何类型的冲突,钥匙...... 如果有人能帮我解决这个问题......
提前致谢
答案 0 :(得分:0)
您的外键必须与主键字段完全相同,所以:
CONSTRAINT FK_dateTimeTraitement FOREIGN KEY (id, dateTimeTraitement) REFERENCES traitement(id, dateHeurePrevue)