0

I'm kinda new at mysql database so I'm 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?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
John
  • 43
  • 2
  • 6

1 Answers1

0

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

CREATE DATABASE a; USE a;
CREATE TABLE a1 (id int not null auto_increment primary key);
CREATE DATABASE b; USE b;
CREATE TABLE b1 (id int, aid int, foreign key (aid) references a.a1 (id));
Explosion Pills
  • 188,624
  • 52
  • 326
  • 405