0
我有兩個表,我希望兩者之間有參考。錯誤代碼:1215.無法添加外鍵約束
我嘗試使用alter table命令,但它給了我一個錯誤,任何人都可以幫助我嗎?
CREATE TABLE `registos` (
`data_registo` char(10) NOT NULL,
`hora_registo` time NOT NULL,
`idSensor` varchar(8) NOT NULL,
`Temperatura` char(6) DEFAULT NULL,
`Humidade` char(6) DEFAULT NULL,
`pt_orvalho` char(6) DEFAULT NULL,
`idRegisto` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`idRegisto`,`idSensor`,`data_registo`,`hora_registo`),
KEY `fk_registos_sensores1_idx` (`idSensor`),
CONSTRAINT `fk_registos_sensores1` FOREIGN KEY (`idSensor`) REFERENCES `sensores` (`idSensor`) ON DELETE NO ACTION ON UPDATE NO ACTION
)
CREATE TABLE `alarmes` (
`idAlarme` int(11) NOT NULL AUTO_INCREMENT,
`descricao_alarme` varchar(45) DEFAULT NULL,
`data_criacao` datetime DEFAULT CURRENT_TIMESTAMP,
`idRegisto` int(11) NOT NULL,
PRIMARY KEY (`idAlarme`,`idRegisto`)
)
ALTER TABLE alarmes
ADD CONSTRAINT FK_alarmes1
FOREIGN KEY (idRegisto) REFERENCES registos(idRegisto)
ON UPDATE CASCADE
ON DELETE CASCADE;
對不起,我複製了一箇舊的腳本...我編輯了代碼,但給我同樣的錯誤:( – user3320956
@ user3320956發佈您在問題中所做的修改,我們不會猜測你編輯 – Kermit
@ user3320956看到我的編輯,對於我的字段類型和表名更正都正常工作 – Air