1

Can a foreign key in one MySQL database/schema refer to a table in a different database/schema?

If so, what limitations are there on doing this?

3

Yes, you just need to specify the schema before the table name. For example, if the referencing table is in schema qa and the referenced table in xx:

ALTER TABLE qa.test_results 
    ADD COLUMN file INTEGER,
    ADD FOREIGN KEY (file) REFERENCES xx.files (file) ;

Then all of your queries will also need to be formatted using schema.table

| improve this answer | |
  • Correct except for the code. The syntax would not add a foreign key. Edited and fixed. – ypercubeᵀᴹ Dec 21 '17 at 15:04

Not the answer you're looking for? Browse other questions tagged or ask your own question.