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 SQLite?
SQLite is a lightweight and self-contained relational database management system in short RDBMS. Its has a server-less architecture which makes it a better option for small desktop and mobile applications. It also requires very low configuration which eventually helps the developer to integrate it i
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 Rename Column in MariaDB
In database management, maintaining an efficient and well-organized schema is important. One common task is to rename columns which can enhance clarity, attach to naming conventions or accommodate structural changes. In this article, we'll learn about various methods along with examples such as ALTE
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 Remove A Column In R
R is a versatile language that is widely used in data analysis and statistical computing. A common task when working with data is removing one or more columns from a data frame. This guide will show you various methods to remove columns in R Programming Language using different approaches and provid
4 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