script for renaming constraints.
A reader, February 01, 2005 - 12:40 pm UTC
This script renames all pks and fk's in the given schema.
(Thanks to Tom as I have used his idea and code
to write this script.)
To run this sciprt make sure you dont' have same column name
in two table.
I have tested this on the tables where the
column names are like,...
tbl_col_name varchar2(..)
tbl_col_name number...
type of conventions.
---------------------------------------------------------------
nydev168>SELECT nvl( uc.constraint_type,'Total') cons_type,
2 COUNT (*) cnt
3 FROM user_constraints uc
4 GROUP BY rollup(uc.constraint_type);
nydev168>
CREATE TABLE cons_def (ID NUMBER,table_name VARCHAR2(30),DDL VARCHAR2(4000));
truncate table cons_def;
CREATE SEQUENCE seq_cons START WITH 1 INCREMENT BY 1 NOCYCLE NOCACHE NOMAXVALUE;
SELECT nvl( uc.constraint_type,'Total') cons_type,
COUNT (*) cnt
FROM user_constraints uc
GROUP BY rollup(uc.constraint_type);
-- create a list of refrences.. fk- ddl s
DECLARE
my_stmt VARCHAR2 (4000);
BEGIN
FOR x IN (SELECT UPPER (table_name) NAME
FROM user_tables
ORDER BY 1)
LOOP
my_stmt := NULL;
for y in( select
'alter table "' || child_tname || '"' || chr(10) ||
'add constraint "' || child_cons_name || '"' || chr(10) ||
'foreign key ( ' || child_columns || ' ) ' || chr(10) ||
'references "' || parent_tname || '" ( ' || parent_columns ||
') '|| decode(DEFERRED ,'DEFERRED','DEFERRABLE INITIALLY DEFERRED',' ') fkey , x.name
from
( select a.table_name child_tname, a.constraint_name child_cons_name,
b.r_constraint_name parent_cons_name, b.deferred,
max(decode(position, 1, '"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position, 2,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position, 3,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position, 4,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position, 5,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position, 6,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position, 7,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position, 8,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position, 9,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position,10,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position,11,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position,12,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position,13,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position,14,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position,15,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position,16,', '||'"'||
substr(column_name,1,30)||'"',NULL)) child_columns
from user_cons_columns a, user_constraints b
where a.constraint_name = b.constraint_name
and b.constraint_type = 'R'
group by a.table_name, a.constraint_name, b.r_constraint_name,b.deferred ) child,
( select a.constraint_name parent_cons_name, a.table_name parent_tname,
max(decode(position, 1, '"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position, 2,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position, 3,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position, 4,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position, 5,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position, 6,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position, 7,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position, 8,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position, 9,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position,10,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position,11,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position,12,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position,13,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position,14,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position,15,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position,16,', '||'"'||
substr(column_name,1,30)||'"',NULL))
parent_columns
from user_cons_columns a, user_constraints b
where a.constraint_name = b.constraint_name
and b.constraint_type in ( 'P', 'U' )
group by a.table_name, a.constraint_name ) parent
where child.parent_cons_name = parent.parent_cons_name
and parent.parent_tname = x.name)
loop
INSERT INTO cons_def
(ID,
table_name,
DDL
)
VALUES (seq_cons.NEXTVAL,
y.NAME,
y.fkey
);
end loop;
END LOOP;
commit;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.put_line ('Error in creating DDL: ' || SQLERRM);
RAISE;
END;
/
select count(*) from cons_def;
----------------------------------------------------------------------------------------------------
-- disable all the fk constraints. for pk
BEGIN
FOR x IN (SELECT uc.constraint_name,
uc.constraint_type,
uc.table_name
FROM user_constraints uc
WHERE uc.constraint_type = 'R'
ORDER BY 2)
LOOP
EXECUTE IMMEDIATE ' alter table '
|| x.table_name
|| ' disable constraint '
|| x.constraint_name;
END LOOP;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.put_line ('Error in Disabling the FK: ' || SQLERRM);
RAISE;
END;
/
SELECT nvl( uc.constraint_type,'Total') cons_type,
COUNT (*) cnt
FROM user_constraints uc
GROUP BY rollup(uc.constraint_type);
--- recreate the pks and unique constriants...
BEGIN
FOR x IN ( select uc.constraint_name,decode(uc.DEFERRED ,'DEFERRED','DEFERRABLE INITIALLY DEFERRED',' ') dfrd,
uc.table_name,uc.constraint_type,
max(decode(position, 1, '"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position, 2,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position, 3,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position, 4,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position, 5,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position, 6,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position, 7,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position, 8,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position, 9,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position,10,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position,11,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position,12,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position,13,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position,14,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position,15,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position,16,', '||'"'||
substr(column_name,1,30)||'"',NULL)) col_list
FROM user_constraints uc,
user_cons_columns ucc
WHERE uc.constraint_type IN ('P', 'U')
AND uc.constraint_name = ucc.constraint_name
GROUP BY uc.constraint_name,decode(uc.DEFERRED ,'DEFERRED','DEFERRABLE INITIALLY DEFERRED',' '),
uc.table_name,uc.constraint_type
ORDER BY 2)
LOOP
IF (x.constraint_type = 'P') THEN
EXECUTE IMMEDIATE ' Alter table '
|| x.table_name
|| ' Drop primary key cascade';
EXECUTE IMMEDIATE ' Alter table '
|| x.table_name
|| ' Add Constraint PK_'
|| TRIM (SUBSTR (x.table_name, 1, 27))
|| ' PRIMARY KEY ('
|| x.col_list
|| ') '|| x.dfrd ;
ELSE
EXECUTE IMMEDIATE ' Alter table '
|| x.table_name
|| ' Drop constraint '
|| x.constraint_name;
EXECUTE IMMEDIATE ' Alter table '
|| x.table_name
|| ' Add CONSTRAINT UNQ_'
|| trim(substr(x.table_name,1,26)) || ' UNIQUE( '
|| x.col_list
||' ) '|| x.dfrd ;
END IF;
END LOOP;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.put_line ('Error in creating the PK/unq: ' || SQLERRM);
RAISE;
END;
/
-----------------------------------------------------------------------------------------------------------
-- recreate the fks
BEGIN
FOR x IN (SELECT *
FROM cons_def
ORDER BY 1)
LOOP
BEGIN
EXECUTE IMMEDIATE x.DDL;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.put_line ('recreating ****'|| sqlerrm ||' ----:-----'|| x.DDL);
END;
END LOOP;
END;
/
SELECT nvl( uc.constraint_type,'Total') cons_type,
COUNT (*) cnt
FROM user_constraints uc
GROUP BY rollup(uc.constraint_type);
---------------------------------------------------------------------------------------------------------
-- change the name of fks
DECLARE
my_stmt VARCHAR2 (4000);
BEGIN
FOR x IN (SELECT UPPER (table_name) NAME
FROM user_tables
where table_name not like ('%_STAGE')
ORDER BY 1)
LOOP
my_stmt := NULL;
for y in( select
'alter table "' || child_tname || '"' || chr(10) ||
'add constraint "' || 'FK_' || substr(trim('"' from child_columns),1,16)||'_'|| substr(trim('"' from parent_columns),1,7 + 14 -length(substr(trim('"' from child_columns),1,14)))|| '"' || chr(10) ||
'foreign key ( ' || child_columns || ' ) ' || chr(10) ||
'references "' || parent_tname || '" ( ' || parent_columns ||
') '|| decode(DEFERRED ,'DEFERRED','DEFERRABLE INITIALLY DEFERRED',' ') fkey , x.name,child.child_cons_name ,child_tname
from
( select a.table_name child_tname, a.constraint_name child_cons_name,
b.r_constraint_name parent_cons_name,b.deferred,
max(decode(position, 1, '"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position, 2,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position, 3,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position, 4,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position, 5,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position, 6,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position, 7,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position, 8,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position, 9,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position,10,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position,11,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position,12,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position,13,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position,14,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position,15,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position,16,', '||'"'||
substr(column_name,1,30)||'"',NULL)) child_columns
from user_cons_columns a, user_constraints b
where a.constraint_name = b.constraint_name
and b.constraint_type = 'R'
group by a.table_name, a.constraint_name, b.r_constraint_name,b.deferred ) child,
( select a.constraint_name parent_cons_name, a.table_name parent_tname,
max(decode(position, 1, '"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position, 2,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position, 3,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position, 4,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position, 5,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position, 6,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position, 7,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position, 8,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position, 9,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position,10,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position,11,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position,12,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position,13,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position,14,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position,15,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position,16,', '||'"'||
substr(column_name,1,30)||'"',NULL))
parent_columns
from user_cons_columns a, user_constraints b
where a.constraint_name = b.constraint_name
and b.constraint_type in ( 'P','U' )
group by a.table_name, a.constraint_name ) parent
where child.parent_cons_name = parent.parent_cons_name
and parent.parent_tname = x.name)
loop
begin
EXECUTE IMMEDIATE ' Alter table '
|| y.child_tname
|| ' Drop constraint '
|| y.child_cons_name;
exception
when others then
dbms_output.put_line(
'Error with stmt : '|| sqlerrm || ' :'
||' Alter table '
||y.child_tname
|| ' Drop constraint '
|| y.child_cons_name
);
end;
begin
EXECUTE IMMEDIATE y.fkey;
exception
when others then
dbms_output.put_line('**Error : '|| sqlerrm || ' :'||y.fkey);
end;
end loop;
end loop;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.put_line ('Error in creating DDL: ' || SQLERRM);
RAISE;
END;
/
SELECT nvl( uc.constraint_type,'Total') cons_type,
COUNT (*) cnt
FROM user_constraints uc
GROUP BY rollup(uc.constraint_type) ;