SQL is a linking/bridge table needed?
I currently have two joined tables in a SQL Server database, one with news items (newsTab) and a table (usrCom) which captures multiple user comments for each article.
I want to add another information table (infoTab) which will also allow users to comment but I wish to use the existing comments table (usrCom) to store these.
How do I go about creating a linking/bridge tabl开发者_StackOverflow中文版e which generates a unique ID so I know which table (newsTab/infoTab) the comments belong to.
NB: I've edited the message to hopefully make it a bit clearer
First Table (newsTab)
NewsId NewsContent
---------------------
1 blah blah
2 and so on
Second Table (infoTab)
infoId InfoContent
---------------------
1 some info
2 more stuff
Comments Table (as is currently)
commentId linksTo Comment
------------------------------
1 1 user input
2 1 random rant
'linksTo' is a foriegn key to the items in the first table, so I cannot just add a type column and I would get conflicts on the foreign key column if a row did not exist in the new table with the same ID. So hence the need for a new generated foreign key based upon the table and Id that was being commented on.
Why not just a "type" column to the usrCom
If you auto-generate commentId (set as primary key) then you won't get any duplicates in the usrCom table. Also, if you want to keep one table for comments for both news and info tables, you should add a new column to distinguish which comments are for which tables (call it type). Then in your queries, make sure you specify the type to filter your results.
To view all comments for newsTab:
select * from usrCom
where type='newsTab'
精彩评论