Error creating MySql foreign key in phpMyAdmin?

2

I'm having trouble creating foreign keys in mysql through phpmyadmin .

I have these tables

CREATE TABLE 'cidade' (
'idCidade' int PRIMARY KEY AUTO_INCREMENT,
'nome' varchar(25),
'Uf' CHAR(2)
);

CREATE TABLE 'aeroporto' (
'idAeroporto' int PRIMARY KEY AUTO_INCREMENT,
'nome' varchar(100),
'endereco' varchar(200),
'idCidade' integer
);

But when I try to create the relationship

ALTER TABLE 'usuario' ADD CONSTRAINT 'fk_cidadeUsuario' FOREIGN KEY ('idCidade')
REFERENCES 'cidade' ('idCidade');

I have the following error:

 #1005 - Can't create table 'trab_tsnet_142'.'#sql-1a80_281' (errno: 150 "Foreign key constraint is incorrectly formed")

What could be happening?

(obs: this is just one of the relationships that is giving error, I have the same problem in other relationships).

    
asked by anonymous 22.05.2016 / 19:27

1 answer

1

Make sure that the fields you are trying to reference are exactly the same type and size. What may also be happening is that to add a foreign key eligible field by phpmyadmin, you will first have to turn it into index. After converting to index, look for the Ver relações option in the table structure and add the foreign key. I do not know if it depends on the version of phpmyadmin, but the path is something like: clique no BD -> Tabela -> Estrutura -> Ver Relações (fica ao lado de "Visualização para impressão").

    
22.05.2016 / 23:35