Adding NOT NULL Constraint to a Column in PostgreSQL

In this, we will add back our NOT NULL constraint to our column ‘contest_rank’. Previously we have removed it from our column but in this topic, we will see how we can add it back to our column.

Query:

ALTER TABLE w3wiki 
ALTER COLUMN contest_rank SET NOT NULL;

The query is nearly same as the previous one but the difference is that instead of DROP command we have used SET.

NOTE: Before setting up a column with NOT NULL constraint, make sure your column do not contains any NULL values. If it does, it will throw you an error.

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.

Similar Reads

How to Set a NOT NULL Column into NULL in PostgreSQL

Setting up a NOT NULL column into NULL is a simple task. We can easily achieve this task with ALTER and DROP statements. ALTER statement is usually used to change the structure of the table and DROP statement is used to remove existing database objects. Together we can use them to achieve our end goal i.e. “Setting up a NOT NULL column into NULL“....

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....

Adding NOT NULL Constraint to a Column in PostgreSQL

In this, we will add back our NOT NULL constraint to our column ‘contest_rank’. Previously we have removed it from our column but in this topic, we will see how we can add it back to our column....

Conclusion

Overall, to set up a NOT NULL column into NULL, we need to remove the NOT NULL constraint from the column. We can achieve this task easily with the help of ALTER and DROP statement. We have covered how we can remove and add back a NOT NULL constraint. We have covered each topic with clear explanation. Now you have a good understanding of removing a NOT NULL constraint. Now you can write queries related to it and get the desired result....