You Asked
Hi Tom,
Here we have a requirement to be done in production database
Our current table structure is something below this
CREATE TABLE &owner..madnog_p_traff_isp_hour
(
subnetwork_id NUMBER not null,
ggsn_id VARCHAR2(100) not null,
funct_id VARCHAR2(10) not null,
isp_id VARCHAR2(10) not null,
period_start_time DATE not null,
period_duration number,
DISCDATAPKT number,
IDATOCTBLOW number,
ODATOCTBLOW number,
IERRINDMSG number,
OERRINDMSG number
) TABLESPACE &tablespace;
All columns that is ,
subnetwork_id NUMBER not null,
ggsn_id VARCHAR2(100) not null,
funct_id VARCHAR2(10) not null,
isp_id VARCHAR2(10) not null
are part of composite primary key and table contains data and now we want to add another column which should be part of composite primary key that already exists ( also note the position of key column in below table definition), that means
CREATE TABLE &owner..madnog_p_traff_isp_hour
(
subnetwork_id NUMBER not null,
ggsn_id VARCHAR2(100) not null,
***node_id VARCHAR2(100) not null, ****
funct_id VARCHAR2(10) not null,
isp_id VARCHAR2(10) not null,
period_start_time DATE not null,
period_duration number,
DISCDATAPKT number,
IDATOCTBLOW number,
ODATOCTBLOW number,
IERRINDMSG number,
OERRINDMSG number
) TABLESPACE &tablespace;
Is it possible for a table that already contains huge data
If so please let us know
Waiting for your reply
Thanks
and Tom said...
If this must be done "online" without significant down time (short locks taken at the very begin and end of this process), then you can use dbms_redefintion.
Else, you would stop modifications, create a new table selecting the default value for this node column you are adding, index, grant, constrain, whatever this new table, drop old and rename the new one.
Because, adding that not-null new column would otherwise perform an UPDATE of every row in the table and you say it is "big", that'll be a really big operation.
Here we change the primary key from (object_id,object_name) to (object_id,object_name,owner) and owner is given a default value of 'XXXX'
ops$tkyte@ORA9IR2> create table t NOLOGGING
2 as
3 select object_id, object_name, created from all_objects;
Table created.
ops$tkyte@ORA9IR2>
ops$tkyte@ORA9IR2> alter table t add constraint t_pk primary key(object_id,object_name);
Table altered.
ops$tkyte@ORA9IR2> exec dbms_redefinition.can_redef_table( user, 'T' );
PL/SQL procedure successfully completed.
ops$tkyte@ORA9IR2>
ops$tkyte@ORA9IR2> create table t_interim
2 as
3 select object_id, object_name, owner, created
4 from all_objects
5 where 1=0;
Table created.
ops$tkyte@ORA9IR2>
ops$tkyte@ORA9IR2> declare
2 l_colmap varchar(512);
3 begin
4 l_colmap :=
5 'object_id,
6 object_name,
7 ''XXXX'' owner,
8 created ';
9
10 dbms_redefinition.start_redef_table
11 ( uname => user,
12 orig_table => 'T',
13 int_table => 'T_INTERIM',
14 col_mapping => l_colmap );
15 end;
16 /
PL/SQL procedure successfully completed.
ops$tkyte@ORA9IR2>
ops$tkyte@ORA9IR2>
ops$tkyte@ORA9IR2> alter table t_interim add constraint t_pk2 primary key(object_id,object_name,owner);
Table altered.
ops$tkyte@ORA9IR2>
ops$tkyte@ORA9IR2> begin
2 dbms_redefinition.sync_interim_table( user, 'T', 'T_INTERIM' );
3 end;
4 /
PL/SQL procedure successfully completed.
ops$tkyte@ORA9IR2> begin
2 dbms_redefinition.finish_redef_table( user, 'T', 'T_INTERIM' );
3 end;
4 /
PL/SQL procedure successfully completed.
ops$tkyte@ORA9IR2>
ops$tkyte@ORA9IR2> desc t
Name Null? Type
---------------------------------------- -------- ----------------------------
OBJECT_ID NOT NULL NUMBER
OBJECT_NAME NOT NULL VARCHAR2(30)
OWNER NOT NULL VARCHAR2(30)
CREATED NOT NULL DATE
ops$tkyte@ORA9IR2> desc t_interim
Name Null? Type
---------------------------------------- -------- ----------------------------
OBJECT_ID NOT NULL NUMBER
OBJECT_NAME NOT NULL VARCHAR2(30)
CREATED NOT NULL DATE
ops$tkyte@ORA9IR2>
ops$tkyte@ORA9IR2> drop table t_interim;
Table dropped.
ops$tkyte@ORA9IR2> drop table t;
Table dropped.
Rating
(3 ratings)
Is this answer out of date? If it is, please let us know via a Comment