How to set all column from null to not null in table in oracle

user8487380 picture user8487380 · Mar 28, 2018 · Viewed 8.7k times · Source

I have 1 table having 40 columns. Out of 40 columns only 5 columns are NOT NULL and rest of the columns are set as NULL. How can i set all NULL column to NOT NULL in one time or in TOAD. Is there any possibility to do this except manually set as NOT NULL.

Answer

Jorge Campos picture Jorge Campos · Mar 28, 2018

You can use the Alter Table command to do so. This way:

ALTER TABLE table_name
  MODIFY (column_1 column_type NOT NULL,
          column_2 column_type NOT NULL,
          ...
          column_n column_type);

This will accomplish the changes in all columns at once. Also if your table already has data with null values on those columns, you will have to define default values as well. Here is a sample:

ALTER TABLE table_name
  MODIFY (column_1 varchar2(100) DEFAULT 'some default' NOT NULL,
          column_2 varchar2(75) DEFAULT 'Some Value' NOT NULL);