snippetMinor
Create a Voting database Scheme
Viewed 0 times
databaseschemevotingcreate
Problem
I have the following tables:
I would like to register Votes made by Users in Places and Posts.
Each Vote has one Value and the CreatedDate so I can track the votes by time.
Which scheme would you advice for this case? A two table scheme:
Or three tables:
Where PostsVotes and PlacesVotes work only as a junction table.
In this moment I am voting only Places and Posts but I will need to add votes to other tables in the future.
A user can vote for multiple places and multiple posts. However can only vote once for each place or each post.
create table dbo.Places (
Id int identity not null primary key clustered (Id),
Name nvarchar (100) not null
)
create table dbo.Posts (
Id int identity not null primary key clustered (Id),
Name nvarchar (100) not null
)
create table dbo.Users (
Id int identity not null primary key clustered (Id),
Name nvarchar (100) not null
)I would like to register Votes made by Users in Places and Posts.
Each Vote has one Value and the CreatedDate so I can track the votes by time.
Which scheme would you advice for this case? A two table scheme:
create table dbo.PlacesVotes (
PlaceId int not null,
UserId int not null,
CreatedDate datetime not null,
Value int not null
)
create table dbo.PostsVotes (
PostId int not null,
UserId int not null,
CreatedDate datetime not null,
Value int not null
)Or three tables:
create table dbo.PlacesVotes (
PlaceId int not null,
UserId int not null,
VoteId int not null
)
create table dbo.PostsVotes (
PostId int not null,
UserId int not null,
VoteId int not null
)
create table dbo.Votes (
Id int not null,
CreatedDate datetime not null,
Value int not null
)Where PostsVotes and PlacesVotes work only as a junction table.
In this moment I am voting only Places and Posts but I will need to add votes to other tables in the future.
A user can vote for multiple places and multiple posts. However can only vote once for each place or each post.
Solution
Assuming a person can only submit one and only one vote, you just need two tables,
This way, the voting event is represented as one entity.
PlaceVotes and PostVotes, as followscreate table dbo.PlaceVotes (
Id int not null,
PlaceId int not null,
UserId int not null,
CreatedDate datetime not null,
Value int not null,
constraint unique index ux_userid (UserId, place),
constraint primary key pk_placevotes(id)
)
create table dbo.PostVotes (
Id int not null,
PostId int not null,
UserId int not null,
CreatedDate datetime not null,
Value int not null,
constraint unique index ux_userid (UserId, postid),
constraint primary key pk_postvotes(id)
)This way, the voting event is represented as one entity.
Code Snippets
create table dbo.PlaceVotes (
Id int not null,
PlaceId int not null,
UserId int not null,
CreatedDate datetime not null,
Value int not null,
constraint unique index ux_userid (UserId, place),
constraint primary key pk_placevotes(id)
)
create table dbo.PostVotes (
Id int not null,
PostId int not null,
UserId int not null,
CreatedDate datetime not null,
Value int not null,
constraint unique index ux_userid (UserId, postid),
constraint primary key pk_postvotes(id)
)Context
StackExchange Database Administrators Q#146557, answer score: 2
Revisions (0)
No revisions yet.