Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exceptions into clause does not work when creating a constraint

Tags:

oracle

I work with Oracle 11g.
I have one table:

create table test (one number(2), two number(2));

There are 2 rows:

insert into test (one, two) values (1, 1);
insert into test (one, two) values (2, null);
commit;

Now I create an exceptions table:

create table exceptions(row_id rowid,
                       owner varchar2(30),
                       table_name varchar2(30),
                       constraint varchar2(30));

Now I want to create the primary key for test:

alter table test add constraint test_pk primary key (one, two) exceptions into exceptions;

Of course I get the following error: ORA-01449

But the row that caused the exception is not in the exception table?

Can anybody help me. Thanks in advance

Wolfgang

like image 808
Wolfgang Adamec Avatar asked Jun 26 '12 16:06

Wolfgang Adamec


People also ask

What is enable and disable constraints in Oracle?

You can use the ALTER TABLE statement to enable, disable, modify, or drop a constraint. When the database is using a UNIQUE or PRIMARY KEY index to enforce a constraint, and constraints associated with that index are dropped or disabled, the index is dropped, unless you specify otherwise.

How do I create a constraint in Oracle?

The syntax for creating a unique constraint using an ALTER TABLE statement in Oracle is: ALTER TABLE table_name ADD CONSTRAINT constraint_name UNIQUE (column1, column2, ... column_n); table_name.

Which exceptions are raised implicitly by Oracle?

Internal exceptions are raised implicitly by the run-time system, as are user-defined exceptions that you have associated with an Oracle error number using EXCEPTION_INIT . However, other user-defined exceptions must be raised explicitly by RAISE statements.


1 Answers

In order to do this, you must first create the constraint disabled:

ALTER TABLE test ADD CONSTRAINT test_pk PRIMARY KEY (one, two) DISABLE;

Then, enable the constraint with exceptions:

ALTER TABLE TEST ENABLE CONSTRAINT test_pk EXCEPTIONS INTO exceptions;

Then you can select the results:

SQL> SELECT * FROM EXCEPTIONS;

ROW_ID             OWNER TABLE_NAME CONSTRAINT
------------------ ----- ---------- ----------
AAHpV4AAHAAApliAAB XXX   TEST       TEST_PK
like image 157
DCookie Avatar answered Oct 11 '22 09:10

DCookie