error creating table with foreign key mysql

0

You are giving Can't create table error when creating this table in MySql. I have no idea where the problem is D:

CREATE TABLE IF NOT EXISTS 'tbnoticiasrel' (
'id' bigint(20) NOT NULL AUTO_INCREMENT,
'id_noticia' bigint(20) NOT NULL,
'id_noticia_relacionada' bigint(20) NOT NULL,
  PRIMARY KEY ('id'),
 FOREIGN KEY fk_id_noticia('id_noticia') 
 REFERENCES tbnews('id'),
 FOREIGN KEY fk_id_noticia_relacionada('id_noticia_relacionada') ,
 REFERENCES tbnews('id')
 )
    
asked by anonymous 23.07.2015 / 16:50

3 answers

1

puts SET FOREIGN_KEY_CHECKS=0; on line before CREATE TABLE IF NOT EXISTS tbnoticiasrel with this it will not check FOREIGN KEY

    
24.07.2015 / 23:07
1

The problem is in the syntax of the second FK, there is a comma between the FK and the Reference:

 FOREIGN KEY fk_id_noticia('id_noticia') 
 REFERENCES tbnews('id'),
 FOREIGN KEY fk_id_noticia_relacionada('id_noticia_relacionada') ,
 REFERENCES tbnews('id')

Just remove it:

 FOREIGN KEY fk_id_noticia('id_noticia') 
 REFERENCES tbnews('id'),
 FOREIGN KEY fk_id_noticia_relacionada('id_noticia_relacionada')
 REFERENCES tbnews('id')
    
23.07.2015 / 19:08
0

Whenever you create a table referenced for a database "with data", by default MySQL does a check of the referenced indexes, when it does not find the reference within the table usually gives this type of problem, to resolve this so that you do not have to use SET FOREIGN_KEY_CHECKS=0; , whose command is used to block reindexing of foreign keys, back up the data of the referenced tables by creating a copy with an underline , for example , at the end of your _backup table, then in the original tables, erase all data, create the new table that references and reimport the data from these_backup tables to the original tables, so that all content is re-indexed so that there is no future conflicts that can slow down your system.

    
23.07.2015 / 19:29