MySQL: How to alter varchar(255) UNIQUE column to UNIQUE Text NOT NULL?

前端 未结 3 614
北恋
北恋 2021-01-13 06:02

The current column is a VARCHAR(255) NOT NULL, so how do I change it to TEXT NOT NULL?

NOTE: The column intended to be changed its property type is a UNIQUE KEY comb

3条回答
  •  清歌不尽
    2021-01-13 06:22

    Are you going to use TEXT column as part of UNIQUE KEY? It's VERY inefficient! Don't do that! I'm strongly suggest you to:

    • Add additional column named for example 'description_hash' char(32) not null default ''
    • Store the hash-value for description field into it. For ex. description_hash=MD5(description)
    • Change your key to UNIQUE KEY (name, description_hash)

    Ofcourse you'll need to keep the description_hash column up-to-date in your code, but as see - in most cases it is require just few code changes. Or you can use trigger for handling this.

提交回复
热议问题