Search notes:

Oracle SQL: EXCEPTIONS INTO clause (Validation of constraints)

Enabling a primary key

The following example uses Oracle's SQL exceptions into clause to write the rowids of records, that violate the uniqueness of a primary key, into a table so that the data quality problem can then be fixed.
create table tq84_exceptions(
   row_id     rowid,
   owner      varchar2(30),
   table_name varchar2(30),
   constraint varchar2(30)
);

create table tq84_table_with_pk (
   id  number,
   txt varchar2(20)
);

alter table tq84_table_with_pk add constraint tq84_table_pk primary key (id) disable;

insert into tq84_table_with_pk values (1, 'one'      );
insert into tq84_table_with_pk values (2, 'two'      );
insert into tq84_table_with_pk values (3, 'three'    );
insert into tq84_table_with_pk values (1, 'one again');

commit;

-----------------------------------------------------

alter table tq84_table_with_pk enable constraint tq84_table_pk exceptions into tq84_exceptions;
--
-- ORA-02437: cannot validate (RENE.TQ84_TABLE_PK) - primary key violated

-- Show offending (two) records
--
select * from tq84_table_with_pk where rowid in (select row_id from tq84_exceptions);

drop table tq84_table_with_pk purge;
drop table tq84_exceptions    purge;
Github repository Oracle-Patterns, path: /DatabaseObjects/Tables/IntegrityConstraints/exceptions_into/enable_pk.sql

See also

An exception table can be created with $ORACLE_HOME/rdbms/admin/utlexcpt.sql.
The dbms_errlog package.
The log errors into clause.
Table constraints

Index

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/developm...', 1740464993, '18.116.50.186', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/development/databases/Oracle/objects/tables/constraints/exceptions-into(85): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78