
- 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 for empty row in MySQL?
To set default value for empty row, use the concept of COALESCE(). Let us first create a table −
mysql> create table DemoTable1863 ( FirstName varchar(20) ); Query OK, 0 rows affected (0.00 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1863 values('Chris'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1863 values(NULL); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1863 values('David'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1863 values(NULL); Query OK, 1 row affected (0.00 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1863;
This will produce the following output −
+-----------+ | FirstName | +-----------+ | Chris | | NULL | | David | | NULL | +-----------+ 4 rows in set (0.00 sec)
Following is the query to set default value for empty row −
mysql> select coalesce(FirstName,'UNKNOWN NAME') from DemoTable1863;
This will produce the following output −
+------------------------------------+ | coalesce(FirstName,'UNKNOWN NAME') | +------------------------------------+ | Chris | | UNKNOWN NAME | | David | | UNKNOWN NAME | +------------------------------------+ 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 set NOW() as default value for datetime datatype in MySQL?
- How to set default value to NULL in MySQL?
- How do I set the default value for a column in MySQL?
- Set a custom value for NULL or empty values in MySQL
- Set a default row size for CSS Grid
- How to use a function for default value in MySQL?
- Set default value to a JSON type column in MySQL?
- How to set a default parameter value for a JavaScript function?
- How to modify column default value in MySQL?
- How to set default value to align content in CSS ?
- How to select an empty result set in MySQL?
- How can I set the default value for an HTML element?
- Set the default character set in MySQL

Advertisements