03-table.md 7.5 KB
Newer Older
1 2 3 4 5 6
---
title: Table
---

## Create Table

7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
You create standard tables and subtables with the `CREATE TABLE` statement.

```sql
CREATE TABLE [IF NOT EXISTS] [db_name.]tb_name (create_definition [, create_definitionn] ...) [table_options]
 
CREATE TABLE create_subtable_clause
 
CREATE TABLE [IF NOT EXISTS] [db_name.]tb_name (create_definition [, create_definitionn] ...)
    [TAGS (create_definition [, create_definitionn] ...)]
    [table_options]
 
create_subtable_clause: {
    create_subtable_clause [create_subtable_clause] ...
  | [IF NOT EXISTS] [db_name.]tb_name USING [db_name.]stb_name [(tag_name [, tag_name] ...)] TAGS (tag_value [, tag_value] ...)
}
 
create_definition:
    col_name column_definition
 
column_definition:
    type_name [comment 'string_value']
 
table_options:
    table_option ...
 
table_option: {
    COMMENT 'string_value'
  | WATERMARK duration[,duration]
  | MAX_DELAY duration[,duration]
  | ROLLUP(func_name [, func_name] ...)
  | SMA(col_name [, col_name] ...)
  | TTL value
}

```

**More explanations**
44

45
1. The first column of a table MUST be of type TIMESTAMP. It is automatically set as the primary key.
S
Sean Ely 已提交
46
2. The maximum length of the table name is 192 bytes.
47
3. The maximum length of each row is 48k bytes, please note that the extra 2 bytes used by each BINARY/NCHAR column are also counted.
48
4. The name of the subtable can only consist of characters from the English alphabet, digits and underscore. Table names can't start with a digit. Table names are case insensitive.
S
Sean Ely 已提交
49
5. The maximum length in bytes must be specified when using BINARY or NCHAR types.
50
6. Escape character "\`" can be used to avoid the conflict between table names and reserved keywords, above rules will be bypassed when using escape character on table names, but the upper limit for the name length is still valid. The table names specified using escape character are case sensitive.
51
   For example \`aBc\` and \`abc\` are different table names but `abc` and `aBc` are same table names because they are both converted to `abc` internally.
52
   Only ASCII visible characters can be used with escape character.
53

54 55 56 57 58 59 60
**Parameter description**
1. COMMENT: specifies comments for the table. This parameter can be used with supertables, standard tables, and subtables.
2. WATERMARK: specifies the time after which the window is closed. The default value is 5 seconds. Enter a value between 0 and 15 minutes in milliseconds, seconds, or minutes. You can enter multiple values separated by commas (,). This parameter applies only to supertables and takes effect only when the RETENTIONS parameter has been specified for the database.
3. MAX_DELAY: specifies the maximum latency for pushing computation results. The default value is 15 minutes or the value of the INTERVAL parameter, whichever is smaller. Enter a value between 0 and 15 minutes in milliseconds, seconds, or minutes. You can enter multiple values separated by commas (,). Note: Retain the default value if possible. Configuring a small MAX_DELAY may cause results to be frequently pushed, affecting storage and query performance. This parameter applies only to supertables and takes effect only when the RETENTIONS parameter has been specified for the database.
4. ROLLUP: specifies aggregate functions to roll up. Rolling up a function provides downsampled results based on multiple axes. This parameter applies only to supertables and takes effect only when the RETENTIONS parameter has been specified for the database. You can specify only one function to roll up. The rollup takes effect on all columns except TS. Enter one of the following values: avg, sum, min, max, last, or first.
5. SMA: specifies functions on which to enable small materialized aggregates (SMA). SMA is user-defined precomputation of aggregates based on data blocks. Enter one of the following values: max, min, or sum This parameter can be used with supertables and standard tables.
6. TTL: specifies the time to live (TTL) for the table. If the period specified by the TTL parameter elapses without any data being written to the table, TDengine will automatically delete the table. Note: The system may not delete the table at the exact moment that the TTL expires. Enter a value in days. The default value is 0. Note: The TTL parameter has a higher priority than the KEEP parameter. If a table is marked for deletion because the TTL has expired, it will be deleted even if the time specified by the KEEP parameter has not elapsed. This parameter can be used with standard tables and subtables.
61

62
## Create Subtables
63

64 65 66
### Create a Subtable

```sql
67 68 69
CREATE TABLE [IF NOT EXISTS] tb_name USING stb_name TAGS (tag_value1, ...);
```

70
### Create a Subtable with Specified Tags
71

72
```sql
73 74 75
CREATE TABLE [IF NOT EXISTS] tb_name USING stb_name (tag_name1, ...) TAGS (tag_value1, ...);
```

76
The preceding SQL statement creates a subtable based on a supertable but specifies a subset of tags to use. Tags that are not included in this subset are assigned a null value.
77

78
### Create Multiple Subtables
79

80
```sql
81 82 83
CREATE TABLE [IF NOT EXISTS] tb_name1 USING stb_name TAGS (tag_value1, ...) [IF NOT EXISTS] tb_name2 USING stb_name TAGS (tag_value2, ...) ...;
```

84 85 86
You can create multiple subtables in a single SQL statement provided that all subtables use the same supertable. For performance reasons, do not create more than 3000 tables per statement.

## Modify a Table
87

88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
```sql
ALTER TABLE [db_name.]tb_name alter_table_clause
 
alter_table_clause: {
    alter_table_options
  | ADD COLUMN col_name column_type
  | DROP COLUMN col_name
  | MODIFY COLUMN col_name column_type
  | RENAME COLUMN old_col_name new_col_name
}
 
alter_table_options:
    alter_table_option ...
 
alter_table_option: {
    TTL value
  | COMMENT 'string_value'
}
106

107
```
108

109 110 111 112 113 114
**More explanations**
You can perform the following modifications on existing tables:
1. ADD COLUMN: adds a column to the supertable.
2. DROP COLUMN: deletes a column from the supertable.
3. MODIFY COLUMN: changes the length of the data type specified for the column. Note that you can only specify a length greater than the current length.
4. RENAME COLUMN: renames a specified column in the table.
115

116
### Add a Column
117

118 119
```sql
ALTER TABLE tb_name ADD COLUMN field_name data_type;
120 121
```

122
### Delete a Column
123

124 125
```sql
ALTER TABLE tb_name DROP COLUMN field_name;
126 127
```

128
### Modify the Data Length
129

130 131
```sql
ALTER TABLE tb_name MODIFY COLUMN field_name data_type(length);
132 133
```

134
### Rename a Column
135

136 137
```sql
ALTER TABLE tb_name RENAME COLUMN old_col_name new_col_name
138
```
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156

## Modify a Subtable

```sql
ALTER TABLE [db_name.]tb_name alter_table_clause
 
alter_table_clause: {
    alter_table_options
  | SET TAG tag_name = new_tag_value
}
 
alter_table_options:
    alter_table_option ...
 
alter_table_option: {
    TTL value
  | COMMENT 'string_value'
}
157 158
```

159 160
**More explanations**
1. Only the value of a tag can be modified directly. For all other modifications, you must modify the supertable from which the subtable was created.
161

162
### Change Tag Value Of Sub Table
163 164

```
165
ALTER TABLE tb_name SET TAG tag_name=new_tag_value;
166 167
```

168
## Delete a Table
169

170
The following SQL statement deletes one or more tables.
171

172 173
```sql
DROP TABLE [IF EXISTS] [db_name.]tb_name [, [IF EXISTS] [db_name.]tb_name] ...
174 175
```

176
## View Tables
177

178
### View All Tables
179

180
The following SQL statement shows all tables in the current database.
181

182 183
```sql
SHOW TABLES [LIKE tb_name_wildchar];
184 185
```

186
### View the CREATE Statement for a Table
187

188 189 190
```
SHOW CREATE TABLE tb_name;
```
191

192
This command is useful in migrating data from one TDengine cluster to another because it can be used to create the exact same tables in the target database.
193

194
## View the Table Schema
195 196

```
197 198
DESCRIBE [db_name.]tb_name;
```