Consultas y Transformaci贸n Avanzada de Datos
Dise帽o y Optimizaci贸n Avanzada de Bases de Datos con SQL y MySQL
Construcci贸n de Base de Datos en MySQL: Terminal, Tablas y Datos Simulados
Transformaci贸n de Datos con SQL: De Consultas Simples a Superqueries
Columnas Generadas en SQL
Expresiones Regulares y Slugs en SQL y MySQL: Casos de Uso Reales
Automatizaci贸n, Reutilizaci贸n y Eficiencia en Consultas
Vistas y Tablas Virtuales en SQL
Consultas Anidadas y Vistas Materializadas
Triggers y Vistas Materializadas en MySQL (Kinda)
Automatizaci贸n de Bases de Datos con Triggers en MySQL
Llaves Primarias e 脥ndices
Trabajo con Datos Avanzados (JSON)
Uso de JSON en MySQL: Almacenamiento Eficiente de Datos Estructurados
B煤squedas Avanzadas con JSON en MySQL: Indexaci贸n y Optimizaci贸n
Joins en SQL: Conecta Tablas y Mejora tus Consultas
Motores de Almacenamiento y Encodings
Gesti贸n de Usuarios y Permisos en SQL
Gesti贸n Avanzada y An谩lisis de Bases de Datos
Information Schema en MySQL: An谩lisis Interno de tu Base de Datos
Mysqldump
Domina las Migrations: Evoluciona y Gestiona tu Base de Datos con Eficiencia
Optimiza tus Decisiones con Metabase
You don't have access to this class
Keep learning! Join and start boosting your career
The JSON format has become an indispensable standard for data handling in modern applications. Although it originally emerged from the JavaScript ecosystem, today its presence is ubiquitous in software development. In the context of databases, JSON offers extraordinary flexibility, but it requires in-depth knowledge to take advantage of its benefits without falling into inefficient practices. Let's see how MySQL has incorporated this data type and how we can use it correctly.
JSON (JavaScript Object Notation) is a data interchange format that allows storing structured information in a flexible way. In the world of databases, there are two main families:
Although these approaches were traditionally considered mutually exclusive, MySQL revolutionized its approach in 2017 by incorporating the JSON data type, allowing to combine the best of both worlds. This decision, while controversial to some purists, offers unprecedented flexibility when used correctly.
The key is understanding when to use JSON columns. They are ideal for data that:
For example, in an online store, we might use JSON to store product-specific characteristics such as brand, size, or color, which vary by product type.
To add a JSON column to an existing table, we use the standard ALTER TABLE syntax:
ALTER TABLE productsADD COLUMN data JSON AFTER price;
This statement creates a column named "data" of type JSON after the "price" column.
MySQL allows to store several types of data inside a JSON object:
{"brand": "Apple", "size": "L"}
[1, 2, 3].
"Hello, how are you?"
42
or 3.14
true
or false
null
It is important to note that for data requiring decimal precision (such as monetary values), it is not recommended to use JSON. In these cases, it is better to use specific types such as DECIMAL or DOUBLE.
We can insert JSON data directly as text, and MySQL will take care of validating and storing it in an optimized way:
UPDATE productsSET data = '{"brand": "Pear", "HDSize": "40 gigabytes"}'WHERE product_id = 10;
MySQL provides three main functions for manipulating JSON data:
UPDATE productsSET data = JSON_REPLACE(data, '$.brand', 'Banana')WHERE product_id = 10;
UPDATE productsSET data = JSON_REMOVE(data, '$.HDSize')WHERE product_id = 10;
UPDATE productsSET data = JSON_SET(data, '$.color', 'black')WHERE product_id = 10;
It is crucial to understand that these functions do not directly modify the JSON column, but return a new modified JSON object. That is why we must assign the result to the column with an UPDATE.
To access elements within a JSON, MySQL uses a path notation:
$
: Represents the root of the document$.property
: Accesses a property of the root object$[n]
: Accesses element n of an array$[*]
: Represents all the elements of an arrayFor example, $.brand
accesses the value of the "brand" property in the root object.
When working with JSON columns in MySQL, it is important to consider:
Selective use: not everything should go in JSON. Data that needs to be indexed or queried frequently should be in regular columns.
Consistent structure: Although JSON allows flexibility, maintaining some consistency in structure makes queries easier.
Performance: Operations on JSON can be more expensive than on traditional columns, especially in searches.
Validation: MySQL automatically validates that the JSON is correct before storing it.
Numeric precision: For values that require exact decimal precision (such as financial data), use specific columns instead of JSON.
The JSON data type in MySQL represents a bridge between the relational and documentary worlds, offering flexibility without sacrificing the advantages of relational databases. By properly using the JSON_REPLACE, JSON_REMOVE and JSON_SET functions, we can efficiently manipulate this complex data. In upcoming lessons, we will explore how to efficiently search within JSON columns and how to implement indexes to optimize performance, overcoming one of the traditional limitations of this data type.
Have you used JSON columns in your projects and what advantages or challenges have you encountered? Share your experience in the comments.
Contributions 0
Questions 0
Want to see more contributions, questions and answers from the community?