Is there a way to insert into an SQL database where the whole record is unique? I know you can make primary keys and unique columns, but that is not what I want.
What is the best way of doing this without overloading the database? I have seen a sort of subquery where you use "WHERE NOT EXISTS ()"
I just want to know the most efficient way of inserting a record so that there are no duplicates of the EXACT same record.
Thank you in advance!
You could add a unique constraint to the table definition and include all columns (except the primary key, I believe). Unique constraints create indexes, so I have no idea what sort of performance impact this might have, but I'd guess the fewer columns, the better.
This will add such a constraint to an existing table:
Note that there are certain restrictions on column types etc. so this may or may not work for your table. See Books Online for details: http://msdn.microsoft.com/en-us/library/ms177420(sql.90).aspx
You know how to make a unique index on a column.
Just make that on all the columns that should, together, be unique:
Note that the need to do this may be an indication that your table is not sufficiently normalized.
You could do:
Run the bottom half twice and it won't insert the record more then once.
Your question was pretty clear (not sure why others answered with something you'd specifically wanted to exclude), and something I'd spent over an hour trying to find ...
This link was most helpful http://www.timrosenblatt.com/blog/2008/03/21/insert-where-not-exists/
On mySQL 5.1.35 ...