ORA-01758: table must be empty to add mandatory (NOT NULL) column

Table must be empty to add mandatory (NOT NULL) column.

While adding NOT NULL column into a table, the following error occurred:

SQL> Alter table TEST_TBL add NAME  CHAR(12) NOT NULL;
Alter table TEST_TBL add NAME  CHAR(12) NOT NULL
            *
ERROR at line 1:
ORA-01758: table must be empty to add mandatory (NOT NULL) column

The workarounds could be either of the following:

1) Default value is given:

SQL> Alter table TEST_TBL add NAME  CHAR(12) DEFAULT 'EMPTY' NOT NULL;

2) Remove NOT NULL:

SQL> Alter TEST_TBL add NAME CHAR(12);

3) Backup the table to another staging table, truncate the table and then add the NOT NULL column, insert the records back to the table from the staging table.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: