2

I want to know whether it is possible to avoid duplicate entries or data without any keys or group by statement

bruce
  • 31
  • 1
  • 6
  • can you provide some examples? – John Woo Sep 26 '14 at 05:15
  • 1
    By just simply not inserting dupe entries? – jpcanamaque Sep 26 '14 at 05:18
  • I had redesigned a table by merging 4 tables into 2 tables the problem now is primary keys used already for preventing duplication is shorted out hence the total functionality is becoming disaster duplication of entries while fetching the data from DB I am using inner join to fetch the data – bruce Sep 26 '14 at 05:30
  • Hence the question Is there is any other method to prevent duplicates??? – bruce Sep 26 '14 at 05:32

2 Answers2

2

Create Unique key constrait.

ALTER TABLE Comment ADD CONSTRAINT uc_Comment UNIQUE (CommentId, Comment)

In above case Comment duplication will not be done as we are creating the unique combination of COmmentId and Comment.

Hope this helps.

More info : http://www.w3schools.com/sql/sql_unique.asp OR

SQL Server 2005 How Create a Unique Constraint?

Community
  • 1
  • 1
Prashant Sarvaiya
  • 364
  • 1
  • 3
  • 14
1

If you want to suppress duplicates when querying, use SELECT DISTINCT.

If you want to avoid putting duplicates into a table, just don't insert records that are already there. It doesn't matter whether you have a primary/unique key: those will make the database not allow duplicate records, but it's still up to you to avoid trying to insert duplicates (assuming you want your queries to succeed).

You can use SELECT to find whether a record already exists before trying to insert it. Or, if you want to be fancy, you can insert the new records into a temporary table, use DELETE to remove any that are already present in the real table, then use INSERT ... SELECT to copy the remaining records from the temporary table into the real one.

Wyzard
  • 33,849
  • 3
  • 67
  • 87