How to Set a NOT NULL Column into NULL in PostgreSQL?
Last Updated :
12 Mar, 2024
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.
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".
Syntax:
ALTER TABLE table_name
ALTER COLUMN column_name DROP NOT NULL;
Creating a Table in Our Database
In this, we will create a table with certain columns. We will also create a column with a NOT NULL constraint.
Table Name: 'geeksforgeeks'
Query:
CREATE TABLE geeksforgeeks(
user_id INT PRIMARY KEY,
username VARCHAR(255),
contest_rank INT NOT NULL,
contest_score INT
);
After executing the above query, a table will be created with the above said columns in our database. Now let's add some data to our table and display them.
Query:
--adding data to our table
INSERT INTO geeksforgeeks (user_id, username, contest_rank, contest_score)
VALUES (108, 'Vishu', 01, 100);
INSERT INTO geeksforgeeks (user_id, username, contest_rank, contest_score)
VALUES (109, 'Ayush', 02, 90);
INSERT INTO geeksforgeeks (user_id, username, contest_rank, contest_score)
VALUES (110, 'Neeraj', 03, 85);
INSERT INTO geeksforgeeks (user_id, username, contest_rank, contest_score)
VALUES (111, 'Sumit', 04, 80);
--displaying the data
SELECT * from geeksforgeeks;
Output:
table - geeksforgeeksExplanation: In the above table, we have created our column 'contest_rank' with let's null constraint. Therefore it will not accept any Null data.
Now, let's try to insert some NULL values to our column with NOT NULL constraint.
Query:
INSERT INTO geeksforgeeks (user_id, username, contest_rank, contest_score)
VALUES (112, 'Vivek', NULL, 70);
Output:
Adding NULL value to a NOT NULL constraint.Explanation: As we have previously discussed we cannot add any NULL values to a column with NOT NULL constraint. In the above image, we can , see that we have attempted to add a NULL value in our 'contest_rank' column. Therefore it is throwing us an error.
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 geeksforgeeks
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 geeksforgeeks
SET contest_rank = NULL;
Output:
Setting contest_rank column to NULLExplanation: 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.
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 geeksforgeeks
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.
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.
Similar Reads
SQL Interview Questions Are you preparing for a SQL interview? SQL is a standard database language used for accessing and manipulating data in databases. It stands for Structured Query Language and was developed by IBM in the 1970's, SQL allows us to create, read, update, and delete data with simple yet effective commands.
15+ min read
SQL Tutorial SQL is a Structured query language used to access and manipulate data in databases. SQL stands for Structured Query Language. We can create, update, delete, and retrieve data in databases like MySQL, Oracle, PostgreSQL, etc. Overall, SQL is a query language that communicates with databases.In this S
11 min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
SQL Commands | DDL, DQL, DML, DCL and TCL Commands SQL commands are crucial for managing databases effectively. These commands are divided into categories such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Data Query Language (DQL), and Transaction Control Language (TCL). In this article, we will e
7 min read
SQL Joins (Inner, Left, Right and Full Join) SQL joins are fundamental tools for combining data from multiple tables in relational databases. Joins allow efficient data retrieval, which is essential for generating meaningful observations and solving complex business queries. Understanding SQL join types, such as INNER JOIN, LEFT JOIN, RIGHT JO
6 min read
Normal Forms in DBMS In the world of database management, Normal Forms are important for ensuring that data is structured logically, reducing redundancy, and maintaining data integrity. When working with databases, especially relational databases, it is critical to follow normalization techniques that help to eliminate
7 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
ACID Properties in DBMS In the world of DBMS, transactions are fundamental operations that allow us to modify and retrieve data. However, to ensure the integrity of a database, it is important that these transactions are executed in a way that maintains consistency, correctness, and reliability. This is where the ACID prop
8 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Steady State Response In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We
9 min read