MySQL InnoDB外键在不同数据库之间。

时间:2022-09-21 23:49:25

I would like to know if it's possible in InnoDB in MySQL to have a table with foreign key that references another table in a different database ?

我想知道在MySQL中的InnoDB中是否可能有一个具有外键的表引用另一个数据库中的表?

And if so, how this can be done ?

如果是的话,怎么做呢?

3 个解决方案

#1


47  

I do not see any limitation on http://dev.mysql.com/doc/refman/5.1/en/innodb-foreign-key-constraints.html .

我不认为http://dev.sqmyl.com/doc/refman/5.1 /en/innodb-foreign-key-constraints.html有任何限制。

So just use otherdb.othertable and you will be good.

所以用otherdb。否则你会很好的。

#2


15  

It's possible : Link to do it

这是可能的:链接来做它

Example (Table1 is in database1 and HelloTable is in database2) :

示例(表1在database1中,HelloTable在database2中):

ALTER TABLE Table1 
ADD foreign key FK_table1(ColumnNameFromTable1)
REFERENCES db2.HelloTable(ColumnNameFromHelloTable)

#3


3  

Below is how to add a foreign key on table t2, reference from table db1.historial(codh):

下面是如何在表t2中添加一个外键,来自表db1. history (codh):

alter table t2
add foreign key FK_t2(micod2)
    references db1.historial(codh)
    on delete cascade
    on update cascade;

#1


47  

I do not see any limitation on http://dev.mysql.com/doc/refman/5.1/en/innodb-foreign-key-constraints.html .

我不认为http://dev.sqmyl.com/doc/refman/5.1 /en/innodb-foreign-key-constraints.html有任何限制。

So just use otherdb.othertable and you will be good.

所以用otherdb。否则你会很好的。

#2


15  

It's possible : Link to do it

这是可能的:链接来做它

Example (Table1 is in database1 and HelloTable is in database2) :

示例(表1在database1中,HelloTable在database2中):

ALTER TABLE Table1 
ADD foreign key FK_table1(ColumnNameFromTable1)
REFERENCES db2.HelloTable(ColumnNameFromHelloTable)

#3


3  

Below is how to add a foreign key on table t2, reference from table db1.historial(codh):

下面是如何在表t2中添加一个外键,来自表db1. history (codh):

alter table t2
add foreign key FK_t2(micod2)
    references db1.historial(codh)
    on delete cascade
    on update cascade;