混淆是使用 HAS 还是属于 1:1

  • 本文关键字:属于 HAS sql database-design
  • 更新时间 :
  • 英文 :


我知道在哪里放置 1:1 的外键。

Owner(pet_id) [HAS] Pet
Pet(owner_id) [BELONGS TO] Owner

但是考虑一个这样的示例:

Profile(section_id) [HAS] MoviesSection
Profile(section_id) [HAS] BooksSection
MoviesSection(profile_id) [BELONGS TO] Profile
BooksSection(profile_id) [BELONGS TO] Profile

我如何决定是要使用 HAS 还是属于,例如,如果我想"抓取配置文件的所有部分以一次显示"。感觉两者都行得通?

profile
---
id movies_section_id books_section_id
movies_section
---
id favorite_movie favorite_actor
books_section
---
id favorite_book favorite_author

与。

属于

profile
---
id
movies_section
---
id profile_id favorite_movie favorite_actor
books_section
---
id profile_id favorite_book favorite_author

我应该问哪些其他问题来决定?例如,如果没有配置文件就无法存在电影部分,这会有所不同吗?

也许不是你想要的答案,但我想用SQL术语来表达它。您询问的部分:

Profile(section_id) [HAS] MoviesSection
Profile(section_id) [HAS] BooksSection
MoviesSection(profile_id) [BELONGS TO] Profile
BooksSection(profile_id) [BELONGS TO] Profile

在 SQL 中看起来像:

create table profile (
profile_id
);
create table moviessection (
section_id,
profile_id,
constraint fk1 foreign key (section_id) references profile (profile_id)
);
create table bookssection (
section_id,
profile_id,
constraint fk2 foreign key (section_id) references profile (profile_id)
);

最新更新