Ok, Here is my problem.
I have a rating system that has 2 tables
User Table uID - Name 1 - Tom 2 - Mary Comment table cID - uID 1 - 1 2 - 3 3 - 3 Rating Table uID - cID - rate 1 - 2 - -1 2 - 2 - +1 3 - 1 - +1
Note:
-In User Table: uID
is primary key
-in CommentTable: cID
is primary key & uID
is foreign key.
-In Rating Table: (uID - cID
) is composite primary key. That means One user can not rate twice on same comment. Rate column has 2 options only +1
or -1
.
-Also, 1 User can rate many comments & 1 comment can be rated by many people. However, a person can not vote for his own comment. For ex, Tom (uID=1
created the comment cID=1
so there should not exist uID=1 & cID=1
in Rating table cos uID=1
can't rate his own cID=1
.
So there should not have any record like this in rating table
Rating Table uID - cID - rate 1 - 1 - +1 //illegal record
Requirement, when a user vote any comment the system should check whether the comment was created by that person. If it was then it should reject, otherwise it should insert uID - cID - rate
into Rating Table.
However, if the insert got uplicate then it should update only. For ex, Tom voted +1
for a comment2 (it will insert uID=1 - cID=2 - rate=+1
into Rating table) & then later he voted -1
for that same comment2 (it will update uID=1 - cID=2 - rate=-1
in Rating table rather than reject the vote)
Here is what I think
given uID, cID & rate
parameters
insert into Rating (uID, cID, rate) value (?,?,?) on Duplicate key Update rate=values(rate)
But how can we check the illegal record?
I prefer to have just 1 query that do these 3 things: "check if ID is legitimate for insert or update", if it is then "Insert", & if the record already there then "Update"?
I am using MYSQL 5.0