MySQL InnoDB 外键可以跨数据库吗?

DDD
发布: 2024-11-13 11:25:02
原创
823 人浏览过

Can MySQL InnoDB Foreign Keys Span Across Databases?

MySQL InnoDB Foreign Keys Across Databases

In MySQL InnoDB, you can create foreign keys between tables in different databases. This allows you to enforce referential integrity even when the tables reside in separate databases. To achieve this:

CREATE TABLE example_table (
  id INT PRIMARY KEY,
  other_id INT,
  FOREIGN KEY (other_id) REFERENCES otherdb.othertable(id)
);
登录后复制

In the example above, the example_table in your current database has a foreign key that references the other_id column in the othertable in the otherdb database. The otherdb.othertable must exist and its id column must be a primary key or unique index.

This allows you to maintain referential integrity even if you move the tables to different databases. The MySQL documentation does not specify any limitations on foreign key references across databases. Therefore, you can confidently use the otherdb.othertable syntax to establish cross-database foreign key relationships.

以上是MySQL InnoDB 外键可以跨数据库吗?的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板