01-sql-writing.mdx 4.6 KB
Newer Older
D
dingbo 已提交
1
---
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
2
sidebar_label: Insert Using SQL
G
gccgdb1234 已提交
3
title: Insert Using SQL
D
dingbo 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
---

import Tabs from "@theme/Tabs";
import TabItem from "@theme/TabItem";
import JavaSQL from "./_java_sql.mdx";
import JavaStmt from "./_java_stmt.mdx";
import PySQL from "./_py_sql.mdx";
import PyStmt from "./_py_stmt.mdx";
import GoSQL from "./_go_sql.mdx";
import GoStmt from "./_go_stmt.mdx";
import RustSQL from "./_rust_sql.mdx";
import RustStmt from "./_rust_stmt.mdx";
import NodeSQL from "./_js_sql.mdx";
import NodeStmt from "./_js_stmt.mdx";
import CsSQL from "./_cs_sql.mdx";
import CsStmt from "./_cs_stmt.mdx";
import CSQL from "./_c_sql.mdx";
import CStmt from "./_c_stmt.mdx";

G
gccgdb1234 已提交
23
## Introduction
D
dingbo 已提交
24

S
Sean Ely 已提交
25
Application programs can execute `INSERT` statement through connectors to insert rows. The TAOS CLI can also be used to manually insert data.
D
dingbo 已提交
26

G
gccgdb1234 已提交
27 28
### Insert Single Row

S
Sean Ely 已提交
29
The below SQL statement is used to insert one row into table "d1001".
D
dingbo 已提交
30 31 32 33 34

```sql
INSERT INTO d1001 VALUES (1538548685000, 10.3, 219, 0.31);
```

G
gccgdb1234 已提交
35
### Insert Multiple Rows
D
dingbo 已提交
36

S
Sean Ely 已提交
37
Multiple rows can be inserted in a single SQL statement. The example below inserts 2 rows into table "d1001".
D
dingbo 已提交
38 39 40 41 42

```sql
INSERT INTO d1001 VALUES (1538548684000, 10.2, 220, 0.23) (1538548696650, 10.3, 218, 0.25);
```

G
gccgdb1234 已提交
43
### Insert into Multiple Tables
D
dingbo 已提交
44

S
Sean Ely 已提交
45
Data can be inserted into multiple tables in the same SQL statement. The example below inserts 2 rows into table "d1001" and 1 row into table "d1002".
D
dingbo 已提交
46 47 48 49 50

```sql
INSERT INTO d1001 VALUES (1538548685000, 10.3, 219, 0.31) (1538548695000, 12.6, 218, 0.33) d1002 VALUES (1538548696800, 12.3, 221, 0.31);
```

G
gccgdb1234 已提交
51
For more details about `INSERT` please refer to [INSERT](/taos-sql/insert).
D
dingbo 已提交
52 53 54

:::info

S
Sean Ely 已提交
55 56
- Inserting in batches can improve performance. Normally, the higher the batch size, the better the performance. Please note that a single row can't exceed 16K bytes and each SQL statement can't exceed 1MB.
- Inserting with multiple threads can also improve performance. However, depending on the system resources on the application side and the server side, when the number of inserting threads grows beyond a specific point the performance may drop instead of improving. The proper number of threads needs to be tested in a specific environment to find the best number.
D
dingbo 已提交
57 58 59 60 61

:::

:::warning

S
Sean Ely 已提交
62
- If the timestamp for the row to be inserted already exists in the table, the behavior depends on the value of parameter `UPDATE`. If it's set to 0 (the default value), the row will be discarded. If it's set to 1, the new values will override the old values for the same row.
G
gccgdb1234 已提交
63
- The timestamp to be inserted must be newer than the timestamp of subtracting current time by the parameter `KEEP`. If `KEEP` is set to 3650 days, then the data older than 3650 days ago can't be inserted. The timestamp to be inserted can't be newer than the timestamp of current time plus parameter `DAYS`. If `DAYS` is set to 2, the data newer than 2 days later can't be inserted.
D
dingbo 已提交
64 65 66

:::

G
gccgdb1234 已提交
67
## Examples
D
dingbo 已提交
68

G
gccgdb1234 已提交
69
### Insert Using SQL
D
dingbo 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96

<Tabs defaultValue="java" groupId="lang">
  <TabItem label="Java" value="java">
    <JavaSQL />
  </TabItem>
  <TabItem label="Python" value="python">
    <PySQL />
  </TabItem>
  <TabItem label="Go" value="go">
    <GoSQL />
  </TabItem>
  <TabItem label="Rust" value="rust">
    <RustSQL />
  </TabItem>
  <TabItem label="Node.js" value="nodejs">
    <NodeSQL />
  </TabItem>
  <TabItem label="C#" value="csharp">
    <CsSQL />
  </TabItem>
  <TabItem label="C" value="c">
    <CSQL />
  </TabItem>
</Tabs>

:::note

G
gccgdb1234 已提交
97
1. With either native connection or REST connection, the above samples can work well.
S
Sean Ely 已提交
98
2. Please note that `use db` can't be used with a REST connection because REST connections are stateless, so in the samples `dbName.tbName` is used to specify the table name.
D
dingbo 已提交
99 100 101

:::

G
gccgdb1234 已提交
102
### Insert with Parameter Binding
D
dingbo 已提交
103

S
Sean Ely 已提交
104
TDengine also provides API support for parameter binding. Similar to MySQL, only `?` can be used in these APIs to represent the parameters to bind. From version 2.1.1.0 and 2.1.2.0, parameter binding support for inserting data has improved significantly to improve the insert performance by avoiding the cost of parsing SQL statements.
D
dingbo 已提交
105

G
gccgdb1234 已提交
106
Parameter binding is available only with native connection.
D
dingbo 已提交
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130

<Tabs defaultValue="java" groupId="lang">
  <TabItem label="Java" value="java">
    <JavaStmt />
  </TabItem>
  <TabItem label="Python" value="python">
    <PyStmt />
  </TabItem>
  <TabItem label="Go" value="go">
    <GoStmt />
  </TabItem>
  <TabItem label="Rust" value="rust">
    <RustStmt />
  </TabItem>
  <TabItem label="Node.js" value="nodejs">
    <NodeStmt />
  </TabItem>
  <TabItem label="C#" value="csharp">
    <CsStmt />
  </TabItem>
  <TabItem label="C" value="c">
    <CStmt />
  </TabItem>
</Tabs>