0
CREATE TABLE `TABLE1` ( `TABLE_ID1` varchar(4) NOT NULL, PRIMARY KEY (`TABLE_ID1`), ) ENGINE=MyISAM DEFAULT CHARSET=latin1; CREATE TABLE `TABLE2` ( `LOCAL_CUST_ID` int(10) NOT NULL, `TABLE_ID1` varchar(4) NOT NULL, ) ENGINE=InnoDB DEFAULT CHARSET=latin1; 

Both table created successfully in MySql but while trying adding foreign key by below Alter statement then getting below issue.

ALTER TABLE TABLE2 ADD CONSTRAINT FK_ID FOREIGN KEY(TABLE_ID1) REFERENCES TABLE1(TABLE_ID1); Error Code: 1452 Cannot add or update a child row: a foreign key constraint fails (`#sql-7fcd_79`, CONSTRAINT `FK_ID` FOREIGN KEY (`TABLE_ID1`) REFERENCES `TABLE1` (`TABLE_ID1`)) 

In both table I have used different engine.

Can someone please help what I am not able to add foreign key?

1 Answer 1

2

See documentation:

13.1.18.6 Using FOREIGN KEY Constraints

...

Foreign keys definitions are subject to the following conditions:

...

  • ... The parent and child tables must use the same storage engine. ...

...

Sign up to request clarification or add additional context in comments.

2 Comments

If I am trying to change engine then its saying table already exist. How to resolve this issue?
@ShiladittyaChakraborty: Try ALTER TABLE `TABLE1` ENGINE=InnoDB;, after, ALTER TABLE `TABLE2` ADD CONSTRAINT ....

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.