Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modify a column to NULL - Oracle

Tags:

oracle11g

I have a table named CUSTOMER, with few columns. One of them is Customer_ID.

Initially Customer_ID column WILL NOT accept NULL values.

I've made some changes from code level, so that Customer_ID column will accept NULL values by default.

Now my requirement is that, I need to again make this column to accept NULL values.

For this I've added executing the below query:

ALTER TABLE Customer MODIFY Customer_ID nvarchar2(20) NULL

I'm getting the following error:

ORA-01451 error, the column already allows null entries so
therefore cannot be modified

This is because already I've made the Customer_ID column to accept NULL values.

Is there a way to check if the column will accept NULL values before executing the above query...??

like image 346
Gokul Nath KP Avatar asked Mar 27 '13 11:03

Gokul Nath KP


People also ask

How do you change a column to null in Oracle?

You can use the column NULLABLE in USER_TAB_COLUMNS. This tells you whether the column allows nulls using a binary Y/N flag. If you wanted to put this in a script you could do something like: declare l_null user_tab_columns.

How do you change a column to allow nulls?

ALTER TABLE table_name ALTER COLUMN column_name DATA_TYPE [(COLUMN_SIZE)] NULL; In this syntax: First, specify the name of the table from which you want to change the column. Second, specify the column name with size which you want to change to allow NULL and then write NULL statement .

How do you change a column that is not null in Oracle?

Go to the Object Explorer, Tables View, Right click on the table you want to change, Select Alter Table option, Select Columns option in the left panel, Then check the not null checkboxes in the right, then click ok.


1 Answers

Or you can just ignore the error:

declare
    already_null exception;
    pragma exception_init (already_null , -01451);
begin
    execute immediate 'alter table <TABLE> modify(<COLUMN> null)';
    exception when already_null then null;
end;
/
like image 92
grokster Avatar answered Nov 06 '22 17:11

grokster