[TDengine] tdengine insert data, view table header, insert empty data

Table of contents

1. Insert data

2. Check the header

 3. Insert empty data


1. Insert data

Inserting data in TDengine can use the INSERT statement, and its basic syntax is as follows:

INSERT INTO table_name [(col_name1 [, col_name2, ...])] VALUES (val1[, val2, ...]);

Where table_nameis the name of the table you want to insert; col_name1, col_name2,...is the name of the field, which can be omitted; val1, val2,...is the value to be inserted, and needs to be one-to-one in accordance with the order of the fields.

Here is an example:

INSERT INTO my_table (ts, col1, col2) VALUES ('2023-05-17 09:30:00', 123.45, 'hello world');

In this example, the my_table table has three columns: ts, col1, and col2. The ts column is a column of timestamp type, and col1 and col2 are columns of floating point number and string type, respectively. The inserted data contains timestamp, float and string type values.

In addition to specifying values, you can also query data from other tables and insert the results into the current table through the SELECT statement. For example:

INSERT INTO my_table (ts, col1, col2) SELECT now(), col1, col2 FROM another_table;

In this example, the col1 and col2 columns in the another_table table are queried through the SELECT statement, and the results are inserted into the my_table table, and the current time is inserted as the timestamp.

It should be noted that when inserting data, TDengine will store data by column. It is recommended to try to insert the data of the same column at one time to reduce the number of disk writes. In addition, if you need to insert data frequently, you can consider using the Bulk Load tool provided by TDengine to improve the efficiency of data insertion.

2. Check the header

You can use the SHOW CREATE TABLE statement to view the table header in TDengine. Its basic syntax is as follows:

SHOW CREATE TABLE table_name;

where table_nameis the name of the table you want to view.

Here is an example:

SHOW CREATE TABLE my_table;

This statement gives detailed information such as table name, column name, data type, label, etc.

It should be noted that all information in the TDengine table header is read-only and cannot be modified through SQL statements. If you need to change the table structure, it is recommended to recreate the table after backing up the data or use the ALTER TABLE statement to modify it.

 3. Insert empty data

In TDengine, empty data can be represented by inserting NULL or empty string '' into the table.

If you want to insert a NULL value, you can directly use NULL instead of the field value. For example:

INSERT INTO my_table (ts, col1, col2) VALUES ('2023-05-17 09:30:00', NULL, 'hello world');

In this example, a timestamp and a null string 'hello world' are inserted, and the value in the second column col1 is NULL.

If you want to insert an empty string, you need to set the field value to ''. For example:

INSERT INTO my_table (ts, col1, col2) VALUES ('2023-05-17 09:30:00', '', 'hello world');

In this example, a timestamp and an empty string are inserted, and the value in the second column col1 is ''.

It should be noted that whether the field can be empty is defined in the table header. If a field is defined as NOT NULL, NULL or empty strings cannot be inserted. When inserting data, it needs to be inserted according to the definition of the table header, otherwise, the insertion will fail or the data will be stored incorrectly.

Guess you like

Origin blog.csdn.net/fanjufei123456/article/details/130719705