Pergunta

Eu tinha uma restrição em uma mesa


CREATE TABLE "USERSAPPLICATIONS" (
    "USERID" NUMBER NOT NULL ,
    "APPLICATIONNAME" VARCHAR2 (30) NOT NULL ,
 CONSTRAINT "PK_USERSAPPLICATIONS" PRIMARY KEY ("USERID","APPLICATIONNAME") 
) 
/

Há duas semanas, modifiquei a tabela, adicionei algumas colunas, excluí a restrição "pk_usersapplications" e adicionei uma chave substituta. Eu posso ver no Oracle SQL Developer que a restrição PK_USERSAPPLICAÇÕES não existe mais.

Independentemente disso, quando tento adicionar duas entradas com a mesma combinação do UserID/ApplicationName, recebo um erro


SQL Error: ORA-00001: unique constraint (ACCOUNTMP1.PK_USERSAPPLICATIONS) violated
00001. 00000 -  "unique constraint (%s.%s) violated"
*Cause:    An UPDATE or INSERT statement attempted to insert a duplicate key.
           For Trusted Oracle configured in DBMS MAC mode, you may see
           this message if a duplicate entry exists at a different level.
*Action:   Either remove the unique restriction or do not insert the key.

Quando eu executo a declaração


SELECT *
FROM   user_cons_columns
WHERE  constraint_name = 'PK_USERSAPPLICATIONS' 

Eu recebo zero linhas. Como pode ser? O Oracle não deve ter nenhum conhecimento da restrição PK_USERSAPPLICAÇÕES, pois já foi excluída há semanas, e também não posso vê -la no banco de dados.

Foi útil?

Solução

Você ainda tem o índice usado por essa restrição? Porque a menos que você tenha incluído o DROP INDEX Cláusula Quando você soltou a restrição, ela ainda estará lá. Começar com

SELECT * 
FROM   user_indexes
WHERE  index_name = 'PK_USERSAPPLICATIONS'  
/

Alternativamente,

select index_name 
from user_indexes
where table_name = 'USERSAPPLICATIONS'
and  uniqueness='UNIQUE' 
/

ou

select index_name 
from user_ind_columns
where table_name = 'USERSAPPLICATIONS'
and  column_name in ('USERID' ,'APPLICATIONNAME')  
/

editar

Prova de conceito

SQL> create table t23 (id number not null, alt_key varchar2(10) not null)
  2  /

Table created.

SQL> create unique index t23_idx on t23 (id)
  2  /

Index created.

SQL> alter table t23 add constraint t23_pk primary key (id) using index
  2  /

Table altered.

SQL> insert into t23 values (1, 'SAM I AM')
  2  /

1 row created.

SQL> insert into t23 values (1, 'MR KNOX')
  2  /
insert into t23 values (1, 'MR KNOX')
*
ERROR at line 1:
ORA-00001: unique constraint (APC.T23_PK) violated

SQL>

Então a restrição funciona. O que acontece se o desistirmos, sem a cláusula de índice de queda?

SQL> alter table t23 drop constraint t23_pk
  2  /

Table altered.

SQL> insert into t23 values (1, 'MR KNOX')
  2  /
insert into t23 values (1, 'MR KNOX')
*
ERROR at line 1:
ORA-00001: unique constraint (APC.T23_IDX) violated


SQL>

Observe a mudança sutil na mensagem de erro. A segunda falha faz referência ao nome do índice, enquanto a mensagem original referenciou a restrição. Se o nome do índice for o mesmo que o nome da restrição, seria difícil diagnosticar isso.

Se você não criar explicitamente o comportamento padrão do índice exclusivo do Oracle, é criar um índice não único. Consequentemente, abandonar a restrição sem abandonar o índice não causa esse problema. (Advertem esse comportamento para 11g. Presumo - mas não tenho certeza - que também seja assim nas versões anteriores).

Outras dicas

Tente verificar o índice para essas colunas. Em alguns casos

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top