简体   繁体   English

SQL多个唯一列

[英]SQL multiple unique columns

Is it possible to use unique on multiple columns? 可以在多个列上使用唯一性吗?

like: 喜欢:

 user_vote user_id 
 ------------------
      1         1
      1         2
      2         1 

both unique 既独特

This must be possible 这一定有可能

But: 但:

  user_vote user_id
     1         2
     1         2 

This must not be possible 这一定不可能

You can add a unique constraint on the column's combination: 您可以在列的组合上添加唯一约束:

ALTER TABLE my_table
ADD CONSTRAINT my_table_uq UNIQUE (user_vote, user_id)

MySQL / SQL Server / Oracle / MS Access: MySQL / SQL Server / Oracle / MS Access:

 CREATE TABLE uservotetable
 (
   user_vote int NOT NULL,
   user_id int NOT NULL,
 CONSTRAINT uservote UNIQUE (user_vote ,user_id)
  );

and if you created your table before ..then you can use ALTER 如果您在..之前创建了表格,则可以使用ALTER

  ALTER TABLE uservotetable
  ADD CONSTRAINT uservote UNIQUE (user_vote ,user_id)

this can be useful for you sql_unique 这对您很有用sql_unique

You need to define a composite unique constraint. 您需要定义一个复合唯一约束。

SQL Server, One way to do that in SQL Server is by adding UNIQUE INDEX SQL Server,在SQL Server中执行此操作的一种方法是添加UNIQUE INDEX

ALTER TABLE table_name ADD UNIQUE INDEX (User_Vote, User_Id);

In Oracle, 在Oracle中,

ALTER TABLE table_name
ADD CONSTRAINT uc_1 UNIQUE (User_Vote, User_Id)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM