The data model has determined that before creating a table, you also need to know what data types MatrixDB contains. Because a table is composed of columns, each column needs to have a certain type.
The data types supported by MatrixDB include the following categories:
Type | Alias | *Size** | Scope | Description |
---|---|---|---|---|
smallint | int2 | 2 bytes | -32768 ~ 32767 | Small range integer |
int | int4 | 4 bytes | -2147483648 ~ 2147483647 | Integer Type |
bigint | int8 | 8 bytes | -9223372036854775808 ~ 9223372036854775807 | Large range integers |
serial | serial4 | 4 bytes | 1 ~ 2147483647 | Auto-incremental integer |
bigserial | serial8 | 8 bytes | 1 ~ 9223372036854775807 | Large range of self-increasing integers |
real | float4 | 4 bytes | 6-bit precision | variable precision |
double precision | float8 | 8 bytes | 15-bit precision | variable precision |
decimal | numeric | variable | unlimited | fixed precision |
As can be seen from the table, the numerical types of MatrixDB are divided into 4 categories:
Type | Alias | *Size** | Scope | Description |
---|---|---|---|---|
character [ (n) ] | char [ (n) ] | 1 byte + n | Longest n characters string | Fixed length string, unspecified characters are filled with spaces |
character varying [ (n) ] | varchar [ (n) ] | 1 byte + string size | string with a maximum of n characters | variable length string with a length limit, upper limit of 10485760 bytes |
text | 1 byte + string length | any length string | variable length string without length limit |
As can be seen from the table, MatrixDB string types are divided into 2 categories:
Type | Alias | *Size** | Scope | Description |
---|---|---|---|---|
date | 4 bytes | 4713 BC ~ 294,277 AD | Calendar date (year, month, day) | |
time [ (p) ] [ without time zone ] | 8 bytes | 00:00:00[.0000000] ~ 24:00:00[.0000000] | Time in one day | |
time [ (p) ] with time zone | timetz | 12 bytes | 00:00:00+1359 ~ 24:00:00-1359 | Time within one day, with time zone |
timestamp [ (p) ] [ without time zone ] | 8 bytes | 4713 BC ~ 294,277 AD | Date and time | |
timestamp [ (p) ] with time zone | timestamptz | 8 bytes | 4713 BC ~ 294,277 AD | Date and time, with time zone |
As can be seen from the table, MatrixDB has three types of date and time:
You can see a special information from the time type time zone real-time zone. If not specified, the default is without time zone, that is, there is no time zone. So what impact does the time zone have on the time type? Let’s create a test table to demonstrate the description.
CREATE TABLE test (
c1 int,
c2 timestamp,
c3 timestamp with time zone
)DISTRIBUTED BY (c1);
In addition to the distribution key, the test table contains two timestamp columns, one without time zone by default and the other with time zone. Then insert the test data and query:
INSERT INTO test VALUES(1, now(), now());
SELECT * FROM test;
c1 | c2 | c3
----+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1 | 2021-12-01 14:54:09.4783 | 2021-12-01 14:54:09.4783+08
(1 row)
SHOW timezone;
TimeZone
------------------------
Asia/Shanghai
(1 row)
From the query results, we can see that the time displayed by the two timestamp columns is the same. There is a +08 behind the column with time zone, indicating that the time zone to which the timestamp belongs is East Eighth District. Then, through the SHOW timezone command, I saw that the current time zone is 'Asia/Shanghai', that is, Beijing time.
Let's modify the time zone and view the results:
SET timezone TO 'Japan';
SELECT * FROM test;
c1 | c2 | c3
----+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1 | 2021-12-01 14:54:09.4783 | 2021-12-01 15:54:09.4783+09
(1 row)
It can be seen that after changing the database time zone to Japan, the time displayed in the c3 column with time zone is different from that in c2, and the subsequent time zone information becomes +09, that is, Dongjiu District.
Therefore, the time stored in the timestamp column with time zone has time zone information and will display different times in different time zones; while the timetamp column without time zones will display the same time in any time zone. When the device is deployed in a region of different time zones, the timestamp must have time zone information. For timestamp type, there is no time zone occupies storage space, so it is recommended to add it.
Timing scene data can be divided into the following categories:
In the relational model, the basic data type, each column represents the value of an attribute, such as: name, height, weight. But face the following challenges:
At this time, you need to use the extended data type. The so-called extended data type means that multiple attributes can be stored in a single column.
There are many ways to implement extended data types, such as customizing the serialized storage format and encoding method, and then storing it into string types. MatrixDB provides two extended data types:
The json/jsonb type inherits from the PostgreSQL database. Unlike directly saving json strings with columns of string type, the json/jsonb type provides commonly used json operation functions for reading key values, as well as merging, deleting and other operations.
The usage methods of json and jsonb are basically the same, and they can also be directly converted to each other. The difference is:
The following are the commonly used json/jsonb type operation methods:
SELECT '{"a":1, "b":2, "c":3}'::json->'a';
?column?
----------
1
(1 row)
If the json/jsonb level is nested, use -> multiple times:
SELECT '{"a":1, "b":2, "c":{"d":2}}'::json->'c'->'d';
?column?
----------
2
(1 row)
Use the - number to delete the key. When writing to a json object, it must be converted to a jsonb type:
SELECT '{"a":1, "b":2, "c":3}'::jsonb - 'a';
?column?
------------------
{"b": 2, "c": 3}
(1 row)
The merge operation can merge two jsonb-type keys together, using the || operator:
SELECT '{"a":1, "b":2}'::jsonb || '{"c":3}'::jsonb;
?column?
------------------------------
{"a": 1, "b": 2, "c": 3}
(1 row)
|| Operator will update the value when the key value is the same:
SELECT '{"a":1, "b":2}'::jsonb || '{"b":3}'::jsonb;
?column?
------------------
{"a": 1, "b": 3}
(1 row)
For more json operation methods, please refer to PostgreSQL [document] (https://www.postgresql.org/docs/12/functions-json.html).
mxkv is a highly efficient and scalable storage type developed by MatrixDB. The usage method is similar to json. The advantage compared to json is: mxkv is binary storage and is compressed, with smaller storage space and higher query efficiency.
For detailed usage methods, please refer to [Document] (/doc/latest/datamodel/mxkv) and [blog] (/blog/20210721-MatrixDB-mxkv).
MatrixDB 4.3 has developed an enhanced version of PostGIS components that support storage and computing of spatial data types. For specific usage methods, please refer to [Document] (/doc/latest/datamodel/spatial_type).