I'm trying to fill in for a colleague in doing some Oracle work, and ran into a snag. In attempting to write a script to modify a column to nullable, I ran into the lovely ORA-01451 error:
ORA-01451: column to be modified to NULL cannot be modified to NULL
This is happening because the column is already NULL. We have several databases that need to be udpated, so in my faulty assumption I figured setting it to NULL should work across the board to make sure everybody was up to date, regardless of whether they had manually set this column to nullable or not. However, this apparently causes an error for some folks who already have the column as nullable.
How does one check if a column is already nullable so as to avoid the error? Something that would accomplish this idea:
IF( MyTable.MyColumn IS NOT NULLABLE) ALTER TABLE MyTable MODIFY(MyColumn NULL);
Select Columnproperty(object_id('Schema. table','U'),'column','allowsnull'); But this is for SQL. Select is_nullable from sys. colmns where object_id = object_id('schema.
NOT NULL constraint specifies that a column cannot contain NULL values. To add a NOT NULL constraint to an existing table by using the ALTER TABLE statement. ALTER TABLE table_name MODIFY ( column_name NOT NULL); In this case, the column_name must not contain any NULL value before applying the NOT NULL constraint.
1) Select the table in which you want to modify changes. 2) Click on Actions.. ---> select column ----> add. 3) Now give the column name, datatype, size, etc. and click ok.
To add not null constraint to an existing column in MySQL, we will use the ALTER command. This is a type of validation to restrict the user from entering null values.
You could do this in PL/SQL:
declare l_nullable user_tab_columns.nullable%type; begin select nullable into l_nullable from user_tab_columns where table_name = 'MYTABLE' and column_name = 'MYCOLUMN'; if l_nullable = 'N' then execute immediate 'alter table mytable modify (mycolumn null)'; end if; end;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With