03-table.md 4.1 KB
Newer Older
D
dingbo 已提交
1
---
2 3 4
sidebar_label: Table
title: Table
description: create super table, normal table and sub table, drop tables and change tables
D
dingbo 已提交
5 6
---

7
## Create Table
D
dingbo 已提交
8 9 10 11 12

```
CREATE TABLE [IF NOT EXISTS] tb_name (timestamp_field_name TIMESTAMP, field1_name data_type1 [, field2_name data_type2 ...]);
```

13
:::info
D
dingbo 已提交
14

15
1. The first column of a table MUST be of type TIMESTAMP. It is automatically set as the primary key.
S
Sean Ely 已提交
16 17
2. The maximum length of the table name is 192 bytes.
3. The maximum length of each row is 16k bytes, please note that the extra 2 bytes used by each BINARY/NCHAR column are also counted.
18
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 已提交
19 20
5. The maximum length in bytes must be specified when using BINARY or NCHAR types.
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. Only ASCII visible characters can be used with escape character.
21
   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.
D
dingbo 已提交
22 23 24

:::

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
25
### Create Subtable Using STable As Template
D
dingbo 已提交
26 27 28 29 30

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

S
Sean Ely 已提交
31
The above command creates a subtable using the specified super table as a template and the specified tag values.
D
dingbo 已提交
32

S
Sean Ely 已提交
33
### Create Subtable Using STable As Template With A Subset of Tags
D
dingbo 已提交
34 35 36 37 38

```
CREATE TABLE [IF NOT EXISTS] tb_name USING stb_name (tag_name1, ...) TAGS (tag_value1, ...);
```

39
The tags for which no value is specified will be set to NULL.
D
dingbo 已提交
40

41
### Create Tables in Batch
D
dingbo 已提交
42 43 44 45 46

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

47
This can be used to create a lot of tables in a single SQL statement while making table creation much faster.
D
dingbo 已提交
48 49 50

:::info

S
Sean Ely 已提交
51
- Creating tables in batch must use a super table as a template.
52
- The length of single statement is suggested to be between 1,000 and 3,000 bytes for best performance.
D
dingbo 已提交
53 54 55

:::

56
## Drop Tables
D
dingbo 已提交
57 58 59 60 61

```
DROP TABLE [IF EXISTS] tb_name;
```

62
## Show All Tables In Current Database
D
dingbo 已提交
63 64

```
D
dingbo 已提交
65
SHOW TABLES [LIKE tb_name_wildcard];
D
dingbo 已提交
66 67
```

68
## Show Create Statement of A Table
D
dingbo 已提交
69 70 71 72 73

```
SHOW CREATE TABLE tb_name;
```

S
Sean Ely 已提交
74
This is useful when migrating the data in one TDengine cluster to another one because it can be used to create the exact same tables in the target database.
D
dingbo 已提交
75

76
## Show Table Definition
D
dingbo 已提交
77 78 79 80 81

```
DESCRIBE tb_name;
```

82
## Change Table Definition
D
dingbo 已提交
83

84
### Add A Column
D
dingbo 已提交
85 86 87 88 89 90 91

```
ALTER TABLE tb_name ADD COLUMN field_name data_type;
```

:::info

92
1. The maximum number of columns is 4096, the minimum number of columns is 2.
S
Sean Ely 已提交
93
2. The maximum length of a column name is 64 bytes.
D
dingbo 已提交
94 95 96

:::

97
### Remove A Column
D
dingbo 已提交
98 99 100 101 102

```
ALTER TABLE tb_name DROP COLUMN field_name;
```

103
:::note
S
Sean Ely 已提交
104
If a table is created using a super table as template, the table definition can only be changed on the corresponding super table, and the change will be automatically applied to all the subtables created using this super table as template. For tables created in the normal way, the table definition can be changed directly on the table.
105 106

:::
D
dingbo 已提交
107

108
### Change Column Length
D
dingbo 已提交
109 110 111 112 113

```
ALTER TABLE tb_name MODIFY COLUMN field_name data_type(length);
```

114
If the type of a column is variable length, like BINARY or NCHAR, this command can be used to change the length of the column.
D
dingbo 已提交
115

116
:::note
S
Sean Ely 已提交
117
If a table is created using a super table as template, the table definition can only be changed on the corresponding super table, and the change will be automatically applied to all the subtables created using this super table as template. For tables created in the normal way, the table definition can be changed directly on the table.
118 119 120 121

:::

### Change Tag Value Of Sub Table
D
dingbo 已提交
122 123 124 125

```
ALTER TABLE tb_name SET TAG tag_name=new_tag_value;
```
126 127

This command can be used to change the tag value if the table is created using a super table as template.