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. grammarDROP ROLLUP [rollup_name [PROPERTIES ("key"="value", ...)],...]
  69. exampleDROP 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 is converted to TINYINT/SMALLINT/INT/BIGINT/DOUBLE.
  114. TINTINT/SMALLINT/INT/BIGINT/LARGEINT/FLOAT/DOUBLE/DECIMAL is converted to VARCHAR
  115. VARCHAR supports modification of maximum length
  116. Convert VARCHAR to TINYINT/SMALLINT/INT/BIGINT/LARGEINT/FLOAT/DOUBLE.
  117. Convert VARCHAR 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. Only support unique tables
  141. Rename supports modification of the following names:
  142. 1. Modify the table name
  143. grammar:
  144. RENAME new_table_name;
  145. 2. Modify the rollup index name
  146. grammar:
  147. RENAME ROLLUP old_rollup_name new_rollup_name;
  148. 3. Modify the partition name
  149. grammar:
  150. RENAME PARTITION old_partition_name new_partition_name;
  151. Bitmap index supports the following modifications:
  152. 1. create bitmap index
  153. grammar:
  154. ADD INDEX index_name (column [, ...],) [USING BITMAP] [COMMENT 'balabala'];
  155. note:
  156. 1. only supports bitmap index for current version
  157. 2. BITMAP index only supports apply on single column
  158. 2. drop index
  159. grammar:
  160. 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. ATLER 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. PROPERTIES("storage_type"="column");
  40. 2. Create index: example_rollup_index2, based on example_rollup_index(k1,k3,v1,v2)
  41. ALTER TABLE example_db.my_table
  42. ADD ROLLUP example_rollup_index2 (k1, v1)
  43. FROM example_rollup_index;
  44. 3. Create index: example_rollup_index3, based on base index (k1, k2, k3, v1), custom rollup timeout time is one hour.
  45. ALTER TABLE example_db.my_table
  46. ADD ROLLUP example_rollup_index(k1, k3, v1)
  47. PROPERTIES("storage_type"="column", "timeout" = "3600");
  48. 3. Delete index: example_rollup_index2
  49. ALTER TABLE example_db.my_table
  50. DROP ROLLUP example_rollup_index2;
  51. [schema change]
  52. 1. Add a key column new_col to the col1 of example_rollup_index (non-aggregate model)
  53. ALTER TABLE example_db.my_table
  54. ADD COLUMN new_col INT KEY DEFAULT "0" AFTER col1
  55. TO example_rollup_index;
  56. 2. Add a value column new_col to the col1 of example_rollup_index (non-aggregate model)
  57. ALTER TABLE example_db.my_table
  58. ADD COLUMN new_col INT DEFAULT "0" AFTER col1
  59. TO example_rollup_index;
  60. 3. Add a key column new_col (aggregation model) to col1 of example_rollup_index
  61. ALTER TABLE example_db.my_table
  62. ADD COLUMN new_col INT DEFAULT "0" AFTER col1
  63. TO example_rollup_index;
  64. 4. Add a value column to the col1 of example_rollup_index. new_col SUM aggregation type (aggregation model)
  65. ALTER TABLE example_db.my_table
  66. ADD COLUMN new_col INT SUM DEFAULT "0" AFTER col1
  67. TO example_rollup_index;
  68. 5. Add multiple columns to the example_rollup_index (aggregate model)
  69. ALTER TABLE example_db.my_table
  70. ADD COLUMN (col1 INT DEFAULT "1", col2 FLOAT SUM DEFAULT "2.3")
  71. TO example_rollup_index;
  72. 6. Remove a column from example_rollup_index
  73. ALTER TABLE example_db.my_table
  74. DROP COLUMN col2
  75. FROM example_rollup_index;
  76. 7. Modify the base index's col1 column to be of type BIGINT and move to the col2 column
  77. ALTER TABLE example_db.my_table
  78. MODIFY COLUMN col1 BIGINT 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. [rename]
  107. 1. Modify the table named table1 to table2
  108. ALTER TABLE table1 RENAME table2;
  109. 2. Modify the rollup index named rollup1 in the table example_table to rollup2
  110. ALTER TABLE example_table RENAME ROLLUP rollup1 rollup2;
  111. 3. Modify the partition named p1 in the table example_table to p2
  112. ALTER TABLE example_table RENAME PARTITION p1 p2;
  113. [index]
  114. 1. create index on table1 column siteid using bitmap
  115. ALTER TABLE table1 ADD INDEX index_name [USING BITMAP] (siteid) COMMENT 'balabala';
  116. 2. drop bitmap index of table1
  117. ALTER TABLE table1 DROP INDEX index_name;

keyword

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