Home > Database > Mysql Tutorial > body text

MySQL query to make date column NULL?

WBOY
Release: 2023-09-23 13:53:08
forward
1393 people have browsed it

MySQL 查询使日期列为 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;
Copy after login

First let us create a table. Here, we set the column to NOT NULL −

mysql> create table DemoTable
(
   ShippingDate date NOT NULL
);
Query OK, 0 rows affected (0.78 sec)
Copy after login

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
Copy after login

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
Copy after login

Now, try inserting NULL into the above table using the insert command again. Since we have modified the table to accept NULL, no error will be generated −

mysql> insert into DemoTable values(null);
Query OK, 1 row affected (1.21 sec
Copy after login

Display all records from the table using select statement −

mysql> select *from DemoTable;
Copy after login

This will produce the following output −

+--------------+
| ShippingDate |
+--------------+
| NULL         |
+--------------+
1 row in set (0.00 sec)
Copy after login

The above is the detailed content of MySQL query to make date column NULL?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template