MySQL 查询使日期列为 NULL?

2023年 9月 23日 75.5k 0

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;

登录后复制

首先让我们创建一个表格。在这里,我们将列设置为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

登录后复制

现在,再次尝试使用插入命令将NULL插入到上述表中。由于我们已经修改了表以接受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;

登录后复制

这将产生以下输出 −

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

登录后复制

以上就是MySQL 查询使日期列为 NULL?的详细内容,更多请关注每日运维网(www.mryunwei.com)其它相关文章!

相关文章

Oracle如何使用授予和撤销权限的语法和示例
Awesome Project: 探索 MatrixOrigin 云原生分布式数据库
下载丨66页PDF,云和恩墨技术通讯(2024年7月刊)
社区版oceanbase安装
Oracle 导出CSV工具-sqluldr2
ETL数据集成丨快速将MySQL数据迁移至Doris数据库

发布评论