- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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)
Advertisements