Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DB design to use sub-type or not?

The database I'm designing has 3 major tables: BOOKS, ARTICLES, NOTES.
Each book or article can have multiple notes, my original design was just like that, which means both notes on books and notes on articles go the 'notes' table. Here are the columns for the NOTES table:

  • note_id
  • note_type
  • note_type_id
  • note_content

NOTE_TYPE can be either 'book' or 'article'; NOTE_TYPE_ID is the FK for a book_id if the note_type is 'book' OR an article id if the note_type is 'article'.

Now I start to wonder if that's the correct(or best normalized) design. An alternative approach is to use 5 tables

books / articles / notes / book_notes / article_notes

This way I can keep book notes and article notes separately, the columns are like

'notes' { note_id, note_content } 'book_notes' { book_id, note_id } 'article_notes' { articel_id, note_id }

Which one is correct or better?

like image 688
Shawn Avatar asked Oct 31 '09 11:10

Shawn


People also ask

What is the benefit of using entity super or sub type relationships?

The advantages are that supertypes allow us to unify common attributes, relationships and integrity for multiple entity sets, while subtypes allow us to support type-specific attributes, relationships and integrity constraints. This allows us to simplify the database and our queries and enforce tighter integrity.

What are sub types database?

A subtype is a sub-grouping of the entities in an entity type that is meaningful to the organization and that shares common attributes or relationships distinct from other subgroups. Create separate tables for the super type and all sub type entities for the following reasons: Data integrity enforced at database level.

Can a subtype have a foreign key?

The primary key of a subtype relation will also be a foreign key that references its supertype relation. Attributes of a supertype (except for the primary key) appear only in the relation that represents the supertype.


1 Answers

Maybe a bit different approach -- supertype/subtype is usually used when you have very specific columns for each subtype, like in Person supertype with Patient and Doctor subtypes. Person holds all data common to people and Patient and Doctor hold very specific columns for each one. In this example your book_notes and article_notes are not really that different.
I would rather consider having a supertype Publication with Book and Article as subtypes. Then you can have just one Note table with FK to Publication. Considering that a PK number in Publication is the same number as the [PK,FK] of Book (Article) you can do joins with notes on Publication, Book or Article. This way you can simply add another publication, like Magazine by adding a new sub-classed table and not changing anything regarding Note.

For example:

TABLE Publication (
      ID (PK)
    , Title
    , -- more columns common to any publication
)

TABLE Book (
      ID (PK) = FK to Publication
    , ISBN
    , -- more columns specific to books only
)

TABLE Article (
    ID (PK) = FK to Publication
    , -- more columns specific to articles only)

TABLE Note (
      ID (PK)
    , PublicationID = FK to Publication
    , NoteText
)

Primary key for Book and Article tables also serves as a foreign key to the Publication.

Now if we add another publication, Magazine:

TABLE Magazine (
    ID (PK) = FK to Publication
    , -- more columns specific to magazines only
)

We do not have to modify Note in any way -- and we have added columns specific to magazines only.


pub_model_01

like image 199
Damir Sudarevic Avatar answered Sep 19 '22 18:09

Damir Sudarevic