表之间的关系

发布于 2024-12-20 09:42:16 字数 69 浏览 4 评论 0原文

我有一个名为目标的表,每个目标与其他目标有零到多个因果关系,这些关系我必须存储在数据库中,让我知道是否有办法关联这个表记录。

I have a table called objectives, each objective has zero to many cause-effect relationships with other objectives, these relationships I have to be stored in the database, let me know if there's a way to relate this table records.

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

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

发布评论

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

评论(1

心凉怎暖 2024-12-27 09:42:16

没有一种方法可以在不创建附加表的情况下关联记录(您需要在当前表上添加 N-1 列来对某个原因的 N 个可能的影响进行建模)。

创建一个如下所示的附加表应该可以满足您的目的。

CREATE TABLE cause_effect (
  cause integer NOT NULL,
  effect integer NOT NULL,
  CONSTRAINT cause_effect_pkey PRIMARY KEY (cause, effect),
  CONSTRAINT cause_effect_cause_fkey FOREIGN KEY (cause)
      REFERENCES yourtable (id),
  CONSTRAINT cause_effect_effect_fkey FOREIGN KEY (effect)
      REFERENCES yourtable (id)
)

应用适用的 FKey 行为。

There is not a way to relate the records without creating an additional table (you would need N-1 additional columns on your current table to model the N possible effects of a cause).

Creating an additional table like the one below should serve your purpose.

CREATE TABLE cause_effect (
  cause integer NOT NULL,
  effect integer NOT NULL,
  CONSTRAINT cause_effect_pkey PRIMARY KEY (cause, effect),
  CONSTRAINT cause_effect_cause_fkey FOREIGN KEY (cause)
      REFERENCES yourtable (id),
  CONSTRAINT cause_effect_effect_fkey FOREIGN KEY (effect)
      REFERENCES yourtable (id)
)

Apply FKey behaviour as applies.

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