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.