tmp
You are viewing an old version of this article. View
the current version here.
Contents
Dynamic columns feature allows to store different sets of columns for each row in the table.
It is targeted at a case when one needs to store entities that may have many different attributes (like size, color, weight, etc). If the set of possible attributes is very large, or not known, one can't create a regular column for each attribute. In this case, dynamic columns can be used.
Introduction
Dynamic columns works by storing the columns in a blob and having a small set of functions to manipulate it.
The table can be created like so:
create table assets ( item_name varchar(40), -- Name is a common attribute for all items dynamic_cols blob, -- A blob column that will be used as a storage -- for dynamic columns. primary key(item_name) -- not required, but it's a good practice to -- have a primary key );
Then, one can insert data like so:
insert into assets values ('MariaDB T-shirt', COLUMN_CREATE('color', 'blue', 'size', 'XL')), ('Thinkpad Laptop', COLUMN_CREATE('color', 'black', 'price', '500'));
note: the example uses MariaDB 10.0.1. In MariaDB 5.3, only numbers could be used. See [#mariadb-5.3] for details
And query it like so:
select item_name, COLUMN_GET(dynamic_cols, 'color' as char) as color from assets; +-----------------+-------+ | item_name | color | +-----------------+-------+ | MariaDB T-shirt | blue | | Thinkpad Laptop | black | +-----------------+-------+
Comments
Comments loading...
Content reproduced on this site is the property of its respective owners,
and this content is not reviewed in advance by MariaDB. The views, information and opinions
expressed by this content do not necessarily represent those of MariaDB or any other party.