在 MySQL 中使用 InnoDB 时,显式定义*每个*外键是否很常见?

发布于 2024-10-28 00:07:46 字数 734 浏览 6 评论 0原文

我一直在使用 MyISAM 并且没有定义显式的外键关系,直到最近我决定开始担心引用完整性等问题。

我正在开发一个数据库,用于存储战斗事件、战士的统计数据,因此我认为这符合使用 InnoDB 和显式定义外键的标准。

我有一个战士表,其中有多列是外键。我想知道是否建议始终显式定义外键关系,无论表中引用了多少个外键?

特别是,对于这个表,我有:

CREATE TABLE `fights` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `event_id` int(10) unsigned DEFAULT NULL,
  `winner_id` int(10) unsigned DEFAULT NULL,
  `referee_id` int(10) unsigned DEFAULT NULL,
  `championship_match` enum('1','0') DEFAULT NULL,
  `weight_class` int(10) unsigned DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

我计划将 referee_idwinner_idevent_id 设为各自表的 id 列的所有外键。这是要走的路吗?或者,出于性能原因,我应该以关系完整性为代价限制显式定义的外键数量?

I've been using MyISAM and not defining explicit foreign key relationships until recently when I decided to start worrying about referential integrity and such.

I'm working on a database that stores statistics of fighting events, fighters, and so I think this meets the criteria for using InnoDB and explicitly defining foreign keys.

I have a fighters table which has multiple columns which are foreign keys. I'm wondering if it's recommended to always, explicitly define foreign key relationships no matter how many foreign keys there are referenced in a table?

In particular, for this table I have:

CREATE TABLE `fights` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `event_id` int(10) unsigned DEFAULT NULL,
  `winner_id` int(10) unsigned DEFAULT NULL,
  `referee_id` int(10) unsigned DEFAULT NULL,
  `championship_match` enum('1','0') DEFAULT NULL,
  `weight_class` int(10) unsigned DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

I plan to make referee_id, winner_id, event_id all foreign keys to their respective tables' id columns. Is this the way to go? Or should I restrict the amount of explicitly defined foreign keys for performance reasons at the cost of relational integrity?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小镇女孩 2024-11-04 00:07:46

永远、永远不要放弃正确的数据。请保持参考完整性。

Never, ever give up correct data, ever. Please keep your referential integrity.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文