All pages
Powered by GitBook
1 of 9

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

COLUMN_CHECK

Verify dynamic column integrity. This function checks if a blob containing dynamic columns is valid and returns 1 if it is, 0 otherwise.

Syntax

COLUMN_CHECK(dyncol_blob);

Description

Check if dyncol_blob is a valid packed dynamic columns blob. Return value of 1 means the blob is valid, return value of 0 means it is not.

Rationale: Normally, one works with valid dynamic column blobs. Functions like , , always return valid dynamic column blobs. However, if a dynamic column blob is accidentally truncated, or transcoded from one character set to another, it will be corrupted. This function can be used to check if a value in a blob field is a valid dynamic column blob.

This page is licensed: CC BY-SA / Gnu FDL

Dynamic Columns Functions

Manage schema-less data within relational tables. These functions, such as COLUMN_CREATE and COLUMN_GET, allow you to store and retrieve variable sets of columns in a single BLOB field.

COLUMN_CREATE
COLUMN_ADD
COLUMN_DELETE

COLUMN_ADD

Update dynamic columns. This function adds or updates values within a dynamic column blob, returning the new blob content.

Syntax

COLUMN_ADD(dyncol_blob, column_nr, value [as type], [column_nr, value [as type]]...)
COLUMN_ADD(dyncol_blob, column_name, value [as type], [column_name, value [as type]]...)

Description

Adds or updates dynamic columns.

  • dyncol_blob must be either a valid dynamic columns blob (for example, COLUMN_CREATE returns such blob), or an empty string.

  • column_name specifies the name of the column to be added. If dyncol_blob already has a column with this name, it will be overwritten.

  • value specifies the new value for the column. Passing a NULL value will cause the column to be deleted.

  • as type is optional. See section for a discussion about types.

The return value is a dynamic column blob after the modifications.

Examples

Note: COLUMN_ADD() is a regular function (just like ), hence, in order to update the value in the table you have to use the UPDATE ... SET dynamic_col=COLUMN_ADD(dynamic_col, ....) pattern.

This page is licensed: CC BY-SA / Gnu FDL

COLUMN_DELETE

Remove dynamic columns. This function deletes specified columns from a dynamic column blob and returns the updated blob.

Syntax

Description

Deletes a with the specified name. Multiple names can be given. The return value is a dynamic column blob after the modification.

This page is licensed: CC BY-SA / Gnu FDL
COLUMN_DELETE(dyncol_blob, column_nr, column_nr...)
COLUMN_DELETE(dyncol_blob, column_name, column_name...)
dynamic column
#datatypes
CONCAT()
UPDATE t1 SET dyncol_blob=COLUMN_ADD(dyncol_blob, "column_name", "value") WHERE id=1;

COLUMN_CREATE

Create a dynamic column blob. This function generates a binary string containing specified column names and values for storage in a BLOB.

Syntax

COLUMN_CREATE(column_nr, value [as type], [column_nr, value [as type]]...)
COLUMN_CREATE(column_name, value [as type], [column_name, value [as type]]...)

Description

Returns a dynamic columns blob that stores the specified columns with values.

The return value is suitable for

  • storing in a table;

  • further modification with other dynamic columns functions.

The as type part allows one to specify the value type. In most cases, this is redundant because MariaDB will be able to deduce the type of the value. Explicit type specification may be needed when the type of the value is not apparent. For example, a literal '2012-12-01' has a CHAR type by default, one will need to specify '2012-12-01' AS DATE to have it stored as a date. See for further details.

Examples

This page is licensed: CC BY-SA / Gnu FDL

COLUMN_LIST

List dynamic column names. This function returns a comma-separated list of all column names contained within a dynamic column blob.

Syntax

COLUMN_LIST(dyncol_blob);

Description

Returns a comma-separated list of column names. The names are quoted with backticks.

See for more information.

This page is licensed: CC BY-SA / Gnu FDL

COLUMN_JSON

Convert dynamic columns to JSON. This function returns a JSON string representation of the data stored in a dynamic column blob.

Syntax

Description

Returns a JSON representation of data in dyncol_blob

. Can also be used to display nested columns. See
for more information.

Example

Limitation: COLUMN_JSON decodes nested dynamic columns at a nesting level of not more than 10 levels deep. Dynamic columns that are nested deeper than 10 levels are shown as BINARY string, without encoding.

This page is licensed: CC BY-SA / Gnu FDL

COLUMN_JSON(dyncol_blob)
dynamic columns
INSERT INTO tbl SET dyncol_blob=COLUMN_CREATE("column_name", "value");
Dynamic Columns:Datatypes
dynamic columns

COLUMN_GET

Retrieve a dynamic column value. This function extracts a specific column's value from a dynamic column blob, casting it to a specified type.

Syntax

COLUMN_GET(dyncol_blob, column_nr as type)
COLUMN_GET(dyncol_blob, column_name as type)

Description

Gets the value of a dynamic column by its name. If no column with the given name exists, NULL will be returned.

column_name as type requires that one specify the datatype of the dynamic column they are reading.

This may seem counter-intuitive: why would one need to specify which datatype they're retrieving? Can't the dynamic columns system figure the datatype from the data being stored?

The answer is: SQL is a statically-typed language. The SQL interpreter needs to know the datatypes of all expressions before the query is run (for example, when one is using prepared statements and runs "select COLUMN_GET(...)", the prepared statement API requires the server to inform the client about the datatype of the column being read before the query is executed and the server can see what datatype the column actually has).

Lengths

Suppose running a query like this:

Without specifying a maximum length (i.e. using as CHAR, not as CHAR(n)), MariaDB will report the maximum length of the result set column to be 16,777,216. This may cause excessive memory usage in some client libraries, because they try to pre-allocate a buffer of maximum result set width. To avoid this problem, use CHAR(n) whenever you're using COLUMN_GET in the select list.

See for more information about datatypes.

This page is licensed: CC BY-SA / Gnu FDL

SELECT item_name, COLUMN_JSON(dynamic_cols) FROM assets;
+-----------------+----------------------------------------+
| item_name       | COLUMN_JSON(dynamic_cols)              |
+-----------------+----------------------------------------+
| MariaDB T-shirt | {"size":"XL","color":"blue"}           |
| Thinkpad Laptop | {"color":"black","warranty":"3 years"} |
+-----------------+----------------------------------------+
SELECT COLUMN_GET(BLOB, 'colname' AS CHAR) ...
Dynamic Columns:Datatypes

COLUMN_EXISTS

Check for a dynamic column. This function returns 1 if a specified column exists within a dynamic column blob, and 0 otherwise.

Syntax

COLUMN_EXISTS(dyncol_blob, column_nr)
COLUMN_EXISTS(dyncol_blob, column_name)

Description

Checks if a column with name column_name exists in dyncol_blob. If yes, return 1, otherwise return 0. See for more information.

This page is licensed: CC BY-SA / Gnu FDL

dynamic columns