04-stable.md 3.3 KB
Newer Older
D
dingbo 已提交
1
---
2 3
sidebar_label: STable
title: Super Table
D
dingbo 已提交
4 5 6 7
---

:::note

G
gccgdb1234 已提交
8
Keyword `STable`, abbreviated for super table, is supported since version 2.0.15.
D
dingbo 已提交
9 10 11

:::

12
## Crate STable
D
dingbo 已提交
13 14

```
G
gccgdb1234 已提交
15
CREATE STable [IF NOT EXISTS] stb_name (timestamp_field_name TIMESTAMP, field1_name data_type1 [, field2_name data_type2 ...]) TAGS (tag1_name tag_type1, tag2_name tag_type2 [, tag3_name tag_type3]);
D
dingbo 已提交
16 17
```

18
The SQL statement of creating STable is similar to that of creating table, but a special column named as `TAGS` must be specified with the names and types of the tags.
D
dingbo 已提交
19 20 21

:::info

22 23 24 25
1. The tag types specified in TAGS should NOT be timestamp. Since 2.1.3.0 timestamp type can be used in TAGS column, but its value must be fixed and arithmetic operation can't be applied on it.
2. The tag names specified in TAGS should NOT be same as other columns.
3. The tag names specified in TAGS should NOT be same as any reserved keywords.(Please refer to [keywords](/taos-sql/keywords/)
4. The maximum number of tags specified in TAGS is 128, but there must be at least one tag, and the total length of all tag columns should NOT exceed 16KB.
D
dingbo 已提交
26 27 28

:::

29
## Drop STable
D
dingbo 已提交
30 31

```
G
gccgdb1234 已提交
32
DROP STable [IF EXISTS] stb_name;
D
dingbo 已提交
33 34
```

G
gccgdb1234 已提交
35
All the sub-tables created using the deleted STable will be deleted automatically.
D
dingbo 已提交
36

37
## Show All STables
D
dingbo 已提交
38 39

```
G
gccgdb1234 已提交
40
SHOW STableS [LIKE tb_name_wildcard];
D
dingbo 已提交
41 42
```

43
This command can be used to display the information of all STables in the current database, including name, creation time, number of columns, number of tags, number of tables created using this STable.
D
dingbo 已提交
44

45
## Show The Create Statement of A STable
D
dingbo 已提交
46 47

```
G
gccgdb1234 已提交
48
SHOW CREATE STable stb_name;
D
dingbo 已提交
49 50
```

51
This command is useful in migrating data from one TDengine cluster to another one because it can be used to create an exactly same STable in the target database.
D
dingbo 已提交
52

53
## Get STable Definition
D
dingbo 已提交
54 55 56 57 58

```
DESCRIBE stb_name;
```

59
## Change Columns Of STable
D
dingbo 已提交
60

61
### Add A Column
D
dingbo 已提交
62 63

```
G
gccgdb1234 已提交
64
ALTER STable stb_name ADD COLUMN field_name data_type;
D
dingbo 已提交
65 66
```

67
### Remove A Column
D
dingbo 已提交
68 69

```
G
gccgdb1234 已提交
70
ALTER STable stb_name DROP COLUMN field_name;
D
dingbo 已提交
71 72
```

73
### Change Column Length
D
dingbo 已提交
74 75

```
G
gccgdb1234 已提交
76
ALTER STable stb_name MODIFY COLUMN field_name data_type(length);
D
dingbo 已提交
77 78
```

D
dingbo 已提交
79
This command can be used to change (or increase, more specifically) the length of a column of variable length types, like BINARY or NCHAR.
D
dingbo 已提交
80

81
## Change Tags of A STable
D
dingbo 已提交
82

83
### Add A Tag
D
dingbo 已提交
84 85

```
G
gccgdb1234 已提交
86
ALTER STable stb_name ADD TAG new_tag_name tag_type;
D
dingbo 已提交
87 88
```

89
This command is used to add a new tag for a STable and specify the tag type.
D
dingbo 已提交
90

91
### Remove A Tag
D
dingbo 已提交
92 93

```
G
gccgdb1234 已提交
94
ALTER STable stb_name DROP TAG tag_name;
D
dingbo 已提交
95 96
```

97
The tag will be removed automatically from all the sub tables crated using the super table as template once a tag is removed from a super table.
D
dingbo 已提交
98

99
### Change A Tag
D
dingbo 已提交
100 101

```
G
gccgdb1234 已提交
102
ALTER STable stb_name CHANGE TAG old_tag_name new_tag_name;
D
dingbo 已提交
103 104
```

105
The tag name will be changed automatically from all the sub tables crated using the super table as template once a tag name is changed for a super table.
D
dingbo 已提交
106

107
### Change Tag Length
D
dingbo 已提交
108 109

```
G
gccgdb1234 已提交
110
ALTER STable stb_name MODIFY TAG tag_name data_type(length);
D
dingbo 已提交
111 112
```

D
dingbo 已提交
113
This command can be used to change (or increase, more specifically) the length of a tag of variable length types, like BINARY or NCHAR.
D
dingbo 已提交
114 115

:::note
116
Changing tag value can be applied to only sub tables. All other tag operations, like add tag, remove tag, however, can be applied to only STable. If a new tag is added for a STable, the tag will be added with NULL value for all its sub tables.
D
dingbo 已提交
117

118
:::