I m trying to add a foreign key to table table2 from table1. But i got an error like this… what is the problem…help
Error
SQL query:
ALTER TABLE `table2` ADD FOREIGN KEY ( `bus_id` ) REFERENCES `db1`.`table1` (
`bus_id`
) ON DELETE CASCADE ON UPDATE CASCADE ;
MySQL said: Documentation
#1452 - Cannot add or update a child row: a foreign key constraint fails (`db1`.`#sql-664_e2`, CONSTRAINT `#sql-664_e2_ibfk_1` FOREIGN KEY (`bus_id`) REFERENCES `table1` (`bus_id`) ON DELETE CASCADE ON UPDATE CASCADE)
Structure of table1

Structure of table2

Your tables have some data. Check that all values stored in
table2.bus_idare correct; all these values have to be intable1.bus_id.Run this query to view wrong
bus_idvalues intable2:Remove this records from
table2, or add thisbus_idto thetable1. Then try to create foreign key again.