Problem in creating table in the bank

0

I put these commands to create the table;

CREATE TABLE pessoa (
    codigo BIGINT(20) PRIMARY KEY AUTO_INCREMENT,
    nome VARCHAR(80) NOT NULL,
    codigo_condominio BIGINT(20) NOT NULL,
    codigo_proprietario BIGINT(20) NOT NULL,
    tipoPessoa VARCHAR(80) NOT NULL,
    FOREIGN KEY (codigo_condominio) REFERENCES condominio (codigo),
    FOREIGN KEY (codigo_proprietario) REFERENCES proprietario (codigo)
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

but it's causing me this error

  

ERROR 1215 (HY000): Can not add foreign key constraint

What will be the problem?

    
asked by anonymous 18.09.2016 / 17:19

1 answer

2

When you receive this error message use the command below to get more details:

SHOW ENGINE INNODB STATUS;

The most common reasons for creating a foreign key are that both the referenced field and the foreign key field must match:

  • Engine : It should be the same

    • Ex: InnoDB
  • Datatype : should be the same and have the same size.

    • Ex: VARCHAR(80)
  • Collation : should be the same

    • Ex: utf8
  • Unique : Foreign keys must refer to the field that is unique (usually private) in the reference table.

Another cause of this error is you set a SET NULL because some of the columns are set to NOT NULL .

Reference: MySQL

    
19.09.2016 / 15:34