Problem creating table in the database

Asked

Viewed 54 times

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 is generating me this mistake

ERROR 1215 (HY000): Cannot add Foreign key Constraint

what will be the problem?

  • 3

    Before creating this table, you created the condominium and owner?

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 when creating a foreign key are that both the referenced field and the foreign key field need to match:

  • Engine: must be the same

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

    • Ex.: VARCHAR(80)
  • Collation: must 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 define a SET NULL having some of the columns are defined as NOT NULL.

Reference: Mysql

Browser other questions tagged

You are not signed in. Login or sign up in order to post.