Search code examples
sqldatabase-designrelationalintegrity

How to design a media table with references to multiple (at least 4) tables?


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


Solution

  • 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