Mysql error 1215: Unable to add foreign key constraint

0
  • How do I resolve this error? I know that one reason for this is that the columns must have the same size and specification. But I've tried everything and it's not working.

CREATE DATABASE'sistema'/*!40100DEFAULT CHARACTER SET utf8*/;
use sistema;
drop database sistema;

CREATE TABLE'conta'(
    'numero'int(9)NOT NULL,
    'agencia'int(4)NOT NULL,
    'saldo'float NOT NULL,
    'limite'float NOT NULL,
    PRIMARY KEY('numero')
)ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE'correntista'(
    'cpf'varchar(11)NOT NULL,
    'nome'varchar(60)NOT NULL,
    'datanascimento'date NOT NULL,
    'sexo'varchar(1)NOT NULL,
    'idendereco'int(9)NOT NULL AUTO_INCREMENT,
    'numero'int(9)NOT NULL,
    PRIMARY KEY('cpf'),
    KEY'idendereco'('idendereco'),
    KEY'numero'('numero'),
    CONSTRAINT'correntista_ibfk_1'FOREIGN KEY('idendereco')
        REFERENCES'endereco' ('idendereco'),
    CONSTRAINT'correntista_ibfk_2' FOREIGN KEY('numero')
        REFERENCES'conta'('numero')
)ENGINE=InnoDB DEFAULT CHARSET = utf8;

CREATE TABLE'endereco'(
    'idendereco'int(9)NOT NULL AUTO_INCREMENT,
    'cep'varchar(8)NOT NULL,
    'logradouro'varchar(60)NOT NULL,
    'tipo'varchar(40)NOT NULL,
    'numero'int(9)NOT NULL,
    'bairro'varchar(40)NOT NULL,
    'cidade'varchar(60)NOT NULL,
    'uf'varchar(2)NOT NULL,
    PRIMARY KEY('idendereco')
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
    
asked by anonymous 07.06.2015 / 17:37

1 answer

1

Two things:

1 - You are trying to create a FOREIGN KEY referencing a table that does not yet exist. The creation of the correntista table must be done after the creation of the other two.

2 - Include a space between REFERENCES and the name of the table.

Try here

AUTO_INCREMENT >     
07.06.2015 / 19:07