News database design

11,307

You don't need NewsHeaderContent unless you want to use the same content in more than one header, but I don't believe you want it, so you can use two tables:

Table NewsHeader

id  
NewsTitle  
NewsDesc  
NewsDate

Table NewsContent

NewsId
ContentId  
Sequence
content
ContentUser
ContentDate

you can use the field Sequence to order the content.

An example of some data in this structure:

NewsHeader

id    NewsTitle       NewsDesc               NewsDate
 1    'First News'    'Some Description'     2012-10-07
 2    'Another News'  'Description of News'  2102-12-07

NewsContent

NewsId  ContentId   Sequence  content                    ContentUser  ContentDate
     1          1          1  'Some Text'                       NULL      NULL
     2          1          1  'Some user inserted text'         NULL      NULL
     2          2          2  [Status]                         User2  2012-12-07
     2          3          4  [Some other status]              User2  2012-12-07
     2          4          3  'Some other text'                 NULL      NULL
     2          5          5  'another text'                    NULL      NULL
     2          6          6  [Another Status]                 User2  2012-12-07

The sequence would be able to be changed, and as ContentUser and ContentDate would only be used when it's a status, then you could use these field to indentify it text or status, or you can add a filed Type that could hold for example T for text and S for status.

Here a SQLFiddle of my example: SQLFiddle

Share:
11,307
Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin almost 2 years

    I am making a news website but I want to let the user insert statuses from Facebook. The user can put the status anywhere in the news while writing it. I want to display the news as it's inserted.

    Here's an example of news' content

    "Some user inserted text Status Some other text Some other status another text Another Status"

    So I'm thinking what is a good database design in that case. The status has user, who has wrote it, date and content. The news has title, description and date.

    I've come up with five tables.

    Table NewsHeader

    id  
    NewsTitle  
    NewsDesc  
    NewsDate
    User
    

    Table NewsContent - contains only id and the message from the status or text of the news

    id  
    content
    

    Table NewsContentDetails - contains details of the status

    id
    ContentUser
    ContentDate
    

    Table NewsContentDetailsLink - junction table of NewsContent and NewsContentDetails

    NewsContentId  
    NewsContentDetailsId
    

    Table NewsHeaderContent - junction table of NewsHeader and NewsContent

    NewsHeaderId  
    NewsContentId
    

    Is that a good database design or is there's a better way to do it? I'm concerned that I'll have to write many JOINs in the sql query when displaying the news and that will be slow.

    EDIT: The DB design suggested by @hrr

    **News:**
    

    ID | Title | Content | User | Date

    **Elements:**
    

    ID | Content | User | Date | News_ID

    But some fields in elements will be empty and I think that this's not a good option.

    Thanks in advance :)