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