[ORACLE] find pk without index

|
01---------------------------------------------------------------------------
02-- title : find pk without index
03-- author : proud
04-- discription : find pk without (unique) index - drop and readd
05-- usage : @find_pk_without_idx
06-- created : 2010.02.02
07-- update date:
08---------------------------------------------------------------------------
09 
10set serveroutput on
11 
12drop table result_pk_without_idx;
13 
14create table result_pk_without_idx
15(
16 script_text varchar2(4000)
17)
18nologging
19compress
20;
21 
22declare
23v_drop_sql varchar2(4000);
24v_add_sql varchar2(4000);
25v_own varchar2(4000);
26v_tab varchar2(4000);
27v_con varchar2(4000);
28v_idx varchar2(4000);
29v_col varchar2(4000);
30v_cnt pls_integer;
31 
32begin
33 
34dbms_output.enable(9999999);
35 
36for i in (
37         select a.owner
38               ,a.table_name
39               ,a.constraint_name
40               ,a.index_name
41           from dba_constraints a
42               ,dba_indexes b
43          where a.index_name = b.index_name(+)
44            and a.constraint_type = 'P'
45            and b.index_name is null
46         ) loop
47v_own := i.owner;
48v_tab := i.table_name;
49v_con := i.constraint_name;
50v_idx := i.index_name;
51 
52v_drop_sql := 'alter table '||v_own||'.'||v_tab||' drop constraint '||v_con;
53v_add_sql := 'alter table '||v_own||'.'||v_tab||' add constraint '||v_con||' primary key (';
54 
55v_cnt := 0;
56 
57begin
58for c in (
59         select column_name
60           from dba_cons_columns
61          where owner = ''||v_own||''
62            and table_name = ''||v_tab||''
63          order by position
64         ) loop
65 
66v_col := c.column_name;
67 
68if v_cnt > 0 then
69v_add_sql := v_add_sql||','||v_col;
70else
71v_add_sql := v_add_sql||v_col;
72end if;
73 
74v_cnt := v_cnt + 1;
75 
76end loop;
77end;
78 
79v_add_sql := v_add_sql||')';
80 
81execute immediate 'insert into result_pk_without_idx values ('''||v_drop_sql||''')';
82execute immediate 'insert into result_pk_without_idx values ('''||v_add_sql||''')';
83execute immediate 'commit';
84end loop;
85 
86dbms_output.put_line('select * from result_pk_without_idx;');
87end;
88/
And