﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / Administering / SQL Server 2005  / Unique Constraint / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Thu, 23 May 2013 06:12:03 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Unique Constraint</title><link>http://www.sqlservercentral.com/Forums/Topic672664-146-1.aspx</link><description>Another suggestion is, backup the database and delete the duplicate rows so that it will remain single record instead of multiple dup records. You can easily remove dup rec if the table has a PK. Then add the UNIQUE INDEX/CONSTRAINT.</description><pubDate>Sat, 14 Mar 2009 12:24:54 GMT</pubDate><dc:creator>Susantha Bathige</dc:creator></item><item><title>RE: Unique Constraint</title><link>http://www.sqlservercentral.com/Forums/Topic672664-146-1.aspx</link><description>I think altering the table and adding a constraint with NO CHECK option is what you need.ALTER TABLE tblTable  WITH NO CHECK ADD CONSTRAINT uqConstraint UNIQUERegards,Sony Antony.</description><pubDate>Sat, 14 Mar 2009 01:37:09 GMT</pubDate><dc:creator>Sony Antony</dc:creator></item><item><title>RE: Unique Constraint</title><link>http://www.sqlservercentral.com/Forums/Topic672664-146-1.aspx</link><description>The other alternative put a where clause on your inserts that checks for the existance of those three values existing in the database already.</description><pubDate>Wed, 11 Mar 2009 21:26:51 GMT</pubDate><dc:creator>Lynn Pettis</dc:creator></item><item><title>RE: Unique Constraint</title><link>http://www.sqlservercentral.com/Forums/Topic672664-146-1.aspx</link><description>I think your best choice here is to rename the existing table, create a new table with the old name, migrate all the existing records into the newly-created table with a "select distinct ..." then put the unique constraint/index on the new table.  That way you've got the old data to refer to if necessary, although how you handle any other fields that you didn't show in your original posting will need to be addressed somehow.If there are additional fields that are different between the non-unique rows then the only way to handle it would be to somehow modify the three columns so each row does end up unique then add the unique constraint/index.  Again that's going to have to be something you work out based on your business requirements.</description><pubDate>Wed, 11 Mar 2009 21:15:36 GMT</pubDate><dc:creator>Glenn Dorling</dc:creator></item><item><title>RE: Unique Constraint</title><link>http://www.sqlservercentral.com/Forums/Topic672664-146-1.aspx</link><description>One way to implement this would be to build an INSTEAD OF INSERT Trigger or check constraint that will check for uniqueness. While you build check constraint, you might be using scalar function that would return 1 or 0 representing new record to be inserted is unique or duplicate. But that would very in effecient. Try using something else if you have some good option.~ IM</description><pubDate>Wed, 11 Mar 2009 20:36:30 GMT</pubDate><dc:creator>sayfrend</dc:creator></item><item><title>RE: Unique Constraint</title><link>http://www.sqlservercentral.com/Forums/Topic672664-146-1.aspx</link><description>I fully understand that using nocheck or ignore_dup_key clause wont help in creating a unique index/constraint. Just that i dont want to delete the existing data but want to make sure going forward all unique_recs(for a combination of col1,col2 and col3 ) is unique</description><pubDate>Wed, 11 Mar 2009 09:06:01 GMT</pubDate><dc:creator>ishaan99</dc:creator></item><item><title>RE: Unique Constraint</title><link>http://www.sqlservercentral.com/Forums/Topic672664-146-1.aspx</link><description>[quote][b]ishaan99 (3/10/2009)[/b][hr]Tried adding the ignore_dup_key clause also as in index below but somehow it errors out saying The CREATE UNIQUE INDEX statement terminated because a duplicate key was found for the object name................any suggestions please?CREATE UNIQUE INDEX UNC_ABC ON ABC(col1,col2,col3) WITH IGNORE_DUP_KEY[/quote]What are you trying to do here?You cannot create a unique index if there are duplicate entries no matter where IGNORE_DUP_KEY is off or on.</description><pubDate>Wed, 11 Mar 2009 08:22:38 GMT</pubDate><dc:creator>maechismo_8514</dc:creator></item><item><title>RE: Unique Constraint</title><link>http://www.sqlservercentral.com/Forums/Topic672664-146-1.aspx</link><description>Unique indexes don't work that way.  You were talking about a constraint.  Have you tried that?</description><pubDate>Wed, 11 Mar 2009 06:56:28 GMT</pubDate><dc:creator>GSquared</dc:creator></item><item><title>RE: Unique Constraint</title><link>http://www.sqlservercentral.com/Forums/Topic672664-146-1.aspx</link><description>Tried adding the ignore_dup_key clause also as in index below but somehow it errors out saying The CREATE UNIQUE INDEX statement terminated because a duplicate key was found for the object name................any suggestions please?CREATE UNIQUE INDEX UNC_ABC ON ABC(col1,col2,col3) WITH IGNORE_DUP_KEY</description><pubDate>Tue, 10 Mar 2009 13:29:10 GMT</pubDate><dc:creator>ishaan99</dc:creator></item><item><title>RE: Unique Constraint</title><link>http://www.sqlservercentral.com/Forums/Topic672664-146-1.aspx</link><description>Any reason to not just try it, on a test/dev copy of the database?I don't think it'll work, but I'm curious as to why you wouldn't just try it and find out.</description><pubDate>Tue, 10 Mar 2009 12:04:19 GMT</pubDate><dc:creator>GSquared</dc:creator></item><item><title>Unique Constraint</title><link>http://www.sqlservercentral.com/Forums/Topic672664-146-1.aspx</link><description>I have a table which has col1,col2,col3. Now if i group by on these columns there are duplicates existing ,keeping the existing data as it is, is it possible to add a unique constraint on these 3 columns. something like a constraint with nocheck clause. so that any future recs that gets added are unique.Any help on this will  be greatly appreciated. TIA</description><pubDate>Tue, 10 Mar 2009 11:52:21 GMT</pubDate><dc:creator>ishaan99</dc:creator></item></channel></rss>