diff --git a/src/.vuepress/sidebar/V2.0.x/en-Table.ts b/src/.vuepress/sidebar/V2.0.x/en-Table.ts index a7a75a2c4..ed9e28dc1 100644 --- a/src/.vuepress/sidebar/V2.0.x/en-Table.ts +++ b/src/.vuepress/sidebar/V2.0.x/en-Table.ts @@ -87,7 +87,7 @@ export const enSidebar = { children: [ { text: 'Database Management', link: 'Database-Management' }, { text: 'Table Management', link: 'Table-Management' }, - { text: 'Write&Updata Data', link: 'Write-Updata-Data' }, + { text: 'Write&Updata Data', link: 'Write-Updata-Data_apache' }, { text: 'Query Data', link: 'Query-Data_apache' }, { text: 'Delete Data', @@ -213,7 +213,7 @@ export const enSidebar = { prefix: 'SQL-Manual/', children: [ { text: 'Metadata Operations', link: 'SQL-Metadata-Operations' }, - { text: 'Data Addition&Deletion', link: 'SQL-Data-Addition-Deletion' }, + { text: 'Data Addition&Deletion', link: 'SQL-Data-Addition-Deletion_apache' }, { text: 'Data Query', collapsible: true, diff --git a/src/.vuepress/sidebar/V2.0.x/zh-Table.ts b/src/.vuepress/sidebar/V2.0.x/zh-Table.ts index 6d555172e..a52bb6eb8 100644 --- a/src/.vuepress/sidebar/V2.0.x/zh-Table.ts +++ b/src/.vuepress/sidebar/V2.0.x/zh-Table.ts @@ -87,7 +87,7 @@ export const zhSidebar = { children: [ { text: '数据库管理', link: 'Database-Management' }, { text: '表管理', link: 'Table-Management' }, - { text: '写入&更新', link: 'Write-Updata-Data' }, + { text: '写入&更新', link: 'Write-Updata-Data_apache' }, { text: '数据查询', link: 'Query-Data_apache' }, { text: '数据删除', @@ -210,7 +210,7 @@ export const zhSidebar = { prefix: 'SQL-Manual/', children: [ { text: '元数据操作', link: 'SQL-Metadata-Operations' }, - { text: '数据增删', link: 'SQL-Data-Addition-Deletion' }, + { text: '数据增删', link: 'SQL-Data-Addition-Deletion_apache' }, { text: '数据查询', collapsible: true, diff --git a/src/.vuepress/sidebar_timecho/V2.0.x/en-Table.ts b/src/.vuepress/sidebar_timecho/V2.0.x/en-Table.ts index d651f24d8..05b435a73 100644 --- a/src/.vuepress/sidebar_timecho/V2.0.x/en-Table.ts +++ b/src/.vuepress/sidebar_timecho/V2.0.x/en-Table.ts @@ -104,7 +104,7 @@ export const enSidebar = { children: [ { text: 'Database Management', link: 'Database-Management' }, { text: 'Table Management', link: 'Table-Management' }, - { text: 'Write&Updata Data', link: 'Write-Updata-Data' }, + { text: 'Write&Updata Data', link: 'Write-Updata-Data_timecho' }, { text: 'Query Data', link: 'Query-Data_timecho' }, { text: 'Delete Data', @@ -231,7 +231,7 @@ export const enSidebar = { prefix: 'SQL-Manual/', children: [ { text: 'Metadata Operations', link: 'SQL-Metadata-Operations' }, - { text: 'Data Addition&Deletion', link: 'SQL-Data-Addition-Deletion' }, + { text: 'Data Addition&Deletion', link: 'SQL-Data-Addition-Deletion_timecho' }, { text: 'Data Query', collapsible: true, diff --git a/src/.vuepress/sidebar_timecho/V2.0.x/zh-Table.ts b/src/.vuepress/sidebar_timecho/V2.0.x/zh-Table.ts index 58806cccd..e136ce0fb 100644 --- a/src/.vuepress/sidebar_timecho/V2.0.x/zh-Table.ts +++ b/src/.vuepress/sidebar_timecho/V2.0.x/zh-Table.ts @@ -95,7 +95,7 @@ export const zhSidebar = { children: [ { text: '数据库管理', link: 'Database-Management' }, { text: '表管理', link: 'Table-Management' }, - { text: '写入&更新', link: 'Write-Updata-Data' }, + { text: '写入&更新', link: 'Write-Updata-Data_timecho' }, { text: '数据查询', link: 'Query-Data_timecho' }, { text: '数据删除', @@ -220,7 +220,7 @@ export const zhSidebar = { prefix: 'SQL-Manual/', children: [ { text: '元数据操作', link: 'SQL-Metadata-Operations' }, - { text: '数据增删', link: 'SQL-Data-Addition-Deletion' }, + { text: '数据增删', link: 'SQL-Data-Addition-Deletion_timecho' }, { text: '数据查询', collapsible: true, diff --git a/src/UserGuide/Master/Table/Basic-Concept/Write-Updata-Data.md b/src/UserGuide/Master/Table/Basic-Concept/Write-Updata-Data.md index a251e2f12..98f66ca6c 100644 --- a/src/UserGuide/Master/Table/Basic-Concept/Write-Updata-Data.md +++ b/src/UserGuide/Master/Table/Basic-Concept/Write-Updata-Data.md @@ -1,3 +1,6 @@ +--- +redirectTo: Write-Updata-Data_apache.html +--- - -# Write & Update Data - -## 1. Data Insertion - -### 1.1 Syntax - -In IoTDB, data insertion follows the general syntax: - -```SQL -INSERT INTO [(COLUMN_NAME[, COLUMN_NAME]*)]? VALUES (COLUMN_VALUE[, COLUMN_VALUE]*) -``` - -**Basic Constraints**: - -1. Tables cannot be automatically created using `INSERT` statements. -2. Columns not specified in the `INSERT` statement will automatically be filled with `null`. -3. If no timestamp is provided, the system will use the current time (`now()`). -4. If a column value does not exist for the identified device, the insertion will overwrite any existing `null` values with the new data. -5. If a column value already exists for the identified device, a new insertion will update the column with the new value. -6. Writing duplicate timestamps will update the values in the columns corresponding to the original timestamps. -7. When an INSERT statement does not specify column names (e.g., INSERT INTO table VALUES (...)), the values in VALUES must strictly follow the physical order of columns in the table (this order can be checked via the DESC table command). - -Since attributes generally do not change over time, it is recommended to update attribute values using the `UPDATE` statement described below,Please refer to the following [Data Update](#2-data-updates). - -
- -
- - -### 1.2 Automatically Create Tables via Session Insertion - -When performing data writing through Session, IoTDB supports schema-less writing: there is no need to manually create tables beforehand. The system automatically constructs the table structure based on the information in the write request, and then directly executes the data writing operation. - -**Example:** - -```Java -try (ITableSession session = - new TableSessionBuilder() - .nodeUrls(Collections.singletonList("127.0.0.1:6667")) - .username("root") - .password("root") - .build()) { - - session.executeNonQueryStatement("CREATE DATABASE db1"); - session.executeNonQueryStatement("use db1"); - - // Insert data without manually creating the table - List columnNameList = - Arrays.asList("region_id", "plant_id", "device_id", "model", "temperature", "humidity"); - List dataTypeList = - Arrays.asList( - TSDataType.STRING, - TSDataType.STRING, - TSDataType.STRING, - TSDataType.STRING, - TSDataType.FLOAT, - TSDataType.DOUBLE); - List columnTypeList = - new ArrayList<>( - Arrays.asList( - ColumnCategory.TAG, - ColumnCategory.TAG, - ColumnCategory.TAG, - ColumnCategory.ATTRIBUTE, - ColumnCategory.FIELD, - ColumnCategory.FIELD)); - Tablet tablet = new Tablet("table1", columnNameList, dataTypeList, columnTypeList, 100); - for (long timestamp = 0; timestamp < 100; timestamp++) { - int rowIndex = tablet.getRowSize(); - tablet.addTimestamp(rowIndex, timestamp); - tablet.addValue("region_id", rowIndex, "1"); - tablet.addValue("plant_id", rowIndex, "5"); - tablet.addValue("device_id", rowIndex, "3"); - tablet.addValue("model", rowIndex, "A"); - tablet.addValue("temperature", rowIndex, 37.6F); - tablet.addValue("humidity", rowIndex, 111.1); - if (tablet.getRowSize() == tablet.getMaxRowNumber()) { - session.insert(tablet); - tablet.reset(); - } - } - if (tablet.getRowSize() != 0) { - session.insert(tablet); - tablet.reset(); - } -} -``` - -After execution, you can verify the table creation using the following command: - -```SQL -IoTDB> desc table1 -+-----------+---------+-----------+ -| ColumnName| DataType| Category| -+-----------+---------+-----------+ -| time|TIMESTAMP| TIME| -| region_id| STRING| TAG| -| plant_id| STRING| TAG| -| device_id| STRING| TAG| -| model| STRING| ATTRIBUTE| -|temperature| FLOAT| FIELD| -| humidity| DOUBLE| FIELD| -+-----------+---------+-----------+ -``` - -### 1.3 Specified Column Insertion - -It is possible to insert data for specific columns. Columns not specified will remain `null`. - -**Example:** - -```SQL -insert into table1("region", "plant_id", "device_id", Time, "temperature", "displacement") values ('Hamburg', '3001', '3', 4, 90.0, 1200.0) - - -insert into table1("region", "plant_id", "device_id", Time, "temperature") values ('Hamburg, '3001', '3', 5, 90.0) -``` - -### 1.4 Null Value Insertion - -You can explicitly set `null` values for tag columns, attribute columns, and field columns. - -**Example**: - -Equivalent to the above partial column insertion. - -```SQL -# Equivalent to the example above -insert into table1("region", "plant_id", "device_id", "model", "maintenance_cycle", Time, "temperature", "displacement") values ('Hamburg', '3001', '3', null, null, 4, 90.0, 1200.0) - -insert into table1("region", "plant_id", "device_id", "model", "maintenance_cycle", Time, "temperature", "displacement") values ('Hamburg', '3001', '3', null, null, 5, 90.0, null) -``` - -If no tag columns are included, the system will automatically create a device with all tag column values set to `null`. - -> **Note:** This operation will not only automatically populate existing tag columns in the table with `null` values but will also populate any newly added tag columns with `null` values in the future. - -### 1.5 Multi-Row Insertion - -IoTDB supports inserting multiple rows of data in a single statement to improve efficiency. - -**Example**: - -```SQL -insert into table1 -values -(4, 'Frankfurt', '3001', '3', '1', '10', 90.0, 1200.0) -(5, 'Frankfurt', '3001', '3', '1', '10', 90.0, 1200.0) - - -insert into table1 -("region", "plant_id", "device_id", Time, "temperature", "displacement") -values -('Frankfurt', '3001', '3', 4, 90.0, 1200.0) -('Frankfurt', '3001', '3', 5, 90.0, 1200.0) -``` - -#### Notes - -- Referencing non-existent columns in SQL will result in an error code `COLUMN_NOT_EXIST(616)`. -- Data type mismatches between the insertion data and the column's data type will result in an error code `DATA_TYPE_MISMATCH(614)`. - - -## 2. Data Updates - -### 2.1 Syntax - -```SQL -UPDATE SET updateAssignment (',' updateAssignment)* (WHERE where=booleanExpression)? - -updateAssignment - : identifier EQ expression - ; -``` - -**Note:** - -- Updates are allowed only on `ATTRIBUTE` columns. -- `WHERE` conditions: - - Can only include `TAG` and `ATTRIBUTE` columns; `FIELD` and `TIME` columns are not allowed. - - Aggregation functions are not supported. -- The result of the `SET` assignment expression must be a `string` type and follow the same constraints as the `WHERE` clause. - -**Example**: - -```SQL -update table1 set b = a where substring(a, 1, 1) like '%' -``` diff --git a/src/UserGuide/Master/Table/Basic-Concept/Write-Updata-Data_apache.md b/src/UserGuide/Master/Table/Basic-Concept/Write-Updata-Data_apache.md new file mode 100644 index 000000000..af0f1cf90 --- /dev/null +++ b/src/UserGuide/Master/Table/Basic-Concept/Write-Updata-Data_apache.md @@ -0,0 +1,332 @@ + + +# Write & Update Data + +## 1. Data Insertion + +### 1.1 Syntax + +In IoTDB, data insertion follows the general syntax: + +```SQL +INSERT INTO [(COLUMN_NAME[, COLUMN_NAME]*)]? VALUES (COLUMN_VALUE[, COLUMN_VALUE]*) +``` + +**Basic Constraints**: + +1. Tables cannot be automatically created using `INSERT` statements. +2. Columns not specified in the `INSERT` statement will automatically be filled with `null`. +3. If no timestamp is provided, the system will use the current time (`now()`). +4. If a column value does not exist for the identified device, the insertion will overwrite any existing `null` values with the new data. +5. If a column value already exists for the identified device, a new insertion will update the column with the new value. +6. Writing duplicate timestamps will update the values in the columns corresponding to the original timestamps. +7. When an INSERT statement does not specify column names (e.g., INSERT INTO table VALUES (...)), the values in VALUES must strictly follow the physical order of columns in the table (this order can be checked via the DESC table command). + +Since attributes generally do not change over time, it is recommended to update attribute values using the `UPDATE` statement described below,Please refer to the following [Data Update](#2-data-updates). + +
+ +
+ + +### 1.2 Automatically Create Tables via Session Insertion + +When performing data writing through Session, IoTDB supports schema-less writing: there is no need to manually create tables beforehand. The system automatically constructs the table structure based on the information in the write request, and then directly executes the data writing operation. + +**Example:** + +```Java +try (ITableSession session = + new TableSessionBuilder() + .nodeUrls(Collections.singletonList("127.0.0.1:6667")) + .username("root") + .password("root") + .build()) { + + session.executeNonQueryStatement("CREATE DATABASE db1"); + session.executeNonQueryStatement("use db1"); + + // Insert data without manually creating the table + List columnNameList = + Arrays.asList("region_id", "plant_id", "device_id", "model", "temperature", "humidity"); + List dataTypeList = + Arrays.asList( + TSDataType.STRING, + TSDataType.STRING, + TSDataType.STRING, + TSDataType.STRING, + TSDataType.FLOAT, + TSDataType.DOUBLE); + List columnTypeList = + new ArrayList<>( + Arrays.asList( + ColumnCategory.TAG, + ColumnCategory.TAG, + ColumnCategory.TAG, + ColumnCategory.ATTRIBUTE, + ColumnCategory.FIELD, + ColumnCategory.FIELD)); + Tablet tablet = new Tablet("table1", columnNameList, dataTypeList, columnTypeList, 100); + for (long timestamp = 0; timestamp < 100; timestamp++) { + int rowIndex = tablet.getRowSize(); + tablet.addTimestamp(rowIndex, timestamp); + tablet.addValue("region_id", rowIndex, "1"); + tablet.addValue("plant_id", rowIndex, "5"); + tablet.addValue("device_id", rowIndex, "3"); + tablet.addValue("model", rowIndex, "A"); + tablet.addValue("temperature", rowIndex, 37.6F); + tablet.addValue("humidity", rowIndex, 111.1); + if (tablet.getRowSize() == tablet.getMaxRowNumber()) { + session.insert(tablet); + tablet.reset(); + } + } + if (tablet.getRowSize() != 0) { + session.insert(tablet); + tablet.reset(); + } +} +``` + +After execution, you can verify the table creation using the following command: + +```SQL +IoTDB> desc table1 ++-----------+---------+-----------+ +| ColumnName| DataType| Category| ++-----------+---------+-----------+ +| time|TIMESTAMP| TIME| +| region_id| STRING| TAG| +| plant_id| STRING| TAG| +| device_id| STRING| TAG| +| model| STRING| ATTRIBUTE| +|temperature| FLOAT| FIELD| +| humidity| DOUBLE| FIELD| ++-----------+---------+-----------+ +``` + +### 1.3 Specified Column Insertion + +It is possible to insert data for specific columns. Columns not specified will remain `null`. + +**Example:** + +```SQL +insert into table1("region", "plant_id", "device_id", Time, "temperature", "displacement") values ('Hamburg', '3001', '3', 4, 90.0, 1200.0) + + +insert into table1("region", "plant_id", "device_id", Time, "temperature") values ('Hamburg, '3001', '3', 5, 90.0) +``` + +### 1.4 Null Value Insertion + +You can explicitly set `null` values for tag columns, attribute columns, and field columns. + +**Example**: + +Equivalent to the above partial column insertion. + +```SQL +# Equivalent to the example above +insert into table1("region", "plant_id", "device_id", "model", "maintenance_cycle", Time, "temperature", "displacement") values ('Hamburg', '3001', '3', null, null, 4, 90.0, 1200.0) + +insert into table1("region", "plant_id", "device_id", "model", "maintenance_cycle", Time, "temperature", "displacement") values ('Hamburg', '3001', '3', null, null, 5, 90.0, null) +``` + +If no tag columns are included, the system will automatically create a device with all tag column values set to `null`. + +> **Note:** This operation will not only automatically populate existing tag columns in the table with `null` values but will also populate any newly added tag columns with `null` values in the future. + +### 1.5 Multi-Row Insertion + +IoTDB supports inserting multiple rows of data in a single statement to improve efficiency. + +**Example**: + +```SQL +insert into table1 +values +(4, 'Frankfurt', '3001', '3', '1', '10', 90.0, 1200.0) +(5, 'Frankfurt', '3001', '3', '1', '10', 90.0, 1200.0) + + +insert into table1 +("region", "plant_id", "device_id", Time, "temperature", "displacement") +values +('Frankfurt', '3001', '3', 4, 90.0, 1200.0) +('Frankfurt', '3001', '3', 5, 90.0, 1200.0) +``` + +#### Notes + +- Referencing non-existent columns in SQL will result in an error code `COLUMN_NOT_EXIST(616)`. +- Data type mismatches between the insertion data and the column's data type will result in an error code `DATA_TYPE_MISMATCH(614)`. + + +### 1.6 Query Write-back + +The IoTDB table model supports the **append-only query write-back** feature, implemented via the `INSERT INTO QUERY` statement. This feature allows writing the results of a query into an **existing** table. + +> ​**Note**​: This feature is available starting from version V2.0.6. + +#### 1.6.1 Syntax Definition + +sql + +```sql +INSERT INTO table_name [ ( column [, ... ] ) ] query +``` + +The **query** component supports three formats, which are illustrated with examples below. + +Using the [sample data](../Reference/Sample-Data.md) as the data source, first create the target table: + +sql + +```sql +IoTDB:database1> CREATE TABLE target_table ( time TIMESTAMP TIME, region STRING TAG, device_id STRING TAG, temperature FLOAT FIELD ); +Msg: The statement is executed successfully. +``` + +1. Write-back via Standard Query Statement + +The `query` part is a direct `select ... from ...` query. + +​**Example**​: Use a standard query statement to write the `time`, `region`, `device_id`, and `temperature` data of the Beijing region from `table1` into `target_table`. + +sql + +```sql +IoTDB:database1> insert into target_table select time,region,device_id,temperature from table1 where region = 'Beijing' +Msg: The statement is executed successfully. +IoTDB:database1> select * from target_table where region='Beijing' ++-----------------------------+--------+-----------+-------------+ +| time| region| device_id| temperature| ++-----------------------------+--------+-----------+-------------+ +|2024-11-26T13:37:00.000+08:00| Beijing| 100| 90.0| +|2024-11-26T13:38:00.000+08:00| Beijing| 100| 90.0| +|2024-11-27T16:38:00.000+08:00| Beijing| 101| null| +|2024-11-27T16:39:00.000+08:00| Beijing| 101| 85.0| +|2024-11-27T16:40:00.000+08:00| Beijing| 101| 85.0| +|2024-11-27T16:41:00.000+08:00| Beijing| 101| 85.0| +|2024-11-27T16:42:00.000+08:00| Beijing| 101| null| +|2024-11-27T16:43:00.000+08:00| Beijing| 101| null| +|2024-11-27T16:44:00.000+08:00| Beijing| 101| null| ++-----------------------------+--------+-----------+-------------+ +Total line number = 9 +It costs 0.029s +``` + +2. Write-back via Table Reference Query + +The `query` part uses the table reference syntax `table source_table`. + +​**Example**​: Use a table reference query to write data from `table3` into `target_table`. + +sql + +```sql +IoTDB:database1> insert into target_table(time,device_id,temperature) table table3 +Msg: The statement is executed successfully. +IoTDB:database1> select * from target_table where region is null ++-----------------------------+------+-----------+-------------+ +| time|region| device_id| temperature| ++-----------------------------+------+-----------+-------------+ +|2025-05-13T00:00:00.001+08:00| null| d1| 90.0| +|2025-05-13T00:00:01.002+08:00| null| d1| 85.0| +|2025-05-13T00:00:02.101+08:00| null| d1| 85.0| +|2025-05-13T00:00:03.201+08:00| null| d1| null| +|2025-05-13T00:00:04.105+08:00| null| d1| 90.0| +|2025-05-13T00:00:05.023+08:00| null| d1| 85.0| +|2025-05-13T00:00:06.129+08:00| null| d1| 90.0| ++-----------------------------+------+-----------+-------------+ +Total line number = 7 +It costs 0.015s +``` + +3. Write-back via Subquery + +The `query` part is a parenthesized subquery. + +​**Example**​: Use a subquery to write the `time`, `region`, `device_id`, and `temperature` data from `table1` whose timestamps match the records of the Shanghai region in `table2` into `target_table`. + +sql + +```sql +IoTDB:database1> insert into target_table (select t1.time, t1.region as region, t1.device_id as device_id, t1.temperature as temperature from table1 t1 where t1.time in (select t2.time from table2 t2 where t2.region = 'Shanghai')) +Msg: The statement is executed successfully. +IoTDB:database1> select * from target_table where region = 'Shanghai' ++-----------------------------+---------+-----------+-------------+ +| time| region| device_id| temperature| ++-----------------------------+---------+-----------+-------------+ +|2024-11-28T08:00:00.000+08:00| Shanghai| 100| 85.0| +|2024-11-29T11:00:00.000+08:00| Shanghai| 100| null| ++-----------------------------+---------+-----------+-------------+ +Total line number = 2 +It costs 0.014s +``` + +#### 1.6.2 Notes + +* The source table in the `query` and the target table `table_name` are allowed to be the same table, e.g., `INSERT INTO testtb SELECT * FROM testtb`. +* The target table ​**must already exist**​; otherwise, the error message `550: Table 'xxx.xxx' does not exist` will be thrown. +* The number and data types of columns returned by the query **must exactly match** those of the target table. Type conversion between compatible types is not supported currently. A type mismatch will trigger the error message `701: Insert query has mismatched column types`. +* You can specify a subset of columns in the target table, provided the following rules are met: + * The timestamp column must be included; otherwise, the error message `701: time column can not be null` will be thrown. + * At least one **FIELD** column must be included; otherwise, the error message `701: No Field column present` will be thrown. + * **TAG** columns are optional. + * The number of specified columns can be less than that of the target table. Missing columns will be automatically filled with `NULL` values. +* For Java applications, the `INSERT INTO QUERY` statement can be executed using the [executeNonQueryStatement](../API/Programming-Java-Native-API_apache.md#_3-1-itablesession-interface) method. +* For REST API access, the `INSERT INTO QUERY` statement can be executed via the [/rest/table/v1/nonQuery](../API/RestAPI-V1.md#_3-3-non-query-interface) endpoint. +* `INSERT INTO QUERY` does **not** support the `EXPLAIN` and `EXPLAIN ANALYZE` commands. +* To execute the query write-back statement successfully, users must have the following permissions: + * The `SELECT` permission on the source tables involved in the query. + * The `WRITE` permission on the target table. + * For more details about user permissions, refer to [Authority Management](../User-Manual/Authority-Management_apache.md). + + + +## 2. Data Updates + +### 2.1 Syntax + +```SQL +UPDATE SET updateAssignment (',' updateAssignment)* (WHERE where=booleanExpression)? + +updateAssignment + : identifier EQ expression + ; +``` + +**Note:** + +- Updates are allowed only on `ATTRIBUTE` columns. +- `WHERE` conditions: + - Can only include `TAG` and `ATTRIBUTE` columns; `FIELD` and `TIME` columns are not allowed. + - Aggregation functions are not supported. +- The result of the `SET` assignment expression must be a `string` type and follow the same constraints as the `WHERE` clause. + +**Example**: + +```SQL +update table1 set b = a where substring(a, 1, 1) like '%' +``` diff --git a/src/UserGuide/Master/Table/Basic-Concept/Write-Updata-Data_timecho.md b/src/UserGuide/Master/Table/Basic-Concept/Write-Updata-Data_timecho.md new file mode 100644 index 000000000..e1c03bd46 --- /dev/null +++ b/src/UserGuide/Master/Table/Basic-Concept/Write-Updata-Data_timecho.md @@ -0,0 +1,332 @@ + + +# Write & Update Data + +## 1. Data Insertion + +### 1.1 Syntax + +In IoTDB, data insertion follows the general syntax: + +```SQL +INSERT INTO [(COLUMN_NAME[, COLUMN_NAME]*)]? VALUES (COLUMN_VALUE[, COLUMN_VALUE]*) +``` + +**Basic Constraints**: + +1. Tables cannot be automatically created using `INSERT` statements. +2. Columns not specified in the `INSERT` statement will automatically be filled with `null`. +3. If no timestamp is provided, the system will use the current time (`now()`). +4. If a column value does not exist for the identified device, the insertion will overwrite any existing `null` values with the new data. +5. If a column value already exists for the identified device, a new insertion will update the column with the new value. +6. Writing duplicate timestamps will update the values in the columns corresponding to the original timestamps. +7. When an INSERT statement does not specify column names (e.g., INSERT INTO table VALUES (...)), the values in VALUES must strictly follow the physical order of columns in the table (this order can be checked via the DESC table command). + +Since attributes generally do not change over time, it is recommended to update attribute values using the `UPDATE` statement described below,Please refer to the following [Data Update](#2-data-updates). + +
+ +
+ + +### 1.2 Automatically Create Tables via Session Insertion + +When performing data writing through Session, IoTDB supports schema-less writing: there is no need to manually create tables beforehand. The system automatically constructs the table structure based on the information in the write request, and then directly executes the data writing operation. + +**Example:** + +```Java +try (ITableSession session = + new TableSessionBuilder() + .nodeUrls(Collections.singletonList("127.0.0.1:6667")) + .username("root") + .password("root") + .build()) { + + session.executeNonQueryStatement("CREATE DATABASE db1"); + session.executeNonQueryStatement("use db1"); + + // Insert data without manually creating the table + List columnNameList = + Arrays.asList("region_id", "plant_id", "device_id", "model", "temperature", "humidity"); + List dataTypeList = + Arrays.asList( + TSDataType.STRING, + TSDataType.STRING, + TSDataType.STRING, + TSDataType.STRING, + TSDataType.FLOAT, + TSDataType.DOUBLE); + List columnTypeList = + new ArrayList<>( + Arrays.asList( + ColumnCategory.TAG, + ColumnCategory.TAG, + ColumnCategory.TAG, + ColumnCategory.ATTRIBUTE, + ColumnCategory.FIELD, + ColumnCategory.FIELD)); + Tablet tablet = new Tablet("table1", columnNameList, dataTypeList, columnTypeList, 100); + for (long timestamp = 0; timestamp < 100; timestamp++) { + int rowIndex = tablet.getRowSize(); + tablet.addTimestamp(rowIndex, timestamp); + tablet.addValue("region_id", rowIndex, "1"); + tablet.addValue("plant_id", rowIndex, "5"); + tablet.addValue("device_id", rowIndex, "3"); + tablet.addValue("model", rowIndex, "A"); + tablet.addValue("temperature", rowIndex, 37.6F); + tablet.addValue("humidity", rowIndex, 111.1); + if (tablet.getRowSize() == tablet.getMaxRowNumber()) { + session.insert(tablet); + tablet.reset(); + } + } + if (tablet.getRowSize() != 0) { + session.insert(tablet); + tablet.reset(); + } +} +``` + +After execution, you can verify the table creation using the following command: + +```SQL +IoTDB> desc table1 ++-----------+---------+-----------+ +| ColumnName| DataType| Category| ++-----------+---------+-----------+ +| time|TIMESTAMP| TIME| +| region_id| STRING| TAG| +| plant_id| STRING| TAG| +| device_id| STRING| TAG| +| model| STRING| ATTRIBUTE| +|temperature| FLOAT| FIELD| +| humidity| DOUBLE| FIELD| ++-----------+---------+-----------+ +``` + +### 1.3 Specified Column Insertion + +It is possible to insert data for specific columns. Columns not specified will remain `null`. + +**Example:** + +```SQL +insert into table1("region", "plant_id", "device_id", Time, "temperature", "displacement") values ('Hamburg', '3001', '3', 4, 90.0, 1200.0) + + +insert into table1("region", "plant_id", "device_id", Time, "temperature") values ('Hamburg, '3001', '3', 5, 90.0) +``` + +### 1.4 Null Value Insertion + +You can explicitly set `null` values for tag columns, attribute columns, and field columns. + +**Example**: + +Equivalent to the above partial column insertion. + +```SQL +# Equivalent to the example above +insert into table1("region", "plant_id", "device_id", "model", "maintenance_cycle", Time, "temperature", "displacement") values ('Hamburg', '3001', '3', null, null, 4, 90.0, 1200.0) + +insert into table1("region", "plant_id", "device_id", "model", "maintenance_cycle", Time, "temperature", "displacement") values ('Hamburg', '3001', '3', null, null, 5, 90.0, null) +``` + +If no tag columns are included, the system will automatically create a device with all tag column values set to `null`. + +> **Note:** This operation will not only automatically populate existing tag columns in the table with `null` values but will also populate any newly added tag columns with `null` values in the future. + +### 1.5 Multi-Row Insertion + +IoTDB supports inserting multiple rows of data in a single statement to improve efficiency. + +**Example**: + +```SQL +insert into table1 +values +(4, 'Frankfurt', '3001', '3', '1', '10', 90.0, 1200.0) +(5, 'Frankfurt', '3001', '3', '1', '10', 90.0, 1200.0) + + +insert into table1 +("region", "plant_id", "device_id", Time, "temperature", "displacement") +values +('Frankfurt', '3001', '3', 4, 90.0, 1200.0) +('Frankfurt', '3001', '3', 5, 90.0, 1200.0) +``` + +#### Notes + +- Referencing non-existent columns in SQL will result in an error code `COLUMN_NOT_EXIST(616)`. +- Data type mismatches between the insertion data and the column's data type will result in an error code `DATA_TYPE_MISMATCH(614)`. + + +### 1.6 Query Write-back + +The IoTDB table model supports the **append-only query write-back** feature, implemented via the `INSERT INTO QUERY` statement. This feature allows writing the results of a query into an **existing** table. + +> ​**Note**​: This feature is available starting from version V2.0.6. + +#### 1.6.1 Syntax Definition + +sql + +```sql +INSERT INTO table_name [ ( column [, ... ] ) ] query +``` + +The **query** component supports three formats, which are illustrated with examples below. + +Using the [sample data](../Reference/Sample-Data.md) as the data source, first create the target table: + +sql + +```sql +IoTDB:database1> CREATE TABLE target_table ( time TIMESTAMP TIME, region STRING TAG, device_id STRING TAG, temperature FLOAT FIELD ); +Msg: The statement is executed successfully. +``` + +1. Write-back via Standard Query Statement + +The `query` part is a direct `select ... from ...` query. + +​**Example**​: Use a standard query statement to write the `time`, `region`, `device_id`, and `temperature` data of the Beijing region from `table1` into `target_table`. + +sql + +```sql +IoTDB:database1> insert into target_table select time,region,device_id,temperature from table1 where region = 'Beijing' +Msg: The statement is executed successfully. +IoTDB:database1> select * from target_table where region='Beijing' ++-----------------------------+--------+-----------+-------------+ +| time| region| device_id| temperature| ++-----------------------------+--------+-----------+-------------+ +|2024-11-26T13:37:00.000+08:00| Beijing| 100| 90.0| +|2024-11-26T13:38:00.000+08:00| Beijing| 100| 90.0| +|2024-11-27T16:38:00.000+08:00| Beijing| 101| null| +|2024-11-27T16:39:00.000+08:00| Beijing| 101| 85.0| +|2024-11-27T16:40:00.000+08:00| Beijing| 101| 85.0| +|2024-11-27T16:41:00.000+08:00| Beijing| 101| 85.0| +|2024-11-27T16:42:00.000+08:00| Beijing| 101| null| +|2024-11-27T16:43:00.000+08:00| Beijing| 101| null| +|2024-11-27T16:44:00.000+08:00| Beijing| 101| null| ++-----------------------------+--------+-----------+-------------+ +Total line number = 9 +It costs 0.029s +``` + +2. Write-back via Table Reference Query + +The `query` part uses the table reference syntax `table source_table`. + +​**Example**​: Use a table reference query to write data from `table3` into `target_table`. + +sql + +```sql +IoTDB:database1> insert into target_table(time,device_id,temperature) table table3 +Msg: The statement is executed successfully. +IoTDB:database1> select * from target_table where region is null ++-----------------------------+------+-----------+-------------+ +| time|region| device_id| temperature| ++-----------------------------+------+-----------+-------------+ +|2025-05-13T00:00:00.001+08:00| null| d1| 90.0| +|2025-05-13T00:00:01.002+08:00| null| d1| 85.0| +|2025-05-13T00:00:02.101+08:00| null| d1| 85.0| +|2025-05-13T00:00:03.201+08:00| null| d1| null| +|2025-05-13T00:00:04.105+08:00| null| d1| 90.0| +|2025-05-13T00:00:05.023+08:00| null| d1| 85.0| +|2025-05-13T00:00:06.129+08:00| null| d1| 90.0| ++-----------------------------+------+-----------+-------------+ +Total line number = 7 +It costs 0.015s +``` + +3. Write-back via Subquery + +The `query` part is a parenthesized subquery. + +​**Example**​: Use a subquery to write the `time`, `region`, `device_id`, and `temperature` data from `table1` whose timestamps match the records of the Shanghai region in `table2` into `target_table`. + +sql + +```sql +IoTDB:database1> insert into target_table (select t1.time, t1.region as region, t1.device_id as device_id, t1.temperature as temperature from table1 t1 where t1.time in (select t2.time from table2 t2 where t2.region = 'Shanghai')) +Msg: The statement is executed successfully. +IoTDB:database1> select * from target_table where region = 'Shanghai' ++-----------------------------+---------+-----------+-------------+ +| time| region| device_id| temperature| ++-----------------------------+---------+-----------+-------------+ +|2024-11-28T08:00:00.000+08:00| Shanghai| 100| 85.0| +|2024-11-29T11:00:00.000+08:00| Shanghai| 100| null| ++-----------------------------+---------+-----------+-------------+ +Total line number = 2 +It costs 0.014s +``` + +#### 1.6.2 Notes + +* The source table in the `query` and the target table `table_name` are allowed to be the same table, e.g., `INSERT INTO testtb SELECT * FROM testtb`. +* The target table ​**must already exist**​; otherwise, the error message `550: Table 'xxx.xxx' does not exist` will be thrown. +* The number and data types of columns returned by the query **must exactly match** those of the target table. Type conversion between compatible types is not supported currently. A type mismatch will trigger the error message `701: Insert query has mismatched column types`. +* You can specify a subset of columns in the target table, provided the following rules are met: + * The timestamp column must be included; otherwise, the error message `701: time column can not be null` will be thrown. + * At least one **FIELD** column must be included; otherwise, the error message `701: No Field column present` will be thrown. + * **TAG** columns are optional. + * The number of specified columns can be less than that of the target table. Missing columns will be automatically filled with `NULL` values. +* For Java applications, the `INSERT INTO QUERY` statement can be executed using the [executeNonQueryStatement](../API/Programming-Java-Native-API_timecho.md#_3-1-itablesession-interface) method. +* For REST API access, the `INSERT INTO QUERY` statement can be executed via the [/rest/table/v1/nonQuery](../API/RestAPI-V1.md#_3-3-non-query-interface) endpoint. +* `INSERT INTO QUERY` does **not** support the `EXPLAIN` and `EXPLAIN ANALYZE` commands. +* To execute the query write-back statement successfully, users must have the following permissions: + * The `SELECT` permission on the source tables involved in the query. + * The `WRITE` permission on the target table. + * For more details about user permissions, refer to [Authority Management](../User-Manual/Authority-Management_timecho.md). + + + +## 2. Data Updates + +### 2.1 Syntax + +```SQL +UPDATE SET updateAssignment (',' updateAssignment)* (WHERE where=booleanExpression)? + +updateAssignment + : identifier EQ expression + ; +``` + +**Note:** + +- Updates are allowed only on `ATTRIBUTE` columns. +- `WHERE` conditions: + - Can only include `TAG` and `ATTRIBUTE` columns; `FIELD` and `TIME` columns are not allowed. + - Aggregation functions are not supported. +- The result of the `SET` assignment expression must be a `string` type and follow the same constraints as the `WHERE` clause. + +**Example**: + +```SQL +update table1 set b = a where substring(a, 1, 1) like '%' +``` diff --git a/src/UserGuide/Master/Table/QuickStart/QuickStart_apache.md b/src/UserGuide/Master/Table/QuickStart/QuickStart_apache.md index b7b7fcf55..612bfbe1a 100644 --- a/src/UserGuide/Master/Table/QuickStart/QuickStart_apache.md +++ b/src/UserGuide/Master/Table/QuickStart/QuickStart_apache.md @@ -53,7 +53,7 @@ This guide will assist you in quickly installing and deploying IoTDB. You can qu - Introduction to Tables: [Table Management](../Basic-Concept/Table-Management.md) -2. Data Insertion & Updates: IoTDB provides multiple methods for inserting real-time data. For basic data insertion and updating operations, please see [Write&Updata Data](../Basic-Concept/Write-Updata-Data.md) +2. Data Insertion & Updates: IoTDB provides multiple methods for inserting real-time data, and supports data write-back. For basic data insertion and updating operations, please see [Write&Updata Data](../Basic-Concept/Write-Updata-Data_apache.md) 3. Data Querying: IoTDB offers a rich set of data querying capabilities. For a basic introduction to data querying, please see [Query Data](../Basic-Concept/Query-Data_apache.md) diff --git a/src/UserGuide/Master/Table/QuickStart/QuickStart_timecho.md b/src/UserGuide/Master/Table/QuickStart/QuickStart_timecho.md index a91a86dcb..f976f1da2 100644 --- a/src/UserGuide/Master/Table/QuickStart/QuickStart_timecho.md +++ b/src/UserGuide/Master/Table/QuickStart/QuickStart_timecho.md @@ -60,7 +60,7 @@ This guide will assist you in quickly installing and deploying IoTDB. You can qu - Introduction to Tables: [Table Management](../Basic-Concept/Table-Management.md) -2. Data Insertion & Updates: IoTDB provides multiple methods for inserting real-time data. For basic data insertion and updating operations, please see [Write&Updata Data](../Basic-Concept/Write-Updata-Data.md) +2. Data Insertion & Updates: IoTDB provides multiple methods for inserting real-time data, and supports data write-back. For basic data insertion and updating operations, please see [Write&Updata Data](../Basic-Concept/Write-Updata-Data_timecho.md) 3. Data Querying: IoTDB offers a rich set of data querying capabilities. For a basic introduction to data querying, please see [Query Data](../Basic-Concept/Query-Data_timecho.md). It includes [Row Pattern Recognition](../SQL-Manual/Row-Pattern-Recognition.md) suitable for business scenarios such as identifying specific patterns in time-series data and detecting specific events, as well as [Window Functions](../SQL-Manual/Featured-Functions_timecho.md#_4-Window-Functions) and other featured functions often used in data analysis scenarios. diff --git a/src/UserGuide/Master/Table/SQL-Manual/SQL-Data-Addition-Deletion.md b/src/UserGuide/Master/Table/SQL-Manual/SQL-Data-Addition-Deletion.md index 475eafd52..84dc187da 100644 --- a/src/UserGuide/Master/Table/SQL-Manual/SQL-Data-Addition-Deletion.md +++ b/src/UserGuide/Master/Table/SQL-Manual/SQL-Data-Addition-Deletion.md @@ -1,3 +1,6 @@ +--- +redirectTo: SQL-Data-Addition-Deletion_apache.html +--- - -# Data Addition & Deletion - -## 1. Data Insertion - -**Syntax:** - -```SQL -INSERT INTO [(COLUMN_NAME[, COLUMN_NAME]*)]? VALUES (COLUMN_VALUE[, COLUMN_VALUE]*) -``` - -[Detailed syntax reference](../Basic-Concept/Write-Updata-Data.md#_1-1-syntax) - -**Example 1: Specified Columns Insertion** - -```SQL -INSERT INTO table1("Region", "PlantID", "DeviceID", Time, "Temperature", "Displacement") -VALUES ('Hunan', '3001', '3', 4, 90.0, 1200.0); - -INSERT INTO table1("Region", "PlantID", "DeviceID", Time, "Temperature") -VALUES ('Hunan', '3001', '3', 5, 90.0); -``` - -**Example 2: NULL Value Insertion** - -```SQL --- Equivalent to partial insertion with NULL values -INSERT INTO table1("Region", "PlantID", "DeviceID", "Model", "MaintenanceCycle", Time, "Temperature", "Displacement") -VALUES ('Hunan', '3001', '3', NULL, NULL, 4, 90.0, 1200.0); - -INSERT INTO table1("Region", "PlantID", "DeviceID", "Model", "MaintenanceCycle", Time, "Temperature", "Displacement") -VALUES ('Hunan', '3001', '3', NULL, NULL, 5, 90.0, NULL); -``` - -**Example 3: Multi-row Insertion** - -```SQL -INSERT INTO table1 VALUES -(4, 'Beijing', '3001', '3', '1', '10', 90.0, 1200.0), -(5, 'Beijing', '3001', '3', '1', '10', 90.0, 1200.0); - -INSERT INTO table1("Region", "PlantID", "DeviceID", Time, "Temperature", "Displacement") -VALUES -('Beijing', '3001', '3', 4, 90.0, 1200.0), -('Beijing', '3001', '3', 5, 90.0, 1200.0); -``` - -## 2. Data Update - -**Syntax:** - -```SQL -UPDATE SET updateAssignment (',' updateAssignment)* (WHERE where=booleanExpression)? - -updateAssignment - : identifier EQ expression - ; -``` - -[Detailed syntax reference](../Basic-Concept/Write-Updata-Data.md#_2-1-syntax) - -**Example:** - -```SQL -update table1 set b = a where substring(a, 1, 1) like '%' -``` - -## 3. Data Deletion - -**Syntax:** - -```SQL -DELETE FROM [WHERE_CLAUSE]? - -WHERE_CLAUSE: - WHERE DELETE_CONDITION - -DELETE_CONDITION: - SINGLE_CONDITION - | DELETE_CONDITION AND DELETE_CONDITION - | DELETE_CONDITION OR DELETE_CONDITION - -SINGLE_CODITION: - TIME_CONDITION | ID_CONDITION - -TIME_CONDITION: - time TIME_OPERATOR LONG_LITERAL - -TIME_OPERATOR: - < | > | <= | >= | = - -ID_CONDITION: - identifier = STRING_LITERAL -``` - -**Example 1: Full Table Deletion** - -```SQL -DELETE FROM table1 -``` - -**Example 2: Time-range Deletion** - -```SQL --- Single time range -DELETE FROM table1 WHERE time <= 2024-11-29 00:00:00 - --- Multiple time ranges -DELETE FROM table1 WHERE time >= 2024-11-27 00:00:00 and time <= 2024-11-29 00:00:00 -``` - -**Example 3: Device-Specific Deletion** - -```SQL --- Delete data for specific device -DELETE FROM table1 -WHERE device_id='101' AND model_id = 'B'; - --- Delete data for device within time range -DELETE FROM table1 -WHERE time >= '2024-11-27 16:39:00' AND time <= '2024-11-29 16:42:00' - AND device_id='101' AND model_id = 'B'; - --- Delete data for specific device model -DELETE FROM table1 WHERE model_id = 'B'; -``` - -## 4. Device Deletion - -**Syntax:** - -```SQL -DELETE DEVICES FROM tableName=qualifiedName (WHERE booleanExpression)? -``` - -**Example: Delete specified device and all associated data** - -```SQL -DELETE DEVICES FROM table1 WHERE device_id = '101' -``` diff --git a/src/UserGuide/Master/Table/SQL-Manual/SQL-Data-Addition-Deletion_apache.md b/src/UserGuide/Master/Table/SQL-Manual/SQL-Data-Addition-Deletion_apache.md new file mode 100644 index 000000000..54046c3c0 --- /dev/null +++ b/src/UserGuide/Master/Table/SQL-Manual/SQL-Data-Addition-Deletion_apache.md @@ -0,0 +1,171 @@ + + +# Data Addition & Deletion + +## 1. Data Insertion + +**Syntax:** + +```SQL +INSERT INTO [(COLUMN_NAME[, COLUMN_NAME]*)]? VALUES (COLUMN_VALUE[, COLUMN_VALUE]*) +``` + +[Detailed syntax reference](../Basic-Concept/Write-Updata-Data_apache.md#_1-1-syntax) + +**Example 1: Specified Columns Insertion** + +```SQL +INSERT INTO table1("Region", "PlantID", "DeviceID", Time, "Temperature", "Displacement") +VALUES ('Hunan', '3001', '3', 4, 90.0, 1200.0); + +INSERT INTO table1("Region", "PlantID", "DeviceID", Time, "Temperature") +VALUES ('Hunan', '3001', '3', 5, 90.0); +``` + +**Example 2: NULL Value Insertion** + +```SQL +-- Equivalent to partial insertion with NULL values +INSERT INTO table1("Region", "PlantID", "DeviceID", "Model", "MaintenanceCycle", Time, "Temperature", "Displacement") +VALUES ('Hunan', '3001', '3', NULL, NULL, 4, 90.0, 1200.0); + +INSERT INTO table1("Region", "PlantID", "DeviceID", "Model", "MaintenanceCycle", Time, "Temperature", "Displacement") +VALUES ('Hunan', '3001', '3', NULL, NULL, 5, 90.0, NULL); +``` + +**Example 3: Multi-row Insertion** + +```SQL +INSERT INTO table1 VALUES +(4, 'Beijing', '3001', '3', '1', '10', 90.0, 1200.0), +(5, 'Beijing', '3001', '3', '1', '10', 90.0, 1200.0); + +INSERT INTO table1("Region", "PlantID", "DeviceID", Time, "Temperature", "Displacement") +VALUES +('Beijing', '3001', '3', 4, 90.0, 1200.0), +('Beijing', '3001', '3', 5, 90.0, 1200.0); +``` + +**Example 4: Query Write-back** + +```SQL +insert into target_table select time,region,device_id,temperature from table1 where region = 'bj'; + +insert into target_table(time,device_id,temperature) table table3; + +insert into target_table (select t1.time, t1.region as region, t1.device_id as device_id, t1.temperature as temperature from table1 t1 where t1.time in (select t2.time from table2 t2 where t2.region = 'shh')); +``` + + +## 2. Data Update + +**Syntax:** + +```SQL +UPDATE SET updateAssignment (',' updateAssignment)* (WHERE where=booleanExpression)? + +updateAssignment + : identifier EQ expression + ; +``` + +[Detailed syntax reference](../Basic-Concept/Write-Updata-Data_apache.md#_2-1-syntax) + +**Example:** + +```SQL +update table1 set b = a where substring(a, 1, 1) like '%' +``` + +## 3. Data Deletion + +**Syntax:** + +```SQL +DELETE FROM [WHERE_CLAUSE]? + +WHERE_CLAUSE: + WHERE DELETE_CONDITION + +DELETE_CONDITION: + SINGLE_CONDITION + | DELETE_CONDITION AND DELETE_CONDITION + | DELETE_CONDITION OR DELETE_CONDITION + +SINGLE_CODITION: + TIME_CONDITION | ID_CONDITION + +TIME_CONDITION: + time TIME_OPERATOR LONG_LITERAL + +TIME_OPERATOR: + < | > | <= | >= | = + +ID_CONDITION: + identifier = STRING_LITERAL +``` + +**Example 1: Full Table Deletion** + +```SQL +DELETE FROM table1 +``` + +**Example 2: Time-range Deletion** + +```SQL +-- Single time range +DELETE FROM table1 WHERE time <= 2024-11-29 00:00:00 + +-- Multiple time ranges +DELETE FROM table1 WHERE time >= 2024-11-27 00:00:00 and time <= 2024-11-29 00:00:00 +``` + +**Example 3: Device-Specific Deletion** + +```SQL +-- Delete data for specific device +DELETE FROM table1 +WHERE device_id='101' AND model_id = 'B'; + +-- Delete data for device within time range +DELETE FROM table1 +WHERE time >= '2024-11-27 16:39:00' AND time <= '2024-11-29 16:42:00' + AND device_id='101' AND model_id = 'B'; + +-- Delete data for specific device model +DELETE FROM table1 WHERE model_id = 'B'; +``` + +## 4. Device Deletion + +**Syntax:** + +```SQL +DELETE DEVICES FROM tableName=qualifiedName (WHERE booleanExpression)? +``` + +**Example: Delete specified device and all associated data** + +```SQL +DELETE DEVICES FROM table1 WHERE device_id = '101' +``` diff --git a/src/UserGuide/Master/Table/SQL-Manual/SQL-Data-Addition-Deletion_timecho.md b/src/UserGuide/Master/Table/SQL-Manual/SQL-Data-Addition-Deletion_timecho.md new file mode 100644 index 000000000..26edcaba3 --- /dev/null +++ b/src/UserGuide/Master/Table/SQL-Manual/SQL-Data-Addition-Deletion_timecho.md @@ -0,0 +1,171 @@ + + +# Data Addition & Deletion + +## 1. Data Insertion + +**Syntax:** + +```SQL +INSERT INTO [(COLUMN_NAME[, COLUMN_NAME]*)]? VALUES (COLUMN_VALUE[, COLUMN_VALUE]*) +``` + +[Detailed syntax reference](../Basic-Concept/Write-Updata-Data_timecho.md#_1-1-syntax) + +**Example 1: Specified Columns Insertion** + +```SQL +INSERT INTO table1("Region", "PlantID", "DeviceID", Time, "Temperature", "Displacement") +VALUES ('Hunan', '3001', '3', 4, 90.0, 1200.0); + +INSERT INTO table1("Region", "PlantID", "DeviceID", Time, "Temperature") +VALUES ('Hunan', '3001', '3', 5, 90.0); +``` + +**Example 2: NULL Value Insertion** + +```SQL +-- Equivalent to partial insertion with NULL values +INSERT INTO table1("Region", "PlantID", "DeviceID", "Model", "MaintenanceCycle", Time, "Temperature", "Displacement") +VALUES ('Hunan', '3001', '3', NULL, NULL, 4, 90.0, 1200.0); + +INSERT INTO table1("Region", "PlantID", "DeviceID", "Model", "MaintenanceCycle", Time, "Temperature", "Displacement") +VALUES ('Hunan', '3001', '3', NULL, NULL, 5, 90.0, NULL); +``` + +**Example 3: Multi-row Insertion** + +```SQL +INSERT INTO table1 VALUES +(4, 'Beijing', '3001', '3', '1', '10', 90.0, 1200.0), +(5, 'Beijing', '3001', '3', '1', '10', 90.0, 1200.0); + +INSERT INTO table1("Region", "PlantID", "DeviceID", Time, "Temperature", "Displacement") +VALUES +('Beijing', '3001', '3', 4, 90.0, 1200.0), +('Beijing', '3001', '3', 5, 90.0, 1200.0); +``` + +**Example 4: Query Write-back** + +```SQL +insert into target_table select time,region,device_id,temperature from table1 where region = 'bj'; + +insert into target_table(time,device_id,temperature) table table3; + +insert into target_table (select t1.time, t1.region as region, t1.device_id as device_id, t1.temperature as temperature from table1 t1 where t1.time in (select t2.time from table2 t2 where t2.region = 'shh')); +``` + + +## 2. Data Update + +**Syntax:** + +```SQL +UPDATE SET updateAssignment (',' updateAssignment)* (WHERE where=booleanExpression)? + +updateAssignment + : identifier EQ expression + ; +``` + +[Detailed syntax reference](../Basic-Concept/Write-Updata-Data_timecho.md#_2-1-syntax) + +**Example:** + +```SQL +update table1 set b = a where substring(a, 1, 1) like '%' +``` + +## 3. Data Deletion + +**Syntax:** + +```SQL +DELETE FROM [WHERE_CLAUSE]? + +WHERE_CLAUSE: + WHERE DELETE_CONDITION + +DELETE_CONDITION: + SINGLE_CONDITION + | DELETE_CONDITION AND DELETE_CONDITION + | DELETE_CONDITION OR DELETE_CONDITION + +SINGLE_CODITION: + TIME_CONDITION | ID_CONDITION + +TIME_CONDITION: + time TIME_OPERATOR LONG_LITERAL + +TIME_OPERATOR: + < | > | <= | >= | = + +ID_CONDITION: + identifier = STRING_LITERAL +``` + +**Example 1: Full Table Deletion** + +```SQL +DELETE FROM table1 +``` + +**Example 2: Time-range Deletion** + +```SQL +-- Single time range +DELETE FROM table1 WHERE time <= 2024-11-29 00:00:00 + +-- Multiple time ranges +DELETE FROM table1 WHERE time >= 2024-11-27 00:00:00 and time <= 2024-11-29 00:00:00 +``` + +**Example 3: Device-Specific Deletion** + +```SQL +-- Delete data for specific device +DELETE FROM table1 +WHERE device_id='101' AND model_id = 'B'; + +-- Delete data for device within time range +DELETE FROM table1 +WHERE time >= '2024-11-27 16:39:00' AND time <= '2024-11-29 16:42:00' + AND device_id='101' AND model_id = 'B'; + +-- Delete data for specific device model +DELETE FROM table1 WHERE model_id = 'B'; +``` + +## 4. Device Deletion + +**Syntax:** + +```SQL +DELETE DEVICES FROM tableName=qualifiedName (WHERE booleanExpression)? +``` + +**Example: Delete specified device and all associated data** + +```SQL +DELETE DEVICES FROM table1 WHERE device_id = '101' +``` diff --git a/src/UserGuide/latest-Table/Basic-Concept/Write-Updata-Data.md b/src/UserGuide/latest-Table/Basic-Concept/Write-Updata-Data.md index a251e2f12..98f66ca6c 100644 --- a/src/UserGuide/latest-Table/Basic-Concept/Write-Updata-Data.md +++ b/src/UserGuide/latest-Table/Basic-Concept/Write-Updata-Data.md @@ -1,3 +1,6 @@ +--- +redirectTo: Write-Updata-Data_apache.html +--- - -# Write & Update Data - -## 1. Data Insertion - -### 1.1 Syntax - -In IoTDB, data insertion follows the general syntax: - -```SQL -INSERT INTO [(COLUMN_NAME[, COLUMN_NAME]*)]? VALUES (COLUMN_VALUE[, COLUMN_VALUE]*) -``` - -**Basic Constraints**: - -1. Tables cannot be automatically created using `INSERT` statements. -2. Columns not specified in the `INSERT` statement will automatically be filled with `null`. -3. If no timestamp is provided, the system will use the current time (`now()`). -4. If a column value does not exist for the identified device, the insertion will overwrite any existing `null` values with the new data. -5. If a column value already exists for the identified device, a new insertion will update the column with the new value. -6. Writing duplicate timestamps will update the values in the columns corresponding to the original timestamps. -7. When an INSERT statement does not specify column names (e.g., INSERT INTO table VALUES (...)), the values in VALUES must strictly follow the physical order of columns in the table (this order can be checked via the DESC table command). - -Since attributes generally do not change over time, it is recommended to update attribute values using the `UPDATE` statement described below,Please refer to the following [Data Update](#2-data-updates). - -
- -
- - -### 1.2 Automatically Create Tables via Session Insertion - -When performing data writing through Session, IoTDB supports schema-less writing: there is no need to manually create tables beforehand. The system automatically constructs the table structure based on the information in the write request, and then directly executes the data writing operation. - -**Example:** - -```Java -try (ITableSession session = - new TableSessionBuilder() - .nodeUrls(Collections.singletonList("127.0.0.1:6667")) - .username("root") - .password("root") - .build()) { - - session.executeNonQueryStatement("CREATE DATABASE db1"); - session.executeNonQueryStatement("use db1"); - - // Insert data without manually creating the table - List columnNameList = - Arrays.asList("region_id", "plant_id", "device_id", "model", "temperature", "humidity"); - List dataTypeList = - Arrays.asList( - TSDataType.STRING, - TSDataType.STRING, - TSDataType.STRING, - TSDataType.STRING, - TSDataType.FLOAT, - TSDataType.DOUBLE); - List columnTypeList = - new ArrayList<>( - Arrays.asList( - ColumnCategory.TAG, - ColumnCategory.TAG, - ColumnCategory.TAG, - ColumnCategory.ATTRIBUTE, - ColumnCategory.FIELD, - ColumnCategory.FIELD)); - Tablet tablet = new Tablet("table1", columnNameList, dataTypeList, columnTypeList, 100); - for (long timestamp = 0; timestamp < 100; timestamp++) { - int rowIndex = tablet.getRowSize(); - tablet.addTimestamp(rowIndex, timestamp); - tablet.addValue("region_id", rowIndex, "1"); - tablet.addValue("plant_id", rowIndex, "5"); - tablet.addValue("device_id", rowIndex, "3"); - tablet.addValue("model", rowIndex, "A"); - tablet.addValue("temperature", rowIndex, 37.6F); - tablet.addValue("humidity", rowIndex, 111.1); - if (tablet.getRowSize() == tablet.getMaxRowNumber()) { - session.insert(tablet); - tablet.reset(); - } - } - if (tablet.getRowSize() != 0) { - session.insert(tablet); - tablet.reset(); - } -} -``` - -After execution, you can verify the table creation using the following command: - -```SQL -IoTDB> desc table1 -+-----------+---------+-----------+ -| ColumnName| DataType| Category| -+-----------+---------+-----------+ -| time|TIMESTAMP| TIME| -| region_id| STRING| TAG| -| plant_id| STRING| TAG| -| device_id| STRING| TAG| -| model| STRING| ATTRIBUTE| -|temperature| FLOAT| FIELD| -| humidity| DOUBLE| FIELD| -+-----------+---------+-----------+ -``` - -### 1.3 Specified Column Insertion - -It is possible to insert data for specific columns. Columns not specified will remain `null`. - -**Example:** - -```SQL -insert into table1("region", "plant_id", "device_id", Time, "temperature", "displacement") values ('Hamburg', '3001', '3', 4, 90.0, 1200.0) - - -insert into table1("region", "plant_id", "device_id", Time, "temperature") values ('Hamburg, '3001', '3', 5, 90.0) -``` - -### 1.4 Null Value Insertion - -You can explicitly set `null` values for tag columns, attribute columns, and field columns. - -**Example**: - -Equivalent to the above partial column insertion. - -```SQL -# Equivalent to the example above -insert into table1("region", "plant_id", "device_id", "model", "maintenance_cycle", Time, "temperature", "displacement") values ('Hamburg', '3001', '3', null, null, 4, 90.0, 1200.0) - -insert into table1("region", "plant_id", "device_id", "model", "maintenance_cycle", Time, "temperature", "displacement") values ('Hamburg', '3001', '3', null, null, 5, 90.0, null) -``` - -If no tag columns are included, the system will automatically create a device with all tag column values set to `null`. - -> **Note:** This operation will not only automatically populate existing tag columns in the table with `null` values but will also populate any newly added tag columns with `null` values in the future. - -### 1.5 Multi-Row Insertion - -IoTDB supports inserting multiple rows of data in a single statement to improve efficiency. - -**Example**: - -```SQL -insert into table1 -values -(4, 'Frankfurt', '3001', '3', '1', '10', 90.0, 1200.0) -(5, 'Frankfurt', '3001', '3', '1', '10', 90.0, 1200.0) - - -insert into table1 -("region", "plant_id", "device_id", Time, "temperature", "displacement") -values -('Frankfurt', '3001', '3', 4, 90.0, 1200.0) -('Frankfurt', '3001', '3', 5, 90.0, 1200.0) -``` - -#### Notes - -- Referencing non-existent columns in SQL will result in an error code `COLUMN_NOT_EXIST(616)`. -- Data type mismatches between the insertion data and the column's data type will result in an error code `DATA_TYPE_MISMATCH(614)`. - - -## 2. Data Updates - -### 2.1 Syntax - -```SQL -UPDATE SET updateAssignment (',' updateAssignment)* (WHERE where=booleanExpression)? - -updateAssignment - : identifier EQ expression - ; -``` - -**Note:** - -- Updates are allowed only on `ATTRIBUTE` columns. -- `WHERE` conditions: - - Can only include `TAG` and `ATTRIBUTE` columns; `FIELD` and `TIME` columns are not allowed. - - Aggregation functions are not supported. -- The result of the `SET` assignment expression must be a `string` type and follow the same constraints as the `WHERE` clause. - -**Example**: - -```SQL -update table1 set b = a where substring(a, 1, 1) like '%' -``` diff --git a/src/UserGuide/latest-Table/Basic-Concept/Write-Updata-Data_apache.md b/src/UserGuide/latest-Table/Basic-Concept/Write-Updata-Data_apache.md new file mode 100644 index 000000000..af0f1cf90 --- /dev/null +++ b/src/UserGuide/latest-Table/Basic-Concept/Write-Updata-Data_apache.md @@ -0,0 +1,332 @@ + + +# Write & Update Data + +## 1. Data Insertion + +### 1.1 Syntax + +In IoTDB, data insertion follows the general syntax: + +```SQL +INSERT INTO [(COLUMN_NAME[, COLUMN_NAME]*)]? VALUES (COLUMN_VALUE[, COLUMN_VALUE]*) +``` + +**Basic Constraints**: + +1. Tables cannot be automatically created using `INSERT` statements. +2. Columns not specified in the `INSERT` statement will automatically be filled with `null`. +3. If no timestamp is provided, the system will use the current time (`now()`). +4. If a column value does not exist for the identified device, the insertion will overwrite any existing `null` values with the new data. +5. If a column value already exists for the identified device, a new insertion will update the column with the new value. +6. Writing duplicate timestamps will update the values in the columns corresponding to the original timestamps. +7. When an INSERT statement does not specify column names (e.g., INSERT INTO table VALUES (...)), the values in VALUES must strictly follow the physical order of columns in the table (this order can be checked via the DESC table command). + +Since attributes generally do not change over time, it is recommended to update attribute values using the `UPDATE` statement described below,Please refer to the following [Data Update](#2-data-updates). + +
+ +
+ + +### 1.2 Automatically Create Tables via Session Insertion + +When performing data writing through Session, IoTDB supports schema-less writing: there is no need to manually create tables beforehand. The system automatically constructs the table structure based on the information in the write request, and then directly executes the data writing operation. + +**Example:** + +```Java +try (ITableSession session = + new TableSessionBuilder() + .nodeUrls(Collections.singletonList("127.0.0.1:6667")) + .username("root") + .password("root") + .build()) { + + session.executeNonQueryStatement("CREATE DATABASE db1"); + session.executeNonQueryStatement("use db1"); + + // Insert data without manually creating the table + List columnNameList = + Arrays.asList("region_id", "plant_id", "device_id", "model", "temperature", "humidity"); + List dataTypeList = + Arrays.asList( + TSDataType.STRING, + TSDataType.STRING, + TSDataType.STRING, + TSDataType.STRING, + TSDataType.FLOAT, + TSDataType.DOUBLE); + List columnTypeList = + new ArrayList<>( + Arrays.asList( + ColumnCategory.TAG, + ColumnCategory.TAG, + ColumnCategory.TAG, + ColumnCategory.ATTRIBUTE, + ColumnCategory.FIELD, + ColumnCategory.FIELD)); + Tablet tablet = new Tablet("table1", columnNameList, dataTypeList, columnTypeList, 100); + for (long timestamp = 0; timestamp < 100; timestamp++) { + int rowIndex = tablet.getRowSize(); + tablet.addTimestamp(rowIndex, timestamp); + tablet.addValue("region_id", rowIndex, "1"); + tablet.addValue("plant_id", rowIndex, "5"); + tablet.addValue("device_id", rowIndex, "3"); + tablet.addValue("model", rowIndex, "A"); + tablet.addValue("temperature", rowIndex, 37.6F); + tablet.addValue("humidity", rowIndex, 111.1); + if (tablet.getRowSize() == tablet.getMaxRowNumber()) { + session.insert(tablet); + tablet.reset(); + } + } + if (tablet.getRowSize() != 0) { + session.insert(tablet); + tablet.reset(); + } +} +``` + +After execution, you can verify the table creation using the following command: + +```SQL +IoTDB> desc table1 ++-----------+---------+-----------+ +| ColumnName| DataType| Category| ++-----------+---------+-----------+ +| time|TIMESTAMP| TIME| +| region_id| STRING| TAG| +| plant_id| STRING| TAG| +| device_id| STRING| TAG| +| model| STRING| ATTRIBUTE| +|temperature| FLOAT| FIELD| +| humidity| DOUBLE| FIELD| ++-----------+---------+-----------+ +``` + +### 1.3 Specified Column Insertion + +It is possible to insert data for specific columns. Columns not specified will remain `null`. + +**Example:** + +```SQL +insert into table1("region", "plant_id", "device_id", Time, "temperature", "displacement") values ('Hamburg', '3001', '3', 4, 90.0, 1200.0) + + +insert into table1("region", "plant_id", "device_id", Time, "temperature") values ('Hamburg, '3001', '3', 5, 90.0) +``` + +### 1.4 Null Value Insertion + +You can explicitly set `null` values for tag columns, attribute columns, and field columns. + +**Example**: + +Equivalent to the above partial column insertion. + +```SQL +# Equivalent to the example above +insert into table1("region", "plant_id", "device_id", "model", "maintenance_cycle", Time, "temperature", "displacement") values ('Hamburg', '3001', '3', null, null, 4, 90.0, 1200.0) + +insert into table1("region", "plant_id", "device_id", "model", "maintenance_cycle", Time, "temperature", "displacement") values ('Hamburg', '3001', '3', null, null, 5, 90.0, null) +``` + +If no tag columns are included, the system will automatically create a device with all tag column values set to `null`. + +> **Note:** This operation will not only automatically populate existing tag columns in the table with `null` values but will also populate any newly added tag columns with `null` values in the future. + +### 1.5 Multi-Row Insertion + +IoTDB supports inserting multiple rows of data in a single statement to improve efficiency. + +**Example**: + +```SQL +insert into table1 +values +(4, 'Frankfurt', '3001', '3', '1', '10', 90.0, 1200.0) +(5, 'Frankfurt', '3001', '3', '1', '10', 90.0, 1200.0) + + +insert into table1 +("region", "plant_id", "device_id", Time, "temperature", "displacement") +values +('Frankfurt', '3001', '3', 4, 90.0, 1200.0) +('Frankfurt', '3001', '3', 5, 90.0, 1200.0) +``` + +#### Notes + +- Referencing non-existent columns in SQL will result in an error code `COLUMN_NOT_EXIST(616)`. +- Data type mismatches between the insertion data and the column's data type will result in an error code `DATA_TYPE_MISMATCH(614)`. + + +### 1.6 Query Write-back + +The IoTDB table model supports the **append-only query write-back** feature, implemented via the `INSERT INTO QUERY` statement. This feature allows writing the results of a query into an **existing** table. + +> ​**Note**​: This feature is available starting from version V2.0.6. + +#### 1.6.1 Syntax Definition + +sql + +```sql +INSERT INTO table_name [ ( column [, ... ] ) ] query +``` + +The **query** component supports three formats, which are illustrated with examples below. + +Using the [sample data](../Reference/Sample-Data.md) as the data source, first create the target table: + +sql + +```sql +IoTDB:database1> CREATE TABLE target_table ( time TIMESTAMP TIME, region STRING TAG, device_id STRING TAG, temperature FLOAT FIELD ); +Msg: The statement is executed successfully. +``` + +1. Write-back via Standard Query Statement + +The `query` part is a direct `select ... from ...` query. + +​**Example**​: Use a standard query statement to write the `time`, `region`, `device_id`, and `temperature` data of the Beijing region from `table1` into `target_table`. + +sql + +```sql +IoTDB:database1> insert into target_table select time,region,device_id,temperature from table1 where region = 'Beijing' +Msg: The statement is executed successfully. +IoTDB:database1> select * from target_table where region='Beijing' ++-----------------------------+--------+-----------+-------------+ +| time| region| device_id| temperature| ++-----------------------------+--------+-----------+-------------+ +|2024-11-26T13:37:00.000+08:00| Beijing| 100| 90.0| +|2024-11-26T13:38:00.000+08:00| Beijing| 100| 90.0| +|2024-11-27T16:38:00.000+08:00| Beijing| 101| null| +|2024-11-27T16:39:00.000+08:00| Beijing| 101| 85.0| +|2024-11-27T16:40:00.000+08:00| Beijing| 101| 85.0| +|2024-11-27T16:41:00.000+08:00| Beijing| 101| 85.0| +|2024-11-27T16:42:00.000+08:00| Beijing| 101| null| +|2024-11-27T16:43:00.000+08:00| Beijing| 101| null| +|2024-11-27T16:44:00.000+08:00| Beijing| 101| null| ++-----------------------------+--------+-----------+-------------+ +Total line number = 9 +It costs 0.029s +``` + +2. Write-back via Table Reference Query + +The `query` part uses the table reference syntax `table source_table`. + +​**Example**​: Use a table reference query to write data from `table3` into `target_table`. + +sql + +```sql +IoTDB:database1> insert into target_table(time,device_id,temperature) table table3 +Msg: The statement is executed successfully. +IoTDB:database1> select * from target_table where region is null ++-----------------------------+------+-----------+-------------+ +| time|region| device_id| temperature| ++-----------------------------+------+-----------+-------------+ +|2025-05-13T00:00:00.001+08:00| null| d1| 90.0| +|2025-05-13T00:00:01.002+08:00| null| d1| 85.0| +|2025-05-13T00:00:02.101+08:00| null| d1| 85.0| +|2025-05-13T00:00:03.201+08:00| null| d1| null| +|2025-05-13T00:00:04.105+08:00| null| d1| 90.0| +|2025-05-13T00:00:05.023+08:00| null| d1| 85.0| +|2025-05-13T00:00:06.129+08:00| null| d1| 90.0| ++-----------------------------+------+-----------+-------------+ +Total line number = 7 +It costs 0.015s +``` + +3. Write-back via Subquery + +The `query` part is a parenthesized subquery. + +​**Example**​: Use a subquery to write the `time`, `region`, `device_id`, and `temperature` data from `table1` whose timestamps match the records of the Shanghai region in `table2` into `target_table`. + +sql + +```sql +IoTDB:database1> insert into target_table (select t1.time, t1.region as region, t1.device_id as device_id, t1.temperature as temperature from table1 t1 where t1.time in (select t2.time from table2 t2 where t2.region = 'Shanghai')) +Msg: The statement is executed successfully. +IoTDB:database1> select * from target_table where region = 'Shanghai' ++-----------------------------+---------+-----------+-------------+ +| time| region| device_id| temperature| ++-----------------------------+---------+-----------+-------------+ +|2024-11-28T08:00:00.000+08:00| Shanghai| 100| 85.0| +|2024-11-29T11:00:00.000+08:00| Shanghai| 100| null| ++-----------------------------+---------+-----------+-------------+ +Total line number = 2 +It costs 0.014s +``` + +#### 1.6.2 Notes + +* The source table in the `query` and the target table `table_name` are allowed to be the same table, e.g., `INSERT INTO testtb SELECT * FROM testtb`. +* The target table ​**must already exist**​; otherwise, the error message `550: Table 'xxx.xxx' does not exist` will be thrown. +* The number and data types of columns returned by the query **must exactly match** those of the target table. Type conversion between compatible types is not supported currently. A type mismatch will trigger the error message `701: Insert query has mismatched column types`. +* You can specify a subset of columns in the target table, provided the following rules are met: + * The timestamp column must be included; otherwise, the error message `701: time column can not be null` will be thrown. + * At least one **FIELD** column must be included; otherwise, the error message `701: No Field column present` will be thrown. + * **TAG** columns are optional. + * The number of specified columns can be less than that of the target table. Missing columns will be automatically filled with `NULL` values. +* For Java applications, the `INSERT INTO QUERY` statement can be executed using the [executeNonQueryStatement](../API/Programming-Java-Native-API_apache.md#_3-1-itablesession-interface) method. +* For REST API access, the `INSERT INTO QUERY` statement can be executed via the [/rest/table/v1/nonQuery](../API/RestAPI-V1.md#_3-3-non-query-interface) endpoint. +* `INSERT INTO QUERY` does **not** support the `EXPLAIN` and `EXPLAIN ANALYZE` commands. +* To execute the query write-back statement successfully, users must have the following permissions: + * The `SELECT` permission on the source tables involved in the query. + * The `WRITE` permission on the target table. + * For more details about user permissions, refer to [Authority Management](../User-Manual/Authority-Management_apache.md). + + + +## 2. Data Updates + +### 2.1 Syntax + +```SQL +UPDATE SET updateAssignment (',' updateAssignment)* (WHERE where=booleanExpression)? + +updateAssignment + : identifier EQ expression + ; +``` + +**Note:** + +- Updates are allowed only on `ATTRIBUTE` columns. +- `WHERE` conditions: + - Can only include `TAG` and `ATTRIBUTE` columns; `FIELD` and `TIME` columns are not allowed. + - Aggregation functions are not supported. +- The result of the `SET` assignment expression must be a `string` type and follow the same constraints as the `WHERE` clause. + +**Example**: + +```SQL +update table1 set b = a where substring(a, 1, 1) like '%' +``` diff --git a/src/UserGuide/latest-Table/Basic-Concept/Write-Updata-Data_timecho.md b/src/UserGuide/latest-Table/Basic-Concept/Write-Updata-Data_timecho.md new file mode 100644 index 000000000..e1c03bd46 --- /dev/null +++ b/src/UserGuide/latest-Table/Basic-Concept/Write-Updata-Data_timecho.md @@ -0,0 +1,332 @@ + + +# Write & Update Data + +## 1. Data Insertion + +### 1.1 Syntax + +In IoTDB, data insertion follows the general syntax: + +```SQL +INSERT INTO [(COLUMN_NAME[, COLUMN_NAME]*)]? VALUES (COLUMN_VALUE[, COLUMN_VALUE]*) +``` + +**Basic Constraints**: + +1. Tables cannot be automatically created using `INSERT` statements. +2. Columns not specified in the `INSERT` statement will automatically be filled with `null`. +3. If no timestamp is provided, the system will use the current time (`now()`). +4. If a column value does not exist for the identified device, the insertion will overwrite any existing `null` values with the new data. +5. If a column value already exists for the identified device, a new insertion will update the column with the new value. +6. Writing duplicate timestamps will update the values in the columns corresponding to the original timestamps. +7. When an INSERT statement does not specify column names (e.g., INSERT INTO table VALUES (...)), the values in VALUES must strictly follow the physical order of columns in the table (this order can be checked via the DESC table command). + +Since attributes generally do not change over time, it is recommended to update attribute values using the `UPDATE` statement described below,Please refer to the following [Data Update](#2-data-updates). + +
+ +
+ + +### 1.2 Automatically Create Tables via Session Insertion + +When performing data writing through Session, IoTDB supports schema-less writing: there is no need to manually create tables beforehand. The system automatically constructs the table structure based on the information in the write request, and then directly executes the data writing operation. + +**Example:** + +```Java +try (ITableSession session = + new TableSessionBuilder() + .nodeUrls(Collections.singletonList("127.0.0.1:6667")) + .username("root") + .password("root") + .build()) { + + session.executeNonQueryStatement("CREATE DATABASE db1"); + session.executeNonQueryStatement("use db1"); + + // Insert data without manually creating the table + List columnNameList = + Arrays.asList("region_id", "plant_id", "device_id", "model", "temperature", "humidity"); + List dataTypeList = + Arrays.asList( + TSDataType.STRING, + TSDataType.STRING, + TSDataType.STRING, + TSDataType.STRING, + TSDataType.FLOAT, + TSDataType.DOUBLE); + List columnTypeList = + new ArrayList<>( + Arrays.asList( + ColumnCategory.TAG, + ColumnCategory.TAG, + ColumnCategory.TAG, + ColumnCategory.ATTRIBUTE, + ColumnCategory.FIELD, + ColumnCategory.FIELD)); + Tablet tablet = new Tablet("table1", columnNameList, dataTypeList, columnTypeList, 100); + for (long timestamp = 0; timestamp < 100; timestamp++) { + int rowIndex = tablet.getRowSize(); + tablet.addTimestamp(rowIndex, timestamp); + tablet.addValue("region_id", rowIndex, "1"); + tablet.addValue("plant_id", rowIndex, "5"); + tablet.addValue("device_id", rowIndex, "3"); + tablet.addValue("model", rowIndex, "A"); + tablet.addValue("temperature", rowIndex, 37.6F); + tablet.addValue("humidity", rowIndex, 111.1); + if (tablet.getRowSize() == tablet.getMaxRowNumber()) { + session.insert(tablet); + tablet.reset(); + } + } + if (tablet.getRowSize() != 0) { + session.insert(tablet); + tablet.reset(); + } +} +``` + +After execution, you can verify the table creation using the following command: + +```SQL +IoTDB> desc table1 ++-----------+---------+-----------+ +| ColumnName| DataType| Category| ++-----------+---------+-----------+ +| time|TIMESTAMP| TIME| +| region_id| STRING| TAG| +| plant_id| STRING| TAG| +| device_id| STRING| TAG| +| model| STRING| ATTRIBUTE| +|temperature| FLOAT| FIELD| +| humidity| DOUBLE| FIELD| ++-----------+---------+-----------+ +``` + +### 1.3 Specified Column Insertion + +It is possible to insert data for specific columns. Columns not specified will remain `null`. + +**Example:** + +```SQL +insert into table1("region", "plant_id", "device_id", Time, "temperature", "displacement") values ('Hamburg', '3001', '3', 4, 90.0, 1200.0) + + +insert into table1("region", "plant_id", "device_id", Time, "temperature") values ('Hamburg, '3001', '3', 5, 90.0) +``` + +### 1.4 Null Value Insertion + +You can explicitly set `null` values for tag columns, attribute columns, and field columns. + +**Example**: + +Equivalent to the above partial column insertion. + +```SQL +# Equivalent to the example above +insert into table1("region", "plant_id", "device_id", "model", "maintenance_cycle", Time, "temperature", "displacement") values ('Hamburg', '3001', '3', null, null, 4, 90.0, 1200.0) + +insert into table1("region", "plant_id", "device_id", "model", "maintenance_cycle", Time, "temperature", "displacement") values ('Hamburg', '3001', '3', null, null, 5, 90.0, null) +``` + +If no tag columns are included, the system will automatically create a device with all tag column values set to `null`. + +> **Note:** This operation will not only automatically populate existing tag columns in the table with `null` values but will also populate any newly added tag columns with `null` values in the future. + +### 1.5 Multi-Row Insertion + +IoTDB supports inserting multiple rows of data in a single statement to improve efficiency. + +**Example**: + +```SQL +insert into table1 +values +(4, 'Frankfurt', '3001', '3', '1', '10', 90.0, 1200.0) +(5, 'Frankfurt', '3001', '3', '1', '10', 90.0, 1200.0) + + +insert into table1 +("region", "plant_id", "device_id", Time, "temperature", "displacement") +values +('Frankfurt', '3001', '3', 4, 90.0, 1200.0) +('Frankfurt', '3001', '3', 5, 90.0, 1200.0) +``` + +#### Notes + +- Referencing non-existent columns in SQL will result in an error code `COLUMN_NOT_EXIST(616)`. +- Data type mismatches between the insertion data and the column's data type will result in an error code `DATA_TYPE_MISMATCH(614)`. + + +### 1.6 Query Write-back + +The IoTDB table model supports the **append-only query write-back** feature, implemented via the `INSERT INTO QUERY` statement. This feature allows writing the results of a query into an **existing** table. + +> ​**Note**​: This feature is available starting from version V2.0.6. + +#### 1.6.1 Syntax Definition + +sql + +```sql +INSERT INTO table_name [ ( column [, ... ] ) ] query +``` + +The **query** component supports three formats, which are illustrated with examples below. + +Using the [sample data](../Reference/Sample-Data.md) as the data source, first create the target table: + +sql + +```sql +IoTDB:database1> CREATE TABLE target_table ( time TIMESTAMP TIME, region STRING TAG, device_id STRING TAG, temperature FLOAT FIELD ); +Msg: The statement is executed successfully. +``` + +1. Write-back via Standard Query Statement + +The `query` part is a direct `select ... from ...` query. + +​**Example**​: Use a standard query statement to write the `time`, `region`, `device_id`, and `temperature` data of the Beijing region from `table1` into `target_table`. + +sql + +```sql +IoTDB:database1> insert into target_table select time,region,device_id,temperature from table1 where region = 'Beijing' +Msg: The statement is executed successfully. +IoTDB:database1> select * from target_table where region='Beijing' ++-----------------------------+--------+-----------+-------------+ +| time| region| device_id| temperature| ++-----------------------------+--------+-----------+-------------+ +|2024-11-26T13:37:00.000+08:00| Beijing| 100| 90.0| +|2024-11-26T13:38:00.000+08:00| Beijing| 100| 90.0| +|2024-11-27T16:38:00.000+08:00| Beijing| 101| null| +|2024-11-27T16:39:00.000+08:00| Beijing| 101| 85.0| +|2024-11-27T16:40:00.000+08:00| Beijing| 101| 85.0| +|2024-11-27T16:41:00.000+08:00| Beijing| 101| 85.0| +|2024-11-27T16:42:00.000+08:00| Beijing| 101| null| +|2024-11-27T16:43:00.000+08:00| Beijing| 101| null| +|2024-11-27T16:44:00.000+08:00| Beijing| 101| null| ++-----------------------------+--------+-----------+-------------+ +Total line number = 9 +It costs 0.029s +``` + +2. Write-back via Table Reference Query + +The `query` part uses the table reference syntax `table source_table`. + +​**Example**​: Use a table reference query to write data from `table3` into `target_table`. + +sql + +```sql +IoTDB:database1> insert into target_table(time,device_id,temperature) table table3 +Msg: The statement is executed successfully. +IoTDB:database1> select * from target_table where region is null ++-----------------------------+------+-----------+-------------+ +| time|region| device_id| temperature| ++-----------------------------+------+-----------+-------------+ +|2025-05-13T00:00:00.001+08:00| null| d1| 90.0| +|2025-05-13T00:00:01.002+08:00| null| d1| 85.0| +|2025-05-13T00:00:02.101+08:00| null| d1| 85.0| +|2025-05-13T00:00:03.201+08:00| null| d1| null| +|2025-05-13T00:00:04.105+08:00| null| d1| 90.0| +|2025-05-13T00:00:05.023+08:00| null| d1| 85.0| +|2025-05-13T00:00:06.129+08:00| null| d1| 90.0| ++-----------------------------+------+-----------+-------------+ +Total line number = 7 +It costs 0.015s +``` + +3. Write-back via Subquery + +The `query` part is a parenthesized subquery. + +​**Example**​: Use a subquery to write the `time`, `region`, `device_id`, and `temperature` data from `table1` whose timestamps match the records of the Shanghai region in `table2` into `target_table`. + +sql + +```sql +IoTDB:database1> insert into target_table (select t1.time, t1.region as region, t1.device_id as device_id, t1.temperature as temperature from table1 t1 where t1.time in (select t2.time from table2 t2 where t2.region = 'Shanghai')) +Msg: The statement is executed successfully. +IoTDB:database1> select * from target_table where region = 'Shanghai' ++-----------------------------+---------+-----------+-------------+ +| time| region| device_id| temperature| ++-----------------------------+---------+-----------+-------------+ +|2024-11-28T08:00:00.000+08:00| Shanghai| 100| 85.0| +|2024-11-29T11:00:00.000+08:00| Shanghai| 100| null| ++-----------------------------+---------+-----------+-------------+ +Total line number = 2 +It costs 0.014s +``` + +#### 1.6.2 Notes + +* The source table in the `query` and the target table `table_name` are allowed to be the same table, e.g., `INSERT INTO testtb SELECT * FROM testtb`. +* The target table ​**must already exist**​; otherwise, the error message `550: Table 'xxx.xxx' does not exist` will be thrown. +* The number and data types of columns returned by the query **must exactly match** those of the target table. Type conversion between compatible types is not supported currently. A type mismatch will trigger the error message `701: Insert query has mismatched column types`. +* You can specify a subset of columns in the target table, provided the following rules are met: + * The timestamp column must be included; otherwise, the error message `701: time column can not be null` will be thrown. + * At least one **FIELD** column must be included; otherwise, the error message `701: No Field column present` will be thrown. + * **TAG** columns are optional. + * The number of specified columns can be less than that of the target table. Missing columns will be automatically filled with `NULL` values. +* For Java applications, the `INSERT INTO QUERY` statement can be executed using the [executeNonQueryStatement](../API/Programming-Java-Native-API_timecho.md#_3-1-itablesession-interface) method. +* For REST API access, the `INSERT INTO QUERY` statement can be executed via the [/rest/table/v1/nonQuery](../API/RestAPI-V1.md#_3-3-non-query-interface) endpoint. +* `INSERT INTO QUERY` does **not** support the `EXPLAIN` and `EXPLAIN ANALYZE` commands. +* To execute the query write-back statement successfully, users must have the following permissions: + * The `SELECT` permission on the source tables involved in the query. + * The `WRITE` permission on the target table. + * For more details about user permissions, refer to [Authority Management](../User-Manual/Authority-Management_timecho.md). + + + +## 2. Data Updates + +### 2.1 Syntax + +```SQL +UPDATE SET updateAssignment (',' updateAssignment)* (WHERE where=booleanExpression)? + +updateAssignment + : identifier EQ expression + ; +``` + +**Note:** + +- Updates are allowed only on `ATTRIBUTE` columns. +- `WHERE` conditions: + - Can only include `TAG` and `ATTRIBUTE` columns; `FIELD` and `TIME` columns are not allowed. + - Aggregation functions are not supported. +- The result of the `SET` assignment expression must be a `string` type and follow the same constraints as the `WHERE` clause. + +**Example**: + +```SQL +update table1 set b = a where substring(a, 1, 1) like '%' +``` diff --git a/src/UserGuide/latest-Table/QuickStart/QuickStart_apache.md b/src/UserGuide/latest-Table/QuickStart/QuickStart_apache.md index b7b7fcf55..612bfbe1a 100644 --- a/src/UserGuide/latest-Table/QuickStart/QuickStart_apache.md +++ b/src/UserGuide/latest-Table/QuickStart/QuickStart_apache.md @@ -53,7 +53,7 @@ This guide will assist you in quickly installing and deploying IoTDB. You can qu - Introduction to Tables: [Table Management](../Basic-Concept/Table-Management.md) -2. Data Insertion & Updates: IoTDB provides multiple methods for inserting real-time data. For basic data insertion and updating operations, please see [Write&Updata Data](../Basic-Concept/Write-Updata-Data.md) +2. Data Insertion & Updates: IoTDB provides multiple methods for inserting real-time data, and supports data write-back. For basic data insertion and updating operations, please see [Write&Updata Data](../Basic-Concept/Write-Updata-Data_apache.md) 3. Data Querying: IoTDB offers a rich set of data querying capabilities. For a basic introduction to data querying, please see [Query Data](../Basic-Concept/Query-Data_apache.md) diff --git a/src/UserGuide/latest-Table/QuickStart/QuickStart_timecho.md b/src/UserGuide/latest-Table/QuickStart/QuickStart_timecho.md index a91a86dcb..f976f1da2 100644 --- a/src/UserGuide/latest-Table/QuickStart/QuickStart_timecho.md +++ b/src/UserGuide/latest-Table/QuickStart/QuickStart_timecho.md @@ -60,7 +60,7 @@ This guide will assist you in quickly installing and deploying IoTDB. You can qu - Introduction to Tables: [Table Management](../Basic-Concept/Table-Management.md) -2. Data Insertion & Updates: IoTDB provides multiple methods for inserting real-time data. For basic data insertion and updating operations, please see [Write&Updata Data](../Basic-Concept/Write-Updata-Data.md) +2. Data Insertion & Updates: IoTDB provides multiple methods for inserting real-time data, and supports data write-back. For basic data insertion and updating operations, please see [Write&Updata Data](../Basic-Concept/Write-Updata-Data_timecho.md) 3. Data Querying: IoTDB offers a rich set of data querying capabilities. For a basic introduction to data querying, please see [Query Data](../Basic-Concept/Query-Data_timecho.md). It includes [Row Pattern Recognition](../SQL-Manual/Row-Pattern-Recognition.md) suitable for business scenarios such as identifying specific patterns in time-series data and detecting specific events, as well as [Window Functions](../SQL-Manual/Featured-Functions_timecho.md#_4-Window-Functions) and other featured functions often used in data analysis scenarios. diff --git a/src/UserGuide/latest-Table/SQL-Manual/SQL-Data-Addition-Deletion.md b/src/UserGuide/latest-Table/SQL-Manual/SQL-Data-Addition-Deletion.md index 475eafd52..84dc187da 100644 --- a/src/UserGuide/latest-Table/SQL-Manual/SQL-Data-Addition-Deletion.md +++ b/src/UserGuide/latest-Table/SQL-Manual/SQL-Data-Addition-Deletion.md @@ -1,3 +1,6 @@ +--- +redirectTo: SQL-Data-Addition-Deletion_apache.html +--- - -# Data Addition & Deletion - -## 1. Data Insertion - -**Syntax:** - -```SQL -INSERT INTO [(COLUMN_NAME[, COLUMN_NAME]*)]? VALUES (COLUMN_VALUE[, COLUMN_VALUE]*) -``` - -[Detailed syntax reference](../Basic-Concept/Write-Updata-Data.md#_1-1-syntax) - -**Example 1: Specified Columns Insertion** - -```SQL -INSERT INTO table1("Region", "PlantID", "DeviceID", Time, "Temperature", "Displacement") -VALUES ('Hunan', '3001', '3', 4, 90.0, 1200.0); - -INSERT INTO table1("Region", "PlantID", "DeviceID", Time, "Temperature") -VALUES ('Hunan', '3001', '3', 5, 90.0); -``` - -**Example 2: NULL Value Insertion** - -```SQL --- Equivalent to partial insertion with NULL values -INSERT INTO table1("Region", "PlantID", "DeviceID", "Model", "MaintenanceCycle", Time, "Temperature", "Displacement") -VALUES ('Hunan', '3001', '3', NULL, NULL, 4, 90.0, 1200.0); - -INSERT INTO table1("Region", "PlantID", "DeviceID", "Model", "MaintenanceCycle", Time, "Temperature", "Displacement") -VALUES ('Hunan', '3001', '3', NULL, NULL, 5, 90.0, NULL); -``` - -**Example 3: Multi-row Insertion** - -```SQL -INSERT INTO table1 VALUES -(4, 'Beijing', '3001', '3', '1', '10', 90.0, 1200.0), -(5, 'Beijing', '3001', '3', '1', '10', 90.0, 1200.0); - -INSERT INTO table1("Region", "PlantID", "DeviceID", Time, "Temperature", "Displacement") -VALUES -('Beijing', '3001', '3', 4, 90.0, 1200.0), -('Beijing', '3001', '3', 5, 90.0, 1200.0); -``` - -## 2. Data Update - -**Syntax:** - -```SQL -UPDATE SET updateAssignment (',' updateAssignment)* (WHERE where=booleanExpression)? - -updateAssignment - : identifier EQ expression - ; -``` - -[Detailed syntax reference](../Basic-Concept/Write-Updata-Data.md#_2-1-syntax) - -**Example:** - -```SQL -update table1 set b = a where substring(a, 1, 1) like '%' -``` - -## 3. Data Deletion - -**Syntax:** - -```SQL -DELETE FROM [WHERE_CLAUSE]? - -WHERE_CLAUSE: - WHERE DELETE_CONDITION - -DELETE_CONDITION: - SINGLE_CONDITION - | DELETE_CONDITION AND DELETE_CONDITION - | DELETE_CONDITION OR DELETE_CONDITION - -SINGLE_CODITION: - TIME_CONDITION | ID_CONDITION - -TIME_CONDITION: - time TIME_OPERATOR LONG_LITERAL - -TIME_OPERATOR: - < | > | <= | >= | = - -ID_CONDITION: - identifier = STRING_LITERAL -``` - -**Example 1: Full Table Deletion** - -```SQL -DELETE FROM table1 -``` - -**Example 2: Time-range Deletion** - -```SQL --- Single time range -DELETE FROM table1 WHERE time <= 2024-11-29 00:00:00 - --- Multiple time ranges -DELETE FROM table1 WHERE time >= 2024-11-27 00:00:00 and time <= 2024-11-29 00:00:00 -``` - -**Example 3: Device-Specific Deletion** - -```SQL --- Delete data for specific device -DELETE FROM table1 -WHERE device_id='101' AND model_id = 'B'; - --- Delete data for device within time range -DELETE FROM table1 -WHERE time >= '2024-11-27 16:39:00' AND time <= '2024-11-29 16:42:00' - AND device_id='101' AND model_id = 'B'; - --- Delete data for specific device model -DELETE FROM table1 WHERE model_id = 'B'; -``` - -## 4. Device Deletion - -**Syntax:** - -```SQL -DELETE DEVICES FROM tableName=qualifiedName (WHERE booleanExpression)? -``` - -**Example: Delete specified device and all associated data** - -```SQL -DELETE DEVICES FROM table1 WHERE device_id = '101' -``` diff --git a/src/UserGuide/latest-Table/SQL-Manual/SQL-Data-Addition-Deletion_apache.md b/src/UserGuide/latest-Table/SQL-Manual/SQL-Data-Addition-Deletion_apache.md new file mode 100644 index 000000000..54046c3c0 --- /dev/null +++ b/src/UserGuide/latest-Table/SQL-Manual/SQL-Data-Addition-Deletion_apache.md @@ -0,0 +1,171 @@ + + +# Data Addition & Deletion + +## 1. Data Insertion + +**Syntax:** + +```SQL +INSERT INTO [(COLUMN_NAME[, COLUMN_NAME]*)]? VALUES (COLUMN_VALUE[, COLUMN_VALUE]*) +``` + +[Detailed syntax reference](../Basic-Concept/Write-Updata-Data_apache.md#_1-1-syntax) + +**Example 1: Specified Columns Insertion** + +```SQL +INSERT INTO table1("Region", "PlantID", "DeviceID", Time, "Temperature", "Displacement") +VALUES ('Hunan', '3001', '3', 4, 90.0, 1200.0); + +INSERT INTO table1("Region", "PlantID", "DeviceID", Time, "Temperature") +VALUES ('Hunan', '3001', '3', 5, 90.0); +``` + +**Example 2: NULL Value Insertion** + +```SQL +-- Equivalent to partial insertion with NULL values +INSERT INTO table1("Region", "PlantID", "DeviceID", "Model", "MaintenanceCycle", Time, "Temperature", "Displacement") +VALUES ('Hunan', '3001', '3', NULL, NULL, 4, 90.0, 1200.0); + +INSERT INTO table1("Region", "PlantID", "DeviceID", "Model", "MaintenanceCycle", Time, "Temperature", "Displacement") +VALUES ('Hunan', '3001', '3', NULL, NULL, 5, 90.0, NULL); +``` + +**Example 3: Multi-row Insertion** + +```SQL +INSERT INTO table1 VALUES +(4, 'Beijing', '3001', '3', '1', '10', 90.0, 1200.0), +(5, 'Beijing', '3001', '3', '1', '10', 90.0, 1200.0); + +INSERT INTO table1("Region", "PlantID", "DeviceID", Time, "Temperature", "Displacement") +VALUES +('Beijing', '3001', '3', 4, 90.0, 1200.0), +('Beijing', '3001', '3', 5, 90.0, 1200.0); +``` + +**Example 4: Query Write-back** + +```SQL +insert into target_table select time,region,device_id,temperature from table1 where region = 'bj'; + +insert into target_table(time,device_id,temperature) table table3; + +insert into target_table (select t1.time, t1.region as region, t1.device_id as device_id, t1.temperature as temperature from table1 t1 where t1.time in (select t2.time from table2 t2 where t2.region = 'shh')); +``` + + +## 2. Data Update + +**Syntax:** + +```SQL +UPDATE SET updateAssignment (',' updateAssignment)* (WHERE where=booleanExpression)? + +updateAssignment + : identifier EQ expression + ; +``` + +[Detailed syntax reference](../Basic-Concept/Write-Updata-Data_apache.md#_2-1-syntax) + +**Example:** + +```SQL +update table1 set b = a where substring(a, 1, 1) like '%' +``` + +## 3. Data Deletion + +**Syntax:** + +```SQL +DELETE FROM [WHERE_CLAUSE]? + +WHERE_CLAUSE: + WHERE DELETE_CONDITION + +DELETE_CONDITION: + SINGLE_CONDITION + | DELETE_CONDITION AND DELETE_CONDITION + | DELETE_CONDITION OR DELETE_CONDITION + +SINGLE_CODITION: + TIME_CONDITION | ID_CONDITION + +TIME_CONDITION: + time TIME_OPERATOR LONG_LITERAL + +TIME_OPERATOR: + < | > | <= | >= | = + +ID_CONDITION: + identifier = STRING_LITERAL +``` + +**Example 1: Full Table Deletion** + +```SQL +DELETE FROM table1 +``` + +**Example 2: Time-range Deletion** + +```SQL +-- Single time range +DELETE FROM table1 WHERE time <= 2024-11-29 00:00:00 + +-- Multiple time ranges +DELETE FROM table1 WHERE time >= 2024-11-27 00:00:00 and time <= 2024-11-29 00:00:00 +``` + +**Example 3: Device-Specific Deletion** + +```SQL +-- Delete data for specific device +DELETE FROM table1 +WHERE device_id='101' AND model_id = 'B'; + +-- Delete data for device within time range +DELETE FROM table1 +WHERE time >= '2024-11-27 16:39:00' AND time <= '2024-11-29 16:42:00' + AND device_id='101' AND model_id = 'B'; + +-- Delete data for specific device model +DELETE FROM table1 WHERE model_id = 'B'; +``` + +## 4. Device Deletion + +**Syntax:** + +```SQL +DELETE DEVICES FROM tableName=qualifiedName (WHERE booleanExpression)? +``` + +**Example: Delete specified device and all associated data** + +```SQL +DELETE DEVICES FROM table1 WHERE device_id = '101' +``` diff --git a/src/UserGuide/latest-Table/SQL-Manual/SQL-Data-Addition-Deletion_timecho.md b/src/UserGuide/latest-Table/SQL-Manual/SQL-Data-Addition-Deletion_timecho.md new file mode 100644 index 000000000..26edcaba3 --- /dev/null +++ b/src/UserGuide/latest-Table/SQL-Manual/SQL-Data-Addition-Deletion_timecho.md @@ -0,0 +1,171 @@ + + +# Data Addition & Deletion + +## 1. Data Insertion + +**Syntax:** + +```SQL +INSERT INTO [(COLUMN_NAME[, COLUMN_NAME]*)]? VALUES (COLUMN_VALUE[, COLUMN_VALUE]*) +``` + +[Detailed syntax reference](../Basic-Concept/Write-Updata-Data_timecho.md#_1-1-syntax) + +**Example 1: Specified Columns Insertion** + +```SQL +INSERT INTO table1("Region", "PlantID", "DeviceID", Time, "Temperature", "Displacement") +VALUES ('Hunan', '3001', '3', 4, 90.0, 1200.0); + +INSERT INTO table1("Region", "PlantID", "DeviceID", Time, "Temperature") +VALUES ('Hunan', '3001', '3', 5, 90.0); +``` + +**Example 2: NULL Value Insertion** + +```SQL +-- Equivalent to partial insertion with NULL values +INSERT INTO table1("Region", "PlantID", "DeviceID", "Model", "MaintenanceCycle", Time, "Temperature", "Displacement") +VALUES ('Hunan', '3001', '3', NULL, NULL, 4, 90.0, 1200.0); + +INSERT INTO table1("Region", "PlantID", "DeviceID", "Model", "MaintenanceCycle", Time, "Temperature", "Displacement") +VALUES ('Hunan', '3001', '3', NULL, NULL, 5, 90.0, NULL); +``` + +**Example 3: Multi-row Insertion** + +```SQL +INSERT INTO table1 VALUES +(4, 'Beijing', '3001', '3', '1', '10', 90.0, 1200.0), +(5, 'Beijing', '3001', '3', '1', '10', 90.0, 1200.0); + +INSERT INTO table1("Region", "PlantID", "DeviceID", Time, "Temperature", "Displacement") +VALUES +('Beijing', '3001', '3', 4, 90.0, 1200.0), +('Beijing', '3001', '3', 5, 90.0, 1200.0); +``` + +**Example 4: Query Write-back** + +```SQL +insert into target_table select time,region,device_id,temperature from table1 where region = 'bj'; + +insert into target_table(time,device_id,temperature) table table3; + +insert into target_table (select t1.time, t1.region as region, t1.device_id as device_id, t1.temperature as temperature from table1 t1 where t1.time in (select t2.time from table2 t2 where t2.region = 'shh')); +``` + + +## 2. Data Update + +**Syntax:** + +```SQL +UPDATE SET updateAssignment (',' updateAssignment)* (WHERE where=booleanExpression)? + +updateAssignment + : identifier EQ expression + ; +``` + +[Detailed syntax reference](../Basic-Concept/Write-Updata-Data_timecho.md#_2-1-syntax) + +**Example:** + +```SQL +update table1 set b = a where substring(a, 1, 1) like '%' +``` + +## 3. Data Deletion + +**Syntax:** + +```SQL +DELETE FROM [WHERE_CLAUSE]? + +WHERE_CLAUSE: + WHERE DELETE_CONDITION + +DELETE_CONDITION: + SINGLE_CONDITION + | DELETE_CONDITION AND DELETE_CONDITION + | DELETE_CONDITION OR DELETE_CONDITION + +SINGLE_CODITION: + TIME_CONDITION | ID_CONDITION + +TIME_CONDITION: + time TIME_OPERATOR LONG_LITERAL + +TIME_OPERATOR: + < | > | <= | >= | = + +ID_CONDITION: + identifier = STRING_LITERAL +``` + +**Example 1: Full Table Deletion** + +```SQL +DELETE FROM table1 +``` + +**Example 2: Time-range Deletion** + +```SQL +-- Single time range +DELETE FROM table1 WHERE time <= 2024-11-29 00:00:00 + +-- Multiple time ranges +DELETE FROM table1 WHERE time >= 2024-11-27 00:00:00 and time <= 2024-11-29 00:00:00 +``` + +**Example 3: Device-Specific Deletion** + +```SQL +-- Delete data for specific device +DELETE FROM table1 +WHERE device_id='101' AND model_id = 'B'; + +-- Delete data for device within time range +DELETE FROM table1 +WHERE time >= '2024-11-27 16:39:00' AND time <= '2024-11-29 16:42:00' + AND device_id='101' AND model_id = 'B'; + +-- Delete data for specific device model +DELETE FROM table1 WHERE model_id = 'B'; +``` + +## 4. Device Deletion + +**Syntax:** + +```SQL +DELETE DEVICES FROM tableName=qualifiedName (WHERE booleanExpression)? +``` + +**Example: Delete specified device and all associated data** + +```SQL +DELETE DEVICES FROM table1 WHERE device_id = '101' +``` diff --git a/src/zh/UserGuide/Master/Table/Basic-Concept/Write-Updata-Data.md b/src/zh/UserGuide/Master/Table/Basic-Concept/Write-Updata-Data.md index 3b3d25d01..98f66ca6c 100644 --- a/src/zh/UserGuide/Master/Table/Basic-Concept/Write-Updata-Data.md +++ b/src/zh/UserGuide/Master/Table/Basic-Concept/Write-Updata-Data.md @@ -1,3 +1,6 @@ +--- +redirectTo: Write-Updata-Data_apache.html +--- - -# 写入&更新 - -## 1. 数据写入 - -### 1.1 语法 - -在 IoTDB 中,数据写入遵循以下通用语法: - -```SQL -INSERT INTO [(COLUMN_NAME[, COLUMN_NAME]*)]? VALUES (COLUMN_VALUE[, COLUMN_VALUE]*) -``` - -基本约束包括: - -1. 通过 insert 语句写入无法自动创建表。 -2. 未指定的标签列将自动填充为 `null`。 -3. 未包含时间戳,系统将使用当前时间 `now()` 进行填充。 -4. 若当前设备(由标识信息定位)不存在该属性列的值,执行写入操作将导致原有的空值(NULL)被写入的数据所替代。 -5. 若当前设备(由标识信息定位)已有属性列的值,再次写入相同的属性列时,系统将更新该属性列的值为新数据。 -6. 写入重复时间戳,原时间戳对应列的值会更新。 -7. 若 INSERT 语句未指定列名(如 INSERT INTO table VALUES (...)),则 VALUES中的值必须严格按表中列的物理顺序排列(顺序可通过 DESC table命令查看)。 - -由于属性一般并不随时间的变化而变化,因此推荐以 update 的方式单独更新属性值,参见下文 [数据更新](#数据更新)。 - -
- -
- - -### 1.2 通过 Session 写入自动创建表 - -在通过 Session 进行数据写入时,IoTDB 支持无模式写入:无需事先手动创建表,系统会根据写入请求中的信息自动构建表结构,之后直接执行数据写入操作。 - -**示例:** - -```Java -try (ITableSession session = - new TableSessionBuilder() - .nodeUrls(Collections.singletonList("127.0.0.1:6667")) - .username("root") - .password("root") - .build()) { - - session.executeNonQueryStatement("CREATE DATABASE db1"); - session.executeNonQueryStatement("use db1"); - - // 不创建表直接写入数据 - List columnNameList = - Arrays.asList("region_id", "plant_id", "device_id", "model", "temperature", "humidity"); - List dataTypeList = - Arrays.asList( - TSDataType.STRING, - TSDataType.STRING, - TSDataType.STRING, - TSDataType.STRING, - TSDataType.FLOAT, - TSDataType.DOUBLE); - List columnTypeList = - new ArrayList<>( - Arrays.asList( - ColumnCategory.TAG, - ColumnCategory.TAG, - ColumnCategory.TAG, - ColumnCategory.ATTRIBUTE, - ColumnCategory.FIELD, - ColumnCategory.FIELD)); - Tablet tablet = new Tablet("table1", columnNameList, dataTypeList, columnTypeList, 100); - for (long timestamp = 0; timestamp < 100; timestamp++) { - int rowIndex = tablet.getRowSize(); - tablet.addTimestamp(rowIndex, timestamp); - tablet.addValue("region_id", rowIndex, "1"); - tablet.addValue("plant_id", rowIndex, "5"); - tablet.addValue("device_id", rowIndex, "3"); - tablet.addValue("model", rowIndex, "A"); - tablet.addValue("temperature", rowIndex, 37.6F); - tablet.addValue("humidity", rowIndex, 111.1); - if (tablet.getRowSize() == tablet.getMaxRowNumber()) { - session.insert(tablet); - tablet.reset(); - } - } - if (tablet.getRowSize() != 0) { - session.insert(tablet); - tablet.reset(); - } -} -``` - -在代码执行完成后,可以通过下述语句确认表已成功创建,其中包含了时间列、标签列、属性列以及测点列等各类信息。 - -```SQL -IoTDB> desc table1 -+-----------+---------+-----------+ -| ColumnName| DataType| Category| -+-----------+---------+-----------+ -| time|TIMESTAMP| TIME| -| region_id| STRING| TAG| -| plant_id| STRING| TAG| -| device_id| STRING| TAG| -| model| STRING| ATTRIBUTE| -|temperature| FLOAT| FIELD| -| humidity| DOUBLE| FIELD| -+-----------+---------+-----------+ -``` - -### 1.3 指定列写入 - -在写入操作中,可以指定部分列,未指定的列将不会被写入任何内容(即设置为 `null`)。 - -**示例:** - -```SQL -insert into table1("地区", "厂号", "设备号", Time, "温度", "排量") values ('湖南', '3001', '3', 4, 90.0, 1200.0) - - -insert into table1("地区", "厂号", "设备号", Time, "温度") values ('湖南, '3001', '3', 5, 90.0) -``` - -### 1.4 空值写入 - -标签列、属性列和测点列可以指定空值(`null`),表示不写入任何内容。 - -**示例(与上述事例等价):** - -```SQL -# 上述部分列写入等价于如下的带空值写入 -insert into table1("地区", "厂号", "设备号", "型号", "维修周期", Time, "温度", "排量") values ('湖南', '3001', '3', null, null, 4, 90.0, 1200.0) - -insert into table1("地区", "厂号", "设备号", "型号", "维修周期", Time, "温度", "排量") values ('湖南', '3001', '3', null, null, 5, 90.0, null) -``` - -当向不包含任何标签列的表中写入数据时,系统将默认创建一个所有标签列值均为 null 的device。 - -> 注意,该操作不仅会自动为表中已有的标签列填充 null 值,而且对于未来新增的标签列,同样会自动填充 null。 - -### 1.5 多行写入 - -支持同时写入多行数据,提高数据写入效率。 - -**示例:** - -```SQL -insert into table1 -values -(4, '北京', '3001', '3', '1', '10', 90.0, 1200.0), -(5, '北京', '3001', '3', '1', '10', 90.0, 1200.0); - - -insert into table1 -("地区", "厂号", "设备号", Time, "温度", "排量") -values -('北京', '3001', '3', 4, 90.0, 1200.0), -('北京', '3001', '3', 5, 90.0, 1200.0); -``` - -#### 注意事项 - -- 如果在 SQL 语句中引用了表中不存在的列,IoTDB 将返回错误码 `COLUMN_NOT_EXIST(616)`。 -- 如果写入的数据类型与表中列的数据类型不一致,将报错 `DATA_TYPE_MISMATCH(507)`。 - -## 2. 数据更新 - -### 2.1 语法 - -```SQL -UPDATE SET updateAssignment (',' updateAssignment)* (WHERE where=booleanExpression)? - -updateAssignment - : identifier EQ expression - ; -``` - -1. `update`语句仅允许修改属性(ATTRIBUTE)列的值。 -2. `WHERE` 的规则: - - 范围仅限于标签列(TAG)和属性列(ATTRIBUTE),不允许涉及测点列(FIELD)和时间列(TIME)。 - - 不允许使用聚合函数 -3. 执行 SET 操作后,赋值表达式的结果应当是字符串类型,且其使用的限制应与 WHERE 子句中的表达式相同。 -4. 属性(ATTRIBUTE)列以及测点(FIELD)列的值也可通过`insert`语句来实现指定行的更新。 - -**示例:** - -```SQL -update table1 set b = a where substring(a, 1, 1) like '%' -``` \ No newline at end of file diff --git a/src/zh/UserGuide/Master/Table/Basic-Concept/Write-Updata-Data_apache.md b/src/zh/UserGuide/Master/Table/Basic-Concept/Write-Updata-Data_apache.md new file mode 100644 index 000000000..3f21c60d9 --- /dev/null +++ b/src/zh/UserGuide/Master/Table/Basic-Concept/Write-Updata-Data_apache.md @@ -0,0 +1,317 @@ + + +# 写入&更新 + +## 1. 数据写入 + +### 1.1 语法 + +在 IoTDB 中,数据写入遵循以下通用语法: + +```SQL +INSERT INTO [(COLUMN_NAME[, COLUMN_NAME]*)]? VALUES (COLUMN_VALUE[, COLUMN_VALUE]*) +``` + +基本约束包括: + +1. 通过 insert 语句写入无法自动创建表。 +2. 未指定的标签列将自动填充为 `null`。 +3. 未包含时间戳,系统将使用当前时间 `now()` 进行填充。 +4. 若当前设备(由标识信息定位)不存在该属性列的值,执行写入操作将导致原有的空值(NULL)被写入的数据所替代。 +5. 若当前设备(由标识信息定位)已有属性列的值,再次写入相同的属性列时,系统将更新该属性列的值为新数据。 +6. 写入重复时间戳,原时间戳对应列的值会更新。 +7. 若 INSERT 语句未指定列名(如 INSERT INTO table VALUES (...)),则 VALUES中的值必须严格按表中列的物理顺序排列(顺序可通过 DESC table命令查看)。 + +由于属性一般并不随时间的变化而变化,因此推荐以 update 的方式单独更新属性值,参见下文 [数据更新](#数据更新)。 + +
+ +
+ + +### 1.2 通过 Session 写入自动创建表 + +在通过 Session 进行数据写入时,IoTDB 支持无模式写入:无需事先手动创建表,系统会根据写入请求中的信息自动构建表结构,之后直接执行数据写入操作。 + +**示例:** + +```Java +try (ITableSession session = + new TableSessionBuilder() + .nodeUrls(Collections.singletonList("127.0.0.1:6667")) + .username("root") + .password("root") + .build()) { + + session.executeNonQueryStatement("CREATE DATABASE db1"); + session.executeNonQueryStatement("use db1"); + + // 不创建表直接写入数据 + List columnNameList = + Arrays.asList("region_id", "plant_id", "device_id", "model", "temperature", "humidity"); + List dataTypeList = + Arrays.asList( + TSDataType.STRING, + TSDataType.STRING, + TSDataType.STRING, + TSDataType.STRING, + TSDataType.FLOAT, + TSDataType.DOUBLE); + List columnTypeList = + new ArrayList<>( + Arrays.asList( + ColumnCategory.TAG, + ColumnCategory.TAG, + ColumnCategory.TAG, + ColumnCategory.ATTRIBUTE, + ColumnCategory.FIELD, + ColumnCategory.FIELD)); + Tablet tablet = new Tablet("table1", columnNameList, dataTypeList, columnTypeList, 100); + for (long timestamp = 0; timestamp < 100; timestamp++) { + int rowIndex = tablet.getRowSize(); + tablet.addTimestamp(rowIndex, timestamp); + tablet.addValue("region_id", rowIndex, "1"); + tablet.addValue("plant_id", rowIndex, "5"); + tablet.addValue("device_id", rowIndex, "3"); + tablet.addValue("model", rowIndex, "A"); + tablet.addValue("temperature", rowIndex, 37.6F); + tablet.addValue("humidity", rowIndex, 111.1); + if (tablet.getRowSize() == tablet.getMaxRowNumber()) { + session.insert(tablet); + tablet.reset(); + } + } + if (tablet.getRowSize() != 0) { + session.insert(tablet); + tablet.reset(); + } +} +``` + +在代码执行完成后,可以通过下述语句确认表已成功创建,其中包含了时间列、标签列、属性列以及测点列等各类信息。 + +```SQL +IoTDB> desc table1 ++-----------+---------+-----------+ +| ColumnName| DataType| Category| ++-----------+---------+-----------+ +| time|TIMESTAMP| TIME| +| region_id| STRING| TAG| +| plant_id| STRING| TAG| +| device_id| STRING| TAG| +| model| STRING| ATTRIBUTE| +|temperature| FLOAT| FIELD| +| humidity| DOUBLE| FIELD| ++-----------+---------+-----------+ +``` + +### 1.3 指定列写入 + +在写入操作中,可以指定部分列,未指定的列将不会被写入任何内容(即设置为 `null`)。 + +**示例:** + +```SQL +insert into table1("地区", "厂号", "设备号", Time, "温度", "排量") values ('湖南', '3001', '3', 4, 90.0, 1200.0) + + +insert into table1("地区", "厂号", "设备号", Time, "温度") values ('湖南, '3001', '3', 5, 90.0) +``` + +### 1.4 空值写入 + +标签列、属性列和测点列可以指定空值(`null`),表示不写入任何内容。 + +**示例(与上述事例等价):** + +```SQL +# 上述部分列写入等价于如下的带空值写入 +insert into table1("地区", "厂号", "设备号", "型号", "维修周期", Time, "温度", "排量") values ('湖南', '3001', '3', null, null, 4, 90.0, 1200.0) + +insert into table1("地区", "厂号", "设备号", "型号", "维修周期", Time, "温度", "排量") values ('湖南', '3001', '3', null, null, 5, 90.0, null) +``` + +当向不包含任何标签列的表中写入数据时,系统将默认创建一个所有标签列值均为 null 的device。 + +> 注意,该操作不仅会自动为表中已有的标签列填充 null 值,而且对于未来新增的标签列,同样会自动填充 null。 + +### 1.5 多行写入 + +支持同时写入多行数据,提高数据写入效率。 + +**示例:** + +```SQL +insert into table1 +values +(4, 北京', '3001', '3', '1', '10', 90.0, 1200.0), +(5, 北京', '3001', '3', '1', '10', 90.0, 1200.0); + + +insert into table1 +("地区", "厂号", "设备号", Time, "温度", "排量") +values +('北京', '3001', '3', 4, 90.0, 1200.0), +('北京', '3001', '3', 5, 90.0, 1200.0); +``` + +#### 注意事项 + +- 如果在 SQL 语句中引用了表中不存在的列,IoTDB 将返回错误码 `COLUMN_NOT_EXIST(616)`。 +- 如果写入的数据类型与表中列的数据类型不一致,将报错 `DATA_TYPE_MISMATCH(507)`。 + +### 1.6 查询写回 + +IoTDB 表模型支持追加查询写回功能,即`INSERT INTO QUERY` 语句,支持将查询结果写入**已经存在**的表中。 + +> 注意:该功能从 V 2.0.6 版本开始提供。 + +#### 1.6.1 语法定义 + +```SQL +INSERT INTO table_name [ ( column [, ... ] ) ] query +``` + +其中**​ ​query** 支持三种形式,下面将通过示例进行说明。 + +以[示例数据](../Reference/Sample-Data.md)为源数据,先创建目标表 + +```SQL +IoTDB:database1> CREATE TABLE target_table ( time TIMESTAMP TIME, region STRING TAG, device_id STRING TAG, temperature FLOAT FIELD ); +Msg: The statement is executed successfully. +``` + +1. 通过标准查询语句写回 + +即 query 处为直接通过`select ... from ...`执行的查询。 + +例如:使用标准查询语句,将 table1 中北京地区的 time, region, device\_id, temperature 数据查询写回到 target\_table 中 + +```SQL +IoTDB:database1> insert into target_table select time,region,device_id,temperature from table1 where region = '北京' +Msg: The statement is executed successfully. +IoTDB:database1> select * from target_table where region='北京' ++-----------------------------+------+---------+-----------+ +| time|region|device_id|temperature| ++-----------------------------+------+---------+-----------+ +|2024-11-26T13:37:00.000+08:00| 北京| 100| 90.0| +|2024-11-26T13:38:00.000+08:00| 北京| 100| 90.0| +|2024-11-27T16:38:00.000+08:00| 北京| 101| null| +|2024-11-27T16:39:00.000+08:00| 北京| 101| 85.0| +|2024-11-27T16:40:00.000+08:00| 北京| 101| 85.0| +|2024-11-27T16:41:00.000+08:00| 北京| 101| 85.0| +|2024-11-27T16:42:00.000+08:00| 北京| 101| null| +|2024-11-27T16:43:00.000+08:00| 北京| 101| null| +|2024-11-27T16:44:00.000+08:00| 北京| 101| null| ++-----------------------------+------+---------+-----------+ +Total line number = 9 +It costs 0.029s +``` + +2. 通过表引用查询写回 + +即 query 处为表引用方式`table source_table`。 + +例如:使用表引用查询,将 table3 中的数据查询写回到 target\_table 中 + +```SQL +IoTDB:database1> insert into target_table(time,device_id,temperature) table table3 +Msg: The statement is executed successfully. +IoTDB:database1> select * from target_table where region is null ++-----------------------------+------+---------+-----------+ +| time|region|device_id|temperature| ++-----------------------------+------+---------+-----------+ +|2025-05-13T00:00:00.001+08:00| null| d1| 90.0| +|2025-05-13T00:00:01.002+08:00| null| d1| 85.0| +|2025-05-13T00:00:02.101+08:00| null| d1| 85.0| +|2025-05-13T00:00:03.201+08:00| null| d1| null| +|2025-05-13T00:00:04.105+08:00| null| d1| 90.0| +|2025-05-13T00:00:05.023+08:00| null| d1| 85.0| +|2025-05-13T00:00:06.129+08:00| null| d1| 90.0| ++-----------------------------+------+---------+-----------+ +Total line number = 7 +It costs 0.015s +``` + +3. 通过子查询写回 + +即 query 处为带括号的子查询。 + +例如:使用子查询,将 table1 中时间与 table2 上海地区记录匹配的数据的 time, region, device\_id, temperature 查询写回到 target\_table + +```SQL +IoTDB:database1> insert into target_table (select t1.time, t1.region as region, t1.device_id as device_id, t1.temperature as temperature from table1 t1 where t1.time in (select t2.time from table2 t2 where t2.region = '上海')) +Msg: The statement is executed successfully. +IoTDB:database1> select * from target_table where region = '上海' ++-----------------------------+------+---------+-----------+ +| time|region|device_id|temperature| ++-----------------------------+------+---------+-----------+ +|2024-11-28T08:00:00.000+08:00| 上海| 100| 85.0| +|2024-11-29T11:00:00.000+08:00| 上海| 100| null| ++-----------------------------+------+---------+-----------+ +Total line number = 2 +It costs 0.014s +``` + +#### 1.6.2 相关说明 + +* 允许 query 中的源表与目标表 table\_name 是同一个表,例如:`INSERT INTO testtb SELECT * FROM testtb`。 +* 目标表必须已存在,否则提示错误信息`550: Table 'xxx.xxx' does not exist`。 +* 查询返回列和目标表列的数量和类型需完全匹配,目前不支持兼容类型的转换,若类型不匹配则提示错误信息 `701: Insert query has mismatched column types`。 +* 允许指定目标表的部分列,指定目标表列名时需符合以下规则: + * 必须包含时间戳列,否则提示错误信息`701: time column can not be null` + * 必须包含至少一个 FIELD 列,否则提示错误信息`701: No Field column present` + * 允许不指定 TAG 列 + * 允许指定列数少于目标表列数,缺失列自动补为 NULL 值 +* JAVA 支持使用 [executeNonQueryStatement](../API/Programming-Java-Native-API_apache.md#_3-1-itablesession接口) 方法执行`INSERT INTO QUERY`。 +* REST 支持[/rest/table/v1/nonQuery](../API/RestServiceV1.md#_3-3-非查询接口)API 执行`INSERT INTO QUERY`。 +* `INSERT INTO QUERY`不支持 Explain 和 Explain Analyze。 +* 用户必须有下列权限才能正常执行查询写回语句: + * 对查询语句中的源表具有 `SELECT` 权限。 + * 对目标表具有`WRITE`权限。 + * 更多用户权限相关的内容,请参考[权限管理](../User-Manual/Authority-Management_apache.md)。 + + +## 2. 数据更新 + +### 2.1 语法 + +```SQL +UPDATE SET updateAssignment (',' updateAssignment)* (WHERE where=booleanExpression)? + +updateAssignment + : identifier EQ expression + ; +``` + +1. `update`语句仅允许修改属性(ATTRIBUTE)列的值。 +2. `WHERE` 的规则: + - 范围仅限于标签列(TAG)和属性列(ATTRIBUTE),不允许涉及测点列(FIELD)和时间列(TIME)。 + - 不允许使用聚合函数 +3. 执行 SET 操作后,赋值表达式的结果应当是字符串类型,且其使用的限制应与 WHERE 子句中的表达式相同。 +4. 属性(ATTRIBUTE)列以及测点(FIELD)列的值也可通过`insert`语句来实现指定行的更新。 + +**示例:** + +```SQL +update table1 set b = a where substring(a, 1, 1) like '%' +``` \ No newline at end of file diff --git a/src/zh/UserGuide/Master/Table/Basic-Concept/Write-Updata-Data_timecho.md b/src/zh/UserGuide/Master/Table/Basic-Concept/Write-Updata-Data_timecho.md new file mode 100644 index 000000000..2cf792b56 --- /dev/null +++ b/src/zh/UserGuide/Master/Table/Basic-Concept/Write-Updata-Data_timecho.md @@ -0,0 +1,317 @@ + + +# 写入&更新 + +## 1. 数据写入 + +### 1.1 语法 + +在 IoTDB 中,数据写入遵循以下通用语法: + +```SQL +INSERT INTO [(COLUMN_NAME[, COLUMN_NAME]*)]? VALUES (COLUMN_VALUE[, COLUMN_VALUE]*) +``` + +基本约束包括: + +1. 通过 insert 语句写入无法自动创建表。 +2. 未指定的标签列将自动填充为 `null`。 +3. 未包含时间戳,系统将使用当前时间 `now()` 进行填充。 +4. 若当前设备(由标识信息定位)不存在该属性列的值,执行写入操作将导致原有的空值(NULL)被写入的数据所替代。 +5. 若当前设备(由标识信息定位)已有属性列的值,再次写入相同的属性列时,系统将更新该属性列的值为新数据。 +6. 写入重复时间戳,原时间戳对应列的值会更新。 +7. 若 INSERT 语句未指定列名(如 INSERT INTO table VALUES (...)),则 VALUES中的值必须严格按表中列的物理顺序排列(顺序可通过 DESC table命令查看)。 + +由于属性一般并不随时间的变化而变化,因此推荐以 update 的方式单独更新属性值,参见下文 [数据更新](#数据更新)。 + +
+ +
+ + +### 1.2 通过 Session 写入自动创建表 + +在通过 Session 进行数据写入时,IoTDB 支持无模式写入:无需事先手动创建表,系统会根据写入请求中的信息自动构建表结构,之后直接执行数据写入操作。 + +**示例:** + +```Java +try (ITableSession session = + new TableSessionBuilder() + .nodeUrls(Collections.singletonList("127.0.0.1:6667")) + .username("root") + .password("root") + .build()) { + + session.executeNonQueryStatement("CREATE DATABASE db1"); + session.executeNonQueryStatement("use db1"); + + // 不创建表直接写入数据 + List columnNameList = + Arrays.asList("region_id", "plant_id", "device_id", "model", "temperature", "humidity"); + List dataTypeList = + Arrays.asList( + TSDataType.STRING, + TSDataType.STRING, + TSDataType.STRING, + TSDataType.STRING, + TSDataType.FLOAT, + TSDataType.DOUBLE); + List columnTypeList = + new ArrayList<>( + Arrays.asList( + ColumnCategory.TAG, + ColumnCategory.TAG, + ColumnCategory.TAG, + ColumnCategory.ATTRIBUTE, + ColumnCategory.FIELD, + ColumnCategory.FIELD)); + Tablet tablet = new Tablet("table1", columnNameList, dataTypeList, columnTypeList, 100); + for (long timestamp = 0; timestamp < 100; timestamp++) { + int rowIndex = tablet.getRowSize(); + tablet.addTimestamp(rowIndex, timestamp); + tablet.addValue("region_id", rowIndex, "1"); + tablet.addValue("plant_id", rowIndex, "5"); + tablet.addValue("device_id", rowIndex, "3"); + tablet.addValue("model", rowIndex, "A"); + tablet.addValue("temperature", rowIndex, 37.6F); + tablet.addValue("humidity", rowIndex, 111.1); + if (tablet.getRowSize() == tablet.getMaxRowNumber()) { + session.insert(tablet); + tablet.reset(); + } + } + if (tablet.getRowSize() != 0) { + session.insert(tablet); + tablet.reset(); + } +} +``` + +在代码执行完成后,可以通过下述语句确认表已成功创建,其中包含了时间列、标签列、属性列以及测点列等各类信息。 + +```SQL +IoTDB> desc table1 ++-----------+---------+-----------+ +| ColumnName| DataType| Category| ++-----------+---------+-----------+ +| time|TIMESTAMP| TIME| +| region_id| STRING| TAG| +| plant_id| STRING| TAG| +| device_id| STRING| TAG| +| model| STRING| ATTRIBUTE| +|temperature| FLOAT| FIELD| +| humidity| DOUBLE| FIELD| ++-----------+---------+-----------+ +``` + +### 1.3 指定列写入 + +在写入操作中,可以指定部分列,未指定的列将不会被写入任何内容(即设置为 `null`)。 + +**示例:** + +```SQL +insert into table1("地区", "厂号", "设备号", Time, "温度", "排量") values ('湖南', '3001', '3', 4, 90.0, 1200.0) + + +insert into table1("地区", "厂号", "设备号", Time, "温度") values ('湖南, '3001', '3', 5, 90.0) +``` + +### 1.4 空值写入 + +标签列、属性列和测点列可以指定空值(`null`),表示不写入任何内容。 + +**示例(与上述事例等价):** + +```SQL +# 上述部分列写入等价于如下的带空值写入 +insert into table1("地区", "厂号", "设备号", "型号", "维修周期", Time, "温度", "排量") values ('湖南', '3001', '3', null, null, 4, 90.0, 1200.0) + +insert into table1("地区", "厂号", "设备号", "型号", "维修周期", Time, "温度", "排量") values ('湖南', '3001', '3', null, null, 5, 90.0, null) +``` + +当向不包含任何标签列的表中写入数据时,系统将默认创建一个所有标签列值均为 null 的device。 + +> 注意,该操作不仅会自动为表中已有的标签列填充 null 值,而且对于未来新增的标签列,同样会自动填充 null。 + +### 1.5 多行写入 + +支持同时写入多行数据,提高数据写入效率。 + +**示例:** + +```SQL +insert into table1 +values +(4, 北京', '3001', '3', '1', '10', 90.0, 1200.0), +(5, 北京', '3001', '3', '1', '10', 90.0, 1200.0); + + +insert into table1 +("地区", "厂号", "设备号", Time, "温度", "排量") +values +('北京', '3001', '3', 4, 90.0, 1200.0), +('北京', '3001', '3', 5, 90.0, 1200.0); +``` + +#### 注意事项 + +- 如果在 SQL 语句中引用了表中不存在的列,IoTDB 将返回错误码 `COLUMN_NOT_EXIST(616)`。 +- 如果写入的数据类型与表中列的数据类型不一致,将报错 `DATA_TYPE_MISMATCH(507)`。 + +### 1.6 查询写回 + +IoTDB 表模型支持追加查询写回功能,即`INSERT INTO QUERY` 语句,支持将查询结果写入**已经存在**的表中。 + +> 注意:该功能从 V 2.0.6 版本开始提供。 + +#### 1.6.1 语法定义 + +```SQL +INSERT INTO table_name [ ( column [, ... ] ) ] query +``` + +其中**​ ​query** 支持三种形式,下面将通过示例进行说明。 + +以[示例数据](../Reference/Sample-Data.md)为源数据,先创建目标表 + +```SQL +IoTDB:database1> CREATE TABLE target_table ( time TIMESTAMP TIME, region STRING TAG, device_id STRING TAG, temperature FLOAT FIELD ); +Msg: The statement is executed successfully. +``` + +1. 通过标准查询语句写回 + +即 query 处为直接通过`select ... from ...`执行的查询。 + +例如:使用标准查询语句,将 table1 中北京地区的 time, region, device\_id, temperature 数据查询写回到 target\_table 中 + +```SQL +IoTDB:database1> insert into target_table select time,region,device_id,temperature from table1 where region = '北京' +Msg: The statement is executed successfully. +IoTDB:database1> select * from target_table where region='北京' ++-----------------------------+------+---------+-----------+ +| time|region|device_id|temperature| ++-----------------------------+------+---------+-----------+ +|2024-11-26T13:37:00.000+08:00| 北京| 100| 90.0| +|2024-11-26T13:38:00.000+08:00| 北京| 100| 90.0| +|2024-11-27T16:38:00.000+08:00| 北京| 101| null| +|2024-11-27T16:39:00.000+08:00| 北京| 101| 85.0| +|2024-11-27T16:40:00.000+08:00| 北京| 101| 85.0| +|2024-11-27T16:41:00.000+08:00| 北京| 101| 85.0| +|2024-11-27T16:42:00.000+08:00| 北京| 101| null| +|2024-11-27T16:43:00.000+08:00| 北京| 101| null| +|2024-11-27T16:44:00.000+08:00| 北京| 101| null| ++-----------------------------+------+---------+-----------+ +Total line number = 9 +It costs 0.029s +``` + +2. 通过表引用查询写回 + +即 query 处为表引用方式`table source_table`。 + +例如:使用表引用查询,将 table3 中的数据查询写回到 target\_table 中 + +```SQL +IoTDB:database1> insert into target_table(time,device_id,temperature) table table3 +Msg: The statement is executed successfully. +IoTDB:database1> select * from target_table where region is null ++-----------------------------+------+---------+-----------+ +| time|region|device_id|temperature| ++-----------------------------+------+---------+-----------+ +|2025-05-13T00:00:00.001+08:00| null| d1| 90.0| +|2025-05-13T00:00:01.002+08:00| null| d1| 85.0| +|2025-05-13T00:00:02.101+08:00| null| d1| 85.0| +|2025-05-13T00:00:03.201+08:00| null| d1| null| +|2025-05-13T00:00:04.105+08:00| null| d1| 90.0| +|2025-05-13T00:00:05.023+08:00| null| d1| 85.0| +|2025-05-13T00:00:06.129+08:00| null| d1| 90.0| ++-----------------------------+------+---------+-----------+ +Total line number = 7 +It costs 0.015s +``` + +3. 通过子查询写回 + +即 query 处为带括号的子查询。 + +例如:使用子查询,将 table1 中时间与 table2 上海地区记录匹配的数据的 time, region, device\_id, temperature 查询写回到 target\_table + +```SQL +IoTDB:database1> insert into target_table (select t1.time, t1.region as region, t1.device_id as device_id, t1.temperature as temperature from table1 t1 where t1.time in (select t2.time from table2 t2 where t2.region = '上海')) +Msg: The statement is executed successfully. +IoTDB:database1> select * from target_table where region = '上海' ++-----------------------------+------+---------+-----------+ +| time|region|device_id|temperature| ++-----------------------------+------+---------+-----------+ +|2024-11-28T08:00:00.000+08:00| 上海| 100| 85.0| +|2024-11-29T11:00:00.000+08:00| 上海| 100| null| ++-----------------------------+------+---------+-----------+ +Total line number = 2 +It costs 0.014s +``` + +#### 1.6.2 相关说明 + +* 允许 query 中的源表与目标表 table\_name 是同一个表,例如:`INSERT INTO testtb SELECT * FROM testtb`。 +* 目标表必须已存在,否则提示错误信息`550: Table 'xxx.xxx' does not exist`。 +* 查询返回列和目标表列的数量和类型需完全匹配,目前不支持兼容类型的转换,若类型不匹配则提示错误信息 `701: Insert query has mismatched column types`。 +* 允许指定目标表的部分列,指定目标表列名时需符合以下规则: + * 必须包含时间戳列,否则提示错误信息`701: time column can not be null` + * 必须包含至少一个 FIELD 列,否则提示错误信息`701: No Field column present` + * 允许不指定 TAG 列 + * 允许指定列数少于目标表列数,缺失列自动补为 NULL 值 +* JAVA 支持使用 [executeNonQueryStatement](../API/Programming-Java-Native-API_timecho.md#_3-1-itablesession接口) 方法执行`INSERT INTO QUERY`。 +* REST 支持[/rest/table/v1/nonQuery](../API/RestServiceV1.md#_3-3-非查询接口)API 执行`INSERT INTO QUERY`。 +* `INSERT INTO QUERY`不支持 Explain 和 Explain Analyze。 +* 用户必须有下列权限才能正常执行查询写回语句: + * 对查询语句中的源表具有 `SELECT` 权限。 + * 对目标表具有`WRITE`权限。 + * 更多用户权限相关的内容,请参考[权限管理](../User-Manual/Authority-Management_timecho.md)。 + + +## 2. 数据更新 + +### 2.1 语法 + +```SQL +UPDATE SET updateAssignment (',' updateAssignment)* (WHERE where=booleanExpression)? + +updateAssignment + : identifier EQ expression + ; +``` + +1. `update`语句仅允许修改属性(ATTRIBUTE)列的值。 +2. `WHERE` 的规则: + - 范围仅限于标签列(TAG)和属性列(ATTRIBUTE),不允许涉及测点列(FIELD)和时间列(TIME)。 + - 不允许使用聚合函数 +3. 执行 SET 操作后,赋值表达式的结果应当是字符串类型,且其使用的限制应与 WHERE 子句中的表达式相同。 +4. 属性(ATTRIBUTE)列以及测点(FIELD)列的值也可通过`insert`语句来实现指定行的更新。 + +**示例:** + +```SQL +update table1 set b = a where substring(a, 1, 1) like '%' +``` \ No newline at end of file diff --git a/src/zh/UserGuide/Master/Table/QuickStart/QuickStart_apache.md b/src/zh/UserGuide/Master/Table/QuickStart/QuickStart_apache.md index 8fbf5e008..f8c960ed5 100644 --- a/src/zh/UserGuide/Master/Table/QuickStart/QuickStart_apache.md +++ b/src/zh/UserGuide/Master/Table/QuickStart/QuickStart_apache.md @@ -53,7 +53,7 @@ - 表介绍:[表管理](../Basic-Concept/Table-Management.md) -2. 数据写入&更新:在数据写入&更新方面,IoTDB 提供了多种方式来插入实时数据,基本的数据写入&更新操作请查看 [数据写入&更新](../Basic-Concept/Write-Updata-Data.md) +2. 数据写入&更新:在数据写入&更新方面,IoTDB 提供了多种方式来插入实时数据,支持追加查询写回,基本介绍请查看 [数据写入&更新](../Basic-Concept/Write-Updata-Data_apache.md) 3. 数据查询:IoTDB 提供了丰富的数据查询功能,数据查询的基本介绍请查看 [数据查询](../Basic-Concept/Query-Data_apache.md) diff --git a/src/zh/UserGuide/Master/Table/QuickStart/QuickStart_timecho.md b/src/zh/UserGuide/Master/Table/QuickStart/QuickStart_timecho.md index 08d1fac39..6481213fc 100644 --- a/src/zh/UserGuide/Master/Table/QuickStart/QuickStart_timecho.md +++ b/src/zh/UserGuide/Master/Table/QuickStart/QuickStart_timecho.md @@ -60,7 +60,7 @@ - 表介绍:[表管理](../Basic-Concept/Table-Management.md) -2. 数据写入&更新:在数据写入&更新方面,IoTDB 提供了多种方式来插入实时数据,基本的数据写入&更新操作请查看 [数据写入&更新](../Basic-Concept/Write-Updata-Data.md) +2. 数据写入&更新:在数据写入&更新方面,IoTDB 提供了多种方式来插入实时数据,支持追加查询写回,基本介绍请查看 [数据写入&更新](../Basic-Concept/Write-Updata-Data_timecho.md) 3. 数据查询:IoTDB 提供了丰富的数据查询功能,数据查询的基本介绍请查看 [数据查询](../Basic-Concept/Query-Data_timecho.md),其中包含了适用于识别时序数据中的特定模式、检测特定事件等业务场景的[行模式识别](../SQL-Manual/Row-Pattern-Recognition.md),以及常用于数据分析场景的[窗口函数](../SQL-Manual/Featured-Functions_timecho.md#_4-窗口函数)等特色函数 diff --git a/src/zh/UserGuide/Master/Table/SQL-Manual/SQL-Data-Addition-Deletion.md b/src/zh/UserGuide/Master/Table/SQL-Manual/SQL-Data-Addition-Deletion.md index 35abe3691..84dc187da 100644 --- a/src/zh/UserGuide/Master/Table/SQL-Manual/SQL-Data-Addition-Deletion.md +++ b/src/zh/UserGuide/Master/Table/SQL-Manual/SQL-Data-Addition-Deletion.md @@ -1,3 +1,6 @@ +--- +redirectTo: SQL-Data-Addition-Deletion_apache.html +--- - -# 数据增删 - -## 1. 数据写入 - -**语法:** - -```SQL -INSERT INTO [(COLUMN_NAME[, COLUMN_NAME]*)]? VALUES (COLUMN_VALUE[, COLUMN_VALUE]*) -``` - -更多详细语法说明请参考:[写入语法](../Basic-Concept/Write-Updata-Data.md#_1-1-语法) - -**示例一:指定列写入** - -```SQL -insert into table1("地区", "厂号", "设备号", Time, "温度", "排量") values ('湖南', '3001', '3', 4, 90.0, 1200.0) - -insert into table1("地区", "厂号", "设备号", Time, "温度") values ('湖南, '3001', '3', 5, 90.0) -``` - -**示例二:空值写入** - -```SQL --- 上述部分列写入等价于如下的带空值写入 -insert into table1("地区", "厂号", "设备号", "型号", "维修周期", Time, "温度", "排量") values ('湖南', '3001', '3', null, null, 4, 90.0, 1200.0) - -insert into table1("地区", "厂号", "设备号", "型号", "维修周期", Time, "温度", "排量") values ('湖南', '3001', '3', null, null, 5, 90.0, null) -``` - -**示例三:多行写入** - -```SQL -insert into table1 -values -(4, '北京', '3001', '3', '1', '10', 90.0, 1200.0), -(5, '北京', '3001', '3', '1', '10', 90.0, 1200.0); - -insert into table1 -("地区", "厂号", "设备号", Time, "温度", "排量") -values -('北京', '3001', '3', 4, 90.0, 1200.0), -('北京', '3001', '3', 5, 90.0, 1200.0); -``` - -## 2. 数据更新 - -**语法:** - -```SQL -UPDATE SET updateAssignment (',' updateAssignment)* (WHERE where=booleanExpression)? - -updateAssignment - : identifier EQ expression - ; -``` - -更多详细语法说明请参考:[更新语法](../Basic-Concept/Write-Updata-Data.md#_2-1-语法) - -**示例:** - -```SQL -update table1 set b = a where substring(a, 1, 1) like '%' -``` - -## 3. 数据删除 - -**语法:** - -```SQL -DELETE FROM [WHERE_CLAUSE]? - -WHERE_CLAUSE: - WHERE DELETE_CONDITION - -DELETE_CONDITION: - SINGLE_CONDITION - | DELETE_CONDITION AND DELETE_CONDITION - | DELETE_CONDITION OR DELETE_CONDITION - -SINGLE_CODITION: - TIME_CONDITION | ID_CONDITION - -TIME_CONDITION: - time TIME_OPERATOR LONG_LITERAL - -TIME_OPERATOR: - < | > | <= | >= | = - -ID_CONDITION: - identifier = STRING_LITERAL -``` - -**示例一: 删除全表数据** - -```SQL --- 全表删除 -DELETE FROM table1 -``` - -**示例二:删除一段时间范围内的数据** - -```SQL --- 单时间段数据删除 -DELETE FROM table1 WHERE time <= 2024-11-29 00:00:00 - --- 多时间段数据删除 -DELETE FROM table1 WHERE time >= 2024-11-27 00:00:00 and time <= 2024-11-29 00:00:00 -``` - -**示例三:删除指定设备的数据** - -```SQL --- 删除指定设备的数据 -DELETE FROM table1 WHERE device_id='101' and model_id = 'B' - --- 删除指定设备及时间段的数据 -DELETE FROM table1 - WHERE time >= 2024-11-27 16:39:00 and time <= 2024-11-29 16:42:00 - AND device_id='101' and model_id = 'B' - --- 删除指定类型设备的数据 -DELETE FROM table1 WHERE model_id = 'B' -``` - -## 4. 设备删除 - -**语法:** - -```SQL -DELETE DEVICES FROM tableName=qualifiedName (WHERE booleanExpression)? -``` - -**示例:删除指定设备及其相关的所有数据** - -```SQL -DELETE DEVICES FROM table1 WHERE device_id = '101' -``` diff --git a/src/zh/UserGuide/Master/Table/SQL-Manual/SQL-Data-Addition-Deletion_apache.md b/src/zh/UserGuide/Master/Table/SQL-Manual/SQL-Data-Addition-Deletion_apache.md new file mode 100644 index 000000000..423898996 --- /dev/null +++ b/src/zh/UserGuide/Master/Table/SQL-Manual/SQL-Data-Addition-Deletion_apache.md @@ -0,0 +1,168 @@ + + +# 数据增删 + +## 1. 数据写入 + +**语法:** + +```SQL +INSERT INTO [(COLUMN_NAME[, COLUMN_NAME]*)]? VALUES (COLUMN_VALUE[, COLUMN_VALUE]*) +``` + +更多详细语法说明请参考:[写入语法](../Basic-Concept/Write-Updata-Data_apache.md#_1-1-语法) + +**示例一:指定列写入** + +```SQL +insert into table1("地区", "厂号", "设备号", Time, "温度", "排量") values ('湖南', '3001', '3', 4, 90.0, 1200.0) + +insert into table1("地区", "厂号", "设备号", Time, "温度") values ('湖南, '3001', '3', 5, 90.0) +``` + +**示例二:空值写入** + +```SQL +-- 上述部分列写入等价于如下的带空值写入 +insert into table1("地区", "厂号", "设备号", "型号", "维修周期", Time, "温度", "排量") values ('湖南', '3001', '3', null, null, 4, 90.0, 1200.0) + +insert into table1("地区", "厂号", "设备号", "型号", "维修周期", Time, "温度", "排量") values ('湖南', '3001', '3', null, null, 5, 90.0, null) +``` + +**示例三:多行写入** + +```SQL +insert into table1 +values +(4, '北京', '3001', '3', '1', '10', 90.0, 1200.0), +(5, '北京', '3001', '3', '1', '10', 90.0, 1200.0); + +insert into table1 +("地区", "厂号", "设备号", Time, "温度", "排量") +values +('北京', '3001', '3', 4, 90.0, 1200.0), +('北京', '3001', '3', 5, 90.0, 1200.0); +``` + +**示例四:查询写回** + +```SQL +insert into target_table select time,region,device_id,temperature from table1 where region = '北京'; + +insert into target_table(time,device_id,temperature) table table3; + +insert into target_table (select t1.time, t1.region as region, t1.device_id as device_id, t1.temperature as temperature from table1 t1 where t1.time in (select t2.time from table2 t2 where t2.region = '上海')); +``` + +## 2. 数据更新 + +**语法:** + +```SQL +UPDATE SET updateAssignment (',' updateAssignment)* (WHERE where=booleanExpression)? + +updateAssignment + : identifier EQ expression + ; +``` + +更多详细语法说明请参考:[更新语法](../Basic-Concept/Write-Updata-Data_apache.md#_2-1-语法) + +**示例:** + +```SQL +update table1 set b = a where substring(a, 1, 1) like '%' +``` + +## 3. 数据删除 + +**语法:** + +```SQL +DELETE FROM [WHERE_CLAUSE]? + +WHERE_CLAUSE: + WHERE DELETE_CONDITION + +DELETE_CONDITION: + SINGLE_CONDITION + | DELETE_CONDITION AND DELETE_CONDITION + | DELETE_CONDITION OR DELETE_CONDITION + +SINGLE_CODITION: + TIME_CONDITION | ID_CONDITION + +TIME_CONDITION: + time TIME_OPERATOR LONG_LITERAL + +TIME_OPERATOR: + < | > | <= | >= | = + +ID_CONDITION: + identifier = STRING_LITERAL +``` + +**示例一: 删除全表数据** + +```SQL +-- 全表删除 +DELETE FROM table1 +``` + +**示例二:删除一段时间范围内的数据** + +```SQL +-- 单时间段数据删除 +DELETE FROM table1 WHERE time <= 2024-11-29 00:00:00 + +-- 多时间段数据删除 +DELETE FROM table1 WHERE time >= 2024-11-27 00:00:00 and time <= 2024-11-29 00:00:00 +``` + +**示例三:删除指定设备的数据** + +```SQL +-- 删除指定设备的数据 +DELETE FROM table1 WHERE device_id='101' and model_id = 'B' + +-- 删除指定设备及时间段的数据 +DELETE FROM table1 + WHERE time >= 2024-11-27 16:39:00 and time <= 2024-11-29 16:42:00 + AND device_id='101' and model_id = 'B' + +-- 删除指定类型设备的数据 +DELETE FROM table1 WHERE model_id = 'B' +``` + +## 4. 设备删除 + +**语法:** + +```SQL +DELETE DEVICES FROM tableName=qualifiedName (WHERE booleanExpression)? +``` + +**示例:删除指定设备及其相关的所有数据** + +```SQL +DELETE DEVICES FROM table1 WHERE device_id = '101' +``` diff --git a/src/zh/UserGuide/Master/Table/SQL-Manual/SQL-Data-Addition-Deletion_timecho.md b/src/zh/UserGuide/Master/Table/SQL-Manual/SQL-Data-Addition-Deletion_timecho.md new file mode 100644 index 000000000..f24b1e923 --- /dev/null +++ b/src/zh/UserGuide/Master/Table/SQL-Manual/SQL-Data-Addition-Deletion_timecho.md @@ -0,0 +1,168 @@ + + +# 数据增删 + +## 1. 数据写入 + +**语法:** + +```SQL +INSERT INTO [(COLUMN_NAME[, COLUMN_NAME]*)]? VALUES (COLUMN_VALUE[, COLUMN_VALUE]*) +``` + +更多详细语法说明请参考:[写入语法](../Basic-Concept/Write-Updata-Data_timecho.md#_1-1-语法) + +**示例一:指定列写入** + +```SQL +insert into table1("地区", "厂号", "设备号", Time, "温度", "排量") values ('湖南', '3001', '3', 4, 90.0, 1200.0) + +insert into table1("地区", "厂号", "设备号", Time, "温度") values ('湖南, '3001', '3', 5, 90.0) +``` + +**示例二:空值写入** + +```SQL +-- 上述部分列写入等价于如下的带空值写入 +insert into table1("地区", "厂号", "设备号", "型号", "维修周期", Time, "温度", "排量") values ('湖南', '3001', '3', null, null, 4, 90.0, 1200.0) + +insert into table1("地区", "厂号", "设备号", "型号", "维修周期", Time, "温度", "排量") values ('湖南', '3001', '3', null, null, 5, 90.0, null) +``` + +**示例三:多行写入** + +```SQL +insert into table1 +values +(4, '北京', '3001', '3', '1', '10', 90.0, 1200.0), +(5, '北京', '3001', '3', '1', '10', 90.0, 1200.0); + +insert into table1 +("地区", "厂号", "设备号", Time, "温度", "排量") +values +('北京', '3001', '3', 4, 90.0, 1200.0), +('北京', '3001', '3', 5, 90.0, 1200.0); +``` + +**示例四:查询写回** + +```SQL +insert into target_table select time,region,device_id,temperature from table1 where region = '北京'; + +insert into target_table(time,device_id,temperature) table table3; + +insert into target_table (select t1.time, t1.region as region, t1.device_id as device_id, t1.temperature as temperature from table1 t1 where t1.time in (select t2.time from table2 t2 where t2.region = '上海')); +``` + +## 2. 数据更新 + +**语法:** + +```SQL +UPDATE SET updateAssignment (',' updateAssignment)* (WHERE where=booleanExpression)? + +updateAssignment + : identifier EQ expression + ; +``` + +更多详细语法说明请参考:[更新语法](../Basic-Concept/Write-Updata-Data_timecho.md#_2-1-语法) + +**示例:** + +```SQL +update table1 set b = a where substring(a, 1, 1) like '%' +``` + +## 3. 数据删除 + +**语法:** + +```SQL +DELETE FROM [WHERE_CLAUSE]? + +WHERE_CLAUSE: + WHERE DELETE_CONDITION + +DELETE_CONDITION: + SINGLE_CONDITION + | DELETE_CONDITION AND DELETE_CONDITION + | DELETE_CONDITION OR DELETE_CONDITION + +SINGLE_CODITION: + TIME_CONDITION | ID_CONDITION + +TIME_CONDITION: + time TIME_OPERATOR LONG_LITERAL + +TIME_OPERATOR: + < | > | <= | >= | = + +ID_CONDITION: + identifier = STRING_LITERAL +``` + +**示例一: 删除全表数据** + +```SQL +-- 全表删除 +DELETE FROM table1 +``` + +**示例二:删除一段时间范围内的数据** + +```SQL +-- 单时间段数据删除 +DELETE FROM table1 WHERE time <= 2024-11-29 00:00:00 + +-- 多时间段数据删除 +DELETE FROM table1 WHERE time >= 2024-11-27 00:00:00 and time <= 2024-11-29 00:00:00 +``` + +**示例三:删除指定设备的数据** + +```SQL +-- 删除指定设备的数据 +DELETE FROM table1 WHERE device_id='101' and model_id = 'B' + +-- 删除指定设备及时间段的数据 +DELETE FROM table1 + WHERE time >= 2024-11-27 16:39:00 and time <= 2024-11-29 16:42:00 + AND device_id='101' and model_id = 'B' + +-- 删除指定类型设备的数据 +DELETE FROM table1 WHERE model_id = 'B' +``` + +## 4. 设备删除 + +**语法:** + +```SQL +DELETE DEVICES FROM tableName=qualifiedName (WHERE booleanExpression)? +``` + +**示例:删除指定设备及其相关的所有数据** + +```SQL +DELETE DEVICES FROM table1 WHERE device_id = '101' +``` diff --git a/src/zh/UserGuide/latest-Table/Basic-Concept/Write-Updata-Data.md b/src/zh/UserGuide/latest-Table/Basic-Concept/Write-Updata-Data.md index 2549bdf52..98f66ca6c 100644 --- a/src/zh/UserGuide/latest-Table/Basic-Concept/Write-Updata-Data.md +++ b/src/zh/UserGuide/latest-Table/Basic-Concept/Write-Updata-Data.md @@ -1,3 +1,6 @@ +--- +redirectTo: Write-Updata-Data_apache.html +--- - -# 写入&更新 - -## 1. 数据写入 - -### 1.1 语法 - -在 IoTDB 中,数据写入遵循以下通用语法: - -```SQL -INSERT INTO [(COLUMN_NAME[, COLUMN_NAME]*)]? VALUES (COLUMN_VALUE[, COLUMN_VALUE]*) -``` - -基本约束包括: - -1. 通过 insert 语句写入无法自动创建表。 -2. 未指定的标签列将自动填充为 `null`。 -3. 未包含时间戳,系统将使用当前时间 `now()` 进行填充。 -4. 若当前设备(由标识信息定位)不存在该属性列的值,执行写入操作将导致原有的空值(NULL)被写入的数据所替代。 -5. 若当前设备(由标识信息定位)已有属性列的值,再次写入相同的属性列时,系统将更新该属性列的值为新数据。 -6. 写入重复时间戳,原时间戳对应列的值会更新。 -7. 若 INSERT 语句未指定列名(如 INSERT INTO table VALUES (...)),则 VALUES中的值必须严格按表中列的物理顺序排列(顺序可通过 DESC table命令查看)。 - -由于属性一般并不随时间的变化而变化,因此推荐以 update 的方式单独更新属性值,参见下文 [数据更新](#数据更新)。 - -
- -
- - -### 1.2 通过 Session 写入自动创建表 - -在通过 Session 进行数据写入时,IoTDB 支持无模式写入:无需事先手动创建表,系统会根据写入请求中的信息自动构建表结构,之后直接执行数据写入操作。 - -**示例:** - -```Java -try (ITableSession session = - new TableSessionBuilder() - .nodeUrls(Collections.singletonList("127.0.0.1:6667")) - .username("root") - .password("root") - .build()) { - - session.executeNonQueryStatement("CREATE DATABASE db1"); - session.executeNonQueryStatement("use db1"); - - // 不创建表直接写入数据 - List columnNameList = - Arrays.asList("region_id", "plant_id", "device_id", "model", "temperature", "humidity"); - List dataTypeList = - Arrays.asList( - TSDataType.STRING, - TSDataType.STRING, - TSDataType.STRING, - TSDataType.STRING, - TSDataType.FLOAT, - TSDataType.DOUBLE); - List columnTypeList = - new ArrayList<>( - Arrays.asList( - ColumnCategory.TAG, - ColumnCategory.TAG, - ColumnCategory.TAG, - ColumnCategory.ATTRIBUTE, - ColumnCategory.FIELD, - ColumnCategory.FIELD)); - Tablet tablet = new Tablet("table1", columnNameList, dataTypeList, columnTypeList, 100); - for (long timestamp = 0; timestamp < 100; timestamp++) { - int rowIndex = tablet.getRowSize(); - tablet.addTimestamp(rowIndex, timestamp); - tablet.addValue("region_id", rowIndex, "1"); - tablet.addValue("plant_id", rowIndex, "5"); - tablet.addValue("device_id", rowIndex, "3"); - tablet.addValue("model", rowIndex, "A"); - tablet.addValue("temperature", rowIndex, 37.6F); - tablet.addValue("humidity", rowIndex, 111.1); - if (tablet.getRowSize() == tablet.getMaxRowNumber()) { - session.insert(tablet); - tablet.reset(); - } - } - if (tablet.getRowSize() != 0) { - session.insert(tablet); - tablet.reset(); - } -} -``` - -在代码执行完成后,可以通过下述语句确认表已成功创建,其中包含了时间列、标签列、属性列以及测点列等各类信息。 - -```SQL -IoTDB> desc table1 -+-----------+---------+-----------+ -| ColumnName| DataType| Category| -+-----------+---------+-----------+ -| time|TIMESTAMP| TIME| -| region_id| STRING| TAG| -| plant_id| STRING| TAG| -| device_id| STRING| TAG| -| model| STRING| ATTRIBUTE| -|temperature| FLOAT| FIELD| -| humidity| DOUBLE| FIELD| -+-----------+---------+-----------+ -``` - -### 1.3 指定列写入 - -在写入操作中,可以指定部分列,未指定的列将不会被写入任何内容(即设置为 `null`)。 - -**示例:** - -```SQL -insert into table1("地区", "厂号", "设备号", Time, "温度", "排量") values ('湖南', '3001', '3', 4, 90.0, 1200.0) - - -insert into table1("地区", "厂号", "设备号", Time, "温度") values ('湖南, '3001', '3', 5, 90.0) -``` - -### 1.4 空值写入 - -标签列、属性列和测点列可以指定空值(`null`),表示不写入任何内容。 - -**示例(与上述事例等价):** - -```SQL -# 上述部分列写入等价于如下的带空值写入 -insert into table1("地区", "厂号", "设备号", "型号", "维修周期", Time, "温度", "排量") values ('湖南', '3001', '3', null, null, 4, 90.0, 1200.0) - -insert into table1("地区", "厂号", "设备号", "型号", "维修周期", Time, "温度", "排量") values ('湖南', '3001', '3', null, null, 5, 90.0, null) -``` - -当向不包含任何标签列的表中写入数据时,系统将默认创建一个所有标签列值均为 null 的device。 - -> 注意,该操作不仅会自动为表中已有的标签列填充 null 值,而且对于未来新增的标签列,同样会自动填充 null。 - -### 1.5 多行写入 - -支持同时写入多行数据,提高数据写入效率。 - -**示例:** - -```SQL -insert into table1 -values -(4, 北京', '3001', '3', '1', '10', 90.0, 1200.0), -(5, 北京', '3001', '3', '1', '10', 90.0, 1200.0); - - -insert into table1 -("地区", "厂号", "设备号", Time, "温度", "排量") -values -('北京', '3001', '3', 4, 90.0, 1200.0), -('北京', '3001', '3', 5, 90.0, 1200.0); -``` - -#### 注意事项 - -- 如果在 SQL 语句中引用了表中不存在的列,IoTDB 将返回错误码 `COLUMN_NOT_EXIST(616)`。 -- 如果写入的数据类型与表中列的数据类型不一致,将报错 `DATA_TYPE_MISMATCH(507)`。 - -## 2. 数据更新 - -### 2.1 语法 - -```SQL -UPDATE SET updateAssignment (',' updateAssignment)* (WHERE where=booleanExpression)? - -updateAssignment - : identifier EQ expression - ; -``` - -1. `update`语句仅允许修改属性(ATTRIBUTE)列的值。 -2. `WHERE` 的规则: - - 范围仅限于标签列(TAG)和属性列(ATTRIBUTE),不允许涉及测点列(FIELD)和时间列(TIME)。 - - 不允许使用聚合函数 -3. 执行 SET 操作后,赋值表达式的结果应当是字符串类型,且其使用的限制应与 WHERE 子句中的表达式相同。 -4. 属性(ATTRIBUTE)列以及测点(FIELD)列的值也可通过`insert`语句来实现指定行的更新。 - -**示例:** - -```SQL -update table1 set b = a where substring(a, 1, 1) like '%' -``` \ No newline at end of file diff --git a/src/zh/UserGuide/latest-Table/Basic-Concept/Write-Updata-Data_apache.md b/src/zh/UserGuide/latest-Table/Basic-Concept/Write-Updata-Data_apache.md new file mode 100644 index 000000000..3f21c60d9 --- /dev/null +++ b/src/zh/UserGuide/latest-Table/Basic-Concept/Write-Updata-Data_apache.md @@ -0,0 +1,317 @@ + + +# 写入&更新 + +## 1. 数据写入 + +### 1.1 语法 + +在 IoTDB 中,数据写入遵循以下通用语法: + +```SQL +INSERT INTO [(COLUMN_NAME[, COLUMN_NAME]*)]? VALUES (COLUMN_VALUE[, COLUMN_VALUE]*) +``` + +基本约束包括: + +1. 通过 insert 语句写入无法自动创建表。 +2. 未指定的标签列将自动填充为 `null`。 +3. 未包含时间戳,系统将使用当前时间 `now()` 进行填充。 +4. 若当前设备(由标识信息定位)不存在该属性列的值,执行写入操作将导致原有的空值(NULL)被写入的数据所替代。 +5. 若当前设备(由标识信息定位)已有属性列的值,再次写入相同的属性列时,系统将更新该属性列的值为新数据。 +6. 写入重复时间戳,原时间戳对应列的值会更新。 +7. 若 INSERT 语句未指定列名(如 INSERT INTO table VALUES (...)),则 VALUES中的值必须严格按表中列的物理顺序排列(顺序可通过 DESC table命令查看)。 + +由于属性一般并不随时间的变化而变化,因此推荐以 update 的方式单独更新属性值,参见下文 [数据更新](#数据更新)。 + +
+ +
+ + +### 1.2 通过 Session 写入自动创建表 + +在通过 Session 进行数据写入时,IoTDB 支持无模式写入:无需事先手动创建表,系统会根据写入请求中的信息自动构建表结构,之后直接执行数据写入操作。 + +**示例:** + +```Java +try (ITableSession session = + new TableSessionBuilder() + .nodeUrls(Collections.singletonList("127.0.0.1:6667")) + .username("root") + .password("root") + .build()) { + + session.executeNonQueryStatement("CREATE DATABASE db1"); + session.executeNonQueryStatement("use db1"); + + // 不创建表直接写入数据 + List columnNameList = + Arrays.asList("region_id", "plant_id", "device_id", "model", "temperature", "humidity"); + List dataTypeList = + Arrays.asList( + TSDataType.STRING, + TSDataType.STRING, + TSDataType.STRING, + TSDataType.STRING, + TSDataType.FLOAT, + TSDataType.DOUBLE); + List columnTypeList = + new ArrayList<>( + Arrays.asList( + ColumnCategory.TAG, + ColumnCategory.TAG, + ColumnCategory.TAG, + ColumnCategory.ATTRIBUTE, + ColumnCategory.FIELD, + ColumnCategory.FIELD)); + Tablet tablet = new Tablet("table1", columnNameList, dataTypeList, columnTypeList, 100); + for (long timestamp = 0; timestamp < 100; timestamp++) { + int rowIndex = tablet.getRowSize(); + tablet.addTimestamp(rowIndex, timestamp); + tablet.addValue("region_id", rowIndex, "1"); + tablet.addValue("plant_id", rowIndex, "5"); + tablet.addValue("device_id", rowIndex, "3"); + tablet.addValue("model", rowIndex, "A"); + tablet.addValue("temperature", rowIndex, 37.6F); + tablet.addValue("humidity", rowIndex, 111.1); + if (tablet.getRowSize() == tablet.getMaxRowNumber()) { + session.insert(tablet); + tablet.reset(); + } + } + if (tablet.getRowSize() != 0) { + session.insert(tablet); + tablet.reset(); + } +} +``` + +在代码执行完成后,可以通过下述语句确认表已成功创建,其中包含了时间列、标签列、属性列以及测点列等各类信息。 + +```SQL +IoTDB> desc table1 ++-----------+---------+-----------+ +| ColumnName| DataType| Category| ++-----------+---------+-----------+ +| time|TIMESTAMP| TIME| +| region_id| STRING| TAG| +| plant_id| STRING| TAG| +| device_id| STRING| TAG| +| model| STRING| ATTRIBUTE| +|temperature| FLOAT| FIELD| +| humidity| DOUBLE| FIELD| ++-----------+---------+-----------+ +``` + +### 1.3 指定列写入 + +在写入操作中,可以指定部分列,未指定的列将不会被写入任何内容(即设置为 `null`)。 + +**示例:** + +```SQL +insert into table1("地区", "厂号", "设备号", Time, "温度", "排量") values ('湖南', '3001', '3', 4, 90.0, 1200.0) + + +insert into table1("地区", "厂号", "设备号", Time, "温度") values ('湖南, '3001', '3', 5, 90.0) +``` + +### 1.4 空值写入 + +标签列、属性列和测点列可以指定空值(`null`),表示不写入任何内容。 + +**示例(与上述事例等价):** + +```SQL +# 上述部分列写入等价于如下的带空值写入 +insert into table1("地区", "厂号", "设备号", "型号", "维修周期", Time, "温度", "排量") values ('湖南', '3001', '3', null, null, 4, 90.0, 1200.0) + +insert into table1("地区", "厂号", "设备号", "型号", "维修周期", Time, "温度", "排量") values ('湖南', '3001', '3', null, null, 5, 90.0, null) +``` + +当向不包含任何标签列的表中写入数据时,系统将默认创建一个所有标签列值均为 null 的device。 + +> 注意,该操作不仅会自动为表中已有的标签列填充 null 值,而且对于未来新增的标签列,同样会自动填充 null。 + +### 1.5 多行写入 + +支持同时写入多行数据,提高数据写入效率。 + +**示例:** + +```SQL +insert into table1 +values +(4, 北京', '3001', '3', '1', '10', 90.0, 1200.0), +(5, 北京', '3001', '3', '1', '10', 90.0, 1200.0); + + +insert into table1 +("地区", "厂号", "设备号", Time, "温度", "排量") +values +('北京', '3001', '3', 4, 90.0, 1200.0), +('北京', '3001', '3', 5, 90.0, 1200.0); +``` + +#### 注意事项 + +- 如果在 SQL 语句中引用了表中不存在的列,IoTDB 将返回错误码 `COLUMN_NOT_EXIST(616)`。 +- 如果写入的数据类型与表中列的数据类型不一致,将报错 `DATA_TYPE_MISMATCH(507)`。 + +### 1.6 查询写回 + +IoTDB 表模型支持追加查询写回功能,即`INSERT INTO QUERY` 语句,支持将查询结果写入**已经存在**的表中。 + +> 注意:该功能从 V 2.0.6 版本开始提供。 + +#### 1.6.1 语法定义 + +```SQL +INSERT INTO table_name [ ( column [, ... ] ) ] query +``` + +其中**​ ​query** 支持三种形式,下面将通过示例进行说明。 + +以[示例数据](../Reference/Sample-Data.md)为源数据,先创建目标表 + +```SQL +IoTDB:database1> CREATE TABLE target_table ( time TIMESTAMP TIME, region STRING TAG, device_id STRING TAG, temperature FLOAT FIELD ); +Msg: The statement is executed successfully. +``` + +1. 通过标准查询语句写回 + +即 query 处为直接通过`select ... from ...`执行的查询。 + +例如:使用标准查询语句,将 table1 中北京地区的 time, region, device\_id, temperature 数据查询写回到 target\_table 中 + +```SQL +IoTDB:database1> insert into target_table select time,region,device_id,temperature from table1 where region = '北京' +Msg: The statement is executed successfully. +IoTDB:database1> select * from target_table where region='北京' ++-----------------------------+------+---------+-----------+ +| time|region|device_id|temperature| ++-----------------------------+------+---------+-----------+ +|2024-11-26T13:37:00.000+08:00| 北京| 100| 90.0| +|2024-11-26T13:38:00.000+08:00| 北京| 100| 90.0| +|2024-11-27T16:38:00.000+08:00| 北京| 101| null| +|2024-11-27T16:39:00.000+08:00| 北京| 101| 85.0| +|2024-11-27T16:40:00.000+08:00| 北京| 101| 85.0| +|2024-11-27T16:41:00.000+08:00| 北京| 101| 85.0| +|2024-11-27T16:42:00.000+08:00| 北京| 101| null| +|2024-11-27T16:43:00.000+08:00| 北京| 101| null| +|2024-11-27T16:44:00.000+08:00| 北京| 101| null| ++-----------------------------+------+---------+-----------+ +Total line number = 9 +It costs 0.029s +``` + +2. 通过表引用查询写回 + +即 query 处为表引用方式`table source_table`。 + +例如:使用表引用查询,将 table3 中的数据查询写回到 target\_table 中 + +```SQL +IoTDB:database1> insert into target_table(time,device_id,temperature) table table3 +Msg: The statement is executed successfully. +IoTDB:database1> select * from target_table where region is null ++-----------------------------+------+---------+-----------+ +| time|region|device_id|temperature| ++-----------------------------+------+---------+-----------+ +|2025-05-13T00:00:00.001+08:00| null| d1| 90.0| +|2025-05-13T00:00:01.002+08:00| null| d1| 85.0| +|2025-05-13T00:00:02.101+08:00| null| d1| 85.0| +|2025-05-13T00:00:03.201+08:00| null| d1| null| +|2025-05-13T00:00:04.105+08:00| null| d1| 90.0| +|2025-05-13T00:00:05.023+08:00| null| d1| 85.0| +|2025-05-13T00:00:06.129+08:00| null| d1| 90.0| ++-----------------------------+------+---------+-----------+ +Total line number = 7 +It costs 0.015s +``` + +3. 通过子查询写回 + +即 query 处为带括号的子查询。 + +例如:使用子查询,将 table1 中时间与 table2 上海地区记录匹配的数据的 time, region, device\_id, temperature 查询写回到 target\_table + +```SQL +IoTDB:database1> insert into target_table (select t1.time, t1.region as region, t1.device_id as device_id, t1.temperature as temperature from table1 t1 where t1.time in (select t2.time from table2 t2 where t2.region = '上海')) +Msg: The statement is executed successfully. +IoTDB:database1> select * from target_table where region = '上海' ++-----------------------------+------+---------+-----------+ +| time|region|device_id|temperature| ++-----------------------------+------+---------+-----------+ +|2024-11-28T08:00:00.000+08:00| 上海| 100| 85.0| +|2024-11-29T11:00:00.000+08:00| 上海| 100| null| ++-----------------------------+------+---------+-----------+ +Total line number = 2 +It costs 0.014s +``` + +#### 1.6.2 相关说明 + +* 允许 query 中的源表与目标表 table\_name 是同一个表,例如:`INSERT INTO testtb SELECT * FROM testtb`。 +* 目标表必须已存在,否则提示错误信息`550: Table 'xxx.xxx' does not exist`。 +* 查询返回列和目标表列的数量和类型需完全匹配,目前不支持兼容类型的转换,若类型不匹配则提示错误信息 `701: Insert query has mismatched column types`。 +* 允许指定目标表的部分列,指定目标表列名时需符合以下规则: + * 必须包含时间戳列,否则提示错误信息`701: time column can not be null` + * 必须包含至少一个 FIELD 列,否则提示错误信息`701: No Field column present` + * 允许不指定 TAG 列 + * 允许指定列数少于目标表列数,缺失列自动补为 NULL 值 +* JAVA 支持使用 [executeNonQueryStatement](../API/Programming-Java-Native-API_apache.md#_3-1-itablesession接口) 方法执行`INSERT INTO QUERY`。 +* REST 支持[/rest/table/v1/nonQuery](../API/RestServiceV1.md#_3-3-非查询接口)API 执行`INSERT INTO QUERY`。 +* `INSERT INTO QUERY`不支持 Explain 和 Explain Analyze。 +* 用户必须有下列权限才能正常执行查询写回语句: + * 对查询语句中的源表具有 `SELECT` 权限。 + * 对目标表具有`WRITE`权限。 + * 更多用户权限相关的内容,请参考[权限管理](../User-Manual/Authority-Management_apache.md)。 + + +## 2. 数据更新 + +### 2.1 语法 + +```SQL +UPDATE SET updateAssignment (',' updateAssignment)* (WHERE where=booleanExpression)? + +updateAssignment + : identifier EQ expression + ; +``` + +1. `update`语句仅允许修改属性(ATTRIBUTE)列的值。 +2. `WHERE` 的规则: + - 范围仅限于标签列(TAG)和属性列(ATTRIBUTE),不允许涉及测点列(FIELD)和时间列(TIME)。 + - 不允许使用聚合函数 +3. 执行 SET 操作后,赋值表达式的结果应当是字符串类型,且其使用的限制应与 WHERE 子句中的表达式相同。 +4. 属性(ATTRIBUTE)列以及测点(FIELD)列的值也可通过`insert`语句来实现指定行的更新。 + +**示例:** + +```SQL +update table1 set b = a where substring(a, 1, 1) like '%' +``` \ No newline at end of file diff --git a/src/zh/UserGuide/latest-Table/Basic-Concept/Write-Updata-Data_timecho.md b/src/zh/UserGuide/latest-Table/Basic-Concept/Write-Updata-Data_timecho.md new file mode 100644 index 000000000..2cf792b56 --- /dev/null +++ b/src/zh/UserGuide/latest-Table/Basic-Concept/Write-Updata-Data_timecho.md @@ -0,0 +1,317 @@ + + +# 写入&更新 + +## 1. 数据写入 + +### 1.1 语法 + +在 IoTDB 中,数据写入遵循以下通用语法: + +```SQL +INSERT INTO [(COLUMN_NAME[, COLUMN_NAME]*)]? VALUES (COLUMN_VALUE[, COLUMN_VALUE]*) +``` + +基本约束包括: + +1. 通过 insert 语句写入无法自动创建表。 +2. 未指定的标签列将自动填充为 `null`。 +3. 未包含时间戳,系统将使用当前时间 `now()` 进行填充。 +4. 若当前设备(由标识信息定位)不存在该属性列的值,执行写入操作将导致原有的空值(NULL)被写入的数据所替代。 +5. 若当前设备(由标识信息定位)已有属性列的值,再次写入相同的属性列时,系统将更新该属性列的值为新数据。 +6. 写入重复时间戳,原时间戳对应列的值会更新。 +7. 若 INSERT 语句未指定列名(如 INSERT INTO table VALUES (...)),则 VALUES中的值必须严格按表中列的物理顺序排列(顺序可通过 DESC table命令查看)。 + +由于属性一般并不随时间的变化而变化,因此推荐以 update 的方式单独更新属性值,参见下文 [数据更新](#数据更新)。 + +
+ +
+ + +### 1.2 通过 Session 写入自动创建表 + +在通过 Session 进行数据写入时,IoTDB 支持无模式写入:无需事先手动创建表,系统会根据写入请求中的信息自动构建表结构,之后直接执行数据写入操作。 + +**示例:** + +```Java +try (ITableSession session = + new TableSessionBuilder() + .nodeUrls(Collections.singletonList("127.0.0.1:6667")) + .username("root") + .password("root") + .build()) { + + session.executeNonQueryStatement("CREATE DATABASE db1"); + session.executeNonQueryStatement("use db1"); + + // 不创建表直接写入数据 + List columnNameList = + Arrays.asList("region_id", "plant_id", "device_id", "model", "temperature", "humidity"); + List dataTypeList = + Arrays.asList( + TSDataType.STRING, + TSDataType.STRING, + TSDataType.STRING, + TSDataType.STRING, + TSDataType.FLOAT, + TSDataType.DOUBLE); + List columnTypeList = + new ArrayList<>( + Arrays.asList( + ColumnCategory.TAG, + ColumnCategory.TAG, + ColumnCategory.TAG, + ColumnCategory.ATTRIBUTE, + ColumnCategory.FIELD, + ColumnCategory.FIELD)); + Tablet tablet = new Tablet("table1", columnNameList, dataTypeList, columnTypeList, 100); + for (long timestamp = 0; timestamp < 100; timestamp++) { + int rowIndex = tablet.getRowSize(); + tablet.addTimestamp(rowIndex, timestamp); + tablet.addValue("region_id", rowIndex, "1"); + tablet.addValue("plant_id", rowIndex, "5"); + tablet.addValue("device_id", rowIndex, "3"); + tablet.addValue("model", rowIndex, "A"); + tablet.addValue("temperature", rowIndex, 37.6F); + tablet.addValue("humidity", rowIndex, 111.1); + if (tablet.getRowSize() == tablet.getMaxRowNumber()) { + session.insert(tablet); + tablet.reset(); + } + } + if (tablet.getRowSize() != 0) { + session.insert(tablet); + tablet.reset(); + } +} +``` + +在代码执行完成后,可以通过下述语句确认表已成功创建,其中包含了时间列、标签列、属性列以及测点列等各类信息。 + +```SQL +IoTDB> desc table1 ++-----------+---------+-----------+ +| ColumnName| DataType| Category| ++-----------+---------+-----------+ +| time|TIMESTAMP| TIME| +| region_id| STRING| TAG| +| plant_id| STRING| TAG| +| device_id| STRING| TAG| +| model| STRING| ATTRIBUTE| +|temperature| FLOAT| FIELD| +| humidity| DOUBLE| FIELD| ++-----------+---------+-----------+ +``` + +### 1.3 指定列写入 + +在写入操作中,可以指定部分列,未指定的列将不会被写入任何内容(即设置为 `null`)。 + +**示例:** + +```SQL +insert into table1("地区", "厂号", "设备号", Time, "温度", "排量") values ('湖南', '3001', '3', 4, 90.0, 1200.0) + + +insert into table1("地区", "厂号", "设备号", Time, "温度") values ('湖南, '3001', '3', 5, 90.0) +``` + +### 1.4 空值写入 + +标签列、属性列和测点列可以指定空值(`null`),表示不写入任何内容。 + +**示例(与上述事例等价):** + +```SQL +# 上述部分列写入等价于如下的带空值写入 +insert into table1("地区", "厂号", "设备号", "型号", "维修周期", Time, "温度", "排量") values ('湖南', '3001', '3', null, null, 4, 90.0, 1200.0) + +insert into table1("地区", "厂号", "设备号", "型号", "维修周期", Time, "温度", "排量") values ('湖南', '3001', '3', null, null, 5, 90.0, null) +``` + +当向不包含任何标签列的表中写入数据时,系统将默认创建一个所有标签列值均为 null 的device。 + +> 注意,该操作不仅会自动为表中已有的标签列填充 null 值,而且对于未来新增的标签列,同样会自动填充 null。 + +### 1.5 多行写入 + +支持同时写入多行数据,提高数据写入效率。 + +**示例:** + +```SQL +insert into table1 +values +(4, 北京', '3001', '3', '1', '10', 90.0, 1200.0), +(5, 北京', '3001', '3', '1', '10', 90.0, 1200.0); + + +insert into table1 +("地区", "厂号", "设备号", Time, "温度", "排量") +values +('北京', '3001', '3', 4, 90.0, 1200.0), +('北京', '3001', '3', 5, 90.0, 1200.0); +``` + +#### 注意事项 + +- 如果在 SQL 语句中引用了表中不存在的列,IoTDB 将返回错误码 `COLUMN_NOT_EXIST(616)`。 +- 如果写入的数据类型与表中列的数据类型不一致,将报错 `DATA_TYPE_MISMATCH(507)`。 + +### 1.6 查询写回 + +IoTDB 表模型支持追加查询写回功能,即`INSERT INTO QUERY` 语句,支持将查询结果写入**已经存在**的表中。 + +> 注意:该功能从 V 2.0.6 版本开始提供。 + +#### 1.6.1 语法定义 + +```SQL +INSERT INTO table_name [ ( column [, ... ] ) ] query +``` + +其中**​ ​query** 支持三种形式,下面将通过示例进行说明。 + +以[示例数据](../Reference/Sample-Data.md)为源数据,先创建目标表 + +```SQL +IoTDB:database1> CREATE TABLE target_table ( time TIMESTAMP TIME, region STRING TAG, device_id STRING TAG, temperature FLOAT FIELD ); +Msg: The statement is executed successfully. +``` + +1. 通过标准查询语句写回 + +即 query 处为直接通过`select ... from ...`执行的查询。 + +例如:使用标准查询语句,将 table1 中北京地区的 time, region, device\_id, temperature 数据查询写回到 target\_table 中 + +```SQL +IoTDB:database1> insert into target_table select time,region,device_id,temperature from table1 where region = '北京' +Msg: The statement is executed successfully. +IoTDB:database1> select * from target_table where region='北京' ++-----------------------------+------+---------+-----------+ +| time|region|device_id|temperature| ++-----------------------------+------+---------+-----------+ +|2024-11-26T13:37:00.000+08:00| 北京| 100| 90.0| +|2024-11-26T13:38:00.000+08:00| 北京| 100| 90.0| +|2024-11-27T16:38:00.000+08:00| 北京| 101| null| +|2024-11-27T16:39:00.000+08:00| 北京| 101| 85.0| +|2024-11-27T16:40:00.000+08:00| 北京| 101| 85.0| +|2024-11-27T16:41:00.000+08:00| 北京| 101| 85.0| +|2024-11-27T16:42:00.000+08:00| 北京| 101| null| +|2024-11-27T16:43:00.000+08:00| 北京| 101| null| +|2024-11-27T16:44:00.000+08:00| 北京| 101| null| ++-----------------------------+------+---------+-----------+ +Total line number = 9 +It costs 0.029s +``` + +2. 通过表引用查询写回 + +即 query 处为表引用方式`table source_table`。 + +例如:使用表引用查询,将 table3 中的数据查询写回到 target\_table 中 + +```SQL +IoTDB:database1> insert into target_table(time,device_id,temperature) table table3 +Msg: The statement is executed successfully. +IoTDB:database1> select * from target_table where region is null ++-----------------------------+------+---------+-----------+ +| time|region|device_id|temperature| ++-----------------------------+------+---------+-----------+ +|2025-05-13T00:00:00.001+08:00| null| d1| 90.0| +|2025-05-13T00:00:01.002+08:00| null| d1| 85.0| +|2025-05-13T00:00:02.101+08:00| null| d1| 85.0| +|2025-05-13T00:00:03.201+08:00| null| d1| null| +|2025-05-13T00:00:04.105+08:00| null| d1| 90.0| +|2025-05-13T00:00:05.023+08:00| null| d1| 85.0| +|2025-05-13T00:00:06.129+08:00| null| d1| 90.0| ++-----------------------------+------+---------+-----------+ +Total line number = 7 +It costs 0.015s +``` + +3. 通过子查询写回 + +即 query 处为带括号的子查询。 + +例如:使用子查询,将 table1 中时间与 table2 上海地区记录匹配的数据的 time, region, device\_id, temperature 查询写回到 target\_table + +```SQL +IoTDB:database1> insert into target_table (select t1.time, t1.region as region, t1.device_id as device_id, t1.temperature as temperature from table1 t1 where t1.time in (select t2.time from table2 t2 where t2.region = '上海')) +Msg: The statement is executed successfully. +IoTDB:database1> select * from target_table where region = '上海' ++-----------------------------+------+---------+-----------+ +| time|region|device_id|temperature| ++-----------------------------+------+---------+-----------+ +|2024-11-28T08:00:00.000+08:00| 上海| 100| 85.0| +|2024-11-29T11:00:00.000+08:00| 上海| 100| null| ++-----------------------------+------+---------+-----------+ +Total line number = 2 +It costs 0.014s +``` + +#### 1.6.2 相关说明 + +* 允许 query 中的源表与目标表 table\_name 是同一个表,例如:`INSERT INTO testtb SELECT * FROM testtb`。 +* 目标表必须已存在,否则提示错误信息`550: Table 'xxx.xxx' does not exist`。 +* 查询返回列和目标表列的数量和类型需完全匹配,目前不支持兼容类型的转换,若类型不匹配则提示错误信息 `701: Insert query has mismatched column types`。 +* 允许指定目标表的部分列,指定目标表列名时需符合以下规则: + * 必须包含时间戳列,否则提示错误信息`701: time column can not be null` + * 必须包含至少一个 FIELD 列,否则提示错误信息`701: No Field column present` + * 允许不指定 TAG 列 + * 允许指定列数少于目标表列数,缺失列自动补为 NULL 值 +* JAVA 支持使用 [executeNonQueryStatement](../API/Programming-Java-Native-API_timecho.md#_3-1-itablesession接口) 方法执行`INSERT INTO QUERY`。 +* REST 支持[/rest/table/v1/nonQuery](../API/RestServiceV1.md#_3-3-非查询接口)API 执行`INSERT INTO QUERY`。 +* `INSERT INTO QUERY`不支持 Explain 和 Explain Analyze。 +* 用户必须有下列权限才能正常执行查询写回语句: + * 对查询语句中的源表具有 `SELECT` 权限。 + * 对目标表具有`WRITE`权限。 + * 更多用户权限相关的内容,请参考[权限管理](../User-Manual/Authority-Management_timecho.md)。 + + +## 2. 数据更新 + +### 2.1 语法 + +```SQL +UPDATE SET updateAssignment (',' updateAssignment)* (WHERE where=booleanExpression)? + +updateAssignment + : identifier EQ expression + ; +``` + +1. `update`语句仅允许修改属性(ATTRIBUTE)列的值。 +2. `WHERE` 的规则: + - 范围仅限于标签列(TAG)和属性列(ATTRIBUTE),不允许涉及测点列(FIELD)和时间列(TIME)。 + - 不允许使用聚合函数 +3. 执行 SET 操作后,赋值表达式的结果应当是字符串类型,且其使用的限制应与 WHERE 子句中的表达式相同。 +4. 属性(ATTRIBUTE)列以及测点(FIELD)列的值也可通过`insert`语句来实现指定行的更新。 + +**示例:** + +```SQL +update table1 set b = a where substring(a, 1, 1) like '%' +``` \ No newline at end of file diff --git a/src/zh/UserGuide/latest-Table/QuickStart/QuickStart_apache.md b/src/zh/UserGuide/latest-Table/QuickStart/QuickStart_apache.md index 8fbf5e008..f8c960ed5 100644 --- a/src/zh/UserGuide/latest-Table/QuickStart/QuickStart_apache.md +++ b/src/zh/UserGuide/latest-Table/QuickStart/QuickStart_apache.md @@ -53,7 +53,7 @@ - 表介绍:[表管理](../Basic-Concept/Table-Management.md) -2. 数据写入&更新:在数据写入&更新方面,IoTDB 提供了多种方式来插入实时数据,基本的数据写入&更新操作请查看 [数据写入&更新](../Basic-Concept/Write-Updata-Data.md) +2. 数据写入&更新:在数据写入&更新方面,IoTDB 提供了多种方式来插入实时数据,支持追加查询写回,基本介绍请查看 [数据写入&更新](../Basic-Concept/Write-Updata-Data_apache.md) 3. 数据查询:IoTDB 提供了丰富的数据查询功能,数据查询的基本介绍请查看 [数据查询](../Basic-Concept/Query-Data_apache.md) diff --git a/src/zh/UserGuide/latest-Table/QuickStart/QuickStart_timecho.md b/src/zh/UserGuide/latest-Table/QuickStart/QuickStart_timecho.md index 08d1fac39..6481213fc 100644 --- a/src/zh/UserGuide/latest-Table/QuickStart/QuickStart_timecho.md +++ b/src/zh/UserGuide/latest-Table/QuickStart/QuickStart_timecho.md @@ -60,7 +60,7 @@ - 表介绍:[表管理](../Basic-Concept/Table-Management.md) -2. 数据写入&更新:在数据写入&更新方面,IoTDB 提供了多种方式来插入实时数据,基本的数据写入&更新操作请查看 [数据写入&更新](../Basic-Concept/Write-Updata-Data.md) +2. 数据写入&更新:在数据写入&更新方面,IoTDB 提供了多种方式来插入实时数据,支持追加查询写回,基本介绍请查看 [数据写入&更新](../Basic-Concept/Write-Updata-Data_timecho.md) 3. 数据查询:IoTDB 提供了丰富的数据查询功能,数据查询的基本介绍请查看 [数据查询](../Basic-Concept/Query-Data_timecho.md),其中包含了适用于识别时序数据中的特定模式、检测特定事件等业务场景的[行模式识别](../SQL-Manual/Row-Pattern-Recognition.md),以及常用于数据分析场景的[窗口函数](../SQL-Manual/Featured-Functions_timecho.md#_4-窗口函数)等特色函数 diff --git a/src/zh/UserGuide/latest-Table/SQL-Manual/SQL-Data-Addition-Deletion.md b/src/zh/UserGuide/latest-Table/SQL-Manual/SQL-Data-Addition-Deletion.md index 35abe3691..84dc187da 100644 --- a/src/zh/UserGuide/latest-Table/SQL-Manual/SQL-Data-Addition-Deletion.md +++ b/src/zh/UserGuide/latest-Table/SQL-Manual/SQL-Data-Addition-Deletion.md @@ -1,3 +1,6 @@ +--- +redirectTo: SQL-Data-Addition-Deletion_apache.html +--- - -# 数据增删 - -## 1. 数据写入 - -**语法:** - -```SQL -INSERT INTO [(COLUMN_NAME[, COLUMN_NAME]*)]? VALUES (COLUMN_VALUE[, COLUMN_VALUE]*) -``` - -更多详细语法说明请参考:[写入语法](../Basic-Concept/Write-Updata-Data.md#_1-1-语法) - -**示例一:指定列写入** - -```SQL -insert into table1("地区", "厂号", "设备号", Time, "温度", "排量") values ('湖南', '3001', '3', 4, 90.0, 1200.0) - -insert into table1("地区", "厂号", "设备号", Time, "温度") values ('湖南, '3001', '3', 5, 90.0) -``` - -**示例二:空值写入** - -```SQL --- 上述部分列写入等价于如下的带空值写入 -insert into table1("地区", "厂号", "设备号", "型号", "维修周期", Time, "温度", "排量") values ('湖南', '3001', '3', null, null, 4, 90.0, 1200.0) - -insert into table1("地区", "厂号", "设备号", "型号", "维修周期", Time, "温度", "排量") values ('湖南', '3001', '3', null, null, 5, 90.0, null) -``` - -**示例三:多行写入** - -```SQL -insert into table1 -values -(4, '北京', '3001', '3', '1', '10', 90.0, 1200.0), -(5, '北京', '3001', '3', '1', '10', 90.0, 1200.0); - -insert into table1 -("地区", "厂号", "设备号", Time, "温度", "排量") -values -('北京', '3001', '3', 4, 90.0, 1200.0), -('北京', '3001', '3', 5, 90.0, 1200.0); -``` - -## 2. 数据更新 - -**语法:** - -```SQL -UPDATE SET updateAssignment (',' updateAssignment)* (WHERE where=booleanExpression)? - -updateAssignment - : identifier EQ expression - ; -``` - -更多详细语法说明请参考:[更新语法](../Basic-Concept/Write-Updata-Data.md#_2-1-语法) - -**示例:** - -```SQL -update table1 set b = a where substring(a, 1, 1) like '%' -``` - -## 3. 数据删除 - -**语法:** - -```SQL -DELETE FROM [WHERE_CLAUSE]? - -WHERE_CLAUSE: - WHERE DELETE_CONDITION - -DELETE_CONDITION: - SINGLE_CONDITION - | DELETE_CONDITION AND DELETE_CONDITION - | DELETE_CONDITION OR DELETE_CONDITION - -SINGLE_CODITION: - TIME_CONDITION | ID_CONDITION - -TIME_CONDITION: - time TIME_OPERATOR LONG_LITERAL - -TIME_OPERATOR: - < | > | <= | >= | = - -ID_CONDITION: - identifier = STRING_LITERAL -``` - -**示例一: 删除全表数据** - -```SQL --- 全表删除 -DELETE FROM table1 -``` - -**示例二:删除一段时间范围内的数据** - -```SQL --- 单时间段数据删除 -DELETE FROM table1 WHERE time <= 2024-11-29 00:00:00 - --- 多时间段数据删除 -DELETE FROM table1 WHERE time >= 2024-11-27 00:00:00 and time <= 2024-11-29 00:00:00 -``` - -**示例三:删除指定设备的数据** - -```SQL --- 删除指定设备的数据 -DELETE FROM table1 WHERE device_id='101' and model_id = 'B' - --- 删除指定设备及时间段的数据 -DELETE FROM table1 - WHERE time >= 2024-11-27 16:39:00 and time <= 2024-11-29 16:42:00 - AND device_id='101' and model_id = 'B' - --- 删除指定类型设备的数据 -DELETE FROM table1 WHERE model_id = 'B' -``` - -## 4. 设备删除 - -**语法:** - -```SQL -DELETE DEVICES FROM tableName=qualifiedName (WHERE booleanExpression)? -``` - -**示例:删除指定设备及其相关的所有数据** - -```SQL -DELETE DEVICES FROM table1 WHERE device_id = '101' -``` diff --git a/src/zh/UserGuide/latest-Table/SQL-Manual/SQL-Data-Addition-Deletion_apache.md b/src/zh/UserGuide/latest-Table/SQL-Manual/SQL-Data-Addition-Deletion_apache.md new file mode 100644 index 000000000..423898996 --- /dev/null +++ b/src/zh/UserGuide/latest-Table/SQL-Manual/SQL-Data-Addition-Deletion_apache.md @@ -0,0 +1,168 @@ + + +# 数据增删 + +## 1. 数据写入 + +**语法:** + +```SQL +INSERT INTO [(COLUMN_NAME[, COLUMN_NAME]*)]? VALUES (COLUMN_VALUE[, COLUMN_VALUE]*) +``` + +更多详细语法说明请参考:[写入语法](../Basic-Concept/Write-Updata-Data_apache.md#_1-1-语法) + +**示例一:指定列写入** + +```SQL +insert into table1("地区", "厂号", "设备号", Time, "温度", "排量") values ('湖南', '3001', '3', 4, 90.0, 1200.0) + +insert into table1("地区", "厂号", "设备号", Time, "温度") values ('湖南, '3001', '3', 5, 90.0) +``` + +**示例二:空值写入** + +```SQL +-- 上述部分列写入等价于如下的带空值写入 +insert into table1("地区", "厂号", "设备号", "型号", "维修周期", Time, "温度", "排量") values ('湖南', '3001', '3', null, null, 4, 90.0, 1200.0) + +insert into table1("地区", "厂号", "设备号", "型号", "维修周期", Time, "温度", "排量") values ('湖南', '3001', '3', null, null, 5, 90.0, null) +``` + +**示例三:多行写入** + +```SQL +insert into table1 +values +(4, '北京', '3001', '3', '1', '10', 90.0, 1200.0), +(5, '北京', '3001', '3', '1', '10', 90.0, 1200.0); + +insert into table1 +("地区", "厂号", "设备号", Time, "温度", "排量") +values +('北京', '3001', '3', 4, 90.0, 1200.0), +('北京', '3001', '3', 5, 90.0, 1200.0); +``` + +**示例四:查询写回** + +```SQL +insert into target_table select time,region,device_id,temperature from table1 where region = '北京'; + +insert into target_table(time,device_id,temperature) table table3; + +insert into target_table (select t1.time, t1.region as region, t1.device_id as device_id, t1.temperature as temperature from table1 t1 where t1.time in (select t2.time from table2 t2 where t2.region = '上海')); +``` + +## 2. 数据更新 + +**语法:** + +```SQL +UPDATE SET updateAssignment (',' updateAssignment)* (WHERE where=booleanExpression)? + +updateAssignment + : identifier EQ expression + ; +``` + +更多详细语法说明请参考:[更新语法](../Basic-Concept/Write-Updata-Data_apache.md#_2-1-语法) + +**示例:** + +```SQL +update table1 set b = a where substring(a, 1, 1) like '%' +``` + +## 3. 数据删除 + +**语法:** + +```SQL +DELETE FROM [WHERE_CLAUSE]? + +WHERE_CLAUSE: + WHERE DELETE_CONDITION + +DELETE_CONDITION: + SINGLE_CONDITION + | DELETE_CONDITION AND DELETE_CONDITION + | DELETE_CONDITION OR DELETE_CONDITION + +SINGLE_CODITION: + TIME_CONDITION | ID_CONDITION + +TIME_CONDITION: + time TIME_OPERATOR LONG_LITERAL + +TIME_OPERATOR: + < | > | <= | >= | = + +ID_CONDITION: + identifier = STRING_LITERAL +``` + +**示例一: 删除全表数据** + +```SQL +-- 全表删除 +DELETE FROM table1 +``` + +**示例二:删除一段时间范围内的数据** + +```SQL +-- 单时间段数据删除 +DELETE FROM table1 WHERE time <= 2024-11-29 00:00:00 + +-- 多时间段数据删除 +DELETE FROM table1 WHERE time >= 2024-11-27 00:00:00 and time <= 2024-11-29 00:00:00 +``` + +**示例三:删除指定设备的数据** + +```SQL +-- 删除指定设备的数据 +DELETE FROM table1 WHERE device_id='101' and model_id = 'B' + +-- 删除指定设备及时间段的数据 +DELETE FROM table1 + WHERE time >= 2024-11-27 16:39:00 and time <= 2024-11-29 16:42:00 + AND device_id='101' and model_id = 'B' + +-- 删除指定类型设备的数据 +DELETE FROM table1 WHERE model_id = 'B' +``` + +## 4. 设备删除 + +**语法:** + +```SQL +DELETE DEVICES FROM tableName=qualifiedName (WHERE booleanExpression)? +``` + +**示例:删除指定设备及其相关的所有数据** + +```SQL +DELETE DEVICES FROM table1 WHERE device_id = '101' +``` diff --git a/src/zh/UserGuide/latest-Table/SQL-Manual/SQL-Data-Addition-Deletion_timecho.md b/src/zh/UserGuide/latest-Table/SQL-Manual/SQL-Data-Addition-Deletion_timecho.md new file mode 100644 index 000000000..f24b1e923 --- /dev/null +++ b/src/zh/UserGuide/latest-Table/SQL-Manual/SQL-Data-Addition-Deletion_timecho.md @@ -0,0 +1,168 @@ + + +# 数据增删 + +## 1. 数据写入 + +**语法:** + +```SQL +INSERT INTO [(COLUMN_NAME[, COLUMN_NAME]*)]? VALUES (COLUMN_VALUE[, COLUMN_VALUE]*) +``` + +更多详细语法说明请参考:[写入语法](../Basic-Concept/Write-Updata-Data_timecho.md#_1-1-语法) + +**示例一:指定列写入** + +```SQL +insert into table1("地区", "厂号", "设备号", Time, "温度", "排量") values ('湖南', '3001', '3', 4, 90.0, 1200.0) + +insert into table1("地区", "厂号", "设备号", Time, "温度") values ('湖南, '3001', '3', 5, 90.0) +``` + +**示例二:空值写入** + +```SQL +-- 上述部分列写入等价于如下的带空值写入 +insert into table1("地区", "厂号", "设备号", "型号", "维修周期", Time, "温度", "排量") values ('湖南', '3001', '3', null, null, 4, 90.0, 1200.0) + +insert into table1("地区", "厂号", "设备号", "型号", "维修周期", Time, "温度", "排量") values ('湖南', '3001', '3', null, null, 5, 90.0, null) +``` + +**示例三:多行写入** + +```SQL +insert into table1 +values +(4, '北京', '3001', '3', '1', '10', 90.0, 1200.0), +(5, '北京', '3001', '3', '1', '10', 90.0, 1200.0); + +insert into table1 +("地区", "厂号", "设备号", Time, "温度", "排量") +values +('北京', '3001', '3', 4, 90.0, 1200.0), +('北京', '3001', '3', 5, 90.0, 1200.0); +``` + +**示例四:查询写回** + +```SQL +insert into target_table select time,region,device_id,temperature from table1 where region = '北京'; + +insert into target_table(time,device_id,temperature) table table3; + +insert into target_table (select t1.time, t1.region as region, t1.device_id as device_id, t1.temperature as temperature from table1 t1 where t1.time in (select t2.time from table2 t2 where t2.region = '上海')); +``` + +## 2. 数据更新 + +**语法:** + +```SQL +UPDATE SET updateAssignment (',' updateAssignment)* (WHERE where=booleanExpression)? + +updateAssignment + : identifier EQ expression + ; +``` + +更多详细语法说明请参考:[更新语法](../Basic-Concept/Write-Updata-Data_timecho.md#_2-1-语法) + +**示例:** + +```SQL +update table1 set b = a where substring(a, 1, 1) like '%' +``` + +## 3. 数据删除 + +**语法:** + +```SQL +DELETE FROM [WHERE_CLAUSE]? + +WHERE_CLAUSE: + WHERE DELETE_CONDITION + +DELETE_CONDITION: + SINGLE_CONDITION + | DELETE_CONDITION AND DELETE_CONDITION + | DELETE_CONDITION OR DELETE_CONDITION + +SINGLE_CODITION: + TIME_CONDITION | ID_CONDITION + +TIME_CONDITION: + time TIME_OPERATOR LONG_LITERAL + +TIME_OPERATOR: + < | > | <= | >= | = + +ID_CONDITION: + identifier = STRING_LITERAL +``` + +**示例一: 删除全表数据** + +```SQL +-- 全表删除 +DELETE FROM table1 +``` + +**示例二:删除一段时间范围内的数据** + +```SQL +-- 单时间段数据删除 +DELETE FROM table1 WHERE time <= 2024-11-29 00:00:00 + +-- 多时间段数据删除 +DELETE FROM table1 WHERE time >= 2024-11-27 00:00:00 and time <= 2024-11-29 00:00:00 +``` + +**示例三:删除指定设备的数据** + +```SQL +-- 删除指定设备的数据 +DELETE FROM table1 WHERE device_id='101' and model_id = 'B' + +-- 删除指定设备及时间段的数据 +DELETE FROM table1 + WHERE time >= 2024-11-27 16:39:00 and time <= 2024-11-29 16:42:00 + AND device_id='101' and model_id = 'B' + +-- 删除指定类型设备的数据 +DELETE FROM table1 WHERE model_id = 'B' +``` + +## 4. 设备删除 + +**语法:** + +```SQL +DELETE DEVICES FROM tableName=qualifiedName (WHERE booleanExpression)? +``` + +**示例:删除指定设备及其相关的所有数据** + +```SQL +DELETE DEVICES FROM table1 WHERE device_id = '101' +```