简体   繁体   English

如何为Oracle中的列组合提供唯一约束?

[英]How to give a unique constraint to a combination of columns in Oracle?

I have a Table with 4 Columns 我有一个包含4列的表

Each Column will be A,B,C,D 每列将是A,B,C,D

Column A is the Primary key. A列是主键。 Column B has unique name constraint. B列具有唯一的名称约束。

Now I want to remove the unique constraint for column B and give a unique constraint by combining the columns B, C and D. So the table will allow only one row with a particular value in columns B,C and D. 现在我想删除B列的唯一约束,并通过组合B,C和D列来提供唯一约束。因此,该表将只允许在B,C和D列中具有特定值的一行。

How can I give this type of a constraint? 我怎样才能给出这种类型的约束?

I tried giving the composite unique key like : 我尝试给出复合唯一键,如:

ALTER TABLE TABLENAME ADD CONSTRAINT CONSTRAINT_NAME UNIQUE (COLUMN_B, COLUMN_C, COLUMN_D)

But it is checking whether any one of the constraint is present rather than checking for the combination of unique key constraint. 但它正在检查是否存在任何一个约束而不是检查唯一键约束的组合。

Create a unique key on those columns 在这些列上创建唯一键

ALTER TABLE YourTable
  add CONSTRAINT YourTable_unique UNIQUE (B, C, D);

Oracle/PLSQL: Unique Constraints Oracle / PLSQL:唯一约束

First of all you should drop an existing Constraint by using below ALTER Query. 首先,您应该使用下面的ALTER Query来删除现有的Constraint。

ALTER TABLE table_name
   DROP CONSTRAINT myUniqueConstraint;

Now, you can create a UNIQUE Constraint by using the keyword UNIQUE with the combination of required Columns. 现在,您可以使用关键字UNIQUE和所需列的组合来创建UNIQUE约束。

For Example: 例如:

ALTER TABLE table_name
   ADD CONSTRAINT myUniqueConstraint UNIQUE(B, C, D);

Detailed explanation of UNIQUE Constraint here. 这里有UNIQUE约束的详细解释。

ALTER TABLE table_name DROP CONSTRAINT constraint_name; ALTER TABLE table_name DROP CONSTRAINT constraint_name;

CREATE UNIQUE INDEX constraint_name ON table_name (B,C,D) CREATE UNIQUE INDEX constraint_name ON table_name(B,C,D)

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

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