Updated on 2023-01-31 GMT+08:00

Online DDL

DDM supports online DDL operations, including adding, deleting, or modifying fields, setting default values, and modifying character sets and table names.

Online DDL extended syntax provides an explicit statement about what algorithm and lock will be used and can transparently transmit the statement to data nodes. This function is available only to DDM 3.1.0 or later.

If your DDM instance is associated with a MySQL 5.7 instance, Online DDL supports the following syntax:

ALTER TABLE tbl_name
    [alter_option [, alter_option] ...]

alter_option: {
  | ADD [COLUMN] col_name column_definition
        [FIRST | AFTER col_name]
  | ADD [COLUMN] (col_name column_definition,...)
  | DROP [COLUMN] col_name
  | ALTER [COLUMN] col_name {
        SET DEFAULT {literal | (expr)}
      | DROP DEFAULT
    }
  | CHANGE [COLUMN] old_col_name new_col_name column_definition
        [FIRST | AFTER col_name]
  | [DEFAULT] CHARACTER SET [=] charset_name [COLLATE [=] collation_name]
  | CONVERT TO CHARACTER SET charset_name [COLLATE collation_name]
  | MODIFY [COLUMN] col_name column_definition
        [FIRST | AFTER col_name]
  | RENAME [TO | AS] new_tbl_name

  | ALGORITHM [=] {DEFAULT | INPLACE | COPY}
  | LOCK [=] {DEFAULT | NONE | SHARED | EXCLUSIVE}
}

If your DDM instance is associated with a MySQL 8.0 instance, Online DDL supports the following syntax:

ALTER TABLE tbl_name
    [alter_option [, alter_option] ...]

alter_option: {
  | ADD [COLUMN] col_name column_definition
        [FIRST | AFTER col_name]
  | ADD [COLUMN] (col_name column_definition,...)
  | DROP [COLUMN] col_name
  | ALTER [COLUMN] col_name {
        SET DEFAULT {literal | (expr)}
      | DROP DEFAULT
    }
  | CHANGE [COLUMN] old_col_name new_col_name column_definition
        [FIRST | AFTER col_name]
  | [DEFAULT] CHARACTER SET [=] charset_name [COLLATE [=] collation_name]
  | CONVERT TO CHARACTER SET charset_name [COLLATE collation_name]
  | MODIFY [COLUMN] col_name column_definition
        [FIRST | AFTER col_name]
  | RENAME COLUMN old_col_name TO new_col_name
  | RENAME [TO | AS] new_tbl_name

  | ALGORITHM [=] {DEFAULT | INSTANT | INPLACE | COPY}
  | LOCK [=] {DEFAULT | NONE | SHARED | EXCLUSIVE}
}

If you use Online DDL syntax, the selected algorithm and lock will take effect on your data node. If there are multiple data nodes, DDM may not handle concurrent requests as specified by parameters.

Syntax Examples

Adding a column

# Add column x of the INT type to table t2 using the in-place algorithm and lock NONE.
ALTER TABLE t2 ADD COLUMN x INT, ALGORITHM=INPLACE, LOCK=NONE;

Modifying a column

# Modify the data type of column x to VARCHAR(255) for table t2 using the copy algorithm and a shared lock.
ALTER TABLE t2 MODIFY x VARCHAR(255), ALGORITHM=COPY, LOCK=SHARED;

Modifying a character set

# Change the character set and collation of table t2 to utf8 and utf8_bin using the copy algorithm and a shared lock.
ALTER TABLE t2 CHARACTER SET utf8 COLLATE utf8_bin, ALGORITHM=COPY, LOCK=SHARED;