在 MySQL 中将 NULL 值插入 INT 列?

mysqlmysqli database

您可以在有条件的情况下将 NULL 值插入 int 列,即该列不能具有 NOT NULL 约束。语法如下。

INSERT INTO yourTableName(yourColumnName) values(NULL);

为了理解上述语法,让我们首先创建一个表。创建表的查询如下。

mysql> create table InsertNullDemo
-> (
-> StudentId int,
-> StudentName varchar(100),
-> StudentAge int
-> );
Query OK, 0 rows impacted (0.53 sec)

以下是当您没有为列传递任何值时插入 NULL 的查询。此处此列为 StudentAge。MySQL 默认插入空值。插入记录的查询如下。

mysql> insert into InsertNullDemo(StudentId,StudentName) values(101,'Mike');
Query OK, 1 row affected (0.19 sec)

mysql> insert into InsertNullDemo values(101,'Mike',NULL);
Query OK, 1 row affected (0.24 sec)

显示表中的所有记录,以检查 INT 列中是否插入了 NULL 值。查询如下。

mysql> select *from InsertNullDemo;

以下是在 INT 列中显示 NULL 的输出。

+-----------+-------------+------------+
| StudentId | StudentName | StudentAge |
+-----------+-------------+------------+
| 101       | Mike       | NULL       |
| 101       | Mike       | NULL       |
+-----------+-------------+------------+
2 rows in set (0.00 sec)

相关文章