mysql

create table with COLLATE

wefree 2023. 5. 4. 16:08

문제

테이블 생성시 query column 을 만들고, query column 으로 unique index 를 생성했다.

그런데 query 에  courreges, courrèges  를 입력시, 아래와 같이 에러가 발생했다.

rpc error: code = AlreadyExists desc = Duplicate entry '24-courrèges' for key ...

 

원인

https://stackoverflow.com/questions/6466901/mysql-distinction-between-e-and-%c3%a9-e-acute-unique-index 에 나온 것 처럼 eè가 mysql 에서 동일한 문자로 취급되었기 때문이다.

 

해결

기존 데이터의 COLLATE 변경

ALTER TABLE my_table CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_bin;

신규 테이블 생성시 COLLATE utf8mb4_bin 지정

create table my_table
(
    id           BIGINT          NOT NULL AUTO_INCREMENT COMMENT '일련 번호',   
    query        varchar(128)    NOT NULL COMMENT '질의 문자열',
    sample       enum ('Y', 'N') NOT NULL COMMENT '샘플 질의 여부',
    created_at   datetime        NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '생성시각',
    updated_at   timestamp       NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '수정시각',
    PRIMARY KEY (id),
    UNIQUE KEY id_query_index (id, query),
    KEY id_sample_index (id, sample),
    FULLTEXT INDEX query_index (query) WITH PARSER NGRAM
) ENGINE = InnoDB
  DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin COMMENT='질의 정보';