The UNIQUE constraint ensures that all values in a column are different. Both the UNIQUE and PRIMARY KEY constraints provide a guarantee for uniqueness for a column or set of columns. A PRIMARY KEY constraint automatically has a UNIQUE constraint. However, you can have many UNIQUE constraints per table, but only one PRIMARY KEY constraint […]
Tag: unique
How to find repetitive records and keep only 1 record for uniqueness in SQL
–STEP 1: Find Repetitive Records (Only the Active Ones) SELECT INDEX1, INDEX2 INTO #TEMP_TABLE FROM MY_TABLE WHERE ACTIVE = 1 GROUP BY INDEX1, INDEX2 HAVING COUNT(1) > 1 ORDER BY COUNT(1) DESC –STEP 2: Create the Cursor DECLARE @INDEX1 INT DECLARE @INDEX2 INT DECLARE @ID INT DECLARE MY_CURSOR CURSOR FOR SELECT INDEX1, INDEX2 FROM #TEMP_TABLE […]