alter table myschema.mytable rename to mytable_old;Change ownership for a table:
alter table myschema.mytable owner to anotherschema;
Minimalistic Oracle contains a collection of practical examples from my encounters with Oracle technologies. When relevant, I also write about other technologies, like Linux or PostgreSQL. Many of the posts starts with "how to" since they derive directly from my own personal experience. My goal is to provide simple examples, so that they can be easily adapted to other situations.
alter table myschema.mytable rename to mytable_old;Change ownership for a table:
alter table myschema.mytable owner to anotherschema;
SELECT TABLE_NAME, PARTITIONED,NUM_ROWS,COMPRESSION FROM DBA_TABLES WHERE TABLE_NAME='SEGMENT_SIZE_SAMPLES';
TABLE_NAME | PARTITIONED | NUM_ROWS | COMPRESSION |
---|---|---|---|
SEGMENT_SIZE_SAMPLES | NO | 948401 | DISABLED |
ALTER TABLE SEGMENT_SIZE_SAMPLES MODIFY PARTITION BY RANGE (SAMPLE_DATE) INTERVAL ( NUMTOYMINTERVAL(1,'MONTH') ) ( PARTITION P_INIT VALUES LESS THAN (TO_DATE('2014-01','YYYY-MM') ) ) ROW STORE COMPRESS ADVANCED ONLINE;
exec DBMS_STATS.GATHER_TABLE_STATS (OwnName => 'DBDRIFT',TabName => 'SEGMENT_SIZE_SAMPLES');
SELECT TABLE_NAME, PARTITIONED,NUM_ROWS,COMPRESSION FROM DBA_TABLES WHERE TABLE_NAME='SEGMENT_SIZE_SAMPLES';
TABLE_NAME | PARTITIONED | NUM_ROWS | COMPRESSION |
---|---|---|---|
SEGMENT_SIZE_SAMPLES | YES | 1000719 |
SELECT TABLE_NAME, PARTITION_NAME,NUM_ROWS,COMPRESSION, COMPRESS_FOR FROM DBA_TAB_PARTITIONS WHERE TABLE_NAME='SEGMENT_SIZE_SAMPLES' FETCH FIRST 5 ROWS ONLY;
TABLE_NAME | PARTITION_NAME | NUM_ROWS | COMPRESSION | COMPRESS_FOR |
---|---|---|---|---|
SEGMENT_SIZE_SAMPLES | P_INIT | 0 | ENABLED | ADVANCED |
SEGMENT_SIZE_SAMPLES | SYS_P17074 | 19320 | ENABLED | ADVANCED |
SEGMENT_SIZE_SAMPLES | SYS_P17075 | 24955 | ENABLED | ADVANCED |
SEGMENT_SIZE_SAMPLES | SYS_P17076 | 24150 | ENABLED | ADVANCED |
SEGMENT_SIZE_SAMPLES | SYS_P17077 | 24934 | ENABLED | ADVANCED |
SELECT INDEX_NAME,UNIQUENESS, PARTITIONED FROM DBA_INDEXES WHERE TABLE_NAME='SEGMENT_SIZE_SAMPLES';
INDEX_NAME | UNIQUENESS | PARTITIONED |
---|---|---|
SEGMENT_SIZE_SAMPLES_IDX1 | UNIQUE | NO |
SEGMENT_SIZE_SAMPLES_IDX2 | NONUNIQUE | NO |
INDEX_NAME | UNIQUENESS | PARTITIONED |
---|---|---|
SEGMENT_SIZE_SAMPLES_IDX1 | UNIQUE | NO |
SEGMENT_SIZE_SAMPLES_IDX2 | NONUNIQUE | YES |
alter table MUSIC.ALBUM modify partition by list (GENRE) ( partition P_ROCK values (( 'ROCK')), partition P_POP values (( 'POP')), partition P_CLASSICAL values (( 'CLASSICAL')), partition P_MISC values (default) ) online;
-- First, give the LOB the desired attributes, if you wish to change any of them: alter TABLE STOCKS MOVE LOB (DOC) STORE AS SECUREFILE( TABLESPACE DATA1 ENABLE STORAGE IN ROW CHUNK 8192 COMPRESS MEDIUM ); -- Alter the table alter TABLE STOCKS MODIFY PARTITION BY LIST (stockname) SUBPARTITION BY RANGE (LASTUPDATED) ( -- First partition is called a_name PARTITION a_name VALUES('a-abc') ( -- older values SUBPARTITION SP_a_name_1_older VALUES LESS THAN ( TO_DATE('2016-01', 'YYYY-MM')) TABLESPACE DATA1 LOB (DOC) STORE AS SECUREFILE ( TABLESPACE DATA1 ), -- 2016 subpartition SP_a_name_201601 VALUES LESS THAN (TO_DATE(' 2016-02-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS')) TABLESPACE DATA_2016 LOB (DOC) STORE AS SECUREFILE ( TABLESPACE DATA_2016 ), ...continue with one subpartition per month per year... ...contine with partitions b_name, c_name etc -- Final partition is called z_name PARTITION z_name VALUES(default) ( -- 2016 SUBPARTITION SP_sp_z_name_older VALUES LESS THAN ( TO_DATE('2016-01', 'YYYY-MM')) TABLESPACE DATA1, subpartition SP_sp_z_name_201601 VALUES LESS THAN (TO_DATE(' 2016-02-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS')) TABLESPACE DATA_2016 LOB (DOC) STORE AS SECUREFILE ( TABLESPACE DATA_2016 ), ...continue with one subpartition per month per year... LOB (DOC) STORE AS SECUREFILE ( TABLESPACE DATA_2022 ), subpartition SP_z_name_202212 VALUES LESS THAN (TO_DATE(' 2023-01-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS')) TABLESPACE DATA_2022 LOB (DOC) STORE AS SECUREFILE ( TABLESPACE DATA_2022 ), SUBPARTITION SP_z_name_DEFAULT VALUES LESS THAN (MAXVALUE) TABLESPACE DATA1 LOB (DOC) STORE AS SECUREFILE ( TABLESPACE DATA1) ) ) ONLINE UPDATE INDEXES( IDX_1 LOCAL, IDX_2 LOCAL );
CREATE GLOBAL TEMPORARY TABLE INCOMING_DATA ( ROW_ID NUMBER(10) NOT NULL, SSN NUMBER(11), ENTRY_DATE DATE NOT NULL, HANDLED_BY VARCHAR2(30 BYTE) NOT NULL, CONTENT_CODE VARCHAR2(4 BYTE), MESSAGE CLOB ) ON COMMIT DELETE ROWS ;
ALTER SESSION SET TEMP_UNDO_ENABLED = TRUE;
DOP = PARALLEL_THREADS_PER_CPU x CPU_COUNT
DOP = PARALLEL_THREADS_PER_CPU x CPU_COUNT x INSTANCE_COUNT
SELECT DEGREE FROM DBA_TABLES WHERE TABLE_NAME = 'EMP' AND OWNER = 'SCOTT'; DEGREE ------------------ 1
ALTER TABLE [table name] SET UNUSED (col1, col2, ... col n);or
ALTER TABLE [table name] SET UNUSED COLUMN (col1);Example:
ALTER TABLE MYTABLE SET UNUSED COLUMN MODIFIED_DATE;
SELECT * FROM USER_UNUSED_COL_TABS;
TABLE_NAME | COUNT |
---|---|
MYTABLE | 1 |
INVOICES_TMP | 1 |
ALTER TABLE MYTABLE DROP UNUSED COLUMNS;
SELECT * FROM USER_UNUSED_COL_TABS;
TABLE_NAME | COUNT |
---|---|
INVOICES_TMP | 1 |
ALTER TABLE MYTABLE DROP UNUSED COLUMNS CHECKPOINT 1000;
CREATE TABLE ADDRESSES ( ADDRESS_ID NUMBER, EMLOYEE_ID NUMBER, STREET_ADDRESS VARCHAR2(40), STREET_ADDRESS2 VARCHAR2(40), CITY VARCHAR2 (30), STATE VARCHAR2(2), ZIP VARCHAR2(5), ZIP_PLUS VARCHAR2(4), COUNTRY VARCHAR2(20), CONSTRAINT PK_ADDRESSES PRIMARY KEY (ADDRESS_ID)-- "OUT-OF-LINE" CONSTRAINT CREATION ); CREATE SEQUENCE ADDRESSES_SEQ START WITH 1 INCREMENT BY 1 MINVALUE 1 MAXVALUE 1000000 NOCACHE NOCYCLE NOORDER; CREATE OR REPLACE TRIGGER ADDRESSES_TRG BEFORE INSERT ON ADDRESSES FOR EACH ROW WHEN (NEW.ADDRESS_ID IS NULL) BEGIN SELECT ADDRESSES_SEQ.NEXTVAL INTO :NEW.ADDRESS_ID FROM DUAL; END; /
SQL> show parameter deferred NAME_COL_PLUS_SHOW_PARAM TYPE VALUE ---------------------------- ----------- -------- deferred_segment_creation boolean TRUE
INSERT /*+ APPEND */ INTO sales SELECT product_id, customer_id, TRUNC(sales_date), 3, promotion_id, quantity, amount FROM sales_activity_direct;Parallelized inserts:
ALTER SESSION ENABLE PARALLEL DML;Then, specify the APPEND hint with the PARALLEL hint:
INSERT /*+ APPEND PARALLEL */ INTO sales SELECT product_id, customer_id, TRUNC(sales_date), 3, promotion_id, quantity, amount FROM sales_activity_direct;You can use NOLOGGING with APPEND to make the process even faster. NOLOGGING allows the direct-path INSERT operation to generate a negligible amount of REDO:
INSERT /*+ APPEND PARALLEL NOLOGGING */ INTO sales SELECT product_id, customer_id, TRUNC(sales_date), 3, promotion_id, quantity, amount FROM sales_activity_direct;