- 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
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)
Advertisements