Domanda

If I run this query on my oracle database:

SELECT COLUMN_NAME, 
       DATA_DEFAULT, 
       DATA_TYPE
  FROM USER_TAB_COLUMNS
 WHERE TABLE_NAME = 'MY_TABLE' 
   AND DATA_DEFAULT IS NOT NULL;

The result I'm getting is:

STOICHIOMETRY   NULL    NUMBER

My Question is WHY? I've asked for not NULL.

If I run:

describe MY_TABLE

I get:

Name          Null     Type         
------------- -------- ------------ 
TID           NOT NULL NUMBER(9)    
COMPONENT_ID  NOT NULL NUMBER(9)    
RELATIONSHIP  NOT NULL VARCHAR2(20) 
STOICHIOMETRY          NUMBER(3)    
TARGCOMP_ID   NOT NULL NUMBER(9)    
HOMOLOGUE     NOT NULL NUMBER(1)
È stato utile?

Soluzione 3

I think NULL is your default value? For example I have a table and I've altered the default value to NULL:

ALTER TABLE UNIQUEVALTEST MODIFY ZIP DEFAULT NULL;

Now when I select:

SELECT COLUMN_NAME, DATA_DEFAULT
FROM ALL_TAB_COLUMNS WHERE TABLE_NAME = 'UNIQUEVALTEST'
AND data_default IS NOT NULL;

I get:

ZIP NULL

Edit: To explain better, if the field was (null), then there is no default value. But the default value is now set to NULL, so any new data inserted would automatically be NULL unless specified otherwise. I don't know if that changes the functionality at all (having no default value, or having the default value set to NULL), but I would think it would operate the same.

Altri suggerimenti

The nullability of a column is stored in NULLABLE, not in DATA_DEFAULT. The DATA_DEFAULT stores the default value for a column (and if you haven't provided a default value in the CREATE TABLE statement, all columns get DEFAULT NULL (even non-nullable ones!)

Try:

SELECT COLUMN_NAME, 
       DATA_DEFAULT, 
       DATA_TYPE,
       NULLABLE
  FROM USER_TAB_COLUMNS
 WHERE TABLE_NAME = 'MY_TABLE' 
   AND NULLABLE = 'N';              -- 'N' means No, not nullable
                                    -- 'Y' means Yes, nullable

It's very likely that you, as @Rob correctly pointed out, assigned NULL as a default value for a column. Unfortunately, you cannot remove once assigned default value for a column - the data_default column of user_tab_columns data dictionary view will always contain data.

Note: If a column has a default value, then you can use the DEFAULT clause to change the default to NULL, but you cannot remove the default value completely. If a column has ever had a default value assigned to it, then the DATA_DEFAULT column of the USER_TAB_COLUMNS data dictionary view will always display either a default value or NULL.

If you want this condition DATA_DEFAULT IS NOT NULL to succeed, the only option is to recreate the column without default clause.

  1. create a new column
  2. Copy data from the damaged one
  3. Drop the column, which has been created with default clause
  4. Rename the newly created column.

Example:

create table TB_Test(
  col number default null
);

insert into tb_test(col)
  select level
   from dual
   connect by level <= 11;


select column_name,
       data_default,
       data_type
  from user_tab_columns
  where table_name = upper('tb_test')
    and data_default is not null

Column_Name Data_Default Data_Type 
--------------------------------------
COL         null         NUMBER 
             ^
             |
        string literal

SQL> alter table tb_test add col2 number;
Table altered

update tb_test
   set col2 = col

SQL> alter table tb_test drop column col;
Table altered

SQL> alter table tb_test rename column col2 to col;
Table altered

select column_name,
       data_default,
       data_type
  from user_tab_columns
  where table_name = upper('tb_test')
    and data_default is not null

no rows selected
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top