不同数据库之间的 MySQL InnoDB 外键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3905013/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 17:24:22  来源:igfitidea点击:

MySQL InnoDB foreign key between different databases

mysqldatabaseforeign-keysinnodb

提问by Alaa

I would like to know if it's possible in InnoDB in MySQLto have a tablewith foreign keythat references another table in a different database?

我想知道是否有可能用在InnoDB中MySQL有一个table外键引用另一个表不同database

And if so, how this can be done ?

如果是这样,如何做到这一点?

采纳答案by BarsMonster

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

我没有看到对http://dev.mysql.com/doc/refman/5.1/en/innodb-foreign-key-constraints.html 的任何限制。

So just use otherdb.othertableand you will be good.

所以只需使用otherdb.othertable就可以了。

回答by Spilarix

It's possible : Link to do it

这是可能的: 链接来做到这一点

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

示例(Table1 在 database1 中,HelloTable 在 database2 中):

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

回答by Salim

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

下面是如何在表 t2 上添加外键,参考表 db1.historial(codh):

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