首页  >  文章  >  数据库  >  MySQL InnoDB 外键可以跨数据库吗?

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

DDD
DDD原创
2024-11-13 11:25:02824浏览

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中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn