VARCHAR 字段的大小能夠通過 ALTER TABLE,命令,以in-place 的方式修改, 例如 :
ALTER TABLE t1 ALGORITHM=INPLACE, CHANGE COLUMN c1 c1 VARCHAR(255);
This is true as long as the number of length bytes required by a VARCHAR column remains the same.只要修改字段后字段varchar所占字節(jié)數(shù)和原先的相同就能實(shí)現(xiàn),例如對(duì)于 VARCHAR 值在 0到 255,只需要一個(gè)bytes. 對(duì)于 VARCHAR 的值是 256 bytes 或者大于256 需要兩個(gè)字節(jié).這樣的話,通過 in-place ALTER TABLE 只支持0到255 之間的修改,或者說256 以及大于256之間修改.in-place alter table 不支持小于256的varchar值變更為大于256的值。因?yàn)樵谶@種情況下存儲(chǔ)的字節(jié)會(huì)從1個(gè)字節(jié)變?yōu)閮蓚€(gè)字節(jié)。只能通algorithm=copy的方式修改,例如將varchar (255)的值修改到256 in-place alter would 會(huì)返回一個(gè)錯(cuò)誤
ALTER TABLE t1 ALGORITHM=INPLACE, CHANGE COLUMN c1 c1 VARCHAR(256);
ERROR 0A000: ALGORITHM=INPLACE is not supported. Reason: Cannot change
column type INPLACE. Try ALGORITHM=COPY.
這里需要著重說明的一點(diǎn)是需要針對(duì)不同的字符集來對(duì)應(yīng)如果是英文 0-255 隨便修改如果是其它字符集那么就需要注意了因?yàn)椴煌址即鎯?chǔ)位不同這里拿中文做演示。
CREATE TABLE `t1` (
`name` varchar(10) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
insert into t1 values ('hu')
commit; mysql> ALTER TABLE t1 ALGORITHM=INPLACE, CHANGE COLUMN name name VARCHAR(100); ERROR 1846 (0A000): ALGORITHM=INPLACE is not supported. Reason: Cannot change column type INPLACE. Try ALGORITHM=COPY.
mysql> ALTER TABLE t1 ALGORITHM=INPLACE, CHANGE COLUMN name name VARCHAR(86); ERROR 1846 (0A000): ALGORITHM=INPLACE is not supported. Reason: Cannot change column type INPLACE. Try ALGORITHM=COPY. mysql>
mysql> ALTER TABLE t1 ALGORITHM=INPLACE, CHANGE COLUMN name name VARCHAR(85); Query OK, 0 rows affected (0.00 sec) Records: 0 Duplicates: 0 Warnings: 0