I have a simple table in my SQL Server database. This table contains two columns: ID int, Name nvarchar(50)
. The ID
column is the primary key for my table.
I want the "Name
" column to be "(No Duplicates)
", like in Microsoft Access, But this column isn't the primary column. How could I do this?
To delete the duplicate rows from the table in SQL Server, you follow these steps: Find duplicate rows using GROUP BY clause or ROW_NUMBER() function. Use DELETE statement to remove the duplicate rows.
The SQL DISTINCT keyword, which we have already discussed is used in conjunction with the SELECT statement to eliminate all the duplicate records and by fetching only the unique records.
How to Eliminate Duplicate Values Based on Only One Column of the Table in SQL? In SQL, some rows contain duplicate entries in a column. For deleting such rows, we need to use the DELETE keyword along with self-joining the table with itself.
Add a unique constraint for that column:
ALTER TABLE Foo ADD CONSTRAINT UQ_Name UNIQUE (Name)
To add it through SQL Management Studio UI:
To handle a situation where a unique constraint violation occurs, see for error 2601.
This can also be done another way with the SSMS GUI if you prefer:
ADD CONSTRAINT
SQL script does.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With