如何使两个3列在SQL服务器中是唯一的?

6 sql sql-server

我想在我的数据表中使三列是唯一的

-----------------------------------
 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, 
---------------------------------
Run Code Online (Sandbox Code Playgroud)

我怎样才能在SQL Server中实现这一目标?

jpw*_*jpw 5

您可以添加唯一约束:

ALTER TABLE [TableName] ADD CONSTRAINT  [constraintName] UNIQUE ([columns])
Run Code Online (Sandbox Code Playgroud)

您可以在此处阅读文档.


Kas*_*Kas 5

这段代码可以满足您的需求.

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]
Run Code Online (Sandbox Code Playgroud)