However noticed that CTAS does not copy the default value for each columns
The reason is plain and simple. CTAS is meant to only copy the data and not the metadata(except the table structure).
So, you will lose:
- all constraints
- all indexes
- all grants
Basically, everything that is optionally related to the table.
You will have to pull the metadata and alter table and apply it explicitly.
alter table new_table_name modify (column default 1);
Alternatively, you could:
- Use dbms_metadata.get_ddl to get the table DDL
- Rename the table with new name
- Insert data from old table to new table.
Of course, the latter would be much slower.
UPDATE
There is a way to provide the default value and not null constraint with CTAS:
Setup
SQL> create table t1 (id number default 1 not null, text varchar2(10));
Table created.
SQL> insert into t1 (text) values ('a');
1 row created.
CTAS
SQL> create table t2 (id default 1 not null, text )
2 as select * from t1;
Table created.
SQL> select *from t2;
ID TEXT
---------- ----------
1 a
The new table is created with the default value along with the data from old table.