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

keyword

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