如何设计引用多个(至少 4 个)表的媒体表?

发布于 2024-10-20 08:47:16 字数 297 浏览 2 评论 0原文

我正在为我的食谱设计一个数据库。我在设计中创建了多个表格:书籍、作者、食谱、成分,对于所有这些项目,我想将媒体(图像或视频)链接到所有这些表格中的项目。

我正在考虑这样的设计:

media_id,
rid (primary key of foreign table),
rtype (1=book, 2=author, 3=recipe, 4=ingredient),
media_type(1=image,2=video),
media_url

但是我如何确保关系完整性?

谢谢

I am designing a database for my cookbooks. I have created multiple tables in my design: books, authors, recipes, ingredients and for all these items I want to link media (images or video) to items in all these tables.

I was thinking of a design like:

media_id,
rid (primary key of foreign table),
rtype (1=book, 2=author, 3=recipe, 4=ingredient),
media_type(1=image,2=video),
media_url

But how will I ensure relational integrity?

Thanks

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

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

发布评论

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

评论(3

怪我闹别瞎闹 2024-10-27 08:47:16

您提出的设计似乎暗示每个实体(书籍、作者等)可以拥有多个媒体文件,因此为了保持关系完整性,我将为每个关系拥有单独的联结表。

在此处输入图像描述

Your proposed design seems to imply that each entity (book, author, etc.) can have multiple media files, so to maintain relational integrity, I'd have separate junction tables for each relationship.

enter image description here

夏花。依旧 2024-10-27 08:47:16

如果每个表只有 1 个媒体项,则 media_id 应该位于表中,而不是相反。

如果可能有多个媒体项,您应该将它们链接到一个额外的表中。每个项目应该有一个额外的表(例如 bookid_mediaid)。

如果您认为它应该在一个表中链接,那么您实际上是在声明这些项目至少有一些共同点。否则,根据类型的不同,rid 在整个记录中将具有不同的含义,而这在关系理论中是不可能的。

结论:

你的设计不好。您应该为每个实体建立一个关系,或者找到所有实体的共同点并使用它来链接到您的媒体类型。

If there's only 1 media-item for each table, the media_id should be in the tables in stead of the other way around.

If several media-items are possible you shoud link them within an extra table. There should be an extra table per item (bookid_mediaid for example).

If you think it should be linkable within one table, you are actually stating that those items have at least something in common. Otherwise the rid would have different meaning throughout the records, depending on type, and that's not possible in relational theory.

Concluding :

Your design is not good. Either you should have a relationship per entity or find what's common for all entities and use that to link to your mediatypes.

梦巷 2024-10-27 08:47:16

Matthijs,

对于关系设计,您必须了解这些对象如何相互关联,然后决定数据模型。这是初稿和我假设的条件。您可能对您的案例有不同的规则。发布它们,有人应该能够发布准确的模型。

Each book can be written by many authors and each author can write different books.   ( M:N)
RECIPES <--> BOOKS (M:N)
RECIPES <--> INGREDIENTS (M:N)

These would be the initial set of tables.
BOOKS, AUTHORS, BOOKS_AUTHORS_ASC, RECIPES, BOOKS_RECIPES_ASC,
INGREDIENTS, RECIPES_INGREDIENTS_ASC.

如果媒体与一本且仅一本书相关,您将有一个不同的表,例如带有以下列的媒体。 BOOK_ID 是与该媒体关联的父列。

MEDIA
-------------------------------------
MEDIA_ID NUMBER -- Primary-key
BOOK_ID  NUMBER -- Foreign Key..
MEDIA_TYPE varchar2(20) -- 'IMAGE','VIDEO' etc..
other_column1 varchar2(50),
other_column2 varchar2(50)
-- so on..

如果给定的图像/视频可以与多本书相关联,并且每本书可以有不同的图像,那么您将拥有不同的关联实体..类似于..

Media
--------
Media_id number -- primary key
media_type varchar2(10),
media_name varchar2(100),
--- other columns.

MEDIA_BOOK_ASC
--------------
MEDIA_BOOK_ASC_ID NUMBER,
MEDIA_ID NUMBER,  --foreign key to media table.
BOOK_ID NUMBER,   --foreign key to book table
--other columns related to associations...

Matthijs,

For a relational design, you have to look at how these objects are related to each other and then decide on the data model. Here is an initial draft and the conditions that I am assuming. You might have different rules for your case.. post them and someone should be able to post an accurate model.

Each book can be written by many authors and each author can write different books.   ( M:N)
RECIPES <--> BOOKS (M:N)
RECIPES <--> INGREDIENTS (M:N)

These would be the initial set of tables.
BOOKS, AUTHORS, BOOKS_AUTHORS_ASC, RECIPES, BOOKS_RECIPES_ASC,
INGREDIENTS, RECIPES_INGREDIENTS_ASC.

If the media is related to one and only one book, you'll have a different table , say, media with the following columns. BOOK_ID is the parent column to which this media is associated.

MEDIA
-------------------------------------
MEDIA_ID NUMBER -- Primary-key
BOOK_ID  NUMBER -- Foreign Key..
MEDIA_TYPE varchar2(20) -- 'IMAGE','VIDEO' etc..
other_column1 varchar2(50),
other_column2 varchar2(50)
-- so on..

And if a given image/video can be associated to multiple books and each book can have different images, then you'll have a different entity for the association.. something like..

Media
--------
Media_id number -- primary key
media_type varchar2(10),
media_name varchar2(100),
--- other columns.

MEDIA_BOOK_ASC
--------------
MEDIA_BOOK_ASC_ID NUMBER,
MEDIA_ID NUMBER,  --foreign key to media table.
BOOK_ID NUMBER,   --foreign key to book table
--other columns related to associations...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文