How to make two 3 columns unique in SQL server?

I want the three columns to be unique in my data table

----------------------------------- Column A | Column B | Column C ---------------------------------- Kasun Cham Nimith ---------------------------------- Kasun Cham Rox - This row ok and must be allowed to add. ---------------------------------- Kasun Cham Nimith - but This row must not be allowed to add again, --------------------------------- 

How can I execute this on a SQL server?

+4
source share
3 answers

This code will do what you want.

 CREATE UNIQUE CLUSTERED INDEX index_name ON TABLE (col1,col2, col3) or CREATE UNIQUE NONCLUSTERED INDEX index_name ON TABLE (col1,col2 , col3) or ALTER TABLE [dbo].[TABLE] ADD CONSTRAINT UNIQUE_Table UNIQUE CLUSTERED ( col1, col2, col3 ) ON [PRIMARY] 
+5
source

You can add a unique restriction:

 ALTER TABLE [TableName] ADD CONSTRAINT [constraintName] UNIQUE ([columns]) 

You can read the documentation here .

+4
source

To deal with this problem when creating this table, you need to create a unique constraint for your A / B / C columns.

+1
source

Source: https://habr.com/ru/post/1487609/


All Articles