首页  >  文章  >  数据库  >  在 MySQL 中将 NULL 值插入 INT 列?

在 MySQL 中将 NULL 值插入 INT 列?

王林
王林转载
2023-09-01 11:05:03851浏览

在 MySQL 中将 NULL 值插入 INT 列?

您可以使用条件将 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 affected (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)

以上是在 MySQL 中将 NULL 值插入 INT 列?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文转载于:tutorialspoint.com。如有侵权,请联系admin@php.cn删除