user023511223
user023511223

Reputation: 13

Two databases' columns reference as foreign key

I have 2 databases A and B

In A there is a table, one of its columns is a column entry for elements in a table of B (id_A=id_B). While creating B, how should I reference A's table's column as a foreign key? Or I just need to ignore and once coding I will reference A's table as "A.Table" for example. I do this in mysql 5.3. Thank you.

[UPDATE]

I mean while I do something like this

CREATE DATABASE B;

USE B;

CREATE TBLE BT(id int primary key,
               _id int foreign key(_id) references A.TableX(_id));

Is this a correct command ?

Upvotes: 0

Views: 87

Answers (1)

ypercubeᵀᴹ
ypercubeᵀᴹ

Reputation: 115530

Yes, this is correct - with some modifications:

CREATE TABLE B.tableB
( id int
, _id int 
, primary key (id)
, foreign key (_id)            --- the FK should not be declared inline in MySQL
    references A.TableX(_id)   --- (_id) should be the the PK of tableX in db A
) ;

Upvotes: 1

Related Questions