0 votes
1 view
in SQL by (20.3k points)

I have two tables, table1 is the parent table with a column ID and table2 with a column IDFromTable1 (not the actual name) when I put an FK on IDFromTable1 to ID in table1 I get the error Foreign key constraint is incorrectly formed error. I would like to delete the table 2 record if the table1 record gets deleted. Thanks for any help

ALTER TABLE `table2`  

   ADD CONSTRAINT `FK1` 

      FOREIGN KEY (`IDFromTable1`) REFERENCES `table1` (`ID`) 

      ON UPDATE CASCADE 

      ON DELETE CASCADE;

Let me know if any other information is needed. I am new to MySQL

1 Answer

0 votes
by (40.4k points)

If you run this same problem with HeidiSQL. The error message you will receive will be very cryptic. 

Your problem will end up as the foreign key column and the referencing column will not be of the same length or type.

The foreign key column will be SMALLINT(5) UNSIGNED and the referenced column will be INT(10) UNSIGNED. 

Once You'll make both of them the same exact type, the foreign key creation will work perfectly.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Jun 26, 2020 in SQL by Sudhir_1997 (51.3k points)
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...