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

时间:2021-01-19 04:27:34

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. Column B has unique name constraint.

A列是主键。 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.

但它正在检查是否存在任何一个约束而不是检查唯一键约束的组合。

3 个解决方案

#1


40  

Create a unique key on those columns

在这些列上创建唯一键

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

Oracle/PLSQL: Unique Constraints

Oracle / PLSQL:唯一约束

#2


6  

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约束的详细解释。

#3


0  

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)

#1


40  

Create a unique key on those columns

在这些列上创建唯一键

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

Oracle/PLSQL: Unique Constraints

Oracle / PLSQL:唯一约束

#2


6  

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约束的详细解释。

#3


0  

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)