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. Modify the partition properties
  31. grammar:
  32. MODIFY PARTITION partition_name SET ("key" = "value", ...)
  33. Description:
  34. 1) The following attributes of the modified partition are currently supported.
  35. - storage_medium
  36. - storage_cooldown_time
  37. - replication_num
  38. in_memory
  39. 2) For single-partition tables, partition_name is the same as the table name.
  40. Rollup supports the following ways to create:
  41. 1. Create a rollup index
  42. grammar:
  43. ADD ROLLUP rollup_name (column_name1, column_name2, ...)
  44. [FROM from_index_name]
  45. [PROPERTIES ("key"="value", ...)]
  46. properties: Support setting timeout time, the default timeout time is 1 day.
  47. example:
  48. ADD ROLLUP r1(col1,col2) from r0
  49. 1.2 Batch create rollup index
  50. grammar:
  51. ADD ROLLUP [rollup_name (column_name1, column_name2, ...)
  52. [FROM from_index_name]
  53. [PROPERTIES ("key"="value", ...)],...]
  54. example
  55. ADD ROLLUP r1(col1,col2) from r0, r2(col3,col4) from r0
  56. 1.3 note:
  57. 1) If from_index_name is not specified, it is created by default from base index
  58. 2) The columns in the rollup table must be existing columns in from_index
  59. 3) In properties, you can specify the storage format. See CREATE TABLE for details.
  60. 2. Delete the rollup index
  61. grammar:
  62. DROP ROLLUP rollup_name
  63. [PROPERTIES ("key"="value", ...)]
  64. example:
  65. DROP ROLLUP r1
  66. 2.1 Batch Delete rollup index
  67. grammarDROP ROLLUP [rollup_name [PROPERTIES ("key"="value", ...)],...]
  68. exampleDROP ROLLUP r1,r2
  69. 2.2 note:
  70. 1) Cannot delete base index
  71. 2) Execute DROP ROLLUP For a period of time, the deleted rollup index can be restored by the RECOVER statement. See the RECOVER statement for details.
  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. Convert LARGEINT to DOUBLE
  116. VARCHAR supports modification of maximum length
  117. Convert VARCHAR to TINYINT/SMALLINT/INT/BIGINT/LARGEINT/FLOAT/DOUBLE.
  118. 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")
  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. Rename supports modification of the following names:
  138. 1. Modify the table name
  139. grammar:
  140. RENAME new_table_name;
  141. 2. Modify the rollup index name
  142. grammar:
  143. RENAME ROLLUP old_rollup_name new_rollup_name;
  144. 3. Modify the partition name
  145. grammar:
  146. RENAME PARTITION old_partition_name new_partition_name;
  147. Bitmap index supports the following modifications:
  148. 1. create bitmap index
  149. grammar:
  150. ADD INDEX index_name (column [, ...],) [USING BITMAP] [COMMENT 'balabala'];
  151. note:
  152. 1. only supports bitmap index for current version
  153. 2. BITMAP index only supports apply on single column
  154. 2. drop index
  155. grammar:
  156. 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. Delete the partition
  24. ALTER TABLE example_db.my_table
  25. DROP PARTITION p1;
  26. 6. Add a partition that specifies the upper and lower bounds
  27. ALTER TABLE example_db.my_table
  28. ADD PARTITION p1 VALUES [("2014-01-01"), ("2014-02-01"));
  29. [rollup]
  30. 1. Create index: example_rollup_index, based on base index(k1,k2,k3,v1,v2). Columnar storage.
  31. ALTER TABLE example_db.my_table
  32. ADD ROLLUP example_rollup_index(k1, k3, v1, v2)
  33. PROPERTIES("storage_type"="column");
  34. 2. Create index: example_rollup_index2, based on example_rollup_index(k1,k3,v1,v2)
  35. ALTER TABLE example_db.my_table
  36. ADD ROLLUP example_rollup_index2 (k1, v1)
  37. FROM example_rollup_index;
  38. 3. Create index: example_rollup_index3, based on base index (k1, k2, k3, v1), custom rollup timeout time is one hour.
  39. ALTER TABLE example_db.my_table
  40. ADD ROLLUP example_rollup_index(k1, k3, v1)
  41. PROPERTIES("storage_type"="column", "timeout" = "3600");
  42. 3. Delete index: example_rollup_index2
  43. ALTER TABLE example_db.my_table
  44. DROP ROLLUP example_rollup_index2;
  45. [schema change]
  46. 1. Add a key column new_col to the col1 of example_rollup_index (non-aggregate model)
  47. ALTER TABLE example_db.my_table
  48. ADD COLUMN new_col INT KEY DEFAULT "0" AFTER col1
  49. TO example_rollup_index;
  50. 2. Add a value column new_col to the col1 of example_rollup_index (non-aggregate model)
  51. ALTER TABLE example_db.my_table
  52. ADD COLUMN new_col INT DEFAULT "0" AFTER col1
  53. TO example_rollup_index;
  54. 3. Add a key column new_col (aggregation model) to col1 of example_rollup_index
  55. ALTER TABLE example_db.my_table
  56. ADD COLUMN new_col INT DEFAULT "0" AFTER col1
  57. TO example_rollup_index;
  58. 4. Add a value column to the col1 of example_rollup_index. new_col SUM aggregation type (aggregation model)
  59. ALTER TABLE example_db.my_table
  60. ADD COLUMN new_col INT SUM DEFAULT "0" AFTER col1
  61. TO example_rollup_index;
  62. 5. Add multiple columns to the example_rollup_index (aggregate model)
  63. ALTER TABLE example_db.my_table
  64. ADD COLUMN (col1 INT DEFAULT "1", col2 FLOAT SUM DEFAULT "2.3")
  65. TO example_rollup_index;
  66. 6. Remove a column from example_rollup_index
  67. ALTER TABLE example_db.my_table
  68. DROP COLUMN col2
  69. FROM example_rollup_index;
  70. 7. Modify the base index's col1 column to be of type BIGINT and move to the col2 column
  71. ALTER TABLE example_db.my_table
  72. MODIFY COLUMN col1 BIGINT DEFAULT "1" AFTER col2;
  73. 8. Modify the maximum length of the val1 column of the base index. The original val1 is (val1 VARCHAR(32) REPLACE DEFAULT "abc")
  74. ALTER TABLE example_db.my_table
  75. MODIFY COLUMN val1 VARCHAR(64) REPLACE DEFAULT "abc";
  76. 9. Reorder the columns in example_rollup_index (set the original column order: k1, k2, k3, v1, v2)
  77. ALTER TABLE example_db.my_table
  78. ORDER BY (k3, k1, k2, v2, v1)
  79. FROM example_rollup_index;
  80. 10. Perform both operations simultaneously
  81. ALTER TABLE example_db.my_table
  82. ADD COLUMN v2 INT MAX DEFAULT "0" AFTER k2 TO example_rollup_index,
  83. ORDER BY (k3,k1,k2,v2,v1) FROM example_rollup_index;
  84. 11. Modify the bloom filter column of the table
  85. ALTER TABLE example_db.my_table SET ("bloom_filter_columns"="k1,k2,k3");
  86. Can also be merged into the above schema change operation (note that the syntax of multiple clauses is slightly different)
  87. ALTER TABLE example_db.my_table
  88. DROP COLUMN col2
  89. PROPERTIES ("bloom_filter_columns"="k1,k2,k3");
  90. 12. Modify the Colocate property of the table
  91. ALTER TABLE example_db.my_table set ("colocate_with" = "t1");
  92. 13. Change the bucketing mode of the table from Random Distribution to Hash Distribution
  93. ALTER TABLE example_db.my_table set ("distribution_type" = "hash");
  94. 14. Modify the dynamic partition properties of the table (support adding dynamic partition properties to tables without dynamic partition properties)
  95. ALTER TABLE example_db.my_table set ("dynamic_partition_enable" = "false");
  96. If you need to add dynamic partition attributes to a table without dynamic partition attributes, you need to specify all dynamic partition attributes
  97. 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");
  98. 15. Modify the in_memory property of the table
  99. ALTER TABLE example_db.my_table set ("in_memory" = "true");
  100. [rename]
  101. 1. Modify the table named table1 to table2
  102. ALTER TABLE table1 RENAME table2;
  103. 2. Modify the rollup index named rollup1 in the table example_table to rollup2
  104. ALTER TABLE example_table RENAME ROLLUP rollup1 rollup2;
  105. 3. Modify the partition named p1 in the table example_table to p2
  106. ALTER TABLE example_table RENAME PARTITION p1 p2;
  107. [index]
  108. 1. create index on table1 column siteid using bitmap
  109. ALTER TABLE table1 ADD INDEX index_name [USING BITMAP] (siteid) COMMENT 'balabala';
  110. 2. drop bitmap index of table1
  111. ALTER TABLE table1 DROP INDEX index_name;

keyword

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