基于各种建议,例如哈希密码字段使用什么数据类型以及长度是多少?,我可以将 md5 存储为 CHAR(32) 或 BINARY(16)。但是当我使用 BINARY(16) 这样做时,存储的值与 CHAR(32) 存储结果的前 16 个字符以及SELECT MD5()
结果的前 16 个字符相同。后面的 16 个字符有什么意义,它们在二进制列中的缺失是否会导致数据丢失?
CREATE TABLE test (id INT NOT NULL AUTO_INCREMENT, value VARCHAR(6), md5_char CHAR(32) NOT NULL, md5_binary BINARY(16) NOT NULL, PRIMARY KEY (id)) ENGINE = InnoDB;
INSERT INTO test(value,md5_char,md5_binary) VALUES("one!",md5("one!"),md5("one!"));
INSERT INTO test(value,md5_char,md5_binary) VALUES("two%",md5("two%"),md5("two%"));
INSERT INTO test(value,md5_char,md5_binary) VALUES("three~",md5("three~"),md5("three~"));
SELECT value,md5(value),md5_char,md5_binary FROM test;
DROP TABLE test;
+--------+----------------------------------+----------------------------------+------------------+
| value | md5(value) | md5_char | md5_binary |
+--------+----------------------------------+----------------------------------+------------------+
| one! | 633c8403325f1cf963809e6eb224d77e | 633c8403325f1cf963809e6eb224d77e | 633c8403325f1cf9 |
| two% | 48bbec047b4451a2018e0f652807b7d0 | 48bbec047b4451a2018e0f652807b7d0 | 48bbec047b4451a2 |
| three~ | fee453bb4eb68dcdfee07575e75c8cc5 | fee453bb4eb68dcdfee07575e75c8cc5 | fee453bb4eb68dcd |
+--------+----------------------------------+----------------------------------+------------------+