Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/.vuepress/sidebar/V2.0.x/en-Table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions src/.vuepress/sidebar/V2.0.x/zh-Table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: '数据删除',
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions src/.vuepress/sidebar_timecho/V2.0.x/en-Table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions src/.vuepress/sidebar_timecho/V2.0.x/zh-Table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: '数据删除',
Expand Down Expand Up @@ -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,
Expand Down
192 changes: 3 additions & 189 deletions src/UserGuide/Master/Table/Basic-Concept/Write-Updata-Data.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
---
redirectTo: Write-Updata-Data_apache.html
---
<!--

Licensed to the Apache Software Foundation (ASF) under one
Expand All @@ -18,192 +21,3 @@
under the License.

-->

# Write & Update Data

## 1. Data Insertion

### 1.1 Syntax

In IoTDB, data insertion follows the general syntax:

```SQL
INSERT INTO <TABLE_NAME> [(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).

<div style="text-align: center;">
<img src="/img/%E6%95%B0%E6%8D%AE%E5%86%99%E5%85%A5%E8%8B%B1%E6%96%87.png" alt="" style="width: 70%;"/>
</div>


### 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<String> columnNameList =
Arrays.asList("region_id", "plant_id", "device_id", "model", "temperature", "humidity");
List<TSDataType> dataTypeList =
Arrays.asList(
TSDataType.STRING,
TSDataType.STRING,
TSDataType.STRING,
TSDataType.STRING,
TSDataType.FLOAT,
TSDataType.DOUBLE);
List<ColumnCategory> 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 <TABLE_NAME> 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 '%'
```
Loading