irl_irl
irl_irl

Reputation: 3975

Question about Normalisation

I am just wondering which of these would be better to use in a web application. I have a web app that lets the user post to the site. There are three different types of posts but similar enough that I could put them under one table. Is that okay to do?

I could normalise the tables in this way? (Putting type under either)

Table 1

UserPost
post_id 
user_id
type

Table 2

Post
post_id 
datetime
text

OR would using one table be better?

Table

Post
user_id
post_id 
datetime
type
text

I am leaning towards the third way, unless someone can point out disadvantages.

Upvotes: 1

Views: 235

Answers (3)

nvogel
nvogel

Reputation: 25526

Based on your latest comment my understanding is that post_id would be a candidate key in ALL THREE of your example tables. If that is correct then I suggest you create one table for each unique set of attributes (each type of post). So if all posts have the same attributes in common then it makes sense to have them all in one table but if there are two or three types then two or three tables would be more appropriate.

Upvotes: 0

jzd
jzd

Reputation: 23629

Combine the tables, there is not really advantage to break them out like you have in Table1 and Table2. Now if Table 1 has a separate key than post_id, you could eliminate some redundancy. Example:

Table 1

UserPost
user_post_id 
user_id
type

Table 2

Post
post_id 
user_post_id 
datetime
text

Upvotes: 1

Matten
Matten

Reputation: 17631

In the first approach, you will always have to create a row in both tables for each user post. So there is no drawback by only having one table, user_id should then be a foreign key for your user table, post_id the primary key and the other columns hold the data. There is no reason for creating two tables.

If the three different types of posts are describable by one common field, a discriminator like type is okay.

Upvotes: 1

Related Questions