
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How to insert NULL into char(1) in MySQL?
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)
- Related Articles
- Insert NULL value into database field with char(2) as type in MySQL?
- Insert NULL value into INT column in MySQL?
- Insert default into not null column if value is null in MySQL?
- Java application to insert null value into a MySQL database?
- How to insert DECIMAL into MySQL database?
- How do I insert a NULL value in MySQL?
- Insert MAX(col)+1 into the same MySQL table?
- How to insert own values into auto_increment column in MySQL?
- How to Insert custom date into MySQL timestamp field?
- What happens if we provide NULL as an argument to MySQL CHAR() function?
- How to insert data into a MySQL database with Java?
- How to write MySQL procedure to insert data into a table?
- Which one is better to insert NULL or empty string in MySQL?
- How to correctly use INSERT INTO … SELECT in MySQL to avoid Error 1064?
- How to insert NULL keyword as a value in a character type column of MySQL table having NOT NULL constraint?

Advertisements