
- 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 set default value to NULL in MySQL?
Use DEFAULT keyword in MySQL to set default value to NULL. Let us first create a −
mysql> create table DemoTable1440 -> ( -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> StudentName varchar(20) DEFAULT NULL, -> StudentAge int DEFAULT NULL -> ); Query OK, 0 rows affected (0.55 sec)
Insert some records in the table using insert command. For values left blank, the default gets inserted −
mysql> insert into DemoTable1440(StudentName,StudentAge) values('Chris',21); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1440 values(); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable1440(StudentName) values('David'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable1440(StudentAge) values(24); Query OK, 1 row affected (0.09 sec)
Display all records from the table using select −
mysql> select * from DemoTable1440;
This will produce the following output −
+-----------+-------------+------------+ | StudentId | StudentName | StudentAge | +-----------+-------------+------------+ | 1 | Chris | 21 | | 2 | NULL | NULL | | 3 | David | NULL | | 4 | NULL | 24 | +-----------+-------------+------------+ 4 rows in set (0.00 sec)
- Related Articles
- How to set default Field Value in MySQL?
- How to set MySQL default value NONE?
- How to check whether column value is NULL or having DEFAULT value in MySQL?
- How to set default value for empty row in MySQL?
- How to set NOW() as default value for datetime datatype in MySQL?
- Insert default into not null column if value is null in MySQL?
- Set default value to a JSON type column in MySQL?
- How to set default value to align content in CSS ?
- Is it necessary to add DEFAULT NULL in MySQL?
- Set 1 for NOT NULL value in MySQL
- How to modify column default value in MySQL?
- How to revert rows to default column value in MySQL?
- How to find records with a null value in a set of columns with MySQL
- Adding a column whose value is not null by default in MySQL?
- How do I set the default value for a column in MySQL?

Advertisements