Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 2 tables relation

Author  Topic 

shapper
Constraint Violating Yak Guru

450 Posts

Posted - 2007-03-15 : 12:07:19
Hello,

I have 3 SQL 2005: Documents, Categories and DocumentsInCategories
Documents PK is DocumentId
Categories PK is CategoryId

DocumentsInCategories is to be used as a way to relate each document to one or more categories. So in this table I am planning to have 2 columns: DocumentId and CategoryId

Is this a good way to do this?

And does the table DocumentsInCategories need to have a column named DocumentsInCategoriesId which would be its PK?
If not, how should I set my DocumentsInCategories PK?

Thanks,
Miguel

snSQL
Master Smack Fu Yak Hacker

1837 Posts

Posted - 2007-03-15 : 13:47:51
That is the accepted way to create a many-to-many relationship (which is what you have) and you don't need a separate column for the PK, the compound PK of CategoryId and DocumentId will be fine, and in fact needs to be unique anyway because you don't want a document added to the same category more than once.
Go to Top of Page

VincentFrandsen
Starting Member

39 Posts

Posted - 2007-03-16 : 06:56:42
I'd say you must set your primary key in documentsincategories.

Can a document have more than one category in your system if yes then that is ok.

If a document only has one category then there should be a reference to from docs to categories...

Vincent Fradnsen
Go to Top of Page

snSQL
Master Smack Fu Yak Hacker

1837 Posts

Posted - 2007-03-16 : 13:17:00
VincentFrandsen:
quote:
Can a document have more than one category in your system if yes then that is ok.


Original post:
quote:
DocumentsInCategories is to be used as a way to relate each document to one or more categories.
Go to Top of Page
   

- Advertisement -