Updated on 2024-10-09 GMT+08:00

ALTER COLUMN

Function

The ALTER TABLE ... ALTER COLUMN command is used to change the attributes of a column, such as the column type, position, and comment.

Syntax

ALTER TABLE Table name ALTER

[COLUMN] col_old_name TYPE column_type

[COMMENT] col_comment

[FIRST|AFTER] column_name

Parameter Description

Table 1 ALTER COLUMN parameters

Parameter

Description

tableName

Table name.

col_old_name

Name of the column to be altered.

column_type

Type of the target column.

col_comment

Column comment.

column_name

New position to place the target column. For example, AFTER column_name indicates that the target column is placed after column_name.

Example

  • Changing the column type
    ALTER TABLE table1 ALTER COLUMN a.b.c TYPE bigint

    a.b.c indicates the full path of a nested column. For details about the nested column rules, see ADD COLUMNS.

    The following changes on column types are supported:

    • int => long/float/double/string/decimal
    • long => float/double/string/decimal
    • float => double/String/decimal
    • From double to string or decimal
    • From decimal to decimal or string
    • From string to date or decimal
    • From date to string
  • Altering other attributes
    ALTER TABLE table1 ALTER COLUMN a.b.c DROP NOT NULL
    ALTER TABLE table1 ALTER COLUMN a.b.c COMMENT 'new comment'
    ALTER TABLE table1 ALTER COLUMN a.b.c FIRST
    ALTER TABLE table1 ALTER COLUMN a.b.c AFTER x

    a.b.c indicates the full path of a nested column. For details about the nested column rules, see ADD COLUMNS.

Response

You can run the DESCRIBE command to view the modified column.