Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set Foreign Key in mysql between Two different databases?

Tags:

database

mysql

I have a database A and database B. In database A i set company_id as Primary key. In database B i have to set same company_id as a reference. Is it possible to set B.company_id as foreign key? If yes means how to set this B.company_id as foreign key?

like image 247
Cody Avatar asked Jan 12 '23 16:01

Cody


2 Answers

Any relationship between/among tables are confined within a schema. You cannot define foreign key constrain between two unrelated tables in two different schema. If you have a real need o do it then you need to re-think about your database design.

like image 116
Juned Ahsan Avatar answered Jan 25 '23 20:01

Juned Ahsan


Please refer to this nearly duplicate item. It is possible to have foreign keys between different schemas (sometimes called "databases") on the same MySQL database server. MySQL InnoDB foreign key between different databases

like image 36
Jim Avatar answered Jan 25 '23 20:01

Jim