MySQL的binary类型操作
示例数据表:
CREATE TABLE test_bin ( bin_id BINARY(16) NOT NULL ) Engine=InnoDB;
插入数据(内容是一个32位的UUID字符串值):
INSERT INTO test_bin(bin_id) VALUES(UNHEX(‘FA34E10293CB42848573A4E39937F479‘)); INSERT INTO test_bin(bin_id) VALUES(UNHEX(?)); 或 INSERT INTO test_bin(bin_id) VALUES(x‘FA34E10293CB42848573A4E39937F479‘);
查询数据:
SELECT HEX(bin_id) AS bin_id FROM test_bin; SELECT HEX(bin_id) AS bin_id FROM test_bin WHERE bin_id = UNHEX(‘FA34E10293CB42848573A4E39937F479‘); SELECT HEX(bin_id) AS bin_id FROM test_bin WHERE bin_id = UNHEX(?); SELECT HEX(bin_id) AS bin_id FROM test_bin WHERE bin_id = x‘FA34E10293CB42848573A4E39937F479‘; 查询结果: bin_id -------------------------- FA34E10293CB42848573A4E39937F479
备注:使用MySQL内置的 UUID() 创建一个函数返回 BINARY(16)类型的UUID值
CREATE FUNCTION uu_id() RETURNS binary(16) RETURN UNHEX(REPLACE(UUID(),‘-‘,‘‘)); 或 CREATE FUNCTION uu_id() RETURNS binary(16) RETURN UNHEX(REVERSE(REPLACE(UUID(),‘-‘,‘‘))); 使用: INSERT INTO test_bin(bin_id) VALUES(uu_id());
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。