Mariadb Cookbook: Chapter No. 10 "Exploring Dynamic and Virtual Columns in Mariadb"
Mariadb Cookbook: Chapter No. 10 "Exploring Dynamic and Virtual Columns in Mariadb"
Mariadb Cookbook: Chapter No. 10 "Exploring Dynamic and Virtual Columns in Mariadb"
Daniel Bartholomew
MariaDB Cookbook
MariaDB is a mature, stable, open source relational database. From its beginning in 2009 as a branch or fork of the MySQL database, to its status today as the default version of that database in most Linux distributions, and the database of choice for many companies large and small, MariaDB has proven that communities of users and developers, working and collaborating together, can do more than a single company could ever do. MariaDB shares many features and capabilities of its parent database, but like most children it has also surpassed its parent in many ways. The recipes in this book tread some common ground, but they are mostly about the features that are unique to or were introduced first in MariaDB. The why of certain features is there, to a small degree, but the main emphasis in each recipe is on the what and the how. The information you need to know to actually do something always trumps the theory behind it. As part of the growing library of MariaDB-specific books from Packt Publishing and other publishers, the goal of this book is to give you a practical, hands-on experience with this powerful, feature-rich database.
Chapter 8, Performance and Usage Statistics, covers using MariaDB's extended statistics, the audit plugin, and the performance schema. Chapter 9, Searching Data Using Sphinx, covers how to install and use this useful fulltext database indexer and search engine. Chapter 10, Exploring Dynamic and Virtual Columns in MariaDB, is all about the built-in NoSQL features of MariaDB including dynamic and virtual columns features in MariaDB. Chapter 11, NoSQL with HandlerSocket, is a chapter devoted to the NoSQL HandlerSocket feature and how to use it with various languages. Chapter 12, NoSQL with the Cassandra Storage Engine, contains several recipes covering the installation and usage of the Cassandra storage engine. Chapter 13, MariaDB Security, contains several recipes relating to securing MariaDB.
10
Creating tables with dynamic columns Inserting, updating, and deleting dynamic column data Reading data from a dynamic column Using virtual columns
Introduction
One recent trend in the database world has been the development and use of NoSQL databases. This trend arose from a realization that relational database servers that use SQL, such as MariaDB, are not always the right tool for the job. Sometimes nonrelational, specialized, scalable, and clustered key-value databases work better for specic tasks. Another trend is the addition of virtual columns to databases. These columns don't change how the data is accessed as dynamic columns do. What they do is change how the data in them is stored. In short, the data is derived from the values of other columns in the row, similar to a spreadsheet. The MariaDB developers see the value in such nontraditional database features, and have implemented these and others in MariaDB to make it as exible and as capable a database server as possible.
Exploring Dynamic and Virtual Columns in MariaDB Both the chapters following this one delve into a couple of additional NoSQL features of MariaDB, HandlerSocket and the Cassandra storage engine, respectively.
This chapter includes several syntax diagrams and data type denitions. The parts of these diagrams and denitions in square brackets [] are optional. Also, a series of three dots ... (also called an ellipsis) means that the previous part in the bracket can be repeated.
How to do it...
1. Launch the mysql command-line client and connect to our MariaDB server. 2. Create a test database and use it with the following command:
CREATE DATABASE IF NOT EXISTS test; USE test;
3. Create a table with a standard PRIMARY KEY column and a BLOB column using the following commands:
CREATE TABLE dyn_example ( id SERIAL PRIMARY KEY, dyn_cols BLOB );
How it works...
The dynamic columns feature in MariaDB is a set of special functions that allow us to dene and redene the number of columns and their data types as needed on a row-by-row basis without altering our table conguration. These special columns exist and are dened as a standard BLOB column in our CREATE TABLE command. But unlike a regular BLOB column, we will only interact with this column using several special dynamic columns helper functions. We will cover these helper functions in the Inserting, updating, and deleting dynamic column data and Reading data from a dynamic column recipes in this chapter.
170
Chapter 10 The two things that a table with dynamic columns needs are an id column (or something similar) for PRIMARY KEY and a column with the type BLOB. Other columns can also be a part of the denition, but these are the ones that need to be there.
There's more...
When using dynamic columns, there are a couple of limitations to know about. The rst is that the maximum number of dynamic columns we can dene inside a single dynamic column BLOB is 65,535. Next, the total length of a packed dynamic BLOB column is whatever the max_allowed_packet size variable is set to, up to one gigabyte. Normally, the server handles all the interactions with dynamic columns and the client only calls the various dynamic columns functions. It is possible, however, for clients to directly manipulate and interact with dynamic columns using an API. The API is part of the libmysql client library.
See also
The full documentation of dynamic columns can be found at https://mariadb. com/kb/en/dynamic-columns/ and https://mariadb.com/kb/en/
dynamic-columns-in-mariadb-10/
Refer to the Inserting, updating, and deleting dynamic column data and Reading data from a dynamic column recipes in this chapter
Getting ready
First, you need to complete the Creating tables with dynamic columns recipe.
171
How to do it...
1. Launch the mysql command-line client and connect to the test database in our MariaDB server. 2. Insert some values into the dyn_example table we created earlier:
INSERT INTO dyn_example (dyn_cols) VALUES (COLUMN_CREATE('name','t-shirt', 'color','blue' AS CHAR, 'size','XL' AS CHAR)), (COLUMN_CREATE('name','t-shirt', 'color','blue' AS CHAR, 'size','L' AS CHAR)), (COLUMN_CREATE('name','t-shirt', 'color','black' AS CHAR, 'size','M' AS CHAR)), (COLUMN_CREATE('name','flashlight', 'color','black' AS CHAR, 'size','AAA' AS CHAR, 'num', 2 AS INT)), (COLUMN_CREATE('name','shovel', 'length','5'));
How it works...
The standard SQL INSERT, UPDATE, and DELETE statements do not work as expected on a dynamic column. These statements see the dynamic column as a regular BLOB column, and if we try to insert or update it directly, we will likely end up corrupting the row. To properly interact with this column, we need to use special dynamic columns functions. The functions for inserting, updating, and deleting data are COLUMN_CREATE, COLUMN_ADD, and COLUMN_DELETE.
172
Chapter 10 Each dynamic column in a row can have a different number of columns, and each of these dynamically dened columns can have the following different data types:
Type BINARY[(N)] CHAR[(N)] DATE DATETIME[(D)] DECIMAL[(M[,D])] INTEGER SIGNED [INTEGER] TIME[(D)] UNSIGNED [INTEGER] Description A variable-length binary string A variable-length string A 3-byte date A 9-byte date and time. Microseconds are supported A variable-length binary decimal A variable-length signed integer, up to 64 bits in length A variable-length signed integer, up to 64 bits in length A 6-byte time. Microseconds are supported and it may be negative A variable-length unsigned integer, up to 64 bits in length
Dening the data type is optional when creating a new dynamic column or updating an existing dynamic column, but it is mandatory to specify a data type when reading data from a dynamic column. The COLUMN_CREATE function is used as part of an INSERT statement to both add a new row and to dene the dynamic columns in that row. Unlike the COLUMN_ADD and COLUMN_DELETE functions, where we specify the dynamic columns BLOB column name inside the function, in the COLUMN_CREATE function, this is taken care of by the INSERT statement this function is a part of. The syntax of this function is as follows:
COLUMN_CREATE(column_name, value [AS type][, column_name, value [AS type]]...);
The COLUMN_ADD function is used as part of an UPDATE statement to either update an existing dynamic column in one or more existing rows or to add a new column to one or more existing rows. The syntax of this function is as follows:
COLUMN_ADD(dyncol_blob_name, column_name, value [AS type][, column_name, value [AS type]]...);
The COLUMN_DELETE function is used as part of an UPDATE statement to delete the specied dynamic column or columns. The syntax of this function is as follows:
COLUMN_DELETE(dyncol_blob_name, column_name[, column_name]...);
173
There's more...
The rst version of the dynamic columns feature, introduced in MariaDB 5.3, only allowed for numbered columns. MariaDB 10.0 was the rst version of MariaDB to support named dynamic columns. So, in MariaDB 5.3 and MariaDB 5.5, in the place where we now specify the column name, we will put a number instead. If we are working with the code that was developed originally for this rst version of dynamic columns, we will see numbers instead of column names. MariaDB 10.0 and later supports the old style of dynamic columns only so long as our code consistently refers to the columns by number. Once we start using names, our dynamic columns will be automatically upgraded to the new format for dynamic columns and we will be unable to continue using numbers to refer to our dynamic columns.
This INSERT statement creates a dynamic column with two nested dynamic columns inside it, one of which has its own nested dynamic column. The names of each column in a dynamic column have to be unique, but we can duplicate names as long as they are in their own uniquely-named nested dynamic column. The Reading nested dynamic columns section of the Reading data from a dynamic column recipe in this chapter has instructions on how to query and read nested dynamic column's data.
See also
The full documentation of dynamic columns can be found at https://mariadb. com/kb/en/dynamic-columns/ and https://mariadb.com/kb/en/
dynamic-columns-in-mariadb-10/
Refer to the Creating tables with dynamic columns and Reading data from a dynamic column recipes in this chapter
174
Chapter 10
Getting ready
Complete the Creating tables with dynamic columns recipe and the Inserting, updating, and deleting dynamic column data recipe in this chapter.
How to do it...
1. Launch the mysql command-line client. Connect to our MariaDB server and the test database. 2. Discover the columns in our data:
SELECT id, COLUMN_LIST(dyn_cols) FROM dyn_example;
175
Exploring Dynamic and Virtual Columns in MariaDB 3. Read data from our table using the following commands:
SELECT id, COLUMN_GET(dyn_cols, 'name' AS CHAR) AS 'name', COLUMN_GET(dyn_cols, 'color' AS CHAR) AS 'color', COLUMN_GET(dyn_cols, 'size' AS CHAR) AS 'size', COLUMN_GET(dyn_cols, 'num' AS INT) AS 'num' FROM dyn_example;
The following screenshot displays data selected using the preceding command:
4. Select everything from our table and output each dynamic columns BLOB as a JSON object using the following command:
SELECT id, COLUMN_JSON(dyn_cols) FROM dyn_example;
176
Chapter 10 5. Check each dynamic columns BLOB to see if the num column exists in it:
SELECT id, COLUMN_EXISTS(dyn_cols, 'num') FROM dyn_example;
6. Check that each dynamic columns BLOB columns in each row is valid using the following command:
SELECT id, COLUMN_CHECK(dyn_cols) FROM dyn_example;
177
How it works...
To read dynamic columns, we must use either the COLUMN_GET or COLUMN_JSON helper functions. If we try to use a standard SELECT statement without using these functions, we will get data that appears to be garbled. In fact, it is a binary representation of our data that the dynamic column's API understands and can read but that SELECT, by itself, cannot. This is similar to how a music program understands how to read a binary MP3 le, but not a le containing a 3D model of an airplane. The COLUMN_GET function requires us to specify the name of our dynamic columns BLOB column along with the name of the dynamic column inside the blob we want to read and the data type of that dynamic column. This is in contrast to the COLUMN_ADD and COLUMN_ CREATE functions, where dening the data type is optional. Also, we must call this function for each individual column we want to retrieve, as it does not allow us to specify multiple columns at once. The syntax of the COLUMN_GET function is as follows:
COLUMN_GET(dyncol_blob_name, column_name AS type);
To discover what columns exist in a given dynamic columns BLOB of a row or of several rows, we use the COLUMN_LIST function. If we omit the WHERE clause, we will get a list of the columns in every dynamic columns BLOB for every row in our table. The syntax of this function is as follows:
COLUMN_LIST(dyncol_blob_name);
The COLUMN_EXISTS function allows us to check if a given column exists in a given dynamic columns BLOB of a given row or rows (or all rows if we omit a WHERE clause). The function returns 1 if the column exists, and 0 if it doesn't. The syntax of this function is as follows:
COLUMN_EXISTS(dyncol_blob_name, column_name);
The COLUMN_JSON function allows us to easily grab all of the columns in a dynamic columns BLOB and output it as a standard JSON object sentence. Because this function outputs all of the dynamic columns, we do not need to specify or know what columns are in the row or rows we are selecting. The syntax of this function is as follows:
COLUMN_JSON(dyncol_blob_name);
The COLUMN_CHECK function allows us to verify that a given dynamic columns BLOB is valid and not corrupted. The syntax of this function is as follows:
COLUMN_CHECK(dyncol_blob_name);
178
Chapter 10
There's more...
The rst version of dynamic columns included in MariaDB 5.3 and MariaDB 5.5 did not allow for column names. Instead, columns were referred to with numbers. These old-style dynamic columns are still supported in MariaDB 10.0 and above, but the output is slightly different. For example, the COLUMN_LIST function, if it is used to query one of these old-style dynamic column's blobs, will return a comma-separated list of column numbers instead of a comma-separated list of column names.
Instead, we must use the COLUMN_JSON function to properly select the nested dynamic columns data, using the following command:
SELECT COLUMN_JSON(dyn_cols) FROM dyn_example WHERE COLUMN_GET(dyn_cols, 'type' AS CHAR) = 'parent';
179
Exploring Dynamic and Virtual Columns in MariaDB The preceding command displays the output shown in the following screenshot:
See also
The full documentation of dynamic columns can be found at https://mariadb. com/kb/en/dynamic-columns/ and https://mariadb.com/kb/en/
dynamic-columns-in-mariadb-10/
Refer to the Creating tables with dynamic columns and Inserting, updating, and deleting dynamic column data recipes in this chapter
How to do it...
1. Launch the mysql command-line client and connect to our MariaDB database. 2. Create a test database and switch to that database using the following command:
CREATE DATABASE IF NOT EXISTS test; USE test;
180
Chapter 10 4. Examine the structure of the table using the following command:
DESCRIBE virt_cols;
The DESCRIBE command displays the structure of the table as shown in the following screenshot:
5. Show a CREATE TABLE command that will recreate the exact table (including the virtual columns):
SHOW CREATE TABLE virt_cols\G
181
Exploring Dynamic and Virtual Columns in MariaDB 6. Insert some data as follows:
INSERT INTO virt_cols (surname,givenname) VALUES ('Packer','Boyd'),('Uchtdorf','Dieter'), ('Ballard','Russell'),('Holland','Jeffrey'), ('Cook','Quentin'),('Bednar','David'); INSERT INTO virt_cols (surname,givenname,uid,username) VALUES ('Christofferson','Todd', DEFAULT, DEFAULT), ('Andersen','Neil', DEFAULT, DEFAULT);
7.
Select the data from our virt_cols table using the following command:
SELECT * FROM virt_cols;
How it works...
The virtual columns feature of MariaDB allows us to create special columns in our table that have calculated values based on the output of a function (or a combination of functions). These values can be either PERSISTENT, meaning the value is stored in the database and only updated when the row is updated, or VIRTUAL, meaning the value is calculated each time the row is read.
182
Chapter 10 When using a standard DESCRIBE statement to examine the structure of a table with virtual columns, the EXTRA column in the output will tell us whether a column is virtual or not with the presence of the text VIRTUAL or PERSISTENT to identify each type of virtual column. What the DESCRIBE statement will not do is show us the function or combination of functions and operators which determine the value of the virtual column. For that, we need to use the SHOW CREATE TABLE command. When inserting or updating data in a table with virtual columns, we can either choose to not specify the columns in our SQL statement or to go ahead and specify them but use the DEFAULT key word instead of providing a value. In our recipe, we perform both actions. Selecting data from a table with virtual columns is just like selecting from a table without virtual columns. The only difference will be if the function calculating the value of a VIRTUAL column takes a noticeable amount of time to run. A PERSISTENT virtual column, because its calculated value is stored in the database, will return results as fast as regular columns in the table, but a VIRTUAL column's value is calculated every time the table is queried.
There's more...
Virtual columns have some limitations. For starters, they can only be used with InnoDB, XtraDB, Aria, and MyISAM tables. Also, indexes are only partially supported for virtual columns. This is because virtual columns do not support primary keys. It is possible to have an index on a PERSISTENT virtual column, but even then statements such as UPDATE CASCADE, ON UPDATE SET NULL, and ON DELETE SET NULL are not allowed. That said, things such as triggers and stored procedures are fully supported by virtual columns.
See also
183
Alternatively, you can buy the book from Amazon, BN.com, Computer Manuals and most internet book retailers.
www.PacktPub.com