脚踏实地、勇往直前!
全部博文(1005)
分类: Oracle
2011-05-12 15:47:48
SQL> CREATE TABLE tb_no_partition(
2 id NUMBER(10),
3 created_date DATE,
4 lookup_id NUMBER(10),
5 data VARCHAR2(50)
6 );
表已创建。
SQL> DECLARE
2 l_lookup_id Number;
3 l_create_date DATE;
4 BEGIN
5 FOR i IN 1 .. 1000000 LOOP
6 IF MOD(i, 3) = 0 THEN
7 l_create_date := ADD_MONTHS(SYSDATE, -24);
8 l_lookup_id := 2;
9 ELSIF MOD(i, 2) = 0 THEN
10 l_create_date := ADD_MONTHS(SYSDATE, -12);
11 l_lookup_id := 1;
12 ELSE
13 l_create_date := SYSDATE;
14 l_lookup_id := 3;
15 END IF;
16
17 INSERT INTO tb_no_partition (id, created_date, lookup_id, Data)
18 VALUES (i, l_create_date, l_lookup_id, 'This is some data for ' || i);
19 END LOOP;
20 COMMIT;
21 END;
22 /
PL/SQL 过程已成功完成。
SQL>
SQL> CREATE TABLE tb_partition (
2 Id NUMBER(10),
3 created_date DATE,
4 lookup_id NUMBER(10),
5 Data VARCHAR2(50)
6 )
7 PARTITION BY RANGE (created_date)
8 (PARTITION tb_partition_2003 VALUES LESS THAN (TO_DATE('01/01/2004', 'DD/MM/YYYY')),
9 PARTITION tb_partition_2004 VALUES LESS THAN (TO_DATE('01/01/2005', 'DD/MM/YYYY')),
10 PARTITION tb_partition_2005 VALUES LESS THAN (MAXVALUE));
表已创建。
SQL> ALTER TABLE tb_no_partition ADD (
2 CONSTRAINT tb_no_partition_pk PRIMARY KEY (id)
3 );
表已更改。
SQL> Begin
2 Dbms_Redefinition.Can_Redef_Table(USER, 'TB_NO_PARTITION');
3 End;
4 /
PL/SQL 过程已成功完成。
SQL>
SQL> BEGIN
2 DBMS_REDEFINITION.start_redef_table(
3 uname => USER,
4 orig_table => 'TB_NO_PARTITION',
5 int_table => 'TB_PARTITION',
6 col_mapping=>'Id Id, Created_Date Created_Date, Lookup_Id Lookup_Id, Data Data');
7 END;
8 /
PL/SQL 过程已成功完成。
SQL> BEGIN
2 dbms_redefinition.sync_interim_table(
3 uname => USER,
4 orig_table => 'TB_NO_PARTITION',
5 int_table => 'TB_PARTITION');
6 END;
7 /
PL/SQL 过程已成功完成。
SQL> ALTER TABLE TB_PARTITION ADD (
2 CONSTRAINT TB_PARTITION_pk2 PRIMARY KEY (id)
3 );
表已更改。
SQL> BEGIN
2 dbms_redefinition.finish_redef_table(
3 uname => USER,
4 orig_table => 'TB_NO_PARTITION',
5 int_table => 'TB_PARTITION');
6 END;
7 /
PL/SQL 过程已成功完成。
SQL> Drop Table TB_PARTITION;
表已删除。
SQL> ALTER TABLE TB_NO_PARTITION
2 RENAME CONSTRAINT TB_PARTITION_PK2 TO TB_NO_PARTITION_PK;
表已更改。
SQL> Select table_name,partitioning_type
2 From dba_part_tables
3 Where table_name='TB_NO_PARTITION';
TABLE_NAME PARTITION
------------------------------ ---------
TB_NO_PARTITION RANGE
SQL> spool off;