#1005 - Cannot create table`db_to_do_list`. `tb_completed` (error no. 150 "Foreign key Constraint is incorrectly Formed")

Asked

Viewed 29 times

0

I’m posting this question here, because as much as I’ve researched, I can’t solve it

I’m starting to study Mysql and the problem that occurred was this:

#1005 - Não pode criar a tabela `db_to_do_list`.`tb_concluido` (erro no. 150 "Foreign key constraint is incorrectly formed")

NOTE: I tried to take out the "DEFAULT 'Empty'" of fk_conteudo and it still doesn’t work.
OBS2: If I take the last FOREIGN KEY, the code works, but the fk_content that I would need to retrieve the "content" from tb_list would not work ;/

The tables are these:

Imagem da tabela tb_lista já criada

CREATE TABLE tb_concluido(
    id_conteudo_concluido INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    fk_id_conteudo INT NOT NULL,
    fk_conteudo TEXT NOT NULL DEFAULT 'Vazio',
    FOREIGN KEY (fk_id_conteudo) REFERENCES tb_lista (id_conteudo),
    FOREIGN KEY (fk_conteudo) REFERENCES tb_lista (conteudo)
)

1 answer

0


First we need to create the PAI table which in your case would be the table tb_list

CREATE TABLE tb_lista(
    id_conteudo INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    conteudo TEXT NOT NULL
);

Later the child table is created

CREATE TABLE tb_concluido( 
    id_conteudo_concluido INT NOT NULL PRIMARY KEY AUTO_INCREMENT, 
    fk_id_conteudo INT NOT NULL, 
    FOREIGN KEY (fk_id_conteudo) REFERENCES tb_lista (id_conteudo) 
);

remember that for each record of a table, it will show a record of the other table, if you need a list of the secondary table, it will be necessary to replicate the data of the main table An example would be a father may have more than one son, but a son will only have one father, that is, if you have a brother the father’s name will be replicated. Another problem that is in its code is the Normalization, it says that it is made reference between the tables through an ID, avoiding to do by VARCHAR.

  • Thank you very much for the answer ! I did not know about this that you mentioned about "normalization". I researched here and it seems that foreign keys are always made by Ids even, I did not know this rsrs. Thank you very much, from a huasha heart I was stuck here and you helped me a lot now !! haha. Someday I hope to help back ^^

Browser other questions tagged

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