You are on page 1of 2

Moving a Table to a new Tablespace

Administration Tips

Moving a Table
Moving a table from one tablespace to another is a piece of cake -provided you have Oracle 8i. The command there is simply:
ALTER TABLE

BLAH

MOVE TABLESPACE

XYZ;

The tablespace name in this command doesn't have to be a completely new tablespace: it's perfectly permissible to move a table within a tablespace, and hence the name can legitimately be the name of the tablespace it's already housed within. There's only one slight word of warning with this command: the way it works is that a new version of the table is created, under a temporary name, in the specified tablespace, and then populated by selecting from the original table. When the populating exercise has finished, the old version is dropped, and the new one simultaneously renamed as the old one. For a split second, therefore, two versions of the table exist -and hence you need sufficient disk space to accomodate those two versions. If this is 12Gb table you are moving, that can be expensive! Otherwise, there's not much to report on the 'move tablespace' command. It brings along all constraints and permissions, so there's none of that to redefine. There is one slight problem: all indexes on the table have got leaf nodes stuffed full of ROWIDs which point to where the old table used to be. Now it's moved, those indexes are basically stuffed full of worthless nonsense... hence the need to rebuild all indexes on a table that's been moved. Suppose, however, you don't have the advantages of 8i and its "move tablespace" command. How then do you do the deed? Well, the simplest way would be to do a CTAS. That stands for "create table as select", and refers to the fact that you could issue the following command:
CREATE TABLE NEW_EMP TABLESPACE NEWONE AS SELECT

FROM OLD_EMP;

Only problem with this command is that you probably need to do it twice: assuming you want the newly re-located table to be the same name as the original, you'd follow the above command with a drop table old_emp; followed by a revamp of the earlier CTAS to read 'create...old_emp as select * from new_emp'.

Copyright Howard Rogers 2001

10/17/2001

Page 1 of 2

Moving a Table to a new Tablespace

Administration Tips

But the real nasty with this technique is that the tables you create each time you run the CTAS command are new tables. That means no-one has any rights on them, there are no constraints defined for them, and no indexes exist for them. You'll need to re-grant all permissions, re-create all constraints, and re-specify and re-create all indexes, too (now isn't that upgrade to 8i looking attractive??!). Another possibility presents itself: export the old table, then drop it. Alter the user so that their quota on the original tablespace is zero, and also alter that user so that their default tablespace is the one you want the new table created in. Import, you see, always likes to create tables in a tablespace of exactly the same name as the one the table originally came from. But if it can't create it there (and the quota of zero prevents that), it will instead choose to create the table in whoever is running import's default tablespace. This technique can get a bit fiddly, but has the advantage of re-creating all indexes, constraints and re-granting all permissions. You'd have to remember to set the quotas and default tablespace for the User involved back to what they were earlier, too. At the end of the day, whichever version of Oracle you use, and whichever technique you go for, moving tables is not a cheap exercise. Best house them correctly in the first place. But if you have to do it, the 'move tablespace' option has the distinct advantages of ease of implementation.

Copyright Howard Rogers 2001

10/17/2001

Page 2 of 2

You might also like