Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 970573
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:49:37+00:00 2026-05-16T02:49:37+00:00

Which table structure is considered better normalized ? for example Note: idType tells on

  • 0

Which table structure is considered better normalized ?

for example

Note: idType tells on which thing the comment has taken place on, and the subjectid is the id of the item the comment has taken place on.

useing idType the textually named identifier for the subjectid.

commentid ---- subjectid ----- idType
--------------------------------------
1                22            post
2                26            photo
3                84            reply
4                36            post
5                22            status

Compared to this.

commentid ---- postid ----- photoid-----replyid
-----------------------------------------------
1                22          NULL        NULL
2                NULL         56         NULL
3                23          NULL        NULL
4                NULL        NULL        55
5                26          NULL        NULL

I am looking at both of them and I dont think in the first table I would be able to relate it to a foreign key constraint =( (ie. comment gets deleted if the post or photo is deleted), where as in the second one that is possible, how would you approach a similar issue keeping in mind that the database will need to expand overtime and data integrity is also important =).

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T02:49:38+00:00Added an answer on May 16, 2026 at 2:49 am

    The first is more normalized, if slightly incomplete. There are a couple of approaches you can take, the simplest (and strictly speaking, the most ‘correct’) will need two tables, with the obvious FK constraint.

    commentid ---- subjectid ----- idType
    --------------------------------------
    1                22            post
    2                26            photo
    3                84            reply
    4                36            post
    5                22            status
    
    idType
    ------
    post
    photo
    reply
    status
    

    If you like, you can use a char(1) or similar to reduce the impact of the varchar on key/index length, or to facilitate use with an ORM if you plan to use one. NULL’s are always a bother, and if you start to see them turn up in your design, you will be better off if you can figure out a convenient way to eliminate them.

    The second approach is one I prefer when dealing with more than 100 million rows:

    commentid ---- subjectid
    ------------------------
    1                22    
    2                26     
    3                84     
    4                36     
    5                22     
    
    postIds ---- subjectid
    ----------------------
    1                22   
    4                36   
    
    photoIds ---- subjectid
    -----------------------
    2                26    
    
    replyIds ---- subjectid
    -----------------------
    3                84    
    
    statusIds ---- subjectid
    ------------------------
    5                22     
    

    There is of course also the (slightly denormalized) hybrid approach, which I use extensively with large datasets, as they tend to be dirty. Simply provide the specialization tables for the pre-defined idTypes, but keep an adhoc idType column on the commentId table.

    Note that even the hybrid approach only requires 2x the space of the denormalized table; and provides trivial query restriction by idType. The integrity constraint however is not straight forward, being an FK constraint on a derived UNION of the type-tables. My general approach is to use a trigger on either the hybrid table, or an equivalent updatable-view to propigate updates to the correct sub-type table.

    Both the simple approach and the more complex sub-type table approach work; still, for most purposes KISS applies, so just I suspect you should probably just introduce an ID_TYPES table, the relevant FK, and be done with it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 516k
  • Answers 516k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If you design your view(s) in terms of interfaces, you… May 16, 2026 at 6:49 pm
  • Editorial Team
    Editorial Team added an answer The expression (u'1S²') is not a tuple, it's a unicode… May 16, 2026 at 6:49 pm
  • Editorial Team
    Editorial Team added an answer Use this to do it: $thisID = 0; for ($i… May 16, 2026 at 6:49 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I running MySQL 5.1.30 on Solaris 10. As I have a table which I
table structure: sysdurationtimeday , sysdurationtimehour, sysdurationtimeminute 1, 12,10 3, 23,10 0, 0,10 i have
I have following table structure. <table> <tr> <td><a href=#></td> </tr> </table> When I click
I have a table structure: <table style=width: 100%;> <tr> <td> <a href=# class=xx>one</a> </td>
Say I have a high score table structured like name score name score ....
I'm working on building a tree structure in MySQL and have been experimenting with
I've already checked out the question Deleting duplicate records using a temporary table and
I have a reasonably complex XML document which I want to flatten down to
I'm thinking about how to represent a complex structure in a SQL Server database.
We have a table with 6 million records, and then we have a SQL

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.