Insert NULL value into INT column in MySQL?

MySQLMySQLi Database

You can insert NULL value into an int column with a condition i.e. the column must not have NOT NULL constraints. 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 a table is as follows.

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

Here is the query to insert NULL whenever you do not pass any value for column. Here this column is StudentAge. MySQL inserts null value 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 from the table to check the NULL value is inserted or not into INT column. The query is as follows.

mysql> select *from InsertNullDemo;

The following is the output displaying NULL in INT column.

+-----------+-------------+------------+
| StudentId | StudentName | StudentAge |
+-----------+-------------+------------+
| 101       | Mike       | NULL       |
| 101       | Mike       | NULL       |
+-----------+-------------+------------+
2 rows in set (0.00 sec)
raja
Published on 11-Jan-2019 07:21:09
Advertisements