I have a problem with my site. The site has multiple entities: Articles, Posts, Reviews … orund 6 types. Now I am introducing the possibility for a user to rate an item (it can be any of these entities)
I created a table Votes (int Id primary key, int ItemId, nvarchar(30) Ip, datetime Timestamp, int VoteValue). Here I will store all votes and their ips.
My problem is that I must have ItemID unique … but my database already have items of various types having the same id. All tables started the ids from 0. What options do you see for my design in order to be able to store all votes in a single table?
Your approach is trying to assign multiple meanings to the field “ItemId”, which will lead to the issues you are encountering. If I were to see “9500” in that field, how would I know what that means?
I would suggest dropping the ItemId field and creating “crosswalk” tables between Votes and the other entities.
For example, your entities:
… etc ….
Your votes table:
Your “crosswalk” tables:
Note that in your crosswalk tables, you would create a composite primary key consisting of both the FK references to the appropriate entities, thereby ensuring uniqueness.
In my experience, this is an appropriate normalized approach to the domain you describe.
In querying, to get the votes for the Articles (for example) simply INNER JOIN Articles through ArticleVotes to Votes. To get all Votes, simply query Votes.
Additionally, I would suggest creating an IPAddresses table and FKing to that in your Votes table to reduce redundancy.