How to insert NULL into char(1) in MySQL?

MySQLMySQLi Database

For this, you need to set sql_mode to 'STRICT_TRANS_TABLES’. This mode issues a warning when an invalid value is inserted but inserts the same value. Let us first create a table −

mysql> create table DemoTable
(
   Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   Name varchar(50),
   Gender char(1) NULL
);
Query OK, 0 rows affected (0.99 sec)

Insert some records in the table using insert command −

mysql> set sql_mode = 'STRICT_TRANS_TABLES';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> insert into DemoTable(Name,Gender) select 'Chris',NULL ;
Query OK, 1 row affected (0.21 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> insert into DemoTable(Name,Gender) select 'David',NULL ;
Query OK, 1 row affected (0.16 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> insert into DemoTable(Name,Gender) select 'Mike','M' ;
Query OK, 1 row affected (0.16 sec)
Records: 1 Duplicates: 0 Warnings: 0

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+----+-------+--------+
| Id | Name  | Gender |
+----+-------+--------+
|  1 | Chris | NULL   |
|  2 | David | NULL   |
|  3 | Mike  | M      |
+----+-------+--------+
3 rows in set (0.00 sec)
raja
Published on 03-Oct-2019 10:21:11
Advertisements