Caba da TI

Caba da TI

Change table column to allow null values in Oracle

Have you ever created a table in Oracle with a not null column and some time later had the need to change it to nullable? This change is simple, see below the code to change a column to allow null in Oracle.

ALTER TABLE TABLE_NAME MODIFY COLUMN_NAME NULL;

Similarly, you may need to change a column to not allow null values. In this case, just use a script like that:

ALTER TABLE TABLE_NAME MODIFY COLUMN_NAME NOT NULL;

If you run the script in Oracle to change the column to not allow null values ​​and an error occurs with the following message: SQL Error [2296] [23000]: ORA-02296: cannot enable … null values ​​found. This error is due to your column already having null values, so it is not possible to change it to not null as Oracle validates all data, including that already existing in the column. To resolve this problem, you must use the NOVALIDATE keyword. When a constraint is created with this keyword, the constraint will only validate new data inserted in the column or changes to the column, failing to validate existing data. This allows you to change the column to NOT NULL even if there are already empty records in that column. See below what the script would look like:

ALTER TABLE TABLE_NAME MODIFY COLUMN_NAME NOT NULL NOVALIDATE;

References

Constraint Checking Updates

Deixe um comentário

O seu endereço de e-mail não será publicado. Campos obrigatórios são marcados com *