How to Set a Column Value to Null in SQL?
Last Updated :
06 Sep, 2023
You can set a column value to NULL using the SQL UPDATE statement. Through the UPDATE statement, existing records in a table can be changed. The fundamental syntax to set a column value to NULL is as follows.
Syntax:
UPDATE table_name
set column_name=NULL
WHERE Conditions;
table_name: The name of the table you want to update should be replaced here.
NULL: It represents the NULL value in SQL.
WHERE: This statement has an optional part that specifies which rows should be updated.
column_name: The name of the column you want to update should be replaced here.
Updating a Column to NULL Value
Firstly, let's create a table using the CREATE TABLE command:
-- create a table
CREATE TABLE students (Sr_No integer,Name varchar(20), Gender varchar(2));
-- insert some values
INSERT INTO students VALUES (1, 'Nikita', 'F');
INSERT INTO students VALUES (2, 'Akshit', 'M');
INSERT INTO students VALUES (3, 'Ritesh', 'F');
INSERT INTO students VALUES (4, 'Himani', 'F');
-- fetch some values
SELECT * FROM students ;
The table would look like this:
student TableCase 1: To UPDATE Column value, use the below command
Syntax:
UPDATE TABLE [TABLE_NAME]
Case 2: To set column value to NULL, use the below command
Syntax:
UPDATE [TABLE_NAME] set [COLUMN_NAME] = NULL where [CRITERIA]
Query
UPDATE students set Gender = NULL where Gender='F';
SELECT * FROM students;
Output
outputColumn value can also be set to NULL without specifying the 'where' condition.
Query
UPDATE students set Gender = NULL;
SELECT * FROM students;
Output
outputIf you have set a constraint that a particular column value can not be NULL, and later try to set it as NULL, then it will generate an error.
Example:
-- create a table
CREATE TABLE students (Sr_No integer,Name varchar(20), Gender varchar(2) NOT NULL);
-- insert some values
INSERT INTO students VALUES (1, 'Nikita', 'F');
INSERT INTO students VALUES (2, 'Akshit', 'M');
INSERT INTO students VALUES (3, 'Ritesh', 'F');
INSERT INTO students VALUES (4, 'Himani', 'F');
-- fetch some values
SELECT * FROM students;
Output
students table Query
UPDATE students set Gender = NULL where Gender ='F';
Output
ERROR: Gender may not be NULL.
Conclusion
In this article 'How to Set a Column Value to Null in SQL', we have reached to some of the basic conclusions, that are listed below.
- Use the table name in the UPDATE statement.
- Put NULL in the column name.
- To select which rows to update, use a WHERE clause.
- To make the modification, run the SQL query.
Similar Reads
How to Set a Column Value to Null in PL/SQL?
In PL/SQL, setting a column value to NULL is a common requirement when working with databases. Understanding how to set column values to NULL is essential for database developers and administrators. In this article, we will look into the concept of setting a column value to NULL in PL/SQL, covering
4 min read
How to Set a Column Value to NULL in SQL Server
In the world of database management, SQL Server is a leading and extensively utilized system. A fundamental task within SQL Server is manipulating data within tables, and setting a column value to NULL is a common operation. Whether it's for maintaining data integrity, performing updates, or meeting
4 min read
How to Set a Column Value to NULL in MariaDB
In MariaDB, the NULL represents an unknown value in a column. Changing a column value to NULL is the most common operation performed in MariaDB that allows us to remove existing data in a specific field. It is applicable in different ways including data correction, record inclusions and values setti
4 min read
How To Reset Identity Column Values In SQL
An Identity Column in SQL is an auto-incrementing column used to generate unique values for each row inserted into a table. This feature is commonly used in tables that require a unique identifier, such as primary keys. The identity column allows the database to automatically handle the generation o
5 min read
How to Update a Column in a Table in SQL Server
In the database management area, updating columns in a table is a normal query and it is important software that ensures the accuracy and integrity of data. Whether we are making spelling corrections, editing or altering existing information, or being attentive to changing requirements, carrying out
4 min read
How to Filter Rows Without Null in a Column in SQL?
Here we will see, how to filter rows without null in a column of an MS SQL Server's database table with the help of a SQL query using IS NOT NULL operator. For the purpose of demonstration, we will be creating a demo_orders table in a database called âgeeksâ. Creating the Database: Use the below SQL
2 min read
How to Rename a Column in PL/SQL?
Renaming a column in PL/SQL is a fundamental operation in Oracle Database management. It enhances clarity, maintains consistency, or accommodates evolving data requirements. Database administrators can ensure the data integrity and process of streamlining data manipulation by altering the column nam
4 min read
How To Update Multiple Columns in MySQL?
To update multiple columns in MySQL we can use the SET clause in the UPDATE statement. SET clause allows users to update values of multiple columns at a time. In this article, we will learn how to update multiple columns in MySQL using UPDATE and SET commands. We will cover the syntax and examples,
3 min read
How to Change a Column Name in SQL?
The ALTER TABLE statement in SQL is a powerful command used to modify the structure of an existing table without affecting its data. It enables changes like adding, dropping, renaming or altering columns in the table. Among these operations, altering a column with the CHANGE or RENAME command is com
3 min read
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
4 min read