shrinking a column in oracle - oracle

Lets say i have a table with the following definition
create table dummy (col1 number(9) not null)
All the values in this dummy.col1 are 7 digit long. Now i want to reduce the length of this column from 9 - 7 using alter command. Oracle gives me error that column to be modified must be empty to decrease precision or scale. Makes sense.
I want to ask is there any work around to reduce the column size?
I can't delete the values in the column.
I can't copy values from this column to another since it has trillions of data.

The column size has no relationship to how the data is physically stored (they are variable length)
e.g. '23' in a number(2) will take exactly the same space if stored in a number(38)
It is purely a constraint on the maximum number that can be stored in the column therefore you could just add a constraint on the column:
ALTER TABLE dummy ADD
CONSTRAINT c1
CHECK (col1 < 9999999)
ENABLE
VALIDATE;
if you want it to go a little quicker change VALIDATE to NOVALIDATE obviously this will not check the validity of the existing data.

Kevin's answer is excellent.
The only other way to do it is to
rename the existing column,
create a new column with the old name and the new size,
issue an update statement to populate the new field (which you said you cannot do)
and then drop the renamed column.
Are you sure you cannot find some downtime one weekend to perform this task ?

Solution #1
My solution below keeps the original column order.
I found that to be important, especially if there are canned SQL statements out
there (middle tier, client tier) that point back to your database that do implicit
SELECTs.
i.e.
SELECT *
FROM tableName
WHERE ...;
INSERT INTO copyTableName(column1,column2,column3,...)
SELECT *
FROM tableName
WHERE ...;
Here goes:
Generate the DDLs for
1. The table containing the column you intend to resize
2. All the relationship constraints, indexes, check constraints, triggers that reference that table.
3. All the foreign keys of other tables that reference the primary key of this table.
Make sure each table-referencing-object DDL is stand-alone, separate from the
CREATE TABLE DDL.
You'll have something like
/* 1. The table containing the column you intend to resize */
CREATE TABLE tableName
(
column1 TYPE(size) [DEFAULT value] [NOT] NULL,
column2 TYPE(size) [DEFAULT value] [NOT] NULL,
column3 TYPE(size) [DEFAULT value] [NOT] NULL,
...
)
TABLESPACE tsName
[OPTIONS];
/* 2. All the relationship constraints, indexes, check constraints, triggers that reference that table. */
CREATE INDEX indexName ON tableName
(column1)
NOLOGGING
TABLESPACE INDX
NOPARALLEL;
CREATE INDEX compositeIndexName ON tableName
(column1,column2,...)
NOLOGGING
TABLESPACE INDX
NOPARALLEL;
CREATE UNIQUE INDEX pkName ON tableName
(column2)
NOLOGGING
TABLESPACE INDX
NOPARALLEL;
ALTER TABLE tableName ADD (
CHECK (column4 IS NOT NULL));
ALTER TABLE tableName ADD (
CONSTRAINT pkName
PRIMARY KEY
(column2)
USING INDEX
TABLESPACE INDX);
ALTER TABLE tableName ADD (
CONSTRAINT fkName
FOREIGN KEY (column2)
REFERENCES otherTable (column2));
/* 3. All the foreign keys of other tables that reference the primary key of this table. */
ALTER TABLE otherTableName ADD (
CONSTRAINT otherTableFkName
FOREIGN KEY (otherTableColumn2)
REFERENCES tableName (column1));
Copy out just the CREATE TABLE statement, change the table name and
reduce the size of the column you wish to modify:
CREATE TABLE tableName_YYYYMMDD
(
column1 TYPE(size) [DEFAULT value] [NOT] NULL,
column2 TYPE(reducedSize) [DEFAULT value] [NOT] NULL,
column3 TYPE(size) [DEFAULT value] [NOT] NULL,
...
)
TABLESPACE tsName
[OPTIONS];
Insert the data from tableName into tableName_YYYYMMDD:
INSERT /* APPEND */ INTO tableName_YYYYMMDD(
column1 ,
column2 ,
column3 ,
... )
SELECT
column1 ,
column2 ,
column3 ,
...
FROM tableName;
COMMIT;
Drop all objects referencing the original table.
Also, drop all foreign keys that reference the tableName primary key pkName.
Don't worry, you've saved the DDL so you'll be able to recreate them.
Notice that I drop indexes after copying the data out of tableName.
I do this because perhaps one of the indexes will be used in the
above SELECT so that operation will complete faster.
DROP INDEX indexName ;
DROP INDEX compositeIndexName ;
DROP UNIQUE INDEX pkName ;
ALTER TABLE tableName DROP CONSTRAINT pkName ;
ALTER TABLE tableName DROP CONSTRAINT fkName ;
ALTER TABLE otherTableName DROP CONSTRAINT otherTableFkName ;
Drop the original table.
DROP TABLE tableName;
Rename the new table.
ALTER TABLE tableName_YYYYMMDD RENAME TO tableName;
Recreate all referencing objects from the DDL statements you saved before.
/* 2. All the relationship constraints, indexes, check constraints, triggers that reference that table. */
CREATE INDEX indexName ON tableName
(column1)
NOLOGGING
TABLESPACE INDX
NOPARALLEL;
CREATE INDEX compositeIndexName ON tableName
(column1,column2,...)
NOLOGGING
TABLESPACE INDX
NOPARALLEL;
CREATE UNIQUE INDEX pkName ON tableName
(column2)
NOLOGGING
TABLESPACE INDX
NOPARALLEL;
ALTER TABLE tableName ADD (
CHECK (column4 IS NOT NULL));
ALTER TABLE tableName ADD (
CONSTRAINT pkName
PRIMARY KEY
(column2)
USING INDEX
TABLESPACE INDX);
ALTER TABLE tableName ADD (
CONSTRAINT fkName
FOREIGN KEY (column2)
REFERENCES otherTable (column2));
/* 3. All the foreign keys of other tables that reference the primary key of this table. */
ALTER TABLE otherTableName ADD (
CONSTRAINT otherTableFkName
FOREIGN KEY (otherTableColumn2)
REFERENCES tableName (column1));
Solution #2
Keep the column order but do not rebuild non-unique-used-by-PK indexes that might contain column2.
ALTER TABLE tableName ADD (column2Copy TYPE(reducedSize));
UPDATE tableName SET column2Copy = column2;
ALTER TABLE tableName MODIFY (column2 TYPE(size) NULL);
ALTER TABLE tableName DROP CONSTRAINT pkName;
DROP INDEX pkName;
UPDATE tableName SET column2 = null;
ALTER TABLE tableName MODIFY (column2 TYPE(reducedSize));
UPDATE tableName SET column2 = column2Copy;
ALTER TABLE tableName DROP COLUMN column2Copy;
CREATE UNIQUE INDEX pkName ON tableName
(column2)
NOLOGGING
TABLESPACE INDX
NOPARALLEL;
ALTER TABLE tableName ADD (
CONSTRAINT pkName
PRIMARY KEY
(column2)
USING INDEX
TABLESPACE INDX);
COMMIT;

Related

In oracle on delete set null is not working

I have created two tables:
Create Table Dept
(Department_id number Constraint Depart_id_pk Primary Key
,Department_name varchar2(20));
Create table Emp
(Emp_id number Constraint Empl_id_pk Primary Key
,First_name varchar2(10)
,salary number
,Department_id number
,Constraint depart_id_fk Foreign Key (department_id)
References Dept (Department_id) on delete set null);
Then I have inserted some records in dept and Emp table. But when I try to drop dept table, instead of setting null in Emp.department_id column it shows error like this:
SQL> Drop Table Dept;
Drop Table Dept
*
ERROR at line 1:
ORA-02449: unique/primary keys in table referenced by foreign keys
The foreign key's clause say "on delete set null". Delete is a DML operation, and had you attempted to delete rows from the dept table, the corresponding emp rows would have been updated with a null dept_id.
But this isn't the case - you tried to drop the entire table, a DDL operation. This isn't allowed, because you'd be leaving behind constraints on the emp table that reference a table that no longer exists. If you want to drop these constraints too, you can use a cascade constraints clause:
DROP TABLE dept CASCADE CONSTRAINTS

Alter table enable novalidate constraint

I am trying to add UNIQUE KEY on a previously existing table with duplicate records by setting ENABLE NOVALIDATE.
But I am getting ORA-02299: cannot validate (my_owner.my_key_UK ) - duplicate keys found
ALTER TABLE my_owner.my_table
ADD CONSTRAINT my_key_UK UNIQUE (ID1,ID2)
ENABLE NOVALIDATE;
A unique constraint uses an index to enforce the noduplicates rule. By default it will create a unique index (makes sense right?). It is this index creation which is hurling ORA-02299.
However, if this is an existing index on the constrained columns the constraint will use that. The good news is, the index doesn't need to be unique for the constraint to use it.
So what you need to do is build a non-unique index first:
create index whatever_idx on my_table (ID1,ID2);
Then you will be able to create your constraint:
ALTER TABLE my_owner.my_table
ADD CONSTRAINT my_key_UK UNIQUE (ID1,ID2)
ENABLE NOVALIDATE;
You can check this by querying the data dictionary:
select uc.constraint_name
, uc.constraint_type
, uc.index_name
, ui.uniqueness as idx_uniqueness
from user_constraints uc
join user_indexes ui
on ui.index_name = uc.index_name
where uc.table_name = 'MY_TABLE'
Oracle ensure unique values using indexes. And if you create unique constrains db automatic creates unique index. Workaround is add DEFERRABLE options. In this case oracle creates normal index. Check example.
create table table_abc (a number,b number);
insert into table_abc values(1,1);
insert into table_abc values(1,2);
ALTER TABLE table_abc ADD CONSTRAINT my_key_a UNIQUE (a) DEFERRABLE enable novalidate; -- no error but in table nonunique values
ALTER TABLE table_abc ADD CONSTRAINT my_key_b UNIQUE (b) ENABLE NOVALIDATE; --no error
select * from user_indexes where table_name ='TABLE_ABC';

Replacing certain lines in a statemement

I am exporting ddl statemements for tables using dbms_metadata.set_transform_param package. This works as intended. However, I am also interested to remove a certain lines from the output.
For example: here is the output of one of the tables:
CREATE TABLE "FREEZES"
( "PLAYER_ID" NUMBER(9,0) NOT NULL ENABLE,
"PRODUCT_ID" NUMBER(19,0)
)
TABLESPACE "MINE" ;
ALTER TABLE "FREEZES" ADD CONSTRAINT "FREEZES_U" UNIQUE ("PLAYER_ID", "PRODUCT_ID")
USING INDEX
TABLESPACE "MINE_IDX" ENABLE;
ALTER TABLE "FREEZES" ADD SUPPLEMENTAL LOG DATA (PRIMARY KEY) COLUMNS;
ALTER TABLE "FREEZES" ADD SUPPLEMENTAL LOG DATA (FOREIGN KEY) COLUMNS;
ALTER TABLE "FREEZES" ADD SUPPLEMENTAL LOG DATA (UNIQUE INDEX) COLUMNS;
ALTER TABLE "FREEZES" ADD SUPPLEMENTAL LOG DATA (ALL) COLUMNS;
What I would like to have is:
CREATE TABLE "FREEZES"
( "PLAYER_ID" NUMBER(9,0) NOT NULL ENABLE,
"PRODUCT_ID" NUMBER(19,0)
)
TABLESPACE "MINE" ;
ALTER TABLE "FREEZES" ADD CONSTRAINT "FREEZES_U" UNIQUE ("PLAYER_ID", "PRODUCT_ID")
USING INDEX
TABLESPACE "MINE_IDX" ENABLE;
And here is the query I am using:
SELECT dbms_metadata.get_ddl(object_type, object_name, owner) as object_ddl
from dba_objects where owner = 'MINE' and object_type = 'TABLE';
I do know that I have to use regular expressions, something like:
SELECT regexp_replace(dbms_metadata.get_ddl(object_type, object_name, owner), 'pattren') as object_ddl
from dba_objects where owner = 'MINE' and object_type = 'TABLE';

How to do partitioning and sub partitioning with exchange partition in Oracle?

I am facing issue with exchange partitioning and sub partitioning in Oracle.
Please explain with example.
Please find following example
CREATE TABLE DEPARTMENT
( DEPT_ID NUMBER(30,0) NOT NULL ENABLE,
IS_ACTIVE VARCHAR2(1 BYTE) NOT NULL ,
BUSINESS_DATE DATE NOT NULL ENABLE,
COMPANY_CODE VARCHAR2(4) DEFAULT 'C1' NOT NULL ,
CONSTRAINT PK_DEPARTMENT PRIMARY KEY (BUSINESS_DATE, COMPANY_CODE, DEPT_ID) ENABLE
)
PARTITION BY RANGE
(
BUSINESS_DATE
)
INTERVAL (
NUMTODSINTERVAL(1,'DAY')
)
SUBPARTITION BY LIST(COMPANY_CODE)
SUBPARTITION TEMPLATE
(
SUBPARTITION CMN_01 VALUES ('C1'),
SUBPARTITION CMN_02 VALUES ('C2'),
SUBPARTITION CMN_03 VALUES ('C3')
)
(
PARTITION DEFAULT_PART VALUES LESS THAN ('01-JAN-2012')
)
;
CREATE INDEX IDX1_DEPARTMENT ON DEPARTMENT (DEPT_ID, IS_ACTIVE);
CREATE TABLE TEMP_DEPARTMENT AS SELECT * FROM DEPARTMENT WHERE 1=2;
CREATE TABLE EMPLOYEE
( ID NUMBER(30,0) NOT NULL ENABLE,
DEPT_ID NUMBER(30,0) NOT NULL ENABLE,
BUSINESS_DATE DATE NOT NULL ENABLE,
COMPANY_CODE VARCHAR2(4) DEFAULT 'C1' NOT NULL,
CONSTRAINT PK_EMPLOYEE PRIMARY KEY (BUSINESS_DATE,COMPANY_CODE, ID) ENABLE,
CONSTRAINT FK1_EMPLOYEE_DEPT FOREIGN KEY (BUSINESS_DATE, COMPANY_CODE, DEPT_ID)
REFERENCES DEPARTMENT (BUSINESS_DATE, COMPANY_CODE, DEPT_ID) ENABLE NOVALIDATE
)
PARTITION BY RANGE
(
BUSINESS_DATE
)
INTERVAL (
NUMTODSINTERVAL(1,'DAY')
)
SUBPARTITION BY LIST(COMPANY_CODE)
SUBPARTITION TEMPLATE
(
SUBPARTITION CMN_01 VALUES ('C1'),
SUBPARTITION CMN_02 VALUES ('C2'),
SUBPARTITION CMN_03 VALUES ('C3')
)
(
PARTITION DEFAULT_PART VALUES LESS THAN ('01-JAN-2012')
);
Insert few thousands records into DEPARTMENT and EMPLOYEE tables for few partitions and their all sub partitions
select * from DEPARTMENT where BUSINESS_DATE='19-JAN-15' and COMPANY_CODE='C1';
select* from EMPLOYEE;
select * from all_tab_partitions where TABLE_OWNER='TEST' and
table_name='DEPARTMENT';
select * from ALL_TAB_SUBPARTITIONS where TABLE_OWNER='TEST' and
table_name='DEPARTMENT';
select * from all_tab_partitions where TABLE_OWNER='TEST' and
table_name='EMPLOYEE';
select * from ALL_TAB_SUBPARTITIONS where TABLE_OWNER='TEST' and
table_name='EMPLOYEE';
select * from TEMP_DEPARTMENT;
Exchange and Truncate sub partitions
1)Exchange sub partitioningALTER TABLE DEPARTMENT EXCHANGE SUBPARTITION SYS_SUBP7356 WITH TABLE TEMP_DEPARTMENT WITHOUT VALIDATION UPDATE GLOBAL INDEXES;
2) Truncate temp exchange table truncate table TEMP_DEPARTMENT;
3) Truncate sub partitioning (sub partition name from ALL_TAB_SUBPARTITIONS table) with global index update `ALTER TABLE DEPARTMENT TRUNCATE SUBPARTITION SYS_SUBP7356 DROP STORAGE UPDATE GLOBAL INDEXES;
Here the above 1 to 3 steps are executed for three sub-partitions
4) Get this partition name from all_tab_partitions table with global index update ALTER TABLE DEPARTMENT DROP PARTITION SYS_P7359 UPDATE GLOBAL INDEXES;
5) Truncate sub partitioning (sub partition name from ALL_TAB_SUBPARTITIONS table) with global index update ALTER TABLE EMPLOYEE TRUNCATE SUBPARTITION SYS_SUBP7360 DROP STORAGE UPDATE GLOBAL INDEXES;
Here the above 5 steps are executed for three sub-partitions
6) Get this partition name from all_tab_partitions table with global index update ALTER TABLE EMPLOYEE DROP PARTITION SYS_P7363 UPDATE GLOBAL INDEXES;
7) alter index PK_DEPARTMENT rebuild;
8) alter index IDX1_DEPARTMENT rebuild;
9) alter index PK_EMPLOYEE rebuild;
Steps 7 to 9 is require only when UPDATE GLOBAL INDEXES not used at the time of Altering Table for exchange, truncate and drop partition or sub partition
10)
begin
dbms_stats.gather_table_stats(ownname=>'TEST', tabname=>'DEPARTMENT', granularity=>'ALL' ,no_invalidate=>FALSE);
dbms_stats.gather_table_stats(ownname=>'TEST', tabname=>'EMPLOYEE', granularity=>'ALL' ,no_invalidate=>FALSE);
end;
UPDATE GLOBAL INDEXES is used at the time of Altering Table for exchange, truncate and drop partition or sub partition where global indexes are applied on that table then we can do DML operation on same table in parallel here.
UPDATE GLOBAL INDEXES is used to rebuild the indexes at same time with alter operation.
If UPDATE GLOBAL INDEXES is not used then DML operation is not allowed on same table in parallel. It will throw below ORA Exception for global indexes.
ERROR Message when perform insert operation after drop/exchange/truncate partition/sub partition from table:
SQL Error: ORA-01502: index 'TEST.PK_DEPARTMENT' or partition of such
index is in unusable state
01502. 00000 - "index '%s.%s' or partition of such index is in unusable state"
Cause: An attempt has been made to access an index or index partition that has been marked unusable by a direct load or by a DDL operation
Action: DROP the specified index, or REBUILD the specified index, or REBUILD the unusable index partition
If you don’t want to use UPDATE GLOBAL INDEXES and want parallel DML operation with purging then make indexes Local (must not contain primary key or unique constraint because it is default created global unique index). It’s on partition and sub partition and refers default table space.
With Local indexes you observed slow insertion on that table.

Oracle: ORA-01408: such column list already indexed

I have created a table:
CREATE TABLE SCHEMA_NAME.TABLE_NAME
(
ID VARCHAR2(100) PRIMARY KEY NOT NULL,
A_ID VARCHAR2(100) NOT NULL,
B_ID VARCHAR2(100),
C_ID INTEGER,
CONSTRAINT FK_C_NOTE FOREIGN KEY (C_ID) REFERENCES C(ID),
CONSTRAINT FK_A_NOTE FOREIGN KEY (A_ID) REFERENCES A(ID),
CONSTRAINT FK_B_NOTE FOREIGN KEY (B_ID) REFERENCES B(ID)
);
And I have tried to apply the following indexes:
CREATE INDEX IDX_FK_A_TABLE_NAME on SCHEMA_NAME.TABLE_NAME(A_ID) tablespace TS_SCHEMA_NAME_DATA;
CREATE INDEX IDX_FK_C_TABLE_NAME on SCHEMA_NAME.TABLE_NAME(C_ID) tablespace TS_SCHEMA_NAME_DATA; --Fails
CREATE INDEX IDX_FK_B_TABLE_NAME on SCHEMA_NAME.TABLE_NAME (B_ID) tablespace TS_SCHEMA_NAME_DATA; --Fails
But when I try to create the last two indexes I get:
ORA-01408: such column list already indexed
Why is this? Are these indexes created automatically?
If I try:
SELECT INDEX_NAME FROM ALL_INDEXES WHERE TABLE_NAME = 'NOTE';
I get:
INDEX_NAME
----------
IDX_FK_A_TABLE_NAME
SYS_C0044692561
Double check you indexed columns by querying ALL_IND_COLUMN (or DBA_IND_COLUMNS if you have acess). It will show you what table columns are indexed by which index.
SELECT c.index_owner, c.index_name, c.table_owner, c.table_name, c.column_name, c.column_position
FROM all_ind_columns c
WHERE c.table_owner = 'SCHEMA_NAME'
AND c.table_name = 'NOTE'
ORDER BY c.index_owner, c.index_name, c.column_position;
On a site note, When you are creating your indexes you're not prefixing them with a schema owner (CREATE INDEX SCHEMA.IDX_FK_A_TABLE_NAME ...). That means they're being created in your current schema. On the other hand, it's probably not related to the error you are facing.

Resources