Insertar en ignorar duplicados que no son la clave principal

I'm trying to find a query for sql that will just insert values but not do it should the value exist. Now ive seen alot of examples but they all rely on primary keys or table to table moves. I just want to add a new row in the table and assuming that one of the collumns doesnt have the same value add it. I know the following wont work but its as close to I think it would be and might just clear it up if my writting is not enough.

    INSERT INTO table (txtLastName,txtEmail,txtZip)
    Values ('Tester','','12345')
    WHERE txtLastName <> 'Tester'
    or WHERE txtEmail <> ''
    or WHERE txtZip <> '12345'

Using MS SQL Server.

preguntado el 09 de marzo de 12 a las 17:03

I'm not trying to make 3 primary keys. I just don't want it inserted if one of those three columns already has the value. -

Please let us know the RDBMS you are using -

2 Respuestas

Debes crear un Unique Constraint composed by the three fields (txtLastName, txtEmail, txtZip).

The links directs you to SQL Server docs, but the concept of unique constraint is RDBMS universal.

Just beware that when you create a Unique Constraint, your duplicate insert will not just fail silently: it will throw an error saying the insert tried to violate the unique constraint. And, of course, it should do that! Make sure your code handles that exception.

respondido 09 mar '12, 17:03

Would something like this help with a table that is already made? - alsu3

Yes, it would. You should, however, search for duplicates before creating the constraint. Some RDBMS won't let you create it unless the duplicates are removed. What's the RDBMS again? - Adriano Carneiro

Looking at that link how would this help in stopping the insert. INSERT INTO tbl(txtLastName,txtEmail,txtZip) WHERE unique Values ('Tester','','12345') - alsu3

I'm sorry, that's not how it works. A unique constraint is something you have to declare for the table. Think of it as a DB structure rule you attach to the table. After that, you just run the plain simple insert (INSERT INTO...VALUES). Since you have todavía no told us the RDBMS (SQL Server? Oracle? MySQL?), I could not post the code for the creation of the UNIQUE CONSTRAINT. - Adriano Carneiro

Prueba esto:

INSERT INTO table (txtLastName,txtEmail,txtZip) 
SELECT 'Tester','','12345'
                    FROM table
                   WHERE txtLastName = 'Tester' 
                     AND txtEmail = '' 
                     AND txtZip = '12345' 

respondido 09 mar '12, 21:03

Just added the values 11 times. - alsu3

This is a good query for this but you really need the unique constraint as a safeguard against someone changing the data or doing an insert using differnt code. - HLGEM

@alsu3: Were there 11 rows in the table to begin with? meaning, you now have 22? I see now that my query would insert AS MANY ROWS as there already are! Oops! Fixed... - n8wrl

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.