How to set NULL values in a table column and insert the same

MySQLMySQLi Database

To set NULL values, set the type as NULL as in the below syntax −

yourColumnName dataType NULL;

Let us first create a table −

mysql> create table DemoTable759 (
   Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   FirstName varchar(100) NULL
);
Query OK, 0 rows affected (0.72 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable759(FirstName) values('John');
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable759(FirstName) values(NULL);
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable759(FirstName) values('Carol');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable759(FirstName) values(NULL);
Query OK, 1 row affected (0.12 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable759;

This will produce the following output. NULL values are also visible in the column −

+----+-----------+
| Id | FirstName |
+----+-----------+
|  1 | John      |
|  2 | NULL      |
|  3 | Carol     |
|  4 | NULL      |
+----+-----------+
4 rows in set (0.00 sec)
raja
Published on 03-Sep-2019 08:13:26
Advertisements