All Articles

MySQL에서 VARCHAR 길이를 늘리기

The number of length bytes required by a VARCHAR column must remain the same. For VARCHAR columns of 0 to 255 bytes in size, one length byte is required to encode the value. For VARCHAR columns of 256 bytes in size or more, two length bytes are required. As a result, in-place ALTER TABLE only supports increasing VARCHAR column size from 0 to 255 bytes, or from 256 bytes to a greater size. In-place ALTER TABLE does not support increasing the size of a VARCHAR column from less than 256 bytes to a size equal to or greater than 256 bytes. In this case, the number of required length bytes changes from 1 to 2, which is only supported by a table copy (ALGORITHM=COPY). For example, attempting to change VARCHAR column size for a single byte character set from VARCHAR(255) to VARCHAR(256) using in-place ALTER TABLE returns this error:

ALTER TABLE tbl_name ALGORITHM=INPLACE, CHANGE COLUMN c1 c1 VARCHAR(256);
ERROR 0A000: ALGORITHM=INPLACE is not supported. Reason: Cannot change
column type INPLACE. Try ALGORITHM=COPY.
  • VARCHAR 컬럼은 문자열의 길이는 나타내는 바이트가 필요하다.
  • 문자열의 길이가 0-255라면 1바이트로 나타낼 수 있지만, 그 이상은 2바이트가 필요하다.
  • 따라서 0-255 글자 -> 0-255 글자로 변환은 메타데이터만 변경하면 되지만,
  • 0-255 글자 -> 256 글자 이상으로의 변환은 테이블 리빌드가 필요하다. (물론 테이블 락도 같이^^)

아무튼.. 무중단으로 ALTER TABLE 진행하는 DDL 이다.

ALTER TABLE <table_name> 
MODIFY <column_name> 
VARCHAR(1000) NULL, 
ALGORITHM=INPLACE, LOCK=NONE;