How to Set a Column Value to NULL in MariaDB
Last Updated :
13 Feb, 2024
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 setting. In this article, we will look through the different methods with which we can set a column value to NULL in MariaDB.
Set a Column Value to NULL in MariaDB
The fundamental syntax to set a column value to NULL is as follows.
Syntax:
UPDATE table_name
SET column_name = NULL
WHERE condition;
Explanation:
- table_name: Name of your table.
- column_name: Name of the column you want to set null.
- WHERE condition: Condition with any necessary criteria to identify the records you want to update.
Method to Set a Column Value to NULL in MariaDB
Method 1: Using UPDATE Statement
First let's create a table and insert data into it.
Create a Table:
CREATE TABLE employees (
employee_id INT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
department VARCHAR(50),
salary DECIMAL(10, 2),
Hire_date DATE
);
Insert Data:
INSERT INTO employees VALUES
(1, 'Kavya', 'Sharma', 'Finance', 50000.00,'2012-01-15'),
(2, 'Vardhana', 'Sharma', 'IT', 60000.00,'2022-02-20'),
(3, 'Bob', 'Johnson', 'Sales', 75000.00,'2019-03-10'),
(4, 'Vivek', 'Sharma', 'IT', 65000.00,'2022-02-20'),
(5, 'Minal', 'Pandey', 'Sales', 70000.00,'2022-03-10');
Output:
Employees TableExample 1:
Suppose we want to set to NULL for all employees who hired before 2020:
Query:
UPDATE employees SET salary = NULL WHERE Hire_date < '2020-01-01';
SELECT * FROM employees;
This query will delete salary details from the database for all employees who were hired before 1 January 2020, by setting the salary value to NULL. This operation is often utilized for such purposes as salary records updating or data cleaning.
Output:
Example 1Example 2:
If you want to set column value NULL for employees who belongs to IT department.
Query:
UPDATE employees set department = NULL WHERE department = 'IT';
SELECT * FROM employees;
Output:
Example 2Explanation: The execution of this query will remove the department assignment for all employees who are part of the 'IT' department by updating their department values to NULL. This operation can be helpful for activities like relocating staff to different departments or for cleaning up data.
Example 3:
Set a column value null without specifying where condition.
Query:
UPDATE employees SET last_name = NULL;
SELECT * FROM employees;
Output:
Example 3Explanation: After running this query you are actually deleting the last names for all the employees in the employees table, leaving the last_name column with NULL values for each record. You should be careful while executing this operation, as it permanently erases the data in the last_name column with no chance to revert the changes unless you have a backup of the data.
Method 2: Using ALTER Statement
If we want to set the default value of a column to NULL or if we want to allow NULL values in a column that previously did not allow them then we can use the ALTER TABLE statement.
Query:
ALTER TABLE table_name MODIFY column_name datatype NULL;
Example 1:
Suppose in our employees table we want to allow null values for the Hire_date column.
Query:
ALTER TABLE employees MODIFY Hire_date DATE NULL;
Output:
Allow NULL ValuesExplanation: The query is asking MariaDB to change the employees table by modifying the Hire_date column to DATE and making that column allow NULL values. The structure of the table would consequently change which may affect how the data in the Hire_date column is stored and retrieved.
Conclusion
In MariaDB the task of setting the value of a column to NULL is a simple process. It can be performed using either the UPDATE statement or the ALTER TABLE statement. According to your particular preferences, you can make selection the method that suits your needs. Irrespective of whether you’ll be updating existing entries or modifying table columns, MariaDB grants you the required tools to smoothly handle NULL values in your database.
Similar Reads
How to Set a Column Value to Null in SQL?
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 th
2 min read
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 Rename a Column Name in MariaDB?
MariaDB is an open-source relational database management system that can be used to create databases, and tables and to query the tables. Anyone who knows how to write SQL queries can use any of the databases like MariaDB or Mysql for the execution of SQL queries. In this article, we will learn abou
4 min read
How to Get the Data Type of a Columns in MariaDB
When it comes to managing databases, understanding the types of data stored in each column is crucial. In MariaDB, this knowledge not only helps in organizing data efficiently but also enables more effective querying and analysis. In this article, we'll explore How to Get the Data Type of Columns in
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 Use NULL Values Inside NOT IN Clause in SQL?
In SQL, NULL holds a special status as it represents the absence of a value, making it fundamentally different from regular values. Unlike numbers or strings, NULL cannot be directly compared using operators like = or !=. This special status often leads to unexpected behavior in SQL queries, especia
4 min read
How to Update NULL Values in a Field in MySQL
There is a situation where we need to update certain columns with NULL values in a MySQL database, you're in the right place. It's a common task when you're working with databases and dealing with incomplete or undefined data. In this article, we'll walk through the process, break down the syntax, a
3 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 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