ALTER TABLE

description

1. This statement is used to modify an existing table. If no rollup index is specified, the base operation is the default.
2. The statement is divided into three types of operations: schema change, rollup, partition
3. These three types of operations cannot appear in an ALTER TABLE statement at the same time.
4. Where schema change and rollup are asynchronous operations and are returned if the task commits successfully. You can then use the SHOW ALTER command to view the progress.
5. Partition is a synchronous operation, and a command return indicates that execution is complete.

7. grammar:
8. ALTER TABLE [database.]table
9. Alter_clause1[, alter_clause2, ...];

11. The alter_clause is divided into partition, rollup, schema change, rename and bimmap index.

13. Partition supports the following modifications
14. Increase the partition
15. grammar:
16. ADD PARTITION [IF NOT EXISTS] partition_name
17. Partition_desc ["key"="value"]
18. [DISTRIBUTED BY HASH (k1[,k2 ...]) [BUCKETS num]]
19. note:
20. 1) partition_desc supports two ways of writing:
21. * VALUES LESS THAN [MAXVALUE|("value1", ...)]
22. * VALUES [("value1", ...), ("value1", ...))
23. 1) The partition is the left closed right open interval. If the user only specifies the right boundary, the system will automatically determine the left boundary.
24. 2) If the bucket mode is not specified, the bucket method used by the built-in table is automatically used.
25. 3) If the bucket mode is specified, only the bucket number can be modified, and the bucket mode or bucket column cannot be modified.
26. 4) ["key"="value"] section can set some properties of the partition, see CREATE TABLE for details.
27. 5) Adding partitions to non-partitioned table is not supported.

29. 2. Delete the partition
30. grammar:
31. DROP PARTITION [IF EXISTS] partition_name
32. note:
33. 1) Use a partitioned table to keep at least one partition.
34. 2) Execute DROP PARTITION For a period of time, the deleted partition can be recovered by the RECOVER statement. See the RECOVER statement for details.
35. 3) If DROP PARTITION FORCE is executed, the system will not check whether the partition has unfinished transactions, the partition will be deleted directly and cannot be recovered, generally this operation is not recommended

37. 3. Modify the partition properties
38. grammar:
39. MODIFY PARTITION p1|(p1[, p2, ...]) SET ("key" = "value", ...)
40. Description:
41. 1) The following attributes of the modified partition are currently supported.
42. - storage_medium
43. - storage_cooldown_time
44. - replication_num
45. — in_memory
46. 2) For single-partition tables, partition_name is the same as the table name.

48. Rollup supports the following ways to create:
49. 1. Create a rollup index
50. grammar:
51. ADD ROLLUP rollup_name (column_name1, column_name2, ...)
52. [FROM from_index_name]
53. [PROPERTIES ("key"="value", ...)]

55. properties: Support setting timeout time, the default timeout time is 1 day.
56. example:
57. ADD ROLLUP r1(col1,col2) from r0
58. 1.2 Batch create rollup index
59. grammar:
60. ADD ROLLUP [rollup_name (column_name1, column_name2, ...)
61. [FROM from_index_name]
62. [PROPERTIES ("key"="value", ...)],...]
63. example:
64. ADD ROLLUP r1(col1,col2) from r0, r2(col3,col4) from r0
65. 1.3 note:
66. 1) If from_index_name is not specified, it is created by default from base index
67. 2) The columns in the rollup table must be existing columns in from_index
68. 3) In properties, you can specify the storage format. See CREATE TABLE for details.

70. 2. Delete the rollup index
71. grammar:
72. DROP ROLLUP rollup_name
73. [PROPERTIES ("key"="value", ...)]
74. example:
75. DROP ROLLUP r1
76. 2.1 Batch Delete rollup index
77. grammar: DROP ROLLUP [rollup_name [PROPERTIES ("key"="value", ...)],...]
78. example: DROP ROLLUP r1,r2
79. 2.2 note:
80. 1) Cannot delete base index

83. Schema change supports the following modifications:
84. 1. Add a column to the specified location of the specified index
85. grammar:
86. ADD COLUMN column_name column_type [KEY | agg_type] [DEFAULT "default_value"]
87. [AFTER column_name|FIRST]
88. [TO rollup_index_name]
89. [PROPERTIES ("key"="value", ...)]
90. note:
91. 1) Aggregate model If you add a value column, you need to specify agg_type
92. 2) Non-aggregate models (such as DUPLICATE KEY) If you add a key column, you need to specify the KEY keyword.
93. 3) You cannot add a column that already exists in the base index to the rollup index
94. Recreate a rollup index if needed

96. 2. Add multiple columns to the specified index
97. grammar:
98. ADD COLUMN (column_name1 column_type [KEY | agg_type] DEFAULT "default_value", ...)
99. [TO rollup_index_name]
100. [PROPERTIES ("key"="value", ...)]
101. note:
102. 1) Aggregate model If you add a value column, you need to specify agg_type
103. 2) Non-aggregate model If you add a key column, you need to specify the KEY keyword.
104. 3) You cannot add a column that already exists in the base index to the rollup index
105. (You can recreate a rollup index if needed)

107. 3. Remove a column from the specified index
108. grammar:
109. DROP COLUMN column_name
110. [FROM rollup_index_name]
111. note:
112. 1) Cannot delete partition column
113. 2) If the column is removed from the base index, it will also be deleted if the column is included in the rollup index

115. 4. Modify the column type and column position of the specified index
116. grammar:
117. MODIFY COLUMN column_name column_type [KEY | agg_type] [NULL | NOT NULL] [DEFAULT "default_value"]
118. [AFTER column_name|FIRST]
119. [FROM rollup_index_name]
120. [PROPERTIES ("key"="value", ...)]
121. note:
122. 1) Aggregate model If you modify the value column, you need to specify agg_type
123. 2) Non-aggregate type If you modify the key column, you need to specify the KEY keyword.
124. 3) Only the type of the column can be modified. The other attributes of the column remain as they are (ie other attributes need to be explicitly written in the statement according to the original attribute, see example 8)
125. 4) The partition column cannot be modified
126. 5) The following types of conversions are currently supported (accuracy loss is guaranteed by the user)
127. TINYINT/SMALLINT/INT/BIGINT/LARGEINT/FLOAT/DOUBLE convert to a wider range of numeric types
128. TINTINT/SMALLINT/INT/BIGINT/LARGEINT/FLOAT/DOUBLE/DECIMAL is converted to VARCHAR
129. VARCHAR supports modification of maximum length
130. Convert VARCHAR/CHAR to TINYINT/SMALLINT/INT/BIGINT/LARGEINT/FLOAT/DOUBLE.
131. Convert VARCHAR/CHAR to DATE (currently support six formats: "%Y-%m-%d", "%y-%m-%d", "%Y%m%d", "%y%m%d", "%Y/%m/%d, "%y/%m/%d")
132. Convert DATETIME to DATE(Only year-month-day information is retained, For example: `2019-12-09 21:47:05` <--> `2019-12-09`)
133. Convert DATE to DATETIME(Set hour, minute, second to zero, For example: `2019-12-09` <--> `2019-12-09 00:00:00`)
134. Convert FLOAT to DOUBLE
135. Convert INT to DATE (If the INT data fails to convert, the original data remains the same)
136. 6) Does not support changing from NULL to NOT NULL

138. 5. Reorder the columns of the specified index
139. grammar:
140. ORDER BY (column_name1, column_name2, ...)
141. [FROM rollup_index_name]
142. [PROPERTIES ("key"="value", ...)]
143. note:
144. 1) All columns in index must be written
145. 2) value is listed after the key column

147. 6. Modify the properties of the table, currently supports modifying the bloom filter column, the colocate_with attribute and the dynamic_partition attribute,  the replication_num and default.replication_num.
148. grammar:
149. PROPERTIES ("key"="value")
150. note:
151. Can also be merged into the above schema change operation to modify, see the example below

153. 7. Enable batch delete support
154. grammar:
155. ENABLE FEATURE "BATCH_DELETE"
156. note:
157. 1) Only support unique tables
158. 2) Batch deletion is supported for old tables, while new tables are already supported when they are created

160. 8. Enable the ability to import in order by the value of the sequence column
161. grammer:
162. ENABLE FEATURE "SEQUENCE_LOAD" WITH PROPERTIES ("function_column.sequence_type" = "Date")
163. note:
164. 1) Only support unique tables
165. 2) The sequence_type is used to specify the type of the sequence column, which can be integral and time type
166. 3) Only the orderliness of newly imported data is supported. Historical data cannot be changed

168. 9. Modify default buckets number of partition
169. grammer:
170. MODIFY DISTRIBUTION DISTRIBUTED BY HASH (k1[,k2 ...]) BUCKETS num
171. note:
172. 1)Only support non colocate table with RANGE partition and HASH distribution

174. 10. Modify table comment
175. grammer:
176. MODIFY COMMENT "new table comment"

178. 11. Modify column comment
179. grammer:
180. MODIFY COLUMN col1 COMMENT "new column comment"

182. 12. Modify engine type

184. Only the MySQL type can be changed to the ODBC type. The value of driver is the name of the driver in the odbc.init configuration.

186. grammar:
187. MODIFY ENGINE TO odbc PROPERTIES("driver" = "MySQL");

189. Rename supports modification of the following names:
190. 1. Modify the table name
191. grammar:
192. RENAME new_table_name;

194. 2. Modify the rollup index name
195. grammar:
196. RENAME ROLLUP old_rollup_name new_rollup_name;

198. 3. Modify the partition name
199. grammar:
200. RENAME PARTITION old_partition_name new_partition_name;

202. Bitmap index supports the following modifications:
203. 1. create bitmap index
204. grammar:
205. ADD INDEX [IF NOT EXISTS] index_name (column [, ...],) [USING BITMAP] [COMMENT 'balabala'];
206. note:
207. 1. only supports bitmap index for current version
208. 2. BITMAP index only supports apply on single column
209. 2. drop index
210. grammar:
211. DROP INDEX [IF EXISTS] index_name;

example

1. [table]
2. 1. Modify the default number of replications of the table, which is used as default number of replications while creating new partition.
3. ALTER TABLE example_db.my_table
4. SET ("default.replication_num" = "2");

6. 2. Modify the actual number of replications of a unpartitioned table (unpartitioned table only)
7. ALTER TABLE example_db.my_table
8. SET ("replication_num" = "3");

10. [partition]
11. 1. Add partition, existing partition [MIN, 2013-01-01), add partition [2013-01-01, 2014-01-01), use default bucket mode
12. ALTER TABLE example_db.my_table
13. ADD PARTITION p1 VALUES LESS THAN ("2014-01-01");

15. 2. Increase the partition and use the new number of buckets
16. ALTER TABLE example_db.my_table
17. ADD PARTITION p1 VALUES LESS THAN ("2015-01-01")
18. DISTRIBUTED BY HASH(k1) BUCKETS 20;

20. 3. Increase the partition and use the new number of copies
21. ALTER TABLE example_db.my_table
22. ADD PARTITION p1 VALUES LESS THAN ("2015-01-01")
23. ("replication_num"="1");

25. 4. Modify the number of partition copies
26. ALTER TABLE example_db.my_table
27. MODIFY PARTITION p1 SET("replication_num"="1");

29. 5. Batch modify the specified partitions
30. ALTER TABLE example_db.my_table
31. MODIFY PARTITION (p1, p2, p4) SET("in_memory"="true");

33. 6. Batch modify all partitions
34. ALTER TABLE example_db.my_table
35. MODIFY PARTITION (*) SET("storage_medium"="HDD");

37. 7. Delete the partition
38. ALTER TABLE example_db.my_table
39. DROP PARTITION p1;

41. 8. Add a partition that specifies the upper and lower bounds

43. ALTER TABLE example_db.my_table
44. ADD PARTITION p1 VALUES [("2014-01-01"), ("2014-02-01"));

46. [rollup]
47. 1. Create index: example_rollup_index, based on base index(k1,k2,k3,v1,v2). Columnar storage.
48. ALTER TABLE example_db.my_table
49. ADD ROLLUP example_rollup_index(k1, k3, v1, v2);

51. 2. Create index: example_rollup_index2, based on example_rollup_index(k1,k3,v1,v2)
52. ALTER TABLE example_db.my_table
53. ADD ROLLUP example_rollup_index2 (k1, v1)
54. FROM example_rollup_index;

56. 3. Create index: example_rollup_index3, based on base index (k1, k2, k3, v1), custom rollup timeout time is one hour.

58. ALTER TABLE example_db.my_table
59. ADD ROLLUP example_rollup_index(k1, k3, v1)
60. PROPERTIES("timeout" = "3600");

62. 3. Delete index: example_rollup_index2
63. ALTER TABLE example_db.my_table
64. DROP ROLLUP example_rollup_index2;

66. [schema change]
67. 1. Add a key column new_col to the col1 of example_rollup_index (non-aggregate model)
68. ALTER TABLE example_db.my_table
69. ADD COLUMN new_col INT KEY DEFAULT "0" AFTER col1
70. TO example_rollup_index;

72. 2. Add a value column new_col to the col1 of example_rollup_index (non-aggregate model)
73. ALTER TABLE example_db.my_table
74. ADD COLUMN new_col INT DEFAULT "0" AFTER col1
75. TO example_rollup_index;

77. 3. Add a key column new_col (aggregation model) to col1 of example_rollup_index
78. ALTER TABLE example_db.my_table
79. ADD COLUMN new_col INT DEFAULT "0" AFTER col1
80. TO example_rollup_index;

82. 4. Add a value column to the col1 of example_rollup_index. new_col SUM aggregation type (aggregation model)
83. ALTER TABLE example_db.my_table
84. ADD COLUMN new_col INT SUM DEFAULT "0" AFTER col1
85. TO example_rollup_index;

87. 5. Add multiple columns to the example_rollup_index (aggregate model)
88. ALTER TABLE example_db.my_table
89. ADD COLUMN (col1 INT DEFAULT "1", col2 FLOAT SUM DEFAULT "2.3")
90. TO example_rollup_index;

92. 6. Remove a column from example_rollup_index
93. ALTER TABLE example_db.my_table
94. DROP COLUMN col2
95. FROM example_rollup_index;

97. 7. Modify the base index's col1 key column to be of type BIGINT and move to the col2 column
98. (*Attention: Whether to modify the key column or the value column, complete column information need to be declared. For example, MODIFY COLUMN xxx COLUMNTYPE [KEY|agg_type]*)
99. ALTER TABLE example_db.my_table
100. MODIFY COLUMN col1 BIGINT KEY DEFAULT "1" AFTER col2;

102. 8. Modify the maximum length of the val1 column of the base index. The original val1 is (val1 VARCHAR(32) REPLACE DEFAULT "abc")
103. ALTER TABLE example_db.my_table
104. MODIFY COLUMN val1 VARCHAR(64) REPLACE DEFAULT "abc";

106. 9. Reorder the columns in example_rollup_index (set the original column order: k1, k2, k3, v1, v2)
107. ALTER TABLE example_db.my_table
108. ORDER BY (k3, k1, k2, v2, v1)
109. FROM example_rollup_index;

111. 10. Perform both operations simultaneously
112. ALTER TABLE example_db.my_table
113. ADD COLUMN v2 INT MAX DEFAULT "0" AFTER k2 TO example_rollup_index,
114. ORDER BY (k3,k1,k2,v2,v1) FROM example_rollup_index;

116. 11. Modify the bloom filter column of the table
117. ALTER TABLE example_db.my_table SET ("bloom_filter_columns"="k1,k2,k3");

119. Can also be merged into the above schema change operation (note that the syntax of multiple clauses is slightly different)
120. ALTER TABLE example_db.my_table
121. DROP COLUMN col2
122. PROPERTIES ("bloom_filter_columns"="k1,k2,k3");

124. 12. Modify the Colocate property of the table

126. ALTER TABLE example_db.my_table set ("colocate_with" = "t1");

128. 13. Change the bucketing mode of the table from Random Distribution to Hash Distribution

130. ALTER TABLE example_db.my_table set ("distribution_type" = "hash");

132. 14. Modify the dynamic partition properties of the table (support adding dynamic partition properties to tables without dynamic partition properties)

134. ALTER TABLE example_db.my_table set ("dynamic_partition.enable" = "false");

136. If you need to add dynamic partition attributes to a table without dynamic partition attributes, you need to specify all dynamic partition attributes.
137. (Note:Adding dynamic partition attributes to non-partitioned table is not supported)

139. ALTER TABLE example_db.my_table set ("dynamic_partition.enable"= "true", "dynamic_partition.time_unit" = "DAY", "dynamic_partition.end "= "3", "dynamic_partition.prefix" = "p", "dynamic_partition.buckets" = "32");

142. 15. Modify the in_memory property of the table

144. ALTER TABLE example_db.my_table set ("in_memory" = "true");
145. 16. Enable batch delete support

147. ALTER TABLE example_db.my_table ENABLE FEATURE "BATCH_DELETE"
148. 17. Enable the ability to import in order by the value of the Sequence column

150. ALTER TABLE example_db.my_table ENABLE FEATURE "SEQUENCE_LOAD" WITH PROPERTIES ("function_column.sequence_type" = "Date")

152. 18. Modify the default buckets number of example_db.my_table to 50

154. ALTER TABLE example_db.my_table MODIFY DISTRIBUTION DISTRIBUTED BY HASH(k1) BUCKETS 50;

156. 19. Modify table comment

158. ALTER TABLE example_db.my_table MODIFY COMMENT "new comment";

160. 20. Modify column comment

162. ALTER TABLE example_db.my_table MODIFY COLUMN k1 COMMENT "k1", MODIFY COLUMN k2 COMMENT "k2";

164. 21. Modify engine Type

166. ALTER TABLE example_db.mysql_table MODIFY ENGINE TO odbc PROPERTIES("driver" = "MySQL");

168. [rename]
169. 1. Modify the table named table1 to table2
170. ALTER TABLE table1 RENAME table2;

172. 2. Modify the rollup index named rollup1 in the table example_table to rollup2
173. ALTER TABLE example_table RENAME ROLLUP rollup1 rollup2;

175. 3. Modify the partition named p1 in the table example_table to p2
176. ALTER TABLE example_table RENAME PARTITION p1 p2;

178. [index]
179. 1. create index on table1 column siteid using bitmap
180. ALTER TABLE table1 ADD INDEX [IF NOT EXISTS] index_name  [USING BITMAP] (siteid) COMMENT 'balabala';
181. 2. drop bitmap index of table1
182. ALTER TABLE table1 DROP INDEX [IF EXISTS] index_name;

keyword

1. ALTER, TABLE, ROLLUP, COLUMN, PARTITION, RENAME