Columnrename9i

  • 181 views
Uploaded on

oracle foreign key primary key constraints performance tuning MTS IOT 9i block size backup rman corrupted column drop rename recovery controlfile backup clone architecture database archives export …

oracle foreign key primary key constraints performance tuning MTS IOT 9i block size backup rman corrupted column drop rename recovery controlfile backup clone architecture database archives export dump dmp duplicate rows extents segments fragmentation hot cold blobs migration tablespace locally managed redo undo new features rollback ora-1555 shrink free space user password link TNS tnsnames.ora listener java shutdown sequence

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
181
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
0
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Renaming a Column in 9i Administration TipsHow do I rename a column in Oracle 9i?In Oracle 9i, you can now perform an in-place rename for any or all columns, and theentire process uses a new package called dbms_redefinition.Step 1: Find out whether the table you want to alter is capable of being altered. To dothat, you issue the following command:EXECUTE DBMS_REDEFINITION.CAN_REDEF_TABLE(‘SCHEMA’,’TABLE NAME’)...though you obviously enter appropriate values for the schema and table names there. Ifthat procedure doesn’t report any errors, then you can proceed with the next steps.Step 2: create a new table that looks like what you want the original table to be when theredefinition process is over (i.e., create it with columns using the new names, or take theopportunity to specify new storage parameters and so on). This is a ‘holding table’ whichwill eventually be swapped with the original. It must not contain any data -we areinterested merely in its definition. The holding table must exist within the sameschema as the original one being modified.Step 3: run the dbms_redefinition.start_redef_table package/procedure to describe thereal table, its holding table counterpart, and any column mapping information needed.For example:EXECUTE DBMS_REDEFINITION.START_REDEF_TABLE (‘SCOTT’,’EMP’,’HOLDING_EMP’, -‘EMPNO EMPLOYEE_NO’, -‘ENAME ENAME’, -‘MGR MANAGER_NO’…AND SO ON…)Note here how two columns are being re-named, but one of them isn’t. Also note the useof the minus sign at the end of each line -thats just a continuation character. Without it,the package/procedure will attempt to run before youve typed in the entire remappingdata.Step 4: run the dbms_redefinition.finish_redef_table procedure, supplying the schema, theoriginal table name and the holding table name as the parameters:EXECUTE DBMS_REDEFINITION.FINISH_REDEF_TABLE (‘SCOTT’,’EMP’,’HOLDING_EMP’)And at the end of that procedure, youll be able to select from the original table, and seethat the columns really have been changed.There are some nasties to watch out for when doing this, however.Copyright © Howard Rogers 2001 10/17/2001 Page 1 of 4
  • 2. Renaming a Column in 9i Administration TipsFirstly, any constraints that exist on the original table must be defined on the holdingtable, too, if you want them to be there at the end of the exercise. But, since this is alltaking place in the one schema, if you were to name your primary or key constraints forthe holding table the same as on the real table, the indexes that get created when suchconstraints are created would end up with the same name… so the constraint names mustbe different. (Incidentally, if you’ve any triggers or secondary indexes on the originaltable, they must be created on the holding table, too. Again, this means index nameschange).Second, any foreign key constraints you want preserved also have to be created on theholding table -but they must be set to be DISABLED (they get automatically enabled at theend of the process).Third, the mechanism Oracle uses to make this all happens is a materialized view. Thetrouble is, you can’t (as of 9.0.1) redefine a table on which a legitimate materialized viewhas been created (it produces an ORA-12091 error if you try it). That might not be aproblem, except that the entire redefinition process starts by taking a snapshot (i.e., amaterialized view) of the original table. What that means is that if anything goes wrongwith the redefinition (perhaps a syntax error in line 128 of the start_redef procedurescript), a materialized view has already been created on the source table. And what thatmeans is that a second attempt at redefinition, with the syntax fixed up, fails for the12091 reason. That is fairly easily fixed, though, by aborting the entire procedure. To dothat, you run the following command:EXECUTE DBMS_REDEFINITION.ABORT_REDEF_TABLE (‘SCOTT’,’EMP’,’HOLDING_EMP’)Fourth -at the end of the process, youll be left with the holding table stuffed full of data,looking exactly as the original table did (i.e., with all the old columns names and so on).That table is just wasting space, so you need to drop it -but thats not done automaticallyfor you, so youll have to remember to issue your own drop table holding_emp, forexample.Fifth, any DML being applied to the original table during this entire process is beingrecorded in a journaling table, and will be applied to the resulting table -but if there’svast amounts of DML involved, then you ought to use the sync_interim_table procedure toperiodically keep the interim table up-to-date with the changes being made.Online Table Redefinition RestrictionsTables that do not have a primary key are not supported (I imagine that this is because anyon-going DML is logged in an Index Organised Table, which can only be created with aprimary key).Copyright © Howard Rogers 2001 10/17/2001 Page 2 of 4
  • 3. Renaming a Column in 9i Administration TipsAny faintly exotic data types are suspect -for example, data types you’ve defined yourself,bfiles and longs (LOBS are OK) can’t be re-defined online.Your new table gets all of the rows in the original table -there’s no facility to stick a whereclause in, and only get a subset of them.And, finally, any new columns added to the table are obviously not going to have anysource data in the original table -so, were you to try and define such columns (on theholding table) as NOT NULL, you can expect the thing to fail.Simple DemoSQL> CONNECT SCOTT/TIGER@HJRCONNECTED.SQL> CREATE TABLE R_TEST( 2 COL1 CHAR(5) CONSTRAINT R_TEST_PK PRIMARY KEY, 3 COL2 NUMBER(7,2));TABLE CREATED.SQL> INSERT INTO R_TEST VALUES (AAAA,1238.90);SQL> INSERT INTO R_TEST VALUES (BBBB,4329.30);SQL> COMMIT;SQL> SELECT * FROM R_TEST;COL1 COL2----- ----------AAAA 1238.9BBBB 4329.3SQL> CREATE TABLE RTEST_HOLD( 2 DEPT_CODE CHAR(5), 3 SALARY NUMBER(7,2));TABLE CREATED.SQL> EXECUTE DBMS_REDEFINITION.CAN_REDEF_TABLE(SCOTT,R_TEST)PL/SQL PROCEDURE SUCCESSFULLY COMPLETED.SQL> EXECUTE DBMS_REDEFINITION.START_REDEF_TABLE( -> SCOTT,R_TEST,RTEST_HOLD, -> UPPER(COL1) DEPT_CODE,-> COL2 SALARY)PL/SQL PROCEDURE SUCCESSFULLY COMPLETED.Copyright © Howard Rogers 2001 10/17/2001 Page 3 of 4
  • 4. Renaming a Column in 9i Administration TipsSQL> EXECUTEDBMS_REDEFINITION.FINISH_REDEF_TABLE(SCOTT,R_TEST,RTEST_HOLD)PL/SQL PROCEDURE SUCCESSFULLY COMPLETED.SQL> SELECT * FROM R_TEST;DEPT_ SALARY----- ----------AAAA 1238.9BBBB 4329.3SQL> DROP TABLE RTEST_HOLD;TABLE DROPPED.SQL> DESC R_TEST; NAME TYPE -------------------------------- ---------------------------- DEPT_CODE CHAR(5) SALARY NUMBER(7,2)Copyright © Howard Rogers 2001 10/17/2001 Page 4 of 4