Database relationships
Author
Discussion

JohnnyPanic

Original Poster:

1,282 posts

239 months

Thursday 1st April 2010
quotequote all
It's been a long time since I did any DB modelling theory.

Say for example I am creating a tagging system for a blog engine, I would start off with a 'Posts' table and a 'Tags' table. (I'm thinking MySQL or MSSQL style database)

PostID PostTitle
1 My first post
2 My second post


TagID TagName
1 Porsche
2 Tyres
3 TVR


I now need a many-to-many relationship between Posts and Tags, which I've seen this implemented in different ways.

1: Comma delimited tag id's in the posts table
PostID PostTitle TagIDs
1 My first post 1,2
2 My second post 2,3


2: Create a link table
LinkID PostID TagID
1 1 1
2 1 2
3 2 3
4 3 2



Am I right in thinking that the link table route is the better way to go? Or is there another new fangled way of doing things?

Don

28,378 posts

314 months

Thursday 1st April 2010
quotequote all
Yes a "link table" is the right way to do it.

JohnnyPanic

Original Poster:

1,282 posts

239 months

Thursday 1st April 2010
quotequote all
Thanks Don. Looks like - against the odds - I have actually remembered something from my University days in the 90's!

pdV6

16,442 posts

291 months

Thursday 1st April 2010
quotequote all
Link table definitely. Only pain awaits you doing it via a CSV list.

The Excession

11,669 posts

280 months

Thursday 1st April 2010
quotequote all
pdV6 said:
Link table definitely. Only pain awaits you doing it via a CSV list.
Definately a links table, index all fields/columns on that table too.

As pdV6 says, only pain awaits burying indexable information in a text column.

anonymous-user

84 months

Thursday 1st April 2010
quotequote all
I think there's more to it than that.

Are you ever going to change the TagName for a TagId? If not then why have the TagId? With the 'link' table scenario changing a TagName could change the meaning of the Tag altogether and you would not necessarily want all your Posts to suddenly be Tagged with a different meaning. If you are never going to change a TagName you should look at it differently.

Post table:
PostID PostTitle
1 My first post
2 My second post


Tag table:
TagName
Porsche
Tyres
TVR


PostTag table:
PostID Tag
1 Porsche
1 Tyres
2 TVR
3 Tyres


Then index the PostTag table on both columns - if you find you need to based on your anticipates volumes.

You need to consider the data, its meaning and its lifetime, not just concentrate on the relationships and normalisation.

Mike

Size Nine Elm

5,167 posts

314 months

Thursday 1st April 2010
quotequote all
Rule 1. Never ever use real world data as a data ID.
Rule 2. Never ever use real world data as a data ID.
Rule 3. Never ever use real world data as a data ID.

Does that explain it? smile

anonymous-user

84 months

Thursday 1st April 2010
quotequote all
Er, no not really.
Not everything needs an ID. Sometimes the data is the ID otherwise you end up with:
ID: 19
Name: "ID 19"
If I've understood your point correctly.
Mike

Edited by MH on Thursday 1st April 14:58

cs02rm0

13,966 posts

221 months

Thursday 1st April 2010
quotequote all
But that way you have the flexibility to change the real world data without screwing up how you access it.

ewenm

28,506 posts

275 months

Thursday 1st April 2010
quotequote all
cs02rm0 said:
But that way you have the flexibility to change the real world data without screwing up how you access it.
Absolutely.

In my experience the client says the data is set in stone, will never change, then 6 months later they're changing the data. Keep the IDs in place.

Size Nine Elm

5,167 posts

314 months

Thursday 1st April 2010
quotequote all
ewenm said:
cs02rm0 said:
But that way you have the flexibility to change the real world data without screwing up how you access it.
Absolutely.

In my experience the client says the data is set in stone, will never change, then 6 months later they're changing the data. Keep the IDs in place.
Indeed - even where real-world data will never change, and never be duplicated... because it will. Somehow, somewhere...

JohnnyPanic

Original Poster:

1,282 posts

239 months

Thursday 1st April 2010
quotequote all
All makes sense to me, thanks guys.

I've always had an ID field in my database tables, it just seems the safest way of doing it, and I don't expect the overhead is that great. It generally just makes your SQL statements a bit more involved.

And you're right, the most fixed of fixed data inevitable ends up needing some kind of tweak at some point. That's clients for you.

Anyhow, glad I'm still going about things the right way smile

Thurbs

2,782 posts

252 months

Thursday 1st April 2010
quotequote all
I echo the comments about link tables. I have also moved on from ID's, especially database indexed ones. GUID is the future. Fact!

TooLateForAName

4,933 posts

214 months

Thursday 1st April 2010
quotequote all
MH said:
Er, no not really.
Not everything needs an ID. Sometimes the data is the ID otherwise you end up with:
ID: 19
Name: "ID 19"
If I've understood your point correctly.
Mike

Edited by MH on Thursday 1st April 14:58
Please tell me that you don't work in DB design.

Size Nine Elm

5,167 posts

314 months

Thursday 1st April 2010
quotequote all
Thurbs said:
I echo the comments about link tables. I have also moved on from ID's, especially database indexed ones. GUID is the future. Fact!
I echo that - specially if you've moved on from databases... smile

anonymous-user

84 months

Thursday 1st April 2010
quotequote all
Not meaning to labour the point, but I will, I understand all that generalisation about changing data, entity life histories, using IDs etc etc. However the original post was specific (not a general question about holding many to many relationships) about adding tags to posts not creating a relationship between major entities.

As I understand it the OP just wants a way to add multiple textual tag attributes to a post. It is the text (i.e. TagName) of tag that is the important bit not the id of the tag in some other table. Based on the examples given you can't change the TagName for TagId 1 from 'Porsche' to 'Porsche 356' - it completely changes the meaning of the tag - that is my whole point. You will end up with all 'Porsche' posts tagged as 'Porsche 356' which would clearly be wrong!! A post originally tagged as 'Porsche' by the poster should remain that way and not change if someone else edits the Tag table. We aren't talking about things like customer references and customer names here where if a customer changes their name they are still the same customer.

Unless of course the designer has some genuine reason for making it different why complicate it unnecessarily?

GUIDs - you having a laugh?

Mike

RacingPete

9,203 posts

234 months

Thursday 1st April 2010
quotequote all
Thurbs said:
I echo the comments about link tables. I have also moved on from ID's, especially database indexed ones. GUID is the future. Fact!
Very interesting! I say it has its place but is definitely not a total replacement of IDs, for example take your classifieds here. The performance implications would be massive for the extra 4 times storage and the execution time to join the larger indexes. Also having a readable ID for the adverts makes things much easier when dealing with enquiries, so if we have to have a unique ID then we might as well index on it too.

cs02rm0

13,966 posts

221 months

Thursday 1st April 2010
quotequote all
Some people use a sequential part of the GUID to get around the performance implications. Not sure I see the point of it then, other than to ease a data migration maybe.

I'm not a fan, in the context of database IDs, really.

130R

7,111 posts

236 months

Thursday 1st April 2010
quotequote all
I'm not a DBA but ... I have worked with Oracle a lot and always use a sequence with a trigger for the ID. When refactoring you are fine because the ID is meaningless.

Silver993tt

9,064 posts

269 months

Thursday 1st April 2010
quotequote all
130R said:
I'm not a DBA but ... I have worked with Oracle a lot and always use a sequence with a trigger for the ID. When refactoring you are fine because the ID is meaningless.
It all depends on what platform you're working with. Some are hash based (for the larger systems/Data Warehouses), so IDs/System generated keys are to avoided and real data should be used as the hash value.

Of course before any of this is looked at a logical model is fundamental since it reflects the business.

Edited by Silver993tt on Thursday 1st April 17:14