Connecting Tech Pros Worldwide Help | Site Map

Preventing duplicate rows in a MySQL table

Newbie
 
Join Date: May 2007
Posts: 6
#1: May 30 '07
Hello Sir,

I have a table with userID and articleID, now if a user access an article it is stored in this table, how do i write a query where no duplicate entry for article viewed by a user is store. after that i need total count of a user to fetch and store in another table. Can anyone help me writing a query.

Thanks in advance
Prateek Batabyal
pbmods's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Posts: 5,435
#2: May 30 '07

re: Preventing duplicate rows in a MySQL table


Simply create a unique key for your table. Something along the lines of:

Expand|Select|Wrap|Line Numbers
  1. ALTER TABLE `tablename` ADD UNIQUE KEY (`userID`, `articleID`);
Not only will this index your table by userID, making searches faster, but it will also prevent inserting a duplicate userID-articleID pair.

http://dev.mysql.com/doc/refman/5.0/en/alter-table.html
pbmods's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Posts: 5,435
#3: May 30 '07

re: Preventing duplicate rows in a MySQL table


Changed thread title to better match contents.

Moving to the MySQL forum...
Reply