There is an Oracle DDL command which will rename a Foreign Key constraint:
SQL>ALTER TABLE <tablename> RENAME CONSTRAINT <foreign_key> TO <foreign_key_new_name>;
eg
SQL>ALTER TABLE emp RENAME CONSTRAINT SYS_C0017017 TO FK_EMP_DEPT;
I have written an Oracle stored procedure which shows how this can be done:
1)
CREATE OR REPLACE PROCEDURE rename_foreign_key(p_table_name IN VARCHAR2, p_foreign_key IN VARCHAR2, p_foreign_key_new VARCHAR2)
IS
l_action VARCHAR2(2000) := NULL;
CURSOR c_uc(p_table_name VARCHAR2, p_fk VARCHAR2, p_fk_new VARCHAR2)
IS
SELECT 'ALTER TABLE ' || uc.table_name || ' RENAME CONSTRAINT ' || uc.constraint_name || ' TO ' || p_fk_new
FROM user_constraints uc
WHERE uc.table_name = p_table_name
AND uc.constraint_name = p_fk
AND uc.constraint_type = 'R';
BEGIN NULL;
OPEN c_uc(p_table_name, p_foreign_key, p_foreign_key_new);
FETCH c_uc INTO l_action;
CLOSE c_uc;
EXECUTE IMMEDIATE l_action;
END;
/
2) Example: create a master table DEPT and a child table EMP
CREATE TABLE dept (
deptno NUMBER(4) PRIMARY KEY,
dname VARCHAR2(14),
loc VARCHAR2(13));
CREATE TABLE emp (
empno NUMBER(4) PRIMARY KEY,
ename VARCHAR2(10),
deptno NUMBER(2),
FOREIGN KEY (DEPTNO) REFERENCES DEPT(DEPTNO));
3) Find the generated name for the foreign key in the EMP table
SELECT constraint_name FROM user_constraints WHERE table_name = 'EMP' AND constraint_type = 'R' AND generated = 'GENERATED NAME';
--> SYS_C0017017
4) Rename SYS_C0017017 to FK_EMP_DEPT
SQL> EXEC rename_foreign_key('EMP','SYS_C0017017','FK_EMP_DEPT')
Monday, February 14, 2011
Renaming Foreign Keys
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment