UPDATING NOT NULL Column into NULL
In this, let’s we are going to remove NOT NULL constraint from our table and update that column value to NULL.
Query:
ALTER TABLE w3wiki
ALTER COLUMN contest_rank DROP NOT NULL;
After executing this query, NOT NULL constraint is removed from our column contest_rank. Now let’s update the column value to NULL.
Query:
UPDATE w3wiki
SET contest_rank = NULL;
Output:
Explanation: In the above image, we can see that we have updated all the values of ‘contest_rank’ column to NULL. As we have seen previously, contest_rank column is not accepting any NULL values but after removing the NOT NULL constraint, we can ,see that it started to accept NULL values.
How to Set a NOT NULL Column into NULL in PostgreSQL?
PostgreSQL is an open-source relational database management system in short RDBMS. It is commonly known for its reliability and vast feature set. We can clearly state that it is one of the most powerful RDBMS available. We often create some columns with NOT NULL constraints but later on, there will be some possibility that we have to add some NULL values to it. There may be some possible reasons like data cleanup or temporary data manipulation-related tasks. In such cases, we need to remove the NOT NULL constraints from our column.
In this article, we are going to explore a possible way through which we can easily remove and add NOT NULL constraints to our column. We will explore various examples with proper explanations.