MySQL InnoDB foreign key between different databases

Viewed 39874

I would like to know if it's possible in InnoDB in MySQL to have a table with foreign key that references another table in a different database ?

And if so, how this can be done ?

4 Answers
ALTER TABLE `tablename1`
ADD CONSTRAINT `tablename1_student_id_foreign` 
FOREIGN KEY (`tablename1`.`id`) 
REFERENCES `db2`.`tablename2`(`id`)
ON DELETE CASCADE ON UPDATE CASCADE;

if we have table calling answers in db1 and student in db2 calling ramiyusu_offline

we must type as below

ALTER TABLE `answers`
ADD CONSTRAINT `answers_student_id_foreign` 
FOREIGN KEY (`id`)
REFERENCES `ramiyusu_offline`.`student`(`id`) 
ON DELETE CASCADE ON UPDATE CASCADE;
Related