日期:2014-05-16  浏览次数:20551 次

oracle迁移表到新的表空间
1.创建新的表空间
CREATE TABLESPACE bayonet 
DATAFILE '/oradata/db/bayonet_01.DBF' 
SIZE 2G 
AUTOEXTEND ON NEXT 1G MAXSIZE UNLIMITED LOGGING
EXTENT MANAGEMENT LOCAL SEGMENT SPACE MANAGEMENT AUTO ;


2.修改用户默认表空间
ALTER USER dbuser DEFAULT tablespace bayonet;


3.创建迁移表到新的表空间SQL执行语句
==普通表:
SELECT 'alter table ' ||table_name || ' move tablespace bayonet;'   
FROM user_all_tables   
WHERE tablespace_name='USERS';

--无子分区
SELECT 'alter table ' ||t1.TABLE_NAME || ' move partition '|| t2.PARTITION_NAME ||' tablespace bayonet;'   
FROM user_all_tables t1,USER_TAB_PARTITIONS t2    
WHERE t1.TABLE_NAME=t2.TABLE_NAME AND t2.tablespace_name='USERS' and t2.subpartition_count>0; 


--子分区
SELECT 'alter table ' ||t1.TABLE_NAME || ' move subpartition '|| t2.subpartition_name ||' tablespace bayonet;'   
FROM user_all_tables t1,all_tab_subpartitions t2    
WHERE t1.TABLE_NAME=t2.TABLE_NAME AND t2.tablespace_name='USERS';

SELECT 'alter table ' ||t1.TABLE_NAME || ' modify default ATTRIBUTES FOR partition '|| t2.PARTITION_NAME ||' tablespace bayonet;'   
FROM user_all_tables t1,USER_TAB_PARTITIONS t2    
WHERE t1.TABLE_NAME=t2.TABLE_NAME AND t2.tablespace_name='USERS'; 


4.创建索引重建的SQL执行语句

select 'alter table '||table_name||' disable primary key;' from user_constraints where constraint_type='P' and R_OWNER='DBUSER';

SELECT 'alter index ' ||index_name || ' rebuild  tablespace bayonet;' 
FROM user_indexes  
WHERE index_type='NORMAL' AND DROPPED='NO' AND  
TABLE_NAME IN (
    SELECT TABLE_NAME 
    FROM user_all_tables 
    WHERE tablespace_name='users'
);

select 'alter table '||table_name||' enable primary key;' from user_constraints 
where constraint_type='P' and R_OWNER='DBUSER';




5.创建表分区
-- Create table
create table GPS_POSITION
(
  ID           NVARCHAR2(32) not null,
  CARDNO       NVARCHAR2(32),
  GPSTIME      DATE,
  LONGITUDE    NUMBER,
  LATITUDE     NUMBER,
  E_W          NVARCHAR2(8),
  N_S          NVARCHAR2(8),
  SPEED        NVARCHAR2(16),
  GPSSPEED     NVARCHAR2(32),
  DIRECTION    NVARCHAR2(16),
  MILEAGE      NVARCHAR2(16),
  MILEAGE1     NVARCHAR2(16),
  HIGHT        NVARCHAR2(16),
  TEMPERATURE  NVARCHAR2(16),
  OIL          NVARCHAR2(16),
  PASSENGER    NVARCHAR2(16),
  S0           NVARCHAR2(2),
  S1           NVARCHAR2(2),
  S2           NVARCHAR2(2),
  S3           NVARCHAR2(2),
  S4           NVARCHAR2(2),
  A0           NVARCHAR2(2),
  A1           NVARCHAR2(2),
  A2           NVARCHAR2(2),
  A3           NVARCHAR2(2),
  A4           NVARCHAR2(2),
  RUNTIME      NVARCHAR2(32),
  A_V          NVARCHAR2(8),
  CREATEDATE   DATE default sysdate,
  GPSID        NUMBER,
  NUM          NUMBER,
  WAY          NUMBER,
  TYPE         NUMBER,
  WAYSENTDATE  NUMBER,
  RECEIVEDDATE NUMBER,
  B0           NVARCHAR2(2),
  B1           NVARCHAR2(2),
  B2           NVARCHAR2(2),
  B3           NVARCHAR2(2),
  B4           NVARCHAR2(2),
  B5           NVARCHAR2(2),
  B6           NVARCHAR2(2),
  CITYID       NVARCHAR2(20),
  DATASTATE    NUMBER,
  GPSLOCK      NVARCHAR2(2),
  GPSPOWER     NVARCHAR2(2),
  ADDRESS      NVARCHAR2(200)
)
partition by range (GPSTIME)
subpartition by list (CITYID)
(
  partition P201010 values less than (TO_DATE(' 2010-10-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
    tablespace bayonet
    pctfree 10
    initrans 1
    maxtrans 255
    storage
    (
      initial 2
      minextents 1
      maxextents unlimited
    )
  (
    subpartition P201010_P600000 values ('600000') tablespace USERS,
    subpartition P201010_P600001 values ('600001') tablespace USERS,
    subpartition P201010_P600002 values ('600002') tablespace USERS,
    subpartition P201010_P600003 values ('600003') tablespace USERS,
    subpartition P201010_P600004 va