Solved

how to change column default value to no default in 11gr2

Posted on 2013-11-20
7
415 Views
Last Modified: 2013-11-22
Have the question as follows:
create table scott.t1(col1 varchar2(1) default null, col2 varchar2(10));
create table scott.t2(col1 varchar2(1), col2 varchar2(10));

create table scott.tt (table_name varchar2(35), column_name varchar2(35), data_default varchar2(20));

begin
    for a in (select table_name, column_name, DATA_DEFAULT from DBA_TAB_COLS where owner = 'SCOTT' and table_name in ('T1', 'T2') ) loop
       insert into scott.tt values(a.table_name, a.column_name, a.data_default);
    end loop;
end;
/

select * from scott.tt order by 1,2,3;

TABLE_NAME COLUMN_NAME DATA_DEFAULT
T1      COL1      null
T1      COL2      
T2      COL1      
T2      COL2      

The question is
without dropping T1, how to remove the default value "null" here to make T1 the same as T2???
Can any guru shed some light on it? Thanks a lot.
0
Comment
Question by:jl66
  • 3
  • 2
  • 2
7 Comments
 
LVL 73

Assisted Solution

by:sdstuber
sdstuber earned 450 total points
ID: 39663161
it's a quirk of oracle.

once you define a default, you can't remove it.  You can only change it.

Of course, setting the default to NULL is the same as not having a default though.  So, it's safe.


The only other options would be  drop and recreate the column, which would then move it to the end of the column list.

Or, create the entire table.

Generally easier to just leave the NULL default
0
 
LVL 31

Expert Comment

by:awking00
ID: 39663180
insert into scott.tt values
(a.table_name, a.column_name, decode(upper(a.data_default),'NULL',null));
0
 
LVL 31

Assisted Solution

by:awking00
awking00 earned 50 total points
ID: 39663234
Should be modified to accommodate actual default values -
(a.table_name, a.column_name, decode(upper(a.data_default),'NULL',null,a.data_default));
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 73

Expert Comment

by:sdstuber
ID: 39663245
Inserting, updating or otherwise modifying the content of the "TT" table doesn't change the default value of a column.

It could change a report that looks at column definitions, but that's not what the question asked.
0
 

Author Comment

by:jl66
ID: 39663249
Thanks for the tips. I also checked some doc and links. However I still would like to try the alternatives. Maybe the question is equavilent to that
1) is there any safe way to update the table dba(user,all)_tab_cols?
2) As sdstuber mentioned, drop/re-create the column and re-arrange the order to the original. Could you please show me an example for it or a link?
Thanks
0
 
LVL 73

Accepted Solution

by:
sdstuber earned 450 total points
ID: 39663264
you can't rearrange the order of columns without rebuilding the entire table.

but, if you want to recreate the column it's pretty easy to do


alter table t1 add(col3 varchar2(1));
update t1 set col3 = col1;
alter table t1 drop column col1;
alter table t1 rename column col3 to col1;



No - there isn't a documented, supported, safe way to alter the data dictionary to remove the default.
0
 

Author Closing Comment

by:jl66
ID: 39669785
Thanks for the answers
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now