0

I have two tables and I want there reference between the two.

I tried using the alter table command, but it gives me an error, can anyone help me please?

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;
strange_098
  • 1,261
  • 6
  • 24
  • 44

1 Answers1

2

You have defined idRegisto as a signed integer in one table and an unsigned integer in the other table. Foreign key references can only be made between fields with the same type.

Another problem is that you created the table with the name registos but you are trying to add a key referencing the table registo.

Air
  • 8,274
  • 2
  • 53
  • 88