
- 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
Set default value to a JSON type column in MySQL?
To set default value, use the DEFAULT constraint as in the below syntax −
alter table yourTableName modify column yourColumnName JSON NOT NULL DEFAULT ( JSON_OBJECT() );
Let us create a table −
mysql> create table demo24 −> ( −> employee_information text −> ) −> ; Query OK, 0 rows affected (1.43 sec)
Here is the description of table. Following is the query −
mysql> desc demo24;
This will produce the following output −
+----------------------+------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------------------+------+------+-----+---------+-------+ | employee_information | text | YES | | NULL | | +----------------------+------+------+-----+---------+-------+ 1 row in set (0.00 sec)
Following is the query to change it to JSON data type with default value −
mysql> alter table demo24 modify column employee_information JSON NOT NULL DEFAULT ( JSON_OBJECT() ); Query OK, 0 rows affected (3.03 sec) Records: 0 Duplicates: 0 Warnings: 0
Now check the description of table. Following is the query −
mysql> desc demo24;
This will produce the following output −
+----------------------+------+------+-----+---------------+-------------------+ | Field | Type | Null | Key | Default | Extra | +----------------------+------+------+-----+---------------+-------------------+ | employee_information | json | NO | | json_object() | DEFAULT_GENERATED | +----------------------+------+------+-----+---------------+-------------------+ 1 row in set (0.00 sec)
Insert some records into the table with the help of insert command −
mysql> insert into demo24 values();; Query OK, 1 row affected (0.10 sec)
Display records from the table using select statement −
mysql> select *from demo24;
This will produce the following output −
+----------------------+ | employee_information | +----------------------+ | {} | +----------------------+ 1 row in set (0.00 sec)
- Related Articles
- How do I set the default value for a column in MySQL?
- How to modify column default value in MySQL?
- How to set default Field Value in MySQL?
- How to set MySQL default value NONE?
- How to set default value to NULL in MySQL?
- How to revert rows to default column value in MySQL?
- How to set default value for empty row in MySQL?
- What is the default type of a hexadecimal value in MySQL?
- How to add time in a MySQL column set with type DATETIME?
- How to check whether column value is NULL or having DEFAULT value in MySQL?
- Adding a column whose value is not null by default in MySQL?
- How to create boolean column in MySQL with false as default value?
- How can I insert default value in MySQL ENUM data type?
- How to set NOW() as default value for datetime datatype in MySQL?
- How to set a default column size in CSS Grid

Advertisements