views:

179

answers:

4

Is there a quick way to prevent insertion of repeated data into a table? I mean, the key will always be different but the rest of the entry could be repeated and by so, there would be 2+ different keys identifying the same data.

I could search the whole table but i am afraid of the performance lost when doing this.

Note: I'm just starting to learn SQL, please bear with me if this is a dumb question to you.

+10  A: 

You want a UNIQUE constraint on the table.

Joel Coehoorn
Also, you might want to canonicalize your data before sending it to the db. If there are multiple ways to represent the same thing, since the db won't know. For example, email addresses may contain html entities, capitalization, etc: FredWeasleyGMail.com == [email protected]
Wedge
A: 

Identification and removal of duplicate entries in databases is actually a topic of academic research. Besides "best-practices" there is hardly a possibility to give the one and true answer.

We also had this problem with company registration data like addresses, telephones etc. You just need to narrow down all possible variations of data and then implement simple checks. There is probably no (good) way to make these checks yield a perfect result on any data.

User
A: 

The UNIQUE-constraint takes care of preventing data from being inserted twice. Learn more about it in the documentation of your DBMS.

Patrick Daryll Glandien
+2  A: 

As Joel said you can use an UNIQUE constraint on several field where you would not want repeated data.

Also if you can make sure your primary key always identifies a unique record you should never run into this trouble.

ex: my personal record would allways be diferent from yours if we use my id card number as a primary key

Nuno Furtado
+1 for the idea of using actual PKs to identify rows
Tom H.