您的位置:首页 > 其它

移动表到另外一个表空间

2015-03-02 22:37 239 查看
把一个表从一个表空间移动到另外一个表空间有两种方式

一,使用"altertableXmovetablespaceY"and"alterindexXrebuildtablespaceY"
优点:简单,快速
缺点:不能移动含有LONGorLONGRAW字段的表

ALTERTABLEMOVETABLESPACEMETHOD:
===================================

Wewillstartwiththebasicsyntaxbelowfollowedbyascriptthatyoucancutandpaste:

Syntax
-----------
altertablemovestorage()tablespace

Example
-------------


SQL>createtableftab(xnumber)storage(initial20Knext20K)tablespaceusers;

SQL>createindexiftabonftab(x)tablespaceusers;
IndexCreated

SQL>altertableftabmovestorage(initial2knext2k)tablespacetrans;
Tablealtered.

SQL>selecttable_name,tablespace_namefromdba_tableswheretable_name='FTAB';

TABLE_NAMETABLESPACE_NAME
------------------------------------------------------------
FTABTRANS

SQL>selectindex_name,tablespace_namefromdba_indexeswhereindex_name='IFTAB';

INDEX_NAMETABLESPACE_NAME
------------------------------------------------------------
IFTABUSERS


NOTE:Whenmovingatableinthismanner,therowidvaluesarechanged.
Indexesdependontherowidinformationandthereforetheywillbecomeunusable.
YouwillgetanORA-1502unlessyourebuildtheassociatedindex(es).
需要重建索引

下面是一个批量脚本


setechooff

columnorder_col1noprint
columnorder_col2noprint

setheadingoff
setverifyoff
setfeedbackoff
setechooff

spooltmp.sql

selectdecode(segment_type,'TABLE',
segment_name,table_name)order_col1,
decode(segment_type,'TABLE',1,2)order_col2,
'alter'||segment_type||''||segment_name||
decode(segment_type,'TABLE','move','rebuild')||
chr(10)||
'tablespace&1'||chr(10)||
'storage(initial'||initial_extent||'next'||
next_extent||chr(10)||
'minextents'||min_extents||'maxextents'||
max_extents||chr(10)||
'pctincrease'||pct_increase||'freelists'||
freelists||');'
fromuser_segments,
(selecttable_name,index_namefromuser_indexes)
wheresegment_typein('TABLE','INDEX')
andsegment_name=index_name(+)
orderby1,2
/

spooloff

setheadingon
setverifyon
setfeedbackon
setechoon

REMUNCOMMENTTOAUTORUNthegeneratedcommands
REMELSEedittmp.sql,modifyasneededandrunit
REM@tmp


上面的脚本在要迁移的表的用户下执行,得到该用户下移动的所有move语句和rebuildindex语句,
Entervaluefor1:Users----这里输入目标表空间
例如,我们在scott用户下运行此脚本


SQL>@moveall
SQL>setechooff

alterTABLEACCOUNTmove
tablespaceusers
storage(initial10240next10240
minextents1maxextents121
pctincrease50freelists1);

alterTABLEBONUSmove
tablespaceusers
storage(initial10240next10240
minextents1maxextents121
pctincrease50freelists1);

alterTABLEDEPTmove
tablespaceusers
storage(initial10240next10240
minextents1maxextents121
pctincrease50freelists1);

alterINDEXPK_DEPTrebuild
tablespaceusers
storage(initial10240next10240
minextents1maxextents121
pctincrease50freelists1);

....


二,使用Export/Import

有三种方式

A.OnaPerTableBasis
B.OnaPerUserBasis
C.FromuserAtouserB

例;


A.OnaPerTableBasis
-----------------------

1.Checkthetablespacesinuseandperformthetablelevelexport


SQL>CONNscott/tiger

SQL>SELECTtable_name,tablespace_nameFROMuser_tables
WHEREtable_name='EMP';

TABLE_NAMETABLESPACE_NAME
------------------------------------------------------------
EMPUSERS

SQL>SELECTindex_name,tablespace_nameFROMuser_indexes
WHEREtable_name='EMP';

INDEX_NAMETABLESPACE_NAME
------------------------------------------------------------
PK_EMPUSERS

expscott/tigerfile=emp.dmprows=yestables=emp


2.Droporrenamethetableyouwishtomove


SQL>CONNscott/tiger

SQL>RENAMEemptoold_emp;

SQL>SELECTindex_name,tablespace_nameFROMuser_indexes
WHEREtable_name='EMP';

norowsselected

SQL>SELECTindex_name,tablespace_nameFROMuser_indexes
WHEREtable_name='OLD_EMP';

TABLE_NAMETABLESPACE_NAME
------------------------------------------------------------
OLD_EMPUSERS



3.RunimportwithINDEXFILE=togetafilewiththecreatetable
andindexstatements.


$impscott/tigerfile=emp.dmpindexfile=emp.sql


4.Usinganeditor(like?vi?)tomakethefollowingchanges:

vi打开删除下面的注释并且修改原来的表空间为新的表空间


-Remove?REM?fromtheCREATEandALTERTABLEstatements
-RemovetheCONNECTandCREATEINDEXstatements
-Replacethetablespacenameswiththenewname(?NEW_USERS?)

Aftertheedit,thefileshouldlooksimilarto:

CREATETABLE"SCOTT"."EMP"("EMPNO"NUMBER(4,0),"ENAME"
VARCHAR2(10),"JOB"VARCHAR2(9),"MGR"NUMBER(4,0),"HIREDATE"DATE,
"SAL"NUMBER(7,2),"COMM"NUMBER(7,2),"DEPTNO"NUMBER(2,0))
PCTFREE10PCTUSED40INITRANS1MAXTRANS255LOGGINGSTORAGE(INITIAL
131072NEXT65536MINEXTENTS1MAXEXTENTS2147483645PCTINCREASE50
FREELISTS1FREELISTGROUPS1BUFFER_POOLDEFAULT)TABLESPACE"NEW_USERS";

ALTERTABLE"SCOTT"."EMP"ADDCONSTRAINT"PK_EMP"PRIMARYKEY
("EMPNO")USINGINDEXPCTFREE10INITRANS2MAXTRANS255
STORAGE(INITIAL131072NEXT65536MINEXTENTS1MAXEXTENTS2147483645
PCTINCREASE50FREELISTS1FREELISTGROUPS1BUFFER_POOLDEFAULT)
TABLESPACE"NEW_USERS"ENABLE;

ALTERTABLE"SCOTT"."EMP"ADDCONSTRAINT"FK_DEPTNO"FOREIGNKEY
("DEPTNO")REFERENCES"DEPT"("DEPTNO")ENABLENOVALIDATE;

ALTERTABLE"SCOTT"."EMP"ENABLECONSTRAINT"PK_EMP";

ALTERTABLE"SCOTT"."EMP"ENABLECONSTRAINT"FK_DEPTNO";



5.Grantquotaonthenewtablespace


SQL>CONNsystem/manager

SQL>ALTERUSERscottQUOTA2mONnew_users;

Iftheuserhasnoquota,thenthecreatewillfailwith

CREATETABLE"SCOTT"."EMP"("EMPNO"NUMBER(4,0),"ENAME"
*
ERRORatline1:
ORA-01536:spacequotaexceededfortablespace'NEW_USERS



6.Runthescripttocreatethetables


SQL>CONNscott/tiger

SQL>@emp.sql

SQL>SELECTtable_name,tablespace_nameFROMuser_tables
WHEREtable_name='EMP';

TABLE_NAMETABLESPACE_NAME
------------------------------------------------------------
EMPNEW_USERS


7.RuntheimportwithIGNORE=Ytopopulatethenewtable(s)andcreatethe导入数据
index(es).


$impscott/tigerfile=emp.dmpignore=yes



B.OnaPerUserBasis---把整个用户全部导出,修改用户的默认表空间,然后再导入
----------------------

1.Performauserlevelorfulldatabaseexport

$expscott/tigerfile=scott.dmplog=scott.log

2.Droporrenamethetable(s)youaremoving

SQL>CONNscott/tiger

SQL>RENAMEempTOold_emp;

SQL>RENAMEdeptTOold_dept;

3.Grantquotaonthenewtablespace

SQL>CONNsystem/manager

SQL>ALTERUSERscottDEFAULTTABLESPACEnew_users;

SQL>ALTERUSERscottQUOTA0ONusers;

SQL>ALTERUSERscottQUOTA2mONnew_users;

SQL>REVOKEunlimitedtablespaceFROMscott;

SQL>REVOKEdbaFROMscott;

4.Testtomakesurethattheusercannolongercreateobjectsintheold?
tablespace.Createatableandspecifytheoldtablespace.

SQL>CONNscott/tiger

SQL>CREATETABLEtest(avarchar2(10))tablespaceusers;
*
ERRORatline1:
ORA-01536:spacequotaexceededfortablespace'USERS'

5.PerformtheimportwithIGNORE=YES

$impscott/tigerfile=scott.dmplog=imp_scott.logignore=yes

6.Re-granttheprivilegesthatwererevokedinstep3,ifrequired.

SQL>CONNsystem/manager

SQL>GRANTdba,resource,unlimitedtablespaceTOscott;

C.FromuserAtouserB---从一个用户导入到另外一个用户下
------------------------

ThefollowingstepswillmovetablesfromuserAtablespaceUSER_A_TS
touserBtablespaceUSER_B_TS:

1.Performauserlevelexportforuser_a

$expuser_a/user_afile=user_a.dmp

2.ForuserBchecktablespacequotasontablespacesUSER_A_TSandUSER_B_TS
andthenamendaccordingly

SQL>SELECTtablespace_name,max_blocksFROMdba_ts_quotas
WHEREusername='USER_B';

TABLESPACE_NAMEMAX_BLOCKS
----------------------------------------
USER_B_TS256
USER_A_TS256

SQL>ALTERUSERuser_bQUOTA0onuser_a_ts;

SQL>REVOKEunlimitedtablespaceFROMuser_b;

SQL>REVOKEdbaFROMuser_b;

SQL>ALTERUSERuser_bQUOTA2mONuser_b_ts;

3.Testtomakesurethattheusercannolongercreateobjectsinthe?old?
tablespace.Createatableandspecifytheoldtablespace.

SQL>CONNuser_b/user_b

SQL>CREATETABLEtest(avarchar2(10))TABLESPACEuser_a_ts;
createtabletest(avarchar2(10))tablespaceuser_a_ts
*
ERRORatline1:
ORA-01536:spacequotaexceededfortablespace'USER_A_TS'

ChecktoseethatuserBcancreatetable(s)inthenewtablespace,USER_B_TS.

SQL>CREATETABLEJUNK(ANUMBER)TABLESPACE<user_b_ts>;
*
ERRORatline1:
ORA-01536:spacequotaexceededfortablespace?USER_B_TS?

4.Performtheimport.

$impuser_b/user_bfromuser=user_atouser=user_bfile=user_a.dmp

5.Re-granttheprivilegesthatwererevokedinstep2,ifrequired.

SQL>connsystem/manager

SQL>ALTERUSERuser_bQUOTA2mONuser_a_ts;

SQL>GRANTunlimitedtablespace,dbaTOuser_b




参考:MOSDocID147356.1
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
章节导航