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.
  6. grammar:
  7. ALTER TABLE [database.]table
  8. Alter_clause1[, alter_clause2, ...];
  9. The alter_clause is divided into partition, rollup, schema change, rename and bimmap index.
  10. Partition supports the following modifications
  11. Increase the partition
  12. grammar:
  13. ADD PARTITION [IF NOT EXISTS] partition_name
  14. Partition_desc ["key"="value"]
  15. [DISTRIBUTED BY HASH (k1[,k2 ...]) [BUCKETS num]]
  16. note:
  17. 1) partition_desc supports two ways of writing:
  18. * VALUES LESS THAN [MAXVALUE|("value1", ...)]
  19. * VALUES [("value1", ...), ("value1", ...))
  20. 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.
  21. 2) If the bucket mode is not specified, the bucket method used by the built-in table is automatically used.
  22. 3) If the bucket mode is specified, only the bucket number can be modified, and the bucket mode or bucket column cannot be modified.
  23. 4) ["key"="value"] section can set some properties of the partition, see CREATE TABLE for details.
  24. 5) Adding partitions to non-partitioned table is not supported.
  25. 2. Delete the partition
  26. grammar:
  27. DROP PARTITION [IF EXISTS] partition_name
  28. note:
  29. 1) Use a partitioned table to keep at least one partition.
  30. 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.
  31. 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
  32. 3. Modify the partition properties
  33. grammar:
  34. MODIFY PARTITION p1|(p1[, p2, ...]) SET ("key" = "value", ...)
  35. Description:
  36. 1) The following attributes of the modified partition are currently supported.
  37. - storage_medium
  38. - storage_cooldown_time
  39. - replication_num
  40. in_memory
  41. 2) For single-partition tables, partition_name is the same as the table name.
  42. Rollup supports the following ways to create:
  43. 1. Create a rollup index
  44. grammar:
  45. ADD ROLLUP rollup_name (column_name1, column_name2, ...)
  46. [FROM from_index_name]
  47. [PROPERTIES ("key"="value", ...)]
  48. properties: Support setting timeout time, the default timeout time is 1 day.
  49. example:
  50. ADD ROLLUP r1(col1,col2) from r0
  51. 1.2 Batch create rollup index
  52. grammar:
  53. ADD ROLLUP [rollup_name (column_name1, column_name2, ...)
  54. [FROM from_index_name]
  55. [PROPERTIES ("key"="value", ...)],...]
  56. example:
  57. ADD ROLLUP r1(col1,col2) from r0, r2(col3,col4) from r0
  58. 1.3 note:
  59. 1) If from_index_name is not specified, it is created by default from base index
  60. 2) The columns in the rollup table must be existing columns in from_index
  61. 3) In properties, you can specify the storage format. See CREATE TABLE for details.
  62. 2. Delete the rollup index
  63. grammar:
  64. DROP ROLLUP rollup_name
  65. [PROPERTIES ("key"="value", ...)]
  66. example:
  67. DROP ROLLUP r1
  68. 2.1 Batch Delete rollup index
  69. grammar: DROP ROLLUP [rollup_name [PROPERTIES ("key"="value", ...)],...]
  70. example: DROP ROLLUP r1,r2
  71. 2.2 note:
  72. 1) Cannot delete base index
  73. Schema change supports the following modifications:
  74. 1. Add a column to the specified location of the specified index
  75. grammar:
  76. ADD COLUMN column_name column_type [KEY | agg_type] [DEFAULT "default_value"]
  77. [AFTER column_name|FIRST]
  78. [TO rollup_index_name]
  79. [PROPERTIES ("key"="value", ...)]
  80. note:
  81. 1) Aggregate model If you add a value column, you need to specify agg_type
  82. 2) Non-aggregate models (such as DUPLICATE KEY) If you add a key column, you need to specify the KEY keyword.
  83. 3) You cannot add a column that already exists in the base index to the rollup index
  84. Recreate a rollup index if needed
  85. 2. Add multiple columns to the specified index
  86. grammar:
  87. ADD COLUMN (column_name1 column_type [KEY | agg_type] DEFAULT "default_value", ...)
  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 model 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. (You can recreate a rollup index if needed)
  95. 3. Remove a column from the specified index
  96. grammar:
  97. DROP COLUMN column_name
  98. [FROM rollup_index_name]
  99. note:
  100. 1) Cannot delete partition column
  101. 2) If the column is removed from the base index, it will also be deleted if the column is included in the rollup index
  102. 4. Modify the column type and column position of the specified index
  103. grammar:
  104. MODIFY COLUMN column_name column_type [KEY | agg_type] [NULL | NOT NULL] [DEFAULT "default_value"]
  105. [AFTER column_name|FIRST]
  106. [FROM rollup_index_name]
  107. [PROPERTIES ("key"="value", ...)]
  108. note:
  109. 1) Aggregate model If you modify the value column, you need to specify agg_type
  110. 2) Non-aggregate type If you modify the key column, you need to specify the KEY keyword.
  111. 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)
  112. 4) The partition column cannot be modified
  113. 5) The following types of conversions are currently supported (accuracy loss is guaranteed by the user)
  114. TINYINT/SMALLINT/INT/BIGINT/LARGEINT/FLOAT/DOUBLE convert to a wider range of numeric types
  115. TINTINT/SMALLINT/INT/BIGINT/LARGEINT/FLOAT/DOUBLE/DECIMAL is converted to VARCHAR
  116. VARCHAR supports modification of maximum length
  117. Convert VARCHAR/CHAR to TINYINT/SMALLINT/INT/BIGINT/LARGEINT/FLOAT/DOUBLE.
  118. 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")
  119. Convert DATETIME to DATE(Only year-month-day information is retained, For example: `2019-12-09 21:47:05` <--> `2019-12-09`)
  120. Convert DATE to DATETIME(Set hour, minute, second to zero, For example: `2019-12-09` <--> `2019-12-09 00:00:00`)
  121. Convert FLOAT to DOUBLE
  122. Convert INT to DATE (If the INT data fails to convert, the original data remains the same)
  123. 6) Does not support changing from NULL to NOT NULL
  124. 5. Reorder the columns of the specified index
  125. grammar:
  126. ORDER BY (column_name1, column_name2, ...)
  127. [FROM rollup_index_name]
  128. [PROPERTIES ("key"="value", ...)]
  129. note:
  130. 1) All columns in index must be written
  131. 2) value is listed after the key column
  132. 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.
  133. grammar:
  134. PROPERTIES ("key"="value")
  135. note:
  136. Can also be merged into the above schema change operation to modify, see the example below
  137. 7. Enable batch delete support
  138. grammar:
  139. ENABLE FEATURE "BATCH_DELETE"
  140. note:
  141. 1) Only support unique tables
  142. 2) Batch deletion is supported for old tables, while new tables are already supported when they are created
  143. 8. Enable the ability to import in order by the value of the sequence column
  144. grammer:
  145. ENABLE FEATURE "SEQUENCE_LOAD" WITH PROPERTIES ("function_column.sequence_type" = "Date")
  146. note:
  147. 1) Only support unique tables
  148. 2) The sequence_type is used to specify the type of the sequence column, which can be integral and time type
  149. 3) Only the orderliness of newly imported data is supported. Historical data cannot be changed
  150. 9. Modify default buckets number of partition
  151. grammer:
  152. MODIFY DISTRIBUTION DISTRIBUTED BY HASH (k1[,k2 ...]) BUCKETS num
  153. note:
  154. 1)Only support non colocate table with RANGE partition and HASH distribution
  155. 10. Modify table comment
  156. grammer:
  157. MODIFY COMMENT "new table comment"
  158. 11. Modify column comment
  159. grammer:
  160. MODIFY COLUMN col1 COMMENT "new column comment"
  161. 12. Modify engine type
  162. 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.
  163. grammar:
  164. MODIFY ENGINE TO odbc PROPERTIES("driver" = "MySQL");
  165. Rename supports modification of the following names:
  166. 1. Modify the table name
  167. grammar:
  168. RENAME new_table_name;
  169. 2. Modify the rollup index name
  170. grammar:
  171. RENAME ROLLUP old_rollup_name new_rollup_name;
  172. 3. Modify the partition name
  173. grammar:
  174. RENAME PARTITION old_partition_name new_partition_name;
  175. Bitmap index supports the following modifications:
  176. 1. create bitmap index
  177. grammar:
  178. ADD INDEX [IF NOT EXISTS] index_name (column [, ...],) [USING BITMAP] [COMMENT 'balabala'];
  179. note:
  180. 1. only supports bitmap index for current version
  181. 2. BITMAP index only supports apply on single column
  182. 2. drop index
  183. grammar:
  184. 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");
  5. 2. Modify the actual number of replications of a unpartitioned table (unpartitioned table only)
  6. ALTER TABLE example_db.my_table
  7. SET ("replication_num" = "3");
  8. [partition]
  9. 1. Add partition, existing partition [MIN, 2013-01-01), add partition [2013-01-01, 2014-01-01), use default bucket mode
  10. ALTER TABLE example_db.my_table
  11. ADD PARTITION p1 VALUES LESS THAN ("2014-01-01");
  12. 2. Increase the partition and use the new number of buckets
  13. ALTER TABLE example_db.my_table
  14. ADD PARTITION p1 VALUES LESS THAN ("2015-01-01")
  15. DISTRIBUTED BY HASH(k1) BUCKETS 20;
  16. 3. Increase the partition and use the new number of copies
  17. ALTER TABLE example_db.my_table
  18. ADD PARTITION p1 VALUES LESS THAN ("2015-01-01")
  19. ("replication_num"="1");
  20. 4. Modify the number of partition copies
  21. ALTER TABLE example_db.my_table
  22. MODIFY PARTITION p1 SET("replication_num"="1");
  23. 5. Batch modify the specified partitions
  24. ALTER TABLE example_db.my_table
  25. MODIFY PARTITION (p1, p2, p4) SET("in_memory"="true");
  26. 6. Batch modify all partitions
  27. ALTER TABLE example_db.my_table
  28. MODIFY PARTITION (*) SET("storage_medium"="HDD");
  29. 7. Delete the partition
  30. ALTER TABLE example_db.my_table
  31. DROP PARTITION p1;
  32. 8. Add a partition that specifies the upper and lower bounds
  33. ALTER TABLE example_db.my_table
  34. ADD PARTITION p1 VALUES [("2014-01-01"), ("2014-02-01"));
  35. [rollup]
  36. 1. Create index: example_rollup_index, based on base index(k1,k2,k3,v1,v2). Columnar storage.
  37. ALTER TABLE example_db.my_table
  38. ADD ROLLUP example_rollup_index(k1, k3, v1, v2);
  39. 2. Create index: example_rollup_index2, based on example_rollup_index(k1,k3,v1,v2)
  40. ALTER TABLE example_db.my_table
  41. ADD ROLLUP example_rollup_index2 (k1, v1)
  42. FROM example_rollup_index;
  43. 3. Create index: example_rollup_index3, based on base index (k1, k2, k3, v1), custom rollup timeout time is one hour.
  44. ALTER TABLE example_db.my_table
  45. ADD ROLLUP example_rollup_index(k1, k3, v1)
  46. PROPERTIES("timeout" = "3600");
  47. 3. Delete index: example_rollup_index2
  48. ALTER TABLE example_db.my_table
  49. DROP ROLLUP example_rollup_index2;
  50. [schema change]
  51. 1. Add a key column new_col to the col1 of example_rollup_index (non-aggregate model)
  52. ALTER TABLE example_db.my_table
  53. ADD COLUMN new_col INT KEY DEFAULT "0" AFTER col1
  54. TO example_rollup_index;
  55. 2. Add a value column new_col to the col1 of example_rollup_index (non-aggregate model)
  56. ALTER TABLE example_db.my_table
  57. ADD COLUMN new_col INT DEFAULT "0" AFTER col1
  58. TO example_rollup_index;
  59. 3. Add a key column new_col (aggregation model) to col1 of example_rollup_index
  60. ALTER TABLE example_db.my_table
  61. ADD COLUMN new_col INT DEFAULT "0" AFTER col1
  62. TO example_rollup_index;
  63. 4. Add a value column to the col1 of example_rollup_index. new_col SUM aggregation type (aggregation model)
  64. ALTER TABLE example_db.my_table
  65. ADD COLUMN new_col INT SUM DEFAULT "0" AFTER col1
  66. TO example_rollup_index;
  67. 5. Add multiple columns to the example_rollup_index (aggregate model)
  68. ALTER TABLE example_db.my_table
  69. ADD COLUMN (col1 INT DEFAULT "1", col2 FLOAT SUM DEFAULT "2.3")
  70. TO example_rollup_index;
  71. 6. Remove a column from example_rollup_index
  72. ALTER TABLE example_db.my_table
  73. DROP COLUMN col2
  74. FROM example_rollup_index;
  75. 7. Modify the base index's col1 key column to be of type BIGINT and move to the col2 column
  76. (*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]*)
  77. ALTER TABLE example_db.my_table
  78. MODIFY COLUMN col1 BIGINT KEY DEFAULT "1" AFTER col2;
  79. 8. Modify the maximum length of the val1 column of the base index. The original val1 is (val1 VARCHAR(32) REPLACE DEFAULT "abc")
  80. ALTER TABLE example_db.my_table
  81. MODIFY COLUMN val1 VARCHAR(64) REPLACE DEFAULT "abc";
  82. 9. Reorder the columns in example_rollup_index (set the original column order: k1, k2, k3, v1, v2)
  83. ALTER TABLE example_db.my_table
  84. ORDER BY (k3, k1, k2, v2, v1)
  85. FROM example_rollup_index;
  86. 10. Perform both operations simultaneously
  87. ALTER TABLE example_db.my_table
  88. ADD COLUMN v2 INT MAX DEFAULT "0" AFTER k2 TO example_rollup_index,
  89. ORDER BY (k3,k1,k2,v2,v1) FROM example_rollup_index;
  90. 11. Modify the bloom filter column of the table
  91. ALTER TABLE example_db.my_table SET ("bloom_filter_columns"="k1,k2,k3");
  92. Can also be merged into the above schema change operation (note that the syntax of multiple clauses is slightly different)
  93. ALTER TABLE example_db.my_table
  94. DROP COLUMN col2
  95. PROPERTIES ("bloom_filter_columns"="k1,k2,k3");
  96. 12. Modify the Colocate property of the table
  97. ALTER TABLE example_db.my_table set ("colocate_with" = "t1");
  98. 13. Change the bucketing mode of the table from Random Distribution to Hash Distribution
  99. ALTER TABLE example_db.my_table set ("distribution_type" = "hash");
  100. 14. Modify the dynamic partition properties of the table (support adding dynamic partition properties to tables without dynamic partition properties)
  101. ALTER TABLE example_db.my_table set ("dynamic_partition.enable" = "false");
  102. If you need to add dynamic partition attributes to a table without dynamic partition attributes, you need to specify all dynamic partition attributes.
  103. (Note:Adding dynamic partition attributes to non-partitioned table is not supported)
  104. 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");
  105. 15. Modify the in_memory property of the table
  106. ALTER TABLE example_db.my_table set ("in_memory" = "true");
  107. 16. Enable batch delete support
  108. ALTER TABLE example_db.my_table ENABLE FEATURE "BATCH_DELETE"
  109. 17. Enable the ability to import in order by the value of the Sequence column
  110. ALTER TABLE example_db.my_table ENABLE FEATURE "SEQUENCE_LOAD" WITH PROPERTIES ("function_column.sequence_type" = "Date")
  111. 18. Modify the default buckets number of example_db.my_table to 50
  112. ALTER TABLE example_db.my_table MODIFY DISTRIBUTION DISTRIBUTED BY HASH(k1) BUCKETS 50;
  113. 19. Modify table comment
  114. ALTER TABLE example_db.my_table MODIFY COMMENT "new comment";
  115. 20. Modify column comment
  116. ALTER TABLE example_db.my_table MODIFY COLUMN k1 COMMENT "k1", MODIFY COLUMN k2 COMMENT "k2";
  117. 21. Modify engine Type
  118. ALTER TABLE example_db.mysql_table MODIFY ENGINE TO odbc PROPERTIES("driver" = "MySQL");
  119. [rename]
  120. 1. Modify the table named table1 to table2
  121. ALTER TABLE table1 RENAME table2;
  122. 2. Modify the rollup index named rollup1 in the table example_table to rollup2
  123. ALTER TABLE example_table RENAME ROLLUP rollup1 rollup2;
  124. 3. Modify the partition named p1 in the table example_table to p2
  125. ALTER TABLE example_table RENAME PARTITION p1 p2;
  126. [index]
  127. 1. create index on table1 column siteid using bitmap
  128. ALTER TABLE table1 ADD INDEX [IF NOT EXISTS] index_name [USING BITMAP] (siteid) COMMENT 'balabala';
  129. 2. drop bitmap index of table1
  130. ALTER TABLE table1 DROP INDEX [IF EXISTS] index_name;

keyword

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