Take the 2-minute tour ×

This question already has an answer here:

Hi everyone :) I'm kinda new at mysql database so im sorry if this is kinda noob question... if I have 2 database, database a and database b.. then I created 1 table in each database.. can I use the table in database b to connect to the table in database a to have a foreign and primary key? or it can only be connected between tables on the same database?

share|improve this question
add comment

marked as duplicate by likeitlikeit, alecxe, guido, Roman C, skuntsel Jun 2 '13 at 8:08


1 Answer

up vote 0 down vote accepted

I think your question is "can foreign key columns reference columns in another database." The answer is "yes they can."

CREATE TABLE a1 (id int not null auto_increment primary key);
CREATE TABLE b1 (id int, aid int, foreign key (aid) references a.a1 (id));
share|improve this answer
Thanks for answering... I will try it.. thanks for the new knowledge :) –  John Jun 1 '13 at 19:33
add comment
