生成的列允许的ALTER TABLE operations permitted for generated columns are ADD, MODIFY, and CHANGE.ALTER TABLE操作包括ADD、MODIFY和CHANGE。
Generated columns can be added.可以添加生成的列。
CREATE TABLE t1 (c1 INT); ALTER TABLE t1 ADD COLUMN c2 INT GENERATED ALWAYS AS (c1 + 1) STORED;
The data type and expression of generated columns can be modified.可以修改生成列的数据类型和表达式。
CREATE TABLE t1 (c1 INT, c2 INT GENERATED ALWAYS AS (c1 + 1) STORED); ALTER TABLE t1 MODIFY COLUMN c2 TINYINT GENERATED ALWAYS AS (c1 + 5) STORED;
Generated columns can be renamed or dropped, if no other column refers to them.如果没有其他列引用生成的列,则可以重命名或删除这些列。
CREATE TABLE t1 (c1 INT, c2 INT GENERATED ALWAYS AS (c1 + 1) STORED); ALTER TABLE t1 CHANGE c2 c3 INT GENERATED ALWAYS AS (c1 + 1) STORED; ALTER TABLE t1 DROP COLUMN c3;
Virtual generated columns cannot be altered to stored generated columns, or vice versa. 虚拟生成的列不能更改为存储的生成列,反之亦然。To work around this, drop the column, then add it with the new definition.若要解决此问题,请删除该列,然后将其添加到新定义中。
CREATE TABLE t1 (c1 INT, c2 INT GENERATED ALWAYS AS (c1 + 1) VIRTUAL); ALTER TABLE t1 DROP COLUMN c2; ALTER TABLE t1 ADD COLUMN c2 INT GENERATED ALWAYS AS (c1 + 1) STORED;
Nongenerated columns can be altered to stored but not virtual generated columns.非生成列可以更改为存储列,但不能更改为虚拟生成列。
CREATE TABLE t1 (c1 INT, c2 INT); ALTER TABLE t1 MODIFY COLUMN c2 INT GENERATED ALWAYS AS (c1 + 1) STORED;
Stored but not virtual generated columns can be altered to nongenerated columns. 存储但非虚拟生成的列可以更改为非生成列。The stored generated values become the values of the nongenerated column.存储的生成值将成为非生成列的值。
CREATE TABLE t1 (c1 INT, c2 INT GENERATED ALWAYS AS (c1 + 1) STORED); ALTER TABLE t1 MODIFY COLUMN c2 INT;
ADD COLUMN is not an in-place operation for stored columns (done without using a temporary table) because the expression must be evaluated by the server. ADD COLUMN不是存储列的就地操作(在不使用临时表的情况下完成),因为表达式必须由服务器计算。For stored columns, indexing changes are done in place, and expression changes are not done in place. 对于存储列,索引更改就地完成,表达式更改未就地完成。Changes to column comments are done in place.对列注释的更改已到位。
For non-partitioned tables, 对于非分区表,ADD COLUMN and DROP COLUMN are in-place operations for virtual columns. ADD COLUMN和DROP COLUMN是虚拟列的就地操作。However, adding or dropping a virtual column cannot be performed in place in combination with other 但是,添加或删除虚拟列不能与其他ALTER TABLE operations.ALTER TABLE操作一起就地执行。
For partitioned tables, 对于分区表,ADD COLUMN and DROP COLUMN are not in-place operations for virtual columns.ADD COLUMN和DROP COLUMN不是虚拟列的就地操作。
InnoDB supports secondary indexes on virtual generated columns. InnoDB支持虚拟生成列上的辅助索引。Adding or dropping a secondary index on a virtual generated column is an in-place operation. 在虚拟生成的列上添加或删除辅助索引是一项就地操作。For more information, see Section 13.1.20.9, “Secondary Indexes and Generated Columns”.有关更多信息,请参阅第13.1.20.9节,“二级索引和生成的列”。
When a 向表中添加或修改VIRTUAL generated column is added to a table or modified, it is not ensured that data being calculated by the generated column expression is be out of range for the column. VIRTUAL生成的列时,无法确保由生成的列表达式计算的数据超出该列的范围。This can lead to inconsistent data being returned and unexpectedly failed statements. 这可能会导致返回不一致的数据和意外失败的语句。To permit control over whether validation occurs for such columns, 为了控制是否对这些列进行验证,ALTER TABLE supports WITHOUT VALIDATION and WITH VALIDATION clauses:ALTER TABLE支持WITHOUT VALIDATION子句和WITH VALIDATION子句:
With 使用WITHOUT VALIDATION (the default if neither clause is specified), an in-place operation is performed (if possible), data integrity is not checked, and the statement finishes more quickly. WITHOUT VALIDATION(如果未指定任何一个子句,则为默认值),执行就地操作(如果可能),不检查数据完整性,并且语句完成得更快。However, later reads from the table might report warnings or errors for the column if values are out of range.但是,如果值超出范围,以后从表中读取可能会报告列的警告或错误。
With 使用WITH VALIDATION, ALTER TABLE copies the table. WITH VALIDATION,ALTER TABLE复制表。If an out-of-range or any other error occurs, the statement fails. 如果出现超出范围或任何其他错误,则语句将失败。Because a table copy is performed, the statement takes longer.因为执行了表复制,所以语句需要更长的时间。
仅允许使用WITHOUT VALIDATION and WITH VALIDATION are permitted only with ADD COLUMN, CHANGE COLUMN, and MODIFY COLUMN operations. ADD COLUMN、CHANGE COLUMN和MODIFY COLUMN操作进行WITHOUT VALIDATION和WITH VALIDATION。Otherwise, an 否则,将发生ER_WRONG_USAGE error occurs.ER_WRONG_USAGE错误。
If expression evaluation causes truncation or provides incorrect input to a function, the 如果表达式求值导致截断或向函数提供不正确的输入,ALTER TABLE statement terminates with an error and the DDL operation is rejected.ALTER TABLE语句将以错误终止,DDL操作将被拒绝。
An 更改列ALTER TABLE statement that changes the default value of a column col_name may also change the value of a generated column expression that refers to the column using col_name, which may change the value of a generated column expression that refers to the column using DEFAULT(. col_name)col_name的默认值的ALTER TABLE语句也可能会更改使用col_name引用该列的生成列表达式的值,这可能会更改使用DEFAULT(引用该列的生成列表达式的值。col_name)For this reason, 因此,如果任何生成的列表达式使用ALTER TABLE operations that change the definition of a column cause a table rebuild if any generated column expression uses DEFAULT().DEFAULT(),则更改列定义的ALTER TABLE操作将导致表重建。