05-insert.md 7.0 KB
Newer Older
1
---
2
sidebar_label: Insert
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
title: Insert
---

## Syntax

```sql
INSERT INTO
    tb_name
        [USING stb_name [(tag1_name, ...)] TAGS (tag1_value, ...)]
        [(field1_name, ...)]
        VALUES (field1_value, ...) [(field1_value2, ...) ...] | FILE csv_file_path
    [tb2_name
        [USING stb_name [(tag1_name, ...)] TAGS (tag1_value, ...)]
        [(field1_name, ...)]
        VALUES (field1_value, ...) [(field1_value2, ...) ...] | FILE csv_file_path
    ...];
19

20
INSERT INTO tb_name [(field1_name, ...)] subquery
21 22
```

23
**Timestamps**
24

25
1. All data writes must include a timestamp. With regard to timestamps, note the following:
26

27
2. The precision of a timestamp depends on its format. The precision configured for the database affects only timestamps that are inserted as long integers (UNIX time). Timestamps inserted as date and time strings are not affected. As an example, the timestamp 2021-07-13 16:16:48 is equivalent to 1626164208 in UNIX time. This UNIX time is modified to 1626164208000 for databases with millisecond precision, 1626164208000000 for databases with microsecond precision, and 1626164208000000000 for databases with nanosecond precision.
28

29 30 31 32 33 34 35 36 37 38 39 40 41
3. If you want to insert multiple rows simultaneously, do not use the NOW function in the timestamp. Using the NOW function in this situation will cause multiple rows to have the same timestamp and prevent them from being stored correctly. This is because the NOW function obtains the current time on the client, and multiple instances of NOW in a single statement will return the same time.
   The earliest timestamp that you can use when inserting data is equal to the current time on the server minus the value of the KEEP parameter. The latest timestamp that you can use when inserting data is equal to the current time on the server plus the value of the DURATION parameter. You can configure the KEEP and DURATION parameters when you create a database. The default values are 3650 days for the KEEP parameter and 10 days for the DURATION parameter.

**Syntax**

1. The USING clause automatically creates the specified subtable if it does not exist. If it's unknown whether the table already exists, the table can be created automatically while inserting using the SQL statement below. To use this functionality, a STable must be used as template and tag values must be provided. Any tags that you do not specify will be assigned a null value.

2. You can insert data into specified columns. Any columns in which you do not insert data will be assigned a null value.

3. The VALUES clause inserts one or more rows of data into a table.

4. The FILE clause inserts tags or data from a comma-separates values (CSV) file. Do not include headers in your CSV files.

42
5. A single `INSERT ... VALUES` statement and `INSERT ... FILE` statement can write data to multiple tables.
43 44 45 46 47 48 49 50 51

6. The INSERT statement is fully parsed before being executed, so that if any element of the statement fails, the entire statement will fail. For example, the following statement will not create a table because the latter part of the statement is invalid:

   ```sql
   INSERT INTO d1001 USING meters TAGS('Beijing.Chaoyang', 2) VALUES('a');
   ```

7. However, an INSERT statement that writes data to multiple subtables can succeed for some tables and fail for others. This situation is caused because vnodes perform write operations independently of each other. One vnode failing to write data does not affect the ability of other vnodes to write successfully.

52
8. Data from TDengine can be inserted into a specified table using the `INSERT ... subquery` statement. Arbitrary query statements are supported. This syntax can only be used for subtables and normal tables, and does not support automatic table creation.
53

54 55 56 57 58
## Insert a Record

Single row or multiple rows specified with VALUES can be inserted into a specific table. A single row is inserted using the below statement.

```sql
59 60 61
INSERT INTO d1001 VALUES (NOW, 10.2, 219, 0.32);
```

62 63
## Insert Multiple Records

S
Sean Ely 已提交
64
Double rows are inserted using the below statement.
65 66 67 68 69

```sql
INSERT INTO d1001 VALUES ('2021-07-13 14:06:32.272', 10.2, 219, 0.32) (1626164208000, 10.15, 217, 0.33);
```

70
## Write to a Specified Column
71

72
Data can be inserted into specific columns, either single row or multiple row, while other columns will be inserted as NULL value. The key (timestamp) cannot be null. For example:
73

74
```sql
75 76 77 78 79
INSERT INTO d1001 (ts, current, phase) VALUES ('2021-07-13 14:06:33.196', 10.27, 0.31);
```

## Insert Into Multiple Tables

80
One or multiple rows can be inserted into multiple tables in a single SQL statement, with or without specifying specific columns. For example:
81 82 83 84 85 86 87 88

```sql
INSERT INTO d1001 VALUES ('2021-07-13 14:06:34.630', 10.2, 219, 0.32) ('2021-07-13 14:06:35.779', 10.15, 217, 0.33)
            d1002 (ts, current, phase) VALUES ('2021-07-13 14:06:34.255', 10.27, 0.31;
```

## Automatically Create Table When Inserting

89
If it's unknown whether the table already exists, the table can be created automatically while inserting using the SQL statement below. To use this functionality, a STable must be used as template and tag values must be provided. For example:
90 91

```sql
G
gccgdb1234 已提交
92
INSERT INTO d21001 USING meters TAGS ('California.SanFrancisco', 2) VALUES ('2021-07-13 14:06:32.272', 10.2, 219, 0.32);
93 94
```

95
It's not necessary to provide values for all tags when creating tables automatically, the tags without values provided will be set to NULL. For example:
96 97 98 99 100

```sql
INSERT INTO d21001 USING meters (groupId) TAGS (2) VALUES ('2021-07-13 14:06:33.196', 10.15, 217, 0.33);
```

101
Multiple rows can also be inserted into the same table in a single SQL statement. For example:
102 103

```sql
G
gccgdb1234 已提交
104
INSERT INTO d21001 USING meters TAGS ('California.SanFrancisco', 2) VALUES ('2021-07-13 14:06:34.630', 10.2, 219, 0.32) ('2021-07-13 14:06:35.779', 10.15, 217, 0.33)
105 106 107 108 109 110
            d21002 USING meters (groupId) TAGS (2) VALUES ('2021-07-13 14:06:34.255', 10.15, 217, 0.33)
            d21003 USING meters (groupId) TAGS (2) (ts, current, phase) VALUES ('2021-07-13 14:06:34.255', 10.27, 0.31);
```

## Insert Rows From A File

S
Sean Ely 已提交
111
Besides using `VALUES` to insert one or multiple rows, the data to be inserted can also be prepared in a CSV file with comma as separator and each field value quoted by single quotes. Table definition is not required in the CSV file. For example, if file "/tmp/csvfile.csv" contains the below data:
112 113 114 115 116 117

```
'2021-07-13 14:07:34.630', '10.2', '219', '0.32'
'2021-07-13 14:07:35.779', '10.15', '217', '0.33'
```

S
Sean Ely 已提交
118
Then data in this file can be inserted by the SQL statement below:
119 120 121 122 123 124 125 126

```sql
INSERT INTO d1001 FILE '/tmp/csvfile.csv';
```

## Create Tables Automatically and Insert Rows From File

```sql
G
gccgdb1234 已提交
127
INSERT INTO d21001 USING meters TAGS ('California.SanFrancisco', 2) FILE '/tmp/csvfile.csv';
128 129
```

130
When writing data from a file, you can automatically create the specified subtable if it does not exist. For example:
131 132

```sql
G
gccgdb1234 已提交
133
INSERT INTO d21001 USING meters TAGS ('California.SanFrancisco', 2) FILE '/tmp/csvfile_21001.csv'
134 135
            d21002 USING meters (groupId) TAGS (2) FILE '/tmp/csvfile_21002.csv';
```