Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make column default to NULL explicitly

How do I make a column default to NULL explicitly?

I would like to declare a column in Oracle SQL Developer to be NULL on default. I'm aware of the fact, that NULL will be the default value, if I do not define any default value at all. But how do I define NULL as default, if I would want to do it explicitly?

-- 1: Does not work.
ALTER TABLE MY_TABLE ADD (
  MY_COLUMN TIMESTAMP(6) DEFAULT null
);

-- 2: Does not work.
ALTER TABLE MY_TABLE ADD (
  MY_COLUMN TIMESTAMP(6) DEFAULT NULL
);

-- 3: Does not work.
ALTER TABLE MY_TABLE ADD (
  MY_COLUMN TIMESTAMP(6) DEFAULT (null)
);

-- 4: This works.
ALTER TABLE MY_TABLE ADD (
  MY_COLUMN TIMESTAMP(6)
);

In case 1-3 the default value will be a String ("NULL", "null" or "(null)"), but not an actual NULL value. So, what am I missing here?

// Edit:

Case (a) and (b) correspond to case 1 and 2. A text value of null or NULL is displayed in SQL Developer. Case (c) corresponds to case 4, where a real (null) value is set explicitly. The screenshots were taken on a table's Columns tab in SQL Developer.

SQL Developer http://s1.postimg.org/fclraa0dp/SQL_Developer.png

like image 861
user1438038 Avatar asked Sep 22 '15 09:09

user1438038


People also ask

Can default have NULL values?

If no default value is declared explicitly, the default value is the null value. This usually makes sense because a null value can be considered to represent unknown data. In a table definition, default values are listed after the column data type.

How do you set a column to NULL in SQL?

You can use this query, to set the specific row on a specific column to null this way: Update myTable set MyColumn = NULL where Field = Condition. Here, the above code will set the specific cell to null as per the inner question (i.e. To clear the value from a cell and make it NULL).

Is a column NULL by default SQL?

By default, a column can hold NULL values. The NOT NULL constraint enforces a column to NOT accept NULL values. This enforces a field to always contain a value, which means that you cannot insert a new record, or update a record without adding a value to this field.


1 Answers

As null, NULL and (null) are the same thing, I don't understand what the problem is.

It is also not a SQL Developer "problem".

Oracle simply stores the default expression exactly as you wrote it in the system catalog. SQL Developer simply displays that.

Assume the following statements:

create table my_table (id integer);
alter table my_table add my_column_1 timestamp(6) default null;
alter table my_table add my_column_2 timestamp(6) default null;
alter table my_table add my_column_3 timestamp(6) default (null);

Then

select column_id, column_name, data_type, data_default
from user_tab_columns
where table_name = 'MY_TABLE'
order by column_id;

Will return the following:

COLUMN_ID | COLUMN_NAME | DATA_TYPE    | DATA_DEFAULT
----------+-------------+--------------+-------------
        1 | ID          | NUMBER       |             
        2 | MY_COLUMN_1 | TIMESTAMP(6) | NULL        
        3 | MY_COLUMN_2 | TIMESTAMP(6) | null        
        4 | MY_COLUMN_3 | TIMESTAMP(6) | (null)      

When you extract the DDL from the system, you again get exactly why you have written:

select dbms_metadata.get_ddl('TABLE', 'MY_TABLE', user)
from dual;

returns:

  CREATE TABLE "TK_HIRAC"."MY_TABLE" 
   (    "ID" NUMBER(*,0), 
    "MY_COLUMN_1" TIMESTAMP (6) DEFAULT NULL, 
    "MY_COLUMN_2" TIMESTAMP (6) DEFAULT null, 
    "MY_COLUMN_3" TIMESTAMP (6) DEFAULT (null)
   ) SEGMENT CREATION DEFERRED 
  PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 
 NOCOMPRESS NOLOGGING
  TABLESPACE "USERS" 
like image 196
a_horse_with_no_name Avatar answered Oct 06 '22 14:10

a_horse_with_no_name