Home  >  Article  >  Database  >  Inserting NULL value into INT column in MySQL?

Inserting NULL value into INT column in MySQL?

王林
王林forward
2023-09-01 11:05:03917browse

在 MySQL 中将 NULL 值插入 INT 列?

You can insert NULL value into an int column using condition, i.e. the column must not have NOT NULL constraint. The syntax is as follows.

INSERT INTO yourTableName(yourColumnName) values(NULL);

To understand the above syntax, let us first create a table. The query to create the table is as follows.

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

The following is the query that inserts NULL whenever you don't pass any value for the column. This column here is StudentAge. MySQL inserts null values ​​by default. The query to insert records is as follows.

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)

Display all records in the table and check whether a NULL value is inserted in the INT column. The query is as follows.

mysql> select *from InsertNullDemo;

The following is the output showing NULL in the INT column.

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

The above is the detailed content of Inserting NULL value into INT column in MySQL?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete