I have a table that has several nullable integer columns. This is undesirable for several reasons, so I am looking to update all nulls to 0 and then set these columns to NOT NULL
. Aside from changing nulls to 0
, data must be preserved.
I am looking for the specific SQL syntax to alter a column (call it ColumnA
) to "not null
". Assume the data has been updated to not contain nulls.
Using SQL server 2000.
I had the same problem, but the field used to default to null, and now I want to default it to 0. That required adding one more line after mdb's solution:
For Oracle 11g, I was able to change the column attribute as follows:
Otherwise abatichev's answer seemed good. You can't repeat the alter - it complains (at least in SQL Developer) that the column is already not null.
Making column not null and adding default can also be done in the SSMS GUI.
UPDATE myTable SET myColumn = 0
You can change the definition of existing DB column using following sql.
this worked for me:
In my case I had difficulties with the posted answers. I ended up using the following:
Change
VARCHAR(200)
to your datatype, and optionally change the default value.If you don't have a default value you're going to have a problem making this change, as default would be null creating a conflict.