- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
MySQL query to make a date column NULL?
To make a date column null, use ALTER TABLE and MODIFY and set the date to NULL. Following is the syntax −
alter table yourTableName modify column yourColumnName date NULL;
Let us first create a table. Here, we have set the column as NOT NULL −
mysql> create table DemoTable ( ShippingDate date NOT NULL ); Query OK, 0 rows affected (0.78 sec)
Now, insert NULL value in the above table. An error would generate since we have set the column to be NOT NULL −
mysql> insert into DemoTable values(null); ERROR 1048 (23000) − Column 'ShippingDate' cannot be null
Now, let us alter the table and allow NULL in the above table −
mysql> alter table DemoTable modify column ShippingDate date NULL; Query OK, 0 rows affected (1.81 sec) Records : 0 Duplicates : 0 Warnings : 0
Now, again try to insert NULL in the above table using insert command. Error won’t generate since we have altered the table to accept NULL −
mysql> insert into DemoTable values(null); Query OK, 1 row affected (1.21 sec
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+--------------+ | ShippingDate | +--------------+ | NULL | +--------------+ 1 row in set (0.00 sec)
- Related Articles
- MySQL query to remove Null Records in a column?
- How to use a single MySQL query to count column values ignoring null?
- MySQL query that returns a specific string if column is null?
- Retrieve MIN and MAX date in a single MySQL query from a column with date values
- Make all column names lower case in MySQL with a single query
- MySQL query to order by NULL values
- MySQL query to convert empty values to NULL?
- Change a MySQL column to have NOT NULL constraint
- How to add a NOT NULL column in MySQL?
- MySQL query to replace a column value
- MySQL query to select date >= current date - 3 weeks?
- MySQL query to set values for NULL occurrence
- How to update a MySQL date type column?
- How to validate expiry date on a MySQL query?
- MySQL query to select records with a particular date?

Advertisements