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

前端 未结 3 577
北恋
北恋 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:35

    I had exactly the same problem.

    I added a new char(32) column (I called it hash and added a unique index on it) and two triggers.

    delimiter | 
    
    CREATE TRIGGER insert_set_hash
        BEFORE INSERT ON my_table_name
        FOR EACH ROW BEGIN  
              SET NEW.hash = MD5(NEW.my_text);
        END; |
    
    CREATE TRIGGER update_set_hash
        BEFORE UPDATE ON my_table_name
        FOR EACH ROW BEGIN  
               SET NEW.hash = MD5(NEW.my_text);
        END; |
    
    
    delimiter ;
    

    By using the triggers and adding a UNIQUE index on hash you can be sure that the hash values will always be up-to-date and unique.

提交回复
热议问题